OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Interpolator.h
Go to the documentation of this file.
1 /* -*-c++-*-
2  * Copyright (C) 2008 Cedric Pinson <cedric.pinson@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  * Authors:
15  * Cedric Pinson <cedric.pinson@plopbyte.net>
16  * Michael Platings <mplatings@pixelpower.com>
17  */
18 
19 #ifndef OSGANIMATION_INTERPOLATOR
20 #define OSGANIMATION_INTERPOLATOR 1
21 
22 #include <osg/Notify>
23 #include <osgAnimation/Keyframe>
24 
25 namespace osgAnimation
26 {
27 
28  template <class TYPE, class KEY>
30  {
31  public:
32  typedef KEY KeyframeType;
33  typedef TYPE UsingType;
34 
35  public:
37 
38  int getKeyIndexFromTime(const TemplateKeyframeContainer<KEY>& keys, double time) const
39  {
40  int key_size = keys.size();
41  if (!key_size) {
42  osg::notify(osg::WARN) << "TemplateInterpolatorBase::getKeyIndexFromTime the container is empty, impossible to get key index from time" << std::endl;;
43  return -1;
44  }
45  const TemplateKeyframe<KeyframeType>* keysVector = &keys.front();
46  int k = 0;
47  int l = key_size;
48  int mid = key_size/2;
49  while(mid != k){
50  double time1 = keysVector[mid].getTime();
51  if(time1 < time){
52  k = mid;
53  } else {
54  l = mid;
55  }
56  mid = (l+k)/2;
57  }
58  return k;
59  }
60  };
61 
62 
63  template <class TYPE, class KEY=TYPE>
65  {
66  public:
67 
69  void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
70  {
71 
72  if (time >= keyframes.back().getTime())
73  {
74  result = keyframes.back().getValue();
75  return;
76  }
77  else if (time <= keyframes.front().getTime())
78  {
79  result = keyframes.front().getValue();
80  return;
81  }
82 
83  int i = this->getKeyIndexFromTime(keyframes,time);
84  result = keyframes[i].getValue();
85  }
86  };
87 
88 
89  template <class TYPE, class KEY=TYPE>
91  {
92  public:
93 
95  void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
96  {
97 
98  if (time >= keyframes.back().getTime())
99  {
100  result = keyframes.back().getValue();
101  return;
102  }
103  else if (time <= keyframes.front().getTime())
104  {
105  result = keyframes.front().getValue();
106  return;
107  }
108 
109  int i = this->getKeyIndexFromTime(keyframes,time);
110  float blend = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
111  const TYPE& v1 = keyframes[i].getValue();
112  const TYPE& v2 = keyframes[i+1].getValue();
113  result = v1*(1-blend) + v2*blend;
114  }
115  };
116 
117 
118  template <class TYPE, class KEY=TYPE>
120  {
121  public:
123  void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
124  {
125  if (time >= keyframes.back().getTime())
126  {
127  result = keyframes.back().getValue();
128  return;
129  }
130  else if (time <= keyframes.front().getTime())
131  {
132  result = keyframes.front().getValue();
133  return;
134  }
135 
136  int i = this->getKeyIndexFromTime(keyframes,time);
137  float blend = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
138  const TYPE& q1 = keyframes[i].getValue();
139  const TYPE& q2 = keyframes[i+1].getValue();
140  result.slerp(blend,q1,q2);
141  }
142  };
143 
144 
145  template <class TYPE, class KEY>
147  {
148  public:
149 
151  void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
152  {
153  if (time >= keyframes.back().getTime())
154  {
155  keyframes.back().getValue().uncompress(keyframes.mScale, keyframes.mMin, result);
156  return;
157  }
158  else if (time <= keyframes.front().getTime())
159  {
160  keyframes.front().getValue().uncompress(keyframes.mScale, keyframes.mMin, result);
161  return;
162  }
163 
164  int i = this->getKeyIndexFromTime(keyframes,time);
165  float blend = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
166  TYPE v1,v2;
167  keyframes[i].getValue().uncompress(keyframes.mScale, keyframes.mMin, v1);
168  keyframes[i+1].getValue().uncompress(keyframes.mScale, keyframes.mMin, v2);
169  result = v1*(1-blend) + v2*blend;
170  }
171  };
172 
173 
174  // http://en.wikipedia.org/wiki/B%C3%A9zier_curve
175  template <class TYPE, class KEY=TYPE>
177  {
178  public:
179 
181  void getValue(const TemplateKeyframeContainer<KEY>& keyframes, double time, TYPE& result) const
182  {
183 
184  if (time >= keyframes.back().getTime())
185  {
186  result = keyframes.back().getValue().getPosition();
187  return;
188  }
189  else if (time <= keyframes.front().getTime())
190  {
191  result = keyframes.front().getValue().getPosition();
192  return;
193  }
194 
195  int i = this->getKeyIndexFromTime(keyframes,time);
196 
197  float t = (time - keyframes[i].getTime()) / ( keyframes[i+1].getTime() - keyframes[i].getTime());
198  float one_minus_t = 1.0-t;
199  float one_minus_t2 = one_minus_t * one_minus_t;
200  float one_minus_t3 = one_minus_t2 * one_minus_t;
201  float t2 = t * t;
202 
203  TYPE v0 = keyframes[i].getValue().getPosition() * one_minus_t3;
204  TYPE v1 = keyframes[i].getValue().getControlPointIn() * (3.0 * t * one_minus_t2);
205  TYPE v2 = keyframes[i].getValue().getControlPointOut() * (3.0 * t2 * one_minus_t);
206  TYPE v3 = keyframes[i+1].getValue().getPosition() * (t2 * t);
207 
208  result = v0 + v1 + v2 + v3;
209  }
210  };
211 
219 
228 
234 
235 }
236 #endif
TemplateSphericalLinearInterpolator< osg::Quat, osg::Quat > QuatSphericalLinearInterpolator
Definition: Interpolator.h:226
TemplateStepInterpolator< float, float > FloatStepInterpolator
Definition: Interpolator.h:213
TemplateLinearInterpolator< osg::Vec2, osg::Vec2 > Vec2LinearInterpolator
Definition: Interpolator.h:222
TemplateStepInterpolator< osg::Vec3, Vec3Packed > Vec3PackedStepInterpolator
Definition: Interpolator.h:216
virtual unsigned int size() const
Definition: Keyframe.h:81
TemplateLinearInterpolator< osg::Vec3, Vec3Packed > Vec3PackedLinearInterpolator
Definition: Interpolator.h:224
TemplateLinearInterpolator< float, float > FloatLinearInterpolator
Definition: Interpolator.h:221
TemplateCubicBezierInterpolator< float, FloatCubicBezier > FloatCubicBezierInterpolator
Definition: Interpolator.h:229
TemplateCubicBezierInterpolator< osg::Vec3, Vec3CubicBezier > Vec3CubicBezierInterpolator
Definition: Interpolator.h:232
TemplateLinearInterpolator< double, double > DoubleLinearInterpolator
Definition: Interpolator.h:220
TemplateCubicBezierInterpolator< double, DoubleCubicBezier > DoubleCubicBezierInterpolator
Definition: Interpolator.h:230
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition: Interpolator.h:123
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition: Interpolator.h:151
TemplateCubicBezierInterpolator< osg::Vec2, Vec2CubicBezier > Vec2CubicBezierInterpolator
Definition: Interpolator.h:231
TemplateStepInterpolator< double, double > DoubleStepInterpolator
Definition: Interpolator.h:212
int getKeyIndexFromTime(const TemplateKeyframeContainer< KEY > &keys, double time) const
Definition: Interpolator.h:38
TemplateLinearInterpolator< osg::Matrixf, osg::Matrixf > MatrixLinearInterpolator
Definition: Interpolator.h:227
TemplateStepInterpolator< osg::Vec4, osg::Vec4 > Vec4StepInterpolator
Definition: Interpolator.h:217
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition: Interpolator.h:95
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition: Interpolator.h:181
double getTime() const
Definition: Keyframe.h:34
OSG_EXPORT std::ostream & notify(const NotifySeverity severity)
TemplateCubicBezierInterpolator< osg::Vec4, Vec4CubicBezier > Vec4CubicBezierInterpolator
Definition: Interpolator.h:233
TemplateLinearInterpolator< osg::Vec4, osg::Vec4 > Vec4LinearInterpolator
Definition: Interpolator.h:225
void getValue(const TemplateKeyframeContainer< KEY > &keyframes, double time, TYPE &result) const
Definition: Interpolator.h:69
TemplateStepInterpolator< osg::Vec3, osg::Vec3 > Vec3StepInterpolator
Definition: Interpolator.h:215
TemplateStepInterpolator< osg::Vec2, osg::Vec2 > Vec2StepInterpolator
Definition: Interpolator.h:214
TemplateStepInterpolator< osg::Quat, osg::Quat > QuatStepInterpolator
Definition: Interpolator.h:218
TemplateLinearInterpolator< osg::Vec3, osg::Vec3 > Vec3LinearInterpolator
Definition: Interpolator.h:223