casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Gaussian2DParam.h
Go to the documentation of this file.
00001 //# Gaussian2DParam.h: Parameter handling for 2 dimensional Gaussian class
00002 //# Copyright (C) 2001,2002,2003
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 //#
00027 //# $Id: Gaussian2DParam.h 21024 2011-03-01 11:46:18Z gervandiepen $
00028 
00029 #ifndef SCIMATH_GAUSSIAN2DPARAM_H
00030 #define SCIMATH_GAUSSIAN2DPARAM_H
00031 
00032 #include <casa/aips.h>
00033 #include <scimath/Functionals/Function.h>
00034 #include <casa/BasicSL/String.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Forward declarations
00039 template<class T> class Vector;
00040 
00041 // <summary> Parameter handling for 2 dimensional Gaussian class
00042 // </summary>
00043 
00044 // <use visibility=local>
00045 
00046 // <reviewed reviewer="mwieringa" date="1996/10/d24" tests="tGaussian2D">
00047 // </reviewed>
00048 
00049 // <prerequisite>
00050 //   <li> <linkto class="FunctionParam">FunctionParam</linkto> class
00051 //   <li> <linkto class="Function">Function</linkto> class
00052 // </prerequisite>
00053 
00054 // <etymology> 
00055 // A 2-dimensional Gaussian's parameters.
00056 // </etymology>
00057 
00058 // <synopsis>
00059 
00060 // A <src>Gaussian2D</src> is described by a height, center, and width,
00061 // and position angle.
00062 
00063 // The width of the Gaussian (for the constructors or the <src> setWidth
00064 // </src> function) is always specified in terms of the full width at half
00065 // maximum (FWHM). The major axis is parallel with the y axis when the
00066 // position angle is zero. The major axis will always have a larger width
00067 // than the minor axis. 
00068 //
00069 // It is not possible to set the width of the major axis (using the <src>
00070 // setMajorAxis </src> function) smaller than the width of the current minor
00071 // axis. Similarly it is not possible to set the width of the minor axis
00072 // (using the <src> setMinorAxis </src> function) to be larger than the
00073 // current major axis. Exceptions are thrown if these rules are violated or
00074 // if either the major or minor axis is set to a non-positive width. To
00075 // set both axis in one hit use the <src> setWidth </src> function. All
00076 // these restrictions can be overcome when the parameters interface is used
00077 // (see below).
00078 //
00079 // The position angle is the angle between the y axis and the major axis and
00080 // is measured counter-clockwise, so a position angle of 45 degrees rotates
00081 // the major axis to the line where <src>y=-x</src>.
00082 // The position angle is always
00083 // specified and returned in radians. When using the <src> setPA </src>
00084 // function its value must be between -2pi and + 2pi, and the returned value
00085 // from the <src> pa </src> function will always be a value between 0 and
00086 // pi. 
00087 //
00088 // The axial ratio can be used as an alternative to specifying the width of
00089 // the minor axis. It is the ratio between the minor and major axis
00090 // widths. The axial ratio is constrained to be between zero and one, and
00091 // specifying something different (using setAxialRatio) will throw an
00092 // exception.
00093 //
00094 // The peak height of the Gaussian can be specified at construction time or
00095 // by using the <src> setHeight </src> function. Alternatively the <src>
00096 // setFlux </src> function can be used to implicitly set the peak height by
00097 // specifying the integrated area under the Gaussian. The height (or flux)
00098 // can be positive, negative or zero, as this class makes no assumptions on
00099 // what quantity the height represents. 
00100 //
00101 // <note role=tip> Changing the width of the Gaussian will not affect
00102 // its peak height but will change its flux. So you should always set the
00103 // width before setting the flux. </note>
00104 //
00105 // The parameter interface (see 
00106 // <linkto class="FunctionParam">FunctionParam</linkto> class), 
00107 // is used to provide an interface to the
00108 // <linkto module="Fitting"> Fitting </linkto> classes. 
00109 //
00110 // There are 6 parameters that are used to describe the Gaussian:
00111 // <ol>
00112 // <li> The height of the Gaussian. This is identical to the value 
00113 //      returned using the <src> height </src> member function.
00114 // <li> The center of the Gaussian in the x direction. This is identical to
00115 //      the value returned using the <src> xCenter </src> member function. 
00116 // <li> The center of the Gaussian in the y direction. This is identical to
00117 //      the value returned using the <src> yCenter </src> member function. 
00118 // <li> The width (FWHM) of the Gaussian on one axis. Initially this will be
00119 //      the major axis, but if the parameters are adjusted by a Fitting
00120 //      class, it may become the axis with the smaller width. To aid
00121 //      convergence of the non-linear fitting routines this parameter is
00122 //      allowed to be negative. This does not affect the shape of the
00123 //      Gaussian as the squares of the widths are used when evaluating the
00124 //      function.
00125 // <li> A modified axial ratio. This parameter is the ratio of the width on
00126 //      the 'other' axis (which initially is the minor axis) and axis given
00127 //      by parameter 4. Because these internal widths are allowed to be
00128 //      negative and because there is no constraints on which axis is the
00129 //      larger one the modified axial ratio is not constrained to be between
00130 //      zero and one.
00131 // <li> The rotation angle. This represents the angle (in radians) between
00132 //      the axis used by parameter 4, and the y axis, measured
00133 //      counterclockwise. If parameter 4 represents the major axis width
00134 //      then this parameter will be identical to the position angle,
00135 //      otherwise it will be different by 90 degrees. The tight constraints
00136 //      on the value of the rotation angle enforced by the setPA() function
00137 //      are relaxed so that any value between -6000 and 6000 is allowed. It
00138 //      is still interpreted in radians. 
00139 // </ol>
00140 //
00141 // An enumeration for the <src>HEIGHT</src>, <src>XCENTER</src>,
00142 // <src>YCENTER</src>, <src>YWIDTH</src>, <src>RATIO</src>, <src>PANGLE</src>
00143 // parameter index is provided, enabling the setting
00144 // and reading of parameters with the <src>[]</src> operator. The 
00145 // <src>mask()</src> methods can be used to check and set the parameter masks.
00146 //
00147 // This class is in general used implicitly by the <src>Gaussian2D</src>
00148 // class only.
00149 //
00150 // <note role=tip>
00151 // Other points to bear in mind when fitting this class to measured data
00152 // are:
00153 // <ul>
00154 // <li> If you need to fit a circular Gaussian to data you MUST set the
00155 //      axial ratio to one, and mask the position angle and axial ratio
00156 //      parameters. This avoids rank deficiency in the fitting routines as
00157 //      the position angle is meaningless when the major and minor axis are
00158 //      equal.
00159 // <li> If fitting an elliptical Gaussian your initial model should not be a
00160 //      circular Gaussian. 
00161 // </ul>
00162 // </note>
00163 //
00164 // </synopsis>
00165 
00166 // <example>
00167 // <srcblock> 
00168 // Gaussian2D<Double> g(10.0, 0.0, 0.0, 2.0, 1.0, 0.0);
00169 // Vector<Double> x(2);
00170 // x(0) = 1.0; x(1) = 0.5;
00171 // cout << "g(" << x(0) << "," << x(1) << ") = " << g(x) << endl;
00172 // </srcblock>
00173 // </example>
00174 
00175 // <motivation>
00176 // Gaussian2D objects allow us to represent models of
00177 // the sky in a more conventional way than the generic interface used in the
00178 // GaussianND class does.
00179 // </motivation>
00180 
00181 // <templating arg=T>
00182 //  <li> T should have standard numerical operators and exp() function. Current
00183 //      implementation only tested for real types (and AutoDiff of them).
00184 // </templating>
00185 
00186 // <thrown>
00187 //    <li> Assertion in debug mode if attempt is made to set a negative width
00188 //    <li> AipsError if incorrect parameter number specified.
00189 // </thrown>
00190 
00191 // <todo asof="2001/08/19">
00192 //   <li> Gaussians that know about their DFT's could be required eventually.
00193 // </todo>
00194 
00195 template<class T> class Gaussian2DParam : public Function<T>
00196 {
00197 public:
00198   //# Enumerations
00199   enum { HEIGHT=0, XCENTER, YCENTER, YWIDTH, RATIO, PANGLE};
00200   
00201   //# Constructors
00202   // Constructs the two dimensional Gaussians. Defaults:
00203   // height=1, center=0, width(FWHM)=1, pa=0.
00204   // <group>
00205   Gaussian2DParam();
00206   Gaussian2DParam(const T &height, const Vector<T> &center, 
00207                   const Vector<T> &width, const T &pa);
00208   Gaussian2DParam(const T &height, const T &xCenter, const T &yCenter,
00209                   const T &majorAxis, const T &axialRatio,
00210                   const T &pa);
00211   // </group>
00212 
00213   // Copy constructor (deep copy)
00214   // <group>
00215   Gaussian2DParam(const Gaussian2DParam<T> &other);
00216   template <class W>
00217     Gaussian2DParam(const Gaussian2DParam<W> &other) :
00218     Function<T>(other),
00219     fwhm2int(T(1.0)/sqrt(log(T(16.0)))) { majorAxis(); setPA(PA()); }
00220   // </group>
00221 
00222   // Copy assignment (deep copy)
00223   Gaussian2DParam<T> &operator=(const Gaussian2DParam<T> &other);
00224     
00225   // Destructor
00226   virtual ~Gaussian2DParam();
00227 
00228   //# Operators    
00229  
00230   // Variable dimensionality
00231   virtual uInt ndim() const { return 2; }
00232 
00233   //# Member functions
00234   // Give name of function
00235   virtual const String &name() const { static String x("gaussian2d");
00236     return x; }
00237 
00238   // Get or set the peak height of the Gaussian
00239   // <group>
00240   T height() const { return param_p[HEIGHT]; }
00241   void setHeight(const T &height) { param_p[HEIGHT] = height; }
00242   // </group>
00243 
00244   // Get or set the analytical integrated area underneath the Gaussian.
00245   // Use these functions as an alternative to the height functions.
00246   // <group>
00247   T flux() const;
00248   void setFlux(const T &flux);
00249   // </group>
00250 
00251   // Get or set the center ordinate of the Gaussian
00252   // <group>
00253   Vector<T> center() const;
00254   void setCenter(const Vector<T> &center);
00255   T xCenter() const { return param_p[XCENTER]; }
00256   void setXcenter(const T &cnter) { param_p[XCENTER] = cnter; }
00257   T yCenter() const { return param_p[YCENTER]; }
00258   void setYcenter(const T &cnter) { param_p[YCENTER] = cnter; }
00259   // </group>
00260 
00261   // Set or get the FWHM of the Gaussian.
00262   // <group>
00263   Vector<T> width() const;
00264   void setWidth(const Vector<T> &width);
00265   T majorAxis() const;
00266   void setMajorAxis(const T &width);
00267   T minorAxis() const;
00268   void setMinorAxis(const T &width);
00269   T axialRatio() const;
00270   void setAxialRatio(const T &axialRatio);
00271   // </group> 
00272 
00273   // Set/get the rotation angle (orientation) of the Gaussian.  PA is given
00274   // in radians counterclockwise. 
00275   // <group>
00276   T PA() const;
00277   void setPA(const T &pa);
00278   // </group>
00279 
00280 protected:
00281   // Constant to scale halfwidth at 1/e to FWHM
00283   T fwhm2int;
00284   // cached vale of the PA
00285   mutable T thePA;
00286   // cached values of the cos and sine of thePA
00287   // <group>
00288   mutable T theSpa;
00289   mutable T theCpa;
00290   // </group>
00291   // cached vale of the Xwidth = ratio*theYwidth;
00292   mutable T theXwidth;
00293 
00294   //# Make members of parent classes known.
00295 protected:
00296   using Function<T>::param_p;
00297 public:
00298   using Function<T>::nparameters;
00299 };
00300 
00301 
00302 } //# NAMESPACE CASA - END
00303 
00304 #ifndef CASACORE_NO_AUTO_TEMPLATES
00305 #include <scimath/Functionals/Gaussian2DParam.tcc>
00306 #endif //# CASACORE_NO_AUTO_TEMPLATES
00307 #endif