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