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