2 * Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
4 * This library is open source and may be redistributed and/or modified under
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version. The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * OpenSceneGraph Public License for more details.
15#ifndef OSGANIMATION_KEYFRAME_H
16#define OSGANIMATION_KEYFRAME_H
19#include <osg/Referenced>
20#include <osg/MixinVector>
21#include <osgAnimation/Vec3Packed>
22#include <osgAnimation/CubicBezier>
36 double getTime() const { return _time; }
37 void setTime(double time) { _time = time; }
45 class TemplateKeyframe : public Keyframe
52 TemplateKeyframe () {}
53 ~TemplateKeyframe () {}
55 TemplateKeyframe (double time, const T& value)
61 void setValue(const T& value) { _value = value;}
62 const T& getValue() const { return _value;}
66 class KeyframeContainer : public osg::Referenced
69 KeyframeContainer() {}
70 virtual unsigned int size() const = 0;
71 virtual unsigned int linearInterpolationDeduplicate() = 0;
73 ~KeyframeContainer() {}
79 class TemplateKeyframeContainer : public osg::MixinVector<TemplateKeyframe<T> >, public KeyframeContainer
82 // const char* getKeyframeType() { return #T ;}
83 TemplateKeyframeContainer() {}
84 typedef TemplateKeyframe<T> KeyType;
85 typedef typename osg::MixinVector< TemplateKeyframe<T> > VectorType;
86 virtual unsigned int size() const { return (unsigned int)osg::MixinVector<TemplateKeyframe<T> >::size(); }
87 virtual unsigned int linearInterpolationDeduplicate() {
92 typename VectorType::iterator keyframe = VectorType::begin(),
93 previous = VectorType::begin();
94 // 1. find number of consecutives identical keyframes
95 std::vector<unsigned int> intervalSizes;
96 unsigned int intervalSize = 1;
97 for(++ keyframe ; keyframe != VectorType::end() ; ++ keyframe, ++ previous, ++ intervalSize) {
98 if(!(previous->getValue() == keyframe->getValue())) {
99 intervalSizes.push_back(intervalSize);
103 intervalSizes.push_back(intervalSize);
105 // 2. build deduplicated list of keyframes
106 unsigned int cumul = 0;
107 VectorType deduplicated;
108 for(std::vector<unsigned int>::iterator iterator = intervalSizes.begin() ; iterator != intervalSizes.end() ; ++ iterator) {
109 deduplicated.push_back((*this)[cumul]);
111 deduplicated.push_back((*this)[cumul + (*iterator) - 1]);
116 unsigned int count = size() - deduplicated.size();
117 this->swap(deduplicated);
123 class TemplateKeyframeContainer<Vec3Packed> : public osg::MixinVector<TemplateKeyframe<Vec3Packed> >, public KeyframeContainer
126 typedef TemplateKeyframe<Vec3Packed> KeyType;
128 TemplateKeyframeContainer() {}
129 const char* getKeyframeType() { return "Vec3Packed" ;}
130 void init(const osg::Vec3f& min, const osg::Vec3f& scale) { _min = min; _scale = scale; }
137 typedef TemplateKeyframe<float> FloatKeyframe;
138 typedef TemplateKeyframeContainer<float> FloatKeyframeContainer;
140 typedef TemplateKeyframe<double> DoubleKeyframe;
141 typedef TemplateKeyframeContainer<double> DoubleKeyframeContainer;
143 typedef TemplateKeyframe<osg::Vec2> Vec2Keyframe;
144 typedef TemplateKeyframeContainer<osg::Vec2> Vec2KeyframeContainer;
146 typedef TemplateKeyframe<osg::Vec3> Vec3Keyframe;
147 typedef TemplateKeyframeContainer<osg::Vec3> Vec3KeyframeContainer;
149 typedef TemplateKeyframe<osg::Vec3us> Vec3usKeyframe;
150 typedef TemplateKeyframeContainer<osg::Vec3us> Vec3usKeyframeContainer;
152 typedef TemplateKeyframe<osg::Vec4> Vec4Keyframe;
153 typedef TemplateKeyframeContainer<osg::Vec4> Vec4KeyframeContainer;
155 typedef TemplateKeyframe<osg::Quat> QuatKeyframe;
156 typedef TemplateKeyframeContainer<osg::Quat> QuatKeyframeContainer;
158 typedef TemplateKeyframe<osg::Vec3us> Vec3usKeyframe;
159 typedef TemplateKeyframeContainer<osg::Vec3us> Vec3usKeyframeContainer;
161 typedef TemplateKeyframe<osg::Matrixf> MatrixKeyframe;
162 typedef TemplateKeyframeContainer<osg::Matrixf> MatrixKeyframeContainer;
164 typedef TemplateKeyframe<Vec3Packed> Vec3PackedKeyframe;
165 typedef TemplateKeyframeContainer<Vec3Packed> Vec3PackedKeyframeContainer;
167 typedef TemplateKeyframe<FloatCubicBezier> FloatCubicBezierKeyframe;
168 typedef TemplateKeyframeContainer<FloatCubicBezier> FloatCubicBezierKeyframeContainer;
170 typedef TemplateKeyframe<DoubleCubicBezier> DoubleCubicBezierKeyframe;
171 typedef TemplateKeyframeContainer<DoubleCubicBezier> DoubleCubicBezierKeyframeContainer;
173 typedef TemplateKeyframe<Vec2CubicBezier> Vec2CubicBezierKeyframe;
174 typedef TemplateKeyframeContainer<Vec2CubicBezier> Vec2CubicBezierKeyframeContainer;
176 typedef TemplateKeyframe<Vec3CubicBezier> Vec3CubicBezierKeyframe;
177 typedef TemplateKeyframeContainer<Vec3CubicBezier> Vec3CubicBezierKeyframeContainer;
179 typedef TemplateKeyframe<Vec4CubicBezier> Vec4CubicBezierKeyframe;
180 typedef TemplateKeyframeContainer<Vec4CubicBezier> Vec4CubicBezierKeyframeContainer;