1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
26 #include "volumerenderermanagerdata.h"
28 /********************************************************************************************
29 ** Start of data viewmanagerData
30 *********************************************************************************************/
32 #include "vtkStripper.h"
33 #include "boxSurfaceObserver.h"
34 #include "vtkProperty.h"
36 #include <vtkVolumeRayCastMapper.h>
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"
45 #if VTK_MAJOR_VERSION >= 6
46 #include "vtkSmartVolumeMapper.h"
53 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, bool usegpu){
54 Initialize(vol, "", usegpu);
57 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, std::string dataname){
59 Initialize(vol, dataname);
64 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, vtkRenderer* render, std::string dataname){
66 Initialize(vol, dataname);
67 render->AddActor(this->getProp3D());
71 void VolumeRendererManagerData::Initialize(vtkImageData* vol, std::string dataname, bool usegpu){
77 _volumePlanes = vtkPlanes::New();
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();
90 if(vol->GetNumberOfScalarComponents() == 1 ){
92 _ctfun->AddRGBPoint(vol->GetScalarRange()[0], 0, 0 , 0);
93 _ctfun->AddRGBPoint(vol->GetScalarRange()[1], 1, 1 , 1);
95 _volumeProperty->SetColor(_ctfun);
98 _newvol = vtkVolume::New();
99 _newvol->SetProperty(_volumeProperty );
102 _compositeFunction = 0;
108 if(usegpu && _vol->GetDataDimension() > 2){
109 _volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
110 _volumeMappergpu->SetClippingPlanes( _volumePlanes );
111 _volumeMappergpu->AutoAdjustSampleDistancesOn();
113 _newvol->SetMapper(_volumeMappergpu );
115 _volumeMappergpu->SetInput( _vol );
116 _volumeMappergpu->Update();
118 _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
119 _MIPFunction = vtkVolumeRayCastMIPFunction::New();
121 _volumeMapper = vtkVolumeRayCastMapper::New();
122 _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
123 _volumeMapper->SetClippingPlanes( _volumePlanes );
124 _volumeMapper->AutoAdjustSampleDistancesOn();
126 _newvol->SetMapper(_volumeMapper );
128 _volumeMapper->SetInput( _vol );
129 _volumeMapper->Update();
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;
144 // _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
145 // _MIPFunction = vtkVolumeRayCastMIPFunction::New();
146 // vtkVolumeRayCastMapper* volumeMapper = vtkVolumeRayCastMapper::New();
147 // volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
148 // volumeMapper->AutoAdjustSampleDistancesOn();
149 // VolumeMapper = volumeMapper;
152 #if VTK_MAJOR_VERSION >= 6
153 cout<<"using SmartVolumeRenderingMapper render GPU"<<endl;
154 vtkSmartVolumeMapper * smartvolumemapper = vtkSmartVolumeMapper::New();
155 VolumeMapper = smartvolumemapper;
156 smartvolumemapper->AutoAdjustSampleDistancesOn();
158 _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
159 _MIPFunction = vtkVolumeRayCastMIPFunction::New();
160 vtkVolumeRayCastMapper* volumeMapper = vtkVolumeRayCastMapper::New();
161 VolumeMapper = volumeMapper;
162 volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
163 volumeMapper->AutoAdjustSampleDistancesOn();
165 VolumeMapper->SetClippingPlanes( _volumePlanes );
166 _newvol->SetMapper(VolumeMapper );
167 VolumeMapper->SetInput( _vol );
168 VolumeMapper->Update();
173 VolumeRendererManagerData::~VolumeRendererManagerData()
177 _volumePlanes->Delete();
178 _volumeProperty->Delete();
181 if(_compositeFunction)
182 _compositeFunction->Delete();
184 _MIPFunction->Delete();
186 VolumeMapper->Delete();
189 DisableBoundingBox();
194 void VolumeRendererManagerData::SetIndependentComponents(bool independent)
198 _volumeProperty->IndependentComponentsOn();
200 _volumeProperty->IndependentComponentsOff();
205 void VolumeRendererManagerData::EnableBoundingBox(vtkRenderWindowInteractor* interactor)
207 //EED9Juin2011 if(_volumeMappergpu){
211 BoxWidget = vtkBoxWidget::New();
212 BoxWidget->SetInteractor( interactor );
213 BoxWidget->SetPlaceFactor(1.01);
215 BoxWidget->SetInput( _vol );
216 BoxWidget->InsideOutOn();
217 BoxWidget->PlaceWidget();
220 vtkBoxWidgetCallback *callback = vtkBoxWidgetCallback::New();
223 callback->SetMapper(VolumeMapper);
226 BoxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
229 BoxWidget->EnabledOn();
230 BoxWidget->GetSelectedFaceProperty()->SetOpacity(0.0);
232 cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox::CREATE"<<endl;
236 BoxWidget->EnabledOn();
238 cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox"<<endl;
244 void VolumeRendererManagerData::DisableBoundingBox()
248 BoxWidget->EnabledOff();
249 //BoxWidget->Delete();
251 cout<<"JPRG::VolumeRendererManagerData::DisableBoundingBox"<<endl;
258 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
259 std::vector<double>& red,
260 std::vector<double>& green,
261 std::vector<double>& blue)
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;
277 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value)
279 _tfun->RemoveAllPoints();
280 for(unsigned int i = 0; i < greylevel.size();i++)
282 _tfun->AddPoint(greylevel[i], value[i]);
283 //std::cout<<"VolumeRendererManagerData::setVolumeOpacity "<<greylevel[i]<<" "<< value[i]<<std::endl;
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);
297 setVolumeOpacity(greylevel, valuevector);
302 ** Check if the variables are setted correctly
304 void VolumeRendererManagerData::checkInvariant()throw (char *){
305 if(!_compositeFunction){
306 throw "No composite function initialized";
309 throw "No MIP function initialized";
312 throw "No volume mapper initialized";
318 ** Check if the variables are setted correctly
320 void VolumeRendererManagerData::Update()
329 vtkProp3D* VolumeRendererManagerData::getProp3D()
331 return this->_newvol;
335 ** return the id from the daat
337 int VolumeRendererManagerData::getId()
346 void VolumeRendererManagerData::setId(int propid)
355 std::string VolumeRendererManagerData::getDataname()
364 void VolumeRendererManagerData::setDataname(std::string dataname)
366 _dataname = dataname;
370 void VolumeRendererManagerData::changeCompositeMIPFunction(int function)
373 if(VolumeMapper!=NULL){
374 #if VTK_MAJOR_VERSION >= 6
375 if(dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper))
377 vtkVolumeRayCastMapper* volumemapper = dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper);
379 volumemapper->SetVolumeRayCastFunction(_compositeFunction);
381 volumemapper->SetVolumeRayCastFunction(_MIPFunction);
383 }else if(dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper)){
384 vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
386 volumemapper->SetBlendModeToComposite();
387 }else if(function == 1){
388 volumemapper->SetBlendModeToMaximumIntensity();
389 }else if(function == 2){
390 volumemapper->SetBlendModeToMinimumIntensity();
394 vtkVolumeRayCastMapper* volumemapper = dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper);
396 volumemapper->SetBlendModeToComposite();
397 }else if(function == 1){
398 volumemapper->SetBlendModeToMaximumIntensity();
399 }else if(function == 2){
400 volumemapper->SetBlendModeToMinimumIntensity();
407 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable)
410 _ctfun->RemoveAllPoints();
411 vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
412 for(int i = 0; i < colort->GetSize(); i++){
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]);
423 void VolumeRendererManagerData::changeInterpolationType(int type)
427 _volumeProperty->SetInterpolationTypeToLinear();
429 _volumeProperty->SetInterpolationTypeToNearest();
433 void VolumeRendererManagerData::SetColorTransferFunction(int i, vtkColorTransferFunction* colorf){
435 _volumeProperty->SetColor(i, colorf);