1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
26 /*=========================================================================
29 Module: $RCSfile: CutModelData.cxx,v $
31 Date: $Date: 2012/11/15 14:15:48 $
32 Version: $Revision: 1.10 $
34 Copyright: (c) 2002, 2003
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.
41 =========================================================================*/
42 #include "CutModelData.h"
43 #include "vtkRenderWindow.h"
45 ** Start of the manager class
47 CutModelData::CutModelData(int id, vtkRenderWindowInteractor* interactor, vtkCommand* observer, vtkImageData* img){
49 initializeData(id, interactor, observer, img);
52 CutModelData::CutModelData(){
55 void CutModelData::initializeData(int id, vtkRenderWindowInteractor* interactor, vtkCommand* observer, vtkImageData* img){
58 createBoxWidget(interactor, observer);
63 interactor->GetRenderWindow ()->Render();
67 CutModelData::~CutModelData(){
69 _boxWidgetVolume->RemoveAllObservers();
70 _boxWidgetVolume->Off();
71 _boxWidgetVolume->SetInteractor(NULL);
72 _boxWidgetVolume->Delete();
76 delete _cylinderfigure;
78 currentmatrix->Delete();
79 modeltransform->Delete();
80 inversModel->Delete();
84 void CutModelData::RefreshViewBox(){
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();
94 _boxWidgetVolume->AddObserver( vtkCommand::InteractionEvent , observer );
95 _boxWidgetVolume->AddObserver( vtkCommand::StartInteractionEvent , observer );
96 _boxWidgetVolume->AddObserver( vtkCommand::RightButtonReleaseEvent , observer );
98 _boxWidgetVolume->HandlesOn ();
99 _boxWidgetVolume->On();
100 //_boxWidgetVolume->GetHandleProperty()->SetOpacity(0.5);
101 //_boxWidgetVolume->GetOutlineProperty()->SetOpacity(0.5);
104 void CutModelData::setTransform(vtkImageData* img)throw( CutModelException){
106 currentmatrix = vtkTransform::New();
107 modeltransform = vtkTransform::New();
108 inversModel = vtkTransform::New();
110 vtkMatrix4x4* matrix = vtkMatrix4x4::New();
112 double *ori = img->GetOrigin();
113 int *ext = img->GetExtent();
114 double *spc = img->GetSpacing();
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]);
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];
124 matrix->SetElement(0,3,orgx);
125 matrix->SetElement(1,3,orgy);
126 matrix->SetElement(2,3,orgz);
128 currentmatrix->SetMatrix(matrix);
129 _boxWidgetVolume->SetTransform(currentmatrix);
132 void CutModelData::createActor(){
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);
141 void CutModelData::udapteActorDirection()throw( CutModelException){
144 _boxWidgetVolume->GetTransform(currentmatrix);
145 _Actor->SetUserMatrix(currentmatrix->GetMatrix());//SetUserTransform(currentmatrix );
148 void CutModelData::createShapes(){
149 _cubefigure = new CutModelFigureCube();
150 _cylinderfigure = new CutModelFigureCylinder();
151 _spherefigure = new CutModelFigureSphere();
154 void CutModelData::changeOpacity(int opacity)throw( CutModelException){
156 _Actor->GetProperty()->SetOpacity((double)opacity/100.0);
159 void CutModelData::ShowViewBox(bool check)throw( CutModelException){
162 _boxWidgetVolume->On();
164 _boxWidgetVolume->Off();
168 void CutModelData::ChangeShape(int selection)throw( CutModelException){
173 //EED 2017-01-01 Migration VTK7
174 #if VTK_MAJOR_VERSION <= 5
175 _Mapper->SetInput(_spherefigure->getPolyData());
177 _Mapper->SetInputData(_spherefigure->getPolyData());
179 }else if(selection == 1){
181 #if VTK_MAJOR_VERSION <= 5
182 _Mapper->SetInput(_cylinderfigure->getPolyData());
184 _Mapper->SetInputData(_cylinderfigure->getPolyData());
187 }else if(selection == 2){
188 //EED 2017-01-01 Migration VTK7
189 #if VTK_MAJOR_VERSION <= 5
190 _Mapper->SetInput(_cubefigure->getPolyData());
192 _Mapper->SetInputData(_cubefigure->getPolyData());
195 throw CutModelException("Shape type not found");
197 _currentshape=selection;
200 void CutModelData::checkInvariant()throw( CutModelException){
201 if(_boxWidgetVolume==NULL){
202 throw CutModelException("Box Widget not created");
205 throw CutModelException("Mapper not created");
208 throw CutModelException("Actor not created");
210 if(_cubefigure==NULL){
211 throw CutModelException("Cube not created");
213 if(_cylinderfigure==NULL){
214 throw CutModelException("Cylinder not created");
216 if(_spherefigure==NULL){
217 throw CutModelException("Sphere not created");
221 vtkActor* CutModelData::getActor()throw( CutModelException){
226 void CutModelData::changeColor(double r,double g,double b)throw( CutModelException){
228 _Actor->GetProperty()->SetColor( r,g,b );
231 CutModelFigure* CutModelData::getCurentCuttingModel(){
234 if(_currentshape == 0){
235 return _spherefigure;
236 }else if(_currentshape == 1){
237 return _cylinderfigure;
238 }else if(_currentshape == 2){
241 throw CutModelException("Shape type not found");
245 void CutModelData::ExecuteCut( double* range, bool isinside, vtkImageData* copyimage)throw( CutModelException)
249 CutModelFigure* actualCuttingModel = getCurentCuttingModel();
251 //_boxWidgetVolume->GetTransform(currentmatrix);
252 actualCuttingModel->SetVtkTransform(getModelTransform(copyimage));
253 //actualCuttingModel->SetVtkTransform(currentmatrix);
254 actualCuttingModel->SetInversVtkTransform(getModelTransformInvers());
257 //bool volInt, volExt; // unused
259 unsigned short* pOrg;
261 //double spc[3]; // unused
262 //long int contAfter = 0; // unused
263 //long int contBefor = 0; // unused
265 double minvalue = range[0];
266 double value = range[1];
267 double maxvalue = range[2];
269 copyimage->GetExtent(ext);
271 for (xx=ext[0];xx<=ext[1]; xx++)
273 for (yy=ext[2];yy<=ext[3]; yy++)
275 for (zz=ext[4];zz<=ext[5];zz++)
277 inside=actualCuttingModel->IfPointInside(xx,yy,zz);
278 if ( ((inside==true)&&(isinside==true)) || ((!inside==true)&&(!isinside)) )
280 pOrg=(unsigned short*)copyimage->GetScalarPointer (xx,yy,zz);
282 //std::cout<<"xx,yy,zz "<<xx<<","<<yy<<","<<zz<<" "<<*pOrg<<std::endl;
283 if ((unsigned short)minvalue <=(*pOrg)&&(*pOrg)<=(unsigned short)maxvalue)
286 *pOrg=(unsigned short)value;
295 void CutModelData::ExecuteUnCut(bool isinside, vtkImageData* image, vtkImageData* copyimage)throw( CutModelException)
299 CutModelFigure* actualCuttingModel = getCurentCuttingModel();
301 actualCuttingModel->SetVtkTransform(getModelTransform(copyimage));
303 actualCuttingModel->SetInversVtkTransform(getModelTransformInvers());
306 // bool volInt, volExt; // unused // JPRx
308 unsigned short* pOrg;
310 //double spc[3]; // unused // JPRx
311 //long int contAfter = 0; // unused // JPRx
312 //long int contBefor = 0; // unused // JPRx
316 copyimage->GetExtent(ext);
318 for (xx=ext[0];xx<=ext[1]; xx++)
320 for (yy=ext[2];yy<=ext[3]; yy++)
322 for (zz=ext[4];zz<=ext[5];zz++)
324 inside=actualCuttingModel->IfPointInside(xx,yy,zz);
325 if ( ((inside==true)&&(isinside==true)) || ((!inside==true)&&(!isinside)) )
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;
337 vtkTransform* CutModelData::getModelTransform(vtkImageData* copyimage){
339 double *spc = copyimage->GetSpacing();
340 modeltransform->Identity();
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]);
358 double* pos = currentmatrix->GetPosition();
359 modeltransform->Translate(pos[0]/spc[0],pos[1]/spc[1],pos[2]/spc[2]);
361 double* scal = currentmatrix->GetScale();
362 modeltransform->Scale(scal[0]/spc[0],scal[1]/spc[1],scal[2]/spc[2]);
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]);
372 modeltransform->Update();
374 return modeltransform;
377 void CutModelData::setTransform(vtkTransform* transform, vtkImageData* img)throw( CutModelException){
379 // double *spc = img->GetSpacing(); // spc unused // JPR
381 currentmatrix->Identity();
383 /*double* orient = transform->GetOrientationWXYZ();
384 currentmatrix->RotateWXYZ(orient[0],orient[1],orient[2],orient[3]);
386 double* pos = transform->GetPosition();
387 currentmatrix->Translate(pos[0]/spc[0],pos[1]/spc[1],pos[2]/spc[2]);
389 double* scal = transform->GetScale();
390 currentmatrix->Scale(scal[0]/spc[0],scal[1]/spc[1],scal[2]/spc[2]);
392 currentmatrix->Update(); */
394 _boxWidgetVolume->SetTransform(transform);
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]); */
411 udapteActorDirection();
412 getModelTransform(img);
415 vtkTransform* CutModelData::getModelTransformInvers(){
416 inversModel->Identity ();
417 inversModel->Concatenate ( modeltransform );
418 inversModel->Inverse();
422 vtkPolyData* CutModelData::getPolyData()throw( CutModelException){
423 return _Mapper->GetInput();