AnimationPath
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_ANIMATIONPATH
#define OSG_ANIMATIONPATH 1
#include <map>
#include <istream>
#include <float.h>
#include <osg/Matrixf>
#include <osg/Matrixd>
#include <osg/Quat>
#include <osg/Callback>
namespace osg {
class OSG_EXPORT AnimationPath : public virtual osg::Object
{
public:
AnimationPath():_loopMode(LOOP) {}
AnimationPath(const AnimationPath& ap, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Object(ap,copyop),
_timeControlPointMap(ap._timeControlPointMap),
_loopMode(ap._loopMode) {}
META_Object(osg,AnimationPath);
class ControlPoint
{
public:
ControlPoint():
_scale(1.0,1.0,1.0) {}
ControlPoint(const osg::Vec3d& position):
_position(position),
_rotation(),
_scale(1.0,1.0,1.0) {}
ControlPoint(const osg::Vec3d& position, const osg::Quat& rotation):
_position(position),
_rotation(rotation),
_scale(1.0,1.0,1.0) {}
ControlPoint(const osg::Vec3d& position, const osg::Quat& rotation, const osg::Vec3d& scale):
_position(position),
_rotation(rotation),
_scale(scale) {}
void setPosition(const osg::Vec3d& position) { _position = position; }
const osg::Vec3d& getPosition() const { return _position; }
void setRotation(const osg::Quat& rotation) { _rotation = rotation; }
const osg::Quat& getRotation() const { return _rotation; }
void setScale(const osg::Vec3d& scale) { _scale = scale; }
const osg::Vec3d& getScale() const { return _scale; }
inline void interpolate(float ratio,const ControlPoint& first, const ControlPoint& second)
{
float one_minus_ratio = 1.0f-ratio;
_position = first._position*one_minus_ratio + second._position*ratio;
_rotation.slerp(ratio,first._rotation,second._rotation);
_scale = first._scale*one_minus_ratio + second._scale*ratio;
}
inline void interpolate(double ratio,const ControlPoint& first, const ControlPoint& second)
{
double one_minus_ratio = 1.0-ratio;
_position = first._position*one_minus_ratio + second._position*ratio;
_rotation.slerp(ratio,first._rotation,second._rotation);
_scale = first._scale*one_minus_ratio + second._scale*ratio;
}
inline void getMatrix(Matrixf& matrix) const
{
matrix.makeRotate(_rotation);
matrix.preMultScale(_scale);
matrix.postMultTranslate(_position);
}
inline void getMatrix(Matrixd& matrix) const
{
matrix.makeRotate(_rotation);
matrix.preMultScale(_scale);
matrix.postMultTranslate(_position);
}
inline void getInverse(Matrixf& matrix) const
{
matrix.makeRotate(_rotation.inverse());
matrix.postMultScale(osg::Vec3d(1.0/_scale.x(),1.0/_scale.y(),1.0/_scale.z()));
matrix.preMultTranslate(-_position);
}
inline void getInverse(Matrixd& matrix) const
{
matrix.makeRotate(_rotation.inverse());
matrix.postMultScale(osg::Vec3d(1.0/_scale.x(),1.0/_scale.y(),1.0/_scale.z()));
matrix.preMultTranslate(-_position);
}
protected:
osg::Vec3d _position;
osg::Quat _rotation;
osg::Vec3d _scale;
};
bool getMatrix(double time,Matrixf& matrix) const
{
ControlPoint cp;
if (!getInterpolatedControlPoint(time,cp)) return false;
cp.getMatrix(matrix);
return true;
}
bool getMatrix(double time,Matrixd& matrix) const
{
ControlPoint cp;
if (!getInterpolatedControlPoint(time,cp)) return false;
cp.getMatrix(matrix);
return true;
}
bool getInverse(double time,Matrixf& matrix) const
{
ControlPoint cp;
if (!getInterpolatedControlPoint(time,cp)) return false;
cp.getInverse(matrix);
return true;
}
bool getInverse(double time,Matrixd& matrix) const
{
ControlPoint cp;
if (!getInterpolatedControlPoint(time,cp)) return false;
cp.getInverse(matrix);
return true;
}
virtual bool getInterpolatedControlPoint(double time,ControlPoint& controlPoint) const;
void insert(double time,const ControlPoint& controlPoint);
double getFirstTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.begin()->first; else return 0.0;}
double getLastTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.rbegin()->first; else return 0.0;}
double getPeriod() const { return getLastTime()-getFirstTime();}
enum LoopMode
{
SWING,
LOOP,
NO_LOOPING
};
void setLoopMode(LoopMode lm) { _loopMode = lm; }
LoopMode getLoopMode() const { return _loopMode; }
typedef std::map<double,ControlPoint> TimeControlPointMap;
void setTimeControlPointMap(TimeControlPointMap& tcpm) { _timeControlPointMap=tcpm; }
TimeControlPointMap& getTimeControlPointMap() { return _timeControlPointMap; }
const TimeControlPointMap& getTimeControlPointMap() const { return _timeControlPointMap; }
bool empty() const { return _timeControlPointMap.empty(); }
void clear() { _timeControlPointMap.clear(); }
void read(std::istream& in);
void write(std::ostream& out) const;
void write(TimeControlPointMap::const_iterator itr, std::ostream& out) const;
protected:
virtual ~AnimationPath() {}
TimeControlPointMap _timeControlPointMap;
LoopMode _loopMode;
};
class OSG_EXPORT AnimationPathCallback : public NodeCallback
{
public:
AnimationPathCallback():
_pivotPoint(0.0,0.0,0.0),
_useInverseMatrix(false),
_timeOffset(0.0),
_timeMultiplier(1.0),
_firstTime(DBL_MAX),
_latestTime(0.0),
_pause(false),
_pauseTime(0.0) {}
AnimationPathCallback(const AnimationPathCallback& apc,const CopyOp& copyop):
Object(apc, copyop),
Callback(apc, copyop),
NodeCallback(apc, copyop),
_animationPath(apc._animationPath),
_pivotPoint(apc._pivotPoint),
_useInverseMatrix(apc._useInverseMatrix),
_timeOffset(apc._timeOffset),
_timeMultiplier(apc._timeMultiplier),
_firstTime(apc._firstTime),
_latestTime(apc._latestTime),
_pause(apc._pause),
_pauseTime(apc._pauseTime) {}
META_Object(osg,AnimationPathCallback);
AnimationPathCallback(AnimationPath* ap,double timeOffset=0.0,double timeMultiplier=1.0):
_animationPath(ap),
_pivotPoint(0.0,0.0,0.0),
_useInverseMatrix(false),
_timeOffset(timeOffset),
_timeMultiplier(timeMultiplier),
_firstTime(DBL_MAX),
_latestTime(0.0),
_pause(false),
_pauseTime(0.0) {}
AnimationPathCallback(const osg::Vec3d& pivot,const osg::Vec3d& axis,float angularVelocity);
void setAnimationPath(AnimationPath* path) { _animationPath = path; }
AnimationPath* getAnimationPath() { return _animationPath.get(); }
const AnimationPath* getAnimationPath() const { return _animationPath.get(); }
inline void setPivotPoint(const Vec3d& pivot) { _pivotPoint = pivot; }
inline const Vec3d& getPivotPoint() const { return _pivotPoint; }
void setUseInverseMatrix(bool useInverseMatrix) { _useInverseMatrix = useInverseMatrix; }
bool getUseInverseMatrix() const { return _useInverseMatrix; }
void setTimeOffset(double offset) { _timeOffset = offset; }
double getTimeOffset() const { return _timeOffset; }
void setTimeMultiplier(double multiplier) { _timeMultiplier = multiplier; }
double getTimeMultiplier() const { return _timeMultiplier; }
virtual void reset();
void setPause(bool pause);
bool getPause() const { return _pause; }
virtual double getAnimationTime() const;
virtual void operator()(Node* node, NodeVisitor* nv);
void update(osg::Node& node);
public:
ref_ptr<AnimationPath> _animationPath;
osg::Vec3d _pivotPoint;
bool _useInverseMatrix;
double _timeOffset;
double _timeMultiplier;
double _firstTime;
double _latestTime;
bool _pause;
double _pauseTime;
protected:
~AnimationPathCallback(){}
};
}
#endif
Callback
Matrixd
Matrixf
Quat
osg::AnimationPathCallback::getUseInverseMatrix
bool getUseInverseMatrix() const
Definition AnimationPath:272
osg::AnimationPathCallback::setPause
void setPause(bool pause)
osg::AnimationPathCallback::_pauseTime
double _pauseTime
Definition AnimationPath:306
osg::AnimationPathCallback::getAnimationPath
AnimationPath * getAnimationPath()
Definition AnimationPath:265
osg::AnimationPathCallback::AnimationPathCallback
AnimationPathCallback()
Definition AnimationPath:221
osg::AnimationPathCallback::_latestTime
double _latestTime
Definition AnimationPath:304
osg::AnimationPathCallback::reset
virtual void reset()
osg::AnimationPathCallback::getAnimationPath
const AnimationPath * getAnimationPath() const
Definition AnimationPath:266
osg::AnimationPathCallback::getAnimationTime
virtual double getAnimationTime() const
osg::AnimationPathCallback::_timeOffset
double _timeOffset
Definition AnimationPath:301
osg::AnimationPathCallback::_animationPath
ref_ptr< AnimationPath > _animationPath
Definition AnimationPath:298
osg::AnimationPathCallback::_pivotPoint
osg::Vec3d _pivotPoint
Definition AnimationPath:299
osg::AnimationPathCallback::setTimeOffset
void setTimeOffset(double offset)
Definition AnimationPath:274
osg::AnimationPathCallback::setPivotPoint
void setPivotPoint(const Vec3d &pivot)
Definition AnimationPath:268
osg::AnimationPathCallback::AnimationPathCallback
AnimationPathCallback(const osg::Vec3d &pivot, const osg::Vec3d &axis, float angularVelocity)
osg::AnimationPathCallback::getPause
bool getPause() const
Definition AnimationPath:284
osg::AnimationPathCallback::_useInverseMatrix
bool _useInverseMatrix
Definition AnimationPath:300
osg::AnimationPathCallback::setAnimationPath
void setAnimationPath(AnimationPath *path)
Definition AnimationPath:264
osg::AnimationPathCallback::_pause
bool _pause
Definition AnimationPath:305
osg::AnimationPathCallback::getTimeMultiplier
double getTimeMultiplier() const
Definition AnimationPath:278
osg::AnimationPathCallback::AnimationPathCallback
AnimationPathCallback(AnimationPath *ap, double timeOffset=0.0, double timeMultiplier=1.0)
Definition AnimationPath:249
osg::AnimationPathCallback::~AnimationPathCallback
~AnimationPathCallback()
Definition AnimationPath:310
osg::AnimationPathCallback::operator()
virtual void operator()(Node *node, NodeVisitor *nv)
osg::AnimationPathCallback::getTimeOffset
double getTimeOffset() const
Definition AnimationPath:275
osg::AnimationPathCallback::update
void update(osg::Node &node)
osg::AnimationPathCallback::setUseInverseMatrix
void setUseInverseMatrix(bool useInverseMatrix)
Definition AnimationPath:271
osg::AnimationPathCallback::META_Object
META_Object(osg, AnimationPathCallback)
osg::AnimationPathCallback::_firstTime
double _firstTime
Definition AnimationPath:303
osg::AnimationPathCallback::AnimationPathCallback
AnimationPathCallback(const AnimationPathCallback &apc, const CopyOp &copyop)
Definition AnimationPath:231
osg::AnimationPathCallback::getPivotPoint
const Vec3d & getPivotPoint() const
Definition AnimationPath:269
osg::AnimationPathCallback::_timeMultiplier
double _timeMultiplier
Definition AnimationPath:302
osg::AnimationPathCallback::setTimeMultiplier
void setTimeMultiplier(double multiplier)
Definition AnimationPath:277
osg::AnimationPath::ControlPoint
Definition AnimationPath:47
osg::AnimationPath::ControlPoint::_rotation
osg::Quat _rotation
Definition AnimationPath:123
osg::AnimationPath::ControlPoint::_position
osg::Vec3d _position
Definition AnimationPath:122
osg::AnimationPath::ControlPoint::interpolate
void interpolate(double ratio, const ControlPoint &first, const ControlPoint &second)
Definition AnimationPath:84
osg::AnimationPath::ControlPoint::getScale
const osg::Vec3d & getScale() const
Definition AnimationPath:74
osg::AnimationPath::ControlPoint::getPosition
const osg::Vec3d & getPosition() const
Definition AnimationPath:68
osg::AnimationPath::ControlPoint::ControlPoint
ControlPoint(const osg::Vec3d &position, const osg::Quat &rotation)
Definition AnimationPath:57
osg::AnimationPath::ControlPoint::_scale
osg::Vec3d _scale
Definition AnimationPath:124
osg::AnimationPath::ControlPoint::interpolate
void interpolate(float ratio, const ControlPoint &first, const ControlPoint &second)
Definition AnimationPath:76
osg::AnimationPath::ControlPoint::getInverse
void getInverse(Matrixf &matrix) const
Definition AnimationPath:106
osg::AnimationPath::ControlPoint::setRotation
void setRotation(const osg::Quat &rotation)
Definition AnimationPath:70
osg::AnimationPath::ControlPoint::getMatrix
void getMatrix(Matrixf &matrix) const
Definition AnimationPath:92
osg::AnimationPath::ControlPoint::getInverse
void getInverse(Matrixd &matrix) const
Definition AnimationPath:113
osg::AnimationPath::ControlPoint::setScale
void setScale(const osg::Vec3d &scale)
Definition AnimationPath:73
osg::AnimationPath::ControlPoint::ControlPoint
ControlPoint(const osg::Vec3d &position, const osg::Quat &rotation, const osg::Vec3d &scale)
Definition AnimationPath:62
osg::AnimationPath::ControlPoint::ControlPoint
ControlPoint()
Definition AnimationPath:49
osg::AnimationPath::ControlPoint::getRotation
const osg::Quat & getRotation() const
Definition AnimationPath:71
osg::AnimationPath::ControlPoint::ControlPoint
ControlPoint(const osg::Vec3d &position)
Definition AnimationPath:52
osg::AnimationPath::ControlPoint::getMatrix
void getMatrix(Matrixd &matrix) const
Definition AnimationPath:99
osg::AnimationPath::ControlPoint::setPosition
void setPosition(const osg::Vec3d &position)
Definition AnimationPath:67
osg::AnimationPath
Definition AnimationPath:34
osg::AnimationPath::empty
bool empty() const
Definition AnimationPath:194
osg::AnimationPath::getInterpolatedControlPoint
virtual bool getInterpolatedControlPoint(double time, ControlPoint &controlPoint) const
osg::AnimationPath::META_Object
META_Object(osg, AnimationPath)
osg::AnimationPath::_timeControlPointMap
TimeControlPointMap _timeControlPointMap
Definition AnimationPath:211
osg::AnimationPath::write
void write(std::ostream &out) const
osg::AnimationPath::TimeControlPointMap
std::map< double, ControlPoint > TimeControlPointMap
Definition AnimationPath:186
osg::AnimationPath::getMatrix
bool getMatrix(double time, Matrixf &matrix) const
Definition AnimationPath:130
osg::AnimationPath::getInverse
bool getInverse(double time, Matrixf &matrix) const
Definition AnimationPath:148
osg::AnimationPath::AnimationPath
AnimationPath()
Definition AnimationPath:37
osg::AnimationPath::setTimeControlPointMap
void setTimeControlPointMap(TimeControlPointMap &tcpm)
Definition AnimationPath:188
osg::AnimationPath::clear
void clear()
Definition AnimationPath:196
osg::AnimationPath::getTimeControlPointMap
const TimeControlPointMap & getTimeControlPointMap() const
Definition AnimationPath:192
osg::AnimationPath::insert
void insert(double time, const ControlPoint &controlPoint)
osg::AnimationPath::getInverse
bool getInverse(double time, Matrixd &matrix) const
Definition AnimationPath:156
osg::AnimationPath::setLoopMode
void setLoopMode(LoopMode lm)
Definition AnimationPath:181
osg::AnimationPath::getMatrix
bool getMatrix(double time, Matrixd &matrix) const
Definition AnimationPath:139
osg::AnimationPath::read
void read(std::istream &in)
osg::AnimationPath::getLoopMode
LoopMode getLoopMode() const
Definition AnimationPath:183
osg::AnimationPath::_loopMode
LoopMode _loopMode
Definition AnimationPath:212
osg::AnimationPath::LoopMode
LoopMode
Definition AnimationPath:175
osg::AnimationPath::NO_LOOPING
@ NO_LOOPING
Definition AnimationPath:178
osg::AnimationPath::SWING
@ SWING
Definition AnimationPath:176
osg::AnimationPath::LOOP
@ LOOP
Definition AnimationPath:177
osg::AnimationPath::getTimeControlPointMap
TimeControlPointMap & getTimeControlPointMap()
Definition AnimationPath:190
osg::AnimationPath::write
void write(TimeControlPointMap::const_iterator itr, std::ostream &out) const
osg::AnimationPath::~AnimationPath
virtual ~AnimationPath()
Definition AnimationPath:209
osg::AnimationPath::AnimationPath
AnimationPath(const AnimationPath &ap, const CopyOp &copyop=CopyOp::SHALLOW_COPY)
Definition AnimationPath:39
osg::AnimationPath::getPeriod
double getPeriod() const
Definition AnimationPath:172
osg::AnimationPath::getFirstTime
double getFirstTime() const
Definition AnimationPath:170
osg::AnimationPath::getLastTime
double getLastTime() const
Definition AnimationPath:171
osg::Callback
Definition Callback:34
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::Matrixd
Definition Matrixd:27
osg::Matrixd::preMultScale
void preMultScale(const Vec3d &v)
Definition Matrixd:747
osg::Matrixd::postMultScale
void postMultScale(const Vec3d &v)
Definition Matrixd:761
osg::Matrixd::preMultTranslate
void preMultTranslate(const Vec3d &v)
Definition Matrixd:691
osg::Matrixd::postMultTranslate
void postMultTranslate(const Vec3d &v)
Definition Matrixd:719
osg::Matrixd::makeRotate
void makeRotate(const Vec3f &from, const Vec3f &to)
osg::Matrixf
Definition Matrixf:27
osg::Matrixf::postMultTranslate
void postMultTranslate(const Vec3d &v)
Definition Matrixf:823
osg::Matrixf::preMultScale
void preMultScale(const Vec3d &v)
Definition Matrixf:851
osg::Matrixf::preMultTranslate
void preMultTranslate(const Vec3d &v)
Definition Matrixf:795
osg::Matrixf::makeRotate
void makeRotate(const Vec3f &from, const Vec3f &to)
osg::Matrixf::postMultScale
void postMultScale(const Vec3d &v)
Definition Matrixf:865
osg::NodeCallback::NodeCallback
NodeCallback()
Definition Callback:219
osg::NodeVisitor
Definition NodeVisitor:82
osg::Node
Definition Node:72
osg::Object
Definition Object:61
osg::Object::Object
Object()
Definition Object:69
osg::Quat
Definition Quat:30
osg::Vec3d
Definition Vec3d:30
osg::ref_ptr
Definition ref_ptr:32
osg
Definition AlphaFunc:19
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39