casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LatticeIndexer.h
Go to the documentation of this file.
00001 //# LatticeIndexer.h: A helper class for stepping through Lattices
00002 //# Copyright (C) 1994,1995,1996,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: LatticeIndexer.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
00027 
00028 #ifndef LATTICES_LATTICEINDEXER_H
00029 #define LATTICES_LATTICEINDEXER_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/IPosition.h>
00034 
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // <summary>
00039 // A helper class for stepping through Lattices.
00040 // </summary>
00041 
00042 // <use visibility=local>
00043 
00044 // <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tLatticeIndexer">
00045 // </reviewed>
00046 
00047 // <prerequisite>
00048 //   <li> <linkto class="Lattice"> Lattice </linkto>
00049 //   <li> <linkto class="IPosition"> IPosition </linkto>
00050 // </prerequisite>
00051 
00052 // <etymology>
00053 // This class does various calculations involved with indexing in
00054 // Lattices. LatticeIndexer is not a good name, but it is
00055 // better than the previous name of LatticeLayout.
00056 // </etymology>
00057 
00058 // <synopsis> 
00059 // A LatticeIndexer contains all the information necessary to define the
00060 // shape of a Lattice or sub-Lattice. It is currently a repository of
00061 // functions that provide indexing calculations.
00062 // <p>
00063 // A sub-Lattice is a section of a Lattice defined by a bottom left corner
00064 // (blc), a top right corner (trc), and a step size or increment on each
00065 // axis. The blc and trc pixels will always be included in the sub-Lattice
00066 // if the step increment is one. If the step increment is greater than one,
00067 // the pixel in top right corner may not be included in the sub-Lattice. 
00068 // <p>
00069 // This class knows the shape of the parent Lattice (including all
00070 // degenerate axes), and allows the user to specify a sub-Lattice that is
00071 // embedded in the parent Lattice. The default sub-Lattice, if none is
00072 // specified, is one identical in shape to the main Lattice. 
00073 // <p>
00074 // A sub-Lattice can be defined on the Lattice by specifying a trc, blc,
00075 // and step increment using the <src>subSection</src> function, or the
00076 // appropriate constructor. A sub-Lattice must be smaller than (or the same
00077 // size as) the Lattice that it is derived from. A sub-Lattice can be further
00078 // created from an already existing sub-Lattice eg.
00079 // <br>
00080 // If we have a 128 by 128 Lattice, we can specify the centre quarter by
00081 // using blc=[32,32] and trc=[95,95]. Then specifying a sub-Lattice of
00082 // blc=[0,0] and trc = [31,31] results in a sub-Lattice that has a blc
00083 // of [32,32] and trc of [63,63] with respect to the parent Lattice.
00084 // <p>
00085 // The only way to increase the size of a sub-Lattice is to first revert to
00086 // the parent Lattice (using the <src>fullSize</src> function) and then
00087 // generate the new, bigger sub-Lattice.
00088 // <p>
00089 // Indexing calculations (eg. the <src>tiledCursorMove</src> or the
00090 // <src>isInside</src> function) are performed on the specified sub-Lattice.
00091 // <p>
00092 // The role of this class is to centralise the information and functions
00093 // needed to operate on sub-Lattices. It will normally be used by other
00094 // Lattice classes, and is currently used by navigator classes like 
00095 // <linkto class="LatticeStepper">LatticeStepper</linkto>.
00096 // </synopsis>
00097 
00098 // <motivation>
00099 // The shape, structure or geometry of a lattice is quite separable from
00100 // its actual contents, and the operations you can do on the contents.  Also,
00101 // there are operations which apply only to the layout such as subsectioning. 
00102 // </motivation>
00103 
00104 //# <todo asof="1997/01/12">
00105 //# </todo>
00106 
00107 
00108 class LatticeIndexer
00109 {
00110 public:
00111   // Default constructor (one dimensional, unit-length instance).
00112   LatticeIndexer();
00113 
00114   // Specify the size of the Lattice. Assume a full size sub-Lattice. 
00115   explicit LatticeIndexer (const IPosition& shape);
00116 
00117   // Specify a Lattice and define a sub-Lattice within it.
00118   LatticeIndexer (const IPosition& shape, const IPosition& blc,
00119                   const IPosition& trc, const IPosition& inc);
00120   
00121   // The copy constructor uses copy semantics. 
00122   LatticeIndexer (const LatticeIndexer& other);
00123 
00124   ~LatticeIndexer();
00125 
00126   // The assignment operator uses copy semantics.
00127   LatticeIndexer& operator= (const LatticeIndexer& other);
00128 
00129   // Function to change the shape of the Lattice. Resets the sub-Lattice to
00130   // fullsize.
00131   void resize (const IPosition& newShape);
00132 
00133   // Returns the length of each axis (or the requested one) in the parent
00134   // Lattice.
00135   // <group>
00136   const IPosition& fullShape() const;
00137   uInt fullShape (uInt axis) const;
00138   // </group>
00139 
00140   // Returns the length of each axis (or the requested one) in the sub-Lattice.
00141   // <group>
00142   const IPosition& shape() const;
00143   uInt shape (uInt axis) const;
00144   // </group>
00145 
00146   // Function to return the increments along each axis (or the requested
00147   // one) of the Lattice.
00148   // <group>
00149   const IPosition& increment() const;
00150   uInt increment (uInt axis) const;
00151   // </group>
00152 
00153   // Function to return the offset (on a specified axis) between the
00154   // sub-Lattice and the parent one.
00155   // <group>
00156   const IPosition& offset() const;
00157   uInt offset (uInt axis) const;
00158   // </group>
00159 
00160   // Function which returns the number of dimensions in the Lattice (or
00161   // sub-Lattice).
00162   uInt ndim() const;
00163 
00164   // Revert from a sub-Lattice description back to the main Lattice. This is
00165   // the only way to "increase" the the size of the sub-Lattice used by the
00166   // LatticeIndexer.
00167   void fullSize();
00168 
00169   // Function which returns the number of elements in the sub-Lattice; 
00170   // this value is equal to the product of shape().
00171   size_t nelements() const;
00172 
00173   // Function which increments (incr=True) or decrements (incr=False) the
00174   // cursor position (the first IPosition argument) by a cursor shape (the
00175   // second IPosition argument), tiling to the next/previous axis if
00176   // necessary.  The path of movement is based upon the third IPosition
00177   // argument (a cursor heading) that is zero-based e.g. IPosition(3,0,2,1)
00178   // implies starting movement along the x-axis, then the z-axis, and then
00179   // the y-axis.  Returns a value of False if the beginning/end of the
00180   // sub-Lattice is reached. The cursorPosition is relative to the origin of
00181   // the sub-Lattice. To get its location relative to the main Lattice use
00182   // the absolutePosition() function. 
00183   Bool tiledCursorMove (Bool incr, IPosition& cursorPos, 
00184                         const IPosition& cursorShape,
00185                         const IPosition& cursorHeading) const;
00186   
00187   // Function which returns a value of True if the IPosition argument
00188   // is within the sub-Lattice.  Returns False if the IPosition argument is 
00189   // outside the sub-Lattice or if the argument doesn't conform to the 
00190   // data members.
00191   // <note role=warning> Due to zero-origins, an index argument equal to the
00192   // shape of this sub-Lattice lies outside and returns False. 
00193   // </note>
00194   Bool isInside (const IPosition& index) const;
00195 
00196   // Function which subsections a LatticeIndexer.  The argument IPositions
00197   // specify "bottom left" and "upper right" corners and axis increments
00198   // (which default to one).  The origins are cumulative. i.e. specifying a
00199   // blc of (2,2), and then (1,1) results in the sub-Lattice having an
00200   // origin at pixel (3,3) in the parent Lattice. Similarly the increment is
00201   // cumulative, i.e. an increment of 2 on top of an increment of 3 results
00202   // in a total increment of 6. This function can only decrease the size of
00203   // the sub-Lattice (i.e. blc >= 0, and trc <= shape(), and inc >= 1). The
00204   // fullSize() function should be used to revert back to the maximum
00205   // possible Lattice size.  Also note that the trc might not be used if an
00206   // integral number of increments does not end on the trc (in which case
00207   // the last position below the trc will be used).
00208   // <group>
00209   void subSection (const IPosition& blc, const IPosition& trc,
00210                    const IPosition& inc);
00211   void subSection (const IPosition& blc, const IPosition& trc);
00212   // </group>
00213 
00214   // Function which returns an IPosition in the parent Lattice given an
00215   // IPostion in the sub-Lattice.  Accounting is taken of any offsets and
00216   // increments caused by subSectioning. No checks are made to ensure the
00217   // supplied IPosition or the returned one are within the bounds of the
00218   // Lattice(s).
00219   IPosition absolutePosition (const IPosition& position) const;
00220 
00221   //# function which returns True if all the elements in this 
00222   //# LatticeIndexer, or LatticeIndexer subsection, are arranged contiguously, 
00223   //# i.e. without any gaps caused by increments or subSectioning.
00224 //#   Bool isContiguous() const;
00225 
00226   // Is this LatticeIndexer consistent, i.e. are the class invariants valid?
00227   // Returns True if every thing is fine otherwise returns False
00228   Bool ok() const;
00229 
00230 private:
00231   IPosition itsFullShape;  //# Size of the main-Lattice.
00232   uInt      itsNdim;       //# Number of dimensions in the main/sub-Lattice
00233   IPosition itsShape;      //# Shape of the sub-Lattice
00234   IPosition itsAxisInc;    //# Increment along each axis of main Lattice
00235   IPosition itsOffset;     //# Offset between a sub-Lattice and the main one.
00236 };
00237 
00238 
00239 inline const IPosition& LatticeIndexer::fullShape() const
00240 {
00241   return itsFullShape;
00242 }
00243 inline const IPosition& LatticeIndexer::shape() const
00244 {
00245   return itsShape;
00246 }
00247 inline const IPosition& LatticeIndexer::increment() const
00248 {
00249   return itsAxisInc;
00250 }
00251 inline const IPosition& LatticeIndexer::offset() const
00252 {
00253   return itsOffset;
00254 }
00255 inline uInt LatticeIndexer::ndim() const
00256 {
00257   return itsNdim;
00258 }
00259 inline size_t LatticeIndexer::nelements() const
00260 {
00261   return itsShape.product();
00262 }
00263 
00264 
00265 
00266 
00267 } //# NAMESPACE CASA - END
00268 
00269 #endif