/* -*-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. */ #ifndef OSGANIMATION_KEYFRAME_H #define OSGANIMATION_KEYFRAME_H #include #include #include #include #include #include #include #include #include #include #include namespace osgAnimation { class Keyframe { public: double getTime() const { return _time; } void setTime(double time) { _time = time; } protected: double _time; }; template class TemplateKeyframe : public Keyframe { protected: T _value; public: typedef T value_type; TemplateKeyframe () {} ~TemplateKeyframe () {} TemplateKeyframe (double time, const T& value) { _time = time; _value = value; } void setValue(const T& value) { _value = value;} const T& getValue() const { return _value;} }; class KeyframeContainer : public osg::Referenced { public: KeyframeContainer() {} virtual unsigned int size() const = 0; virtual unsigned int linearInterpolationDeduplicate() = 0; protected: ~KeyframeContainer() {} std::string _name; }; template class TemplateKeyframeContainer : public osg::MixinVector >, public KeyframeContainer { public: // const char* getKeyframeType() { return #T ;} TemplateKeyframeContainer() {} typedef TemplateKeyframe KeyType; typedef typename osg::MixinVector< TemplateKeyframe > VectorType; virtual unsigned int size() const { return (unsigned int)osg::MixinVector >::size(); } virtual unsigned int linearInterpolationDeduplicate() { if(size() <= 1) { return 0; } typename VectorType::iterator keyframe = VectorType::begin(), previous = VectorType::begin(); // 1. find number of consecutives identical keyframes std::vector intervalSizes; unsigned int intervalSize = 1; for(++ keyframe ; keyframe != VectorType::end() ; ++ keyframe, ++ previous, ++ intervalSize) { if(!(previous->getValue() == keyframe->getValue())) { intervalSizes.push_back(intervalSize); intervalSize = 0; } } intervalSizes.push_back(intervalSize); // 2. build deduplicated list of keyframes unsigned int cumul = 0; VectorType deduplicated; for(std::vector::iterator iterator = intervalSizes.begin() ; iterator != intervalSizes.end() ; ++ iterator) { deduplicated.push_back((*this)[cumul]); if(*iterator > 1) { deduplicated.push_back((*this)[cumul + (*iterator) - 1]); } cumul += *iterator; } unsigned int count = size() - deduplicated.size(); this->swap(deduplicated); return count; } }; template <> class TemplateKeyframeContainer : public osg::MixinVector >, public KeyframeContainer { public: typedef TemplateKeyframe KeyType; TemplateKeyframeContainer() {} const char* getKeyframeType() { return "Vec3Packed" ;} void init(const osg::Vec3f& min, const osg::Vec3f& scale) { _min = min; _scale = scale; } osg::Vec3f _min; osg::Vec3f _scale; }; typedef TemplateKeyframe FloatKeyframe; typedef TemplateKeyframeContainer FloatKeyframeContainer; typedef TemplateKeyframe DoubleKeyframe; typedef TemplateKeyframeContainer DoubleKeyframeContainer; typedef TemplateKeyframe Vec2Keyframe; typedef TemplateKeyframeContainer Vec2KeyframeContainer; typedef TemplateKeyframe Vec3Keyframe; typedef TemplateKeyframeContainer Vec3KeyframeContainer; typedef TemplateKeyframe Vec3usKeyframe; typedef TemplateKeyframeContainer Vec3usKeyframeContainer; typedef TemplateKeyframe Vec4Keyframe; typedef TemplateKeyframeContainer Vec4KeyframeContainer; typedef TemplateKeyframe QuatKeyframe; typedef TemplateKeyframeContainer QuatKeyframeContainer; typedef TemplateKeyframe Vec3usKeyframe; typedef TemplateKeyframeContainer Vec3usKeyframeContainer; typedef TemplateKeyframe MatrixKeyframe; typedef TemplateKeyframeContainer MatrixKeyframeContainer; typedef TemplateKeyframe Vec3PackedKeyframe; typedef TemplateKeyframeContainer Vec3PackedKeyframeContainer; typedef TemplateKeyframe FloatCubicBezierKeyframe; typedef TemplateKeyframeContainer FloatCubicBezierKeyframeContainer; typedef TemplateKeyframe DoubleCubicBezierKeyframe; typedef TemplateKeyframeContainer DoubleCubicBezierKeyframeContainer; typedef TemplateKeyframe Vec2CubicBezierKeyframe; typedef TemplateKeyframeContainer Vec2CubicBezierKeyframeContainer; typedef TemplateKeyframe Vec3CubicBezierKeyframe; typedef TemplateKeyframeContainer Vec3CubicBezierKeyframeContainer; typedef TemplateKeyframe Vec4CubicBezierKeyframe; typedef TemplateKeyframeContainer Vec4CubicBezierKeyframeContainer; } #endif