osgAnimation/Interpolator
Go to the documentation of this file.
/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
*
* 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.
*
* Authors:
* Cedric Pinson <cedric.pinson@plopbyte.net>
* Michael Platings <mplatings@pixelpower.com>
*/
#ifndef OSGANIMATION_INTERPOLATOR
#define OSGANIMATION_INTERPOLATOR 1
#include <osg/Notify>
#include <osgAnimation/Keyframe>
namespace osgAnimation
{
template <class TYPE, class KEY>
class TemplateInterpolatorBase
{
public:
typedef KEY KeyframeType;
typedef TYPE UsingType;
public:
TemplateInterpolatorBase() {}
int getKeyIndexFromTime(const TemplateKeyframeContainer<KEY>& keys, double time) const
{
int key_size = keys.size();
if (!key_size) {
osg::notify(osg::WARN) << "TemplateInterpolatorBase::getKeyIndexFromTime the container is empty, impossible to get key index from time" << std::endl;;
return -1;
}
const TemplateKeyframe<KeyframeType>* keysVector = &keys.front();
int k = 0;
int l = key_size;
int mid = key_size/2;
while(mid != k){
double time1 = keysVector[mid].getTime();
if(time1 < time){
k = mid;
} else {
l = mid;
}
mid = (l+k)/2;
}
return k;
}
};
template <class TYPE, class KEY=TYPE>
class TemplateStepInterpolator : public TemplateInterpolatorBase<TYPE,KEY>
{
public:
TemplateStepInterpolator() {}
void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
{
if (time >= keyframes.back().getTime())
{
result = keyframes.back().getValue();
return;
}
else if (time <= keyframes.front().getTime())
{
result = keyframes.front().getValue();
return;
}
int i = this->getKeyIndexFromTime(keyframes,time);
result = keyframes[i].getValue();
}
};
template <class TYPE, class KEY=TYPE>
class TemplateLinearInterpolator : public TemplateInterpolatorBase<TYPE,KEY>
{
public:
TemplateLinearInterpolator() {}
void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
{
if (time >= keyframes.back().getTime())
{
result = keyframes.back().getValue();
return;
}
else if (time <= keyframes.front().getTime())
{
result = keyframes.front().getValue();
return;
}
int i = this->getKeyIndexFromTime(keyframes,time);
float blend = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
const TYPE& v1 = keyframes[i].getValue();
const TYPE& v2 = keyframes[i+1].getValue();
result = v1*(1-blend) + v2*blend;
}
};
template <class TYPE, class KEY=TYPE>
class TemplateSphericalLinearInterpolator : public TemplateInterpolatorBase<TYPE,KEY>
{
public:
TemplateSphericalLinearInterpolator() {}
void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
{
if (time >= keyframes.back().getTime())
{
result = keyframes.back().getValue();
return;
}
else if (time <= keyframes.front().getTime())
{
result = keyframes.front().getValue();
return;
}
int i = this->getKeyIndexFromTime(keyframes,time);
float blend = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
const TYPE& q1 = keyframes[i].getValue();
const TYPE& q2 = keyframes[i+1].getValue();
result.slerp(blend,q1,q2);
}
};
template <class TYPE, class KEY>
class TemplateLinearPackedInterpolator : public TemplateInterpolatorBase<TYPE,KEY>
{
public:
TemplateLinearPackedInterpolator() {}
void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
{
if (time >= keyframes.back().getTime())
{
keyframes.back().getValue().uncompress(keyframes.mScale, keyframes.mMin, result);
return;
}
else if (time <= keyframes.front().getTime())
{
keyframes.front().getValue().uncompress(keyframes.mScale, keyframes.mMin, result);
return;
}
int i = this->getKeyIndexFromTime(keyframes,time);
float blend = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
TYPE v1,v2;
keyframes[i].getValue().uncompress(keyframes.mScale, keyframes.mMin, v1);
keyframes[i+1].getValue().uncompress(keyframes.mScale, keyframes.mMin, v2);
result = v1*(1-blend) + v2*blend;
}
};
// http://en.wikipedia.org/wiki/B%C3%A9zier_curve
template <class TYPE, class KEY=TYPE>
class TemplateCubicBezierInterpolator : public TemplateInterpolatorBase<TYPE,KEY>
{
public:
TemplateCubicBezierInterpolator() {}
void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
{
if (time >= keyframes.back().getTime())
{
result = keyframes.back().getValue().getPosition();
return;
}
else if (time <= keyframes.front().getTime())
{
result = keyframes.front().getValue().getPosition();
return;
}
int i = this->getKeyIndexFromTime(keyframes,time);
float t = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
float one_minus_t = 1.0-t;
float one_minus_t2 = one_minus_t * one_minus_t;
float one_minus_t3 = one_minus_t2 * one_minus_t;
float t2 = t * t;
TYPE v0 = keyframes[i].getValue().getPosition() * one_minus_t3;
TYPE v1 = keyframes[i].getValue().getControlPointIn() * (3.0 * t * one_minus_t2);
TYPE v2 = keyframes[i].getValue().getControlPointOut() * (3.0 * t2 * one_minus_t);
TYPE v3 = keyframes[i+1].getValue().getPosition() * (t2 * t);
result = v0 + v1 + v2 + v3;
}
};
typedef TemplateStepInterpolator<double, double> DoubleStepInterpolator;
typedef TemplateStepInterpolator<float, float> FloatStepInterpolator;
typedef TemplateStepInterpolator<osg::Vec2, osg::Vec2> Vec2StepInterpolator;
typedef TemplateStepInterpolator<osg::Vec3, osg::Vec3> Vec3StepInterpolator;
typedef TemplateStepInterpolator<osg::Vec3, Vec3Packed> Vec3PackedStepInterpolator;
typedef TemplateStepInterpolator<osg::Vec4, osg::Vec4> Vec4StepInterpolator;
typedef TemplateStepInterpolator<osg::Quat, osg::Quat> QuatStepInterpolator;
typedef TemplateLinearInterpolator<double, double> DoubleLinearInterpolator;
typedef TemplateLinearInterpolator<float, float> FloatLinearInterpolator;
typedef TemplateLinearInterpolator<osg::Vec2, osg::Vec2> Vec2LinearInterpolator;
typedef TemplateLinearInterpolator<osg::Vec3, osg::Vec3> Vec3LinearInterpolator;
typedef TemplateLinearInterpolator<osg::Vec3, Vec3Packed> Vec3PackedLinearInterpolator;
typedef TemplateLinearInterpolator<osg::Vec4, osg::Vec4> Vec4LinearInterpolator;
typedef TemplateSphericalLinearInterpolator<osg::Quat, osg::Quat> QuatSphericalLinearInterpolator;
typedef TemplateLinearInterpolator<osg::Matrixf, osg::Matrixf> MatrixLinearInterpolator;
typedef TemplateCubicBezierInterpolator<float, FloatCubicBezier > FloatCubicBezierInterpolator;
typedef TemplateCubicBezierInterpolator<double, DoubleCubicBezier> DoubleCubicBezierInterpolator;
typedef TemplateCubicBezierInterpolator<osg::Vec2, Vec2CubicBezier> Vec2CubicBezierInterpolator;
typedef TemplateCubicBezierInterpolator<osg::Vec3, Vec3CubicBezier> Vec3CubicBezierInterpolator;
typedef TemplateCubicBezierInterpolator<osg::Vec4, Vec4CubicBezier> Vec4CubicBezierInterpolator;
}
#endif
Keyframe
Notify
osgAnimation::Keyframe::getTime
double getTime() const
Definition Keyframe:36
osgAnimation::TemplateCubicBezierInterpolator
Definition osgAnimation/Interpolator:177
osgAnimation::TemplateCubicBezierInterpolator::TemplateCubicBezierInterpolator
TemplateCubicBezierInterpolator()
Definition osgAnimation/Interpolator:180
osgAnimation::TemplateCubicBezierInterpolator::getValue
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition osgAnimation/Interpolator:181
osgAnimation::TemplateInterpolatorBase::KeyframeType
KEY KeyframeType
Definition osgAnimation/Interpolator:32
osgAnimation::TemplateInterpolatorBase::UsingType
TYPE UsingType
Definition osgAnimation/Interpolator:33
osgAnimation::TemplateInterpolatorBase::getKeyIndexFromTime
int getKeyIndexFromTime(const TemplateKeyframeContainer< KEY > &keys, double time) const
Definition osgAnimation/Interpolator:38
osgAnimation::TemplateInterpolatorBase::TemplateInterpolatorBase
TemplateInterpolatorBase()
Definition osgAnimation/Interpolator:36
osgAnimation::TemplateKeyframeContainer
Definition Keyframe:80
osgAnimation::TemplateKeyframeContainer::size
virtual unsigned int size() const
Definition Keyframe:86
osgAnimation::TemplateKeyframe
Definition Keyframe:46
osgAnimation::TemplateLinearInterpolator
Definition osgAnimation/Interpolator:91
osgAnimation::TemplateLinearInterpolator::TemplateLinearInterpolator
TemplateLinearInterpolator()
Definition osgAnimation/Interpolator:94
osgAnimation::TemplateLinearInterpolator::getValue
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition osgAnimation/Interpolator:95
osgAnimation::TemplateLinearPackedInterpolator::TemplateLinearPackedInterpolator
TemplateLinearPackedInterpolator()
Definition osgAnimation/Interpolator:150
osgAnimation::TemplateLinearPackedInterpolator::getValue
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition osgAnimation/Interpolator:151
osgAnimation::TemplateSphericalLinearInterpolator
Definition osgAnimation/Interpolator:120
osgAnimation::TemplateSphericalLinearInterpolator::getValue
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition osgAnimation/Interpolator:123
osgAnimation::TemplateSphericalLinearInterpolator::TemplateSphericalLinearInterpolator
TemplateSphericalLinearInterpolator()
Definition osgAnimation/Interpolator:122
osgAnimation::TemplateStepInterpolator
Definition osgAnimation/Interpolator:65
osgAnimation::TemplateStepInterpolator::TemplateStepInterpolator
TemplateStepInterpolator()
Definition osgAnimation/Interpolator:68
osgAnimation::TemplateStepInterpolator::getValue
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition osgAnimation/Interpolator:69
osg::MixinVector::back
const_reference back() const
Definition MixinVector:136
osg::MixinVector::front
const_reference front() const
Definition MixinVector:138
osgAnimation
Definition Action:34
osgAnimation::FloatCubicBezierInterpolator
TemplateCubicBezierInterpolator< float, FloatCubicBezier > FloatCubicBezierInterpolator
Definition osgAnimation/Interpolator:229
osgAnimation::Vec3PackedStepInterpolator
TemplateStepInterpolator< osg::Vec3, Vec3Packed > Vec3PackedStepInterpolator
Definition osgAnimation/Interpolator:216
osgAnimation::Vec3LinearInterpolator
TemplateLinearInterpolator< osg::Vec3, osg::Vec3 > Vec3LinearInterpolator
Definition osgAnimation/Interpolator:223
osgAnimation::Vec4StepInterpolator
TemplateStepInterpolator< osg::Vec4, osg::Vec4 > Vec4StepInterpolator
Definition osgAnimation/Interpolator:217
osgAnimation::Vec3PackedLinearInterpolator
TemplateLinearInterpolator< osg::Vec3, Vec3Packed > Vec3PackedLinearInterpolator
Definition osgAnimation/Interpolator:224
osgAnimation::FloatLinearInterpolator
TemplateLinearInterpolator< float, float > FloatLinearInterpolator
Definition osgAnimation/Interpolator:221
osgAnimation::Vec2StepInterpolator
TemplateStepInterpolator< osg::Vec2, osg::Vec2 > Vec2StepInterpolator
Definition osgAnimation/Interpolator:214
osgAnimation::Vec2CubicBezierInterpolator
TemplateCubicBezierInterpolator< osg::Vec2, Vec2CubicBezier > Vec2CubicBezierInterpolator
Definition osgAnimation/Interpolator:231
osgAnimation::Vec4LinearInterpolator
TemplateLinearInterpolator< osg::Vec4, osg::Vec4 > Vec4LinearInterpolator
Definition osgAnimation/Interpolator:225
osgAnimation::Vec3StepInterpolator
TemplateStepInterpolator< osg::Vec3, osg::Vec3 > Vec3StepInterpolator
Definition osgAnimation/Interpolator:215
osgAnimation::Vec3CubicBezierInterpolator
TemplateCubicBezierInterpolator< osg::Vec3, Vec3CubicBezier > Vec3CubicBezierInterpolator
Definition osgAnimation/Interpolator:232
osgAnimation::FloatStepInterpolator
TemplateStepInterpolator< float, float > FloatStepInterpolator
Definition osgAnimation/Interpolator:213
osgAnimation::DoubleStepInterpolator
TemplateStepInterpolator< double, double > DoubleStepInterpolator
Definition osgAnimation/Interpolator:212
osgAnimation::Vec4CubicBezierInterpolator
TemplateCubicBezierInterpolator< osg::Vec4, Vec4CubicBezier > Vec4CubicBezierInterpolator
Definition osgAnimation/Interpolator:233
osgAnimation::QuatStepInterpolator
TemplateStepInterpolator< osg::Quat, osg::Quat > QuatStepInterpolator
Definition osgAnimation/Interpolator:218
osgAnimation::DoubleCubicBezierInterpolator
TemplateCubicBezierInterpolator< double, DoubleCubicBezier > DoubleCubicBezierInterpolator
Definition osgAnimation/Interpolator:230
osgAnimation::MatrixLinearInterpolator
TemplateLinearInterpolator< osg::Matrixf, osg::Matrixf > MatrixLinearInterpolator
Definition osgAnimation/Interpolator:227
osgAnimation::QuatSphericalLinearInterpolator
TemplateSphericalLinearInterpolator< osg::Quat, osg::Quat > QuatSphericalLinearInterpolator
Definition osgAnimation/Interpolator:226
osgAnimation::DoubleLinearInterpolator
TemplateLinearInterpolator< double, double > DoubleLinearInterpolator
Definition osgAnimation/Interpolator:220
osgAnimation::Vec2LinearInterpolator
TemplateLinearInterpolator< osg::Vec2, osg::Vec2 > Vec2LinearInterpolator
Definition osgAnimation/Interpolator:222
osg::WARN
@ WARN
Definition Notify:33
osg::notify
std::ostream & notify(void)
Definition Notify:80