]> Creatis software - creaMaracasVisu.git/blob - lib/Kernel/VTKObjects/VolumeRenderer/volumerenderermanagerdata.cxx
*** empty log 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
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==NULL){
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         cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox::CREATE"<<endl;
166     }else{
167         cout<<"JPRG::VolumeRendererManagerData::EnableBoundingBox"<<endl;
168         _boxWidgetS1->EnabledOn();
169     }
170 //EED9Juin2011  }
171 }
172
173 void VolumeRendererManagerData::DisableBoundingBox(){
174     if(_boxWidgetS1){
175         cout<<"JPRG::VolumeRendererManagerData::DisableBoundingBox"<<endl;
176         _boxWidgetS1->EnabledOff();
177         //_boxWidgetS1->Delete();
178         //_boxWidgetS1 = 0;
179     }
180 }
181
182 /**
183 **      Set Volume Color
184 **/
185 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
186                                                                 std::vector<double>& red,
187                                                                 std::vector<double>& green,
188                                                                 std::vector<double>& blue)
189 {
190
191         _ctfun->RemoveAllPoints();
192         for(unsigned int i = 0; i < greylevelcolors.size();i++){
193                 _ctfun->AddRGBPoint(greylevelcolors[i], red[i],green[i], blue[i]);
194                                 //std::cout<<"VolumeRendererManagerData::setVolumeColor "<<greylevelcolors[i]<<" "<<red[i]<<" "<<green[i]<<" "<<blue[i]<<std::endl;
195         }
196         _newvol->Update();
197 }
198
199 /**
200 **      Volume Opacity
201 **/
202 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value){
203
204         _tfun->RemoveAllPoints();
205         for(unsigned int i = 0; i < greylevel.size();i++){
206                 _tfun->AddPoint(greylevel[i], value[i]);
207                 //std::cout<<"VolumeRendererManagerData::setVolumeOpacity "<<greylevel[i]<<" "<< value[i]<<std::endl;
208         }
209     _newvol->Update();
210 }
211
212 /**
213 **      Volume Opacity
214 **/
215 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,double value){
216     std::vector<double> valuevector;
217     for(unsigned i = 0; i < greylevel.size(); i++){
218         valuevector.push_back(value);
219     }
220     setVolumeOpacity(greylevel, valuevector);
221 }
222
223 /**
224 **      Check if the variables are setted correctly
225 **/
226 void VolumeRendererManagerData::checkInvariant()throw (char *){
227         if(!_compositeFunction){
228                 throw "No composite function initialized";
229         }
230         if(!_MIPFunction){
231                 throw "No MIP function initialized";
232         }
233         if(!_volumeMapper && !_volumeMappergpu){
234                 throw "No volume mapper initialized";
235         }
236 }
237
238 /**
239 **      Check if the variables are setted correctly
240 **/
241 void VolumeRendererManagerData::Update(){
242          _newvol->Update();
243 }
244 /**
245 **      get the prop3D
246 **/
247 vtkProp3D* VolumeRendererManagerData::getProp3D(){
248         return this->_newvol;
249 }
250 /**
251 **      return the id from the daat
252 **/
253 int VolumeRendererManagerData::getId(){
254         return _id;
255 }
256 /**
257 **      set data id
258 **/
259 void VolumeRendererManagerData::setId(int propid){
260         _id = propid;
261 }
262
263 /**
264 **      Get the filanme
265 **/
266 std::string VolumeRendererManagerData::getDataname(){
267         return _dataname;
268 }
269 /**
270 ** Set the filanme
271 **/
272 void VolumeRendererManagerData::setDataname(std::string dataname){
273         _dataname = dataname;
274 }
275
276 void VolumeRendererManagerData::changeCompositeMIPFunction(int function){
277         checkInvariant();
278         if(_volumeMapper){
279             if(function == 0){
280                     _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
281             }else{
282                     _volumeMapper->SetVolumeRayCastFunction(_MIPFunction);
283             }
284         }
285
286 }
287
288 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable){
289      _ctfun->RemoveAllPoints();
290      vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
291      for(int i = 0; i < colort->GetSize(); i++){
292          double val[6];
293          colort->GetNodeValue(i, val);
294          cout<< "JPRG::VolumeRendererManagerData::SetLookupTable::"<<val[0]<<" "<< val[1]<<" "<<val[2]<<" "<< val[3]<<endl;
295          _ctfun->AddRGBPoint(val[0], val[1], val[2], val[3]);
296      }
297      _newvol->Update();
298
299 }