]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/kernel/CutModelData.cxx
#3138 creaMaracasVisu Feature New Normal - branch vtk7itk4wx3
[creaMaracasVisu.git] / lib / maracasVisuLib / src / CutModule / kernel / CutModelData.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 /*=========================================================================
27
28   Program:   wxMaracas
29   Module:    $RCSfile: CutModelData.cxx,v $
30   Language:  C++
31   Date:      $Date: 2012/11/15 14:15:48 $
32   Version:   $Revision: 1.10 $
33
34   Copyright: (c) 2002, 2003
35   License:
36
37      This software is distributed WITHOUT ANY WARRANTY; without even
38      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
39      PURPOSE.  See the above copyright notice for more information.
40
41 =========================================================================*/
42 #include "CutModelData.h"
43 #include "vtkRenderWindow.h"
44 /**
45 **      Start of the manager class
46 **/
47 CutModelData::CutModelData(int id, vtkRenderWindowInteractor* interactor, vtkCommand* observer, vtkImageData* img){
48         
49         initializeData(id, interactor, observer, img);
50 }
51
52 CutModelData::CutModelData(){
53
54 }
55 void CutModelData::initializeData(int id, vtkRenderWindowInteractor* interactor, vtkCommand* observer, vtkImageData* img){
56         _id = id;
57         _currentshape=0;
58         createBoxWidget(interactor, observer);
59         setTransform(img);
60         createActor();
61         createShapes();
62         ChangeShape(0);
63         interactor->GetRenderWindow ()->Render();
64         checkInvariant();
65 }
66
67 CutModelData::~CutModelData(){
68         checkInvariant();
69         _boxWidgetVolume->RemoveAllObservers();
70         _boxWidgetVolume->Off();
71         _boxWidgetVolume->SetInteractor(NULL);
72         _boxWidgetVolume->Delete();     
73         _Mapper->Delete();      
74         _Actor->Delete();       
75         delete _cubefigure;
76         delete _cylinderfigure;
77         delete _spherefigure;
78         currentmatrix->Delete();
79         modeltransform->Delete();
80         inversModel->Delete();
81         _id = -1;
82 }
83
84 void CutModelData::RefreshViewBox(){
85                 
86 }
87 void CutModelData::createBoxWidget(vtkRenderWindowInteractor* interactor, vtkCommand* observer){
88         _boxWidgetVolume = vtkBoxWidget::New();
89         _boxWidgetVolume->SetInteractor( interactor );
90         //_boxWidgetVolume->SetPlaceFactor(1);
91         //_boxWidgetVolume->SetInput( img );    
92         //_boxWidgetVolume->PlaceWidget();
93
94         _boxWidgetVolume->AddObserver( vtkCommand::InteractionEvent              , observer );
95         _boxWidgetVolume->AddObserver( vtkCommand::StartInteractionEvent                 , observer );
96         _boxWidgetVolume->AddObserver( vtkCommand::RightButtonReleaseEvent               , observer );
97         
98         _boxWidgetVolume->HandlesOn ();
99         _boxWidgetVolume->On();
100         //_boxWidgetVolume->GetHandleProperty()->SetOpacity(0.5);       
101         //_boxWidgetVolume->GetOutlineProperty()->SetOpacity(0.5);      
102 }
103
104 void CutModelData::setTransform(vtkImageData* img)throw( CutModelException){
105         
106         currentmatrix = vtkTransform::New();
107         modeltransform = vtkTransform::New();
108         inversModel     = vtkTransform::New();    
109
110         vtkMatrix4x4* matrix = vtkMatrix4x4::New();  
111         matrix->Identity();
112         double *ori = img->GetOrigin();
113         int *ext = img->GetExtent();
114         double *spc = img->GetSpacing();
115
116         matrix->SetElement(0,0,(ext[1]-ext[0])/4*spc[0]);
117         matrix->SetElement(1,1,(ext[3]-ext[2])/4*spc[1]);       
118         matrix->SetElement(2,2,(ext[5]-ext[4])/4*spc[2]);
119
120         double orgx = (ext[1]+ext[0])/2.*spc[0];
121         double orgy = (ext[3]+ext[2])/2.*spc[1];
122         double orgz = (ext[5]+ext[4])/2.*spc[2];
123
124         matrix->SetElement(0,3,orgx);
125         matrix->SetElement(1,3,orgy);
126         matrix->SetElement(2,3,orgz);
127
128         currentmatrix->SetMatrix(matrix);
129         _boxWidgetVolume->SetTransform(currentmatrix);                  
130 }
131
132 void CutModelData::createActor(){       
133
134         _Mapper = vtkPolyDataMapper::New();     
135         _Actor  = vtkActor::New();
136         _Actor->SetMapper(_Mapper);     
137         _Actor->GetProperty()->SetColor(1, 0, 0);
138         _Actor->GetProperty()->SetOpacity(0.5);         
139 }
140
141 void CutModelData::udapteActorDirection()throw( CutModelException){
142         checkInvariant();
143         
144         _boxWidgetVolume->GetTransform(currentmatrix);          
145         _Actor->SetUserMatrix(currentmatrix->GetMatrix());//SetUserTransform(currentmatrix );
146 }
147
148 void CutModelData::createShapes(){
149         _cubefigure = new CutModelFigureCube();
150         _cylinderfigure = new CutModelFigureCylinder();
151         _spherefigure = new CutModelFigureSphere();
152 }
153
154 void CutModelData::changeOpacity(int opacity)throw( CutModelException){
155         checkInvariant();       
156         _Actor->GetProperty()->SetOpacity((double)opacity/100.0);
157 }
158
159 void CutModelData::ShowViewBox(bool check)throw( CutModelException){
160         checkInvariant();
161         if(check){
162                 _boxWidgetVolume->On();
163         }else{
164                 _boxWidgetVolume->Off();
165         }    
166 }
167
168 void CutModelData::ChangeShape(int selection)throw( CutModelException){
169         checkInvariant();
170         
171         if(selection == 0)
172         {
173 //EED 2017-01-01 Migration VTK7
174 #if VTK_MAJOR_VERSION <= 5
175                 _Mapper->SetInput(_spherefigure->getPolyData());
176 #else
177                 _Mapper->SetInputData(_spherefigure->getPolyData());
178 #endif
179         }else if(selection == 1){
180
181 #if VTK_MAJOR_VERSION <= 5
182                 _Mapper->SetInput(_cylinderfigure->getPolyData());
183 #else
184                 _Mapper->SetInputData(_cylinderfigure->getPolyData());
185 #endif
186
187         }else if(selection == 2){
188 //EED 2017-01-01 Migration VTK7
189 #if VTK_MAJOR_VERSION <= 5
190                 _Mapper->SetInput(_cubefigure->getPolyData());
191 #else
192                 _Mapper->SetInputData(_cubefigure->getPolyData());
193 #endif
194         }else{
195                 throw CutModelException("Shape type not found");
196         }
197         _currentshape=selection;
198 }
199
200 void CutModelData::checkInvariant()throw( CutModelException){
201         if(_boxWidgetVolume==NULL){
202                 throw CutModelException("Box Widget not created");
203         }
204         if(_Mapper==NULL){
205                 throw CutModelException("Mapper not created");
206         }
207         if(_Actor==NULL){
208                 throw CutModelException("Actor not created");
209         }
210         if(_cubefigure==NULL){
211                 throw CutModelException("Cube not created");
212         }
213         if(_cylinderfigure==NULL){
214                 throw CutModelException("Cylinder not created");
215         }
216         if(_spherefigure==NULL){
217                 throw CutModelException("Sphere not created");
218         }
219 }
220
221 vtkActor* CutModelData::getActor()throw( CutModelException){
222         checkInvariant();
223         return _Actor;
224 }
225
226 void CutModelData::changeColor(double r,double g,double b)throw( CutModelException){
227         checkInvariant();               
228         _Actor->GetProperty()->SetColor( r,g,b );       
229 }
230
231 CutModelFigure* CutModelData::getCurentCuttingModel(){
232     checkInvariant();
233         
234         if(_currentshape == 0){
235                 return _spherefigure;
236         }else if(_currentshape == 1){
237                 return _cylinderfigure;
238         }else if(_currentshape == 2){
239                 return _cubefigure;
240         }else{
241                 throw CutModelException("Shape type not found");
242         }       
243 }
244
245 void CutModelData::ExecuteCut( double* range, bool isinside, vtkImageData* copyimage)throw( CutModelException)
246 {
247         wxBusyCursor wait;
248
249         CutModelFigure* actualCuttingModel = getCurentCuttingModel();
250
251         //_boxWidgetVolume->GetTransform(currentmatrix);        
252         actualCuttingModel->SetVtkTransform(getModelTransform(copyimage));
253         //actualCuttingModel->SetVtkTransform(currentmatrix);
254         actualCuttingModel->SetInversVtkTransform(getModelTransformInvers());   
255
256         bool                    inside;
257         //bool                  volInt, volExt; // unused
258         int                     xx,yy,zz;
259         unsigned short*         pOrg;   
260         int                     ext[6];
261         //double                        spc[3]; // unused
262         //long int              contAfter = 0; // unused
263         //long int              contBefor = 0; // unused
264         
265         double minvalue = range[0]; 
266         double value    = range[1];
267         double maxvalue = range[2]; 
268         
269         copyimage->GetExtent(ext);        
270         
271         for (xx=ext[0];xx<=ext[1]; xx++)
272         {
273                 for (yy=ext[2];yy<=ext[3]; yy++)
274                 {
275                         for (zz=ext[4];zz<=ext[5];zz++)
276                         {
277                                 inside=actualCuttingModel->IfPointInside(xx,yy,zz);
278                                 if (  ((inside==true)&&(isinside==true)) || ((!inside==true)&&(!isinside)) )
279                                 {
280                                         pOrg=(unsigned short*)copyimage->GetScalarPointer (xx,yy,zz); 
281
282                                         //std::cout<<"xx,yy,zz "<<xx<<","<<yy<<","<<zz<<" "<<*pOrg<<std::endl;
283                                         if ((unsigned short)minvalue <=(*pOrg)&&(*pOrg)<=(unsigned short)maxvalue)
284                                         {       
285                                                 
286                                                 *pOrg=(unsigned short)value;
287                                                 
288                                         }                                       
289                                 } // if inside
290                         } // for zz
291                 } // for yy
292         } // for xx
293 }
294
295 void CutModelData::ExecuteUnCut(bool isinside, vtkImageData* image, vtkImageData* copyimage)throw( CutModelException)
296 {
297         wxBusyCursor wait;
298
299         CutModelFigure* actualCuttingModel = getCurentCuttingModel();
300
301         actualCuttingModel->SetVtkTransform(getModelTransform(copyimage));
302
303         actualCuttingModel->SetInversVtkTransform(getModelTransformInvers());   
304
305         bool                    inside;
306         // bool                 volInt, volExt; // unused // JPRx
307         int                     xx,yy,zz;
308         unsigned short* pOrg;   
309         int                     ext[6];
310         //double                spc[3]; // unused // JPRx
311         //long int              contAfter = 0; // unused // JPRx
312         //long int              contBefor = 0;   // unused // JPRx
313
314         double value;
315
316         copyimage->GetExtent(ext);        
317
318         for (xx=ext[0];xx<=ext[1]; xx++)
319         {
320                 for (yy=ext[2];yy<=ext[3]; yy++)
321                 {
322                         for (zz=ext[4];zz<=ext[5];zz++)
323                         {
324                                 inside=actualCuttingModel->IfPointInside(xx,yy,zz);
325                                 if (  ((inside==true)&&(isinside==true)) || ((!inside==true)&&(!isinside)) )
326                                 {
327                                         value = *((unsigned short*)image->GetScalarPointer (xx,yy,zz));
328                                         pOrg=(unsigned short*)copyimage->GetScalarPointer (xx,yy,zz); 
329                                         //std::cout<<"CutModelData::ExecuteUnCut() "<<value<<" "<<*pOrg<<std::endl;
330                                         *pOrg=(unsigned short)value;
331                                 } // if inside
332                         } // for zz
333                 } // for yy
334         } // for xx
335 }
336
337 vtkTransform* CutModelData::getModelTransform(vtkImageData* copyimage){
338
339         double *spc = copyimage->GetSpacing();
340         modeltransform->Identity();
341
342         vtkMatrix4x4* matrix = currentmatrix->GetMatrix();
343         vtkMatrix4x4* matrixmodel = modeltransform->GetMatrix();
344         matrixmodel->SetElement(0,0,matrix->GetElement(0,0)/spc[0]);
345         matrixmodel->SetElement(1,0,matrix->GetElement(1,0)/spc[0]);
346         matrixmodel->SetElement(2,0,matrix->GetElement(2,0)/spc[0]);
347         matrixmodel->SetElement(0,1,matrix->GetElement(0,1)/spc[1]);
348         matrixmodel->SetElement(1,1,matrix->GetElement(1,1)/spc[1]);
349         matrixmodel->SetElement(2,1,matrix->GetElement(2,1)/spc[1]);
350         matrixmodel->SetElement(0,2,matrix->GetElement(0,2)/spc[2]);
351         matrixmodel->SetElement(1,2,matrix->GetElement(1,2)/spc[2]);
352         matrixmodel->SetElement(2,2,matrix->GetElement(2,2)/spc[2]);            
353         matrixmodel->SetElement(0,3,matrix->GetElement(0,3)/spc[0]);
354         matrixmodel->SetElement(1,3,matrix->GetElement(1,3)/spc[1]);
355         matrixmodel->SetElement(2,3,matrix->GetElement(2,3)/spc[2]);    
356
357         /*
358         double* pos = currentmatrix->GetPosition();
359         modeltransform->Translate(pos[0]/spc[0],pos[1]/spc[1],pos[2]/spc[2]);   
360
361         double* scal = currentmatrix->GetScale();       
362         modeltransform->Scale(scal[0]/spc[0],scal[1]/spc[1],scal[2]/spc[2]);
363
364         //double* orient = currentmatrix->GetOrientation();
365         //modeltransform->RotateZ(orient[2]);   
366         //modeltransform->RotateY(orient[1]);   
367         //modeltransform->RotateX(orient[0]);   
368         double* orient = currentmatrix->GetOrientationWXYZ();   
369         modeltransform->RotateWXYZ(orient[0],orient[1],orient[2],orient[3]);    
370         */
371
372         modeltransform->Update();
373
374         return modeltransform;
375 }
376
377 void CutModelData::setTransform(vtkTransform* transform, vtkImageData* img)throw( CutModelException){
378         
379 //      double *spc = img->GetSpacing();  // spc unused // JPR
380
381         currentmatrix->Identity();      
382
383         /*double* orient = transform->GetOrientationWXYZ();
384         currentmatrix->RotateWXYZ(orient[0],orient[1],orient[2],orient[3]);     
385
386         double* pos = transform->GetPosition();
387         currentmatrix->Translate(pos[0]/spc[0],pos[1]/spc[1],pos[2]/spc[2]);
388
389         double* scal = transform->GetScale();   
390         currentmatrix->Scale(scal[0]/spc[0],scal[1]/spc[1],scal[2]/spc[2]);
391
392         currentmatrix->Update();        */
393
394         _boxWidgetVolume->SetTransform(transform);
395
396     /*vtkMatrix4x4* matrix = transform->GetMatrix();
397         vtkMatrix4x4* matrixcurrent = currentmatrix->GetMatrix();
398         matrixcurrent->SetElement(0,0,matrix->GetElement(0,0)/spc[0]);
399         matrixcurrent->SetElement(1,0,matrix->GetElement(1,0)/spc[0]);
400         matrixcurrent->SetElement(2,0,matrix->GetElement(2,0)/spc[0]);
401         matrixcurrent->SetElement(0,1,matrix->GetElement(0,1)/spc[1]);
402         matrixcurrent->SetElement(1,1,matrix->GetElement(1,1)/spc[1]);
403         matrixcurrent->SetElement(2,1,matrix->GetElement(2,1)/spc[1]);
404         matrixcurrent->SetElement(0,2,matrix->GetElement(0,2)/spc[2]);
405         matrixcurrent->SetElement(1,2,matrix->GetElement(1,2)/spc[2]);
406         matrixcurrent->SetElement(2,2,matrix->GetElement(2,2)/spc[2]);          
407         matrixcurrent->SetElement(0,3,matrix->GetElement(0,3)/spc[0]);
408         matrixcurrent->SetElement(1,3,matrix->GetElement(1,3)/spc[1]);
409         matrixcurrent->SetElement(2,3,matrix->GetElement(2,3)/spc[2]);  */
410
411         udapteActorDirection();
412         getModelTransform(img);
413 }
414
415 vtkTransform* CutModelData::getModelTransformInvers(){
416         inversModel->Identity ();
417         inversModel->Concatenate ( modeltransform );
418         inversModel->Inverse(); 
419         return inversModel;
420 }
421
422 vtkPolyData* CutModelData::getPolyData()throw( CutModelException){
423         return _Mapper->GetInput();
424 }
425