casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Interpolate2D.h
Go to the documentation of this file.
00001 //# Interpolate2D.h: this defines the Interpolate2D class
00002 //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2004
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: Interpolate2D.h 21024 2011-03-01 11:46:18Z gervandiepen $
00027 
00028 #ifndef SCIMATH_INTERPOLATE2D_H
00029 #define SCIMATH_INTERPOLATE2D_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 
00034 namespace casa { //# NAMESPACE CASA - BEGIN
00035 
00036 //# Forward declarations
00037 template <typename T> class Vector;
00038 template <typename T> class Matrix;
00039 class String;
00040 
00041 // <summary>
00042 // A two dimension interpolator for Matrices or Arrays
00043 // </summary>
00044 
00045 // <use visibility=export>
00046 
00047 // <reviewed reviewer="wbrouw" date="2004/05/26" tests="" demos="">
00048 // </reviewed>
00049 
00050 // <prerequisite> 
00051 // <li> <linkto class=Array>Arrays</linkto>
00052 // </prerequisite>
00053 //
00054 // <etymology>
00055 // This class is called Interpolate2D because it does 2 dimensional interpolations
00056 // </etymology>
00057 //
00058 // <synopsis>
00059 // Given a regular Array or Matrix and a vector of pixel
00060 // coordinates, interpolate the values of that array/matrix onto those
00061 // pixel coordinates.
00062 //
00063 // Absolutely no checking of the consistency of the input data
00064 // is done in order to preserve maximum speed.   The coordinate vector
00065 // *must* have at least 2 elements (others will be ignored). If 
00066 // you supply data and mask, those arrays *must* be the same shape.
00067 // Failure to follow these rules will result in your program 
00068 // crashing.
00069 // </synopsis>
00070 //
00071 // <example>
00072 // <srcblock>
00073 //
00074 // Matrix<Float> matt(10,10);
00075 // Vector<Float> where(2);  
00076 // where(0) = 3.452;  where(1) = 6.1;
00077 // Interpolate2D myInterp(Interpolate2D::LINEAR);
00078 // Float result;
00079 // Bool ok = myInterp(result, where, matt);
00080 //
00081 // </srcblock> 
00082 // </example>
00083 //
00084 // <motivation>
00085 // 2-D interpolation is required in geometry transformation routines
00086 // such as in ImageRegrid.
00087 // </motivation>
00088 //
00089 //
00090 // <todo asof="1998/08/02">
00091 //   <li> Now that there are float/double/bool versions, the class should
00092 //        be templated and specialized versions made as needed. The
00093 //        code duplucation in the Float/Double versions is pretty awful presently.
00094 //   <li> Alternative approach: instantiate with an Array, take a block of
00095 //        vector locations, return a block of interpolation results
00096 // </todo>
00097 
00098 
00099 class Interpolate2D {
00100  public:
00101 
00102   enum Method {
00103 
00104     // Nearest neighbour
00105     NEAREST,  
00106     
00107     // Bilinear 
00108     LINEAR, 
00109     
00110     // Bicubic 
00111     CUBIC};
00112   
00113   // Constructor
00114   Interpolate2D(Interpolate2D::Method method=Interpolate2D::LINEAR);
00115   
00116   // Copy constructor (copy semantics)
00117   Interpolate2D(const Interpolate2D &other);
00118   
00119   // destructor
00120   ~Interpolate2D();
00121   
00122   // Assignment operator (copy semantics)
00123   Interpolate2D &operator=(const Interpolate2D &other);
00124   
00125   // Do one Float interpolation, supply Matrix and mask (True is good),
00126   // and pixel coordinate.  Returns False if coordinate out of range or data 
00127   // are masked.  No shape integrity checking is done (see above).
00128   // <group>
00129   Bool interp (Float &result, 
00130                const Vector<Double> &where,
00131                const Matrix<Float> &data) const;
00132   Bool interp (Float &result, 
00133                const Vector<Double> &where,
00134                const Matrix<Float> &data, 
00135                const Matrix<Bool> &mask) const;
00136   // </group>
00137   
00138   // Do one Double interpolation, supply Matrix/Array and mask (True is good),
00139   // and pixel coordinate.  Returns False if coordinate out of range or data 
00140   // are masked.  No shape integrity checking is done (see above).
00141   // <group>
00142   Bool interp (Double &result, 
00143                const Vector<Double> &where,
00144                const Matrix<Double> &data) const;
00145   Bool interp (Double &result, 
00146                const Vector<Double> &where,
00147                const Matrix<Double> &data, 
00148                const Matrix<Bool> &mask) const;
00149   // </group>
00150   // Do two linear interpolations simultaneously. The second call is direct.
00151   // The first call transfers to the second call. It is assumed that the
00152   // structure (shape, steps) of the mask and data files are the same.
00153   // <group>
00154   Bool interp(Double &resultI, Double &resultJ, 
00155               const Vector<Double> &where, 
00156               const Matrix<Double> &dataI,
00157               const Matrix<Double> &dataJ,
00158               const Matrix<Bool> &mask) const;
00159   template <typename T>
00160   Bool interpLinear2(T &resultI, T &resultJ, 
00161                      const Vector<Double> &where, 
00162                      const Matrix<T> &dataI,
00163                      const Matrix<T> &dataJ,
00164                      const Matrix<Bool> &mask) const;
00165   // </group>
00166   
00167   // Do one interpolation, supply boolean Matrix (True is good),
00168   // and pixel coordinate.  Returns False if coordinate
00169   // out of range. The result is False if any data value in the interpolation
00170   // grid are False (bad), else True.  No shape integrity checking is done.
00171   // <group>
00172   Bool  interp (Bool &result, 
00173                 const Vector<Double> &where,
00174                 const Matrix<Bool> &data) const;
00175   // </group>
00176   
00177   // Recover interpolation method
00178   Method interpolationMethod() const {return itsMethod;}
00179   
00180   // Convert string ("nearest", "linear", "cubic") to interpolation method
00181   // Minimum match will do.
00182   static Interpolate2D::Method stringToMethod(const String &method);
00183   
00184  private:
00185   
00186   // Are any of the mask pixels bad ? Returns False if no mask.
00187   Bool anyBadMaskPixels (const Matrix<Bool>* &mask, Int i1, Int i2,
00188                          Int j1, Int j2) const;
00189   
00190   // nearest neighbour interpolation
00191   template <typename T>
00192   Bool interpNearest(T &result, const Vector<Double> &where,
00193                      const Matrix<T> &data,
00194                      const Matrix<Bool>* &maskPtr) const;
00195   Bool interpNearestBool(Bool &result, const Vector<Double> &where,
00196                          const Matrix<Bool> &data) const;
00197 
00198   // bi-linear interpolation 
00199   template <typename T>
00200   Bool interpLinear(T &result, const Vector<Double> &where,
00201                     const Matrix<T> &data,
00202                     const Matrix<Bool>* &maskPtr) const;
00203   Bool interpLinearBool(Bool &result, const Vector<Double> &where,
00204                         const Matrix<Bool> &data) const;
00205   
00206   // bi-cubic interpolation
00207   template <typename T>
00208     Bool interpCubic(T &result, const Vector<Double> &where,
00209                      const Matrix<T> &data,
00210                      const Matrix<Bool>* &maskPtr) const;
00211   Bool interpCubicBool(Bool &result, const Vector<Double> &where,
00212                        const Matrix<Bool> &data) const;
00213   // helping routine from numerical recipes
00214   void bcucof (Double c[4][4], const Double y[4],
00215                const Double y1[4], 
00216                const Double y2[4], const Double y12[4]) const;
00217   //
00218   Interpolate2D::Method itsMethod;
00219   
00220   // Typedefs for function pointers
00221   typedef Bool(Interpolate2D::*FuncPtrFloat)
00222     (Float &result, 
00223      const Vector<Double> &where, 
00224      const Matrix<Float> &data,
00225      const Matrix<Bool>* &maskPtr) const;
00226   typedef Bool(Interpolate2D::*FuncPtrDouble)
00227     (Double &result, 
00228      const Vector<Double> &where, 
00229      const Matrix<Double> &data,
00230      const Matrix<Bool>* &maskPtr) const;
00231   typedef Bool(Interpolate2D::*FuncPtrBool)
00232     (Bool &result, 
00233      const Vector<Double> &where, 
00234      const Matrix<Bool> &data) const;
00235   //
00236   FuncPtrFloat itsFuncPtrFloat;
00237   FuncPtrDouble itsFuncPtrDouble;
00238   FuncPtrBool itsFuncPtrBool;
00239 
00240 };
00241 
00242 
00243 } //# NAMESPACE CASA - END
00244 
00245 #ifndef CASACORE_NO_AUTO_TEMPLATES
00246 #include <scimath/Mathematics/Interpolate2D2.tcc>
00247 #endif //# CASACORE_NO_AUTO_TEMPLATES
00248 #endif
00249