]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/ViewerWidgets/imageplanewidget.cxx
3e044e99c19a4e7343687ef8938eb4e5625807f5
[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     int *w_ext = _img->GetWholeExtent();
84     //cout<<w_ext[0]<<" "<<w_ext[1]<<" "<<w_ext[2]<<" "<<w_ext[3]<<" "<<w_ext[4]<<" "<<w_ext[5]<<" "<<endl;
85     _imageactorx = vtkImageActor::New();
86     _imageactorx->SetInput(_img);
87     xslice =  (w_ext[4] + w_ext[5])/2;
88     _imageactorx->SetDisplayExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], xslice, xslice);
89
90
91     _imageactory = vtkImageActor::New();
92     _imageactory->SetInput(_img);
93     yslice =  (w_ext[2] + w_ext[3])/2;
94     _imageactory->SetDisplayExtent(w_ext[0], w_ext[1], yslice, yslice, w_ext[4], w_ext[5]);
95
96     _imageactorz = vtkImageActor::New();
97     _imageactorz->SetInput(_img);
98     zslice =  (w_ext[0] + w_ext[1])/2;
99     _imageactorz->SetDisplayExtent(zslice, zslice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
100
101
102 }
103
104 void ImagePlaneWidget::setImage(vtkImageData* img){
105     _img = img;
106     this->invariant();
107
108     int *dims = _img->GetDimensions( );
109     unsigned int sliceX = dims[0]/2;
110     unsigned int sliceY = dims[1]/2;
111     unsigned int sliceZ = dims[2]/2;
112
113     // Set input X,Y and Z plane
114     _xwidget->Off( );
115     _xwidget->SetInput( _img );
116     _xwidget->SetPlaneOrientationToXAxes( );
117     _xwidget->SetSliceIndex( sliceX );
118     _xwidget->TextureInterpolateOff();
119     _xwidget->SetResliceInterpolateToLinear();
120     _xwidget->On( );
121     _xwidget->DisplayTextOn();
122     _xwidget->GetTextProperty()->SetColor(0.3, 0.3, 0.3);
123
124     _ywidget->Off( );
125     _ywidget->SetInput( _img );
126     _ywidget->SetPlaneOrientationToYAxes( );
127     _ywidget->SetSliceIndex( sliceY );
128     _ywidget->TextureInterpolateOff();
129     _ywidget->SetResliceInterpolateToLinear();
130     _ywidget->DisplayTextOn();
131     _ywidget->GetTextProperty()->SetColor(0.3, 0.3, 0.3);
132     _ywidget->On( );
133
134     _zwidget->Off( );
135     _zwidget->SetInput( _img );
136     _zwidget->SetPlaneOrientationToZAxes( );
137     _zwidget->SetSliceIndex( sliceZ );
138     _zwidget->TextureInterpolateOff();
139     _zwidget->SetResliceInterpolateToLinear();
140     _zwidget->DisplayTextOn();
141     _zwidget->On( );
142     _zwidget->GetTextProperty()->SetColor(0.3, 0.3, 0.3);
143 }
144
145 void ImagePlaneWidget::SetLookupTable(vtkLookupTable *lookuptable){
146     this->invariant();
147     _xwidget->SetLookupTable(lookuptable);
148     _ywidget->SetLookupTable(lookuptable);
149     _zwidget->SetLookupTable(lookuptable);
150 }
151
152 void ImagePlaneWidget::SetColorTable(vtkColorTransferFunction *colortransfer){
153     SetLookupTable((vtkLookupTable*)colortransfer);
154 }
155
156 void ImagePlaneWidget::showPlanes(bool show){
157     this->invariant();
158     if(show){
159         _xwidget->On();
160         _ywidget->On();
161         _zwidget->On();
162     }else{
163         _xwidget->Off();
164         _ywidget->Off();
165         _zwidget->Off();
166     }
167 }
168
169
170 void ImagePlaneWidget::invariant(){
171     if(_img == NULL){
172         throw "Image data is not initialized";
173     }
174     if(!_xwidget ||!_ywidget || !_zwidget){
175         throw "The widgets are not initialized";
176     }
177 }
178
179
180 void ImagePlaneWidget::setSliceXImageActor(int value){
181     int *w_ext = _img->GetWholeExtent();
182     xslice = value;
183     _imageactorx->SetDisplayExtent(w_ext[0], w_ext[1], w_ext[2], w_ext[3], xslice, xslice);
184     m_Interactor->Render();
185 }
186 void ImagePlaneWidget::setSliceYImageActor(int value){
187     int *w_ext = _img->GetWholeExtent();
188     yslice = value;
189     _imageactory->SetDisplayExtent(w_ext[0], w_ext[1], yslice, yslice, w_ext[4], w_ext[5]);
190     m_Interactor->Render();
191 }
192 void ImagePlaneWidget::setSliceZImageActor(int value){
193     int *w_ext = _img->GetWholeExtent();
194     zslice = value;
195     _imageactorz->SetDisplayExtent(zslice, zslice, w_ext[2], w_ext[3], w_ext[4], w_ext[5]);
196     m_Interactor->Render();
197 }