]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/kernel/CutModelManager.cxx
changes in vtkInteractorStyleBaseView2D. Check out the constructor of this class...
[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/04/02 07:22:25 $
7 Version:   $Revision: 1.9 $
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         for(int i= 0; i < _vectordata.size();i++){
146                 CutModelData* temp = _vectordata[i];
147                 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
148                 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
149
150                 if(temp->getId() == id){
151                         current =  temp;
152                 }
153         }
154         if(current ==NULL){
155
156                 throw CutModelException("Data not found");
157         }
158         return current;
159 }
160
161 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
162         checkInvariant();
163         CutModelData* current = getCutModelData(id);
164         current->udapteActorDirection();
165
166 }
167
168 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
169
170         checkInvariant();
171         CutModelData* current = getCutModelData(id);
172         current->changeColor(r,g,b);
173         _render->Render();
174 }
175 void CutModelManager::RemoveActor(int id)throw( CutModelException){
176
177         checkInvariant();
178
179         CutModelData* current = getCutModelData(id);
180         for(int i = 0; i < _vectordata.size()-1;i++){
181                 if(_vectordata[i]->getId()==id){                                
182                         for(int j = i; j < _vectordata.size()-1;j++){
183                                 _vectordata[j]=_vectordata[j+1];
184                         }
185                         i = _vectordata.size();
186                 }
187         }
188         _render->RemoveViewProp(current->getActor());           
189         //_render->RemoveViewProp(current->get
190         delete current;
191         _vectordata.pop_back();
192         _render->Render();
193
194 }
195
196 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
197         checkInvariant();
198
199         CutModelData* current = getCutModelData(id);
200         current->ExecuteCut(range, isinside,_img2);
201
202
203         /*
204         Setting extra information for the undo
205         */
206         CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
207         undoaction->setRange(range);
208         undoaction->setIsInside(isinside);
209
210 }
211
212 vtkImageData* CutModelManager::GetResultImage(){
213         checkInvariant();
214         return _img2;
215 }
216
217 void CutModelManager::RefreshActor(int id){
218         checkInvariant();
219         CutModelData* current = getCutModelData(id);    
220         _render->RemoveActor(current->getActor());
221         _render->AddActor(current->getActor()); 
222         current->RefreshViewBox();
223         _render->Render();
224 }
225
226 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){  
227
228
229         throw CutModelException("not implemented");
230
231
232
233
234 }
235
236
237
238 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
239
240         throw CutModelException("not implemented");
241
242 }
243
244 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
245
246         for(int i = _undoredo.size()-1; i > _currentaction;i--){
247                 delete _undoredo[i];
248                 _undoredo.pop_back();           
249         }
250
251         CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
252         if(type == CUTMODEL_CUT){
253                 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
254                 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
255         }
256
257         _undoredo.push_back(cutmodel);
258
259         _currentaction++;// = _undoredo.size();
260         //std::cout<<"current index "<<_currentaction;
261
262         return cutmodel;
263 }
264
265 int CutModelManager::Undo()     throw( CutModelException){
266         //&& _currentaction < _undoredo.size()
267         if(_currentaction > 0){
268                 int tempaction = _currentaction-1;
269                 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
270                 CutModelData* currentmodel;
271
272                 if(currentundo->getActionType()==CUTMODEL_CUT){
273                         //Undo the cut
274                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
275
276                         currentmodel = getCutModelData(currentundo->getId());
277
278                         currentmodel->setTransform(transform, _img2);
279
280                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
281
282                         currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _img2);
283
284                 }
285                 //Every thing ok
286                 _currentaction--;
287                 return 0;
288         }
289         return -1;
290 }
291
292 int CutModelManager::Redo()     throw( CutModelException){
293
294         if(_currentaction >= 0 && _currentaction < _undoredo.size()){
295
296                 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
297                 CutModelData* currentmodel;
298
299                 if(currentundo->getActionType()==CUTMODEL_CUT){
300                         //Redo the cut
301                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
302                         currentmodel = getCutModelData(currentundo->getId());
303                         currentmodel->setTransform(transform, _img2);
304                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
305                         currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _img2);
306                 }
307
308                 _currentaction++;
309
310                 return 0;
311         }
312         return -1;
313 }
314
315 void CutModelManager::ParallelProjectionOn(){
316         _render->GetActiveCamera()->ParallelProjectionOn();
317 }
318
319 void CutModelManager::ParallelProjectionOff(){
320         _render->GetActiveCamera()->ParallelProjectionOff();
321 }
322
323
324 //RaC
325 void CutModelManager::UpdatePolygon(bool mode)
326 {
327         if(mode)
328         {
329                 cutterstyle->Delete();
330                 cutterstyle = vtkInteractorStyleCutter::New();
331
332                 cutterstyle->SetInteractor ( _interactor );
333                 _interactor ->SetInteractorStyle( cutterstyle );
334         }
335         else
336         {
337                 cutterstyle->VisibilityOff();
338
339                 loop = vtkImplicitSelectionLoop::New();
340                 mapper = vtkPolyDataMapper::New();
341                 if(cutterstyle->Finished())
342                 {
343                         if(actor3D != NULL)
344                         {
345                                 _render->RemoveActor(actor3D);
346                         }
347
348                         loop->SetLoop( cutterstyle->GetLoopPoints() );
349                         loop->SetNormal( cutterstyle->GetDirection());
350
351
352                         /// Printing Points
353                         int numPoints = cutterstyle->GetLoopPoints()->GetNumberOfPoints();
354                         contourDirection = cutterstyle->GetDirection();
355
356                         contourPoints = cutterstyle->GetLoopPoints();
357
358                         _polygonCutter = new CutModelPolygon();
359
360                         cout<<"RaC Printing points......"<<endl;
361                         for(int t=0;t<numPoints;t++)
362                         {
363                                 double point[3];
364                                 cutterstyle->GetLoopPoints()->GetPoint(t,point);
365                                 cout<<"Initial Point:"<<t<<" XX:"<<point[0]<<" YY:"<<point[1]<<" ZZ:"<<point[2]<<endl;
366                         }
367                         cout<<endl;
368
369                         sample = vtkSampleFunction::New();
370
371                         sample->SetImplicitFunction(loop);
372                         sample->CappingOn();                    
373
374                         contour = vtkContourFilter::New();
375                         contour->SetInput((vtkDataObject *)sample->GetOutput());
376                         contour->SetValue(0,1);
377
378                         actor = contour->GetOutput();
379
380                         actor3D = vtkActor::New();                      
381                         mapper->SetInput(actor);                         
382                         mapper->SetScalarModeToUseCellData();
383                         actor3D->SetMapper( mapper);
384                         _render->AddActor(actor3D);                     
385                 }               
386
387                 interactorstyle->SetInteractor (  _interactor );
388                 _interactor->SetInteractorStyle( interactorstyle );
389         } 
390
391         _interactor->Render();
392         _render->ResetCameraClippingRange();
393 }
394 void CutModelManager::ExecuteCutPolygon(bool inOutCut){
395
396         _polygonCutter->setInImage(_img2);
397         _polygonCutter->setPoints(contourPoints);
398         _polygonCutter->setDirection(contourDirection);
399         _polygonCutter->processOutImage(inOutCut);
400 }
401
402 void CutModelManager::InitializePolygonInteractorStyle(){
403
404         cutterstyle = vtkInteractorStyleCutter::New();          
405         interactorstyle = vtkInteractorStyleTrackballCamera ::New();
406         interactorstyle->SetInteractor (  _interactor );
407         _interactor->SetInteractorStyle( interactorstyle );
408 }