openscenegraph
AnimationPath
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
14#ifndef OSG_ANIMATIONPATH
15#define OSG_ANIMATIONPATH 1
16
17#include <map>
18#include <istream>
19#include <float.h>
20
21#include <osg/Matrixf>
22#include <osg/Matrixd>
23#include <osg/Quat>
24#include <osg/Callback>
25
26namespace osg {
27
28/** AnimationPath encapsulates a time varying transformation pathway. Can be
29 * used for updating camera position and model object position.
30 * AnimationPathCallback can be attached directly to Transform nodes to
31 * move subgraphs around the scene.
32*/
33class OSG_EXPORT AnimationPath : public virtual osg::Object
34{
35 public:
36
37 AnimationPath():_loopMode(LOOP) {}
38
39 AnimationPath(const AnimationPath& ap, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
40 Object(ap,copyop),
41 _timeControlPointMap(ap._timeControlPointMap),
42 _loopMode(ap._loopMode) {}
43
44 META_Object(osg,AnimationPath);
45
46 class ControlPoint
47 {
48 public:
49 ControlPoint():
50 _scale(1.0,1.0,1.0) {}
51
52 ControlPoint(const osg::Vec3d& position):
53 _position(position),
54 _rotation(),
55 _scale(1.0,1.0,1.0) {}
56
57 ControlPoint(const osg::Vec3d& position, const osg::Quat& rotation):
58 _position(position),
59 _rotation(rotation),
60 _scale(1.0,1.0,1.0) {}
61
62 ControlPoint(const osg::Vec3d& position, const osg::Quat& rotation, const osg::Vec3d& scale):
63 _position(position),
64 _rotation(rotation),
65 _scale(scale) {}
66
67 void setPosition(const osg::Vec3d& position) { _position = position; }
68 const osg::Vec3d& getPosition() const { return _position; }
69
70 void setRotation(const osg::Quat& rotation) { _rotation = rotation; }
71 const osg::Quat& getRotation() const { return _rotation; }
72
73 void setScale(const osg::Vec3d& scale) { _scale = scale; }
74 const osg::Vec3d& getScale() const { return _scale; }
75
76 inline void interpolate(float ratio,const ControlPoint& first, const ControlPoint& second)
77 {
78 float one_minus_ratio = 1.0f-ratio;
79 _position = first._position*one_minus_ratio + second._position*ratio;
80 _rotation.slerp(ratio,first._rotation,second._rotation);
81 _scale = first._scale*one_minus_ratio + second._scale*ratio;
82 }
83
84 inline void interpolate(double ratio,const ControlPoint& first, const ControlPoint& second)
85 {
86 double one_minus_ratio = 1.0-ratio;
87 _position = first._position*one_minus_ratio + second._position*ratio;
88 _rotation.slerp(ratio,first._rotation,second._rotation);
89 _scale = first._scale*one_minus_ratio + second._scale*ratio;
90 }
91
92 inline void getMatrix(Matrixf& matrix) const
93 {
94 matrix.makeRotate(_rotation);
95 matrix.preMultScale(_scale);
96 matrix.postMultTranslate(_position);
97 }
98
99 inline void getMatrix(Matrixd& matrix) const
100 {
101 matrix.makeRotate(_rotation);
102 matrix.preMultScale(_scale);
103 matrix.postMultTranslate(_position);
104 }
105
106 inline void getInverse(Matrixf& matrix) const
107 {
108 matrix.makeRotate(_rotation.inverse());
109 matrix.postMultScale(osg::Vec3d(1.0/_scale.x(),1.0/_scale.y(),1.0/_scale.z()));
110 matrix.preMultTranslate(-_position);
111 }
112
113 inline void getInverse(Matrixd& matrix) const
114 {
115 matrix.makeRotate(_rotation.inverse());
116 matrix.postMultScale(osg::Vec3d(1.0/_scale.x(),1.0/_scale.y(),1.0/_scale.z()));
117 matrix.preMultTranslate(-_position);
118 }
119
120 protected:
121
122 osg::Vec3d _position;
123 osg::Quat _rotation;
124 osg::Vec3d _scale;
125
126 };
127
128
129 /** Given a specific time, return the transformation matrix for a point. */
130 bool getMatrix(double time,Matrixf& matrix) const
131 {
132 ControlPoint cp;
133 if (!getInterpolatedControlPoint(time,cp)) return false;
134 cp.getMatrix(matrix);
135 return true;
136 }
137
138 /** Given a specific time, return the transformation matrix for a point. */
139 bool getMatrix(double time,Matrixd& matrix) const
140 {
141 ControlPoint cp;
142 if (!getInterpolatedControlPoint(time,cp)) return false;
143 cp.getMatrix(matrix);
144 return true;
145 }
146
147 /** Given a specific time, return the inverse transformation matrix for a point. */
148 bool getInverse(double time,Matrixf& matrix) const
149 {
150 ControlPoint cp;
151 if (!getInterpolatedControlPoint(time,cp)) return false;
152 cp.getInverse(matrix);
153 return true;
154 }
155
156 bool getInverse(double time,Matrixd& matrix) const
157 {
158 ControlPoint cp;
159 if (!getInterpolatedControlPoint(time,cp)) return false;
160 cp.getInverse(matrix);
161 return true;
162 }
163
164 /** Given a specific time, return the local ControlPoint frame for a point. */
165 virtual bool getInterpolatedControlPoint(double time,ControlPoint& controlPoint) const;
166
167 /** Insert a control point into the AnimationPath.*/
168 void insert(double time,const ControlPoint& controlPoint);
169
170 double getFirstTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.begin()->first; else return 0.0;}
171 double getLastTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.rbegin()->first; else return 0.0;}
172 double getPeriod() const { return getLastTime()-getFirstTime();}
173
174 enum LoopMode
175 {
176 SWING,
177 LOOP,
178 NO_LOOPING
179 };
180
181 void setLoopMode(LoopMode lm) { _loopMode = lm; }
182
183 LoopMode getLoopMode() const { return _loopMode; }
184
185
186 typedef std::map<double,ControlPoint> TimeControlPointMap;
187
188 void setTimeControlPointMap(TimeControlPointMap& tcpm) { _timeControlPointMap=tcpm; }
189
190 TimeControlPointMap& getTimeControlPointMap() { return _timeControlPointMap; }
191
192 const TimeControlPointMap& getTimeControlPointMap() const { return _timeControlPointMap; }
193
194 bool empty() const { return _timeControlPointMap.empty(); }
195
196 void clear() { _timeControlPointMap.clear(); }
197
198 /** Read the animation path from a flat ASCII file stream. */
199 void read(std::istream& in);
200
201 /** Write the animation path to a flat ASCII file stream. */
202 void write(std::ostream& out) const;
203
204 /** Write the control point to a flat ASCII file stream. */
205 void write(TimeControlPointMap::const_iterator itr, std::ostream& out) const;
206
207 protected:
208
209 virtual ~AnimationPath() {}
210
211 TimeControlPointMap _timeControlPointMap;
212 LoopMode _loopMode;
213
214};
215
216
217class OSG_EXPORT AnimationPathCallback : public NodeCallback
218{
219 public:
220
221 AnimationPathCallback():
222 _pivotPoint(0.0,0.0,0.0),
223 _useInverseMatrix(false),
224 _timeOffset(0.0),
225 _timeMultiplier(1.0),
226 _firstTime(DBL_MAX),
227 _latestTime(0.0),
228 _pause(false),
229 _pauseTime(0.0) {}
230
231 AnimationPathCallback(const AnimationPathCallback& apc,const CopyOp& copyop):
232 Object(apc, copyop),
233 Callback(apc, copyop),
234 NodeCallback(apc, copyop),
235 _animationPath(apc._animationPath),
236 _pivotPoint(apc._pivotPoint),
237 _useInverseMatrix(apc._useInverseMatrix),
238 _timeOffset(apc._timeOffset),
239 _timeMultiplier(apc._timeMultiplier),
240 _firstTime(apc._firstTime),
241 _latestTime(apc._latestTime),
242 _pause(apc._pause),
243 _pauseTime(apc._pauseTime) {}
244
245
246 META_Object(osg,AnimationPathCallback);
247
248 /** Construct an AnimationPathCallback with a specified animation path.*/
249 AnimationPathCallback(AnimationPath* ap,double timeOffset=0.0,double timeMultiplier=1.0):
250 _animationPath(ap),
251 _pivotPoint(0.0,0.0,0.0),
252 _useInverseMatrix(false),
253 _timeOffset(timeOffset),
254 _timeMultiplier(timeMultiplier),
255 _firstTime(DBL_MAX),
256 _latestTime(0.0),
257 _pause(false),
258 _pauseTime(0.0) {}
259
260 /** Construct an AnimationPathCallback and automatically create an animation path to produce a rotation about a point.*/
261 AnimationPathCallback(const osg::Vec3d& pivot,const osg::Vec3d& axis,float angularVelocity);
262
263
264 void setAnimationPath(AnimationPath* path) { _animationPath = path; }
265 AnimationPath* getAnimationPath() { return _animationPath.get(); }
266 const AnimationPath* getAnimationPath() const { return _animationPath.get(); }
267
268 inline void setPivotPoint(const Vec3d& pivot) { _pivotPoint = pivot; }
269 inline const Vec3d& getPivotPoint() const { return _pivotPoint; }
270
271 void setUseInverseMatrix(bool useInverseMatrix) { _useInverseMatrix = useInverseMatrix; }
272 bool getUseInverseMatrix() const { return _useInverseMatrix; }
273
274 void setTimeOffset(double offset) { _timeOffset = offset; }
275 double getTimeOffset() const { return _timeOffset; }
276
277 void setTimeMultiplier(double multiplier) { _timeMultiplier = multiplier; }
278 double getTimeMultiplier() const { return _timeMultiplier; }
279
280
281 virtual void reset();
282
283 void setPause(bool pause);
284 bool getPause() const { return _pause; }
285
286 /** Get the animation time that is used to specify the position along
287 * the AnimationPath. Animation time is computed from the formula:
288 * ((_latestTime-_firstTime)-_timeOffset)*_timeMultiplier.*/
289 virtual double getAnimationTime() const;
290
291 /** Implements the callback. */
292 virtual void operator()(Node* node, NodeVisitor* nv);
293
294 void update(osg::Node& node);
295
296 public:
297
298 ref_ptr<AnimationPath> _animationPath;
299 osg::Vec3d _pivotPoint;
300 bool _useInverseMatrix;
301 double _timeOffset;
302 double _timeMultiplier;
303 double _firstTime;
304 double _latestTime;
305 bool _pause;
306 double _pauseTime;
307
308 protected:
309
310 ~AnimationPathCallback(){}
311
312};
313
314}
315
316#endif