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