]> Creatis software - crea.git/blob - src/creaVtkBasicSlicer.cxx
#3374 crea Bug New Normal - vtk8itk5wx3-mingw64
[crea.git] / src / creaVtkBasicSlicer.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ 
26 */                                                                         
27
28 #ifdef USE_VTK
29
30 #include <creaVtkBasicSlicer.h>
31
32 #include "vtkRenderWindow.h"
33 #include "vtkRenderWindowInteractor.h"
34
35 #include "vtkOutlineFilter.h"
36 #include "vtkPolyDataMapper.h"
37 #include "vtkActor.h"
38 #include "vtkImagePlaneWidget.h"
39 #include "vtkCellPicker.h"
40 #include "vtkProperty.h"
41 #include "vtkRenderer.h"
42 #include "vtkCamera.h"
43
44 #include "vtkInteractorStyleTrackballCamera.h"
45
46 namespace crea
47 {
48   
49   void VtkBasicSlicer( vtkImageData* I )
50   {
51
52     int xMin, xMax, yMin, yMax, zMin, zMax;
53   I->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
54
55   double sx, sy, sz;
56   I->GetSpacing(sx, sy, sz);
57
58   double ox, oy, oz;
59   I->GetOrigin(ox,oy,oz);
60
61   
62   // An outline is shown for context.
63   
64   vtkOutlineFilter* outline = vtkOutlineFilter::New();
65
66 //EED 2017-01-01 Migration VTK7
67 #if (VTK_MAJOR_VERSION <= 5) 
68   outline->SetInput(I);
69 #endif
70 #if (VTK_MAJOR_VERSION >= 6) 
71   outline->SetInputData(I);
72 #endif
73   
74   vtkPolyDataMapper* outlineMapper = vtkPolyDataMapper::New();
75   outlineMapper->SetInputConnection(outline->GetOutputPort());
76   
77   vtkActor* outlineActor = vtkActor::New();
78   outlineActor->SetMapper(outlineMapper);
79   
80
81   // The shared picker enables us to use 3 planes at one time
82   // and gets the picking order right
83   vtkCellPicker* picker = vtkCellPicker::New();
84   picker->SetTolerance(0.005);
85   
86   // The 3 image plane widgets are used to probe the dataset.
87   vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
88   planeWidgetX->DisplayTextOn();
89
90 //EED 2017-01-01 Migration VTK7
91 #if (VTK_MAJOR_VERSION <= 5) 
92   planeWidgetX->SetInput(I);
93 #endif
94 #if (VTK_MAJOR_VERSION >= 6) 
95   planeWidgetX->SetInputData(I);
96 #endif
97
98   planeWidgetX->SetPlaneOrientationToXAxes();
99   planeWidgetX->SetSlicePosition((xMax+xMin)/2.);
100   planeWidgetX->SetPicker(picker);
101   planeWidgetX->SetKeyPressActivationValue('x');
102   vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
103   prop1->SetColor(1, 0, 0);
104
105   vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
106   planeWidgetY->DisplayTextOn();
107
108 //EED 2017-01-01 Migration VTK7
109 #if (VTK_MAJOR_VERSION <= 5) 
110   planeWidgetY->SetInput(I);
111 #endif
112 #if (VTK_MAJOR_VERSION >= 6) 
113   planeWidgetY->SetInputData(I);
114 #endif
115
116   planeWidgetY->SetPlaneOrientationToYAxes();
117   planeWidgetY->SetSlicePosition((yMax+yMin)/2.);
118   planeWidgetY->SetPicker(picker);
119   planeWidgetY->SetKeyPressActivationValue('y');
120   vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
121   prop2->SetColor(1, 1, 0);
122   planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
123
124   // for the z-slice, turn off texture interpolation:
125   // interpolation is now nearest neighbour, to demonstrate
126   // cross-hair cursor snapping to pixel centers
127   vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
128   planeWidgetZ->DisplayTextOn();
129
130 //EED 2017-01-01 Migration VTK7
131 #if (VTK_MAJOR_VERSION <= 5) 
132   planeWidgetZ->SetInput(I);
133 #endif
134 #if (VTK_MAJOR_VERSION >= 6) 
135   planeWidgetZ->SetInputData(I);
136 #endif
137
138
139   planeWidgetZ->SetPlaneOrientationToZAxes();
140   planeWidgetZ->SetSlicePosition((zMax+zMin)/2.);
141   planeWidgetZ->SetPicker(picker);
142   planeWidgetZ->SetKeyPressActivationValue('z');
143   vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
144   prop3->SetColor(0, 0, 1);
145   planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
146
147   planeWidgetZ->SetWindowLevel(512,256);
148
149   // Create the RenderWindow and Renderer
150   vtkRenderer* ren = vtkRenderer::New();
151   vtkRenderWindow* renWin = vtkRenderWindow::New();
152   renWin->AddRenderer(ren);
153   
154   // Add the outline actor to the renderer, set the background color and size
155   ren->AddActor(outlineActor);
156   renWin->SetSize(600, 600);
157   ren->SetBackground(0.1, 0.1, 0.2);
158   
159   // an interactor
160   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
161   iren->SetInteractorStyle( vtkInteractorStyleTrackballCamera::New() );
162   iren->SetRenderWindow(renWin);
163
164   // Set the interactor for the widgets
165   planeWidgetX->SetInteractor(iren);
166   planeWidgetX->On();
167   planeWidgetY->SetInteractor(iren);
168   planeWidgetY->On();
169   planeWidgetZ->SetInteractor(iren);
170   planeWidgetZ->On();
171
172   // Create an initial interesting view
173   vtkCamera* cam1 = ren->GetActiveCamera();
174   cam1->SetFocalPoint(0, 0, 0);
175   cam1->SetPosition(0, 0, -1500);
176   //cam1->OrthogonalizeViewUp();
177   cam1->Elevation(110);
178   cam1->SetViewUp(0, 0, -1);
179   cam1->Azimuth(45);
180   ren->ResetCameraClippingRange();
181
182   // Render it
183   //  render_widget->Render();
184
185   iren->Initialize();
186   renWin->Render();
187   iren->Start();
188
189   ren->Delete();  
190   renWin->Delete();
191   iren->Delete();
192   planeWidgetX->Delete();
193   planeWidgetY->Delete();
194   planeWidgetZ->Delete();
195   outline->Delete();
196   outlineMapper->Delete();
197   outlineActor->Delete();
198   picker->Delete();
199   }
200
201 }
202 #endif // USE_VTK