]> Creatis software - creaMaracasVisu.git/blob - lib/GUI/Base/SurfaceRenderer/wxMaracasSurfaceRenderingManager.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / GUI / Base / SurfaceRenderer / wxMaracasSurfaceRenderingManager.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /*=========================================================================
27
28   Program:   wxMaracas
29   Module:    $RCSfile: wxMaracasSurfaceRenderingManager.cxx,v $
30   Language:  C++
31   Date:      $Date: 2012/11/15 14:17:03 $
32   Version:   $Revision: 1.4 $
33
34   Copyright: (c) 2002, 2003
35   License:
36
37      This software is distributed WITHOUT ANY WARRANTY; without even
38      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
39      PURPOSE.  See the above copyright notice for more information.
40
41 =========================================================================*/
42
43 #include "wxMaracasSurfaceRenderingManager.h"
44 #include "wxMaracasSurfaceRenderingManagerDataMhd.h"
45
46 /**
47 **      Start of the manager class
48 **/
49 wxMaracasSurfaceRenderingManager::wxMaracasSurfaceRenderingManager(){
50         _renderer = NULL;
51         _interactor = NULL;
52         _idCount=0;
53         image = 0;
54 }
55 wxMaracasSurfaceRenderingManager::~wxMaracasSurfaceRenderingManager(){
56 }
57
58 /**
59 **      Sets the renderer to manage the prop3D from the surface render
60 **/
61 void wxMaracasSurfaceRenderingManager::setRenderer(vtkRenderer*  renderer){
62         _renderer = renderer;
63 }
64
65 /**
66 **      Sets the renderer to manage the prop3D from the surface render
67 **/
68 void wxMaracasSurfaceRenderingManager::setInteractor(vtkRenderWindowInteractor*  interactor){
69         _interactor = interactor;
70 }
71
72
73 /**
74 ** Gets the renderer to manage the prop3D from the surface render
75 **/
76 vtkRenderer* wxMaracasSurfaceRenderingManager::getRenderer(){
77         return _renderer;
78 }
79
80 /**
81 ** Updates volume
82 **/
83 void wxMaracasSurfaceRenderingManager::Update(int pid)throw(char*){
84         wxMaracasSurfaceRenderingManagerData* data = this->getViewData(pid);    
85         ((wxMaracasSurfaceRenderingManagerDataMhd*)data)->UpdateSurface();
86         _renderer->Render();
87 }
88
89 /**
90 **      Adds a prop3D to the manager and returns the identifier
91 **/
92 int wxMaracasSurfaceRenderingManager::addProp3D(int idTP, vtkProp3D* prop3D, std::string dataname)  throw(char*){
93         //checkInvariant();
94         if(prop3D != NULL){
95                 wxMaracasSurfaceRenderingManagerData* data = new wxMaracasSurfaceRenderingManagerData(prop3D, dataname, _interactor);
96                 prop3Dvect.push_back(data);
97                 _renderer->AddActor(data->getProp3D());
98                 if(idTP == -1)
99                 {
100                         data->setId(_idCount);
101                         _idCount++;
102                 }
103                 else
104                 {
105                         data->setId(idTP);
106                 }
107                 return data->getId();
108         }else{
109                 throw "Check vtkProp3D file or input";
110         }
111         return -1;
112 }
113
114 /**
115 **      Adds a polydata and creates the prop3D (actor) to the manager and returns the identifier
116 **/
117 /**
118 **      Adds a polydata and creates the prop3D (actor) to the manager and returns the identifier
119 **/
120 int wxMaracasSurfaceRenderingManager::addProp3D(int idTP, std::string filename, std::string dataname){
121
122
123     wxMaracasSurfaceRenderingManagerData* data = new wxMaracasSurfaceRenderingManagerData(filename, _interactor);
124     if(dataname.compare("") != 0)
125         data->setDataname(dataname);
126
127     prop3Dvect.push_back(data);
128     _renderer->AddActor(data->getProp3D());
129     if(idTP == -1)
130     {
131             data->setId(_idCount);
132             _idCount++;
133     }
134     else
135     {
136             data->setId(idTP);
137     }
138     return data->getId();
139
140 }
141
142 int wxMaracasSurfaceRenderingManager::addPropMHD(int idTP, vtkImageData* imagedata, std::string dataname) throw(char*){
143         checkInvariant();
144         if(imagedata != NULL){                
145                 image = imagedata;
146                 wxMaracasSurfaceRenderingManagerData* data = new wxMaracasSurfaceRenderingManagerDataMhd(imagedata, dataname, _interactor);
147                 prop3Dvect.push_back(data);     
148                 _renderer->AddActor(data->getProp3D());
149                 if(idTP == -1)
150                 {
151                         data->setId(_idCount);
152                         _idCount++;
153                 }
154                 else
155                 {
156                         data->setId(idTP);
157                 }
158                 printf("wxMaracasSurfaceRenderingManager::addPropMHD->idSurfaceRender: %i\n", data->getId());
159                 return data->getId();
160         }else{
161                 throw "Check ImageData file or input";
162         }
163         return -1;
164 }
165 /**
166 **      adds or removes an actor depending of the bool value
167 **/
168 bool wxMaracasSurfaceRenderingManager::addRemoveActor(int propid, bool addremove)  throw(char*){
169         checkInvariant();
170         
171         wxMaracasSurfaceRenderingManagerData* data = this->getViewData(propid);
172         if(data && data->getProp3D()!=NULL){
173                 if(addremove){
174                         _renderer->AddViewProp(data->getProp3D());
175                 }else{
176                         _renderer->RemoveViewProp(data->getProp3D());
177                 }
178                 _renderer->Render();
179                 return true;
180         }else {
181             if(addremove && propid == 0 && image){
182                 addPropMHD(0, image, "id0");
183                 return true;
184             }
185         }
186         return false;
187 }
188 /**
189 **      adds or removes the surface box depending of the bool value
190 **/
191 void wxMaracasSurfaceRenderingManager::addRemoveSurfaceBox(int propid, bool addremove)  throw(char*){
192         checkInvariant();
193         
194         wxMaracasSurfaceRenderingManagerData* data = this->getViewData(propid);         
195         if(data->getProp3D()!=NULL){
196                 data->addRemoveSurfaceBox(addremove);
197                 /*if(addremove){
198                         data->
199                         _renderer->AddViewProp(data->getProp3D());
200                 }else{
201                         _renderer->RemoveViewProp(data->getProp3D());
202                 }
203                 _renderer->Render();*/
204         }
205         
206 }
207
208 /**
209 **      Changes the opacity in a prop3D
210 **/
211 void wxMaracasSurfaceRenderingManager::changeOpacity(int propid, int value)  throw(char*){
212         checkInvariant();       
213
214
215         this->getViewData(propid)->changeOpacity(value);
216         
217         _renderer->Render();
218
219 }
220
221 /**
222 **      changes the isovalue in a prop3D
223 **/
224 void wxMaracasSurfaceRenderingManager::changeIsoValue(int propid, double value ){
225         checkInvariant();       
226
227         wxMaracasSurfaceRenderingManagerData* data = this->getViewData(propid);         
228
229         //_renderer->RemoveActor(data->getProp3D());
230         ((wxMaracasSurfaceRenderingManagerDataMhd*)data)->changeIsoValue(value);
231         //_renderer->AddActor(data->getProp3D());
232
233         _renderer->Render();
234 }
235
236 /**
237 **      changes the isovalue in a prop3D
238 **/
239 void wxMaracasSurfaceRenderingManager::changeIsoValue(int propid, double min, double max ){
240         checkInvariant();
241
242         wxMaracasSurfaceRenderingManagerData* data = this->getViewData(propid);
243
244         //_renderer->RemoveActor(data->getProp3D());
245         ((wxMaracasSurfaceRenderingManagerDataMhd*)data)->changeIsoValue(min, max);
246         //_renderer->AddActor(data->getProp3D());
247
248         _renderer->Render();
249 }
250
251 /*vtkProp3D* wxMaracasSurfaceRenderingManager:: getProp3D(std::string filename){
252         if(filename.compare("")!= 0){
253
254             vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
255             std::string ext = filename.substr(filename.find_last_of("."), 4);
256             if(ext.compare(STL)==0){
257                 vtkSmartPointer<vtkSTLReader> STLReader=vtkSmartPointer<vtkSTLReader>::New();
258                 STLReader->SetFileName(filename.c_str());
259                 STLReader->Update();
260                 polydata->DeepCopy(STLReader->GetOutput());
261
262             }else if(ext.compare(PLY)==0){
263                 vtkSmartPointer<vtkPLYReader> plyreader =vtkSmartPointer<vtkPLYReader>::New();
264                 plyreader->SetFileName(filename.c_str());
265                 plyreader->Update();
266                 polydata->DeepCopy(plyreader->GetOutput());
267             }else if(ext.compare(VTK)==0){
268                 vtkSmartPointer<vtkPolyDataReader> polyreader =vtkSmartPointer<vtkPolyDataReader>::New();
269                 polyreader->SetFileName(filename.c_str());
270                 polyreader->Update();
271                 polydata->DeepCopy(polyreader->GetOutput());
272             }
273
274             vtkSmartPointer<vtkPolyDataMapper> dataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
275             dataMapper->SetInput(polydata);
276
277             vtkActor* dataActor = vtkActor::New();
278             dataActor->SetMapper(dataMapper);
279             dataActor->GetProperty()->SetOpacity(1);
280             return dataActor;
281         }       
282         return NULL;
283 }*/
284
285 vtkImageData* wxMaracasSurfaceRenderingManager::getImageData(std::string filename){
286         if(filename.compare("")!= 0){   
287                 
288                 vtkMetaImageReader* reader =  vtkMetaImageReader::New();        
289                 reader->SetFileName(filename.c_str());
290                 reader->Update();
291                 vtkImageData* img = reader->GetOutput();
292                 //reader->Delete();
293                 return img;
294         }       
295         return NULL;
296 }
297
298 vtkImageData* wxMaracasSurfaceRenderingManager::getImageData(){
299         return image;
300 }
301
302 void wxMaracasSurfaceRenderingManager::setImageData(vtkImageData* img){
303     image = img;
304 }
305
306 void wxMaracasSurfaceRenderingManager::checkInvariant()  throw(char*){
307         if(this->_renderer==NULL){
308                 throw "Renderer not set";
309         }
310 }
311
312 wxMaracasSurfaceRenderingManagerData* wxMaracasSurfaceRenderingManager::getViewData(int id) throw(char*){
313     int i = 0;
314     for(i = 0; i < (int)(prop3Dvect.size());i++){
315             if(prop3Dvect[i]->getId() == id){
316                     return prop3Dvect[i];
317             }
318     }    
319 #ifndef USE_QT
320     throw "id not found in the data";
321 #else
322     return 0;
323 #endif
324
325
326 }
327
328 int wxMaracasSurfaceRenderingManager::getMaxIsoValue(int propid) throw(char*){
329         
330         return ((wxMaracasSurfaceRenderingManagerDataMhd*)this->getViewData(propid))->getMaxGreyLevel();
331
332 }
333
334 void wxMaracasSurfaceRenderingManager::changeColor(int propid, double red, double green, double blue) throw(char*){
335         checkInvariant();       
336         this->getViewData(propid)->changeColor(red, green, blue);
337
338         _renderer->Render();
339 }
340
341 void wxMaracasSurfaceRenderingManager::deleteActor(int propid) throw (char *){
342         checkInvariant();       
343
344         this->addRemoveActor(propid, false);
345
346         int i,n;
347         bool exit = false;
348         for(i = 0; i < (int)(prop3Dvect.size())&&!exit;i++){
349                 if(prop3Dvect[i]->getId() == propid){                   
350                         n=i;
351                         exit = true;
352                 }
353         }
354         if(exit){
355                 wxMaracasSurfaceRenderingManagerData* data = prop3Dvect[n];                     
356                 int j;
357                 for(j = i; j < (int)(prop3Dvect.size())-1;j++){
358                         prop3Dvect[j] = prop3Dvect[j+1];
359                 }               
360                 delete data;
361                 prop3Dvect.pop_back();
362         }else{
363 #ifndef USE_QT
364                 throw "id not found in the data";
365 #endif
366
367         }   
368 }
369
370 void wxMaracasSurfaceRenderingManager::enableBoundingBox(int propid, bool enable){
371     checkInvariant();
372
373     wxMaracasSurfaceRenderingManagerData* data = this->getViewData(propid);
374
375     if(data)
376         data->enableBoxWidget(enable);
377 }
378 void wxMaracasSurfaceRenderingManager::Transform(vtkMatrix4x4* tmatrix){
379         
380 }
381
382 bool wxMaracasSurfaceRenderingManager::interactorSet(){
383         return _interactor? true:false;
384 }
385
386 void wxMaracasSurfaceRenderingManager::saveProp3DSTL(int propid,const char* filename){
387     checkInvariant();
388
389     wxMaracasSurfaceRenderingManagerData* data = this->getViewData(propid);
390     if(data->getProp3D()!=NULL){
391             data->saveProp3DSTL(filename);
392     }
393 }
394
395 void wxMaracasSurfaceRenderingManager::loadProp3DSTL(const char* filename){
396     this->addProp3D(-1, filename);
397 }
398
399 void wxMaracasSurfaceRenderingManager::exportImageStencil(int propid,const char* filename){
400     checkInvariant();
401
402     wxMaracasSurfaceRenderingManagerData* data = this->getViewData(propid);
403     if(data && data->getProp3D()!=NULL){
404             data->exportImageStencil(filename);
405     }
406 }