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 #if VTK_MAJOR_VERSION >= 5
39 #if VTK_MINOR_VERSION >= 6
40 #include "vtkGPUVolumeRayCastMapper.h"
46 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, bool usegpu){
47 Initialize(vol, "", usegpu);
50 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, std::string dataname){
52 Initialize(vol, dataname);
57 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, vtkRenderer* render, std::string dataname){
59 Initialize(vol, dataname);
60 render->AddActor(this->getProp3D());
64 void VolumeRendererManagerData::Initialize(vtkImageData* vol, std::string dataname, bool usegpu){
70 _volumePlanes = vtkPlanes::New();
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();
83 if(vol->GetNumberOfScalarComponents() == 1 ){
85 _ctfun->AddRGBPoint(vol->GetScalarRange()[0], 0, 0 , 0);
86 _ctfun->AddRGBPoint(vol->GetScalarRange()[1], 1, 1 , 1);
88 _volumeProperty->SetColor(_ctfun);
91 _newvol = vtkVolume::New();
92 _newvol->SetProperty(_volumeProperty );
95 _compositeFunction = 0;
101 if(usegpu && _vol->GetDataDimension() > 2){
102 _volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
103 _volumeMappergpu->SetClippingPlanes( _volumePlanes );
104 _volumeMappergpu->AutoAdjustSampleDistancesOn();
106 _newvol->SetMapper(_volumeMappergpu );
108 _volumeMappergpu->SetInput( _vol );
109 _volumeMappergpu->Update();
111 _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
112 _MIPFunction = vtkVolumeRayCastMIPFunction::New();
114 _volumeMapper = vtkVolumeRayCastMapper::New();
115 _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
116 _volumeMapper->SetClippingPlanes( _volumePlanes );
117 _volumeMapper->AutoAdjustSampleDistancesOn();
119 _newvol->SetMapper(_volumeMapper );
121 _volumeMapper->SetInput( _vol );
122 _volumeMapper->Update();
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;
137 _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
138 _MIPFunction = vtkVolumeRayCastMIPFunction::New();
139 vtkVolumeRayCastMapper* volumeMapper = vtkVolumeRayCastMapper::New();
140 volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
141 volumeMapper->AutoAdjustSampleDistancesOn();
142 VolumeMapper = volumeMapper;
146 VolumeMapper->SetClippingPlanes( _volumePlanes );
147 _newvol->SetMapper(VolumeMapper );
148 VolumeMapper->SetInput( _vol );
149 VolumeMapper->Update();
154 VolumeRendererManagerData::~VolumeRendererManagerData()
159 _volumePlanes->Delete();
160 _volumeProperty->Delete();
163 if(_compositeFunction)
164 _compositeFunction->Delete();
166 _MIPFunction->Delete();
168 VolumeMapper->Delete();
171 DisableBoundingBox();
175 void VolumeRendererManagerData::SetIndependentComponents(bool independent){
178 _volumeProperty->IndependentComponentsOn();
180 _volumeProperty->IndependentComponentsOff();
184 void VolumeRendererManagerData::EnableBoundingBox(vtkRenderWindowInteractor* interactor)
186 //EED9Juin2011 if(_volumeMappergpu){
189 BoxWidget = vtkBoxWidget::New();
190 BoxWidget->SetInteractor( interactor );
191 BoxWidget->SetPlaceFactor(1.01);
193 BoxWidget->SetInput( _vol );
194 BoxWidget->InsideOutOn();
195 BoxWidget->PlaceWidget();
198 vtkBoxWidgetCallback *callback = vtkBoxWidgetCallback::New();
201 callback->SetMapper(VolumeMapper);
204 BoxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
207 BoxWidget->EnabledOn();
208 BoxWidget->GetSelectedFaceProperty()->SetOpacity(0.0);
210 cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox::CREATE"<<endl;
214 BoxWidget->EnabledOn();
216 cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox"<<endl;
222 void VolumeRendererManagerData::DisableBoundingBox(){
225 BoxWidget->EnabledOff();
226 //BoxWidget->Delete();
229 cout<<"JPRG::VolumeRendererManagerData::DisableBoundingBox"<<endl;
238 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
239 std::vector<double>& red,
240 std::vector<double>& green,
241 std::vector<double>& blue)
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;
255 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value){
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;
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);
273 setVolumeOpacity(greylevel, valuevector);
277 ** Check if the variables are setted correctly
279 void VolumeRendererManagerData::checkInvariant()throw (char *){
280 if(!_compositeFunction){
281 throw "No composite function initialized";
284 throw "No MIP function initialized";
287 throw "No volume mapper initialized";
292 ** Check if the variables are setted correctly
294 void VolumeRendererManagerData::Update(){
300 vtkProp3D* VolumeRendererManagerData::getProp3D(){
301 return this->_newvol;
304 ** return the id from the daat
306 int VolumeRendererManagerData::getId(){
312 void VolumeRendererManagerData::setId(int propid){
319 std::string VolumeRendererManagerData::getDataname(){
325 void VolumeRendererManagerData::setDataname(std::string dataname){
326 _dataname = dataname;
329 void VolumeRendererManagerData::changeCompositeMIPFunction(int function){
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);
337 volumemapper->SetVolumeRayCastFunction(_compositeFunction);
339 volumemapper->SetVolumeRayCastFunction(_MIPFunction);
341 }else if(dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper)){
342 vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
344 volumemapper->SetBlendModeToComposite();
345 }else if(function == 1){
346 volumemapper->SetBlendModeToMaximumIntensity();
347 }else if(function == 2){
348 volumemapper->SetBlendModeToMinimumIntensity();
353 vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
355 volumemapper->SetBlendModeToComposite();
356 }else if(function == 1){
357 volumemapper->SetBlendModeToMaximumIntensity();
358 }else if(function == 2){
359 volumemapper->SetBlendModeToMinimumIntensity();
366 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable){
368 _ctfun->RemoveAllPoints();
369 vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
370 for(int i = 0; i < colort->GetSize(); i++){
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]);
381 void VolumeRendererManagerData::changeInterpolationType(int type){
384 _volumeProperty->SetInterpolationTypeToLinear();
386 _volumeProperty->SetInterpolationTypeToNearest();
390 void VolumeRendererManagerData::SetColorTransferFunction(int i, vtkColorTransferFunction* colorf){
392 _volumeProperty->SetColor(i, colorf);