]>
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>
40 void clamp(Scalar
& value
)
86 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
88 scale_
= 1.0 / seconds
;
94 void setMode(Mode mode
)
101 void update(Scalar dt
)
105 alpha_
+= dt
* scale_
;
111 virtual void calculate(Scalar alpha
) = 0;
121 class InterpolatorBase
: public Interpolator
124 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
126 Interpolator::init(seconds
, mode
);
128 calculate(0.0); // set value
132 void calculate(Scalar alpha
)
135 calculate(value_
, alpha
);
138 virtual void calculate(T
& value
, Scalar alpha
) = 0;
145 const T
getState(Scalar alpha
)
147 return cml::lerp(previous_
, value_
, alpha
);
156 template <class T
, int D
>
157 class BinomialInterpolator
: public InterpolatorBase
<T
>
160 BinomialInterpolator() {}
162 explicit BinomialInterpolator(const T coefficients
[D
+1],
163 Scalar seconds
= 1.0, Interpolator::Mode mode
= Interpolator::STOP
)
165 init(coefficients
, seconds
, mode
);
168 void init(const T coefficients
[D
+1], Scalar seconds
= 1.0,
169 Interpolator::Mode mode
= Interpolator::STOP
)
176 // build an array of the computed factorials we will need
177 for (int i
= 2; i
<= D
; i
++)
179 fac
[i
] = i
* fac
[i
- 1];
182 // combine the coefficients for fast updating
183 for (int i
= 0; i
<= D
; i
++)
185 // n! / (k! * (n - k)!)
186 coefficients_
[i
] = coefficients
[i
] * fac
[D
] / (fac
[i
] * fac
[D
- i
]);
189 InterpolatorBase
<T
>::init(seconds
, mode
);
193 void calculate(T
& value
, Scalar alpha
)
195 Scalar beta
= 1.0 - alpha
;
197 value
= coefficients_
[0] * std::pow(beta
, D
);
199 for (int i
= 1; i
<= D
; i
++)
201 value
+= coefficients_
[i
] * std::pow(beta
, D
- i
) *
207 T coefficients_
[D
+1];
212 class BinomialInterpolator
<T
,1> : public InterpolatorBase
<T
>
215 BinomialInterpolator() {}
217 explicit BinomialInterpolator(const T coefficients
[2], Scalar seconds
= 1.0,
218 Interpolator::Mode mode
= Interpolator::STOP
)
219 //InterpolatorBase<T>(seconds, mode)
221 init(coefficients
, seconds
, mode
);
224 void init(const T coefficients
[2], Scalar seconds
= 1.0,
225 Interpolator::Mode mode
= Interpolator::STOP
)
227 a_
= coefficients
[0];
228 b_
= coefficients
[1];
230 InterpolatorBase
<T
>::init(seconds
, mode
);
234 void calculate(T
& value
, Scalar alpha
)
236 value
= cml::lerp(a_
, b_
, alpha
);
245 // Here are some aliases for more common interpolators. Also see the
246 // interpolation functions in cml for other types of interpolation such as
247 // slerp and some multi-alpha interpolators.
249 typedef BinomialInterpolator
<Scalar
, 1> Lerps
; // linear
250 typedef BinomialInterpolator
<Vector2
,1> Lerpv2
;
251 typedef BinomialInterpolator
<Vector3
,1> Lerpv3
;
252 typedef BinomialInterpolator
<Vector4
,1> Lerpv4
;
254 typedef BinomialInterpolator
<Scalar
,2> Qerps
; // quadratic
255 typedef BinomialInterpolator
<Vector2
,2> Qerpv2
;
256 typedef BinomialInterpolator
<Vector3
,2> Qerpv3
;
257 typedef BinomialInterpolator
<Vector4
,2> Qerpv4
;
259 typedef BinomialInterpolator
<Scalar
,3> Cerps
; // cubic
260 typedef BinomialInterpolator
<Vector2
,3> Cerpv2
;
261 typedef BinomialInterpolator
<Vector3
,3> Cerpv3
;
262 typedef BinomialInterpolator
<Vector4
,3> Cerpv4
;
267 #endif // _MOOF_INTERPOLATOR_HH_
269 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.045973 seconds and 4 git commands to generate.