casacore
Interpolate2D.h
Go to the documentation of this file.
1 //# Interpolate2D.h: this defines the Interpolate2D class
2 //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2004
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef SCIMATH_INTERPOLATE2D_H
29 #define SCIMATH_INTERPOLATE2D_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 
34 namespace casacore { //# NAMESPACE CASACORE - BEGIN
35 
36 //# Forward declarations
37 template <typename T> class Vector;
38 template <typename T> class Matrix;
39 class String;
40 
41 // <summary>
42 // A two dimension interpolator for Matrices or Arrays
43 // </summary>
44 
45 // <use visibility=export>
46 
47 // <reviewed reviewer="wbrouw" date="2004/05/26" tests="" demos="">
48 // </reviewed>
49 
50 // <prerequisite>
51 // <li> <linkto class=Array>Arrays</linkto>
52 // </prerequisite>
53 //
54 // <etymology>
55 // This class is called Interpolate2D because it does 2 dimensional interpolations
56 // </etymology>
57 //
58 // <synopsis>
59 // Given a regular Array or Matrix and a vector of pixel
60 // coordinates, interpolate the values of that array/matrix onto those
61 // pixel coordinates.
62 //
63 // Absolutely no checking of the consistency of the input data
64 // is done in order to preserve maximum speed. The coordinate vector
65 // *must* have at least 2 elements (others will be ignored). If
66 // you supply data and mask, those arrays *must* be the same shape.
67 // Failure to follow these rules will result in your program
68 // crashing.
69 // </synopsis>
70 //
71 // <example>
72 // <srcblock>
73 //
74 // Matrix<Float> matt(10,10);
75 // Vector<Float> where(2);
76 // where(0) = 3.452; where(1) = 6.1;
77 // Interpolate2D myInterp(Interpolate2D::LINEAR);
78 // Float result;
79 // Bool ok = myInterp(result, where, matt);
80 //
81 // </srcblock>
82 // </example>
83 //
84 // <motivation>
85 // 2-D interpolation is required in geometry transformation routines
86 // such as in ImageRegrid.
87 // </motivation>
88 //
89 //
90 // <todo asof="1998/08/02">
91 // <li> Now that there are float/double/bool versions, the class should
92 // be templated and specialized versions made as needed. The
93 // code duplucation in the Float/Double versions is pretty awful presently.
94 // <li> Alternative approach: instantiate with an Array, take a block of
95 // vector locations, return a block of interpolation results
96 // </todo>
97 
98 
100  public:
101 
102  enum Method {
103 
104  // Nearest neighbour
106 
107  // Bilinear
109 
110  // Bicubic
112 
113  // Lanczos
115 
116  // Constructor
118 
119  // Copy constructor (copy semantics)
120  Interpolate2D(const Interpolate2D &other);
121 
122  // destructor
123  ~Interpolate2D();
124 
125  // Assignment operator (copy semantics)
126  Interpolate2D &operator=(const Interpolate2D &other);
127 
128  // Do one Float interpolation, supply Matrix and mask (True is good),
129  // and pixel coordinate. Returns False if coordinate out of range or data
130  // are masked. No shape integrity checking is done (see above).
131  // <group>
132  Bool interp (Float &result,
133  const Vector<Double> &where,
134  const Matrix<Float> &data) const;
135  Bool interp (Float &result,
136  const Vector<Double> &where,
137  const Matrix<Float> &data,
138  const Matrix<Bool> &mask) const;
139  // </group>
140 
141  // Do one Double interpolation, supply Matrix/Array and mask (True is good),
142  // and pixel coordinate. Returns False if coordinate out of range or data
143  // are masked. No shape integrity checking is done (see above).
144  // <group>
145  Bool interp (Double &result,
146  const Vector<Double> &where,
147  const Matrix<Double> &data) const;
148  Bool interp (Double &result,
149  const Vector<Double> &where,
150  const Matrix<Double> &data,
151  const Matrix<Bool> &mask) const;
152  // </group>
153  // Do two linear interpolations simultaneously. The second call is direct.
154  // The first call transfers to the second call. It is assumed that the
155  // structure (shape, steps) of the mask and data files are the same.
156  // <group>
157  Bool interp(Double &resultI, Double &resultJ,
158  const Vector<Double> &where,
159  const Matrix<Double> &dataI,
160  const Matrix<Double> &dataJ,
161  const Matrix<Bool> &mask) const;
162  template <typename T>
163  Bool interpLinear2(T &resultI, T &resultJ,
164  const Vector<Double> &where,
165  const Matrix<T> &dataI,
166  const Matrix<T> &dataJ,
167  const Matrix<Bool> &mask) const;
168  // </group>
169 
170  // Do one interpolation, supply boolean Matrix (True is good),
171  // and pixel coordinate. Returns False if coordinate
172  // out of range. The result is False if any data value in the interpolation
173  // grid are False (bad), else True. No shape integrity checking is done.
174  // <group>
175  Bool interp (Bool &result,
176  const Vector<Double> &where,
177  const Matrix<Bool> &data) const;
178  // </group>
179 
180  // Recover interpolation method
182 
183  // Convert string ("nearest", "linear", "cubic") to interpolation method
184  // Minimum match will do.
185  static Interpolate2D::Method stringToMethod(const String &method);
186 
187  private:
188 
189  // Are any of the mask pixels bad ? Returns False if no mask.
190  Bool anyBadMaskPixels (const Matrix<Bool>* &mask, Int i1, Int i2,
191  Int j1, Int j2) const;
192 
193  // nearest neighbour interpolation
194  template <typename T>
195  Bool interpNearest(T &result, const Vector<Double> &where,
196  const Matrix<T> &data,
197  const Matrix<Bool>* &maskPtr) const;
198  Bool interpNearestBool(Bool &result, const Vector<Double> &where,
199  const Matrix<Bool> &data) const;
200 
201  // bi-linear interpolation
202  template <typename T>
203  Bool interpLinear(T &result, const Vector<Double> &where,
204  const Matrix<T> &data,
205  const Matrix<Bool>* &maskPtr) const;
206  Bool interpLinearBool(Bool &result, const Vector<Double> &where,
207  const Matrix<Bool> &data) const;
208 
209  // bi-cubic interpolation
210  template <typename T>
211  Bool interpCubic(T &result, const Vector<Double> &where,
212  const Matrix<T> &data,
213  const Matrix<Bool>* &maskPtr) const;
214  Bool interpCubicBool(Bool &result, const Vector<Double> &where,
215  const Matrix<Bool> &data) const;
216 
217  // Lanczos interpolation
218  template <typename T>
219  Bool interpLanczos(T &result, const Vector<Double> &where,
220  const Matrix<T> &data,
221  const Matrix<Bool>* &maskPtr) const;
222  Bool interpLanczosBool(Bool &result, const Vector<Double> &where,
223  const Matrix<Bool> &data) const;
224  // Lanczos interpolation: helper functions
225  template <typename T>
226  T sinc(const T x) const;
227  template <typename T>
228  T L(const T x, const Int a) const;
229 
230  // helping routine from numerical recipes
231  void bcucof (Double c[4][4], const Double y[4],
232  const Double y1[4],
233  const Double y2[4], const Double y12[4]) const;
234  //
236 
237  // Typedefs for function pointers
238  typedef Bool(Interpolate2D::*FuncPtrFloat)
239  (Float &result,
240  const Vector<Double> &where,
241  const Matrix<Float> &data,
242  const Matrix<Bool>* &maskPtr) const;
243  typedef Bool(Interpolate2D::*FuncPtrDouble)
244  (Double &result,
245  const Vector<Double> &where,
246  const Matrix<Double> &data,
247  const Matrix<Bool>* &maskPtr) const;
248  typedef Bool(Interpolate2D::*FuncPtrBool)
249  (Bool &result,
250  const Vector<Double> &where,
251  const Matrix<Bool> &data) const;
252  //
256 
257 };
258 
259 
260 } //# NAMESPACE CASACORE - END
261 
262 #ifndef CASACORE_NO_AUTO_TEMPLATES
263 #include <casacore/scimath/Mathematics/Interpolate2D2.tcc>
264 #endif //# CASACORE_NO_AUTO_TEMPLATES
265 #endif
266 
int Int
Definition: aipstype.h:47
Bool interpLinear2(T &resultI, T &resultJ, const Vector< Double > &where, const Matrix< T > &dataI, const Matrix< T > &dataJ, const Matrix< Bool > &mask) const
Bool interpLinear(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
bi-linear interpolation
static Interpolate2D::Method stringToMethod(const String &method)
Convert string ("nearest", "linear", "cubic") to interpolation method Minimum match will do...
FuncPtrDouble itsFuncPtrDouble
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
Bool interpNearest(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
nearest neighbour interpolation
Bool anyBadMaskPixels(const Matrix< Bool > *&mask, Int i1, Int i2, Int j1, Int j2) const
Are any of the mask pixels bad ? Returns False if no mask.
Bool interpLanczos(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
Lanczos interpolation.
Bool interp(Float &result, const Vector< Double > &where, const Matrix< Float > &data) const
Do one Float interpolation, supply Matrix and mask (True is good), and pixel coordinate.
FuncPtrFloat itsFuncPtrFloat
Interpolate2D & operator=(const Interpolate2D &other)
Assignment operator (copy semantics)
Interpolate2D(Interpolate2D::Method method=Interpolate2D::LINEAR)
Constructor.
Bool(Interpolate2D::* FuncPtrDouble)(Double &result, const Vector< Double > &where, const Matrix< Double > &data, const Matrix< Bool > *&maskPtr) const
double Double
Definition: aipstype.h:52
void bcucof(Double c[4][4], const Double y[4], const Double y1[4], const Double y2[4], const Double y12[4]) const
helping routine from numerical recipes
Bool interpLinearBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
float Float
Definition: aipstype.h:51
Bool interpCubicBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
~Interpolate2D()
destructor
Bool(Interpolate2D::* FuncPtrFloat)(Float &result, const Vector< Double > &where, const Matrix< Float > &data, const Matrix< Bool > *&maskPtr) const
Typedefs for function pointers.
const Double c
Fundamental physical constants (SI units):
A two dimension interpolator for Matrices or Arrays.
Definition: Interpolate2D.h:99
Interpolate2D::Method itsMethod
String: the storage and methods of handling collections of characters.
Definition: String.h:223
T L(const T x, const Int a) const
Bool interpLanczosBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
Bool interpCubic(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
bi-cubic interpolation
Bool(Interpolate2D::* FuncPtrBool)(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
this file contains all the compiler specific defines
Definition: mainpage.dox:28
Bool interpNearestBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
T sinc(const T x) const
Lanczos interpolation: helper functions.
Method interpolationMethod() const
Recover interpolation method.