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