]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/kernel/CutModelManager.cxx
Machète detruction viewer
[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: 2009/11/25 16:35:37 $
7   Version:   $Revision: 1.4 $
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 /**
20 **      Start of the manager class
21 **/
22 CutModelManager::CutModelManager(std::string path){
23         _path = path;
24         _img = NULL;
25         _copyimg = NULL;
26         _interactor = NULL;     
27         _render = NULL;
28         _currentaction=0;
29 }
30 CutModelManager::~CutModelManager(){
31         std::string files = _path;
32     files+="/infounrd_0_fig_0.info";    
33         remove(files.c_str());  
34         _vectordata.clear();
35         _img = NULL;
36         _copyimg = NULL;
37         _interactor = NULL;     
38         _render = NULL;
39 }
40
41
42 void CutModelManager::setImageData(vtkImageData* img){
43     _img = img;
44         if(_copyimg!=NULL){
45                 _copyimg->Delete();
46         }
47         _copyimg = vtkImageData::New();
48         _copyimg->SetExtent(_img->GetExtent());
49         _copyimg->SetSpacing(_img->GetSpacing());
50         _copyimg->AllocateScalars();
51
52         _copyimg->DeepCopy(_img);
53
54 }
55
56 void CutModelManager::setInteractor(vtkRenderWindowInteractor* interactor){
57         _interactor = interactor;
58 }
59
60 void CutModelManager::setRenderer(vtkRenderer* renderer){
61         _render = renderer;
62 }
63
64 void CutModelManager::onAddCutModel(int id, vtkCommand* observer) throw( CutModelException){
65         checkInvariant();
66
67         CutModelData* data = new CutModelData(id,_interactor, observer, _img);
68         _vectordata.push_back(data);
69
70         _render->AddActor(data->getActor());
71
72         //_render->UpdateCamera();
73         _render->Render();
74 }
75
76 void CutModelManager::checkInvariant() throw( CutModelException){
77         if(_img==NULL){
78                 throw CutModelException("The image is not set");
79         }
80         if(_copyimg==NULL){
81                 throw CutModelException("The image is not set");
82         }
83         if(_interactor==NULL){
84                 throw CutModelException("Interactor not set");
85         }
86         if(_render==NULL){
87                 throw CutModelException("Render not set");
88         }
89 }
90
91 double* CutModelManager::getImageRange()throw( CutModelException){
92         checkInvariant();
93         return _img->GetScalarRange();
94 }
95
96 void CutModelManager::changeOpacity(int id,int opacity)throw( CutModelException){
97         checkInvariant();
98         CutModelData* current = getCutModelData(id);
99         current->changeOpacity(opacity);
100 }
101
102 void CutModelManager::ShowViewBox(int id,bool check)throw( CutModelException){
103         checkInvariant();
104         CutModelData* current = getCutModelData(id);
105         current->ShowViewBox(check);
106 }
107
108 void CutModelManager::ChangeShape(int id,int selection)throw( CutModelException){
109         checkInvariant();
110         CutModelData* current = getCutModelData(id);
111         current->ChangeShape(selection);                        
112         _render->Render();
113 }
114
115 CutModelData* CutModelManager::getCutModelData(int id)throw( CutModelException){
116
117         CutModelData* current = NULL;
118         for(int i= 0; i < _vectordata.size();i++){
119                 CutModelData* temp = _vectordata[i];
120                 std::cout<<"id in CutModelManager:: "<<id<<std::endl;
121                 std::cout<<"vectordataid in CutModelManager:: "<<temp->getId()<<std::endl;
122
123                 if(temp->getId() == id){
124                         current =  temp;
125                 }
126         }
127         if(current ==NULL){
128                 
129                 throw CutModelException("Data not found");
130         }
131         return current;
132 }
133
134 void CutModelManager::updateActorDirection(int id)throw( CutModelException){
135         checkInvariant();
136         CutModelData* current = getCutModelData(id);
137         current->udapteActorDirection();
138         
139 }
140
141 void CutModelManager::changeColor(int id,double r,double g,double b)throw( CutModelException){
142
143         checkInvariant();
144         CutModelData* current = getCutModelData(id);
145         current->changeColor(r,g,b);
146         _render->Render();
147 }
148 void CutModelManager::RemoveActor(int id)throw( CutModelException){
149         
150                 checkInvariant();
151
152                 CutModelData* current = getCutModelData(id);
153                 for(int i = 0; i < _vectordata.size()-1;i++){
154                         if(_vectordata[i]->getId()==id){                                
155                                 for(int j = i; j < _vectordata.size()-1;j++){
156                     _vectordata[j]=_vectordata[j+1];
157                                 }
158                                 i = _vectordata.size();
159                         }
160                 }
161                 _render->RemoveViewProp(current->getActor());           
162                 //_render->RemoveViewProp(current->get
163                 delete current;
164                 _vectordata.pop_back();
165                 _render->Render();
166         
167 }
168
169 void CutModelManager::ExecuteCut(int id, double* range, bool isinside)throw( CutModelException){
170     checkInvariant();
171                 
172         CutModelData* current = getCutModelData(id);
173         current->ExecuteCut(range, isinside,_copyimg);
174
175
176         /*
177         Setting extra information for the undo
178         */
179         CutModelSaveBinInfo* undoaction = this->AddActionUndo(id, CUTMODEL_CUT);
180         undoaction->setRange(range);
181         undoaction->setIsInside(isinside);
182
183 }
184
185 vtkImageData* CutModelManager::GetResultImage(){
186          checkInvariant();
187      return _copyimg;
188 }
189
190 void CutModelManager::RefreshActor(int id){
191      checkInvariant();
192         CutModelData* current = getCutModelData(id);    
193         _render->RemoveActor(current->getActor());
194         _render->AddActor(current->getActor()); 
195         current->RefreshViewBox();
196         _render->Render();
197 }
198
199 void CutModelManager::SaveCutModelData(std::string filename)throw( CutModelException){  
200
201         
202         throw CutModelException("not implemented");
203         
204         
205         
206         
207 }
208
209
210
211 void CutModelManager::LoadCutModelData(std::string filename)throw( CutModelException){
212         
213         throw CutModelException("not implemented");
214         
215 }
216
217 CutModelSaveBinInfo* CutModelManager::AddActionUndo(int idc, UNDOTYPE type)throw( CutModelException){
218         
219         for(int i = _undoredo.size()-1; i > _currentaction;i--){
220                 delete _undoredo[i];
221                 _undoredo.pop_back();           
222         }
223
224         CutModelSaveBinInfo* cutmodel = new CutModelSaveBinInfo(idc, _currentaction, type, _path);
225         if(type == CUTMODEL_CUT){
226                 cutmodel->saveMatrix4x4(this->getCutModelData(idc)->getCurrentMatrix()->GetMatrix());
227                 cutmodel->setCurrentShape(this->getCutModelData(idc)->getCurrentShape());
228         }
229
230         _undoredo.push_back(cutmodel);
231
232         _currentaction++;// = _undoredo.size();
233         //std::cout<<"current index "<<_currentaction;
234                 
235         return cutmodel;
236 }
237
238 int CutModelManager::Undo()     throw( CutModelException){
239  //&& _currentaction < _undoredo.size()
240         if(_currentaction > 0){
241                 int tempaction = _currentaction-1;
242                 CutModelSaveBinInfo* currentundo = _undoredo[tempaction];
243                 CutModelData* currentmodel;
244
245                 if(currentundo->getActionType()==CUTMODEL_CUT){
246                         //Undo the cut
247                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
248
249                         currentmodel = getCutModelData(currentundo->getId());
250
251                         currentmodel->setTransform(transform, _copyimg);
252
253                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
254
255                         currentmodel->ExecuteUnCut(currentundo->getIsInside(), _img, _copyimg);
256
257                 }
258                 //Every thing ok
259                 _currentaction--;
260                 return 0;
261         }
262         return -1;
263 }
264
265 int CutModelManager::Redo()     throw( CutModelException){
266
267         if(_currentaction >= 0 && _currentaction < _undoredo.size()){
268                 
269
270                 CutModelSaveBinInfo* currentundo = _undoredo[_currentaction];
271                 CutModelData* currentmodel;
272
273                 if(currentundo->getActionType()==CUTMODEL_CUT){
274                         //Redo the cut
275                         vtkTransform* transform = currentundo->getTransformFromMatrixFile();
276                         currentmodel = getCutModelData(currentundo->getId());
277                         currentmodel->setTransform(transform, _copyimg);
278                         currentmodel->setCurrentShape(currentundo->getCurrentShape());
279                         currentmodel->ExecuteCut(currentundo->getRange(), currentundo->getIsInside(), _copyimg);
280                 }
281
282                 _currentaction++;
283
284                 return 0;
285         }
286         return -1;
287 }