/* -*-c++-*- * Copyright (C) 2008 Cedric Pinson * * 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 * Michael Platings */ #ifndef OSGANIMATION_INTERPOLATOR #define OSGANIMATION_INTERPOLATOR 1 #include #include namespace osgAnimation { template class TemplateInterpolatorBase { public: typedef KEY KeyframeType; typedef TYPE UsingType; public: TemplateInterpolatorBase() {} int getKeyIndexFromTime(const TemplateKeyframeContainer& 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* 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 TemplateStepInterpolator : public TemplateInterpolatorBase { public: TemplateStepInterpolator() {} void getValue(const TemplateKeyframeContainer& 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 TemplateLinearInterpolator : public TemplateInterpolatorBase { public: TemplateLinearInterpolator() {} void getValue(const TemplateKeyframeContainer& 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 TemplateSphericalLinearInterpolator : public TemplateInterpolatorBase { public: TemplateSphericalLinearInterpolator() {} void getValue(const TemplateKeyframeContainer& 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 TemplateLinearPackedInterpolator : public TemplateInterpolatorBase { public: TemplateLinearPackedInterpolator() {} void getValue(const TemplateKeyframeContainer& 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 TemplateCubicBezierInterpolator : public TemplateInterpolatorBase { public: TemplateCubicBezierInterpolator() {} void getValue(const TemplateKeyframeContainer& 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 DoubleStepInterpolator; typedef TemplateStepInterpolator FloatStepInterpolator; typedef TemplateStepInterpolator Vec2StepInterpolator; typedef TemplateStepInterpolator Vec3StepInterpolator; typedef TemplateStepInterpolator Vec3PackedStepInterpolator; typedef TemplateStepInterpolator Vec4StepInterpolator; typedef TemplateStepInterpolator QuatStepInterpolator; typedef TemplateLinearInterpolator DoubleLinearInterpolator; typedef TemplateLinearInterpolator FloatLinearInterpolator; typedef TemplateLinearInterpolator Vec2LinearInterpolator; typedef TemplateLinearInterpolator Vec3LinearInterpolator; typedef TemplateLinearInterpolator Vec3PackedLinearInterpolator; typedef TemplateLinearInterpolator Vec4LinearInterpolator; typedef TemplateSphericalLinearInterpolator QuatSphericalLinearInterpolator; typedef TemplateLinearInterpolator MatrixLinearInterpolator; typedef TemplateCubicBezierInterpolator FloatCubicBezierInterpolator; typedef TemplateCubicBezierInterpolator DoubleCubicBezierInterpolator; typedef TemplateCubicBezierInterpolator Vec2CubicBezierInterpolator; typedef TemplateCubicBezierInterpolator Vec3CubicBezierInterpolator; typedef TemplateCubicBezierInterpolator Vec4CubicBezierInterpolator; } #endif