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