2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_INTERPOLATOR_HH_
13 #define _MOOF_INTERPOLATOR_HH_
16 * \file interpolator.hh
17 * Functions and classes concerning interpolations between values.
20 #include <moof/math.hh>
27 * Interpolators should inherit from this base class to get some free
28 * functionality. Subclasses of this base class will implement their own
29 * interpolation functions.
31 template <class F
, class T
= scalar
>
41 stop
= 0, /// Interpolator will stop when done.
42 repeat
= 1, /// Interpolator will go back to the beginning.
43 oscillate
= 2 /// Interpolator will reverse direction.
48 * Construct an uninitialized interpolator.
54 * Construct an interpolator.
55 * \param a The initial value.
56 * \param b The target value.
57 * \param t The duration of the interpolation.
58 * \param mode The interpolation mode.
60 interpolator(const T
& a
, const T
& b
, scalar t
= 1.0, mode mode
= stop
) :
66 scale_(SCALAR(1.0) / t
),
71 * Initialize the interpolator. Any interpolation already being
72 * tracked by this object will be replaced with a new interpolation
73 * based on the initialization arguments.
74 * \param a The initial value.
75 * \param b The target value.
76 * \param t The duration of the interpolation.
77 * \param mode The interpolation mode.
79 void init(const T
& a
, const T
& b
, scalar t
= 1.0, mode mode
= stop
)
91 * Update the interpolation state with a timeslice.
92 * \param t The total time in seconds.
93 * \param dt The number of seconds passed since the last call to
96 void update(scalar t
, scalar dt
)
102 alpha_
+= dt
* scale_
;
108 alpha_
= SCALAR(1.0);
112 alpha_
-= SCALAR(1.0);
115 alpha_
= SCALAR(2.0) - alpha_
;
120 else if (alpha_
< 0.0)
125 alpha_
= SCALAR(0.0);
129 alpha_
+= SCALAR(1.0);
138 state_
= function_(a_
, b_
, alpha_
);
144 * Get the interpolated value.
145 * \return The interpolated value.
153 * Get the interpolated value interpolated between two updates.
154 * \param alpha The fraction between updates.
155 * \return The interpolated value.
157 T
state(scalar alpha
) const
159 return lerp(prior_
, state_
, alpha
);
163 * Get whether or not the interpolation is done. This will only return
164 * true if the interpolation mode is stop.
165 * \return True if the interpolation has finished.
189 struct linear_interpolation_function
192 T
operator () (const T
& a
, const T
& b
, scalar alpha
)
194 return lerp(a
, b
, alpha
);
199 typedef interpolator
<linear_interpolation_function
> lerp_scalar
;
204 #endif // _MOOF_INTERPOLATOR_HH_