]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/VolumeRenderer/volumerenderermanagerdata.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / Kernel / VTKObjects / VolumeRenderer / volumerenderermanagerdata.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 #include "volumerenderermanagerdata.h"
27
28 /********************************************************************************************
29 ** Start of data viewmanagerData
30 *********************************************************************************************/
31
32 #include "vtkStripper.h"
33 #include "boxSurfaceObserver.h"
34 #include "vtkProperty.h"
35
36 #include <vtkVolumeRayCastMapper.h>
37
38 #if VTK_MAJOR_VERSION >= 5
39     #if VTK_MINOR_VERSION >= 6
40         #include "vtkGPUVolumeRayCastMapper.h"
41     #endif
42 #endif
43
44 using namespace std;
45
46 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, bool usegpu){
47     Initialize(vol, "", usegpu);
48 }
49
50 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, std::string dataname){
51
52     Initialize(vol, dataname);
53
54
55 }
56
57 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, vtkRenderer* render, std::string dataname){
58
59     Initialize(vol, dataname);
60     render->AddActor(this->getProp3D());
61
62 }
63
64 void VolumeRendererManagerData::Initialize(vtkImageData* vol, std::string dataname, bool usegpu){
65     _id = 0;
66     _vol = vol;
67     _dataname = dataname;
68
69
70     _volumePlanes  = vtkPlanes::New();
71
72     _volumeProperty = vtkVolumeProperty::New();
73     _volumeProperty->SetInterpolationTypeToLinear();
74     _volumeProperty->ShadeOn();
75     _volumeProperty->SetAmbient(3);
76     _volumeProperty->SetDiffuse(0.2);
77     _volumeProperty->SetSpecular(0.5);
78     //_volumeProperty->DisableGradientOpacityOn();
79     _tfun = vtkPiecewiseFunction::New();    
80     _volumeProperty->SetScalarOpacity(_tfun );
81     _ctfun = vtkColorTransferFunction::New();
82
83     if(vol->GetNumberOfScalarComponents() == 1 ){
84
85         _ctfun->AddRGBPoint(vol->GetScalarRange()[0], 0, 0 , 0);
86         _ctfun->AddRGBPoint(vol->GetScalarRange()[1], 1, 1 , 1);
87         _ctfun->Build();
88         _volumeProperty->SetColor(_ctfun);
89     }
90
91     _newvol = vtkVolume::New();
92     _newvol->SetProperty(_volumeProperty );    
93
94
95     _compositeFunction = 0;
96     _MIPFunction = 0;
97
98     BoxWidget = 0;
99
100 /* EED9Juin2011
101     if(usegpu && _vol->GetDataDimension() > 2){
102         _volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
103         _volumeMappergpu->SetClippingPlanes( _volumePlanes );
104         _volumeMappergpu->AutoAdjustSampleDistancesOn();
105
106         _newvol->SetMapper(_volumeMappergpu );
107
108         _volumeMappergpu->SetInput( _vol );
109         _volumeMappergpu->Update();
110     }else{
111         _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
112         _MIPFunction = vtkVolumeRayCastMIPFunction::New();
113
114         _volumeMapper = vtkVolumeRayCastMapper::New();
115         _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
116         _volumeMapper->SetClippingPlanes( _volumePlanes );
117         _volumeMapper->AutoAdjustSampleDistancesOn();
118
119         _newvol->SetMapper(_volumeMapper );
120
121         _volumeMapper->SetInput( _vol );
122         _volumeMapper->Update();
123     }
124 */
125
126        VolumeMapper = 0;
127 #if VTK_MAJOR_VERSION >= 5
128     #if VTK_MINOR_VERSION >= 6
129        cout<<"using volume render GPU"<<endl;
130        vtkGPUVolumeRayCastMapper * volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
131        volumeMappergpu->AutoAdjustSampleDistancesOn();       
132        VolumeMapper = volumeMappergpu;
133     #endif
134 #else
135
136
137        _compositeFunction                                                               = vtkVolumeRayCastCompositeFunction::New();
138        _MIPFunction                                                                     = vtkVolumeRayCastMIPFunction::New();
139        vtkVolumeRayCastMapper* volumeMapper                                                                     = vtkVolumeRayCastMapper::New();
140        volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
141                volumeMapper->AutoAdjustSampleDistancesOn();
142                VolumeMapper                                                                     = volumeMapper;
143 #endif
144
145
146         VolumeMapper->SetClippingPlanes( _volumePlanes );
147         _newvol->SetMapper(VolumeMapper );
148         VolumeMapper->SetInput( _vol );
149         VolumeMapper->Update();
150     _newvol->Update();
151 }
152
153
154 VolumeRendererManagerData::~VolumeRendererManagerData()
155 {
156
157         _tfun->Delete();
158         _ctfun->Delete();
159         _volumePlanes->Delete();
160         _volumeProperty->Delete();
161         _newvol->Delete();
162
163         if(_compositeFunction)
164             _compositeFunction->Delete();
165         if(_MIPFunction)
166             _MIPFunction->Delete();
167         if(VolumeMapper)
168             VolumeMapper->Delete();
169
170         if(BoxWidget){
171             DisableBoundingBox();
172         }
173
174 }
175 void VolumeRendererManagerData::SetIndependentComponents(bool independent){
176
177     if(independent){
178         _volumeProperty->IndependentComponentsOn();
179     }else{
180         _volumeProperty->IndependentComponentsOff();
181     }
182 }
183
184 void VolumeRendererManagerData::EnableBoundingBox(vtkRenderWindowInteractor* interactor)
185 {
186 //EED9Juin2011    if(_volumeMappergpu){
187
188     if(!BoxWidget){
189         BoxWidget = vtkBoxWidget::New();
190         BoxWidget->SetInteractor( interactor );
191         BoxWidget->SetPlaceFactor(1.01);
192
193         BoxWidget->SetInput( _vol );
194         BoxWidget->InsideOutOn();
195         BoxWidget->PlaceWidget();
196
197
198         vtkBoxWidgetCallback *callback = vtkBoxWidgetCallback::New();
199
200
201         callback->SetMapper(VolumeMapper);
202
203
204         BoxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
205         callback->Delete();
206
207         BoxWidget->EnabledOn();
208         BoxWidget->GetSelectedFaceProperty()->SetOpacity(0.0);
209
210         cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox::CREATE"<<endl;
211
212     }else{
213
214         BoxWidget->EnabledOn();
215
216         cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox"<<endl;
217
218     }
219 //EED9Juin2011  }
220 }
221
222 void VolumeRendererManagerData::DisableBoundingBox(){
223
224     if(BoxWidget){
225         BoxWidget->EnabledOff();
226         //BoxWidget->Delete();
227         //BoxWidget = 0;
228
229         cout<<"JPRG::VolumeRendererManagerData::DisableBoundingBox"<<endl;
230
231
232     }
233 }
234
235 /**
236 **      Set Volume Color
237 **/
238 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
239                                                                 std::vector<double>& red,
240                                                                 std::vector<double>& green,
241                                                                 std::vector<double>& blue)
242 {
243
244         _ctfun->RemoveAllPoints();
245         for(unsigned int i = 0; i < greylevelcolors.size();i++){
246                 _ctfun->AddRGBPoint(greylevelcolors[i], red[i],green[i], blue[i]);
247                                 //std::cout<<"VolumeRendererManagerData::setVolumeColor "<<greylevelcolors[i]<<" "<<red[i]<<" "<<green[i]<<" "<<blue[i]<<std::endl;
248         }
249         _newvol->Update();
250 }
251
252 /**
253 **      Volume Opacity
254 **/
255 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value){
256
257         _tfun->RemoveAllPoints();
258         for(unsigned int i = 0; i < greylevel.size();i++){
259                 _tfun->AddPoint(greylevel[i], value[i]);
260                 //std::cout<<"VolumeRendererManagerData::setVolumeOpacity "<<greylevel[i]<<" "<< value[i]<<std::endl;
261         }
262     _newvol->Update();
263 }
264
265 /**
266 **      Volume Opacity
267 **/
268 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,double value){
269     std::vector<double> valuevector;
270     for(unsigned i = 0; i < greylevel.size(); i++){
271         valuevector.push_back(value);
272     }
273     setVolumeOpacity(greylevel, valuevector);
274 }
275
276 /**
277 **      Check if the variables are setted correctly
278 **/
279 void VolumeRendererManagerData::checkInvariant()throw (char *){
280         if(!_compositeFunction){
281                 throw "No composite function initialized";
282         }
283         if(!_MIPFunction){
284                 throw "No MIP function initialized";
285         }
286         if(!VolumeMapper){
287                 throw "No volume mapper initialized";
288         }
289 }
290
291 /**
292 **      Check if the variables are setted correctly
293 **/
294 void VolumeRendererManagerData::Update(){
295          _newvol->Update();
296 }
297 /**
298 **      get the prop3D
299 **/
300 vtkProp3D* VolumeRendererManagerData::getProp3D(){
301         return this->_newvol;
302 }
303 /**
304 **      return the id from the daat
305 **/
306 int VolumeRendererManagerData::getId(){
307         return _id;
308 }
309 /**
310 **      set data id
311 **/
312 void VolumeRendererManagerData::setId(int propid){
313         _id = propid;
314 }
315
316 /**
317 **      Get the filanme
318 **/
319 std::string VolumeRendererManagerData::getDataname(){
320         return _dataname;
321 }
322 /**
323 ** Set the filanme
324 **/
325 void VolumeRendererManagerData::setDataname(std::string dataname){
326         _dataname = dataname;
327 }
328
329 void VolumeRendererManagerData::changeCompositeMIPFunction(int function){
330         //checkInvariant();
331         if(VolumeMapper){
332 #if VTK_MAJOR_VERSION >= 5
333     #if VTK_MINOR_VERSION >= 6
334             if(dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper)){
335                 vtkVolumeRayCastMapper* volumemapper = dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper);
336                 if(function == 0){
337                         volumemapper->SetVolumeRayCastFunction(_compositeFunction);
338                 }else{
339                         volumemapper->SetVolumeRayCastFunction(_MIPFunction);
340                 }
341             }else if(dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper)){
342                 vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
343                 if(function == 0){
344                     volumemapper->SetBlendModeToComposite();
345                 }else if(function == 1){
346                     volumemapper->SetBlendModeToMaximumIntensity();
347                 }else if(function == 2){
348                     volumemapper->SetBlendModeToMinimumIntensity();
349                 }
350             }
351     #endif
352 #else
353             vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
354             if(function == 0){
355                 volumemapper->SetBlendModeToComposite();
356             }else if(function == 1){
357                 volumemapper->SetBlendModeToMaximumIntensity();
358             }else if(function == 2){
359                 volumemapper->SetBlendModeToMinimumIntensity();
360             }
361 #endif
362         }
363
364 }
365
366 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable){
367     if(lookuptable){
368          _ctfun->RemoveAllPoints();
369          vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
370          for(int i = 0; i < colort->GetSize(); i++){
371              double val[6];
372              colort->GetNodeValue(i, val);
373              cout<< "JPRG::VolumeRendererManagerData::SetLookupTable::"<<val[0]<<" "<< val[1]<<" "<<val[2]<<" "<< val[3]<<endl;
374              _ctfun->AddRGBPoint(val[0], val[1], val[2], val[3]);
375          }
376          _newvol->Update();
377     }
378
379 }
380
381 void VolumeRendererManagerData::changeInterpolationType(int type){
382     //checkInvariant();
383     if(type == 0){
384         _volumeProperty->SetInterpolationTypeToLinear();
385     }else if(type == 1){
386         _volumeProperty->SetInterpolationTypeToNearest();
387     }
388 }
389
390 void VolumeRendererManagerData::SetColorTransferFunction(int i, vtkColorTransferFunction* colorf){
391
392     _volumeProperty->SetColor(i, colorf);
393
394 }