]> 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
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>=6 && 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         _boxWidgetS1 = vtkBoxWidget::New();
145         _boxWidgetS1->SetInteractor( interactor );
146         _boxWidgetS1->SetPlaceFactor(1.01);
147
148         _boxWidgetS1->SetInput( _vol );
149         _boxWidgetS1->InsideOutOn();
150         _boxWidgetS1->PlaceWidget();
151
152         vtkBoxWidgetCallback *callback = vtkBoxWidgetCallback::New();
153         
154                 if (_volumeMapper!=0){
155                         callback->SetMapper(_volumeMapper);
156                 } else {
157                         callback->SetMapper(_volumeMappergpu);
158                 } 
159         
160         _boxWidgetS1->AddObserver(vtkCommand::InteractionEvent, callback);
161         callback->Delete();
162         _boxWidgetS1->EnabledOn();
163         _boxWidgetS1->GetSelectedFaceProperty()->SetOpacity(0.0);
164 //EED9Juin2011  }
165 }
166
167 void VolumeRendererManagerData::DisableBoundingBox(){
168     if(_boxWidgetS1){
169         _boxWidgetS1->EnabledOff();
170         _boxWidgetS1->Delete();
171         _boxWidgetS1 = 0;
172     }
173 }
174
175 /**
176 **      Set Volume Color
177 **/
178 void VolumeRendererManagerData::setVolumeColor(std::vector<double>& greylevelcolors,
179                                                                 std::vector<double>& red,
180                                                                 std::vector<double>& green,
181                                                                 std::vector<double>& blue)
182 {
183
184         _ctfun->RemoveAllPoints();
185         for(unsigned int i = 0; i < greylevelcolors.size();i++){
186                 _ctfun->AddRGBPoint(greylevelcolors[i], red[i],green[i], blue[i]);
187                                 //std::cout<<"VolumeRendererManagerData::setVolumeColor "<<greylevelcolors[i]<<" "<<red[i]<<" "<<green[i]<<" "<<blue[i]<<std::endl;
188         }
189         _newvol->Update();
190 }
191
192 /**
193 **      Volume Opacity
194 **/
195 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,std::vector<double> value){
196
197         _tfun->RemoveAllPoints();
198         for(unsigned int i = 0; i < greylevel.size();i++){
199                 _tfun->AddPoint(greylevel[i], value[i]);
200                 //std::cout<<"VolumeRendererManagerData::setVolumeOpacity "<<greylevel[i]<<" "<< value[i]<<std::endl;
201         }
202     _newvol->Update();
203 }
204
205 /**
206 **      Volume Opacity
207 **/
208 void VolumeRendererManagerData::setVolumeOpacity(std::vector<double> greylevel,double value){
209     std::vector<double> valuevector;
210     for(unsigned i = 0; i < greylevel.size(); i++){
211         valuevector.push_back(value);
212     }
213     setVolumeOpacity(greylevel, valuevector);
214 }
215
216 /**
217 **      Check if the variables are setted correctly
218 **/
219 void VolumeRendererManagerData::checkInvariant()throw (char *){
220         if(!_compositeFunction){
221                 throw "No composite function initialized";
222         }
223         if(!_MIPFunction){
224                 throw "No MIP function initialized";
225         }
226         if(!_volumeMapper && !_volumeMappergpu){
227                 throw "No volume mapper initialized";
228         }
229 }
230
231 /**
232 **      Check if the variables are setted correctly
233 **/
234 void VolumeRendererManagerData::Update(){
235          _newvol->Update();
236 }
237 /**
238 **      get the prop3D
239 **/
240 vtkProp3D* VolumeRendererManagerData::getProp3D(){
241         return this->_newvol;
242 }
243 /**
244 **      return the id from the daat
245 **/
246 int VolumeRendererManagerData::getId(){
247         return _id;
248 }
249 /**
250 **      set data id
251 **/
252 void VolumeRendererManagerData::setId(int propid){
253         _id = propid;
254 }
255
256 /**
257 **      Get the filanme
258 **/
259 std::string VolumeRendererManagerData::getDataname(){
260         return _dataname;
261 }
262 /**
263 ** Set the filanme
264 **/
265 void VolumeRendererManagerData::setDataname(std::string dataname){
266         _dataname = dataname;
267 }
268
269 void VolumeRendererManagerData::changeCompositeMIPFunction(int function) throw (char *){
270         checkInvariant();
271         if(_volumeMapper){
272             if(function == 0){
273                     _volumeMapper->SetVolumeRayCastFunction(_compositeFunction);
274             }else{
275                     _volumeMapper->SetVolumeRayCastFunction(_MIPFunction);
276             }
277         }
278
279 }
280
281 void VolumeRendererManagerData::SetLookupTable(vtkLookupTable* lookuptable){
282      _ctfun->RemoveAllPoints();
283      vtkColorTransferFunction* colort = (vtkColorTransferFunction*)lookuptable;
284      for(int i = 0; i < colort->GetSize(); i++){
285          double val[6];
286          colort->GetNodeValue(i, val);
287          //cout<< val[0]<<" "<< val[1]<<" "<<val[2]<<" "<< val[3]<<endl;
288          _ctfun->AddRGBPoint(val[0], val[1], val[2], val[3]);
289      }
290      _newvol->Update();
291 }