Property 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. #ifndef OSGVOLUME_PROPERTY
  14. #define OSGVOLUME_PROPERTY 1
  15. #include <osg/TransferFunction>
  16. #include <osg/Uniform>
  17. #include <osg/AlphaFunc>
  18. #include <osgGA/GUIEventHandler>
  19. #include <osgVolume/Export>
  20. namespace osgVolume {
  21. // forward decarle
  22. class Property;
  23. class CompositeProperty;
  24. class SwitchProperty;
  25. class TransferFunctionProperty;
  26. class ScalarProperty;
  27. class IsoSurfaceProperty;
  28. class MaximumIntensityProjectionProperty;
  29. class LightingProperty;
  30. class AlphaFuncProperty;
  31. class SampleRatioProperty;
  32. class SampleRatioWhenMovingProperty;
  33. class SampleDensityProperty;
  34. class SampleDensityWhenMovingProperty;
  35. class TransparencyProperty;
  36. class ExteriorTransparencyFactorProperty;
  37. class VolumeSettings;
  38. class OSGVOLUME_EXPORT PropertyVisitor
  39. {
  40. public:
  41. PropertyVisitor(bool traverseOnlyActiveChildren=true);
  42. virtual ~PropertyVisitor() {}
  43. virtual void apply(Property&);
  44. virtual void apply(CompositeProperty&);
  45. virtual void apply(SwitchProperty&);
  46. virtual void apply(TransferFunctionProperty&);
  47. virtual void apply(ScalarProperty&);
  48. virtual void apply(IsoSurfaceProperty&);
  49. virtual void apply(AlphaFuncProperty&);
  50. virtual void apply(MaximumIntensityProjectionProperty&);
  51. virtual void apply(LightingProperty&);
  52. virtual void apply(SampleRatioProperty&);
  53. virtual void apply(SampleRatioWhenMovingProperty&);
  54. virtual void apply(SampleDensityProperty&);
  55. virtual void apply(SampleDensityWhenMovingProperty&);
  56. virtual void apply(TransparencyProperty&);
  57. virtual void apply(ExteriorTransparencyFactorProperty&);
  58. virtual void apply(VolumeSettings&);
  59. bool _traverseOnlyActiveChildren;
  60. };
  61. class OSGVOLUME_EXPORT Property : public osg::Object
  62. {
  63. public:
  64. Property();
  65. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  66. Property(const Property&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  67. META_Object(osgVolume, Property);
  68. void dirty() { ++_modifiedCount; }
  69. void setModifiedCount(unsigned int c) { _modifiedCount = c; }
  70. unsigned int getModifiedCount() const { return _modifiedCount; }
  71. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  72. virtual void traverse(PropertyVisitor& /*pv*/) {}
  73. protected:
  74. virtual ~Property();
  75. unsigned int _modifiedCount;
  76. };
  77. class OSGVOLUME_EXPORT CompositeProperty : public Property
  78. {
  79. public:
  80. CompositeProperty();
  81. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  82. CompositeProperty(const CompositeProperty& compositeProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  83. META_Object(osgVolume, CompositeProperty);
  84. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  85. virtual void traverse(PropertyVisitor& pv)
  86. {
  87. for(Properties::iterator itr = _properties.begin();
  88. itr != _properties.end();
  89. ++itr)
  90. {
  91. (*itr)->accept(pv);
  92. }
  93. }
  94. void clear();
  95. typedef std::vector< osg::ref_ptr<Property> > Properties;
  96. void setProperty(unsigned int i, Property* property) { if (i>=_properties.size()) _properties.resize(i+1); _properties[i] = property; }
  97. template<class T> void setProperty(unsigned int i, const osg::ref_ptr<T>& p) { setProperty(i, p.get()); }
  98. Property* getProperty(unsigned int i) { return i<_properties.size() ? _properties[i].get() : 0; }
  99. const Property* getProperty(unsigned int i) const { return i<_properties.size() ? _properties[i].get() : 0; }
  100. void addProperty(Property* property) { _properties.push_back(property); dirty(); }
  101. template<class T> void addProperty(const osg::ref_ptr<T>& p) { addProperty(p.get()); }
  102. void removeProperty(unsigned int i) { _properties.erase(_properties.begin()+i); }
  103. unsigned int getNumProperties() const { return _properties.size(); }
  104. protected:
  105. virtual ~CompositeProperty() {}
  106. Properties _properties;
  107. };
  108. class OSGVOLUME_EXPORT SwitchProperty : public CompositeProperty
  109. {
  110. public:
  111. SwitchProperty();
  112. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  113. SwitchProperty(const SwitchProperty& switchProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  114. META_Object(osgVolume, SwitchProperty);
  115. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  116. virtual void traverse(PropertyVisitor& pv)
  117. {
  118. if (pv._traverseOnlyActiveChildren)
  119. {
  120. if (_activeProperty>=0 && static_cast<unsigned int>(_activeProperty)<=getNumProperties())
  121. {
  122. _properties[_activeProperty]->accept(pv);
  123. }
  124. }
  125. else
  126. {
  127. CompositeProperty::traverse(pv);
  128. }
  129. }
  130. /** Set which child property is active.
  131. * -1 disables all children.*/
  132. void setActiveProperty(int i) { _activeProperty = i; dirty(); }
  133. /** Get the active property.*/
  134. int getActiveProperty() const { return _activeProperty; }
  135. protected:
  136. virtual ~SwitchProperty() {}
  137. int _activeProperty;
  138. };
  139. class OSGVOLUME_EXPORT TransferFunctionProperty : public Property
  140. {
  141. public:
  142. TransferFunctionProperty(osg::TransferFunction* tf = 0);
  143. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  144. TransferFunctionProperty(const TransferFunctionProperty& tfp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  145. META_Object(osgVolume, TransferFunctionProperty);
  146. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  147. /** Set the transfer function.*/
  148. void setTransferFunction(osg::TransferFunction* tf) { _tf = tf; }
  149. /** Get the transfer function.*/
  150. osg::TransferFunction* getTransferFunction() { return _tf.get(); }
  151. /** Get the const transfer function.*/
  152. const osg::TransferFunction* getTransferFunction() const { return _tf.get(); }
  153. protected:
  154. virtual ~TransferFunctionProperty() {}
  155. osg::ref_ptr<osg::TransferFunction> _tf;
  156. };
  157. class OSGVOLUME_EXPORT ScalarProperty : public Property
  158. {
  159. public:
  160. ScalarProperty(const std::string& scaleName, float value);
  161. ScalarProperty(const ScalarProperty& scalarProperty,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  162. META_Object(osgVolume, ScalarProperty);
  163. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  164. /** Set the value.*/
  165. virtual void setValue(float v) { _uniform->set(v); dirty(); }
  166. /** Get the value.*/
  167. float getValue() const { float v; _uniform->get(v); return v; }
  168. /** Get the underlying uniform.*/
  169. osg::Uniform* getUniform() { return _uniform.get(); }
  170. /** Get the underlying uniform.*/
  171. const osg::Uniform* getUniform() const { return _uniform.get(); }
  172. protected:
  173. virtual ~ScalarProperty() {}
  174. ScalarProperty();
  175. osg::ref_ptr<osg::Uniform> _uniform;
  176. };
  177. class OSGVOLUME_EXPORT IsoSurfaceProperty : public ScalarProperty
  178. {
  179. public:
  180. IsoSurfaceProperty(float value=1.0f);
  181. IsoSurfaceProperty(const IsoSurfaceProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  182. META_Object(osgVolume, IsoSurfaceProperty);
  183. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  184. protected:
  185. virtual ~IsoSurfaceProperty() {}
  186. };
  187. class OSGVOLUME_EXPORT AlphaFuncProperty : public ScalarProperty
  188. {
  189. public:
  190. AlphaFuncProperty(float value=1.0f);
  191. AlphaFuncProperty(const AlphaFuncProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  192. META_Object(osgVolume, AlphaFuncProperty);
  193. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  194. virtual void setValue(float v);
  195. osg::AlphaFunc* getAlphaFunc() { return _alphaFunc.get(); }
  196. const osg::AlphaFunc* getAlphaFunc() const { return _alphaFunc.get(); }
  197. protected:
  198. virtual ~AlphaFuncProperty() {}
  199. osg::ref_ptr<osg::AlphaFunc> _alphaFunc;
  200. };
  201. class OSGVOLUME_EXPORT MaximumIntensityProjectionProperty : public Property
  202. {
  203. public:
  204. MaximumIntensityProjectionProperty();
  205. MaximumIntensityProjectionProperty(const MaximumIntensityProjectionProperty& mipp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  206. META_Object(osgVolume, MaximumIntensityProjectionProperty);
  207. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  208. protected:
  209. virtual ~MaximumIntensityProjectionProperty() {}
  210. };
  211. class OSGVOLUME_EXPORT LightingProperty : public Property
  212. {
  213. public:
  214. LightingProperty();
  215. LightingProperty(const LightingProperty& mipp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  216. META_Object(osgVolume, LightingProperty);
  217. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  218. protected:
  219. virtual ~LightingProperty() {}
  220. };
  221. /** Sample density to use when the volume is static relative to the eye point or when moving if no SampleDensityWhenMovingProperty is assigned.*/
  222. class OSGVOLUME_EXPORT SampleDensityProperty : public ScalarProperty
  223. {
  224. public:
  225. SampleDensityProperty(float value=1.0f);
  226. SampleDensityProperty(const SampleDensityProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  227. META_Object(osgVolume, SampleDensityProperty);
  228. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  229. protected:
  230. virtual ~SampleDensityProperty() {}
  231. };
  232. /** Sample density to use when the volume is moving relative to the eye point.*/
  233. class OSGVOLUME_EXPORT SampleDensityWhenMovingProperty : public ScalarProperty
  234. {
  235. public:
  236. SampleDensityWhenMovingProperty(float value=1.0f);
  237. SampleDensityWhenMovingProperty(const SampleDensityWhenMovingProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  238. META_Object(osgVolume, SampleDensityWhenMovingProperty);
  239. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  240. protected:
  241. virtual ~SampleDensityWhenMovingProperty() {}
  242. };
  243. /** Sample ratioto use when the volume is static relative to the eye point or when moving if no SampleRatioWhenMovingProperty is assigned.*/
  244. class OSGVOLUME_EXPORT SampleRatioProperty : public ScalarProperty
  245. {
  246. public:
  247. SampleRatioProperty(float value=1.0f);
  248. SampleRatioProperty(const SampleRatioProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  249. META_Object(osgVolume, SampleRatioProperty);
  250. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  251. protected:
  252. virtual ~SampleRatioProperty() {}
  253. };
  254. /** Sample density to use when the volume is moving relative to the eye point.*/
  255. class OSGVOLUME_EXPORT SampleRatioWhenMovingProperty : public ScalarProperty
  256. {
  257. public:
  258. SampleRatioWhenMovingProperty(float value=1.0f);
  259. SampleRatioWhenMovingProperty(const SampleRatioWhenMovingProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  260. META_Object(osgVolume, SampleRatioWhenMovingProperty);
  261. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  262. protected:
  263. virtual ~SampleRatioWhenMovingProperty() {}
  264. };
  265. class OSGVOLUME_EXPORT TransparencyProperty : public ScalarProperty
  266. {
  267. public:
  268. TransparencyProperty(float value=1.0f);
  269. TransparencyProperty(const TransparencyProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  270. META_Object(osgVolume, TransparencyProperty);
  271. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  272. protected:
  273. virtual ~TransparencyProperty() {}
  274. };
  275. class OSGVOLUME_EXPORT ExteriorTransparencyFactorProperty : public ScalarProperty
  276. {
  277. public:
  278. ExteriorTransparencyFactorProperty(float value=0.0f);
  279. ExteriorTransparencyFactorProperty(const ExteriorTransparencyFactorProperty& isp,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  280. META_Object(osgVolume, ExteriorTransparencyFactorProperty);
  281. virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
  282. protected:
  283. virtual ~ExteriorTransparencyFactorProperty() {}
  284. };
  285. class OSGVOLUME_EXPORT CollectPropertiesVisitor : public osgVolume::PropertyVisitor
  286. {
  287. public:
  288. CollectPropertiesVisitor(bool traverseOnlyActiveChildren=true);
  289. virtual void apply(TransferFunctionProperty&);
  290. virtual void apply(ScalarProperty&);
  291. virtual void apply(IsoSurfaceProperty& iso);
  292. virtual void apply(AlphaFuncProperty& af);
  293. virtual void apply(MaximumIntensityProjectionProperty& mip);
  294. virtual void apply(LightingProperty& lp);
  295. virtual void apply(SampleDensityProperty& sdp);
  296. virtual void apply(SampleDensityWhenMovingProperty& sdp);
  297. virtual void apply(SampleRatioProperty& sdp);
  298. virtual void apply(SampleRatioWhenMovingProperty& sdp);
  299. virtual void apply(TransparencyProperty& tp);
  300. virtual void apply(ExteriorTransparencyFactorProperty& tp);
  301. osg::ref_ptr<TransferFunctionProperty> _tfProperty;
  302. osg::ref_ptr<IsoSurfaceProperty> _isoProperty;
  303. osg::ref_ptr<AlphaFuncProperty> _afProperty;
  304. osg::ref_ptr<MaximumIntensityProjectionProperty> _mipProperty;
  305. osg::ref_ptr<LightingProperty> _lightingProperty;
  306. osg::ref_ptr<SampleDensityProperty> _sampleDensityProperty;
  307. osg::ref_ptr<SampleDensityWhenMovingProperty> _sampleDensityWhenMovingProperty;
  308. osg::ref_ptr<SampleRatioProperty> _sampleRatioProperty;
  309. osg::ref_ptr<SampleRatioWhenMovingProperty> _sampleRatioWhenMovingProperty;
  310. osg::ref_ptr<TransparencyProperty> _transparencyProperty;
  311. osg::ref_ptr<ExteriorTransparencyFactorProperty> _exteriorTransparencyFactorProperty;
  312. };
  313. class OSGVOLUME_EXPORT PropertyAdjustmentCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback
  314. {
  315. public:
  316. PropertyAdjustmentCallback();
  317. PropertyAdjustmentCallback(const PropertyAdjustmentCallback&,const osg::CopyOp&);
  318. META_Object(osgVolume, PropertyAdjustmentCallback);
  319. virtual NodeCallback* asNodeCallback() { return osg::NodeCallback::asNodeCallback(); }
  320. virtual const NodeCallback* asNodeCallback() const { return osg::NodeCallback::asNodeCallback(); }
  321. virtual DrawableEventCallback* asDrawableEventCallback() { return osg::DrawableEventCallback::asDrawableEventCallback(); }
  322. virtual const DrawableEventCallback* asDrawableEventCallback() const { return osg::DrawableEventCallback::asDrawableEventCallback(); }
  323. virtual osgGA::EventHandler* asEventHandler() { return osgGA::EventHandler::asEventHandler(); }
  324. virtual const osgGA::EventHandler* asEventHandler() const { return osgGA::EventHandler::asEventHandler(); }
  325. virtual bool run(osg::Object* object, osg::Object* data) { return osgGA::GUIEventHandler::run(object, data); }
  326. void setKeyEventCycleForward(int key) { _cyleForwardKey = key; }
  327. int getKeyEventCycleForward() const { return _cyleForwardKey; }
  328. void setKeyEventCycleBackward(int key) { _cyleBackwardKey = key; }
  329. int getKeyEventCycleBackward() const { return _cyleBackwardKey; }
  330. void setKeyEventActivatesTransparencyAdjustment(int key) { _transparencyKey = key; }
  331. int getKeyEventActivatesTransparencyAdjustment() const { return _transparencyKey; }
  332. void setKeyEventActivatesExteriorTransparencyFactorAdjustment(int key) { _exteriorTransparencyFactorKey = key; }
  333. int getKeyEventActivatesExteriorTransparencyFactorAdjustment() const { return _exteriorTransparencyFactorKey; }
  334. void setKeyEventActivatesSampleDensityAdjustment(int key) { _sampleDensityKey = key; }
  335. int getKeyEventActivatesSampleDensityAdjustment() const { return _sampleDensityKey; }
  336. void setKeyEventActivatesAlphaFuncAdjustment(int key) { _alphaFuncKey = key; }
  337. int getKeyEventActivatesAlphaFuncAdjustment() const { return _alphaFuncKey; }
  338. virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*);
  339. int _cyleForwardKey;
  340. int _cyleBackwardKey;
  341. int _transparencyKey;
  342. int _exteriorTransparencyFactorKey;
  343. int _alphaFuncKey;
  344. int _sampleDensityKey;
  345. bool _updateTransparency;
  346. bool _updateExteriorTransparencyFactor;
  347. bool _updateAlphaCutOff;
  348. bool _updateSampleDensity;
  349. };
  350. }
  351. #endif