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 <casa/Exceptions/Error.h> |
---|
15 | #include <casa/Utilities/Assert.h> |
---|
16 | |
---|
17 | #include "BufferedLinearInterpolator1D.h" |
---|
18 | |
---|
19 | namespace asap { |
---|
20 | |
---|
21 | template <class T, class U> |
---|
22 | BufferedLinearInterpolator1D<T, U>::BufferedLinearInterpolator1D() |
---|
23 | : Interpolator1D<T, U>(), |
---|
24 | reusable_(false) |
---|
25 | {} |
---|
26 | |
---|
27 | template <class T, class U> |
---|
28 | BufferedLinearInterpolator1D<T, U>::~BufferedLinearInterpolator1D() |
---|
29 | {} |
---|
30 | |
---|
31 | template <class T, class U> |
---|
32 | void BufferedLinearInterpolator1D<T, U>::setData(T *x, U *y, unsigned int n) |
---|
33 | { |
---|
34 | Interpolator1D<T, U>::setData(x, y, n); |
---|
35 | reusable_ = false; |
---|
36 | } |
---|
37 | |
---|
38 | template <class T, class U> |
---|
39 | void BufferedLinearInterpolator1D<T, U>::setX(T *x, unsigned int n) |
---|
40 | { |
---|
41 | Interpolator1D<T, U>::setX(x, n); |
---|
42 | reusable_ = false; |
---|
43 | } |
---|
44 | |
---|
45 | template <class T, class U> |
---|
46 | U BufferedLinearInterpolator1D<T, U>::interpolate(T x) |
---|
47 | { |
---|
48 | //assert(this->isready()); |
---|
49 | assert_<casa::AipsError>(this->isready(), "object is not ready to process."); |
---|
50 | if (this->n_ == 1) |
---|
51 | return this->y_[0]; |
---|
52 | |
---|
53 | unsigned int i; |
---|
54 | bool b = (reusable_ && x == xold_); |
---|
55 | if (b) { |
---|
56 | i = prev_; |
---|
57 | } |
---|
58 | else { |
---|
59 | i = this->locator_->locate(x); |
---|
60 | prev_ = i; |
---|
61 | xold_ = x; |
---|
62 | } |
---|
63 | |
---|
64 | // do not perform extrapolation |
---|
65 | if (i == 0) { |
---|
66 | return this->y_[i]; |
---|
67 | } |
---|
68 | else if (i == this->n_) { |
---|
69 | return this->y_[i-1]; |
---|
70 | } |
---|
71 | |
---|
72 | // linear interpolation |
---|
73 | if (!b) |
---|
74 | factor_ = (x - this->x_[i-1]) / (this->x_[i] - this->x_[i-1]); |
---|
75 | U y = this->y_[i-1] + (this->y_[i] - this->y_[i-1]) * factor_; |
---|
76 | reusable_ = true; |
---|
77 | return y; |
---|
78 | } |
---|
79 | |
---|
80 | } |
---|