X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FInterpolator.hh;h=4de3f6974bd3a7ceeaf93945b9d7ca5323126c07;hp=87e3acdbcf26a517fdaaa84cfbdac1c8de1c98d4;hb=a4debfe4a5f5d339410788971b698ba00cb7f09c;hpb=c2321281bf12a7efaedde930422c7ddbc92080d4 diff --git a/src/Moof/Interpolator.hh b/src/Moof/Interpolator.hh index 87e3acd..4de3f69 100644 --- a/src/Moof/Interpolator.hh +++ b/src/Moof/Interpolator.hh @@ -29,10 +29,15 @@ #ifndef _MOOF_INTERPOLATOR_HH_ #define _MOOF_INTERPOLATOR_HH_ +#include +#include + namespace Mf { +// TODO - cleanup these classes + class Interpolator { void clamp(Scalar& value) @@ -43,7 +48,7 @@ class Interpolator { case STOP: value = 1.0; - stopped_ = true; + done_ = true; break; case REPEAT: value -= 1.0; @@ -60,7 +65,7 @@ class Interpolator { case STOP: value = 0.0; - stopped_ = true; + done_ = true; break; case REPEAT: value += 1.0; @@ -92,13 +97,13 @@ public: 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_); @@ -106,16 +111,22 @@ public: } } + bool isDone() const + { + return done_; + } + virtual void calculate(Scalar alpha) = 0; private: + Scalar alpha_; Mode mode_; Scalar scale_; - bool stopped_; + bool done_; }; -template +template class InterpolatorBase : public Interpolator { public: @@ -124,7 +135,7 @@ public: Interpolator::init(seconds, mode); calculate(0.0); // set value - calculate(0.0); // set previous + previous_ = value_; } void calculate(Scalar alpha) @@ -135,12 +146,12 @@ public: 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); } @@ -151,13 +162,13 @@ private: }; -template -class BinomialInterpolator : public InterpolatorBase +template +class PolynomialInterpolator : public InterpolatorBase { 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); @@ -172,13 +183,13 @@ public: 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]); @@ -194,7 +205,7 @@ public: 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); @@ -206,13 +217,15 @@ private: }; +// specialized linear interpolator + template -class BinomialInterpolator : public InterpolatorBase +class PolynomialInterpolator<1,T> : public InterpolatorBase { 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(seconds, mode) { @@ -244,20 +257,20 @@ private: // interpolation functions in cml for other types of interpolation such as // slerp and some multi-alpha interpolators. -typedef BinomialInterpolator Lerps; // linear -typedef BinomialInterpolator Lerpv2; -typedef BinomialInterpolator Lerpv3; -typedef BinomialInterpolator Lerpv4; +typedef PolynomialInterpolator<1> Lerp; // linear +typedef PolynomialInterpolator<1,Vector2> Lerp2; +typedef PolynomialInterpolator<1,Vector3> Lerp3; +typedef PolynomialInterpolator<1,Vector4> Lerp4; -typedef BinomialInterpolator Qerps; // quadratic -typedef BinomialInterpolator Qerpv2; -typedef BinomialInterpolator Qerpv3; -typedef BinomialInterpolator Qerpv4; +typedef PolynomialInterpolator<2> Qerp; // quadratic +typedef PolynomialInterpolator<2,Vector2> Qerp2; +typedef PolynomialInterpolator<2,Vector3> Qerp3; +typedef PolynomialInterpolator<2,Vector4> Qerp4; -typedef BinomialInterpolator Cerps; // cubic -typedef BinomialInterpolator Cerpv2; -typedef BinomialInterpolator Cerpv3; -typedef BinomialInterpolator Cerpv4; +typedef PolynomialInterpolator<3> Cerp; // cubic +typedef PolynomialInterpolator<3,Vector2> Cerp2; +typedef PolynomialInterpolator<3,Vector3> Cerp3; +typedef PolynomialInterpolator<3,Vector4> Cerp4; } // namespace Mf