]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/VolumeRenderer/volumerenderermanagerdata.cxx
addition of image stencil to surface rendering
[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 #include <vtkVolumeRayCastMapper.h>
13
14 #if VTK_MAJOR_VERSION >= 5
15     #if VTK_MINOR_VERSION >= 6
16         #include "vtkGPUVolumeRayCastMapper.h"
17     #endif
18 #endif
19
20 using namespace std;
21
22 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, bool usegpu){
23     Initialize(vol, "", usegpu);
24 }
25
26 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, std::string dataname){
27
28     Initialize(vol, dataname);
29
30
31 }
32
33 VolumeRendererManagerData::VolumeRendererManagerData(vtkImageData* vol, vtkRenderer* render, std::string dataname){
34
35     Initialize(vol, dataname);
36     render->AddActor(this->getProp3D());
37
38 }
39
40 void VolumeRendererManagerData::Initialize(vtkImageData* vol, std::string dataname, bool usegpu){
41     _id = 0;
42     _vol = vol;
43     _dataname = dataname;
44
45
46     _volumePlanes  = vtkPlanes::New();
47
48     _volumeProperty = vtkVolumeProperty::New();
49     _volumeProperty->SetInterpolationTypeToLinear();
50     _volumeProperty->ShadeOn();
51     _volumeProperty->SetAmbient(3);
52     _volumeProperty->SetDiffuse(0.2);
53     _volumeProperty->SetSpecular(0.5);
54     //_volumeProperty->DisableGradientOpacityOn();
55     _tfun = vtkPiecewiseFunction::New();    
56     _volumeProperty->SetScalarOpacity(_tfun );
57     _ctfun = vtkColorTransferFunction::New();
58
59     if(vol->GetNumberOfScalarComponents() == 1 ){
60
61         _ctfun->AddRGBPoint(vol->GetScalarRange()[0], 0, 0 , 0);
62         _ctfun->AddRGBPoint(vol->GetScalarRange()[1], 1, 1 , 1);
63         _ctfun->Build();
64         _volumeProperty->SetColor(_ctfun);
65     }
66
67     _newvol = vtkVolume::New();
68     _newvol->SetProperty(_volumeProperty );    
69
70
71     _compositeFunction = 0;
72     _MIPFunction = 0;
73
74     BoxWidget = 0;
75
76 /* EED9Juin2011
77     if(usegpu && _vol->GetDataDimension() > 2){
78         _volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
79         _volumeMappergpu->SetClippingPlanes( _volumePlanes );
80         _volumeMappergpu->AutoAdjustSampleDistancesOn();
81
82         _newvol->SetMapper(_volumeMappergpu );
83
84         _volumeMappergpu->SetInput( _vol );
85         _volumeMappergpu->Update();
86     }else{
87         _compositeFunction = vtkVolumeRayCastCompositeFunction::New();
88         _MIPFunction = vtkVolumeRayCastMIPFunction::New();
89
90         _volumeMapper = vtkVolumeRayCastMapper::New();
91         _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
92         _volumeMapper->SetClippingPlanes( _volumePlanes );
93         _volumeMapper->AutoAdjustSampleDistancesOn();
94
95         _newvol->SetMapper(_volumeMapper );
96
97         _volumeMapper->SetInput( _vol );
98         _volumeMapper->Update();
99     }
100 */
101
102        VolumeMapper = 0;
103 #if VTK_MAJOR_VERSION >= 5
104     #if VTK_MINOR_VERSION >= 6
105        cout<<"using volume render GPU"<<endl;
106        vtkGPUVolumeRayCastMapper * volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
107        volumeMappergpu->AutoAdjustSampleDistancesOn();       
108        VolumeMapper = volumeMappergpu;
109     #endif
110 #else
111
112
113        _compositeFunction                                                               = vtkVolumeRayCastCompositeFunction::New();
114        _MIPFunction                                                                     = vtkVolumeRayCastMIPFunction::New();
115        vtkVolumeRayCastMapper* volumeMapper                                                                     = vtkVolumeRayCastMapper::New();
116        volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
117                volumeMapper->AutoAdjustSampleDistancesOn();
118                VolumeMapper                                                                     = volumeMapper;
119 #endif
120
121
122         VolumeMapper->SetClippingPlanes( _volumePlanes );
123         _newvol->SetMapper(VolumeMapper );
124         VolumeMapper->SetInput( _vol );
125         VolumeMapper->Update();
126     _newvol->Update();
127 }
128
129
130 VolumeRendererManagerData::~VolumeRendererManagerData()
131 {
132
133         _tfun->Delete();
134         _ctfun->Delete();
135         _volumePlanes->Delete();
136         _volumeProperty->Delete();
137         _newvol->Delete();
138
139         if(_compositeFunction)
140             _compositeFunction->Delete();
141         if(_MIPFunction)
142             _MIPFunction->Delete();
143         if(VolumeMapper)
144             VolumeMapper->Delete();
145
146         if(BoxWidget){
147             DisableBoundingBox();
148         }
149
150 }
151 void VolumeRendererManagerData::SetIndependentComponents(bool independent){
152
153     if(independent){
154         _volumeProperty->IndependentComponentsOn();
155     }else{
156         _volumeProperty->IndependentComponentsOff();
157     }
158 }
159
160 void VolumeRendererManagerData::EnableBoundingBox(vtkRenderWindowInteractor* interactor)
161 {
162 //EED9Juin2011    if(_volumeMappergpu){
163
164     if(!BoxWidget){
165         BoxWidget = vtkBoxWidget::New();
166         BoxWidget->SetInteractor( interactor );
167         BoxWidget->SetPlaceFactor(1.01);
168
169         BoxWidget->SetInput( _vol );
170         BoxWidget->InsideOutOn();
171         BoxWidget->PlaceWidget();
172
173
174         vtkBoxWidgetCallback *callback = vtkBoxWidgetCallback::New();
175
176
177         callback->SetMapper(VolumeMapper);
178
179
180         BoxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
181         callback->Delete();
182
183         BoxWidget->EnabledOn();
184         BoxWidget->GetSelectedFaceProperty()->SetOpacity(0.0);
185
186         cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox::CREATE"<<endl;
187
188     }else{
189
190         BoxWidget->EnabledOn();
191
192         cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox"<<endl;
193
194     }
195 //EED9Juin2011  }
196 }
197
198 void VolumeRendererManagerData::DisableBoundingBox(){
199
200     if(BoxWidget){
201         BoxWidget->EnabledOff();
202         //BoxWidget->Delete();
203         //BoxWidget = 0;
204
205         cout<<"JPRG::VolumeRendererManagerData::DisableBoundingBox"<<endl;
206
207
208     }
209 }
210
211 /**
212 **      Set Volume Color
213 **/
214 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
215                                                                 std::vector<double>& red,
216                                                                 std::vector<double>& green,
217                                                                 std::vector<double>& blue)
218 {
219
220         _ctfun->RemoveAllPoints();
221         for(unsigned int i = 0; i < greylevelcolors.size();i++){
222                 _ctfun->AddRGBPoint(greylevelcolors[i], red[i],green[i], blue[i]);
223                                 //std::cout<<"VolumeRendererManagerData::setVolumeColor "<<greylevelcolors[i]<<" "<<red[i]<<" "<<green[i]<<" "<<blue[i]<<std::endl;
224         }
225         _newvol->Update();
226 }
227
228 /**
229 **      Volume Opacity
230 **/
231 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value){
232
233         _tfun->RemoveAllPoints();
234         for(unsigned int i = 0; i < greylevel.size();i++){
235                 _tfun->AddPoint(greylevel[i], value[i]);
236                 //std::cout<<"VolumeRendererManagerData::setVolumeOpacity "<<greylevel[i]<<" "<< value[i]<<std::endl;
237         }
238     _newvol->Update();
239 }
240
241 /**
242 **      Volume Opacity
243 **/
244 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,double value){
245     std::vector<double> valuevector;
246     for(unsigned i = 0; i < greylevel.size(); i++){
247         valuevector.push_back(value);
248     }
249     setVolumeOpacity(greylevel, valuevector);
250 }
251
252 /**
253 **      Check if the variables are setted correctly
254 **/
255 void VolumeRendererManagerData::checkInvariant()throw (char *){
256         if(!_compositeFunction){
257                 throw "No composite function initialized";
258         }
259         if(!_MIPFunction){
260                 throw "No MIP function initialized";
261         }
262         if(!VolumeMapper){
263                 throw "No volume mapper initialized";
264         }
265 }
266
267 /**
268 **      Check if the variables are setted correctly
269 **/
270 void VolumeRendererManagerData::Update(){
271          _newvol->Update();
272 }
273 /**
274 **      get the prop3D
275 **/
276 vtkProp3D* VolumeRendererManagerData::getProp3D(){
277         return this->_newvol;
278 }
279 /**
280 **      return the id from the daat
281 **/
282 int VolumeRendererManagerData::getId(){
283         return _id;
284 }
285 /**
286 **      set data id
287 **/
288 void VolumeRendererManagerData::setId(int propid){
289         _id = propid;
290 }
291
292 /**
293 **      Get the filanme
294 **/
295 std::string VolumeRendererManagerData::getDataname(){
296         return _dataname;
297 }
298 /**
299 ** Set the filanme
300 **/
301 void VolumeRendererManagerData::setDataname(std::string dataname){
302         _dataname = dataname;
303 }
304
305 void VolumeRendererManagerData::changeCompositeMIPFunction(int function){
306         //checkInvariant();
307         if(VolumeMapper){
308 #if VTK_MAJOR_VERSION >= 5
309     #if VTK_MINOR_VERSION >= 6
310             if(dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper)){
311                 vtkVolumeRayCastMapper* volumemapper = dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper);
312                 if(function == 0){
313                         volumemapper->SetVolumeRayCastFunction(_compositeFunction);
314                 }else{
315                         volumemapper->SetVolumeRayCastFunction(_MIPFunction);
316                 }
317             }else if(dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper)){
318                 vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
319                 if(function == 0){
320                     volumemapper->SetBlendModeToComposite();
321                 }else if(function == 1){
322                     volumemapper->SetBlendModeToMaximumIntensity();
323                 }else if(function == 2){
324                     volumemapper->SetBlendModeToMinimumIntensity();
325                 }
326             }
327     #endif
328 #else
329             vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
330             if(function == 0){
331                 volumemapper->SetBlendModeToComposite();
332             }else if(function == 1){
333                 volumemapper->SetBlendModeToMaximumIntensity();
334             }else if(function == 2){
335                 volumemapper->SetBlendModeToMinimumIntensity();
336             }
337 #endif
338         }
339
340 }
341
342 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable){
343     if(lookuptable){
344          _ctfun->RemoveAllPoints();
345          vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
346          for(int i = 0; i < colort->GetSize(); i++){
347              double val[6];
348              colort->GetNodeValue(i, val);
349              cout<< "JPRG::VolumeRendererManagerData::SetLookupTable::"<<val[0]<<" "<< val[1]<<" "<<val[2]<<" "<< val[3]<<endl;
350              _ctfun->AddRGBPoint(val[0], val[1], val[2], val[3]);
351          }
352          _newvol->Update();
353     }
354
355 }
356
357 void VolumeRendererManagerData::changeInterpolationType(int type){
358     //checkInvariant();
359     if(type == 0){
360         _volumeProperty->SetInterpolationTypeToLinear();
361     }else if(type == 1){
362         _volumeProperty->SetInterpolationTypeToNearest();
363     }
364 }
365
366 void VolumeRendererManagerData::SetColorTransferFunction(int i, vtkColorTransferFunction* colorf){
367
368     _volumeProperty->SetColor(i, colorf);
369
370 }