]>
Dogcows Code - chaz/yoink/blob - src/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 _INTERPOLATOR_HH_
30 #define _INTERPOLATOR_HH_
38 void clamp(scalar
& value
)
84 void init(scalar seconds
= 1.0, mode onFinish
= stop
)
86 scale
= 1.0 / seconds
;
92 void setMode(mode onFinish
)
99 void update(scalar dt
)
109 virtual void calculate(scalar alpha
) = 0;
119 class interpolator_base
: public interpolator
122 void init(scalar seconds
= 1.0, mode onFinish
= stop
)
124 interpolator::init(seconds
, onFinish
);
126 calculate(0.0); // set value
127 calculate(0.0); // set previous
130 void calculate(scalar alpha
)
133 calculate(value
, alpha
);
136 virtual void calculate(T
& value
, scalar alpha
) = 0;
143 const T
getState(scalar alpha
)
145 return cml::lerp(previous
, value
, alpha
);
154 template <class T
, int D
>
155 class binomial_interpolator
: public interpolator_base
<T
>
158 binomial_interpolator() {}
160 explicit binomial_interpolator(const T coeff
[D
+1], scalar seconds
= 1.0,
161 interpolator::mode onFinish
= interpolator::stop
)
163 init(coeff
, seconds
, onFinish
);
166 void init(const T coeff
[D
+1], scalar seconds
= 1.0,
167 interpolator::mode onFinish
= interpolator::stop
)
174 // build an array of the computed factorials we will need
175 for (int i
= 2; i
<= D
; i
++)
177 fac
[i
] = i
* fac
[i
- 1];
180 // combine the coefficients for fast updating
181 for (int i
= 0; i
<= D
; i
++)
183 // n! / (k! * (n - k)!)
184 coefficient
[i
] = coeff
[i
] * fac
[D
] / (fac
[i
] * fac
[D
- i
]);
187 interpolator_base
<T
>::init(seconds
, onFinish
);
191 void calculate(T
& value
, scalar alpha
)
193 scalar beta
= 1.0 - alpha
;
195 value
= coefficient
[0] * std::pow(beta
, D
);
197 for (int i
= 1; i
<= D
; i
++)
199 value
+= coefficient
[i
] * std::pow(beta
, D
- i
) * std::pow(alpha
, i
);
210 class binomial_interpolator
<T
,1> : public interpolator_base
<T
>
213 binomial_interpolator() {}
215 explicit binomial_interpolator(const T coeff
[2], scalar seconds
= 1.0,
216 interpolator::mode onFinish
= interpolator::stop
)
217 //interpolator_base<T>(seconds, onFinish)
219 init(coeff
, seconds
, onFinish
);
222 void init(const T coeff
[2], scalar seconds
= 1.0,
223 interpolator::mode onFinish
= interpolator::stop
)
225 coefficient
[0] = coeff
[0];
226 coefficient
[1] = coeff
[1];
228 interpolator_base
<T
>::init(seconds
, onFinish
);
232 void calculate(T
& value
, scalar alpha
)
234 value
= cml::lerp(coefficient
[0], coefficient
[1], alpha
);
242 // Here are some aliases for more common interpolators. Also see the
243 // interpolation functions in cml for other types of interpolation such as
244 // slerp and some multi-alpha interpolators.
246 typedef binomial_interpolator
<scalar
, 1> lerps
; // linear
247 typedef binomial_interpolator
<vector2
,1> lerpv2
;
248 typedef binomial_interpolator
<vector3
,1> lerpv3
;
249 typedef binomial_interpolator
<vector4
,1> lerpv4
;
251 typedef binomial_interpolator
<scalar
,2> qerps
; // quadratic
252 typedef binomial_interpolator
<vector2
,2> qerpv2
;
253 typedef binomial_interpolator
<vector3
,2> qerpv3
;
254 typedef binomial_interpolator
<vector4
,2> qerpv4
;
256 typedef binomial_interpolator
<scalar
,3> cerps
; // cubic
257 typedef binomial_interpolator
<vector2
,3> cerpv2
;
258 typedef binomial_interpolator
<vector3
,3> cerpv3
;
259 typedef binomial_interpolator
<vector4
,3> cerpv4
;
264 #endif // _INTERPOLATOR_HH_
266 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.050574 seconds and 5 git commands to generate.