casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LatticeApply.h
Go to the documentation of this file.
00001 //# LatticeApply.h: Optimally iterate through a Lattice and apply provided function object
00002 //# Copyright (C) 1997,1998,1999
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: LatticeApply.h 20229 2008-01-29 15:19:06Z gervandiepen $
00027 
00028 #ifndef LATTICES_LATTICEAPPLY_H
00029 #define LATTICES_LATTICEAPPLY_H
00030  
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <casa/Containers/Block.h>
00035 #include <scimath/Mathematics/NumericTraits.h>
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 //# Forward Declarations
00040 template <class T, class U> class TiledCollapser;
00041 template <class T, class U> class LineCollapser;
00042 template <class T> class Lattice;
00043 template <class T> class MaskedLattice;
00044 class LatticeProgress;
00045 class IPosition;
00046 class LatticeRegion;
00047 
00048 
00049 // <summary>
00050 // Optimally iterate through a Lattice and apply provided function object
00051 // </summary>
00052 
00053 // <use visibility=export>
00054 
00055 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00056 // </reviewed>
00057 
00058 // <prerequisite>
00059 //   <li> <linkto class=Lattice>MaskedLattice</linkto>
00060 //   <li> <linkto class=LineCollapser>LineCollapser</linkto>
00061 //   <li> <linkto class=TiledCollapser>TiledCollapser</linkto>
00062 // </prerequisite>
00063 
00064 // <synopsis>
00065 // This function iterates through a Lattice and applies a user given
00066 // function object to chunks along the specified axes. Usually the
00067 // function collapses the chunk to 1 or a few values (e.g. get min/max).
00068 // The result of the function is written into the output Lattice(s) at the
00069 // location of the collapsed chunk.  The output lattice(s) must be supplied
00070 // with the correct shape. E.g. when a lattice with shape [nx,ny,nz] is
00071 // collapsed by calculating the mean of each y-line, the output lattice
00072 // has to have shape [nx,nz]. It is also possible to have output shape
00073 // [nx,1,nz], [1,nx,nz], [nx,nz,1] or even e.g. [nx,1,1,1,nz].
00074 // <p>
00075 // By specifying a region it is possible to apply the function object
00076 // to a subset of the lattice. Of course, the shape of the output lattice(s)
00077 // have to match the shape of the region.
00078 // <p>
00079 // The iteration is done in an optimal way. To keep memory usage down,
00080 // it caches as few tiles as possible.
00081 // There are 2 ways to iterate.
00082 // <ol>
00083 // <li> For some applications an entire line is needed. An example is
00084 // the calculation of the moment. The functions <src>lineApply</src>
00085 // and <src>lineMultiApply</src> can be used for that purpose.
00086 // Internally they use the
00087 // <linkto class=TiledLineStepper>TiledLineStepper</linkto>
00088 // navigator, so only a few tiles are kept in the cache.
00089 // <br> One can also think of applications where an entire plane (or cube)
00090 // is needed. This is not supported, but can be implemented when needed.
00091 // <li> Other applications do not care how the data are traversed,
00092 // making it possible to iterate tile by tile (which is optimal).
00093 // An example is the calculation of the minimum, maximum, mean of
00094 // a line, plane, etc..
00095 // For this purpose the function <src>tiledApply</src> can be used.
00096 // This function is faster and uses less memory than <src>lineApply</src>,
00097 // so whenever possible this one should be used. Another advantage of
00098 // this function is that it is possible to operate per line, plane, etc.
00099 // or even for the entire lattice.
00100 // </ol>
00101 // The user has to supply a function object derived from the abstract base
00102 // class <linkto class=LineCollapser>LineCollapser</linkto> or 
00103 // <linkto class=TiledCollapser>TiledCollapser</linkto>, resp..
00104 // The <src>process</src> function in these classes has to process
00105 // the chunk of data passed in. The <src>nstepsDone</src> function
00106 // in these classes can be used to monitor the progress.
00107 // <p>
00108 // The class is Doubly templated.  Ths first template type
00109 // is for the data type you are processing.  The second type is
00110 // for what type you want the results of the processing assigned to.
00111 // For example, if you are computing sums of squares for statistical
00112 // purposes, you might use higher precision (Float->Double) for this.
00113 // No check is made that the template types are self-consistent.
00114 // </synopsis>
00115 
00116 // <example>
00117 // Collapse each line in the y-direction using my collapser function object.
00118 // <srcblock>
00119 //    MyLineCollapser collapser;
00120 //    PagedArray<Float> latticeIn("lattice.file");
00121 //    IPosition shape = latticeIn.shape();
00122 //    shape(1) = 1;
00123 //    ArrayLattice<Double> latticeOut(shape);
00124 //    LatticeApply<Float,Double>::lineApply (latticeOut, latticeIn, collapser, 1);
00125 // </srcblock>
00126 // </example>
00127 
00128 // <motivation>
00129 // This class makes it possible that a user can apply functions to
00130 // a lattice in an optimal way, without having to know all the details
00131 // of iterating through a lattice.
00132 // </motivation>
00133 
00134 //# <todo asof="1997/08/01">   
00135 //#   <li> 
00136 //# </todo>
00137 
00138  
00139 template <class T, class U=T> class LatticeApply
00140 {
00141 public:
00142 
00143 // This function iterates line by line through an input lattice and applies
00144 // a user supplied function object to each line along the specified axis.
00145 // The scalar result of the function object is written into the output
00146 // lattice at the location of the collapsed line. The output lattice must
00147 // be supplied with the correct shape (the shape of the supplied region).
00148 // The default region is the entire input lattice.
00149 // <group>
00150     static void lineApply (MaskedLattice<U>& latticeOut, 
00151                            const MaskedLattice<T>& latticeIn,
00152                            LineCollapser<T,U>& collapser,
00153                            uInt collapseAxis,
00154                            LatticeProgress* tellProgress = 0);
00155     static void lineApply (MaskedLattice<U>& latticeOut, 
00156                            const MaskedLattice<T>& latticeIn,
00157                            const LatticeRegion& region,
00158                            LineCollapser<T,U>& collapser,
00159                            uInt collapseAxis,
00160                            LatticeProgress* tellProgress = 0);
00161 // </group>
00162     
00163 // This function iterates line by line through an input lattice and applies
00164 // a user supplied function object to each line along the specified axis.
00165 // The vector result of the function object is written into the output
00166 // lattices at the location of the collapsed line (1 value per lattice).
00167 // The output lattices must be supplied with the correct shape (the shape
00168 // of the supplied region).
00169 // The default region is the entire input lattice.
00170 // <group>
00171     static void lineMultiApply (PtrBlock<MaskedLattice<U>*>& latticeOut, 
00172                                 const MaskedLattice<T>& latticeIn,
00173                                 LineCollapser<T,U>& collapser,
00174                                 uInt collapseAxis,
00175                                 LatticeProgress* tellProgress = 0);
00176     static void lineMultiApply (PtrBlock<MaskedLattice<U>*>& latticeOut, 
00177                                 const MaskedLattice<T>& latticeIn,
00178                                 const LatticeRegion& region,
00179                                 LineCollapser<T,U>& collapser,
00180                                 uInt collapseAxis,
00181                                 LatticeProgress* tellProgress = 0);
00182 // </group>
00183 
00184 // This function iterates tile by tile through an input lattice and applies
00185 // a user supplied function object to each chunk along the specified axes.
00186 // A chunk can be a line, plane, etc. which is determined by the argument
00187 // <src>collapseAxes</src>. E.g. IPosition(2,1,2) means planes along
00188 // axes 1 and 2 (thus y,z planes).
00189 // The result of the function object is written into the output
00190 // lattice at the location of the collapsed chunk. The output lattice must
00191 // be supplied with the correct shape (the shape of the supplied region
00192 // plus the number of values resulting from the collapse).
00193 // The default region is the entire input lattice.
00194 // <group>
00195     static void tiledApply (MaskedLattice<U>& latticeOut,
00196                             const MaskedLattice<T>& latticeIn,
00197                             TiledCollapser<T,U>& collapser,
00198                             const IPosition& collapseAxes,
00199                             Int newOutAxis = -1,
00200                             LatticeProgress* tellProgress = 0);
00201     static void tiledApply (MaskedLattice<U>& latticeOut,
00202                             const MaskedLattice<T>& latticeIn,
00203                             const LatticeRegion& region,
00204                             TiledCollapser<T,U>& collapser,
00205                             const IPosition& collapseAxes,
00206                             Int newOutAxis = -1,
00207                             LatticeProgress* tellProgress = 0);
00208 // </group>
00209 
00210 // This function iterates tile by tile through an input lattice and applies
00211 // a user supplied function object to each chunk along the specified axes.
00212 // A chunk can be a line, plane, etc. which is determined by the argument
00213 // <src>collapseAxes</src>. E.g. IPosition(2,1,2) means planes along
00214 // axes 1 and 2 (thus y,z planes).
00215 // The result of the function object is written into the output
00216 // lattices at the location of the collapsed chunk. The output lattices must
00217 // be supplied with the correct shape (the shape of the supplied region).
00218 // The default region is the entire input lattice.
00219 // <note role=warning>
00220 // These functions are only declared, but not implemented yet.
00221 // Thus they cannot be used yet.
00222 // </note>
00223 // <group>
00224     static void tiledMultiApply (PtrBlock<MaskedLattice<U>*>& latticeOut, 
00225                                  const MaskedLattice<T>& latticeIn,
00226                                  TiledCollapser<T,U>& collapser,
00227                                  const IPosition& collapseAxes,
00228                                  LatticeProgress* tellProgress = 0);
00229     static void tiledMultiApply (PtrBlock<MaskedLattice<U>*>& latticeOut, 
00230                                  const MaskedLattice<T>& latticeIn,
00231                                  const LatticeRegion& region,
00232                                  TiledCollapser<T,U>& collapser,
00233                                  const IPosition& collapseAxes,
00234                                  LatticeProgress* tellProgress = 0);
00235 // </group>
00236 
00237 
00238 private:
00239     // Do some checks on the given arguments.
00240     // It returns an IPosition with the same length as shapeOut.
00241     // It contains a mapping of output to input axes. A value of -1
00242     // indicates that the axis is new (to contain the collapse result).
00243     // <br>Argument newOutAxis tells the output axis to store the results.
00244     // -1 means that the function has to find it out itself; it takes the
00245     // first axis with a length mismatching the corresponding input axis.
00246     static IPosition prepare (const IPosition& shapeIn,
00247                               const IPosition& shapeOut,
00248                               const IPosition& collapseAxes,
00249                               Int newOutAxis);
00250 };
00251 
00252 
00253 
00254 } //# NAMESPACE CASA - END
00255 
00256 #ifndef CASACORE_NO_AUTO_TEMPLATES
00257 #include <lattices/Lattices/LatticeApply.tcc>
00258 #endif //# CASACORE_NO_AUTO_TEMPLATES
00259 #endif