Command 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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_COMMAND
  15. #define OSGMANIPULATOR_COMMAND 1
  16. #include <osgManipulator/Constraint>
  17. #include <osg/LineSegment>
  18. #include <osg/Plane>
  19. #include <osg/Vec2>
  20. #include <vector>
  21. namespace osgManipulator {
  22. /** Base class for motion commands that are generated by draggers. */
  23. class OSGMANIPULATOR_EXPORT MotionCommand : public osg::Referenced
  24. {
  25. public:
  26. /**
  27. * Motion command are based on click-drag-release actions. So each
  28. * command needs to indicate which stage of the motion the command
  29. * represents.
  30. */
  31. enum Stage
  32. {
  33. NONE,
  34. /** Click or pick start. */
  35. START,
  36. /** Drag or pick move. */
  37. MOVE,
  38. /** Release or pick finish. */
  39. FINISH
  40. };
  41. MotionCommand();
  42. /** create a MotionCommand that is the inverse of this command, and if applied will undo this commands changes. */
  43. virtual MotionCommand* createCommandInverse() = 0;
  44. /**
  45. * Gets the matrix for transforming the object being dragged. This matrix is in the
  46. * command's coordinate systems.
  47. */
  48. virtual osg::Matrix getMotionMatrix() const = 0;
  49. virtual void accept(const Constraint& constraint) { constraint.constrain(*this); }
  50. virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
  51. /**
  52. * Sets the matrix for transforming the command's local coordinate
  53. * system to the world/object coordinate system.
  54. */
  55. void setLocalToWorldAndWorldToLocal(const osg::Matrix& localToWorld, const osg::Matrix& worldToLocal)
  56. {
  57. _localToWorld = localToWorld;
  58. _worldToLocal = worldToLocal;
  59. }
  60. /**
  61. * Gets the matrix for transforming the command's local coordinate
  62. * system to the world/object coordinate system.
  63. */
  64. inline const osg::Matrix& getLocalToWorld() const { return _localToWorld; }
  65. /**
  66. * Gets the matrix for transforming the command's world/object
  67. * coordinate system to the command's local coordinate system.
  68. */
  69. inline const osg::Matrix& getWorldToLocal() const { return _worldToLocal; }
  70. void setStage(const Stage s) { _stage = s; }
  71. Stage getStage() const { return _stage; }
  72. protected:
  73. virtual ~MotionCommand();
  74. private:
  75. osg::Matrix _localToWorld;
  76. osg::Matrix _worldToLocal;
  77. Stage _stage;
  78. };
  79. /**
  80. * Command for translating in a line.
  81. */
  82. class OSGMANIPULATOR_EXPORT TranslateInLineCommand : public MotionCommand
  83. {
  84. public:
  85. TranslateInLineCommand();
  86. TranslateInLineCommand(const osg::LineSegment::vec_type& s, const osg::LineSegment::vec_type& e);
  87. virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
  88. virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
  89. virtual MotionCommand* createCommandInverse();
  90. inline void setLine(const osg::LineSegment::vec_type& s, const osg::LineSegment::vec_type& e) { _line->start() = s; _line->end() = e; }
  91. inline const osg::LineSegment::vec_type& getLineStart() const { return _line->start(); }
  92. inline const osg::LineSegment::vec_type& getLineEnd() const { return _line->end(); }
  93. inline void setTranslation(const osg::Vec3d& t) { _translation = t; }
  94. inline const osg::Vec3d& getTranslation() const { return _translation; }
  95. virtual osg::Matrix getMotionMatrix() const
  96. {
  97. return osg::Matrix::translate(_translation);
  98. }
  99. protected:
  100. virtual ~TranslateInLineCommand();
  101. private:
  102. osg::ref_ptr<osg::LineSegment> _line;
  103. osg::Vec3d _translation;
  104. };
  105. /**
  106. * Command for translating in a plane.
  107. */
  108. class OSGMANIPULATOR_EXPORT TranslateInPlaneCommand : public MotionCommand
  109. {
  110. public:
  111. TranslateInPlaneCommand();
  112. TranslateInPlaneCommand(const osg::Plane& plane);
  113. virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
  114. virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
  115. virtual MotionCommand* createCommandInverse();
  116. inline void setPlane(const osg::Plane& plane) { _plane = plane; }
  117. inline const osg::Plane& getPlane() const { return _plane; }
  118. inline void setTranslation(const osg::Vec3d& t) { _translation = t; }
  119. inline const osg::Vec3d& getTranslation() const { return _translation; }
  120. /** ReferencePoint is used only for snapping. */
  121. inline void setReferencePoint(const osg::Vec3d& rp) { _referencePoint = rp; }
  122. inline const osg::Vec3d& getReferencePoint() const { return _referencePoint; }
  123. virtual osg::Matrix getMotionMatrix() const
  124. {
  125. return osg::Matrix::translate(_translation);
  126. }
  127. protected:
  128. virtual ~TranslateInPlaneCommand();
  129. private:
  130. osg::Plane _plane;
  131. osg::Vec3d _translation;
  132. osg::Vec3d _referencePoint;
  133. };
  134. /**
  135. * Command for 1D scaling.
  136. */
  137. class OSGMANIPULATOR_EXPORT Scale1DCommand : public MotionCommand
  138. {
  139. public:
  140. Scale1DCommand();
  141. virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
  142. virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
  143. virtual MotionCommand* createCommandInverse();
  144. inline void setScale(double s) { _scale = s; }
  145. inline double getScale() const { return _scale; }
  146. inline void setScaleCenter(double center) { _scaleCenter = center; }
  147. inline double getScaleCenter() const { return _scaleCenter; }
  148. /** ReferencePoint is used only for snapping. */
  149. inline void setReferencePoint(double rp) { _referencePoint = rp; }
  150. inline double getReferencePoint() const { return _referencePoint; }
  151. inline void setMinScale(double min) { _minScale = min; }
  152. inline double getMinScale() const { return _minScale; }
  153. virtual osg::Matrix getMotionMatrix() const
  154. {
  155. return (osg::Matrix::translate(-_scaleCenter,0.0,0.0)
  156. * osg::Matrix::scale(_scale,1.0,1.0)
  157. * osg::Matrix::translate(_scaleCenter,0.0,0.0));
  158. }
  159. protected:
  160. virtual ~Scale1DCommand();
  161. private:
  162. double _scale;
  163. double _scaleCenter;
  164. double _referencePoint;
  165. double _minScale;
  166. };
  167. /**
  168. * Command for 2D scaling.
  169. */
  170. class OSGMANIPULATOR_EXPORT Scale2DCommand : public MotionCommand
  171. {
  172. public:
  173. Scale2DCommand();
  174. virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
  175. virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
  176. virtual MotionCommand* createCommandInverse();
  177. inline void setScale(const osg::Vec2d& s) { _scale = s; }
  178. inline const osg::Vec2d& getScale() const { return _scale; }
  179. inline void setScaleCenter(const osg::Vec2d& center) { _scaleCenter = center; }
  180. inline const osg::Vec2d& getScaleCenter() const { return _scaleCenter; }
  181. /** ReferencePoint is used only for snapping. */
  182. inline void setReferencePoint(const osg::Vec2d& rp) { _referencePoint = rp; }
  183. inline const osg::Vec2d& getReferencePoint() const { return _referencePoint; }
  184. inline void setMinScale(const osg::Vec2d& min) { _minScale = min; }
  185. inline const osg::Vec2d& getMinScale() const { return _minScale; }
  186. virtual osg::Matrix getMotionMatrix() const
  187. {
  188. return (osg::Matrix::translate(-_scaleCenter[0],0.0,-_scaleCenter[1])
  189. * osg::Matrix::scale(_scale[0],1.0,_scale[1])
  190. * osg::Matrix::translate(_scaleCenter[0],0.0,_scaleCenter[1]));
  191. }
  192. protected:
  193. virtual ~Scale2DCommand();
  194. private:
  195. osg::Vec2d _scale;
  196. osg::Vec2d _scaleCenter;
  197. osg::Vec2d _referencePoint;
  198. osg::Vec2d _minScale;
  199. };
  200. /**
  201. * Command for uniform 3D scaling.
  202. */
  203. class OSGMANIPULATOR_EXPORT ScaleUniformCommand : public MotionCommand
  204. {
  205. public:
  206. ScaleUniformCommand();
  207. virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
  208. virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
  209. virtual MotionCommand* createCommandInverse();
  210. inline void setScale(double s) { _scale = s; }
  211. inline double getScale() const { return _scale; }
  212. inline void setScaleCenter(const osg::Vec3d& center) { _scaleCenter = center; }
  213. inline const osg::Vec3d& getScaleCenter() const { return _scaleCenter; }
  214. virtual osg::Matrix getMotionMatrix() const
  215. {
  216. return (osg::Matrix::translate(-_scaleCenter)
  217. * osg::Matrix::scale(_scale,_scale,_scale)
  218. * osg::Matrix::translate(_scaleCenter));
  219. }
  220. protected:
  221. virtual ~ScaleUniformCommand();
  222. private:
  223. double _scale;
  224. osg::Vec3d _scaleCenter;
  225. };
  226. /**
  227. * Command for rotation in 3D.
  228. */
  229. class OSGMANIPULATOR_EXPORT Rotate3DCommand : public MotionCommand
  230. {
  231. public:
  232. Rotate3DCommand();
  233. virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
  234. virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
  235. virtual MotionCommand* createCommandInverse();
  236. inline void setRotation(const osg::Quat& rotation) { _rotation = rotation; }
  237. inline const osg::Quat& getRotation() const { return _rotation; }
  238. virtual osg::Matrix getMotionMatrix() const
  239. {
  240. return osg::Matrix::rotate(_rotation);
  241. }
  242. protected:
  243. virtual ~Rotate3DCommand();
  244. private:
  245. osg::Quat _rotation;
  246. };
  247. }
  248. #endif