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