]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/kernel/CutModelManager.cxx
#3109 creaMaracasVisu Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaMaracasVisu.git] / lib / maracasVisuLib / src / CutModule / kernel / CutModelManager.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: CutModelManager.cxx,v $
30 Language:  C++
31 Date:      $Date: 2012/11/15 14:15:48 $
32 Version:   $Revision: 1.11 $
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 "CutModelManager.h"
43
44 //Machete
45 #include "CutModelMainPanel.h"
46
47 /**
48 **      Start of the manager class
49 **/
50 CutModelManager::CutModelManager(std::string path){
51         _path = path;
52         _img = NULL;
53         _img2 = NULL;
54         _interactor = NULL;     
55         _render = NULL;
56         _currentaction=0;
57
58 }
59 CutModelManager::~CutModelManager(){
60         std::string files = _path;
61         files+="/infounrd_0_fig_0.info";        
62         remove(files.c_str());  
63         _vectordata.clear();
64         _img = NULL;
65         _img2 = NULL;
66         _interactor = NULL;     
67         _render = NULL;
68 }
69
70
71 vtkImageData* CutModelManager::getImageData(){
72         return _img2;
73 }
74
75 void CutModelManager::setImageData(vtkImageData* img){
76         int type = CutModelMainPanel::getInstance()->GetType();
77         if( type == 0)
78         {
79                 _img = img;
80
81                 if(_img2!=NULL){
82                         _img2->Delete();
83                 }
84                 _img2 = vtkImageData::New();
85                 _img2->SetExtent(_img->GetExtent());
86                 _img2->SetSpacing(_img->GetSpacing());
87 //EED 2017-01-01 Migration VTK7
88 #if VTK_MAJOR_VERSION <= 5
89                 _img2->AllocateScalars();
90 #else
91   //...
92 #endif
93                 _img2->DeepCopy(_img);
94         }
95         else
96         {
97                 _img2 = img;
98
99                 if(_img!=NULL){
100                         _img->Delete();
101                 }
102                 _img = vtkImageData::New();
103                 _img->SetExtent(_img2->GetExtent());
104                 _img->SetSpacing(_img2->GetSpacing());
105 //EED 2017-01-01 Migration VTK7
106 #if VTK_MAJOR_VERSION <= 5
107                 _img->AllocateScalars();
108 #else
109   //...
110 #endif
111
112                 _img->DeepCopy(_img2);
113         }
114 }
115
116 void CutModelManager::setInteractor(vtkRenderWindowInteractor* interactor){
117         _interactor = interactor;
118 }
119
120 void CutModelManager::setRenderer(vtkRenderer* renderer){
121         _render = renderer;
122 }
123
124 void CutModelManager::onAddCutModel(int id, vtkCommand* observer) throw( CutModelException){
125         checkInvariant();
126
127
128         CutModelData* data = new CutModelData(id,_interactor, observer, _img);
129         _vectordata.push_back(data);
130
131         _render->AddActor(data->getActor());
132
133         //_render->UpdateCamera();
134         _render->Render();
135 }
136
137 void CutModelManager::checkInvariant() throw( CutModelException){
138         if(_img==NULL){
139                 throw CutModelException("The image is not set");
140         }
141         if(_img2==NULL){
142                 throw CutModelException("The image is not set");
143         }
144         if(_interactor==NULL){
145                 throw CutModelException("Interactor not set");
146         }
147         if(_render==NULL){
148                 throw CutModelException("Render not set");
149         }
150 }
151
152 double* CutModelManager::getImageRange()throw( CutModelException){
153         checkInvariant();
154         return _img->GetScalarRange();
155 }
156
157 void CutModelManager::changeOpacity(int id,int opacity)throw( CutModelException){
158         checkInvariant();
159         CutModelData* current = getCutModelData(id);
160         current->changeOpacity(opacity);
161 }
162
163 void CutModelManager::ShowViewBox(int id,bool check)throw( CutModelException){
164         checkInvariant();
165         CutModelData* current = getCutModelData(id);
166         current->ShowViewBox(check);
167 }
168
169 void CutModelManager::ChangeShape(int id,int selection)throw( CutModelException){
170         checkInvariant();
171         CutModelData* current = getCutModelData(id);
172         current->ChangeShape(selection);                        
173         _render->Render();
174 }
175
176 CutModelData* CutModelManager::getCutModelData(int id)throw( CutModelException){
177
178         CutModelData* current = NULL;
179         int i;
180         for(i= 0; i < (int)_vectordata.size();i++)
181         {
182                 CutModelData* temp = _vectordata[i];
183                 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
184                 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
185
186                 if(temp->getId() == id){
187                         current =  temp;
188                 }
189         }
190         if(current ==NULL){
191
192                 throw CutModelException("Data not found");
193         }
194         return current;
195 }
196
197 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
198         checkInvariant();
199         CutModelData* current = getCutModelData(id);
200         current->udapteActorDirection();
201
202 }
203
204 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
205
206         checkInvariant();
207         CutModelData* current = getCutModelData(id);
208         current->changeColor(r,g,b);
209         _render->Render();
210 }
211 void CutModelManager::RemoveActor(int id)throw( CutModelException){
212
213         checkInvariant();
214
215         CutModelData* current = getCutModelData(id);
216         int i,j;
217         for(i = 0; i < (int)_vectordata.size()-1;i++)
218         {
219                 if(_vectordata[i]->getId()==id){                                
220                         for(j = i; j < (int)_vectordata.size()-1;j++){
221                                 _vectordata[j]=_vectordata[j+1];
222                         }
223                         i = _vectordata.size();
224                 }
225         }
226         _render->RemoveViewProp(current->getActor());           
227         //_render->RemoveViewProp(current->get
228         delete current;
229         _vectordata.pop_back();
230         _render->Render();
231
232 }
233
234 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
235         checkInvariant();
236
237         CutModelData* current = getCutModelData(id);
238         current->ExecuteCut(range, isinside,_img2);
239
240
241         /*
242         Setting extra information for the undo
243         */
244         CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
245         undoaction->setRange(range);
246         undoaction->setIsInside(isinside);
247
248 }
249
250 vtkImageData* CutModelManager::GetResultImage(){
251         checkInvariant();
252         return _img2;
253 }
254
255 void CutModelManager::RefreshActor(int id){
256         checkInvariant();
257         CutModelData* current = getCutModelData(id);    
258         _render->RemoveActor(current->getActor());
259         _render->AddActor(current->getActor()); 
260         current->RefreshViewBox();
261         _render->Render();
262 }
263
264 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){  
265
266
267         throw CutModelException("not implemented");
268
269
270
271
272 }
273
274
275
276 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
277
278         throw CutModelException("not implemented");
279
280 }
281
282 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
283
284         for(int i = _undoredo.size()-1; i > _currentaction;i--){
285                 delete _undoredo[i];
286                 _undoredo.pop_back();           
287         }
288
289         CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
290         if(type == CUTMODEL_CUT){
291                 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
292                 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
293         }
294
295         _undoredo.push_back(cutmodel);
296
297         _currentaction++;// = _undoredo.size();
298         //std::cout<<"current index "<<_currentaction;
299
300         return cutmodel;
301 }
302
303 int CutModelManager::Undo()     throw( CutModelException){
304         //&& _currentaction < _undoredo.size()
305         if(_currentaction > 0){
306                 int tempaction = _currentaction-1;
307                 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
308                 CutModelData* currentmodel;
309
310                 if(currentundo->getActionType()==CUTMODEL_CUT){
311                         //Undo the cut
312                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
313
314                         currentmodel = getCutModelData(currentundo->getId());
315
316                         currentmodel->setTransform(transform, _img2);
317
318                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
319
320                         currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
321
322                 }
323                 //Every thing ok
324                 _currentaction--;
325                 return 0;
326         }
327         return -1;
328 }
329
330 int CutModelManager::Redo()     throw( CutModelException){
331
332         if(_currentaction >= 0 && _currentaction < (int)_undoredo.size()){
333
334                 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
335                 CutModelData* currentmodel;
336
337                 if(currentundo->getActionType()==CUTMODEL_CUT){
338                         //Redo the cut
339                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
340                         currentmodel = getCutModelData(currentundo->getId());
341                         currentmodel->setTransform(transform, _img2);
342                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
343                         currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
344                 }
345
346                 _currentaction++;
347
348                 return 0;
349         }
350         return -1;
351 }
352
353 void CutModelManager::ParallelProjectionOn(){
354         _render->GetActiveCamera()->ParallelProjectionOn();
355 }
356
357 void CutModelManager::ParallelProjectionOff(){
358         _render->GetActiveCamera()->ParallelProjectionOff();
359 }
360
361
362 //RaC
363 void CutModelManager::UpdatePolygon(bool mode)
364 {
365         if(mode)
366         {
367                 cutterstyle->Delete();
368                 cutterstyle = vtkInteractorStyleCutter::New();
369
370                 cutterstyle->SetInteractor ( _interactor );
371                 _interactor ->SetInteractorStyle( cutterstyle );
372         }
373         else
374         {
375                 cutterstyle->VisibilityOff();
376
377                 loop = vtkImplicitSelectionLoop::New();
378                 mapper = vtkPolyDataMapper::New();
379                 if(cutterstyle->Finished())
380                 {
381                         if(actor3D != NULL)
382                         {
383                                 _render->RemoveActor(actor3D);
384                         }
385
386                         loop->SetLoop( cutterstyle->GetLoopPoints() );
387                         loop->SetNormal( cutterstyle->GetDirection());
388
389
390                         /// Printing Points
391                         int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
392                         contourDirection = cutterstyle->GetDirection();
393
394                         contourPoints = cutterstyle->GetLoopPoints();
395
396                         _polygonCutter = new CutModelPolygon();
397
398                         cout<<"RaC Printing points......"<<endl;
399                         for(int t=0;t<numPoints;t++)
400                         {
401                                 double point[3];
402                                 cutterstyle->GetLoopPoints()->GetPoint(t,point);
403                                 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
404                         }
405                         cout<<endl;
406
407                         sample = vtkSampleFunction::New();
408
409                         sample->SetImplicitFunction(loop);
410                         sample->CappingOn();                    
411
412                         contour = vtkContourFilter::New();
413 //EED 2017-01-01 Migration VTK7
414 #if VTK_MAJOR_VERSION <= 5
415                         contour->SetInput((vtkDataObject *)sample->GetOutput());
416 #else
417                         contour->SetInputData((vtkDataObject *)sample->GetOutput());
418 #endif
419                         contour->SetValue(0,1);
420
421                         actor = contour->GetOutput();
422
423                         actor3D = vtkActor::New();                      
424 //EED 2017-01-01 Migration VTK7
425 #if VTK_MAJOR_VERSION <= 5
426                         mapper->SetInput(actor);                         
427 #else
428                         mapper->SetInputData(actor);                     
429 #endif
430                         mapper->SetScalarModeToUseCellData();
431                         actor3D->SetMapper( mapper);
432                         _render->AddActor(actor3D);                     
433                 }               
434
435                 interactorstyle->SetInteractor (  _interactor );
436                 _interactor->SetInteractorStyle( interactorstyle );
437         } 
438
439         _interactor->Render();
440         _render->ResetCameraClippingRange();
441 }
442 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
443
444         _polygonCutter->setInImage(_img2);
445         _polygonCutter->setPoints(contourPoints);
446         _polygonCutter->setDirection(contourDirection);
447         _polygonCutter->processOutImage(inOutCut);
448 }
449
450 void CutModelManager::InitializePolygonInteractorStyle(){
451
452         cutterstyle = vtkInteractorStyleCutter::New();          
453         interactorstyle = vtkInteractorStyleTrackballCamera ::New();
454         interactorstyle->SetInteractor (  _interactor );
455         _interactor->SetInteractorStyle( interactorstyle );
456 }