openscenegraph
Property
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSGVOLUME_PROPERTY
15#define OSGVOLUME_PROPERTY 1
16
17#include <osg/TransferFunction>
18#include <osg/Uniform>
19#include <osg/AlphaFunc>
20
21#include <osgGA/GUIEventHandler>
22
23#include <osgVolume/Export>
24
25namespace osgVolume {
26
27// forward decarle
28class Property;
29class CompositeProperty;
30class SwitchProperty;
31class TransferFunctionProperty;
32class ScalarProperty;
33class IsoSurfaceProperty;
34class MaximumIntensityProjectionProperty;
35class LightingProperty;
36class AlphaFuncProperty;
37class SampleRatioProperty;
38class SampleRatioWhenMovingProperty;
39class SampleDensityProperty;
40class SampleDensityWhenMovingProperty;
41class TransparencyProperty;
42class ExteriorTransparencyFactorProperty;
43class VolumeSettings;
44
45class OSGVOLUME_EXPORT PropertyVisitor
46{
47 public:
48
49 PropertyVisitor(bool traverseOnlyActiveChildren=true);
50
51 virtual ~PropertyVisitor() {}
52
53 virtual void apply(Property&);
54 virtual void apply(CompositeProperty&);
55 virtual void apply(SwitchProperty&);
56 virtual void apply(TransferFunctionProperty&);
57 virtual void apply(ScalarProperty&);
58 virtual void apply(IsoSurfaceProperty&);
59 virtual void apply(AlphaFuncProperty&);
60 virtual void apply(MaximumIntensityProjectionProperty&);
61 virtual void apply(LightingProperty&);
62 virtual void apply(SampleRatioProperty&);
63 virtual void apply(SampleRatioWhenMovingProperty&);
64 virtual void apply(SampleDensityProperty&);
65 virtual void apply(SampleDensityWhenMovingProperty&);
66 virtual void apply(TransparencyProperty&);
67 virtual void apply(ExteriorTransparencyFactorProperty&);
68 virtual void apply(VolumeSettings&);
69
70 bool _traverseOnlyActiveChildren;
71};
72
73
74class OSGVOLUME_EXPORT Property : public osg::Object
75{
76 public:
77
78 Property();
79
80 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
81 Property(const Property&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
82
83 META_Object(osgVolume, Property);
84
85 void dirty() { ++_modifiedCount; }
86
87 void setModifiedCount(unsigned int c) { _modifiedCount = c; }
88 unsigned int getModifiedCount() const { return _modifiedCount; }
89
90 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
91 virtual void traverse(PropertyVisitor& /*pv*/) {}
92
93 protected:
94
95 virtual ~Property();
96
97 unsigned int _modifiedCount;
98};
99
100class OSGVOLUME_EXPORT CompositeProperty : public Property
101{
102 public:
103
104 CompositeProperty();
105
106 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
107 CompositeProperty(const CompositeProperty& compositeProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
108
109 META_Object(osgVolume, CompositeProperty);
110
111 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
112
113 virtual void traverse(PropertyVisitor& pv)
114 {
115 for(Properties::iterator itr = _properties.begin();
116 itr != _properties.end();
117 ++itr)
118 {
119 (*itr)->accept(pv);
120 }
121 }
122
123 void clear();
124
125 typedef std::vector< osg::ref_ptr<Property> > Properties;
126
127 void setProperty(unsigned int i, Property* property) { if (i>=_properties.size()) _properties.resize(i+1); _properties[i] = property; }
128
129 template<class T> void setProperty(unsigned int i, const osg::ref_ptr<T>& p) { setProperty(i, p.get()); }
130
131 Property* getProperty(unsigned int i) { return i<_properties.size() ? _properties[i].get() : 0; }
132
133 const Property* getProperty(unsigned int i) const { return i<_properties.size() ? _properties[i].get() : 0; }
134
135 void addProperty(Property* property) { _properties.push_back(property); dirty(); }
136
137 template<class T> void addProperty(const osg::ref_ptr<T>& p) { addProperty(p.get()); }
138
139 void removeProperty(unsigned int i) { _properties.erase(_properties.begin()+i); }
140
141 unsigned int getNumProperties() const { return _properties.size(); }
142
143 protected:
144
145 virtual ~CompositeProperty() {}
146
147
148 Properties _properties;
149};
150
151
152class OSGVOLUME_EXPORT SwitchProperty : public CompositeProperty
153{
154 public:
155
156 SwitchProperty();
157
158 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
159 SwitchProperty(const SwitchProperty& switchProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
160
161 META_Object(osgVolume, SwitchProperty);
162
163 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
164
165 virtual void traverse(PropertyVisitor& pv)
166 {
167 if (pv._traverseOnlyActiveChildren)
168 {
169 if (_activeProperty>=0 && static_cast<unsigned int>(_activeProperty)<=getNumProperties())
170 {
171 _properties[_activeProperty]->accept(pv);
172 }
173 }
174 else
175 {
176 CompositeProperty::traverse(pv);
177 }
178 }
179
180
181 /** Set which child property is active.
182 * -1 disables all children.*/
183 void setActiveProperty(int i) { _activeProperty = i; dirty(); }
184
185 /** Get the active property.*/
186 int getActiveProperty() const { return _activeProperty; }
187
188 protected:
189
190 virtual ~SwitchProperty() {}
191
192 int _activeProperty;
193};
194
195class OSGVOLUME_EXPORT TransferFunctionProperty : public Property
196{
197 public:
198
199 TransferFunctionProperty(osg::TransferFunction* tf = 0);
200
201 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
202 TransferFunctionProperty(const TransferFunctionProperty& tfp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
203
204 META_Object(osgVolume, TransferFunctionProperty);
205
206 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
207
208 /** Set the transfer function.*/
209 void setTransferFunction(osg::TransferFunction* tf) { _tf = tf; }
210
211 /** Get the transfer function.*/
212 osg::TransferFunction* getTransferFunction() { return _tf.get(); }
213
214 /** Get the const transfer function.*/
215 const osg::TransferFunction* getTransferFunction() const { return _tf.get(); }
216
217 protected:
218
219 virtual ~TransferFunctionProperty() {}
220
221 osg::ref_ptr<osg::TransferFunction> _tf;
222};
223
224
225
226class OSGVOLUME_EXPORT ScalarProperty : public Property
227{
228 public:
229
230 ScalarProperty(const std::string& scaleName, float value);
231
232 ScalarProperty(const ScalarProperty& scalarProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
233
234 META_Object(osgVolume, ScalarProperty);
235
236 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
237
238 /** Set the value.*/
239 virtual void setValue(float v) { _uniform->set(v); dirty(); }
240
241 /** Get the value.*/
242 float getValue() const { float v; _uniform->get(v); return v; }
243
244 /** Get the underlying uniform.*/
245 osg::Uniform* getUniform() { return _uniform.get(); }
246
247 /** Get the underlying uniform.*/
248 const osg::Uniform* getUniform() const { return _uniform.get(); }
249
250 protected:
251
252 virtual ~ScalarProperty() {}
253
254 ScalarProperty();
255
256 osg::ref_ptr<osg::Uniform> _uniform;
257};
258
259
260class OSGVOLUME_EXPORT IsoSurfaceProperty : public ScalarProperty
261{
262 public:
263
264 IsoSurfaceProperty(float value=1.0f);
265
266 IsoSurfaceProperty(const IsoSurfaceProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
267
268 META_Object(osgVolume, IsoSurfaceProperty);
269
270 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
271
272 protected:
273
274 virtual ~IsoSurfaceProperty() {}
275};
276
277class OSGVOLUME_EXPORT AlphaFuncProperty : public ScalarProperty
278{
279 public:
280
281 AlphaFuncProperty(float value=1.0f);
282
283 AlphaFuncProperty(const AlphaFuncProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
284
285 META_Object(osgVolume, AlphaFuncProperty);
286
287 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
288
289 virtual void setValue(float v);
290
291 osg::AlphaFunc* getAlphaFunc() { return _alphaFunc.get(); }
292
293 const osg::AlphaFunc* getAlphaFunc() const { return _alphaFunc.get(); }
294
295
296 protected:
297
298 virtual ~AlphaFuncProperty() {}
299
300 osg::ref_ptr<osg::AlphaFunc> _alphaFunc;
301};
302
303class OSGVOLUME_EXPORT MaximumIntensityProjectionProperty : public Property
304{
305 public:
306
307 MaximumIntensityProjectionProperty();
308
309 MaximumIntensityProjectionProperty(const MaximumIntensityProjectionProperty& mipp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
310
311 META_Object(osgVolume, MaximumIntensityProjectionProperty);
312
313 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
314
315 protected:
316
317 virtual ~MaximumIntensityProjectionProperty() {}
318};
319
320
321class OSGVOLUME_EXPORT LightingProperty : public Property
322{
323 public:
324
325 LightingProperty();
326
327 LightingProperty(const LightingProperty& mipp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
328
329 META_Object(osgVolume, LightingProperty);
330
331 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
332
333 protected:
334
335 virtual ~LightingProperty() {}
336};
337
338
339/** Sample density to use when the volume is static relative to the eye point or when moving if no SampleDensityWhenMovingProperty is assigned.*/
340class OSGVOLUME_EXPORT SampleDensityProperty : public ScalarProperty
341{
342 public:
343
344 SampleDensityProperty(float value=1.0f);
345
346 SampleDensityProperty(const SampleDensityProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
347
348 META_Object(osgVolume, SampleDensityProperty);
349
350 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
351
352 protected:
353
354 virtual ~SampleDensityProperty() {}
355};
356
357/** Sample density to use when the volume is moving relative to the eye point.*/
358class OSGVOLUME_EXPORT SampleDensityWhenMovingProperty : public ScalarProperty
359{
360 public:
361
362 SampleDensityWhenMovingProperty(float value=1.0f);
363
364 SampleDensityWhenMovingProperty(const SampleDensityWhenMovingProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
365
366 META_Object(osgVolume, SampleDensityWhenMovingProperty);
367
368 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
369
370 protected:
371
372 virtual ~SampleDensityWhenMovingProperty() {}
373};
374
375/** Sample ratioto use when the volume is static relative to the eye point or when moving if no SampleRatioWhenMovingProperty is assigned.*/
376class OSGVOLUME_EXPORT SampleRatioProperty : public ScalarProperty
377{
378 public:
379
380 SampleRatioProperty(float value=1.0f);
381
382 SampleRatioProperty(const SampleRatioProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
383
384 META_Object(osgVolume, SampleRatioProperty);
385
386 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
387
388 protected:
389
390 virtual ~SampleRatioProperty() {}
391};
392
393/** Sample density to use when the volume is moving relative to the eye point.*/
394class OSGVOLUME_EXPORT SampleRatioWhenMovingProperty : public ScalarProperty
395{
396 public:
397
398 SampleRatioWhenMovingProperty(float value=1.0f);
399
400 SampleRatioWhenMovingProperty(const SampleRatioWhenMovingProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
401
402 META_Object(osgVolume, SampleRatioWhenMovingProperty);
403
404 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
405
406 protected:
407
408 virtual ~SampleRatioWhenMovingProperty() {}
409};
410
411
412class OSGVOLUME_EXPORT TransparencyProperty : public ScalarProperty
413{
414 public:
415
416 TransparencyProperty(float value=1.0f);
417
418 TransparencyProperty(const TransparencyProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
419
420 META_Object(osgVolume, TransparencyProperty);
421
422 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
423
424 protected:
425
426 virtual ~TransparencyProperty() {}
427};
428
429class OSGVOLUME_EXPORT ExteriorTransparencyFactorProperty : public ScalarProperty
430{
431 public:
432
433 ExteriorTransparencyFactorProperty(float value=0.0f);
434
435 ExteriorTransparencyFactorProperty(const ExteriorTransparencyFactorProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
436
437 META_Object(osgVolume, ExteriorTransparencyFactorProperty);
438
439 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
440
441 protected:
442
443 virtual ~ExteriorTransparencyFactorProperty() {}
444};
445
446
447class OSGVOLUME_EXPORT CollectPropertiesVisitor : public osgVolume::PropertyVisitor
448{
449 public:
450
451 CollectPropertiesVisitor(bool traverseOnlyActiveChildren=true);
452
453 virtual void apply(TransferFunctionProperty&);
454 virtual void apply(ScalarProperty&);
455 virtual void apply(IsoSurfaceProperty& iso);
456 virtual void apply(AlphaFuncProperty& af);
457 virtual void apply(MaximumIntensityProjectionProperty& mip);
458 virtual void apply(LightingProperty& lp);
459 virtual void apply(SampleDensityProperty& sdp);
460 virtual void apply(SampleDensityWhenMovingProperty& sdp);
461 virtual void apply(SampleRatioProperty& sdp);
462 virtual void apply(SampleRatioWhenMovingProperty& sdp);
463 virtual void apply(TransparencyProperty& tp);
464 virtual void apply(ExteriorTransparencyFactorProperty& tp);
465
466 osg::ref_ptr<TransferFunctionProperty> _tfProperty;
467 osg::ref_ptr<IsoSurfaceProperty> _isoProperty;
468 osg::ref_ptr<AlphaFuncProperty> _afProperty;
469 osg::ref_ptr<MaximumIntensityProjectionProperty> _mipProperty;
470 osg::ref_ptr<LightingProperty> _lightingProperty;
471 osg::ref_ptr<SampleDensityProperty> _sampleDensityProperty;
472 osg::ref_ptr<SampleDensityWhenMovingProperty> _sampleDensityWhenMovingProperty;
473 osg::ref_ptr<SampleRatioProperty> _sampleRatioProperty;
474 osg::ref_ptr<SampleRatioWhenMovingProperty> _sampleRatioWhenMovingProperty;
475 osg::ref_ptr<TransparencyProperty> _transparencyProperty;
476 osg::ref_ptr<ExteriorTransparencyFactorProperty> _exteriorTransparencyFactorProperty;
477
478};
479
480class OSGVOLUME_EXPORT PropertyAdjustmentCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback
481{
482 public:
483
484 PropertyAdjustmentCallback();
485
486 PropertyAdjustmentCallback(const PropertyAdjustmentCallback&,const osg::CopyOp&);
487
488 META_Object(osgVolume, PropertyAdjustmentCallback);
489
490 virtual NodeCallback* asNodeCallback() { return osg::NodeCallback::asNodeCallback(); }
491 virtual const NodeCallback* asNodeCallback() const { return osg::NodeCallback::asNodeCallback(); }
492
493 virtual DrawableEventCallback* asDrawableEventCallback() { return osg::DrawableEventCallback::asDrawableEventCallback(); }
494 virtual const DrawableEventCallback* asDrawableEventCallback() const { return osg::DrawableEventCallback::asDrawableEventCallback(); }
495
496 virtual osgGA::EventHandler* asEventHandler() { return osgGA::EventHandler::asEventHandler(); }
497 virtual const osgGA::EventHandler* asEventHandler() const { return osgGA::EventHandler::asEventHandler(); }
498
499 virtual bool run(osg::Object* object, osg::Object* data) { return osgGA::GUIEventHandler::run(object, data); }
500
501 void setKeyEventCycleForward(int key) { _cyleForwardKey = key; }
502 int getKeyEventCycleForward() const { return _cyleForwardKey; }
503
504 void setKeyEventCycleBackward(int key) { _cyleBackwardKey = key; }
505 int getKeyEventCycleBackward() const { return _cyleBackwardKey; }
506
507 void setKeyEventActivatesTransparencyAdjustment(int key) { _transparencyKey = key; }
508 int getKeyEventActivatesTransparencyAdjustment() const { return _transparencyKey; }
509
510 void setKeyEventActivatesExteriorTransparencyFactorAdjustment(int key) { _exteriorTransparencyFactorKey = key; }
511 int getKeyEventActivatesExteriorTransparencyFactorAdjustment() const { return _exteriorTransparencyFactorKey; }
512
513 void setKeyEventActivatesSampleDensityAdjustment(int key) { _sampleDensityKey = key; }
514 int getKeyEventActivatesSampleDensityAdjustment() const { return _sampleDensityKey; }
515
516 void setKeyEventActivatesAlphaFuncAdjustment(int key) { _alphaFuncKey = key; }
517 int getKeyEventActivatesAlphaFuncAdjustment() const { return _alphaFuncKey; }
518
519 virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*);
520
521 int _cyleForwardKey;
522 int _cyleBackwardKey;
523 int _transparencyKey;
524 int _exteriorTransparencyFactorKey;
525 int _alphaFuncKey;
526 int _sampleDensityKey;
527
528 bool _updateTransparency;
529 bool _updateExteriorTransparencyFactor;
530 bool _updateAlphaCutOff;
531 bool _updateSampleDensity;
532};
533
534}
535
536#endif