]> Creatis software - crea.git/blob - src/creaVtkBasicSlicer.cxx
c027da12f98007685df5148b72bee5481142c96b
[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   outline->SetInput(I);
66   
67   vtkPolyDataMapper* outlineMapper = vtkPolyDataMapper::New();
68   outlineMapper->SetInputConnection(outline->GetOutputPort());
69   
70   vtkActor* outlineActor = vtkActor::New();
71   outlineActor->SetMapper(outlineMapper);
72   
73
74   // The shared picker enables us to use 3 planes at one time
75   // and gets the picking order right
76   vtkCellPicker* picker = vtkCellPicker::New();
77   picker->SetTolerance(0.005);
78   
79   // The 3 image plane widgets are used to probe the dataset.
80   vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
81   planeWidgetX->DisplayTextOn();
82   planeWidgetX->SetInput(I);
83   planeWidgetX->SetPlaneOrientationToXAxes();
84   planeWidgetX->SetSlicePosition((xMax+xMin)/2.);
85   planeWidgetX->SetPicker(picker);
86   planeWidgetX->SetKeyPressActivationValue('x');
87   vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
88   prop1->SetColor(1, 0, 0);
89
90   vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
91   planeWidgetY->DisplayTextOn();
92   planeWidgetY->SetInput(I);
93   planeWidgetY->SetPlaneOrientationToYAxes();
94   planeWidgetY->SetSlicePosition((yMax+yMin)/2.);
95   planeWidgetY->SetPicker(picker);
96   planeWidgetY->SetKeyPressActivationValue('y');
97   vtkProperty* prop2 = planeWidgetY->GetPlaneProperty();
98   prop2->SetColor(1, 1, 0);
99   planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
100
101   // for the z-slice, turn off texture interpolation:
102   // interpolation is now nearest neighbour, to demonstrate
103   // cross-hair cursor snapping to pixel centers
104   vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
105   planeWidgetZ->DisplayTextOn();
106
107 //EED 2017-01-01 Migration VTK7
108 #if (VTK_MAJOR_VERSION <= 5) 
109   planeWidgetZ->SetInput(I);
110 #endif
111 #if (VTK_MAJOR_VERSION >= 6) 
112   planeWidgetZ->SetInputData(I);
113 #endif
114
115
116   planeWidgetZ->SetPlaneOrientationToZAxes();
117   planeWidgetZ->SetSlicePosition((zMax+zMin)/2.);
118   planeWidgetZ->SetPicker(picker);
119   planeWidgetZ->SetKeyPressActivationValue('z');
120   vtkProperty* prop3 = planeWidgetZ->GetPlaneProperty();
121   prop3->SetColor(0, 0, 1);
122   planeWidgetZ->SetLookupTable(planeWidgetX->GetLookupTable());
123
124   planeWidgetZ->SetWindowLevel(512,256);
125
126   // Create the RenderWindow and Renderer
127   vtkRenderer* ren = vtkRenderer::New();
128   vtkRenderWindow* renWin = vtkRenderWindow::New();
129   renWin->AddRenderer(ren);
130   
131   // Add the outline actor to the renderer, set the background color and size
132   ren->AddActor(outlineActor);
133   renWin->SetSize(600, 600);
134   ren->SetBackground(0.1, 0.1, 0.2);
135   
136   // an interactor
137   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
138   iren->SetInteractorStyle( vtkInteractorStyleTrackballCamera::New() );
139   iren->SetRenderWindow(renWin);
140
141   // Set the interactor for the widgets
142   planeWidgetX->SetInteractor(iren);
143   planeWidgetX->On();
144   planeWidgetY->SetInteractor(iren);
145   planeWidgetY->On();
146   planeWidgetZ->SetInteractor(iren);
147   planeWidgetZ->On();
148
149   // Create an initial interesting view
150   vtkCamera* cam1 = ren->GetActiveCamera();
151   cam1->SetFocalPoint(0, 0, 0);
152   cam1->SetPosition(0, 0, -1500);
153   //cam1->OrthogonalizeViewUp();
154   cam1->Elevation(110);
155   cam1->SetViewUp(0, 0, -1);
156   cam1->Azimuth(45);
157   ren->ResetCameraClippingRange();
158
159   // Render it
160   //  render_widget->Render();
161
162   iren->Initialize();
163   renWin->Render();
164   iren->Start();
165
166   ren->Delete();  
167   renWin->Delete();
168   iren->Delete();
169   planeWidgetX->Delete();
170   planeWidgetY->Delete();
171   planeWidgetZ->Delete();
172   outline->Delete();
173   outlineMapper->Delete();
174   outlineActor->Delete();
175   picker->Delete();
176   }
177
178 }
179 #endif // USE_VTK