]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxMaracasIRMViewManager.cxx
*** empty log message ***
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / wxMaracasIRMViewManager.cxx
1 /*=========================================================================
2
3   Program:   wxMaracas
4   Module:    $RCSfile: wxMaracasIRMViewManager.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/05/04 07:35:42 $
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 "wxMaracasIRMViewManager.h"
18
19 /**
20 **      Start of the manager class
21 **/
22 wxMaracasIRMViewManager::wxMaracasIRMViewManager(){
23         _renderer = NULL;
24         _idCount=0;
25 }
26 wxMaracasIRMViewManager::~wxMaracasIRMViewManager(){
27 }
28
29 /**
30 **      Sets the renderer to manage the prop3D from the view
31 **/
32 void wxMaracasIRMViewManager::setRenderer(vtkRenderer*  renderer){
33         _renderer = renderer;
34 }
35 /**
36 **      Adds a prop3D to the manager and returns the identifier
37 **/
38 int wxMaracasIRMViewManager::addProp3D(vtkProp3D* prop3D, std::string dataname)  throw(char*){
39         checkInvariant();
40         if(prop3D != NULL){
41                 wxMaracasIRMViewManagerData* data = new wxMaracasIRMViewManagerData(prop3D, dataname);
42                 prop3Dvect.push_back(data);             
43                 _renderer->AddActor(data->getProp3D());
44                 data->setId(_idCount);
45                 _idCount++;
46                 return data->getId();
47         }else{
48                 throw "Check vtkProp3D file or input";
49         }
50         return -1;
51 }
52 int wxMaracasIRMViewManager::addPropMHD(vtkImageData* imagedata, std::string dataname) throw(char*){
53
54         checkInvariant();
55         if(imagedata != NULL){
56                 wxMaracasIRMViewManagerData* data = new wxMaracasIRMViewManagerData(imagedata, dataname);
57                 prop3Dvect.push_back(data);             
58                 _renderer->AddActor(data->getProp3D());
59                 data->setId(_idCount);
60                 _idCount++;
61                 return data->getId();
62         }else{
63                 throw "Check ImageData file or input";
64         }
65         return -1;
66
67 }
68 /**
69 **      adds or removes an actor depending of the bool value
70 **/
71 void wxMaracasIRMViewManager::addRemoveActor(int propid, bool addremove)  throw(char*){
72         checkInvariant();
73         
74         wxMaracasIRMViewManagerData* data = this->getViewData(propid);          
75         if(data->getProp3D()!=NULL){
76                 if(addremove){
77                         _renderer->AddViewProp(data->getProp3D());
78                 }else{
79                         _renderer->RemoveViewProp(data->getProp3D());
80                 }
81                 _renderer->Render();
82         }
83         
84 }
85 /**
86 **      Changes the opacity in a prop3D
87 **/
88 void wxMaracasIRMViewManager::changeOpacity(int propid, int value)  throw(char*){
89         checkInvariant();       
90
91
92         this->getViewData(propid)->changeOpacity(value);
93         
94         _renderer->Render();
95
96 }
97
98 /**
99 **      changes the isovalue in a prop3D
100 **/
101 void wxMaracasIRMViewManager::changeIsoValue(int propid, double value )throw(char*){
102         checkInvariant();       
103
104         wxMaracasIRMViewManagerData* data = this->getViewData(propid);          
105
106         //_renderer->RemoveActor(data->getProp3D());
107         data->changeIsoValue(value);
108         //_renderer->AddActor(data->getProp3D());
109
110         _renderer->Render();
111 }
112
113 vtkProp3D* wxMaracasIRMViewManager:: getProp3D(std::string filename){
114         if(filename.compare("")!= 0){
115                 vtkSTLReader *STLReader=vtkSTLReader::New();
116                 STLReader->SetFileName(filename.c_str());
117                 STLReader->Update();
118                 vtkPolyDataMapper* dataMapper = vtkPolyDataMapper::New();
119                 dataMapper->SetInput(STLReader->GetOutput());
120                 
121                 vtkActor* dataActor = vtkActor::New();
122                 dataActor->SetMapper(dataMapper);       
123                 dataActor->GetProperty()->SetOpacity(1);
124                 
125                 return dataActor;
126         }       
127         return NULL;
128 }
129
130 vtkImageData* wxMaracasIRMViewManager::getImageData(std::string filename){
131         if(filename.compare("")!= 0){   
132                 
133                 
134                 vtkMetaImageReader* reader =  vtkMetaImageReader::New();        
135                 reader->SetFileName(filename.c_str());
136                 reader->Update();
137                 vtkImageData* img = reader->GetOutput();
138                 //reader->Delete();
139                 return img;
140         }       
141         return NULL;
142 }
143
144 void wxMaracasIRMViewManager::checkInvariant()  throw(char*){
145         if(this->_renderer==NULL){
146                 throw "Renderer not set";
147         }
148 }
149
150 wxMaracasIRMViewManagerData* wxMaracasIRMViewManager::getViewData(int id) throw(char*){
151     int i;
152         for(i = 0; i < prop3Dvect.size();i++){
153                 if(prop3Dvect[i]->getId() == id){
154                         return prop3Dvect[i];
155                 }
156         }
157         throw "id not found in the data";
158
159         return NULL;
160 }
161
162 int wxMaracasIRMViewManager::getMaxIsoValue(int propid) throw(char*){
163         
164         return this->getViewData(propid)->getMaxGreyLevel();
165
166 }
167
168 void wxMaracasIRMViewManager::changeColor(int propid, double red, double green, double blue) throw(char*){
169         checkInvariant();       
170         this->getViewData(propid)->changeColor(red, green, blue);
171
172         _renderer->Render();
173 }
174
175 void wxMaracasIRMViewManager::deleteActor(int propid) throw (char *){
176         checkInvariant();       
177
178         this->addRemoveActor(propid, false);
179
180         int i,n;
181         bool exit = false;
182         for(i = 0; i < prop3Dvect.size()&&!exit;i++){
183                 if(prop3Dvect[i]->getId() == propid){                   
184                         n=i;
185                         exit = true;
186                 }
187         }
188         if(exit){
189                 wxMaracasIRMViewManagerData* data = prop3Dvect[n];                      
190                 int j;
191                 for(j = i; j < prop3Dvect.size()-1;j++){
192                         prop3Dvect[j] = prop3Dvect[j+1];
193                 }               
194                 delete data;
195                 prop3Dvect.pop_back();
196         }else{
197                 throw "id not found in the data";
198         }
199
200         
201         
202     
203 }
204
205 /********************************************************************************************
206 ** Start of data viewmanagerData
207 *********************************************************************************************/
208
209 wxMaracasIRMViewManagerData::wxMaracasIRMViewManagerData(vtkProp3D* prop3Dvect, std::string dataname){
210
211         _prop3D = prop3Dvect;   
212         _dataname = dataname;
213         _maxgreylevel=-1;
214
215         _cubesFilter=NULL;
216         _cleanFilter=NULL;      
217         _dataMapper=NULL;
218 }
219 wxMaracasIRMViewManagerData::wxMaracasIRMViewManagerData(vtkImageData* imagedata, std::string dataname){
220
221         this->setVtkImageData(imagedata);
222         _dataname = dataname;
223         
224         _maxgreylevel = getMaxLevel(imagedata);
225         _prop3D=NULL;                   
226
227         _cubesFilter = vtkMarchingCubes::New();
228         _cleanFilter = vtkCleanPolyData::New();         
229         _dataMapper = vtkPolyDataMapper::New();
230         vtkActor* dataActor = vtkActor::New();
231         
232         _cubesFilter->SetInput(this->_imagedata);
233         _cubesFilter->ComputeGradientsOn ();
234         _cubesFilter->ComputeScalarsOn ();
235         _cubesFilter->SetNumberOfContours( 1 );
236         _cleanFilter->SetInput ( _cubesFilter->GetOutput() );
237         _dataMapper->SetInput(_cleanFilter->GetOutput());
238         _dataMapper->ScalarVisibilityOff();
239         _dataMapper->ImmediateModeRenderingOn();
240         dataActor->SetMapper(_dataMapper);      
241
242         this->_prop3D = dataActor;
243
244         this->changeIsoValue(this->_maxgreylevel);      
245
246
247 }
248 /**
249 **      changes the isovalue in a prop3D
250 **/
251 void wxMaracasIRMViewManagerData::changeIsoValue(double value){ 
252                 
253    
254     _cubesFilter->SetValue(0,value);            
255         _cubesFilter->Update();    
256         _cleanFilter->Update();
257         _dataMapper->Update();  
258         
259         
260         
261 }
262 int wxMaracasIRMViewManagerData::getMaxGreyLevel(){
263         return _maxgreylevel;
264 }
265 wxMaracasIRMViewManagerData::~wxMaracasIRMViewManagerData(){    
266         if(_cubesFilter!=NULL){
267                 _cubesFilter->Delete();
268                 _cleanFilter->Delete();
269                 _dataMapper->Delete();
270         }
271         _prop3D->Delete();      
272 }
273 /**
274 **      Adds a prop3D to the world of the application
275 **/
276 /**
277         ** Get's the max grey level of the image
278         **/
279 int wxMaracasIRMViewManagerData::getMaxLevel(vtkImageData* img){
280
281         int ext[6], i, j, k,max=0;
282         img->GetExtent(ext);
283
284         for(i = ext[0]; i < ext[1];i++){
285                 for(j = ext[2]; j < ext[3];j++){
286                         for(k = ext[4]; k < ext[5];k++){
287                 unsigned short* ptr = (unsigned short*)img->GetScalarPointer(i,j,k);
288                                 int temp = (int)*ptr;
289                                 if(temp > max){
290                     max = temp;
291                                 }
292                         }
293                 }
294         }
295         return max;
296
297
298 }
299 void wxMaracasIRMViewManagerData::setVtkImageData(vtkImageData* imagedata){
300         _imagedata = imagedata;
301 }
302 /**
303 **      Adds a prop3D to the world of the application
304 **/
305 void wxMaracasIRMViewManagerData::setProp3D(vtkProp3D* prop3D){
306         _prop3D = prop3D;
307 }
308 /**
309 **      Changes the opacity in a prop3D
310 **/
311 void wxMaracasIRMViewManagerData::changeOpacity(int value){
312         std::cout<<"chage op"<<value<<std::endl;
313         vtkActor* actor = (vtkActor*)this->_prop3D;     
314         actor->GetProperty()->SetOpacity((double)value/100.0);
315
316         
317
318 }
319 void wxMaracasIRMViewManagerData::changeColor(double red, double green, double blue){
320         std::cout<<"chage col"<<red<<green<<blue<<std::endl;
321     vtkActor* actor = (vtkActor*)this->_prop3D; 
322         actor->GetProperty()->SetColor(red,green,blue); 
323 }
324 /**
325 **      Check if the variables are setted correctly
326 **/
327 void wxMaracasIRMViewManagerData::checkInvariant(){
328
329 }
330 /**
331 **      get the prop3D 
332 **/
333 vtkProp3D* wxMaracasIRMViewManagerData::getProp3D(){
334         return this->_prop3D;
335 }
336 /**
337 **      return the id from the daat
338 **/
339 int wxMaracasIRMViewManagerData::getId(){
340         return _id;
341 }
342 /**
343 **      set data id
344 **/
345 void wxMaracasIRMViewManagerData::setId(int propid){
346         _id = propid;
347 }
348
349 /**
350 **      Get the filanme
351 **/
352 std::string wxMaracasIRMViewManagerData::getDataname(){
353         return _dataname;
354 }
355 /**
356 ** Set the filanme
357 **/
358 void wxMaracasIRMViewManagerData::setDataname(std::string dataname){
359         _dataname = dataname;
360 }
361
362