-/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+/*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
**] All rights reserved.
*
-* vi:ts=4 sw=4 tw=75
-*
* Distributable under the terms and conditions of the 2-clause BSD license;
* see the file COPYING for a complete text of the license.
*
-**************************************************************************/
+*****************************************************************************/
#ifndef _MOOF_INTERPOLATOR_HH_
#define _MOOF_INTERPOLATOR_HH_
+#include <moof/math.hh>
+
+
/**
* \file interpolator.hh
* Functions and classes concerning interpolations between values.
*/
-#include <moof/math.hh>
-
-
namespace moof {
{
stop = 0, /// Interpolator will stop when done.
repeat = 1, /// Interpolator will go back to the beginning.
- oscillate = 2 /// Interpolator will reverse direction.
+ oscillate = 2 /// Interpolator will reverse direction.
};
-
/**
* Construct an uninitialized interpolator.
*/
* \param t The duration of the interpolation.
* \param mode The interpolation mode.
*/
- interpolator(const T& a, const T& b, scalar t = 1.0, mode mode = stop) :
+ interpolator(const T& a, const T& b,
+ scalar t = 1.0, mode mode = stop) :
state_(a),
prior_(a),
a_(a),
is_done_ = false;
}
-
/**
* Update the interpolation state with a timeslice.
* \param t The total time in seconds.
*/
void update(scalar t, scalar dt)
{
+ prior_ = state_;
+
if (!is_done_)
{
alpha_ += dt * scale_;
- prior_ = state_;
- state_ = function_(a_, b_, alpha_);
-
if (alpha_ > 1.0)
{
switch (mode_)
{
- case stop:
- alpha_ = SCALAR(1.0);
- is_done_ = true;
- break;
- case repeat:
- alpha_ -= SCALAR(1.0);
- break;
- case oscillate:
- alpha_ = SCALAR(2.0) - alpha_;
- scale_ = -scale_;
- break;
+ case stop:
+ alpha_ = SCALAR(1.0);
+ is_done_ = true;
+ break;
+ case repeat:
+ alpha_ -= SCALAR(1.0);
+ break;
+ case oscillate:
+ alpha_ = SCALAR(2.0) - alpha_;
+ scale_ = -scale_;
+ break;
}
}
else if (alpha_ < 0.0)
{
switch (mode_)
{
- case stop:
- alpha_ = SCALAR(0.0);
- is_done_ = true;
- break;
- case repeat:
- alpha_ += SCALAR(1.0);
- break;
- case oscillate:
- alpha_ = -alpha_;
- scale_ = -scale_;
- break;
+ case stop:
+ alpha_ = SCALAR(0.0);
+ is_done_ = true;
+ break;
+ case repeat:
+ alpha_ += SCALAR(1.0);
+ break;
+ case oscillate:
+ alpha_ = -alpha_;
+ scale_ = -scale_;
+ break;
}
}
+ state_ = function_(a_, b_, alpha_);
}
}
-
/**
* Get the interpolated value.
* \return The interpolated value.
return is_done_;
}
-
private:
T state_;
}
};
-
typedef interpolator<linear_interpolation_function> lerp_scalar;