]>
Dogcows Code - chaz/yoink/blob - Interpolator.hh
4de3f6974bd3a7ceeaf93945b9d7ca5323126c07
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/Log.hh>
33 #include <Moof/Math.hh>
39 // TODO - cleanup these classes
43 void clamp(Scalar
& value
)
89 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
91 scale_
= 1.0 / seconds
;
97 void setMode(Mode mode
)
104 void update(Scalar t
, Scalar dt
)
108 alpha_
+= dt
* scale_
;
119 virtual void calculate(Scalar alpha
) = 0;
129 template <class T
= Scalar
>
130 class InterpolatorBase
: public Interpolator
133 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
135 Interpolator::init(seconds
, mode
);
137 calculate(0.0); // set value
141 void calculate(Scalar alpha
)
144 calculate(value_
, alpha
);
147 virtual void calculate(T
& value
, Scalar alpha
) = 0;
149 const T
& getValue() const
154 const T
getState(Scalar alpha
) const
156 return cml::lerp(previous_
, value_
, alpha
);
165 template <int D
, class T
= Scalar
>
166 class PolynomialInterpolator
: public InterpolatorBase
<T
>
169 PolynomialInterpolator() {}
171 PolynomialInterpolator(const T coefficients
[D
+1],
172 Scalar seconds
= 1.0, Interpolator::Mode mode
= Interpolator::STOP
)
174 init(coefficients
, seconds
, mode
);
177 void init(const T coefficients
[D
+1], Scalar seconds
= 1.0,
178 Interpolator::Mode mode
= Interpolator::STOP
)
185 // build an array of the computed factorials we will need
186 for (int i
= 2; i
<= D
; ++i
)
188 fac
[i
] = i
* fac
[i
- 1];
191 // combine the coefficients for fast updating
192 for (int i
= 0; i
<= D
; ++i
)
194 // n! / (k! * (n - k)!)
195 coefficients_
[i
] = coefficients
[i
] * fac
[D
] / (fac
[i
] * fac
[D
- i
]);
198 InterpolatorBase
<T
>::init(seconds
, mode
);
202 void calculate(T
& value
, Scalar alpha
)
204 Scalar beta
= 1.0 - alpha
;
206 value
= coefficients_
[0] * std::pow(beta
, D
);
208 for (int i
= 1; i
<= D
; ++i
)
210 value
+= coefficients_
[i
] * std::pow(beta
, D
- i
) *
216 T coefficients_
[D
+1];
220 // specialized linear interpolator
223 class PolynomialInterpolator
<1,T
> : public InterpolatorBase
<T
>
226 PolynomialInterpolator() {}
228 PolynomialInterpolator(const T coefficients
[2], Scalar seconds
= 1.0,
229 Interpolator::Mode mode
= Interpolator::STOP
)
230 //InterpolatorBase<T>(seconds, mode)
232 init(coefficients
, seconds
, mode
);
235 void init(const T coefficients
[2], Scalar seconds
= 1.0,
236 Interpolator::Mode mode
= Interpolator::STOP
)
238 a_
= coefficients
[0];
239 b_
= coefficients
[1];
241 InterpolatorBase
<T
>::init(seconds
, mode
);
245 void calculate(T
& value
, Scalar alpha
)
247 value
= cml::lerp(a_
, b_
, alpha
);
256 // Here are some aliases for more common interpolators. Also see the
257 // interpolation functions in cml for other types of interpolation such as
258 // slerp and some multi-alpha interpolators.
260 typedef PolynomialInterpolator
<1> Lerp
; // linear
261 typedef PolynomialInterpolator
<1,Vector2
> Lerp2
;
262 typedef PolynomialInterpolator
<1,Vector3
> Lerp3
;
263 typedef PolynomialInterpolator
<1,Vector4
> Lerp4
;
265 typedef PolynomialInterpolator
<2> Qerp
; // quadratic
266 typedef PolynomialInterpolator
<2,Vector2
> Qerp2
;
267 typedef PolynomialInterpolator
<2,Vector3
> Qerp3
;
268 typedef PolynomialInterpolator
<2,Vector4
> Qerp4
;
270 typedef PolynomialInterpolator
<3> Cerp
; // cubic
271 typedef PolynomialInterpolator
<3,Vector2
> Cerp2
;
272 typedef PolynomialInterpolator
<3,Vector3
> Cerp3
;
273 typedef PolynomialInterpolator
<3,Vector4
> Cerp4
;
278 #endif // _MOOF_INTERPOLATOR_HH_
280 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.04962 seconds and 3 git commands to generate.