]>
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/Log.hh>
33 #include <Moof/Math.hh>
39 // TODO - cleanup these classes
43 void clamp(Scalar
& value
)
90 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
92 mScale
= 1.0 / seconds
;
98 void setMode(Mode mode
)
105 void update(Scalar t
, Scalar dt
)
109 mAlpha
+= dt
* mScale
;
120 virtual void calculate(Scalar alpha
) = 0;
130 template <class T
= Scalar
>
131 class InterpolatorBase
: public Interpolator
135 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
137 Interpolator::init(seconds
, mode
);
139 calculate(0.0); // set value
143 void calculate(Scalar alpha
)
146 calculate(mValue
, alpha
);
149 virtual void calculate(T
& value
, Scalar alpha
) = 0;
151 const T
& getValue() const
156 const T
getState(Scalar alpha
) const
158 return cml::lerp(mPrevious
, mValue
, alpha
);
168 template <int D
, class T
= Scalar
>
169 class PolynomialInterpolator
: public InterpolatorBase
<T
>
173 PolynomialInterpolator() {}
175 PolynomialInterpolator(const T coefficients
[D
+1],
176 Scalar seconds
= 1.0, Interpolator::Mode mode
= Interpolator::STOP
)
178 init(coefficients
, seconds
, mode
);
181 void init(const T coefficients
[D
+1], Scalar seconds
= 1.0,
182 Interpolator::Mode mode
= Interpolator::STOP
)
189 // build an array of the computed factorials we will need
190 for (int i
= 2; i
<= D
; ++i
)
192 fac
[i
] = i
* fac
[i
- 1];
195 // combine the coefficients for fast updating
196 for (int i
= 0; i
<= D
; ++i
)
198 // n! / (k! * (n - k)!)
199 mCoefficients
[i
] = coefficients
[i
] * fac
[D
] / (fac
[i
] * fac
[D
- i
]);
202 InterpolatorBase
<T
>::init(seconds
, mode
);
206 void calculate(T
& value
, Scalar alpha
)
208 Scalar beta
= 1.0 - alpha
;
210 value
= mCoefficients
[0] * std::pow(beta
, D
);
212 for (int i
= 1; i
<= D
; ++i
)
214 value
+= mCoefficients
[i
] * std::pow(beta
, D
- i
) *
221 T mCoefficients
[D
+1];
225 // specialized linear interpolator
228 class PolynomialInterpolator
<1,T
> : public InterpolatorBase
<T
>
232 PolynomialInterpolator() {}
234 PolynomialInterpolator(const T coefficients
[2], Scalar seconds
= 1.0,
235 Interpolator::Mode mode
= Interpolator::STOP
)
236 //InterpolatorBase<T>(seconds, mode)
238 init(coefficients
, seconds
, mode
);
241 void init(const T coefficients
[2], Scalar seconds
= 1.0,
242 Interpolator::Mode mode
= Interpolator::STOP
)
244 mA
= coefficients
[0];
245 mB
= coefficients
[1];
247 InterpolatorBase
<T
>::init(seconds
, mode
);
251 void calculate(T
& value
, Scalar alpha
)
253 value
= cml::lerp(mA
, mB
, alpha
);
263 // Here are some aliases for more common interpolators. Also see the
264 // interpolation functions in cml for other types of interpolation such as
265 // slerp and some multi-alpha interpolators.
267 typedef PolynomialInterpolator
<1> Lerp
; // linear
268 typedef PolynomialInterpolator
<1,Vector2
> Lerp2
;
269 typedef PolynomialInterpolator
<1,Vector3
> Lerp3
;
270 typedef PolynomialInterpolator
<1,Vector4
> Lerp4
;
272 typedef PolynomialInterpolator
<2> Qerp
; // quadratic
273 typedef PolynomialInterpolator
<2,Vector2
> Qerp2
;
274 typedef PolynomialInterpolator
<2,Vector3
> Qerp3
;
275 typedef PolynomialInterpolator
<2,Vector4
> Qerp4
;
277 typedef PolynomialInterpolator
<3> Cerp
; // cubic
278 typedef PolynomialInterpolator
<3,Vector2
> Cerp2
;
279 typedef PolynomialInterpolator
<3,Vector3
> Cerp3
;
280 typedef PolynomialInterpolator
<3,Vector4
> Cerp4
;
285 #endif // _MOOF_INTERPOLATOR_HH_
287 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.043786 seconds and 4 git commands to generate.