00001 //# LatticeExpr.h: LatticeExpr.h 00002 //# Copyright (C) 1997,1998,1999,2000,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 //# $Id$ 00027 00028 #ifndef LATTICES_LATTICEEXPR_H 00029 #define LATTICES_LATTICEEXPR_H 00030 00031 00032 //# Includes 00033 #include <lattices/Lattices/MaskedLattice.h> 00034 #include <lattices/Lattices/LatticeExprNode.h> 00035 #include <lattices/Lattices/LatticeRegion.h> 00036 #include <casa/Arrays/Slicer.h> 00037 00038 namespace casa { //# NAMESPACE CASA - BEGIN 00039 00040 //# Forward Declarations 00041 template <class T> class Array; 00042 template <class T> class LELArray; 00043 00044 00045 // <summary> Class to allow C++ expressions involving lattices </summary> 00046 00047 // <use visibility=export> 00048 00049 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos=""> 00050 // </reviewed> 00051 00052 // <prerequisite> 00053 // <li> <linkto class="Lattice"> Lattice</linkto> 00054 // <li> <linkto class="LatticeExprNode"> LatticeExprNode</linkto> 00055 // 00056 // </prerequisite> 00057 // 00058 // <etymology> 00059 // The name is derived from the fact that this class provides 00060 // an expression interface to the user which s/he may use to 00061 // write C++ expressions involving Lattices. 00062 // </etymology> 00063 // 00064 // <synopsis> 00065 // This class provides an interface which allows the C++ programmer 00066 // to enter expressions such as "sin(a)+b" where "a" and "b" 00067 // are Lattices. 00068 // 00069 // This class is termed an envelope class, and inside it are the 00070 // letter classes which do the real work. In reality, the letter 00071 // classes are actually accessed via bridging class called 00072 // LatticeExprNode, which exists to handle type conversions. 00073 // The letter classes iterate through the Lattices and evaluate the 00074 // expression for each chunk of the iteration (usually a tile shape). 00075 // 00076 // It is in the LatticeExprNode class that all the available expression 00077 // operations are defined, so you should look there to see what 00078 // functionality is available. 00079 // 00080 // A description of the implementation details of these classes can 00081 // be found in <a href="../../../notes/216/216.html">Note 216</a> 00082 // </synopsis> 00083 // 00084 // <example> 00085 // <srcblock> 00086 // ArrayLattice<Float> f1(IPosition (2,nx,ny)); 00087 // ArrayLattice<Float> f2(IPosition (2,nx,ny)); 00088 // f2.set(2.0); 00089 // f1.copyData(2*f2+f2); 00090 // </srcblock> 00091 // 00092 // In this example, the values of the pixels in Lattice f1 are set 00093 // to the values resulting from the expression "2*f2 + f2" 00094 // I.e. the expression is evaluated for each pixel in the Lattices 00095 // 00096 // Note that : 00097 // 1) the Lattice::copyData function is expecting a Lattice argument. 00098 // 2) LatticeExpr inherits from Lattice and therefore a LatticeExpr 00099 // object is a valid argument object type 00100 // 3) The expression in the copyData call is automatically converted to 00101 // a LatticeExprNode by the constructors and operators in LatticeExprNode 00102 // 4) The LatticeExprNode object so created is automatically converted 00103 // to a LatticeExpr by casting functions in LatticeExprNode. 00104 // </example> 00105 // 00106 // <example> 00107 // <srcblock> 00108 // ArrayLattice<Float> f1(IPosition (2,nx,ny)); 00109 // ArrayLattice<Float> f2(IPosition (2,nx,ny)); 00110 // ArrayLattice<Double> d(IPosition (2,nx,ny)); 00111 // ArrayLattice<Complex> c(IPosition (2,nx,ny)); 00112 // ArrayLattice<Bool> b(IPosition (2,nx,ny)); 00113 // 00114 // f2.set(1.0); d.set(2.0); c.set(Complex(2.0,3.0)); b.set(True); 00115 // f1.copyData( (3.5*f2) + (cos(d)) - (10/min(d,f2)*(-abs(c))*ntrue(b)) - (C::pi) ); 00116 // </srcblock> 00117 // 00118 // In this rather silly example, we fill Lattice "f1" with the result of the 00119 // expression. The expression shows the use of constants, unary operations, 00120 // binary operations, 1D and 2D functions. It also shows how mixed types can 00121 // be handled. The output Lattice is a Float, whereas mixed into the 00122 // expression are subexpressions involving Float, Double, Complex and Bool 00123 // Lattices. 00124 // 00125 // </example> 00126 // 00127 // <motivation> 00128 // The Lattice expression classes enable the C++ programmer much simpler 00129 // handling of mathematical expressions involving lattices. In addition, 00130 // these classes provide the infrastructure on top of which we can build 00131 // an image calculator for Glish users 00132 // </motivation> 00133 00134 // <todo asof="1997/01/15"> 00135 // <li> masks 00136 // <li> regions 00137 // </todo> 00138 00139 00140 template <class T> class LatticeExpr : public MaskedLattice<T> 00141 { 00142 public: 00143 00144 // Default constructor 00145 LatticeExpr(); 00146 00147 // Constructor from an arbitrary LatticeExprNode expression object. 00148 // An exception is thrown if the expression data type cannot be 00149 // converted to the template data type. 00150 // The shape argument is mandatory if the expression has no shape. 00151 // If the expression has a shape and if shape is given, it is checked 00152 // if they are equal. 00153 LatticeExpr (const LatticeExprNode& expr); 00154 LatticeExpr (const LatticeExprNode& expr, const IPosition& latticeShape); 00155 00156 // Copy constructor (reference semantics) 00157 LatticeExpr (const LatticeExpr<T>& other); 00158 00159 // Destructor, does nothing 00160 virtual ~LatticeExpr(); 00161 00162 // Assignment (reference semantics) 00163 LatticeExpr<T>& operator=(const LatticeExpr<T>& other); 00164 00165 // Make a copy of the derived object (reference semantics). 00166 virtual MaskedLattice<T>* cloneML() const; 00167 00168 // Has the object really a mask? 00169 virtual Bool isMasked() const; 00170 00171 // Get the region used (always returns 0). 00172 virtual const LatticeRegion* getRegionPtr() const; 00173 00174 // Returns False, as the LatticeExpr lattice is not writable. 00175 virtual Bool isWritable() const; 00176 00177 // Handle locking of the LatticeExpr which is delegated to all of its parts. 00178 // <br>hasLock() is True if all parts of the expression return True. 00179 // <br>It is strongly recommended to use class 00180 // <linkto class=LatticeLocker>LatticeLocker</linkto> to 00181 // handle lattice locking. It also contains a more detailed 00182 // explanation of the locking process. 00183 // <group> 00184 virtual Bool lock (FileLocker::LockType, uInt nattempts); 00185 virtual void unlock(); 00186 virtual Bool hasLock (FileLocker::LockType) const; 00187 // </group> 00188 00189 // Resynchronize the Lattice object with the lattice file. 00190 // This function is only useful if no read-locking is used, ie. 00191 // if the table lock option is UserNoReadLocking or AutoNoReadLocking. 00192 // In that cases the table system does not acquire a read-lock, thus 00193 // does not synchronize itself automatically. 00194 // <br>By default the function does not do anything at all. 00195 virtual void resync(); 00196 00197 // Returns the shape of the Lattice including all degenerate axes 00198 // (i.e. axes with a length of one) 00199 virtual IPosition shape() const; 00200 00201 // Return the best cursor shape. 00202 virtual IPosition doNiceCursorShape (uInt maxPixels) const; 00203 00204 // Returns the coordinates of the lattice expression. 00205 virtual LELCoordinates lelCoordinates() const; 00206 00207 // Do the actual get of the data. 00208 // The return value is always False, thus the buffer does not reference 00209 // another array. 00210 virtual Bool doGetSlice (Array<T>& buffer, const Slicer& section); 00211 00212 // Do the actual get of the mask data. 00213 // The return value is always False, thus the buffer does not reference 00214 // another array. 00215 virtual Bool doGetMaskSlice (Array<Bool>& buffer, const Slicer& section); 00216 00217 // An expression is not writable so this functions throws an exception. 00218 virtual void doPutSlice (const Array<T>& sourceBuffer, 00219 const IPosition& where, 00220 const IPosition& stride); 00221 00222 // Copy the data from this lattice to the given lattice. 00223 virtual void copyDataTo (Lattice<T>& to) const; 00224 00225 // Handle the Math operators (+=, -=, *=, /=). 00226 // They work similarly to copyData(To). 00227 // However, they are not defined for Bool types, thus specialized below. 00228 virtual void handleMathTo (Lattice<T>& to, int oper) const; 00229 00230 private: 00231 // Initialize the object from the expression. 00232 void init (const LatticeExprNode& expr); 00233 00234 00235 LatticeExprNode expr_p; //# its shape can be undefined 00236 IPosition shape_p; //# this shape is always defined 00237 LELArray<T>* lastChunkPtr_p; 00238 Slicer lastSlicer_p; 00239 }; 00240 00241 00242 template<> inline 00243 void LatticeExpr<Bool>::handleMathTo (Lattice<Bool>& to, int oper) const 00244 { throwBoolMath(); } 00245 00246 00247 00248 } //# NAMESPACE CASA - END 00249 00250 #ifndef AIPS_NO_TEMPLATE_SRC 00251 #include <lattices/Lattices/LatticeExpr.cc> 00252 #endif //# AIPS_NO_TEMPLATE_SRC 00253 #endif
1.5.1