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