1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
13//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
15#ifndef OSGMANIPULATOR_COMMAND
16#define OSGMANIPULATOR_COMMAND 1
18#include <osgManipulator/Constraint>
20#include <osg/LineSegment>
26namespace osgManipulator {
29/** Base class for motion commands that are generated by draggers. */
30class OSGMANIPULATOR_EXPORT MotionCommand : public osg::Referenced
35 * Motion command are based on click-drag-release actions. So each
36 * command needs to indicate which stage of the motion the command
42 /** Click or pick start. */
44 /** Drag or pick move. */
46 /** Release or pick finish. */
52 /** create a MotionCommand that is the inverse of this command, and if applied will undo this commands changes. */
53 virtual MotionCommand* createCommandInverse() = 0;
56 * Gets the matrix for transforming the object being dragged. This matrix is in the
57 * command's coordinate systems.
59 virtual osg::Matrix getMotionMatrix() const = 0;
61 virtual void accept(const Constraint& constraint) { constraint.constrain(*this); }
62 virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
65 * Sets the matrix for transforming the command's local coordinate
66 * system to the world/object coordinate system.
68 void setLocalToWorldAndWorldToLocal(const osg::Matrix& localToWorld, const osg::Matrix& worldToLocal)
70 _localToWorld = localToWorld;
71 _worldToLocal = worldToLocal;
75 * Gets the matrix for transforming the command's local coordinate
76 * system to the world/object coordinate system.
78 inline const osg::Matrix& getLocalToWorld() const { return _localToWorld; }
81 * Gets the matrix for transforming the command's world/object
82 * coordinate system to the command's local coordinate system.
84 inline const osg::Matrix& getWorldToLocal() const { return _worldToLocal; }
86 void setStage(const Stage s) { _stage = s; }
87 Stage getStage() const { return _stage; }
91 virtual ~MotionCommand();
94 osg::Matrix _localToWorld;
95 osg::Matrix _worldToLocal;
102 * Command for translating in a line.
104class OSGMANIPULATOR_EXPORT TranslateInLineCommand : public MotionCommand
108 TranslateInLineCommand();
110 TranslateInLineCommand(const osg::LineSegment::vec_type& s, const osg::LineSegment::vec_type& e);
112 virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
113 virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
115 virtual MotionCommand* createCommandInverse();
117 inline void setLine(const osg::LineSegment::vec_type& s, const osg::LineSegment::vec_type& e) { _line->start() = s; _line->end() = e; }
118 inline const osg::LineSegment::vec_type& getLineStart() const { return _line->start(); }
119 inline const osg::LineSegment::vec_type& getLineEnd() const { return _line->end(); }
121 inline void setTranslation(const osg::Vec3d& t) { _translation = t; }
122 inline const osg::Vec3d& getTranslation() const { return _translation; }
124 virtual osg::Matrix getMotionMatrix() const
126 return osg::Matrix::translate(_translation);
131 virtual ~TranslateInLineCommand();
134 osg::ref_ptr<osg::LineSegment> _line;
135 osg::Vec3d _translation;
139 * Command for translating in a plane.
141class OSGMANIPULATOR_EXPORT TranslateInPlaneCommand : public MotionCommand
145 TranslateInPlaneCommand();
147 TranslateInPlaneCommand(const osg::Plane& plane);
149 virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
150 virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
152 virtual MotionCommand* createCommandInverse();
154 inline void setPlane(const osg::Plane& plane) { _plane = plane; }
155 inline const osg::Plane& getPlane() const { return _plane; }
157 inline void setTranslation(const osg::Vec3d& t) { _translation = t; }
158 inline const osg::Vec3d& getTranslation() const { return _translation; }
160 /** ReferencePoint is used only for snapping. */
161 inline void setReferencePoint(const osg::Vec3d& rp) { _referencePoint = rp; }
162 inline const osg::Vec3d& getReferencePoint() const { return _referencePoint; }
164 virtual osg::Matrix getMotionMatrix() const
166 return osg::Matrix::translate(_translation);
171 virtual ~TranslateInPlaneCommand();
175 osg::Vec3d _translation;
176 osg::Vec3d _referencePoint;
180 * Command for 1D scaling.
182class OSGMANIPULATOR_EXPORT Scale1DCommand : public MotionCommand
188 virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
189 virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
191 virtual MotionCommand* createCommandInverse();
193 inline void setScale(double s) { _scale = s; }
194 inline double getScale() const { return _scale; }
196 inline void setScaleCenter(double center) { _scaleCenter = center; }
197 inline double getScaleCenter() const { return _scaleCenter; }
199 /** ReferencePoint is used only for snapping. */
200 inline void setReferencePoint(double rp) { _referencePoint = rp; }
201 inline double getReferencePoint() const { return _referencePoint; }
203 inline void setMinScale(double min) { _minScale = min; }
204 inline double getMinScale() const { return _minScale; }
206 virtual osg::Matrix getMotionMatrix() const
208 return (osg::Matrix::translate(-_scaleCenter,0.0,0.0)
209 * osg::Matrix::scale(_scale,1.0,1.0)
210 * osg::Matrix::translate(_scaleCenter,0.0,0.0));
215 virtual ~Scale1DCommand();
220 double _referencePoint;
225 * Command for 2D scaling.
227class OSGMANIPULATOR_EXPORT Scale2DCommand : public MotionCommand
233 virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
234 virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
236 virtual MotionCommand* createCommandInverse();
238 inline void setScale(const osg::Vec2d& s) { _scale = s; }
239 inline const osg::Vec2d& getScale() const { return _scale; }
241 inline void setScaleCenter(const osg::Vec2d& center) { _scaleCenter = center; }
242 inline const osg::Vec2d& getScaleCenter() const { return _scaleCenter; }
244 /** ReferencePoint is used only for snapping. */
245 inline void setReferencePoint(const osg::Vec2d& rp) { _referencePoint = rp; }
246 inline const osg::Vec2d& getReferencePoint() const { return _referencePoint; }
248 inline void setMinScale(const osg::Vec2d& min) { _minScale = min; }
249 inline const osg::Vec2d& getMinScale() const { return _minScale; }
251 virtual osg::Matrix getMotionMatrix() const
253 return (osg::Matrix::translate(-_scaleCenter[0],0.0,-_scaleCenter[1])
254 * osg::Matrix::scale(_scale[0],1.0,_scale[1])
255 * osg::Matrix::translate(_scaleCenter[0],0.0,_scaleCenter[1]));
260 virtual ~Scale2DCommand();
264 osg::Vec2d _scaleCenter;
265 osg::Vec2d _referencePoint;
266 osg::Vec2d _minScale;
270 * Command for uniform 3D scaling.
272class OSGMANIPULATOR_EXPORT ScaleUniformCommand : public MotionCommand
276 ScaleUniformCommand();
278 virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
279 virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
281 virtual MotionCommand* createCommandInverse();
283 inline void setScale(double s) { _scale = s; }
284 inline double getScale() const { return _scale; }
286 inline void setScaleCenter(const osg::Vec3d& center) { _scaleCenter = center; }
287 inline const osg::Vec3d& getScaleCenter() const { return _scaleCenter; }
289 virtual osg::Matrix getMotionMatrix() const
291 return (osg::Matrix::translate(-_scaleCenter)
292 * osg::Matrix::scale(_scale,_scale,_scale)
293 * osg::Matrix::translate(_scaleCenter));
298 virtual ~ScaleUniformCommand();
302 osg::Vec3d _scaleCenter;
306 * Command for rotation in 3D.
308class OSGMANIPULATOR_EXPORT Rotate3DCommand : public MotionCommand
314 virtual void accept(const Constraint& constraint) {constraint.constrain(*this); }
315 virtual void accept(DraggerCallback& callback) { callback.receive(*this); }
317 virtual MotionCommand* createCommandInverse();
319 inline void setRotation(const osg::Quat& rotation) { _rotation = rotation; }
320 inline const osg::Quat& getRotation() const { return _rotation; }
322 virtual osg::Matrix getMotionMatrix() const
324 return osg::Matrix::rotate(_rotation);
329 virtual ~Rotate3DCommand();