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