]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/VolumeRenderer/volumerenderermanagerdata.cxx
3e6ad74dda2afae136efb66e602c061bfdbf8297
[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->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 #if VTK_MAJOR_VERSION >= 5
93     #if VTK_MINOR_VERSION >= 6
94        vtkGPUVolumeRayCastMapper * volumeMappergpu = vtkGPUVolumeRayCastMapper::New();
95        volumeMappergpu->AutoAdjustSampleDistancesOn();
96        VolumeMapper = volumeMappergpu;
97     #endif
98 #else
99
100        _volumeProperty->SetColor(_ctfun);
101
102
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
195         cout<<"JPRG::VolumeRendererManagerData::DisableBoundingBox"<<endl;
196
197
198     }
199 }
200
201 /**
202 **      Set Volume Color
203 **/
204 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
205                                                                 std::vector<double>& red,
206                                                                 std::vector<double>& green,
207                                                                 std::vector<double>& blue)
208 {
209
210         _ctfun->RemoveAllPoints();
211         for(unsigned int i = 0; i < greylevelcolors.size();i++){
212                 _ctfun->AddRGBPoint(greylevelcolors[i], red[i],green[i], blue[i]);
213                                 //std::cout<<"VolumeRendererManagerData::setVolumeColor "<<greylevelcolors[i]<<" "<<red[i]<<" "<<green[i]<<" "<<blue[i]<<std::endl;
214         }
215         _newvol->Update();
216 }
217
218 /**
219 **      Volume Opacity
220 **/
221 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value){
222
223         _tfun->RemoveAllPoints();
224         for(unsigned int i = 0; i < greylevel.size();i++){
225                 _tfun->AddPoint(greylevel[i], value[i]);
226                 //std::cout<<"VolumeRendererManagerData::setVolumeOpacity "<<greylevel[i]<<" "<< value[i]<<std::endl;
227         }
228     _newvol->Update();
229 }
230
231 /**
232 **      Volume Opacity
233 **/
234 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,double value){
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          _newvol->Update();
262 }
263 /**
264 **      get the prop3D
265 **/
266 vtkProp3D* VolumeRendererManagerData::getProp3D(){
267         return this->_newvol;
268 }
269 /**
270 **      return the id from the daat
271 **/
272 int VolumeRendererManagerData::getId(){
273         return _id;
274 }
275 /**
276 **      set data id
277 **/
278 void VolumeRendererManagerData::setId(int propid){
279         _id = propid;
280 }
281
282 /**
283 **      Get the filanme
284 **/
285 std::string VolumeRendererManagerData::getDataname(){
286         return _dataname;
287 }
288 /**
289 ** Set the filanme
290 **/
291 void VolumeRendererManagerData::setDataname(std::string dataname){
292         _dataname = dataname;
293 }
294
295 void VolumeRendererManagerData::changeCompositeMIPFunction(int function){
296         //checkInvariant();
297         if(VolumeMapper){
298 #if VTK_MAJOR_VERSION >= 5
299     #if VTK_MINOR_VERSION >= 6
300             if(dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper)){
301                 vtkVolumeRayCastMapper* volumemapper = dynamic_cast<vtkVolumeRayCastMapper*>(VolumeMapper);
302                 if(function == 0){
303                         volumemapper->SetVolumeRayCastFunction(_compositeFunction);
304                 }else{
305                         volumemapper->SetVolumeRayCastFunction(_MIPFunction);
306                 }
307             }else if(dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper)){
308                 vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
309                 if(function == 0){
310                     volumemapper->SetBlendModeToComposite();
311                 }else if(function == 1){
312                     volumemapper->SetBlendModeToMaximumIntensity();
313                 }else if(function == 2){
314                     volumemapper->SetBlendModeToMinimumIntensity();
315                 }
316             }
317     #endif
318 #else
319             vtkGPUVolumeRayCastMapper* volumemapper = dynamic_cast<vtkGPUVolumeRayCastMapper*>(VolumeMapper);
320             if(function == 0){
321                 volumemapper->SetBlendModeToComposite();
322             }else if(function == 1){
323                 volumemapper->SetBlendModeToMaximumIntensity();
324             }else if(function == 2){
325                 volumemapper->SetBlendModeToMinimumIntensity();
326             }
327 #endif
328         }
329
330 }
331
332 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable){
333     if(lookuptable){
334          _ctfun->RemoveAllPoints();
335          vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
336          for(int i = 0; i < colort->GetSize(); i++){
337              double val[6];
338              colort->GetNodeValue(i, val);
339              cout<< "JPRG::VolumeRendererManagerData::SetLookupTable::"<<val[0]<<" "<< val[1]<<" "<<val[2]<<" "<< val[3]<<endl;
340              _ctfun->AddRGBPoint(val[0], val[1], val[2], val[3]);
341          }
342          _newvol->Update();
343     }
344
345 }
346
347 void VolumeRendererManagerData::changeInterpolationType(int type){
348     //checkInvariant();
349     if(type == 0){
350         _volumeProperty->SetInterpolationTypeToLinear();
351     }else if(type == 1){
352         _volumeProperty->SetInterpolationTypeToNearest();
353     }
354 }
355
356 void VolumeRendererManagerData::SetColorTransferFunction(int i, vtkColorTransferFunction* colorf){
357
358     _volumeProperty->SetColor(i, colorf);
359
360 }