]>
Dogcows Code - chaz/yoink/blob - src/Moof/Interpolator.hh
5c9f09788172cb62fd21f03ff4fc748b76b7498e
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
)
83 virtual ~Interpolator() {}
92 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
94 mScale
= 1.0 / seconds
;
100 void setMode(Mode mode
)
107 void update(Scalar t
, Scalar dt
)
111 mAlpha
+= dt
* mScale
;
122 virtual void calculate(Scalar alpha
) = 0;
132 template <class T
= Scalar
>
133 class InterpolatorBase
: public Interpolator
137 virtual ~InterpolatorBase() {}
139 void init(Scalar seconds
= 1.0, Mode mode
= STOP
)
141 Interpolator::init(seconds
, mode
);
143 calculate(0.0); // set value
147 void calculate(Scalar alpha
)
150 calculate(mValue
, alpha
);
153 virtual void calculate(T
& value
, Scalar alpha
) = 0;
155 const T
& getValue() const
160 const T
getState(Scalar alpha
) const
162 return cml::lerp(mPrevious
, mValue
, alpha
);
172 template <int D
, class T
= Scalar
>
173 class PolynomialInterpolator
: public InterpolatorBase
<T
>
177 PolynomialInterpolator() {}
179 PolynomialInterpolator(const T coefficients
[D
+1],
180 Scalar seconds
= 1.0, Interpolator::Mode mode
= Interpolator::STOP
)
182 init(coefficients
, seconds
, mode
);
185 void init(const T coefficients
[D
+1], Scalar seconds
= 1.0,
186 Interpolator::Mode mode
= Interpolator::STOP
)
193 // build an array of the computed factorials we will need
194 for (int i
= 2; i
<= D
; ++i
)
196 fac
[i
] = i
* fac
[i
- 1];
199 // combine the coefficients for fast updating
200 for (int i
= 0; i
<= D
; ++i
)
202 // n! / (k! * (n - k)!)
203 mCoefficients
[i
] = coefficients
[i
] * fac
[D
] / (fac
[i
] * fac
[D
- i
]);
206 InterpolatorBase
<T
>::init(seconds
, mode
);
210 void calculate(T
& value
, Scalar alpha
)
212 Scalar beta
= 1.0 - alpha
;
214 value
= mCoefficients
[0] * std::pow(beta
, D
);
216 for (int i
= 1; i
<= D
; ++i
)
218 value
+= mCoefficients
[i
] * std::pow(beta
, D
- i
) *
225 T mCoefficients
[D
+1];
229 // specialized linear interpolator
232 class PolynomialInterpolator
<1,T
> : public InterpolatorBase
<T
>
236 PolynomialInterpolator() {}
238 PolynomialInterpolator(const T coefficients
[2], Scalar seconds
= 1.0,
239 Interpolator::Mode mode
= Interpolator::STOP
)
240 //InterpolatorBase<T>(seconds, mode)
242 init(coefficients
, seconds
, mode
);
245 void init(const T coefficients
[2], Scalar seconds
= 1.0,
246 Interpolator::Mode mode
= Interpolator::STOP
)
248 mA
= coefficients
[0];
249 mB
= coefficients
[1];
251 InterpolatorBase
<T
>::init(seconds
, mode
);
255 void calculate(T
& value
, Scalar alpha
)
257 value
= cml::lerp(mA
, mB
, alpha
);
267 // Here are some aliases for more common interpolators. Also see the
268 // interpolation functions in cml for other types of interpolation such as
269 // slerp and some multi-alpha interpolators.
271 typedef PolynomialInterpolator
<1> Lerp
; // linear
272 typedef PolynomialInterpolator
<1,Vector2
> Lerp2
;
273 typedef PolynomialInterpolator
<1,Vector3
> Lerp3
;
274 typedef PolynomialInterpolator
<1,Vector4
> Lerp4
;
276 typedef PolynomialInterpolator
<2> Qerp
; // quadratic
277 typedef PolynomialInterpolator
<2,Vector2
> Qerp2
;
278 typedef PolynomialInterpolator
<2,Vector3
> Qerp3
;
279 typedef PolynomialInterpolator
<2,Vector4
> Qerp4
;
281 typedef PolynomialInterpolator
<3> Cerp
; // cubic
282 typedef PolynomialInterpolator
<3,Vector2
> Cerp2
;
283 typedef PolynomialInterpolator
<3,Vector3
> Cerp3
;
284 typedef PolynomialInterpolator
<3,Vector4
> Cerp4
;
289 #endif // _MOOF_INTERPOLATOR_HH_
291 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.049954 seconds and 4 git commands to generate.