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<Double> where(2);
76 // where(0) = 3.452; where(1) = 6.1;
77 // Interpolate2D myInterp(Interpolate2D::LINEAR);
78 // Float result;
79 // Bool ok = myInterp.interp(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> Alternative approach: instantiate with an Array, take a block of
92 // vector locations, return a block of interpolation results
93 // </todo>
94 
95 
97  public:
98 
99  enum Method {
100 
101  // Nearest neighbour
103 
104  // Bilinear
106 
107  // Bicubic
109 
110  // Lanczos
112 
113  // Constructor
115 
116  // Copy constructor (copy semantics)
117  Interpolate2D(const Interpolate2D &other);
118 
119  // destructor
120  ~Interpolate2D();
121 
122  // Assignment operator (copy semantics)
123  Interpolate2D &operator=(const Interpolate2D &other);
124 
125  // Do one Float interpolation, supply Matrix and mask (True is good),
126  // and pixel coordinate. Returns False if coordinate out of range or data
127  // are masked. No shape integrity checking is done (see above).
128  // <group>
129  Bool interp (Float &result,
130  const Vector<Double> &where,
131  const Matrix<Float> &data) const;
132  Bool interp (Float &result,
133  const Vector<Double> &where,
134  const Matrix<Float> &data,
135  const Matrix<Bool> &mask) const;
136  // </group>
137 
138  // Do one Double interpolation, supply Matrix/Array and mask (True is good),
139  // and pixel coordinate. Returns False if coordinate out of range or data
140  // are masked. No shape integrity checking is done (see above).
141  // <group>
142  Bool interp (Double &result,
143  const Vector<Double> &where,
144  const Matrix<Double> &data) const;
145  Bool interp (Double &result,
146  const Vector<Double> &where,
147  const Matrix<Double> &data,
148  const Matrix<Bool> &mask) const;
149  // </group>
150  // Do two linear interpolations simultaneously. The second call is direct.
151  // The first call transfers to the second call. It is assumed that the
152  // structure (shape, steps) of the mask and data files are the same.
153  // <group>
154  Bool interp(Double &resultI, Double &resultJ,
155  const Vector<Double> &where,
156  const Matrix<Double> &dataI,
157  const Matrix<Double> &dataJ,
158  const Matrix<Bool> &mask) const;
159  template <typename T>
160  Bool interpLinear2(T &resultI, T &resultJ,
161  const Vector<Double> &where,
162  const Matrix<T> &dataI,
163  const Matrix<T> &dataJ,
164  const Matrix<Bool> &mask) const;
165  // </group>
166 
167  // Do one interpolation, supply boolean Matrix (True is good),
168  // and pixel coordinate. Returns False if coordinate
169  // out of range. The result is False if any data value in the interpolation
170  // grid are False (bad), else True. No shape integrity checking is done.
171  // <group>
172  Bool interp (Bool &result,
173  const Vector<Double> &where,
174  const Matrix<Bool> &data) const;
175  // </group>
176 
177  // Convert string ("nearest", "linear", "cubic", "lanczos") to interpolation
178  // method. The match is case insensitive.
179  static Interpolate2D::Method stringToMethod(const String &method);
180 
181  private:
182 
183  // Are any of the mask pixels bad ? Returns False if no mask.
184  Bool anyBadMaskPixels (const Matrix<Bool>* &mask, Int i1, Int i2,
185  Int j1, Int j2) const;
186 
187  // nearest neighbour interpolation
188  template <typename T>
189  Bool interpNearest(T &result, const Vector<Double> &where,
190  const Matrix<T> &data,
191  const Matrix<Bool>* &maskPtr) const;
192  Bool interpNearestBool(Bool &result, const Vector<Double> &where,
193  const Matrix<Bool> &data) const;
194 
195  // bi-linear interpolation
196  template <typename T>
197  Bool interpLinear(T &result, const Vector<Double> &where,
198  const Matrix<T> &data,
199  const Matrix<Bool>* &maskPtr) const;
200  Bool interpLinearBool(Bool &result, const Vector<Double> &where,
201  const Matrix<Bool> &data) const;
202 
203  // bi-cubic interpolation
204  template <typename T>
205  Bool interpCubic(T &result, const Vector<Double> &where,
206  const Matrix<T> &data,
207  const Matrix<Bool>* &maskPtr) const;
208  Bool interpCubicBool(Bool &result, const Vector<Double> &where,
209  const Matrix<Bool> &data) const;
210 
211  // Lanczos interpolation
212  template <typename T>
213  Bool interpLanczos(T &result, const Vector<Double> &where,
214  const Matrix<T> &data,
215  const Matrix<Bool>* &maskPtr) const;
216  Bool interpLanczosBool(Bool &result, const Vector<Double> &where,
217  const Matrix<Bool> &data) const;
218  // Lanczos interpolation: helper functions
219  template <typename T>
220  T sinc(const T x) const;
221  template <typename T>
222  T L(const T x, const Int a) const;
223 
224  // helping routine from numerical recipes
225  void bcucof (Double c[4][4], const Double y[4],
226  const Double y1[4],
227  const Double y2[4], const Double y12[4]) const;
228  //
229 
230  // Typedefs for function pointers
231  typedef Bool(Interpolate2D::*FuncPtrFloat)
232  (Float &result,
233  const Vector<Double> &where,
234  const Matrix<Float> &data,
235  const Matrix<Bool>* &maskPtr) const;
236  typedef Bool(Interpolate2D::*FuncPtrDouble)
237  (Double &result,
238  const Vector<Double> &where,
239  const Matrix<Double> &data,
240  const Matrix<Bool>* &maskPtr) const;
241  typedef Bool(Interpolate2D::*FuncPtrBool)
242  (Bool &result,
243  const Vector<Double> &where,
244  const Matrix<Bool> &data) const;
245  //
249 
250 };
251 
252 
253 } //# NAMESPACE CASACORE - END
254 
255 #ifndef CASACORE_NO_AUTO_TEMPLATES
256 #include <casacore/scimath/Mathematics/Interpolate2D2.tcc>
257 #endif //# CASACORE_NO_AUTO_TEMPLATES
258 #endif
259 
int Int
Definition: aipstype.h:50
Bool(Interpolate2D::* FuncPtrFloat)(Float &result, const Vector< Double > &where, const Matrix< Float > &data, const Matrix< Bool > *&maskPtr) const
Typedefs for function pointers.
T L(const T x, const Int a) const
static Interpolate2D::Method stringToMethod(const String &method)
Convert string ("nearest", "linear", "cubic", "lanczos") to interpolation method. ...
FuncPtrDouble itsFuncPtrDouble
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
Bool interpNearestBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
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.
T sinc(const T x) const
Lanczos interpolation: helper functions.
Bool interpCubicBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
FuncPtrFloat itsFuncPtrFloat
Bool interpLinear(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
bi-linear interpolation
Interpolate2D & operator=(const Interpolate2D &other)
Assignment operator (copy semantics)
Interpolate2D(Interpolate2D::Method method=Interpolate2D::LINEAR)
Constructor.
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(Interpolate2D::* FuncPtrDouble)(Double &result, const Vector< Double > &where, const Matrix< Double > &data, const Matrix< Bool > *&maskPtr) const
Bool(Interpolate2D::* FuncPtrBool)(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
double Double
Definition: aipstype.h:55
Bool interpLinear2(T &resultI, T &resultJ, const Vector< Double > &where, const Matrix< T > &dataI, const Matrix< T > &dataJ, const Matrix< Bool > &mask) const
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
float Float
Definition: aipstype.h:54
~Interpolate2D()
destructor
Bool interpCubic(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
bi-cubic interpolation
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 interpLanczosBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
Bool interpLinearBool(Bool &result, const Vector< Double > &where, const Matrix< Bool > &data) const
Bool interpLanczos(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
Lanczos interpolation.
const Double c
Fundamental physical constants (SI units):
A two dimension interpolator for Matrices or Arrays.
Definition: Interpolate2D.h:96
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Bool interpNearest(T &result, const Vector< Double > &where, const Matrix< T > &data, const Matrix< Bool > *&maskPtr) const
nearest neighbour interpolation
this file contains all the compiler specific defines
Definition: mainpage.dox:28