openscenegraph
Constraint
Go to the documentation of this file.
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
15#ifndef OSGMANIPULATOR_CONSTRAINT
16#define OSGMANIPULATOR_CONSTRAINT 1
17
18#include <osgManipulator/Export>
19
20#include <osg/observer_ptr>
21#include <osg/Node>
22#include <osg/Matrix>
23
24namespace osgManipulator {
25
26class MotionCommand;
27class TranslateInLineCommand;
28class TranslateInPlaneCommand;
29class Scale1DCommand;
30class Scale2DCommand;
31class ScaleUniformCommand;
32class Rotate3DCommand;
33
34
35class DraggerCallback : virtual public osg::Object
36{
37 public:
38 DraggerCallback():
39 osg::Object(true) {}
40
41 DraggerCallback(const DraggerCallback& org, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
42 osg::Object(org, copyop) {}
43
44 META_Object(osgManipulator, DraggerCallback);
45
46 /**
47 * Receive motion commands. Returns true on success.
48 */
49 virtual bool receive(const MotionCommand&) { return false; }
50 virtual bool receive(const TranslateInLineCommand& command) { return receive((MotionCommand&)command); }
51 virtual bool receive(const TranslateInPlaneCommand& command) { return receive((MotionCommand&)command); }
52 virtual bool receive(const Scale1DCommand& command) { return receive((MotionCommand&)command); }
53 virtual bool receive(const Scale2DCommand& command) { return receive((MotionCommand&)command); }
54 virtual bool receive(const ScaleUniformCommand& command) { return receive((MotionCommand&)command); }
55 virtual bool receive(const Rotate3DCommand& command) { return receive((MotionCommand&)command); }
56};
57
58
59class OSGMANIPULATOR_EXPORT Constraint : public osg::Referenced
60{
61 public:
62
63 virtual bool constrain(MotionCommand&) const { return false; }
64 virtual bool constrain(TranslateInLineCommand& command) const { return constrain((MotionCommand&)command); }
65 virtual bool constrain(TranslateInPlaneCommand& command) const { return constrain((MotionCommand&)command); }
66 virtual bool constrain(Scale1DCommand& command) const { return constrain((MotionCommand&)command); }
67 virtual bool constrain(Scale2DCommand& command) const { return constrain((MotionCommand&)command); }
68 virtual bool constrain(ScaleUniformCommand& command) const { return constrain((MotionCommand&)command); }
69 virtual bool constrain(Rotate3DCommand& command) const { return constrain((MotionCommand&)command); }
70
71protected:
72
73 Constraint() {}
74 Constraint(osg::Node& refNode) : _refNode(&refNode) {}
75 virtual ~Constraint() {}
76
77 osg::Node& getReferenceNode() { return *_refNode; }
78 const osg::Node& getReferenceNode() const { return *_refNode; }
79
80 const osg::Matrix& getLocalToWorld() const { return _localToWorld; }
81 const osg::Matrix& getWorldToLocal() const { return _worldToLocal; }
82
83 void computeLocalToWorldAndWorldToLocal() const;
84
85 private:
86
87 osg::observer_ptr<osg::Node> _refNode;
88 mutable osg::Matrix _localToWorld;
89 mutable osg::Matrix _worldToLocal;
90};
91
92/**
93 * Constraint to snap motion commands to a sugar cube grid.
94 */
95class OSGMANIPULATOR_EXPORT GridConstraint : public Constraint
96{
97 public:
98
99 GridConstraint(osg::Node& refNode, const osg::Vec3d& origin, const osg::Vec3d& spacing);
100
101 void setOrigin(const osg::Vec3d& origin) { _origin = origin; }
102 const osg::Vec3d& getOrigin() const { return _origin; }
103
104 void setSpacing(const osg::Vec3d& spacing) { _spacing = spacing; }
105 const osg::Vec3d& getSpacing() const { return _spacing; }
106
107 virtual bool constrain(TranslateInLineCommand& command) const;
108 virtual bool constrain(TranslateInPlaneCommand& command) const;
109 virtual bool constrain(Scale1DCommand& command) const;
110 virtual bool constrain(Scale2DCommand& command) const;
111 virtual bool constrain(ScaleUniformCommand& command) const;
112
113 protected:
114
115 virtual ~GridConstraint() {}
116
117 private:
118
119 osg::Vec3d _origin;
120 osg::Vec3d _spacing;
121 mutable osg::Matrix _startMatrix;
122 mutable osg::Matrix _matrix;
123};
124
125}
126
127#endif