]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/ViewerWidgets/imageplanewidget.cxx
#3109 creaMaracasVisu Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaMaracasVisu.git] / lib / Kernel / VTKObjects / ViewerWidgets / imageplanewidget.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "imageplanewidget.h"
27
28 #include "vtkSmartPointer.h"
29 #include "vector"
30 #include "vtkImageActor.h"
31 #include "vtkImageAppendComponents.h"
32 #include "vtkRenderWindowInteractor.h"
33 #include "vtkTextProperty.h"
34
35 using namespace std;
36
37 ImagePlaneWidget::ImagePlaneWidget()
38 {
39     _img = 0;
40     _xwidget = 0;
41     _ywidget = 0;
42     _zwidget = 0;
43     m_Interactor = 0;
44     _imageactorx = 0;
45     _imageactory = 0;
46     _imageactorz = 0;
47 }
48
49 void ImagePlaneWidget::initialize(vtkRenderWindowInteractor* interactor){
50     m_Interactor = interactor;
51     // Plane widget
52     _xwidget = vtkImagePlaneWidget::New( );
53     _ywidget = vtkImagePlaneWidget::New( );
54     _zwidget = vtkImagePlaneWidget::New( );
55
56
57     _xwidget->SetInteractor( interactor );
58     _ywidget->SetInteractor( interactor );
59     _zwidget->SetInteractor( interactor );
60
61 }
62 bool ImagePlaneWidget::showImageActors(bool show){
63     if(show && m_Interactor){
64         if(!_imageactorx){
65             initializeImageActors();
66         }
67         m_Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->AddViewProp(_imageactorx);
68         m_Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->AddViewProp(_imageactory);
69         m_Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->AddViewProp(_imageactorz);
70         return true;
71     }else if(_imageactorx){
72         m_Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->RemoveViewProp(_imageactorx);
73         m_Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->RemoveViewProp(_imageactory);
74         m_Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->RemoveViewProp(_imageactorz);
75         return true;
76     }
77
78     return false;
79 }
80
81 void ImagePlaneWidget::initializeImageActors(){
82
83     //cout<<w_ext[0]<<" "<<w_ext[1]<<" "<<w_ext[2]<<" "<<w_ext[3]<<" "<<w_ext[4]<<" "<<w_ext[5]<<" "<<endl;
84     _imageactorx = vtkImageActor::New();
85
86 //EED 2017-01-01 Migration VTK7
87 #if VTK_MAJOR_VERSION <= 5
88     int *w_ext = _img->GetWholeExtent();
89     _imageactorx->SetInput(_img);
90 #else
91     int *w_ext = _img->GetExtent();
92     _imageactorx->SetInputData(_img);
93 #endif
94
95     xslice =  (w_ext[4] + w_ext[5])/2;
96     _imageactorx->SetDisplayExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], xslice, xslice);
97
98
99     _imageactory = vtkImageActor::New();
100
101 //EED 2017-01-01 Migration VTK7
102 #if VTK_MAJOR_VERSION <= 5
103     _imageactory->SetInput(_img);
104 #else
105     _imageactory->SetInputData(_img);
106 #endif
107
108     yslice =  (w_ext[2] + w_ext[3])/2;
109     _imageactory->SetDisplayExtent(w_ext[0], w_ext[1], yslice, yslice, w_ext[4], w_ext[5]);
110
111     _imageactorz = vtkImageActor::New();
112
113 //EED 2017-01-01 Migration VTK7
114 #if VTK_MAJOR_VERSION <= 5
115     _imageactorz->SetInput(_img);
116 #else
117     _imageactorz->SetInputData(_img);
118 #endif
119
120     zslice =  (w_ext[0] + w_ext[1])/2;
121     _imageactorz->SetDisplayExtent(zslice, zslice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
122
123
124 }
125
126 void ImagePlaneWidget::setImage(vtkImageData* img){
127     _img = img;
128     this->invariant();
129
130     int *dims = _img->GetDimensions( );
131     unsigned int sliceX = dims[0]/2;
132     unsigned int sliceY = dims[1]/2;
133     unsigned int sliceZ = dims[2]/2;
134
135     // Set input X,Y and Z plane
136     _xwidget->Off( );
137
138 //EED 2017-01-01 Migration VTK7
139 #if VTK_MAJOR_VERSION <= 5
140     _xwidget->SetInput( _img );
141 #else
142     _xwidget->SetInputData( _img );
143 #endif
144
145     _xwidget->SetPlaneOrientationToXAxes( );
146     _xwidget->SetSliceIndex( sliceX );
147     _xwidget->TextureInterpolateOff();
148     _xwidget->SetResliceInterpolateToLinear();
149     _xwidget->On( );
150     _xwidget->DisplayTextOn();
151     _xwidget->GetTextProperty()->SetColor(0.3, 0.3, 0.3);
152
153     _ywidget->Off( );
154
155 //EED 2017-01-01 Migration VTK7
156 #if VTK_MAJOR_VERSION <= 5
157     _ywidget->SetInput( _img );
158 #else
159     _ywidget->SetInputData( _img );
160 #endif
161
162     _ywidget->SetPlaneOrientationToYAxes( );
163     _ywidget->SetSliceIndex( sliceY );
164     _ywidget->TextureInterpolateOff();
165     _ywidget->SetResliceInterpolateToLinear();
166     _ywidget->DisplayTextOn();
167     _ywidget->GetTextProperty()->SetColor(0.3, 0.3, 0.3);
168     _ywidget->On( );
169
170     _zwidget->Off( );
171
172
173 //EED 2017-01-01 Migration VTK7
174 #if VTK_MAJOR_VERSION <= 5
175     _zwidget->SetInput( _img );
176 #else
177     _zwidget->SetInputData( _img );
178 #endif
179
180
181     _zwidget->SetPlaneOrientationToZAxes( );
182     _zwidget->SetSliceIndex( sliceZ );
183     _zwidget->TextureInterpolateOff();
184     _zwidget->SetResliceInterpolateToLinear();
185     _zwidget->DisplayTextOn();
186     _zwidget->On( );
187     _zwidget->GetTextProperty()->SetColor(0.3, 0.3, 0.3);
188 }
189
190 void ImagePlaneWidget::SetLookupTable(vtkLookupTable *lookuptable){
191     this->invariant();
192     _xwidget->SetLookupTable(lookuptable);
193     _ywidget->SetLookupTable(lookuptable);
194     _zwidget->SetLookupTable(lookuptable);
195 }
196
197 void ImagePlaneWidget::SetColorTable(vtkColorTransferFunction *colortransfer){
198     SetLookupTable((vtkLookupTable*)colortransfer);
199 }
200
201 void ImagePlaneWidget::showPlanes(bool show){
202     this->invariant();
203     if(show){
204         _xwidget->On();
205         _ywidget->On();
206         _zwidget->On();
207     }else{
208         _xwidget->Off();
209         _ywidget->Off();
210         _zwidget->Off();
211     }
212 }
213
214
215 void ImagePlaneWidget::invariant()
216 {
217     if(_img == NULL){
218         throw "Image data is not initialized";
219     }
220     if(!_xwidget ||!_ywidget || !_zwidget){
221         throw "The widgets are not initialized";
222     }
223 }
224
225
226 void ImagePlaneWidget::setSliceXImageActor(int value)
227 {
228 //EED 2017-01-01 Migration VTK7
229 #if VTK_MAJOR_VERSION <= 5
230     int *w_ext = _img->GetWholeExtent();
231 #else
232     int *w_ext = _img->GetExtent();
233 #endif
234
235     xslice = value;
236     _imageactorx->SetDisplayExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], xslice, xslice);
237     m_Interactor->Render();
238 }
239
240 void ImagePlaneWidget::setSliceYImageActor(int value)
241 {
242 //EED 2017-01-01 Migration VTK7
243 #if VTK_MAJOR_VERSION <= 5
244     int *w_ext = _img->GetWholeExtent();
245 #else
246     int *w_ext = _img->GetExtent();
247 #endif
248     yslice = value;
249     _imageactory->SetDisplayExtent(w_ext[0], w_ext[1], yslice, yslice, w_ext[4], w_ext[5]);
250     m_Interactor->Render();
251 }
252
253
254 void ImagePlaneWidget::setSliceZImageActor(int value)
255 {
256
257 //EED 2017-01-01 Migration VTK7
258 #if VTK_MAJOR_VERSION <= 5
259     int *w_ext = _img->GetWholeExtent();
260 #else
261     int *w_ext = _img->GetExtent();
262 #endif
263
264     zslice = value;
265     _imageactorz->SetDisplayExtent(zslice, zslice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
266     m_Interactor->Render();
267 }