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