2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
10 #ifndef _MOOF_INTERPOLATOR_HH_
11 #define _MOOF_INTERPOLATOR_HH_
13 #include <moof/math.hh>
17 * \file interpolator.hh
18 * Functions and classes concerning interpolations between values.
25 * Interpolators should inherit from this base class to get some free
26 * functionality. Subclasses of this base class will implement their own
27 * interpolation functions.
29 template <class F
, class T
= scalar
>
39 stop
= 0, /// Interpolator will stop when done.
40 repeat
= 1, /// Interpolator will go back to the beginning.
41 oscillate
= 2 /// Interpolator will reverse direction.
45 * Construct an uninitialized interpolator.
51 * Construct an interpolator.
52 * \param a The initial value.
53 * \param b The target value.
54 * \param t The duration of the interpolation.
55 * \param mode The interpolation mode.
57 interpolator(const T
& a
, const T
& b
,
58 scalar t
= 1.0, mode mode
= stop
) :
64 scale_(SCALAR(1.0) / t
),
69 * Initialize the interpolator. Any interpolation already being
70 * tracked by this object will be replaced with a new interpolation
71 * based on the initialization arguments.
72 * \param a The initial value.
73 * \param b The target value.
74 * \param t The duration of the interpolation.
75 * \param mode The interpolation mode.
77 void init(const T
& a
, const T
& b
, scalar t
= 1.0, mode mode
= stop
)
88 * Update the interpolation state with a timeslice.
89 * \param t The total time in seconds.
90 * \param dt The number of seconds passed since the last call to
93 void update(scalar t
, scalar dt
)
99 alpha_
+= dt
* scale_
;
105 alpha_
= SCALAR(1.0);
109 alpha_
-= SCALAR(1.0);
112 alpha_
= SCALAR(2.0) - alpha_
;
117 else if (alpha_
< 0.0)
122 alpha_
= SCALAR(0.0);
126 alpha_
+= SCALAR(1.0);
134 state_
= function_(a_
, b_
, alpha_
);
139 * Get the interpolated value.
140 * \return The interpolated value.
148 * Get the interpolated value interpolated between two updates.
149 * \param alpha The fraction between updates.
150 * \return The interpolated value.
152 T
state(scalar alpha
) const
154 return lerp(prior_
, state_
, alpha
);
158 * Get whether or not the interpolation is done. This will only return
159 * true if the interpolation mode is stop.
160 * \return True if the interpolation has finished.
183 struct linear_interpolation_function
186 T
operator () (const T
& a
, const T
& b
, scalar alpha
)
188 return lerp(a
, b
, alpha
);
192 typedef interpolator
<linear_interpolation_function
> lerp_scalar
;
197 #endif // _MOOF_INTERPOLATOR_HH_