]> Creatis software - crea.git/blob - src/creaVtkBasicSlicer.cxx
388644c16e9223235bd020b7ee31874ce8fc5fb3
[crea.git] / src / creaVtkBasicSlicer.cxx
1 #ifdef USE_VTK
2
3 #include <creaVtkBasicSlicer.h>
4
5 #include "vtkRenderWindow.h"
6 #include "vtkRenderWindowInteractor.h"
7
8 #include "vtkOutlineFilter.h"
9 #include "vtkPolyDataMapper.h"
10 #include "vtkActor.h"
11 #include "vtkImagePlaneWidget.h"
12 #include "vtkCellPicker.h"
13 #include "vtkProperty.h"
14 #include "vtkRenderer.h"
15 #include "vtkCamera.h"
16
17 namespace crea
18 {
19   
20   void VtkBasicSlicer( vtkImageData* I )
21   {
22
23     int xMin, xMax, yMin, yMax, zMin, zMax;
24   I->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
25
26   double sx, sy, sz;
27   I->GetSpacing(sx, sy, sz);
28
29   double ox, oy, oz;
30   I->GetOrigin(ox,oy,oz);
31
32   
33   // An outline is shown for context.
34   
35   vtkOutlineFilter* outline = vtkOutlineFilter::New();
36   outline->SetInput(I);
37   
38   vtkPolyDataMapper* outlineMapper = vtkPolyDataMapper::New();
39   outlineMapper->SetInputConnection(outline->GetOutputPort());
40   
41   vtkActor* outlineActor = vtkActor::New();
42   outlineActor->SetMapper(outlineMapper);
43   
44
45   // The shared picker enables us to use 3 planes at one time
46   // and gets the picking order right
47   vtkCellPicker* picker = vtkCellPicker::New();
48   picker->SetTolerance(0.005);
49   
50   // The 3 image plane widgets are used to probe the dataset.
51   vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
52   planeWidgetX->DisplayTextOn();
53   planeWidgetX->SetInput(I);
54   planeWidgetX->SetPlaneOrientationToXAxes();
55   planeWidgetX->SetSlicePosition((xMax+xMin)/2.);
56   planeWidgetX->SetPicker(picker);
57   planeWidgetX->SetKeyPressActivationValue('x');
58   vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
59   prop1->SetColor(1, 0, 0);
60
61   vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
62   planeWidgetY->DisplayTextOn();
63   planeWidgetY->SetInput(I);
64   planeWidgetY->SetPlaneOrientationToYAxes();
65   planeWidgetY->SetSlicePosition((yMax+yMin)/2.);
66   planeWidgetY->SetPicker(picker);
67   planeWidgetY->SetKeyPressActivationValue('y');
68   vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
69   prop2->SetColor(1, 1, 0);
70   planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
71
72   // for the z-slice, turn off texture interpolation:
73   // interpolation is now nearest neighbour, to demonstrate
74   // cross-hair cursor snapping to pixel centers
75   vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
76   planeWidgetZ->DisplayTextOn();
77   planeWidgetZ->SetInput(I);
78   planeWidgetZ->SetPlaneOrientationToZAxes();
79   planeWidgetZ->SetSlicePosition((zMax+zMin)/2.);
80   planeWidgetZ->SetPicker(picker);
81   planeWidgetZ->SetKeyPressActivationValue('z');
82   vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
83   prop3->SetColor(0, 0, 1);
84   planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
85
86   planeWidgetZ->SetWindowLevel(512,256);
87
88   // Create the RenderWindow and Renderer
89   vtkRenderer* ren = vtkRenderer::New();
90   vtkRenderWindow* renWin = vtkRenderWindow::New();
91   renWin->AddRenderer(ren);
92   
93   // Add the outline actor to the renderer, set the background color and size
94   ren->AddActor(outlineActor);
95   renWin->SetSize(600, 600);
96   ren->SetBackground(0.1, 0.1, 0.2);
97   
98   // an interactor
99   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
100   iren->SetRenderWindow(renWin);
101
102   // Set the interactor for the widgets
103   planeWidgetX->SetInteractor(iren);
104   planeWidgetX->On();
105   planeWidgetY->SetInteractor(iren);
106   planeWidgetY->On();
107   planeWidgetZ->SetInteractor(iren);
108   planeWidgetZ->On();
109
110   // Create an initial interesting view
111   vtkCamera* cam1 = ren->GetActiveCamera();
112   cam1->SetFocalPoint(0, 0, 0);
113   cam1->SetPosition(0, 0, -500);
114   //cam1->OrthogonalizeViewUp();
115   cam1->Elevation(110);
116   cam1->SetViewUp(0, 0, -1);
117   cam1->Azimuth(45);
118   ren->ResetCameraClippingRange();
119
120   // Render it
121   //  render_widget->Render();
122
123   iren->Initialize();
124   renWin->Render();
125   iren->Start();
126
127   ren->Delete();  
128   renWin->Delete();
129   iren->Delete();
130   planeWidgetX->Delete();
131   planeWidgetY->Delete();
132   planeWidgetZ->Delete();
133   outline->Delete();
134   outlineMapper->Delete();
135   outlineActor->Delete();
136   picker->Delete();
137   }
138
139 }
140 #endif // USE_VTK