1 | // |
---|
2 | // C++ Implementation: Interpolator1D |
---|
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/Arrays/Vector.h> |
---|
15 | #include <casa/Exceptions/Error.h> |
---|
16 | #include <casa/Utilities/Assert.h> |
---|
17 | |
---|
18 | #include "Interpolator1D.h" |
---|
19 | #include "Locator.h" |
---|
20 | #include "BisectionLocator.h" |
---|
21 | #include "HuntLocator.h" |
---|
22 | |
---|
23 | using namespace casa; |
---|
24 | |
---|
25 | namespace asap { |
---|
26 | |
---|
27 | template <class T, class U> Interpolator1D<T, U>::Interpolator1D() |
---|
28 | : order_(1), |
---|
29 | n_(0), |
---|
30 | x_(0), |
---|
31 | y_(0), |
---|
32 | locator_(0) |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | template <class T, class U> Interpolator1D<T, U>::~Interpolator1D() |
---|
37 | { |
---|
38 | if (locator_) |
---|
39 | delete locator_; |
---|
40 | } |
---|
41 | |
---|
42 | template <class T, class U> |
---|
43 | void Interpolator1D<T, U>::setData(T *x, U *y, unsigned int n) |
---|
44 | { |
---|
45 | x_ = x; |
---|
46 | y_ = y; |
---|
47 | n_ = n; |
---|
48 | createLocator(); |
---|
49 | locator_->set(x, n); |
---|
50 | } |
---|
51 | |
---|
52 | template <class T, class U> |
---|
53 | void Interpolator1D<T, U>::setX(T *x, unsigned int n) |
---|
54 | { |
---|
55 | //assert(n_ == 0 || n_ == n); |
---|
56 | casa::assert_<casa::AipsError>(n_ == 0 || n_ == n, "length mismatch in data."); |
---|
57 | x_ = x; |
---|
58 | n_ = n; |
---|
59 | createLocator(); |
---|
60 | locator_->set(x, n); |
---|
61 | } |
---|
62 | |
---|
63 | template <class T, class U> |
---|
64 | void Interpolator1D<T, U>::setY(U *y, unsigned int n) |
---|
65 | { |
---|
66 | //assert(n_ == 0 || n_ == n); |
---|
67 | casa::assert_<casa::AipsError>(n_ == 0 || n_ == n, "length mismatch in data."); |
---|
68 | y_ = y; |
---|
69 | n_ = n; |
---|
70 | } |
---|
71 | |
---|
72 | template <class T, class U> void Interpolator1D<T, U>::reset() |
---|
73 | { |
---|
74 | n_ = 0; |
---|
75 | x_ = 0; |
---|
76 | y_ = 0; |
---|
77 | if (locator_) { |
---|
78 | delete locator_; |
---|
79 | locator_ = 0; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | template <class T, class U> bool Interpolator1D<T, U>::isready() |
---|
84 | { |
---|
85 | return (n_ > 0 && x_ != 0 && y_ != 0); |
---|
86 | } |
---|
87 | |
---|
88 | template <class T, class U> unsigned int Interpolator1D<T, U>::locate(T x) |
---|
89 | { |
---|
90 | return locator_->locate(x); |
---|
91 | } |
---|
92 | |
---|
93 | template <class T, class U> void Interpolator1D<T, U>::createLocator() |
---|
94 | { |
---|
95 | if (!locator_) { |
---|
96 | if (n_ > 1000) |
---|
97 | locator_ = new HuntLocator<T>(); |
---|
98 | else |
---|
99 | locator_ = new BisectionLocator<T>(); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|