1 | // |
---|
2 | // C++ Implementation: BufferedLinearInterpolator1D |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2012 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | #include <assert.h> |
---|
13 | |
---|
14 | #include "BufferedLinearInterpolator1D.h" |
---|
15 | |
---|
16 | namespace asap { |
---|
17 | |
---|
18 | template <class T, class U> |
---|
19 | BufferedLinearInterpolator1D<T, U>::BufferedLinearInterpolator1D() |
---|
20 | : Interpolator1D<T, U>(), |
---|
21 | reusable_(false) |
---|
22 | {} |
---|
23 | |
---|
24 | template <class T, class U> |
---|
25 | BufferedLinearInterpolator1D<T, U>::~BufferedLinearInterpolator1D() |
---|
26 | {} |
---|
27 | |
---|
28 | template <class T, class U> |
---|
29 | void BufferedLinearInterpolator1D<T, U>::setData(T *x, U *y, unsigned int n) |
---|
30 | { |
---|
31 | Interpolator1D<T, U>::setData(x, y, n); |
---|
32 | reusable_ = false; |
---|
33 | } |
---|
34 | |
---|
35 | template <class T, class U> |
---|
36 | void BufferedLinearInterpolator1D<T, U>::setX(T *x, unsigned int n) |
---|
37 | { |
---|
38 | Interpolator1D<T, U>::setX(x, n); |
---|
39 | reusable_ = false; |
---|
40 | } |
---|
41 | |
---|
42 | template <class T, class U> |
---|
43 | U BufferedLinearInterpolator1D<T, U>::interpolate(T x) |
---|
44 | { |
---|
45 | assert(this->isready()); |
---|
46 | if (this->n_ == 1) |
---|
47 | return this->y_[0]; |
---|
48 | |
---|
49 | unsigned int i; |
---|
50 | bool b = (reusable_ && x == xold_); |
---|
51 | if (b) { |
---|
52 | i = prev_; |
---|
53 | } |
---|
54 | else { |
---|
55 | i = this->locator_->locate(x); |
---|
56 | prev_ = i; |
---|
57 | xold_ = x; |
---|
58 | } |
---|
59 | |
---|
60 | // do not perform extrapolation |
---|
61 | if (i == 0) { |
---|
62 | return this->y_[i]; |
---|
63 | } |
---|
64 | else if (i == this->n_) { |
---|
65 | return this->y_[i-1]; |
---|
66 | } |
---|
67 | |
---|
68 | // linear interpolation |
---|
69 | if (!b) |
---|
70 | factor_ = (x - this->x_[i-1]) / (this->x_[i] - this->x_[i-1]); |
---|
71 | U y = this->y_[i-1] + (this->y_[i] - this->y_[i-1]) * factor_; |
---|
72 | reusable_ = true; |
---|
73 | return y; |
---|
74 | } |
---|
75 | |
---|
76 | } |
---|