openscenegraph
Keyframe
Go to the documentation of this file.
1/* -*-c++-*-
2 * Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
3 *
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.
8 *
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.
13*/
14
15#ifndef OSGANIMATION_KEYFRAME_H
16#define OSGANIMATION_KEYFRAME_H
17
18#include <string>
19#include <osg/Referenced>
20#include <osg/MixinVector>
21#include <osgAnimation/Vec3Packed>
22#include <osgAnimation/CubicBezier>
23#include <osg/Quat>
24#include <osg/Vec4>
25#include <osg/Vec3>
26#include <osg/Vec2>
27#include <osg/Vec3us>
28#include <osg/Matrixf>
29
30namespace osgAnimation
31{
32
33 class Keyframe
34 {
35 public:
36 double getTime() const { return _time; }
37 void setTime(double time) { _time = time; }
38
39 protected:
40 double _time;
41
42 };
43
44 template <class T>
45 class TemplateKeyframe : public Keyframe
46 {
47 protected:
48 T _value;
49 public:
50 typedef T value_type;
51
52 TemplateKeyframe () {}
53 ~TemplateKeyframe () {}
54
55 TemplateKeyframe (double time, const T& value)
56 {
57 _time = time;
58 _value = value;
59 }
60
61 void setValue(const T& value) { _value = value;}
62 const T& getValue() const { return _value;}
63 };
64
65
66 class KeyframeContainer : public osg::Referenced
67 {
68 public:
69 KeyframeContainer() {}
70 virtual unsigned int size() const = 0;
71 virtual unsigned int linearInterpolationDeduplicate() = 0;
72 protected:
73 ~KeyframeContainer() {}
74 std::string _name;
75 };
76
77
78 template <class T>
79 class TemplateKeyframeContainer : public osg::MixinVector<TemplateKeyframe<T> >, public KeyframeContainer
80 {
81 public:
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() {
88 if(size() <= 1) {
89 return 0;
90 }
91
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);
100 intervalSize = 0;
101 }
102 }
103 intervalSizes.push_back(intervalSize);
104
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]);
110 if(*iterator > 1) {
111 deduplicated.push_back((*this)[cumul + (*iterator) - 1]);
112 }
113 cumul += *iterator;
114 }
115
116 unsigned int count = size() - deduplicated.size();
117 this->swap(deduplicated);
118 return count;
119 }
120 };
121
122 template <>
123 class TemplateKeyframeContainer<Vec3Packed> : public osg::MixinVector<TemplateKeyframe<Vec3Packed> >, public KeyframeContainer
124 {
125 public:
126 typedef TemplateKeyframe<Vec3Packed> KeyType;
127
128 TemplateKeyframeContainer() {}
129 const char* getKeyframeType() { return "Vec3Packed" ;}
130 void init(const osg::Vec3f& min, const osg::Vec3f& scale) { _min = min; _scale = scale; }
131
132 osg::Vec3f _min;
133 osg::Vec3f _scale;
134 };
135
136
137 typedef TemplateKeyframe<float> FloatKeyframe;
138 typedef TemplateKeyframeContainer<float> FloatKeyframeContainer;
139
140 typedef TemplateKeyframe<double> DoubleKeyframe;
141 typedef TemplateKeyframeContainer<double> DoubleKeyframeContainer;
142
143 typedef TemplateKeyframe<osg::Vec2> Vec2Keyframe;
144 typedef TemplateKeyframeContainer<osg::Vec2> Vec2KeyframeContainer;
145
146 typedef TemplateKeyframe<osg::Vec3> Vec3Keyframe;
147 typedef TemplateKeyframeContainer<osg::Vec3> Vec3KeyframeContainer;
148
149 typedef TemplateKeyframe<osg::Vec3us> Vec3usKeyframe;
150 typedef TemplateKeyframeContainer<osg::Vec3us> Vec3usKeyframeContainer;
151
152 typedef TemplateKeyframe<osg::Vec4> Vec4Keyframe;
153 typedef TemplateKeyframeContainer<osg::Vec4> Vec4KeyframeContainer;
154
155 typedef TemplateKeyframe<osg::Quat> QuatKeyframe;
156 typedef TemplateKeyframeContainer<osg::Quat> QuatKeyframeContainer;
157
158 typedef TemplateKeyframe<osg::Vec3us> Vec3usKeyframe;
159 typedef TemplateKeyframeContainer<osg::Vec3us> Vec3usKeyframeContainer;
160
161 typedef TemplateKeyframe<osg::Matrixf> MatrixKeyframe;
162 typedef TemplateKeyframeContainer<osg::Matrixf> MatrixKeyframeContainer;
163
164 typedef TemplateKeyframe<Vec3Packed> Vec3PackedKeyframe;
165 typedef TemplateKeyframeContainer<Vec3Packed> Vec3PackedKeyframeContainer;
166
167 typedef TemplateKeyframe<FloatCubicBezier> FloatCubicBezierKeyframe;
168 typedef TemplateKeyframeContainer<FloatCubicBezier> FloatCubicBezierKeyframeContainer;
169
170 typedef TemplateKeyframe<DoubleCubicBezier> DoubleCubicBezierKeyframe;
171 typedef TemplateKeyframeContainer<DoubleCubicBezier> DoubleCubicBezierKeyframeContainer;
172
173 typedef TemplateKeyframe<Vec2CubicBezier> Vec2CubicBezierKeyframe;
174 typedef TemplateKeyframeContainer<Vec2CubicBezier> Vec2CubicBezierKeyframeContainer;
175
176 typedef TemplateKeyframe<Vec3CubicBezier> Vec3CubicBezierKeyframe;
177 typedef TemplateKeyframeContainer<Vec3CubicBezier> Vec3CubicBezierKeyframeContainer;
178
179 typedef TemplateKeyframe<Vec4CubicBezier> Vec4CubicBezierKeyframe;
180 typedef TemplateKeyframeContainer<Vec4CubicBezier> Vec4CubicBezierKeyframeContainer;
181
182}
183
184#endif