]>
Dogcows Code - chaz/yoink/blob - src/Moof/Interpolator.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
29 #ifndef _MOOF_INTERPOLATOR_HH_
30 #define _MOOF_INTERPOLATOR_HH_
32 #include <Moof/Math.hh>
38 // TODO - cleanup these classes
42 void clamp(Scalar
& value
)
88 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
90 scale_
= 1.0 / seconds
;
96 void setMode(Mode mode
)
103 void update(Scalar dt
)
107 alpha_
+= dt
* scale_
;
113 virtual void calculate(Scalar alpha
) = 0;
122 template <class T
= Scalar
>
123 class InterpolatorBase
: public Interpolator
126 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
128 Interpolator::init(seconds
, mode
);
130 calculate(0.0); // set value
134 void calculate(Scalar alpha
)
137 calculate(value_
, alpha
);
140 virtual void calculate(T
& value
, Scalar alpha
) = 0;
147 const T
getState(Scalar alpha
)
149 return cml::lerp(previous_
, value_
, alpha
);
158 template <int D
, class T
= Scalar
>
159 class PolynomialInterpolator
: public InterpolatorBase
<T
>
162 PolynomialInterpolator() {}
164 PolynomialInterpolator(const T coefficients
[D
+1],
165 Scalar seconds
= 1.0, Interpolator::Mode mode
= Interpolator::STOP
)
167 init(coefficients
, seconds
, mode
);
170 void init(const T coefficients
[D
+1], Scalar seconds
= 1.0,
171 Interpolator::Mode mode
= Interpolator::STOP
)
178 // build an array of the computed factorials we will need
179 for (int i
= 2; i
<= D
; ++i
)
181 fac
[i
] = i
* fac
[i
- 1];
184 // combine the coefficients for fast updating
185 for (int i
= 0; i
<= D
; ++i
)
187 // n! / (k! * (n - k)!)
188 coefficients_
[i
] = coefficients
[i
] * fac
[D
] / (fac
[i
] * fac
[D
- i
]);
191 InterpolatorBase
<T
>::init(seconds
, mode
);
195 void calculate(T
& value
, Scalar alpha
)
197 Scalar beta
= 1.0 - alpha
;
199 value
= coefficients_
[0] * std::pow(beta
, D
);
201 for (int i
= 1; i
<= D
; ++i
)
203 value
+= coefficients_
[i
] * std::pow(beta
, D
- i
) *
209 T coefficients_
[D
+1];
213 // specialized linear interpolator
216 class PolynomialInterpolator
<1,T
> : public InterpolatorBase
<T
>
219 PolynomialInterpolator() {}
221 PolynomialInterpolator(const T coefficients
[2], Scalar seconds
= 1.0,
222 Interpolator::Mode mode
= Interpolator::STOP
)
223 //InterpolatorBase<T>(seconds, mode)
225 init(coefficients
, seconds
, mode
);
228 void init(const T coefficients
[2], Scalar seconds
= 1.0,
229 Interpolator::Mode mode
= Interpolator::STOP
)
231 a_
= coefficients
[0];
232 b_
= coefficients
[1];
234 InterpolatorBase
<T
>::init(seconds
, mode
);
238 void calculate(T
& value
, Scalar alpha
)
240 value
= cml::lerp(a_
, b_
, alpha
);
249 // Here are some aliases for more common interpolators. Also see the
250 // interpolation functions in cml for other types of interpolation such as
251 // slerp and some multi-alpha interpolators.
253 typedef PolynomialInterpolator
<1> Lerp
; // linear
254 typedef PolynomialInterpolator
<1,Vector2
> Lerp2
;
255 typedef PolynomialInterpolator
<1,Vector3
> Lerp3
;
256 typedef PolynomialInterpolator
<1,Vector4
> Lerp4
;
258 typedef PolynomialInterpolator
<2> Qerp
; // quadratic
259 typedef PolynomialInterpolator
<2,Vector2
> Qerp2
;
260 typedef PolynomialInterpolator
<2,Vector3
> Qerp3
;
261 typedef PolynomialInterpolator
<2,Vector4
> Qerp4
;
263 typedef PolynomialInterpolator
<3> Cerp
; // cubic
264 typedef PolynomialInterpolator
<3,Vector2
> Cerp2
;
265 typedef PolynomialInterpolator
<3,Vector3
> Cerp3
;
266 typedef PolynomialInterpolator
<3,Vector4
> Cerp4
;
271 #endif // _MOOF_INTERPOLATOR_HH_
273 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.050374 seconds and 4 git commands to generate.