#ifndef _MOOF_INTERPOLATOR_HH_
#define _MOOF_INTERPOLATOR_HH_
+#include <Moof/Log.hh>
+#include <Moof/Math.hh>
+
namespace Mf {
+// TODO - cleanup these classes
+
class Interpolator
{
void clamp(Scalar& value)
{
case STOP:
value = 1.0;
- stopped_ = true;
+ done_ = true;
break;
case REPEAT:
value -= 1.0;
{
case STOP:
value = 0.0;
- stopped_ = true;
+ done_ = true;
break;
case REPEAT:
value += 1.0;
void setMode(Mode mode)
{
mode_ = mode;
- stopped_ = false;
+ done_ = false;
}
- void update(Scalar dt)
+ void update(Scalar t, Scalar dt)
{
- if (!stopped_)
+ if (!done_)
{
alpha_ += dt * scale_;
clamp(alpha_);
}
}
+ bool isDone() const
+ {
+ return done_;
+ }
+
virtual void calculate(Scalar alpha) = 0;
private:
+
Scalar alpha_;
Mode mode_;
Scalar scale_;
- bool stopped_;
+ bool done_;
};
-template <class T>
+template <class T = Scalar>
class InterpolatorBase : public Interpolator
{
public:
Interpolator::init(seconds, mode);
calculate(0.0); // set value
- calculate(0.0); // set previous
+ previous_ = value_;
}
void calculate(Scalar alpha)
virtual void calculate(T& value, Scalar alpha) = 0;
- const T& getValue()
+ const T& getValue() const
{
return value_;
}
- const T getState(Scalar alpha)
+ const T getState(Scalar alpha) const
{
return cml::lerp(previous_, value_, alpha);
}
};
-template <class T, int D>
-class BinomialInterpolator : public InterpolatorBase<T>
+template <int D, class T = Scalar>
+class PolynomialInterpolator : public InterpolatorBase<T>
{
public:
- BinomialInterpolator() {}
+ PolynomialInterpolator() {}
- explicit BinomialInterpolator(const T coefficients[D+1],
+ PolynomialInterpolator(const T coefficients[D+1],
Scalar seconds = 1.0, Interpolator::Mode mode = Interpolator::STOP)
{
init(coefficients, seconds, mode);
fac[1] = 1.0;
// build an array of the computed factorials we will need
- for (int i = 2; i <= D; i++)
+ for (int i = 2; i <= D; ++i)
{
fac[i] = i * fac[i - 1];
}
// combine the coefficients for fast updating
- for (int i = 0; i <= D; i++)
+ for (int i = 0; i <= D; ++i)
{
// n! / (k! * (n - k)!)
coefficients_[i] = coefficients[i] * fac[D] / (fac[i] * fac[D - i]);
value = coefficients_[0] * std::pow(beta, D);
- for (int i = 1; i <= D; i++)
+ for (int i = 1; i <= D; ++i)
{
value += coefficients_[i] * std::pow(beta, D - i) *
std::pow(alpha, i);
};
+// specialized linear interpolator
+
template <class T>
-class BinomialInterpolator<T,1> : public InterpolatorBase<T>
+class PolynomialInterpolator<1,T> : public InterpolatorBase<T>
{
public:
- BinomialInterpolator() {}
+ PolynomialInterpolator() {}
- explicit BinomialInterpolator(const T coefficients[2], Scalar seconds = 1.0,
+ PolynomialInterpolator(const T coefficients[2], Scalar seconds = 1.0,
Interpolator::Mode mode = Interpolator::STOP)
//InterpolatorBase<T>(seconds, mode)
{
// interpolation functions in cml for other types of interpolation such as
// slerp and some multi-alpha interpolators.
-typedef BinomialInterpolator<Scalar, 1> Lerps; // linear
-typedef BinomialInterpolator<Vector2,1> Lerpv2;
-typedef BinomialInterpolator<Vector3,1> Lerpv3;
-typedef BinomialInterpolator<Vector4,1> Lerpv4;
+typedef PolynomialInterpolator<1> Lerp; // linear
+typedef PolynomialInterpolator<1,Vector2> Lerp2;
+typedef PolynomialInterpolator<1,Vector3> Lerp3;
+typedef PolynomialInterpolator<1,Vector4> Lerp4;
-typedef BinomialInterpolator<Scalar ,2> Qerps; // quadratic
-typedef BinomialInterpolator<Vector2,2> Qerpv2;
-typedef BinomialInterpolator<Vector3,2> Qerpv3;
-typedef BinomialInterpolator<Vector4,2> Qerpv4;
+typedef PolynomialInterpolator<2> Qerp; // quadratic
+typedef PolynomialInterpolator<2,Vector2> Qerp2;
+typedef PolynomialInterpolator<2,Vector3> Qerp3;
+typedef PolynomialInterpolator<2,Vector4> Qerp4;
-typedef BinomialInterpolator<Scalar ,3> Cerps; // cubic
-typedef BinomialInterpolator<Vector2,3> Cerpv2;
-typedef BinomialInterpolator<Vector3,3> Cerpv3;
-typedef BinomialInterpolator<Vector4,3> Cerpv4;
+typedef PolynomialInterpolator<3> Cerp; // cubic
+typedef PolynomialInterpolator<3,Vector2> Cerp2;
+typedef PolynomialInterpolator<3,Vector3> Cerp3;
+typedef PolynomialInterpolator<3,Vector4> Cerp4;
} // namespace Mf