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