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