Constraint 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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. //osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
  14. #ifndef OSGMANIPULATOR_CONSTRAINT
  15. #define OSGMANIPULATOR_CONSTRAINT 1
  16. #include <osgManipulator/Export>
  17. #include <osg/observer_ptr>
  18. #include <osg/Node>
  19. #include <osg/Matrix>
  20. namespace osgManipulator {
  21. class MotionCommand;
  22. class TranslateInLineCommand;
  23. class TranslateInPlaneCommand;
  24. class Scale1DCommand;
  25. class Scale2DCommand;
  26. class ScaleUniformCommand;
  27. class Rotate3DCommand;
  28. class DraggerCallback : virtual public osg::Object
  29. {
  30. public:
  31. DraggerCallback():
  32. osg::Object(true) {}
  33. DraggerCallback(const DraggerCallback& org, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
  34. osg::Object(org, copyop) {}
  35. META_Object(osgManipulator, DraggerCallback);
  36. /**
  37. * Receive motion commands. Returns true on success.
  38. */
  39. virtual bool receive(const MotionCommand&) { return false; }
  40. virtual bool receive(const TranslateInLineCommand& command) { return receive((MotionCommand&)command); }
  41. virtual bool receive(const TranslateInPlaneCommand& command) { return receive((MotionCommand&)command); }
  42. virtual bool receive(const Scale1DCommand& command) { return receive((MotionCommand&)command); }
  43. virtual bool receive(const Scale2DCommand& command) { return receive((MotionCommand&)command); }
  44. virtual bool receive(const ScaleUniformCommand& command) { return receive((MotionCommand&)command); }
  45. virtual bool receive(const Rotate3DCommand& command) { return receive((MotionCommand&)command); }
  46. };
  47. class OSGMANIPULATOR_EXPORT Constraint : public osg::Referenced
  48. {
  49. public:
  50. virtual bool constrain(MotionCommand&) const { return false; }
  51. virtual bool constrain(TranslateInLineCommand& command) const { return constrain((MotionCommand&)command); }
  52. virtual bool constrain(TranslateInPlaneCommand& command) const { return constrain((MotionCommand&)command); }
  53. virtual bool constrain(Scale1DCommand& command) const { return constrain((MotionCommand&)command); }
  54. virtual bool constrain(Scale2DCommand& command) const { return constrain((MotionCommand&)command); }
  55. virtual bool constrain(ScaleUniformCommand& command) const { return constrain((MotionCommand&)command); }
  56. virtual bool constrain(Rotate3DCommand& command) const { return constrain((MotionCommand&)command); }
  57. protected:
  58. Constraint() {}
  59. Constraint(osg::Node& refNode) : _refNode(&refNode) {}
  60. virtual ~Constraint() {}
  61. osg::Node& getReferenceNode() { return *_refNode; }
  62. const osg::Node& getReferenceNode() const { return *_refNode; }
  63. const osg::Matrix& getLocalToWorld() const { return _localToWorld; }
  64. const osg::Matrix& getWorldToLocal() const { return _worldToLocal; }
  65. void computeLocalToWorldAndWorldToLocal() const;
  66. private:
  67. osg::observer_ptr<osg::Node> _refNode;
  68. mutable osg::Matrix _localToWorld;
  69. mutable osg::Matrix _worldToLocal;
  70. };
  71. /**
  72. * Constraint to snap motion commands to a sugar cube grid.
  73. */
  74. class OSGMANIPULATOR_EXPORT GridConstraint : public Constraint
  75. {
  76. public:
  77. GridConstraint(osg::Node& refNode, const osg::Vec3d& origin, const osg::Vec3d& spacing);
  78. void setOrigin(const osg::Vec3d& origin) { _origin = origin; }
  79. const osg::Vec3d& getOrigin() const { return _origin; }
  80. void setSpacing(const osg::Vec3d& spacing) { _spacing = spacing; }
  81. const osg::Vec3d& getSpacing() const { return _spacing; }
  82. virtual bool constrain(TranslateInLineCommand& command) const;
  83. virtual bool constrain(TranslateInPlaneCommand& command) const;
  84. virtual bool constrain(Scale1DCommand& command) const;
  85. virtual bool constrain(Scale2DCommand& command) const;
  86. virtual bool constrain(ScaleUniformCommand& command) const;
  87. protected:
  88. virtual ~GridConstraint() {}
  89. private:
  90. osg::Vec3d _origin;
  91. osg::Vec3d _spacing;
  92. mutable osg::Matrix _startMatrix;
  93. mutable osg::Matrix _matrix;
  94. };
  95. }
  96. #endif