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