Locator 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_LOCATOR
  14. #define OSGVOLUME_LOCATOR 1
  15. #include <osgVolume/Export>
  16. #include <osg/Object>
  17. #include <osg/observer_ptr>
  18. #include <osg/Matrixd>
  19. #include <osg/TexGen>
  20. #include <osg/MatrixTransform>
  21. #include <vector>
  22. namespace osgVolume {
  23. class OSGVOLUME_EXPORT Locator : public osg::Object
  24. {
  25. public:
  26. Locator() {}
  27. Locator(const osg::Matrixd& transform) { setTransform(transform); }
  28. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  29. Locator(const Locator& locator,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  30. osg::Object(locator, copyop),
  31. _transform(locator._transform) {}
  32. META_Object(osgVolume, Locator);
  33. /** Set the transformation from local coordinates to model coordinates.*/
  34. void setTransform(const osg::Matrixd& transform) { _transform = transform; _inverse.invert(_transform); locatorModified(); }
  35. /** Set the transformation from local coordinates to model coordinates.*/
  36. const osg::Matrixd& getTransform() const { return _transform; }
  37. /** Set the extents of the local coords.*/
  38. void setTransformAsExtents(double minX, double minY, double maxX, double maxY, double minZ, double maxZ);
  39. virtual bool convertLocalToModel(const osg::Vec3d& /*local*/, osg::Vec3d& /*world*/) const;
  40. virtual bool convertModelToLocal(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) const;
  41. static bool convertLocalCoordBetween(const Locator& source, const osg::Vec3d& sourceNDC,
  42. const Locator& destination, osg::Vec3d& destinationNDC)
  43. {
  44. osg::Vec3d model;
  45. if (!source.convertLocalToModel(sourceNDC, model)) return false;
  46. if (!destination.convertModelToLocal(model, destinationNDC)) return false;
  47. return true;
  48. }
  49. bool computeLocalBounds(osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const;
  50. bool computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const;
  51. /** Return true if the axis of the Locator are inverted requiring the faces of any cubes used from rendering to be flipped to ensure the correct front/back face is used.*/
  52. bool inverted() const;
  53. /** apply the appropriate FrontFace setting to provided StateSet to ensure that the rendering of hull of the volume is the correct orientation.*/
  54. void applyAppropriateFrontFace(osg::StateSet* ss) const;
  55. /** Callback interface for enabling the monitoring of changes to the Locator.*/
  56. class LocatorCallback : virtual public osg::Object
  57. {
  58. public:
  59. LocatorCallback() {}
  60. LocatorCallback(const LocatorCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(rhs,copyop) {}
  61. META_Object(osgVolume, LocatorCallback);
  62. virtual void locatorModified(Locator* /*locator*/) {};
  63. protected:
  64. virtual ~LocatorCallback() {}
  65. };
  66. void addCallback(LocatorCallback* callback);
  67. template<class T> void addCallback(const osg::ref_ptr<T>& callback) { addCallback(callback.get()); }
  68. void removeCallback(LocatorCallback* callback);
  69. typedef std::vector< osg::ref_ptr<LocatorCallback> > LocatorCallbacks;
  70. LocatorCallbacks& getLocatorCallbacks() { return _locatorCallbacks; }
  71. const LocatorCallbacks& getLocatorCallbacks() const { return _locatorCallbacks; }
  72. protected:
  73. void locatorModified();
  74. osg::Matrixd _transform;
  75. osg::Matrixd _inverse;
  76. LocatorCallbacks _locatorCallbacks;
  77. };
  78. class OSGVOLUME_EXPORT TransformLocatorCallback : public Locator::LocatorCallback
  79. {
  80. public:
  81. TransformLocatorCallback(osg::MatrixTransform* transform);
  82. void locatorModified(Locator* locator);
  83. protected:
  84. osg::observer_ptr<osg::MatrixTransform> _transform;
  85. };
  86. class OSGVOLUME_EXPORT TexGenLocatorCallback : public Locator::LocatorCallback
  87. {
  88. public:
  89. TexGenLocatorCallback(osg::TexGen* texgen, Locator* geometryLocator, Locator* imageLocator);
  90. void locatorModified(Locator*);
  91. protected:
  92. osg::observer_ptr<osg::TexGen> _texgen;
  93. osg::observer_ptr<osgVolume::Locator> _geometryLocator;
  94. osg::observer_ptr<osgVolume::Locator> _imageLocator;
  95. };
  96. }
  97. #endif