1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
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.
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.
14#ifndef OSGVOLUME_PROPERTY
15#define OSGVOLUME_PROPERTY 1
17#include <osg/TransferFunction>
19#include <osg/AlphaFunc>
21#include <osgGA/GUIEventHandler>
23#include <osgVolume/Export>
29class CompositeProperty;
31class TransferFunctionProperty;
33class IsoSurfaceProperty;
34class MaximumIntensityProjectionProperty;
35class LightingProperty;
36class AlphaFuncProperty;
37class SampleRatioProperty;
38class SampleRatioWhenMovingProperty;
39class SampleDensityProperty;
40class SampleDensityWhenMovingProperty;
41class TransparencyProperty;
42class ExteriorTransparencyFactorProperty;
45class OSGVOLUME_EXPORT PropertyVisitor
49 PropertyVisitor(bool traverseOnlyActiveChildren=true);
51 virtual ~PropertyVisitor() {}
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&);
70 bool _traverseOnlyActiveChildren;
74class OSGVOLUME_EXPORT Property : public osg::Object
80 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
81 Property(const Property&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
83 META_Object(osgVolume, Property);
85 void dirty() { ++_modifiedCount; }
87 void setModifiedCount(unsigned int c) { _modifiedCount = c; }
88 unsigned int getModifiedCount() const { return _modifiedCount; }
90 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
91 virtual void traverse(PropertyVisitor& /*pv*/) {}
97 unsigned int _modifiedCount;
100class OSGVOLUME_EXPORT CompositeProperty : public Property
106 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
107 CompositeProperty(const CompositeProperty& compositeProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
109 META_Object(osgVolume, CompositeProperty);
111 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
113 virtual void traverse(PropertyVisitor& pv)
115 for(Properties::iterator itr = _properties.begin();
116 itr != _properties.end();
125 typedef std::vector< osg::ref_ptr<Property> > Properties;
127 void setProperty(unsigned int i, Property* property) { if (i>=_properties.size()) _properties.resize(i+1); _properties[i] = property; }
129 template<class T> void setProperty(unsigned int i, const osg::ref_ptr<T>& p) { setProperty(i, p.get()); }
131 Property* getProperty(unsigned int i) { return i<_properties.size() ? _properties[i].get() : 0; }
133 const Property* getProperty(unsigned int i) const { return i<_properties.size() ? _properties[i].get() : 0; }
135 void addProperty(Property* property) { _properties.push_back(property); dirty(); }
137 template<class T> void addProperty(const osg::ref_ptr<T>& p) { addProperty(p.get()); }
139 void removeProperty(unsigned int i) { _properties.erase(_properties.begin()+i); }
141 unsigned int getNumProperties() const { return _properties.size(); }
145 virtual ~CompositeProperty() {}
148 Properties _properties;
152class OSGVOLUME_EXPORT SwitchProperty : public CompositeProperty
158 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
159 SwitchProperty(const SwitchProperty& switchProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
161 META_Object(osgVolume, SwitchProperty);
163 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
165 virtual void traverse(PropertyVisitor& pv)
167 if (pv._traverseOnlyActiveChildren)
169 if (_activeProperty>=0 && static_cast<unsigned int>(_activeProperty)<=getNumProperties())
171 _properties[_activeProperty]->accept(pv);
176 CompositeProperty::traverse(pv);
181 /** Set which child property is active.
182 * -1 disables all children.*/
183 void setActiveProperty(int i) { _activeProperty = i; dirty(); }
185 /** Get the active property.*/
186 int getActiveProperty() const { return _activeProperty; }
190 virtual ~SwitchProperty() {}
195class OSGVOLUME_EXPORT TransferFunctionProperty : public Property
199 TransferFunctionProperty(osg::TransferFunction* tf = 0);
201 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
202 TransferFunctionProperty(const TransferFunctionProperty& tfp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
204 META_Object(osgVolume, TransferFunctionProperty);
206 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
208 /** Set the transfer function.*/
209 void setTransferFunction(osg::TransferFunction* tf) { _tf = tf; }
211 /** Get the transfer function.*/
212 osg::TransferFunction* getTransferFunction() { return _tf.get(); }
214 /** Get the const transfer function.*/
215 const osg::TransferFunction* getTransferFunction() const { return _tf.get(); }
219 virtual ~TransferFunctionProperty() {}
221 osg::ref_ptr<osg::TransferFunction> _tf;
226class OSGVOLUME_EXPORT ScalarProperty : public Property
230 ScalarProperty(const std::string& scaleName, float value);
232 ScalarProperty(const ScalarProperty& scalarProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
234 META_Object(osgVolume, ScalarProperty);
236 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
239 virtual void setValue(float v) { _uniform->set(v); dirty(); }
242 float getValue() const { float v; _uniform->get(v); return v; }
244 /** Get the underlying uniform.*/
245 osg::Uniform* getUniform() { return _uniform.get(); }
247 /** Get the underlying uniform.*/
248 const osg::Uniform* getUniform() const { return _uniform.get(); }
252 virtual ~ScalarProperty() {}
256 osg::ref_ptr<osg::Uniform> _uniform;
260class OSGVOLUME_EXPORT IsoSurfaceProperty : public ScalarProperty
264 IsoSurfaceProperty(float value=1.0f);
266 IsoSurfaceProperty(const IsoSurfaceProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
268 META_Object(osgVolume, IsoSurfaceProperty);
270 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
274 virtual ~IsoSurfaceProperty() {}
277class OSGVOLUME_EXPORT AlphaFuncProperty : public ScalarProperty
281 AlphaFuncProperty(float value=1.0f);
283 AlphaFuncProperty(const AlphaFuncProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
285 META_Object(osgVolume, AlphaFuncProperty);
287 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
289 virtual void setValue(float v);
291 osg::AlphaFunc* getAlphaFunc() { return _alphaFunc.get(); }
293 const osg::AlphaFunc* getAlphaFunc() const { return _alphaFunc.get(); }
298 virtual ~AlphaFuncProperty() {}
300 osg::ref_ptr<osg::AlphaFunc> _alphaFunc;
303class OSGVOLUME_EXPORT MaximumIntensityProjectionProperty : public Property
307 MaximumIntensityProjectionProperty();
309 MaximumIntensityProjectionProperty(const MaximumIntensityProjectionProperty& mipp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
311 META_Object(osgVolume, MaximumIntensityProjectionProperty);
313 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
317 virtual ~MaximumIntensityProjectionProperty() {}
321class OSGVOLUME_EXPORT LightingProperty : public Property
327 LightingProperty(const LightingProperty& mipp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
329 META_Object(osgVolume, LightingProperty);
331 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
335 virtual ~LightingProperty() {}
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
344 SampleDensityProperty(float value=1.0f);
346 SampleDensityProperty(const SampleDensityProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
348 META_Object(osgVolume, SampleDensityProperty);
350 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
354 virtual ~SampleDensityProperty() {}
357/** Sample density to use when the volume is moving relative to the eye point.*/
358class OSGVOLUME_EXPORT SampleDensityWhenMovingProperty : public ScalarProperty
362 SampleDensityWhenMovingProperty(float value=1.0f);
364 SampleDensityWhenMovingProperty(const SampleDensityWhenMovingProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
366 META_Object(osgVolume, SampleDensityWhenMovingProperty);
368 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
372 virtual ~SampleDensityWhenMovingProperty() {}
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
380 SampleRatioProperty(float value=1.0f);
382 SampleRatioProperty(const SampleRatioProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
384 META_Object(osgVolume, SampleRatioProperty);
386 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
390 virtual ~SampleRatioProperty() {}
393/** Sample density to use when the volume is moving relative to the eye point.*/
394class OSGVOLUME_EXPORT SampleRatioWhenMovingProperty : public ScalarProperty
398 SampleRatioWhenMovingProperty(float value=1.0f);
400 SampleRatioWhenMovingProperty(const SampleRatioWhenMovingProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
402 META_Object(osgVolume, SampleRatioWhenMovingProperty);
404 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
408 virtual ~SampleRatioWhenMovingProperty() {}
412class OSGVOLUME_EXPORT TransparencyProperty : public ScalarProperty
416 TransparencyProperty(float value=1.0f);
418 TransparencyProperty(const TransparencyProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
420 META_Object(osgVolume, TransparencyProperty);
422 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
426 virtual ~TransparencyProperty() {}
429class OSGVOLUME_EXPORT ExteriorTransparencyFactorProperty : public ScalarProperty
433 ExteriorTransparencyFactorProperty(float value=0.0f);
435 ExteriorTransparencyFactorProperty(const ExteriorTransparencyFactorProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
437 META_Object(osgVolume, ExteriorTransparencyFactorProperty);
439 virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
443 virtual ~ExteriorTransparencyFactorProperty() {}
447class OSGVOLUME_EXPORT CollectPropertiesVisitor : public osgVolume::PropertyVisitor
451 CollectPropertiesVisitor(bool traverseOnlyActiveChildren=true);
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);
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;
480class OSGVOLUME_EXPORT PropertyAdjustmentCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback
484 PropertyAdjustmentCallback();
486 PropertyAdjustmentCallback(const PropertyAdjustmentCallback&,const osg::CopyOp&);
488 META_Object(osgVolume, PropertyAdjustmentCallback);
490 virtual NodeCallback* asNodeCallback() { return osg::NodeCallback::asNodeCallback(); }
491 virtual const NodeCallback* asNodeCallback() const { return osg::NodeCallback::asNodeCallback(); }
493 virtual DrawableEventCallback* asDrawableEventCallback() { return osg::DrawableEventCallback::asDrawableEventCallback(); }
494 virtual const DrawableEventCallback* asDrawableEventCallback() const { return osg::DrawableEventCallback::asDrawableEventCallback(); }
496 virtual osgGA::EventHandler* asEventHandler() { return osgGA::EventHandler::asEventHandler(); }
497 virtual const osgGA::EventHandler* asEventHandler() const { return osgGA::EventHandler::asEventHandler(); }
499 virtual bool run(osg::Object* object, osg::Object* data) { return osgGA::GUIEventHandler::run(object, data); }
501 void setKeyEventCycleForward(int key) { _cyleForwardKey = key; }
502 int getKeyEventCycleForward() const { return _cyleForwardKey; }
504 void setKeyEventCycleBackward(int key) { _cyleBackwardKey = key; }
505 int getKeyEventCycleBackward() const { return _cyleBackwardKey; }
507 void setKeyEventActivatesTransparencyAdjustment(int key) { _transparencyKey = key; }
508 int getKeyEventActivatesTransparencyAdjustment() const { return _transparencyKey; }
510 void setKeyEventActivatesExteriorTransparencyFactorAdjustment(int key) { _exteriorTransparencyFactorKey = key; }
511 int getKeyEventActivatesExteriorTransparencyFactorAdjustment() const { return _exteriorTransparencyFactorKey; }
513 void setKeyEventActivatesSampleDensityAdjustment(int key) { _sampleDensityKey = key; }
514 int getKeyEventActivatesSampleDensityAdjustment() const { return _sampleDensityKey; }
516 void setKeyEventActivatesAlphaFuncAdjustment(int key) { _alphaFuncKey = key; }
517 int getKeyEventActivatesAlphaFuncAdjustment() const { return _alphaFuncKey; }
519 virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*);
522 int _cyleBackwardKey;
523 int _transparencyKey;
524 int _exteriorTransparencyFactorKey;
526 int _sampleDensityKey;
528 bool _updateTransparency;
529 bool _updateExteriorTransparencyFactor;
530 bool _updateAlphaCutOff;
531 bool _updateSampleDensity;