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