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