]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/VolumeRenderer/volumerenderermanagerdata.cxx
changes to volume rendering base
[creaMaracasVisu.git] / lib / Kernel / VTKObjects / VolumeRenderer / volumerenderermanagerdata.cxx
1
2 #include "volumerenderermanagerdata.h"
3
4 /********************************************************************************************
5 ** Start of data viewmanagerData
6 *********************************************************************************************/
7
8 #include "vtkStripper.h"
9 #include "boxSurfaceObserver.h"
10 #include "vtkProperty.h"
11
12 using namespace std;
13
14 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, std::string dataname){
15
16     Initialize(vol, dataname);
17
18
19 }
20
21 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, vtkRenderer* render, std::string dataname){
22
23     Initialize(vol, dataname);
24     render->AddActor(this->getProp3D());
25
26 }
27
28 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, bool usegpu, std::string dataname){
29     Initialize(vol, dataname, usegpu);    
30 }
31
32 void VolumeRendererManagerData::Initialize(vtkImageData* vol, std::string dataname, bool usegpu){
33     _vol = vol;
34     _dataname = dataname;    
35
36     _tfun = vtkPiecewiseFunction::New();
37     _ctfun = vtkColorTransferFunction::New();
38
39     _volumePlanes  = vtkPlanes::New();
40
41     _volumeProperty = vtkVolumeProperty::New();
42     _volumeProperty->SetInterpolationTypeToLinear();
43     //_volumeProperty->ShadeOn();
44     _volumeProperty->DisableGradientOpacityOn();
45     _volumeProperty->SetColor(_ctfun);
46     _volumeProperty->SetScalarOpacity(_tfun );
47
48
49     _newvol = vtkVolume::New();
50     _newvol->SetProperty(_volumeProperty );
51
52     _volumeMapper = 0;
53     _volumeMappergpu = 0;
54     _compositeFunction = 0;
55     _MIPFunction = 0;
56     _boxWidgetS1 = 0;
57
58     if(usegpu && _vol->GetDataDimension() > 2){
59         _volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
60         _volumeMappergpu->SetClippingPlanes( _volumePlanes );
61         _volumeMappergpu->AutoAdjustSampleDistancesOn();
62
63         _newvol->SetMapper(_volumeMappergpu );
64
65         _volumeMappergpu->SetInput( _vol );
66         _volumeMappergpu->Update();
67     }else{
68         _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
69         _MIPFunction = vtkVolumeRayCastMIPFunction::New();
70
71         _volumeMapper = vtkVolumeRayCastMapper::New();
72         _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
73         _volumeMapper->SetClippingPlanes( _volumePlanes );
74         _volumeMapper->AutoAdjustSampleDistancesOn();
75
76         _newvol->SetMapper(_volumeMapper );
77
78         _volumeMapper->SetInput( _vol );
79         _volumeMapper->Update();
80     }
81
82     _newvol->Update();
83 }
84
85
86 VolumeRendererManagerData::~VolumeRendererManagerData(){
87
88         _tfun->Delete();
89         _ctfun->Delete();
90         _volumePlanes->Delete();
91         _volumeProperty->Delete();
92         _newvol->Delete();
93
94         if(_compositeFunction)
95             _compositeFunction->Delete();
96         if(_MIPFunction)
97             _MIPFunction->Delete();
98         if(_volumeMapper)
99             _volumeMapper->Delete();
100         if(_volumeMappergpu)
101             _volumeMappergpu->Delete();
102         if(_boxWidgetS1){
103             DisableBoundingBox();
104         }
105
106 }
107
108 void VolumeRendererManagerData::EnableBoundingBox(vtkRenderWindowInteractor* interactor){
109     if(_volumeMappergpu){
110         _boxWidgetS1 = vtkBoxWidget::New();
111         _boxWidgetS1->SetInteractor( interactor );
112         _boxWidgetS1->SetPlaceFactor(1.01);
113
114         _boxWidgetS1->SetInput( _vol );
115         _boxWidgetS1->InsideOutOn();
116         _boxWidgetS1->PlaceWidget();
117
118         vtkBoxWidgetCallback *callback = vtkBoxWidgetCallback::New();
119         callback->SetMapper(_volumeMappergpu);
120         _boxWidgetS1->AddObserver(vtkCommand::InteractionEvent, callback);
121         callback->Delete();
122         _boxWidgetS1->EnabledOn();
123         _boxWidgetS1->GetSelectedFaceProperty()->SetOpacity(0.0);
124     }
125 }
126
127 void VolumeRendererManagerData::DisableBoundingBox(){
128     if(_boxWidgetS1){
129         _boxWidgetS1->EnabledOff();
130         _boxWidgetS1->Delete();
131         _boxWidgetS1 = 0;
132     }
133 }
134
135 /**
136 **      Set Volume Color
137 **/
138 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
139                                                                 std::vector<double>& red,
140                                                                 std::vector<double>& green,
141                                                                 std::vector<double>& blue)
142 {
143
144         _ctfun->RemoveAllPoints();
145         for(unsigned int i = 0; i < greylevelcolors.size();i++){
146                 _ctfun->AddRGBPoint(greylevelcolors[i], red[i],green[i], blue[i]);
147                                 //std::cout<<"VolumeRendererManagerData::setVolumeColor "<<greylevelcolors[i]<<" "<<red[i]<<" "<<green[i]<<" "<<blue[i]<<std::endl;
148         }
149         _newvol->Update();
150 }
151
152 /**
153 **      Volume Opacity
154 **/
155 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value){
156
157         _tfun->RemoveAllPoints();
158         for(unsigned int i = 0; i < greylevel.size();i++){
159                 _tfun->AddPoint(greylevel[i], value[i]);
160                 //std::cout<<"VolumeRendererManagerData::setVolumeOpacity "<<greylevel[i]<<" "<< value[i]<<std::endl;
161         }
162     _newvol->Update();
163 }
164
165 /**
166 **      Volume Opacity
167 **/
168 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,double value){
169     std::vector<double> valuevector;
170     for(unsigned i = 0; i < greylevel.size(); i++){
171         valuevector.push_back(value);
172     }
173     setVolumeOpacity(greylevel, valuevector);
174 }
175
176 /**
177 **      Check if the variables are setted correctly
178 **/
179 void VolumeRendererManagerData::checkInvariant()throw (char *){
180         if(!_compositeFunction){
181                 throw "No composite function initialized";
182         }
183         if(!_MIPFunction){
184                 throw "No MIP function initialized";
185         }
186         if(!_volumeMapper && !_volumeMappergpu){
187                 throw "No volume mapper initialized";
188         }
189 }
190
191 /**
192 **      Check if the variables are setted correctly
193 **/
194 void VolumeRendererManagerData::Update(){
195          _newvol->Update();
196 }
197 /**
198 **      get the prop3D
199 **/
200 vtkProp3D* VolumeRendererManagerData::getProp3D(){
201         return this->_newvol;
202 }
203 /**
204 **      return the id from the daat
205 **/
206 int VolumeRendererManagerData::getId(){
207         return _id;
208 }
209 /**
210 **      set data id
211 **/
212 void VolumeRendererManagerData::setId(int propid){
213         _id = propid;
214 }
215
216 /**
217 **      Get the filanme
218 **/
219 std::string VolumeRendererManagerData::getDataname(){
220         return _dataname;
221 }
222 /**
223 ** Set the filanme
224 **/
225 void VolumeRendererManagerData::setDataname(std::string dataname){
226         _dataname = dataname;
227 }
228
229 void VolumeRendererManagerData::changeCompositeMIPFunction(int function) throw (char *){
230         checkInvariant();
231         if(_volumeMapper){
232             if(function == 0){
233                     _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
234             }else{
235                     _volumeMapper->SetVolumeRayCastFunction(_MIPFunction);
236             }
237         }
238
239 }
240
241 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable){
242      _ctfun->RemoveAllPoints();
243      vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
244      for(int i = 0; i < colort->GetSize(); i++){
245          double val[6];
246          colort->GetNodeValue(i, val);
247          //cout<< val[0]<<" "<< val[1]<<" "<<val[2]<<" "<< val[3]<<endl;
248          _ctfun->AddRGBPoint(val[0], val[1], val[2], val[3]);
249      }
250      _newvol->Update();
251 }