]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/wxMaracasMultipleVolumeRendererManager.cxx
2108 Bug Volume Rendering widget options are not stable
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / wxMaracasMultipleVolumeRendererManager.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: wxMaracasMultipleVolumeRendererManager.cxx,v $
30   Language:  C++
31   Date:      $Date: 2012/11/15 14:14:35 $
32   Version:   $Revision: 1.10 $
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 #include "wxMaracasMultipleVolumeRendererManager.h"
43
44 #include "vtkRenderWindow.h"
45 #include <vtkMetaImageReader.h>
46 #include <vtkImageCast.h>
47 /**
48 **      Start of the manager class
49 **/
50 wxMaracasMultipleVolumeRendererManager::wxMaracasMultipleVolumeRendererManager(){
51         _renderer = NULL;
52         _idCount=0;
53 }
54 wxMaracasMultipleVolumeRendererManager::~wxMaracasMultipleVolumeRendererManager(){
55 }
56
57 /**
58 **      Sets the renderer to manage the prop3D from the view
59 **/
60 void wxMaracasMultipleVolumeRendererManager::setRenderer(vtkRenderer*  renderer){
61         _renderer = renderer;
62 }
63
64 /**
65 ** Gets the renderer which manage the prop3D from the view
66 **/
67 vtkRenderer* wxMaracasMultipleVolumeRendererManager::getRenderer(){
68         return _renderer;
69 }
70
71 /**
72 ** Updates Volume
73 **/
74 void wxMaracasMultipleVolumeRendererManager::Update(int ppid){
75         wxMaracasMultipleVolumeRendererManagerData* data = this->getViewData(ppid);
76         data->Update();
77         _renderer->GetRenderWindow()->Render();
78 }
79
80 /**
81 **      Adds a prop3D to the manager and returns the identifier
82 **/
83 int wxMaracasMultipleVolumeRendererManager::addVolume(int idTP, vtkImageData* vol, std::string dataname) throw(char*){
84         checkInvariant();
85         image = vol;
86         if(vol != NULL){
87                 wxMaracasMultipleVolumeRendererManagerData* data = new wxMaracasMultipleVolumeRendererManagerData(vol, dataname);
88                 prop3Dvect.push_back(data);
89                 _renderer->AddActor(data->getProp3D());
90                 if(idTP == -1)
91                 {
92                         data->setId(_idCount);
93                         _idCount++;
94                 }
95                 else
96                 {
97                         data->setId(idTP);
98                 }
99                 printf("wxMaracasMultipleVolumeRendererManager::addVolume->idVolumeRenderer: %i\n", data->getId());
100                 return data->getId();
101         }else{
102                 throw "Check mhd imagefile file or input";
103         }
104         return -1;
105 }
106 /**
107 **      adds or removes an actor depending of the bool value
108 **/
109
110 void wxMaracasMultipleVolumeRendererManager::addRemoveActor(int propid, bool addremove)  throw(char*)
111 {
112         checkInvariant();
113         
114         wxMaracasMultipleVolumeRendererManagerData* data = this->getViewData(propid);           
115         if(data->getProp3D()!=NULL){
116                 if(addremove){
117                         _renderer->AddViewProp(data->getProp3D());
118                 }else{
119                         _renderer->RemoveViewProp(data->getProp3D());
120                 }
121                 _renderer->GetRenderWindow()->Render();
122         }
123         
124 }
125
126
127 /**
128 **      Changes the opacity in a prop3D
129 **/
130 void wxMaracasMultipleVolumeRendererManager::setVolumeOpacity(int propid, std::vector<double> greylevel,std::vector<double> value)  throw(char*)
131 {
132         checkInvariant();               
133
134         this->getViewData(propid)->setVolumeOpacity(greylevel, value);
135         
136         _renderer->GetRenderWindow()->Render();
137
138 }
139
140 /**
141 **      Set Volume Color
142 **/
143 void wxMaracasMultipleVolumeRendererManager::setVolumeColor(int volid, std::vector<double> greylevel,
144                                                                 std::vector<double> red,
145                                                                 std::vector<double> green,
146                                                                 std::vector<double> blue)throw(char*)
147 {
148         checkInvariant();               
149
150         this->getViewData(volid)->setVolumeColor(greylevel, red, green, blue);
151         
152         _renderer->GetRenderWindow()->Render();
153 }
154
155
156 vtkImageData* wxMaracasMultipleVolumeRendererManager::getImageData(std::string filename)
157 {
158         if(filename.compare("")!= 0){   
159                 
160                 vtkMetaImageReader* reader =  vtkMetaImageReader::New();        
161                 reader->SetFileName(filename.c_str());
162                 reader->Update();
163                 vtkImageData* img = reader->GetOutput();        
164
165                 vtkImageCast* cast = vtkImageCast::New(); 
166                 cast->SetInput(img);
167                 cast->SetOutputScalarTypeToUnsignedShort();
168                 cast->Update();                 
169                 //reader->Delete();
170                 //img->Delete();
171                 return cast->GetOutput();
172                 //return img;
173         }       
174         return NULL;
175 }
176
177
178 vtkImageData* wxMaracasMultipleVolumeRendererManager::getImageData()
179 {
180         return image;
181 }
182
183
184 void wxMaracasMultipleVolumeRendererManager::checkInvariant()  throw(char*)
185 {
186         if(this->_renderer==NULL){
187                 throw "Renderer not set";
188         }
189 }
190
191
192 wxMaracasMultipleVolumeRendererManagerData* wxMaracasMultipleVolumeRendererManager::getViewData(int id) throw(char*)
193 {
194     int i;
195         for(i = 0; i < (int)(prop3Dvect.size());i++){
196                 if(prop3Dvect[i]->getId() == id){
197                         return prop3Dvect[i];
198                 }
199         }
200         throw "id not found in the data";
201
202         return NULL;
203 }
204
205
206 void wxMaracasMultipleVolumeRendererManager::deleteActor(int propid) throw (char *)
207 {
208         checkInvariant();       
209
210         this->addRemoveActor(propid, false);
211
212         int i,n;
213         bool exit = false;
214         for(i = 0; i < (int)(prop3Dvect.size())&&!exit;i++){
215                 if(prop3Dvect[i]->getId() == propid){                   
216                         n=i;
217                         exit = true;
218                 }
219         }
220         if(exit){
221                 wxMaracasMultipleVolumeRendererManagerData* data = prop3Dvect[n];                       
222                 int j;
223                 for(j = i; j < (int)(prop3Dvect.size())-1;j++){
224                         prop3Dvect[j] = prop3Dvect[j+1];
225                 }               
226                 delete data;
227                 prop3Dvect.pop_back();
228         }else{
229                 throw "id not found in the data";
230         }       
231     
232 }
233
234
235 vtkPiecewiseFunction* wxMaracasMultipleVolumeRendererManager::GetTransferFunction(int volumeid)
236 {
237         return getViewData(volumeid)->GetTransferFunction();
238 }
239
240
241 vtkColorTransferFunction* wxMaracasMultipleVolumeRendererManager::GetColorFunction(int volumeid)
242 {
243
244         return getViewData(volumeid)->GetColorFunction();
245 }
246
247
248 void wxMaracasMultipleVolumeRendererManager::changeCompositeMIPFunction(int id, int function) throw (char *)
249 {
250         getViewData(id)->changeCompositeMIPFunction(function);
251         Update(id);
252 }