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$
00027 
00028 #ifndef SCIMATH_INTERPOLATE2D_H
00029 #define SCIMATH_INTERPOLATE2D_H
00030 
00031 //# Includes
00032 #include <casacore/casa/aips.h>
00033 
00034 namespace casacore { //# NAMESPACE CASACORE - 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     // Lanczos
00114     LANCZOS};
00115   
00116   // Constructor
00117   Interpolate2D(Interpolate2D::Method method=Interpolate2D::LINEAR);
00118   
00119   // Copy constructor (copy semantics)
00120   Interpolate2D(const Interpolate2D &other);
00121   
00122   // destructor
00123   ~Interpolate2D();
00124   
00125   // Assignment operator (copy semantics)
00126   Interpolate2D &operator=(const Interpolate2D &other);
00127   
00128   // Do one Float interpolation, supply Matrix and mask (True is good),
00129   // and pixel coordinate.  Returns False if coordinate out of range or data 
00130   // are masked.  No shape integrity checking is done (see above).
00131   // <group>
00132   Bool interp (Float &result, 
00133                const Vector<Double> &where,
00134                const Matrix<Float> &data) const;
00135   Bool interp (Float &result, 
00136                const Vector<Double> &where,
00137                const Matrix<Float> &data, 
00138                const Matrix<Bool> &mask) const;
00139   // </group>
00140   
00141   // Do one Double interpolation, supply Matrix/Array and mask (True is good),
00142   // and pixel coordinate.  Returns False if coordinate out of range or data 
00143   // are masked.  No shape integrity checking is done (see above).
00144   // <group>
00145   Bool interp (Double &result, 
00146                const Vector<Double> &where,
00147                const Matrix<Double> &data) const;
00148   Bool interp (Double &result, 
00149                const Vector<Double> &where,
00150                const Matrix<Double> &data, 
00151                const Matrix<Bool> &mask) const;
00152   // </group>
00153   // Do two linear interpolations simultaneously. The second call is direct.
00154   // The first call transfers to the second call. It is assumed that the
00155   // structure (shape, steps) of the mask and data files are the same.
00156   // <group>
00157   Bool interp(Double &resultI, Double &resultJ, 
00158               const Vector<Double> &where, 
00159               const Matrix<Double> &dataI,
00160               const Matrix<Double> &dataJ,
00161               const Matrix<Bool> &mask) const;
00162   template <typename T>
00163   Bool interpLinear2(T &resultI, T &resultJ, 
00164                      const Vector<Double> &where, 
00165                      const Matrix<T> &dataI,
00166                      const Matrix<T> &dataJ,
00167                      const Matrix<Bool> &mask) const;
00168   // </group>
00169   
00170   // Do one interpolation, supply boolean Matrix (True is good),
00171   // and pixel coordinate.  Returns False if coordinate
00172   // out of range. The result is False if any data value in the interpolation
00173   // grid are False (bad), else True.  No shape integrity checking is done.
00174   // <group>
00175   Bool  interp (Bool &result, 
00176                 const Vector<Double> &where,
00177                 const Matrix<Bool> &data) const;
00178   // </group>
00179   
00180   // Recover interpolation method
00181   Method interpolationMethod() const {return itsMethod;}
00182   
00183   // Convert string ("nearest", "linear", "cubic") to interpolation method
00184   // Minimum match will do.
00185   static Interpolate2D::Method stringToMethod(const String &method);
00186   
00187  private:
00188   
00189   // Are any of the mask pixels bad ? Returns False if no mask.
00190   Bool anyBadMaskPixels (const Matrix<Bool>* &mask, Int i1, Int i2,
00191                          Int j1, Int j2) const;
00192   
00193   // nearest neighbour interpolation
00194   template <typename T>
00195   Bool interpNearest(T &result, const Vector<Double> &where,
00196                      const Matrix<T> &data,
00197                      const Matrix<Bool>* &maskPtr) const;
00198   Bool interpNearestBool(Bool &result, const Vector<Double> &where,
00199                          const Matrix<Bool> &data) const;
00200 
00201   // bi-linear interpolation 
00202   template <typename T>
00203   Bool interpLinear(T &result, const Vector<Double> &where,
00204                     const Matrix<T> &data,
00205                     const Matrix<Bool>* &maskPtr) const;
00206   Bool interpLinearBool(Bool &result, const Vector<Double> &where,
00207                         const Matrix<Bool> &data) const;
00208   
00209   // bi-cubic interpolation
00210   template <typename T>
00211     Bool interpCubic(T &result, const Vector<Double> &where,
00212                      const Matrix<T> &data,
00213                      const Matrix<Bool>* &maskPtr) const;
00214   Bool interpCubicBool(Bool &result, const Vector<Double> &where,
00215                        const Matrix<Bool> &data) const;
00216 
00217   // Lanczos interpolation
00218   template <typename T>
00219   Bool interpLanczos(T &result, const Vector<Double> &where,
00220                      const Matrix<T> &data,
00221                      const Matrix<Bool>* &maskPtr) const;
00222   Bool interpLanczosBool(Bool &result, const Vector<Double> &where,
00223                        const Matrix<Bool> &data) const;
00224   // Lanczos interpolation: helper functions
00225   template <typename T>
00226   T sinc(const T x) const;
00227   template <typename T>
00228   T L(const T x, const Int a) const;
00229 
00230   // helping routine from numerical recipes
00231   void bcucof (Double c[4][4], const Double y[4],
00232                const Double y1[4], 
00233                const Double y2[4], const Double y12[4]) const;
00234   //
00235   Interpolate2D::Method itsMethod;
00236   
00237   // Typedefs for function pointers
00238   typedef Bool(Interpolate2D::*FuncPtrFloat)
00239     (Float &result, 
00240      const Vector<Double> &where, 
00241      const Matrix<Float> &data,
00242      const Matrix<Bool>* &maskPtr) const;
00243   typedef Bool(Interpolate2D::*FuncPtrDouble)
00244     (Double &result, 
00245      const Vector<Double> &where, 
00246      const Matrix<Double> &data,
00247      const Matrix<Bool>* &maskPtr) const;
00248   typedef Bool(Interpolate2D::*FuncPtrBool)
00249     (Bool &result, 
00250      const Vector<Double> &where, 
00251      const Matrix<Bool> &data) const;
00252   //
00253   FuncPtrFloat itsFuncPtrFloat;
00254   FuncPtrDouble itsFuncPtrDouble;
00255   FuncPtrBool itsFuncPtrBool;
00256 
00257 };
00258 
00259 
00260 } //# NAMESPACE CASACORE - END
00261 
00262 #ifndef CASACORE_NO_AUTO_TEMPLATES
00263 #include <casacore/scimath/Mathematics/Interpolate2D2.tcc>
00264 #endif //# CASACORE_NO_AUTO_TEMPLATES
00265 #endif
00266 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1