]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/SurfaceRenderer/wxMaracasSurfaceRenderingManagerDataMhd.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / Kernel / VTKObjects / SurfaceRenderer / wxMaracasSurfaceRenderingManagerDataMhd.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 "wxMaracasSurfaceRenderingManagerDataMhd.h"
27
28 #include "vtkStripper.h"
29
30
31 wxMaracasSurfaceRenderingManagerDataMhd::wxMaracasSurfaceRenderingManagerDataMhd(vtkImageData* imagedata, std::string dataname, vtkRenderWindowInteractor*  interactor)
32 : wxMaracasSurfaceRenderingManagerData(NULL, dataname)
33 {
34
35         this->setVtkImageData(imagedata);
36         _dataname = dataname;
37         
38         _maxgreylevel = getMaxLevel(imagedata);
39         _prop3D=NULL;                   
40
41         _tresholdFilter = vtkImageThreshold::New();
42         _tresholdFilter->SetInput(this->_imagedata);
43         _tresholdFilter->SetInValue(255);        
44         _tresholdFilter->SetOutValue(0);
45
46
47         _cubesFilter = vtkMarchingCubes::New(); 
48         _cubesFilter->SetInput(_tresholdFilter->GetOutput());
49         //_cubesFilter->ComputeGradientsOn();
50         _cubesFilter->ComputeScalarsOn();
51         _cubesFilter->ComputeNormalsOn();
52         _cubesFilter->SetNumberOfContours( 1 );
53         _cleanFilter = vtkCleanPolyData::New();         
54         _cleanFilter->SetInput ( _cubesFilter->GetOutput() );
55
56         _smooth = vtkSmoothPolyDataFilter::New();
57         _smooth->SetInput(_cleanFilter->GetOutput());
58         _smooth->SetNumberOfIterations(6);
59         _smooth->SetRelaxationFactor(0.3);
60         //_smooth->FeatureEdgeSmoothingOff();
61
62         _dataMapper = vtkPolyDataMapper::New( );
63         _dataMapper->ScalarVisibilityOff( );
64         _dataMapper->ImmediateModeRenderingOn();
65         vtkActor* dataActor = vtkActor::New();
66
67         //if(_boxWidgetS1){
68         if(interactor){
69             _boxWidgetS1 = vtkBoxWidget::New();
70             _boxWidgetS1->SetInteractor( interactor );
71             _boxWidgetS1->SetPlaceFactor(1.25);
72
73             _boxWidgetS1->SetInput( this->_imagedata );
74             _boxWidgetS1->PlaceWidget();
75             boxSurfaceObserver* observer = boxSurfaceObserver::New();
76
77             vtkStripper* striper = vtkStripper::New();
78             striper->SetInput( _smooth->GetOutput() );
79             //striper->SetInput( _cleanFilter->GetOutput() );
80
81             striper->Update();
82             _boxWidgetS1->SetInput(striper->GetOutput());
83             //_boxWidgetS1->PlaceWidget();
84
85
86             _tissuePlanes  = vtkPlanes::New();
87
88             int x1,x2,y1,y2,z1,z2;
89             this->_imagedata->GetExtent(x1,x2,y1,y2,z1,z2);
90             _tissuePlanes->SetBounds  (x1,x2,y1,y2,z1,z2);
91
92
93
94             _boxWidgetS1->GetPlanes( _tissuePlanes );
95
96             _tissueClipper = vtkClipPolyData::New();
97             _tissueClipper->SetInput( striper->GetOutput() );
98             _tissueClipper->SetClipFunction( _tissuePlanes );
99             _tissueClipper->InsideOutOn( );
100             _dataMapper->SetInput( _tissueClipper->GetOutput() );
101             observer->SetPlanes( _tissuePlanes );
102             observer->SetActor( dataActor );
103             _boxWidgetS1->AddObserver( vtkCommand::InteractionEvent              , observer );
104
105
106             _boxWidgetS1->HandlesOn ();
107             _boxWidgetS1->EnabledOff();
108         }else{          
109                 //_dataMapper->SetInput(_cleanFilter->GetOutput());
110             _dataMapper->SetInput(_smooth->GetOutput());
111         }
112         
113         dataActor->SetMapper(_dataMapper);      
114         float cr=1,cg=0.5,cb=0.5;
115         dataActor->GetProperty()->SetDiffuseColor(1,0.5,0.5   );
116         dataActor->GetProperty()->SetSpecular(.3);
117         dataActor->GetProperty()->SetSpecularPower(20);
118
119         this->_prop3D = dataActor;
120         
121
122         this->changeIsoValue(this->_maxgreylevel);      
123
124
125 }
126
127 void wxMaracasSurfaceRenderingManagerDataMhd::enableBoxWidget(bool enable){
128
129     if(_boxWidgetS1){
130         if(enable){
131             _boxWidgetS1->EnabledOn();
132         }else{
133             _boxWidgetS1->EnabledOff();
134         }
135     }else{
136         cout<<"box widget not initialized!"<<endl;
137     }
138 }
139
140
141 wxMaracasSurfaceRenderingManagerDataMhd::~wxMaracasSurfaceRenderingManagerDataMhd()
142 {
143         _cubesFilter->Delete();
144         _cleanFilter->Delete();
145         _dataMapper->Delete();
146 }
147         
148 void wxMaracasSurfaceRenderingManagerDataMhd::UpdateSurface()
149 {
150         _cubesFilter->Update();    
151         _cleanFilter->Update();
152         _dataMapper->Update();  
153 }
154 /**
155 **      changes the isovalue in a prop3D
156 **/
157 void wxMaracasSurfaceRenderingManagerDataMhd::changeIsoValue(double value){     
158                 
159    
160     _tresholdFilter->ThresholdByLower(value);
161     _tresholdFilter->Update();
162
163     _cubesFilter->SetValue(0,255);
164         _cubesFilter->Update();    
165         _cleanFilter->Update();
166         _dataMapper->Update();  
167         
168         
169         
170 }
171 /**
172 **      changes the isovalue in a prop3D
173 **/
174 void wxMaracasSurfaceRenderingManagerDataMhd::changeIsoValue(double min, double max){
175
176     _tresholdFilter->ThresholdBetween(min, max);
177     _tresholdFilter->Update();
178     _cubesFilter->SetValue(0,255);
179         _cubesFilter->Update();
180         _cleanFilter->Update();
181         _dataMapper->Update();
182
183
184
185 }
186 int wxMaracasSurfaceRenderingManagerDataMhd::getMaxGreyLevel(){
187         return _maxgreylevel;
188 }
189
190 /**
191         ** Get's the max grey level of the image
192         **/
193 int wxMaracasSurfaceRenderingManagerDataMhd::getMaxLevel(vtkImageData* img){
194
195         int ext[6], i, j, k,max=0;
196         img->GetExtent(ext);
197
198         for(i = ext[0]; i < ext[1];i++){
199                 for(j = ext[2]; j < ext[3];j++){
200                         for(k = ext[4]; k < ext[5];k++){
201                 unsigned short* ptr = (unsigned short*)img->GetScalarPointer(i,j,k);
202                                 int temp = (int)*ptr;
203                                 if(temp > max){
204                     max = temp;
205                                 }
206                         }
207                 }
208         }
209         return max;
210
211
212 }
213
214 /**
215 **      Sets the VTK image data
216 **/
217
218 void wxMaracasSurfaceRenderingManagerDataMhd::setVtkImageData(vtkImageData* imagedata){
219         _imagedata = imagedata;
220 }
221
222
223
224