// // C++ Implementation: BufferedLinearInterpolator1D // // Description: // // // Author: Takeshi Nakazato , (C) 2012 // // Copyright: See COPYING file that comes with this distribution // // #include #include #include #include "BufferedLinearInterpolator1D.h" namespace asap { template BufferedLinearInterpolator1D::BufferedLinearInterpolator1D() : Interpolator1D(), reusable_(false) {} template BufferedLinearInterpolator1D::~BufferedLinearInterpolator1D() {} template void BufferedLinearInterpolator1D::setData(T *x, U *y, unsigned int n) { Interpolator1D::setData(x, y, n); reusable_ = false; } template void BufferedLinearInterpolator1D::setX(T *x, unsigned int n) { Interpolator1D::setX(x, n); reusable_ = false; } template U BufferedLinearInterpolator1D::interpolate(T x) { //assert(this->isready()); assert_(this->isready(), "object is not ready to process."); if (this->n_ == 1) return this->y_[0]; unsigned int i; bool b = (reusable_ && x == xold_); if (b) { i = prev_; } else { i = this->locator_->locate(x); prev_ = i; xold_ = x; } // do not perform extrapolation if (i == 0) { return this->y_[i]; } else if (i == this->n_) { return this->y_[i-1]; } // linear interpolation if (!b) factor_ = (x - this->x_[i-1]) / (this->x_[i] - this->x_[i-1]); U y = this->y_[i-1] + (this->y_[i] - this->y_[i-1]) * factor_; reusable_ = true; return y; } }