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