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
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
45
47 {
48 public:
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
125
126 };
127
128
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
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
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
165 virtual bool getInterpolatedControlPoint(double time,ControlPoint& controlPoint) const;
166
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
175 {
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
199 void read(std::istream& in);
200
202 void write(std::ostream& out) const;
203
205 void write(TimeControlPointMap::const_iterator itr, std::ostream& out) const;
206
207 protected:
208
209 virtual ~AnimationPath() {}
210
213
214};
215
216
218{
219 public:
220
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
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
247
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
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
289 virtual double getAnimationTime() const;
290
292 virtual void operator()(Node* node, NodeVisitor* nv);
293
294 void update(osg::Node& node);
295
296 public:
297
305 bool _pause;
307
308 protected:
309
311
312};
313
314}
315
316#endif
Definition AnimationPath:218
bool getUseInverseMatrix() const
Definition AnimationPath:272
void setPause(bool pause)
double _pauseTime
Definition AnimationPath:306
AnimationPath * getAnimationPath()
Definition AnimationPath:265
AnimationPathCallback()
Definition AnimationPath:221
double _latestTime
Definition AnimationPath:304
const AnimationPath * getAnimationPath() const
Definition AnimationPath:266
virtual double getAnimationTime() const
double _timeOffset
Definition AnimationPath:301
ref_ptr< AnimationPath > _animationPath
Definition AnimationPath:298
osg::Vec3d _pivotPoint
Definition AnimationPath:299
void setTimeOffset(double offset)
Definition AnimationPath:274
void setPivotPoint(const Vec3d &pivot)
Definition AnimationPath:268
AnimationPathCallback(const osg::Vec3d &pivot, const osg::Vec3d &axis, float angularVelocity)
bool getPause() const
Definition AnimationPath:284
bool _useInverseMatrix
Definition AnimationPath:300
void setAnimationPath(AnimationPath *path)
Definition AnimationPath:264
bool _pause
Definition AnimationPath:305
double getTimeMultiplier() const
Definition AnimationPath:278
AnimationPathCallback(AnimationPath *ap, double timeOffset=0.0, double timeMultiplier=1.0)
Definition AnimationPath:249
~AnimationPathCallback()
Definition AnimationPath:310
virtual void operator()(Node *node, NodeVisitor *nv)
double getTimeOffset() const
Definition AnimationPath:275
void update(osg::Node &node)
void setUseInverseMatrix(bool useInverseMatrix)
Definition AnimationPath:271
META_Object(osg, AnimationPathCallback)
double _firstTime
Definition AnimationPath:303
AnimationPathCallback(const AnimationPathCallback &apc, const CopyOp &copyop)
Definition AnimationPath:231
const Vec3d & getPivotPoint() const
Definition AnimationPath:269
double _timeMultiplier
Definition AnimationPath:302
void setTimeMultiplier(double multiplier)
Definition AnimationPath:277
Definition AnimationPath:47
osg::Quat _rotation
Definition AnimationPath:123
osg::Vec3d _position
Definition AnimationPath:122
void interpolate(double ratio, const ControlPoint &first, const ControlPoint &second)
Definition AnimationPath:84
const osg::Vec3d & getScale() const
Definition AnimationPath:74
const osg::Vec3d & getPosition() const
Definition AnimationPath:68
ControlPoint(const osg::Vec3d &position, const osg::Quat &rotation)
Definition AnimationPath:57
osg::Vec3d _scale
Definition AnimationPath:124
void interpolate(float ratio, const ControlPoint &first, const ControlPoint &second)
Definition AnimationPath:76
void getInverse(Matrixf &matrix) const
Definition AnimationPath:106
void setRotation(const osg::Quat &rotation)
Definition AnimationPath:70
void getMatrix(Matrixf &matrix) const
Definition AnimationPath:92
void getInverse(Matrixd &matrix) const
Definition AnimationPath:113
void setScale(const osg::Vec3d &scale)
Definition AnimationPath:73
ControlPoint(const osg::Vec3d &position, const osg::Quat &rotation, const osg::Vec3d &scale)
Definition AnimationPath:62
ControlPoint()
Definition AnimationPath:49
const osg::Quat & getRotation() const
Definition AnimationPath:71
ControlPoint(const osg::Vec3d &position)
Definition AnimationPath:52
void getMatrix(Matrixd &matrix) const
Definition AnimationPath:99
void setPosition(const osg::Vec3d &position)
Definition AnimationPath:67
Definition AnimationPath:34
bool empty() const
Definition AnimationPath:194
virtual bool getInterpolatedControlPoint(double time, ControlPoint &controlPoint) const
META_Object(osg, AnimationPath)
TimeControlPointMap _timeControlPointMap
Definition AnimationPath:211
void write(std::ostream &out) const
std::map< double, ControlPoint > TimeControlPointMap
Definition AnimationPath:186
bool getMatrix(double time, Matrixf &matrix) const
Definition AnimationPath:130
bool getInverse(double time, Matrixf &matrix) const
Definition AnimationPath:148
AnimationPath()
Definition AnimationPath:37
void setTimeControlPointMap(TimeControlPointMap &tcpm)
Definition AnimationPath:188
void clear()
Definition AnimationPath:196
const TimeControlPointMap & getTimeControlPointMap() const
Definition AnimationPath:192
void insert(double time, const ControlPoint &controlPoint)
bool getInverse(double time, Matrixd &matrix) const
Definition AnimationPath:156
void setLoopMode(LoopMode lm)
Definition AnimationPath:181
bool getMatrix(double time, Matrixd &matrix) const
Definition AnimationPath:139
void read(std::istream &in)
LoopMode getLoopMode() const
Definition AnimationPath:183
LoopMode _loopMode
Definition AnimationPath:212
LoopMode
Definition AnimationPath:175
@ SWING
Definition AnimationPath:176
@ LOOP
Definition AnimationPath:177
TimeControlPointMap & getTimeControlPointMap()
Definition AnimationPath:190
void write(TimeControlPointMap::const_iterator itr, std::ostream &out) const
virtual ~AnimationPath()
Definition AnimationPath:209
AnimationPath(const AnimationPath &ap, const CopyOp &copyop=CopyOp::SHALLOW_COPY)
Definition AnimationPath:39
double getPeriod() const
Definition AnimationPath:172
double getFirstTime() const
Definition AnimationPath:170
double getLastTime() const
Definition AnimationPath:171
Definition Callback:34
Definition CopyOp:41
Definition Matrixd:27
void preMultScale(const Vec3d &v)
Definition Matrixd:747
void postMultScale(const Vec3d &v)
Definition Matrixd:761
void preMultTranslate(const Vec3d &v)
Definition Matrixd:691
void postMultTranslate(const Vec3d &v)
Definition Matrixd:719
void makeRotate(const Vec3f &from, const Vec3f &to)
Definition Matrixf:27
void postMultTranslate(const Vec3d &v)
Definition Matrixf:823
void preMultScale(const Vec3d &v)
Definition Matrixf:851
void preMultTranslate(const Vec3d &v)
Definition Matrixf:795
void makeRotate(const Vec3f &from, const Vec3f &to)
void postMultScale(const Vec3d &v)
Definition Matrixf:865
Definition Callback:215
Definition NodeVisitor:82
Definition Node:72
Definition Object:61
Definition Quat:30
Definition Vec3d:30
Definition ref_ptr:32
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
#define OSG_EXPORT
Definition osg/Export:39