]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/kernel/CutModelData.cxx
Support #1768 CREATIS Licence insertion
[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                 _Mapper->SetInput(_spherefigure->getPolyData());
173         }else if(selection == 1){
174                 _Mapper->SetInput(_cylinderfigure->getPolyData());
175         }else if(selection == 2){
176                 _Mapper->SetInput(_cubefigure->getPolyData());
177         }else{
178                 throw CutModelException("Shape type not found");
179         }
180         _currentshape=selection;
181 }
182
183 void CutModelData::checkInvariant()throw( CutModelException){
184         if(_boxWidgetVolume==NULL){
185                 throw CutModelException("Box Widget not created");
186         }
187         if(_Mapper==NULL){
188                 throw CutModelException("Mapper not created");
189         }
190         if(_Actor==NULL){
191                 throw CutModelException("Actor not created");
192         }
193         if(_cubefigure==NULL){
194                 throw CutModelException("Cube not created");
195         }
196         if(_cylinderfigure==NULL){
197                 throw CutModelException("Cylinder not created");
198         }
199         if(_spherefigure==NULL){
200                 throw CutModelException("Sphere not created");
201         }
202 }
203
204 vtkActor* CutModelData::getActor()throw( CutModelException){
205         checkInvariant();
206         return _Actor;
207 }
208
209 void CutModelData::changeColor(double r,double g,double b)throw( CutModelException){
210         checkInvariant();               
211         _Actor->GetProperty()->SetColor( r,g,b );       
212 }
213
214 CutModelFigure* CutModelData::getCurentCuttingModel(){
215     checkInvariant();
216         
217         if(_currentshape == 0){
218                 return _spherefigure;
219         }else if(_currentshape == 1){
220                 return _cylinderfigure;
221         }else if(_currentshape == 2){
222                 return _cubefigure;
223         }else{
224                 throw CutModelException("Shape type not found");
225         }       
226 }
227
228 void CutModelData::ExecuteCut( double* range, bool isinside, vtkImageData* copyimage)throw( CutModelException)
229 {
230         wxBusyCursor wait;
231
232         CutModelFigure* actualCuttingModel = getCurentCuttingModel();
233
234         //_boxWidgetVolume->GetTransform(currentmatrix);        
235         actualCuttingModel->SetVtkTransform(getModelTransform(copyimage));
236         //actualCuttingModel->SetVtkTransform(currentmatrix);
237         actualCuttingModel->SetInversVtkTransform(getModelTransformInvers());   
238
239         bool                    inside;
240         //bool                  volInt, volExt; // unused
241         int                     xx,yy,zz;
242         unsigned short*         pOrg;   
243         int                     ext[6];
244         //double                        spc[3]; // unused
245         //long int              contAfter = 0; // unused
246         //long int              contBefor = 0; // unused
247         
248         double minvalue = range[0]; 
249         double value    = range[1];
250         double maxvalue = range[2]; 
251         
252         copyimage->GetExtent(ext);        
253         
254         for (xx=ext[0];xx<=ext[1]; xx++)
255         {
256                 for (yy=ext[2];yy<=ext[3]; yy++)
257                 {
258                         for (zz=ext[4];zz<=ext[5];zz++)
259                         {
260                                 inside=actualCuttingModel->IfPointInside(xx,yy,zz);
261                                 if (  ((inside==true)&&(isinside==true)) || ((!inside==true)&&(!isinside)) )
262                                 {
263                                         pOrg=(unsigned short*)copyimage->GetScalarPointer (xx,yy,zz); 
264
265                                         //std::cout<<"xx,yy,zz "<<xx<<","<<yy<<","<<zz<<" "<<*pOrg<<std::endl;
266                                         if ((unsigned short)minvalue <=(*pOrg)&&(*pOrg)<=(unsigned short)maxvalue)
267                                         {       
268                                                 
269                                                 *pOrg=(unsigned short)value;
270                                                 
271                                         }                                       
272                                 } // if inside
273                         } // for zz
274                 } // for yy
275         } // for xx
276 }
277
278 void CutModelData::ExecuteUnCut(bool isinside, vtkImageData* image, vtkImageData* copyimage)throw( CutModelException)
279 {
280         wxBusyCursor wait;
281
282         CutModelFigure* actualCuttingModel = getCurentCuttingModel();
283
284         actualCuttingModel->SetVtkTransform(getModelTransform(copyimage));
285
286         actualCuttingModel->SetInversVtkTransform(getModelTransformInvers());   
287
288         bool                    inside;
289         // bool                 volInt, volExt; // unused // JPRx
290         int                     xx,yy,zz;
291         unsigned short* pOrg;   
292         int                     ext[6];
293         //double                spc[3]; // unused // JPRx
294         //long int              contAfter = 0; // unused // JPRx
295         //long int              contBefor = 0;   // unused // JPRx
296
297         double value;
298
299         copyimage->GetExtent(ext);        
300
301         for (xx=ext[0];xx<=ext[1]; xx++)
302         {
303                 for (yy=ext[2];yy<=ext[3]; yy++)
304                 {
305                         for (zz=ext[4];zz<=ext[5];zz++)
306                         {
307                                 inside=actualCuttingModel->IfPointInside(xx,yy,zz);
308                                 if (  ((inside==true)&&(isinside==true)) || ((!inside==true)&&(!isinside)) )
309                                 {
310                                         value = *((unsigned short*)image->GetScalarPointer (xx,yy,zz));
311                                         pOrg=(unsigned short*)copyimage->GetScalarPointer (xx,yy,zz); 
312                                         //std::cout<<"CutModelData::ExecuteUnCut() "<<value<<" "<<*pOrg<<std::endl;
313                                         *pOrg=(unsigned short)value;
314                                 } // if inside
315                         } // for zz
316                 } // for yy
317         } // for xx
318 }
319
320 vtkTransform* CutModelData::getModelTransform(vtkImageData* copyimage){
321
322         double *spc = copyimage->GetSpacing();
323         modeltransform->Identity();
324
325         vtkMatrix4x4* matrix = currentmatrix->GetMatrix();
326         vtkMatrix4x4* matrixmodel = modeltransform->GetMatrix();
327         matrixmodel->SetElement(0,0,matrix->GetElement(0,0)/spc[0]);
328         matrixmodel->SetElement(1,0,matrix->GetElement(1,0)/spc[0]);
329         matrixmodel->SetElement(2,0,matrix->GetElement(2,0)/spc[0]);
330         matrixmodel->SetElement(0,1,matrix->GetElement(0,1)/spc[1]);
331         matrixmodel->SetElement(1,1,matrix->GetElement(1,1)/spc[1]);
332         matrixmodel->SetElement(2,1,matrix->GetElement(2,1)/spc[1]);
333         matrixmodel->SetElement(0,2,matrix->GetElement(0,2)/spc[2]);
334         matrixmodel->SetElement(1,2,matrix->GetElement(1,2)/spc[2]);
335         matrixmodel->SetElement(2,2,matrix->GetElement(2,2)/spc[2]);            
336         matrixmodel->SetElement(0,3,matrix->GetElement(0,3)/spc[0]);
337         matrixmodel->SetElement(1,3,matrix->GetElement(1,3)/spc[1]);
338         matrixmodel->SetElement(2,3,matrix->GetElement(2,3)/spc[2]);    
339
340         /*
341         double* pos = currentmatrix->GetPosition();
342         modeltransform->Translate(pos[0]/spc[0],pos[1]/spc[1],pos[2]/spc[2]);   
343
344         double* scal = currentmatrix->GetScale();       
345         modeltransform->Scale(scal[0]/spc[0],scal[1]/spc[1],scal[2]/spc[2]);
346
347         //double* orient = currentmatrix->GetOrientation();
348         //modeltransform->RotateZ(orient[2]);   
349         //modeltransform->RotateY(orient[1]);   
350         //modeltransform->RotateX(orient[0]);   
351         double* orient = currentmatrix->GetOrientationWXYZ();   
352         modeltransform->RotateWXYZ(orient[0],orient[1],orient[2],orient[3]);    
353         */
354
355         modeltransform->Update();
356
357         return modeltransform;
358 }
359
360 void CutModelData::setTransform(vtkTransform* transform, vtkImageData* img)throw( CutModelException){
361         
362 //      double *spc = img->GetSpacing();  // spc unused // JPR
363
364         currentmatrix->Identity();      
365
366         /*double* orient = transform->GetOrientationWXYZ();
367         currentmatrix->RotateWXYZ(orient[0],orient[1],orient[2],orient[3]);     
368
369         double* pos = transform->GetPosition();
370         currentmatrix->Translate(pos[0]/spc[0],pos[1]/spc[1],pos[2]/spc[2]);
371
372         double* scal = transform->GetScale();   
373         currentmatrix->Scale(scal[0]/spc[0],scal[1]/spc[1],scal[2]/spc[2]);
374
375         currentmatrix->Update();        */
376
377         _boxWidgetVolume->SetTransform(transform);
378
379     /*vtkMatrix4x4* matrix = transform->GetMatrix();
380         vtkMatrix4x4* matrixcurrent = currentmatrix->GetMatrix();
381         matrixcurrent->SetElement(0,0,matrix->GetElement(0,0)/spc[0]);
382         matrixcurrent->SetElement(1,0,matrix->GetElement(1,0)/spc[0]);
383         matrixcurrent->SetElement(2,0,matrix->GetElement(2,0)/spc[0]);
384         matrixcurrent->SetElement(0,1,matrix->GetElement(0,1)/spc[1]);
385         matrixcurrent->SetElement(1,1,matrix->GetElement(1,1)/spc[1]);
386         matrixcurrent->SetElement(2,1,matrix->GetElement(2,1)/spc[1]);
387         matrixcurrent->SetElement(0,2,matrix->GetElement(0,2)/spc[2]);
388         matrixcurrent->SetElement(1,2,matrix->GetElement(1,2)/spc[2]);
389         matrixcurrent->SetElement(2,2,matrix->GetElement(2,2)/spc[2]);          
390         matrixcurrent->SetElement(0,3,matrix->GetElement(0,3)/spc[0]);
391         matrixcurrent->SetElement(1,3,matrix->GetElement(1,3)/spc[1]);
392         matrixcurrent->SetElement(2,3,matrix->GetElement(2,3)/spc[2]);  */
393
394         udapteActorDirection();
395         getModelTransform(img);
396 }
397
398 vtkTransform* CutModelData::getModelTransformInvers(){
399         inversModel->Identity ();
400         inversModel->Concatenate ( modeltransform );
401         inversModel->Inverse(); 
402         return inversModel;
403 }
404
405 vtkPolyData* CutModelData::getPolyData()throw( CutModelException){
406         return _Mapper->GetInput();
407 }
408