casacore
Loading...
Searching...
No Matches
LatticeStepper.h
Go to the documentation of this file.
1//# LatticeStepper.h: provides 'natural' traversal, by cursor shape
2//# Copyright (C) 1994,1995,1996,1997,1998,1999,2000,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef LATTICES_LATTICESTEPPER_H
27#define LATTICES_LATTICESTEPPER_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/lattices/Lattices/LatticeNavigator.h>
32#include <casacore/lattices/Lattices/LatticeIndexer.h>
33#include <casacore/casa/Arrays/IPosition.h>
34
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// <summary>
39// Traverse a Lattice by cursor shape
40// </summary>
41
42// <use visibility=export>
43
44// <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tLatticeStepper.cc">
45// </reviewed>
46
47// <prerequisite>
48// <li> <linkto class=LatticeNavigator> LatticeNavigator </linkto>
49// </prerequisite>
50
51// <etymology>
52// LatticeStepper is so-called because it performs the calculations
53// necessary to step through a Lattice. The next position is always one
54// simple step forward from the current position. The step-size is
55// calculated directly from the size of the LatticeIterator's cursor or
56// window.
57// </etymology>
58
59// <synopsis>
60// When you wish to traverse a Lattice (say, a PagedArray or an Image) you
61// will usually create a LatticeIterator. Once created, you must attach a
62// LatticeNavigator to the iterator. A LatticeStepper, is a concrete class
63// derived from the abstract LatticeNavigator that allows you to move
64// sequentially through the Lattice.
65// <p>
66// In constructing a LatticeStepper, you specify the Lattice shape and the
67// shape of the "cursor" used to step through the data. The cursor position
68// can be incremented or decremented to retrieve the next portion of the
69// Lattice.
70// The specified cursor shape can (and often will) have fewer dimensions
71// that the Lattice itself. For example if we have a 4-dimensional Lattice
72// with <src>latticeShape = IPosition(4,64,64,4,16)</src>, then specifying a
73// cursor of <src>cursorShape = IPosition(1,64)</src>, will step through the
74// hypercube row by row. When the cursor shape has fewer dimensions than the
75// Lattice degenerate dimensions are added to the end of the cursor so that
76// in the above example the specified cursor is assumed to mean
77// <src>cursorShape = IPosition(4,64,1,1,1)</src>. To access the data
78// spectrum by spectrum (assuming the last axis is the spectral axis), you
79// must use a 1-dimensional cursor of <src>IPosition(4,1,1,1,16)</src>. The
80// <src>cursorShape</src> function always returns a shape with as many
81// dimensions as the underlying Lattice.
82// <p>
83// It is an error (and an exception will be thrown) if the cursor has more
84// dimensions than the Lattice or if it is larger on any axis than the
85// Lattice shape.
86// <br>
87// Also the cursor shape on all axes must be less than or equal to the Lattice
88// shape on that axis. Otherwise an exception will be thrown.
89// <p>
90// In principle cursor axes with length 1 are degenerate axes. They
91// are removed from the lattice cursor if the
92// <linkto class=LatticeIterator>LatticeIterator</linkto> cursor is accessed
93// using e.g. the <src>matrixCursor</src> function.
94// Using a special LatticeStepper constructor it is, however, possible
95// to specify which cursor axes with length 1 have to be treated as
96// normal axes. In that way one can be sure that a cursor is, for
97// example, always 2D, even if an axis happens to have length 1.
98// <srcblock>
99// IPosition latticeShape(4,20,16,1,4);
100// IPosition cursorAxes(2,1,2);
101// IPosition cursorShape(2,16,1);
102// IPosition axisPath;
103// LatticeStepper stepper(latticeShape, cursorShape,
104// cursorAxes, axisPath);
105// </srcblock>
106// This results in a cursor with shape [1,16,1,1]. The first and last
107// axis are degenerate, so the cursor can also be accessed using
108// <src>matrixCursor</src> (with shape [16,1]).
109// Note that the cursor shape could also be specified as [1,16,1,1].
110// <p>
111// The "path" of the cursor through the Lattice can be controlled by
112// specifying an axisPath during construction of the class. This is an
113// IPosition which has exactly as many elements as the Lattice
114// dimension. Each element must contain an integer between
115// 0 -- Lattice_Dimension-1, and must be unique. For example,
116// <srcblock>
117// axisPath = IPosition(4,0,1,2,3) or
118// axisPath = IPosition(4,3,1,2,0)
119// </srcblock>
120// are valid but
121// <srcblock>
122// axisPath = IPosition(4,1,2,3,4) or
123// axisPath = IPosition(4,0,1,1,3)
124// </srcblock>
125// are not, given the latticeShape specified above. An exception is thrown
126// if the AxisPath is bad.
127// <br>
128// The "axis path" defines which axis will be iterated through fastest as
129// the cursor moves through the Lattice. With the above mentioned
130// 4-dimensional Lattice and a single element cursor
131// (<src>cursorShape=IPosition(4,1,1,1,1)</src>) setting an
132// <src>axisPath=IPosition(4,0,1,2,3)</src> will move the cursor through all
133// the columns, and then onto the next row, and again through all the
134// columns in the second row. Once all the rows in the first plane have
135// been exhausted the cursor will then iterate to the next plane, and
136// eventually to the next spectral channel. If, however, the axisPath was
137// <src>axisPath=IPosition(4,3,0,1,2)</src> then the cursor would iterate
138// through each spectral channel first, before moving onto the next column in
139// the first row.
140// <p>
141// The cursor never changes dimensionality as it traverses the Lattice. But it
142// may change shape if the cursor shape is not a factor of the Lattice
143// shape. A cursor shape is not a factor of the Lattice shape if the Lattice
144// shape is not an integer multiple of the cursor shape on all axes.
145// The integer multiplier need not to be the same for each axes.
146// For example, for a Lattice of shape [10,10,10] a cursor of shape [8,5,2]
147// is not a factor but one with a shape of [10,5,1] is.
148// <br>
149// When the cursor is not congruent with the Lattice moving the cursor through
150// the Lattice will sometimes result in part of the cursor hanging over the
151// edge of the Lattice. When this occurs the hangOver member function will
152// return True. What to do in these situtations is specified by the
153// hangOverPolicy enumerator.
154// <ol>
155// <li>
156// If the LatticeStepper::PAD option (the default) is used at construction time
157// the cursor shape does not change. The parts of the cursor that hang over the
158// edge of the Lattice are filled with a default value, usually zero, that is
159// defined by the particular LatticeIterator used.
160// <li>
161// If the LatticeStepper::RESIZE option is used at construction time the cursor
162// shape does change to a smaller value when near the edge of the Lattice so
163// that it is just big enough. For example with a Lattice shape of 10x10 and a
164// cursor of 8x8 the cursor shape will initally be 8x8, then resize to 2x8 on
165// the first step, then resize to 8x2 on the second step and finally resize to
166// 2x2. The hangover function will return True for the last three steps, even
167// though the cursor has resized.
168// </ol>
169// The portion of the Lattice that the cursor will traverse can be
170// restricted to a region defined by a top right corner, bottom left corner
171// and a step size. This is done using the <src>subSection</src> function,
172// which also resets the cursor position to the origin of the sub-Lattice.
173// The cursor shape will remain unchanged. It is no error when the cursor
174// shape exceeds the sub-Lattice shape (instead it is a hangover state).
175// <br>
176// If a sub-Lattice is defined then cursor positions relative
177// to the sub-Lattice origins can be obtained using the
178// <src>relativePosition</src> function rather than the
179// <src>position</src> function, which always returns positions relative to
180// the origin of the main Lattice.
181// <br>
182// To change the size of the sub-Lattice simply call the
183// <src>subSection</src> function again with a different trc, blc &
184// inc. This first clears the old sub-Lattice, then imposes the newly
185// specified one, and finally moves the cursor to the origin of the
186// new sub-Lattice.
187// </synopsis>
188
189// <example>
190// This example is of a global function that will iterate through a
191// 4-dimensional Lattice. It is assumed that the axes are RA, Dec, Stokes &
192// Frequency, and it will calculate the average flux in the I polarization
193// on each frequency channel. Imagine it is passed a data set (ie. Lattice)
194// of size 256 x 256 x 4 x 1024. This corresponds to 1GByte of data. However
195// the iterator will page through this data using a cursor of size 256 x 256
196// (or 256kByte) and will only read (because of subsectioning) the relevant
197// quarter of the data set. It is usually a good idea to set up the axis
198// path as this is gives hints to data cache about which data to retrieve in
199// advance.
200// <srcblock>
201// void averageFluxByChannel(const Lattice<Float>& data)
202// {
203// // for convenience, get the shape into a local variable
204// IPosition latticeShape = data.shape();
205// cout << "Data has shape: " << latticeShape << endl;
206//
207// // check that the data has 4 axes.
208// DebugAssert(latticeShape.nelements() == 4, AipsError);
209//
210// // specify the cursor, or window shape. Here the cursor is a matrix
211// // that is the shape of the first plane of our Lattice.
212// // For convenience, get the first two axis lengths into local vars
213// uInt nCols = latticeShape(0);
214// uInt nRows = latticeShape(1);
215// IPosition cursorShape(2, nCols, nRows);
216//
217// // construct a stepper, which needs to know the shape of the lattice
218// // and the shape of the iterator's cursor. By using cursorShape, which
219// // is directly determined by the lattice's shape, we can be sure
220// // that the cursor is a factor of the lattice, and thus that
221// // all elements will be picked up efficiently during the traversal.
222// // Because we will not be iterating through the stokes axis this axis
223// // is made the slowest moving one.
224// IPosition axisPath(4, 0, 1, 3, 2)
225// LatticeStepper stepper(latticeShape, cursorShape, axisPath);
226//
227// // Subsection the stepper so that it only iterates through the I
228// // Stokes parameter (assumed to be when the third axis is zero)
229// uInt nFreqs = latticeShape(3);
230// IPosition blc(4, 0, 0, 0, 0), trc(4, nCols-1, nRows-1, 0, nFreqs-1);
231// stepper.subSection(blc, trc);
232//
233// // construct the iterator. Since we only want to read the Data,
234// // use the read-only class, which disallows writing back to the cursor
235// // (and hence is more efficient).
236// RO_LatticeIterator<Float> iterator(data, stepper);
237//
238// Vector<Float> spectrum(nFreqs);
239// spectrum = 0.0;
240// uInt channel = 0;
241// for (iterator.reset(); !iterator.atEnd(); iterator++) {
242// const Matrix<Float>& cursor = iterator.matrixCursor();
243// for (uInt col = 0; col < nCols; col++) {
244// for (uInt row = 0; row < nRows; row++) {
245// spectrum(channel) += cursor(col, row);
246// }
247// }
248// channel++;
249// } // for iterator
250// cout << "Average spectrum is: "
251// << spectrum / cursorShape.product() << endl;
252// }
253// </srcblock>
254// </example>
255
256// <motivation>
257// Moving through a Lattice by equal sized chunks, and without regard
258// to the nature of the data, is a basic and common procedure.
259// </motivation>
260
261//# <todo asof="1995/08/28">
262//# </todo>
263
264
266{
267public:
268
269 // The hangOverPolicy enumerator is used in the constructors to indicate
270 // what this class should do when the cursor shape hangs over the edge
271 // of the Lattice.
273 // PAD is the default and means that the cursor size supplied by the user is
274 // kept fixed. But if the cursor overhangs the Lattice the part that
275 // overhangs is filled with a default value that is specified by the
276 // Iterator. Currently the default value is zero.
278 // RESIZE means that the cursor shape is adjusted whenever it approaches the
279 // edges of the Lattice so that it is always the right size to include only
280 // the parts of the Lattice that are available. The user specified cursor
281 // shape now becomes the default and largest possible cursor shape.
283
284 // The first argument is the shape of the Lattice to be iterated and the
285 // second argument is the shape of the cursor. The cursor will increment
286 // initially along first axis, then the second and then the third
287 // (ie. axisPath = IPosition(ndim,0,1,2,...))
288 // The dimensionality of the cursorShape can be less than the
289 // dimensionality of the lattice. It will be padded with 1s.
290 // <br>The cursorShape axes with length > 1 are seen as the true cursor axes.
291 // The other axes are degenerated and are removed by the functions
292 // <src>vectorCursor()</src>, etc., in class
293 // <linkto class=RO_LatticeIterator>(RO_)LatticeIterator</linkto>.
295 const uInt hangOverPolicy=PAD);
296
297 // Same as the above constructor except that the axis path is explicitly
298 // specified. The axis path is described in the synopsis above.
301
302 // Same as the above constructor except that the cursor axes are
303 // explicitly specified. This can be useful to avoid that cursor axes
304 // with length=1 are treated as degenerated axes by the Iterator classes.
305 // The following rules have to be obeyed:
306 // <br>- <src>cursorAxes.nelements() <= latticeShape.nelements()</src>
307 // <br>- <src>cursorShape.nelements() == latticeShape.nelements()</src>
308 // <br>or <src>cursorShape.nelements() == cursorAxes.nelements()</src>
309 // The latter means that the cursorShape contains the axes mentioned in
310 // cursorAxes.
311 // <br>See also the example in the synopsis.
313 const IPosition& cursorAxes,
315
316 // The copy constructor uses copy semantics.
318
320
321 // The assignment operator uses copy semantics.
323
324 // Increment operator (postfix version) - move the cursor
325 // forward one step. Returns True if the cursor was moved.
326 virtual Bool operator++(int);
327
328 // Decrement operator (postfix version) - move the cursor
329 // backwards one step. Returns True if the cursor was moved.
330 virtual Bool operator--(int);
331
332 // Function to move the cursor to the beginning of the (sub)-Lattice. Also
333 // resets the number of steps (<src>nsteps</src> function) to zero.
334 virtual void reset();
335
336 // Function which returns "True" if the cursor is at the beginning of the
337 // (sub)-Lattice, otherwise, returns "False"
338 virtual Bool atStart() const;
339
340 // Function which returns "True" if an attempt has been made to increment
341 // the cursor beyond the end of the (sub)-Lattice.
342 virtual Bool atEnd() const;
343
344 // Function to return the number of steps (increments & decrements) taken
345 // since construction (or since last reset). This is a running count of
346 // all cursor movement (operator++ or operator--), even though
347 // N-increments followed by N-decrements will ALWAYS leave the cursor in
348 // the original position.
349 virtual uInt nsteps() const;
350
351 // Functions which return the current position of the beginning of the
352 // cursor. The <src>position</src> function is relative to the origin
353 // in the main Lattice and the <src>relativePosition</src> function is
354 // relative to the origin and increment used in the sub-Lattice (defined
355 // using the <src>subSection</src> function). If no sub-Lattice is defined
356 // the two functions return identical positions.
357 // <group>
358 virtual IPosition position() const;
360 // </group>
361
362 // Functions which return the current position of the end of the
363 // cursor. The <src>endPosition</src> function is relative to the origin
364 // in the main Lattice and the <src>relativeEndPosition</src> function
365 // is relative to the origin and increment used in the sub-Lattice
366 // (defined using the <src>subSection</src> function). If no sub-Lattice
367 // is defined the two functions return identical positions.
368 // <note role=caution> It returns the end position in the lattice and
369 // does not take overhang into account. </note>
370 // <group>
371 virtual IPosition endPosition() const;
373 // </group>
374
375 // Functions which return the shape of the Lattice being iterated
376 // through. <src>latticeShape</src> always returns the shape of the main
377 // Lattice while <src>subLatticeShape</src> returns the shape of any
378 // sub-Lattice defined using the <src>subSection</src> function.
379 // <group>
380 virtual IPosition latticeShape() const;
381 virtual IPosition subLatticeShape() const;
382 // </group>
383
384 // Functions to change the cursor shape to a new one. They always reset
385 // the cursor to the beginning of the Lattice (and reset the number of
386 // steps to zero).
387 // <group>
390 const IPosition& cursorAxes);
391 // </group>
392
393 // Function which returns the shape of the cursor. This always includes
394 // all axes (ie. it includes degenerates axes)
395 virtual IPosition cursorShape() const;
396
397 // Function which returns the axes of the cursor.
398 virtual IPosition cursorAxes() const;
399
400 // Function which returns "True" if the increment/decrement operators have
401 // moved the cursor position such that part of the cursor beginning or end
402 // is hanging over the edge of the (sub)-Lattice.
403 virtual Bool hangOver() const;
404
405 // Functions to specify a "section" of the Lattice to step over. A section
406 // is defined in terms of the Bottom Left Corner (blc), Top Right Corner
407 // (trc), and step size (inc), on ALL of its axes, including degenerate
408 // axes. The step size defaults to one if not specified.
409 // <group>
410 virtual void subSection (const IPosition& blc, const IPosition& trc);
411 virtual void subSection (const IPosition& blc, const IPosition& trc,
412 const IPosition& inc);
413 // </group>
414
415 // Return the bottom left hand corner (blc), top right corner (trc) or
416 // step size (increment) used by the current sub-Lattice. If no
417 // sub-Lattice has been defined (with the <src>subSection</src> function)
418 // these functions return blc=0, trc=latticeShape-1, increment=1, ie. the
419 // entire Lattice.
420 // <group>
421 virtual IPosition blc() const;
422 virtual IPosition trc() const;
423 virtual IPosition increment() const;
424 // </group>
425
426 // Return the axis path.
427 virtual const IPosition& axisPath() const;
428
429 // Function which returns a pointer to dynamic memory of an exact copy
430 // of this instance. The pointer returned by this function must
431 // be deleted externally.
432 virtual LatticeNavigator* clone() const;
433
434 // Function which checks the internal data of this class for correct
435 // dimensionality and consistant values.
436 // Returns True if everything is fine otherwise returns False
437 virtual Bool ok() const;
438
439 // Calculate the cache size (in tiles) for this type of access to a lattice
440 // in the given row of the tiled hypercube.
441 virtual uInt calcCacheSize (const IPosition& cubeShape,
442 const IPosition& tileShape,
443 uInt maxCacheSize, uInt bucketSize) const;
444
445private:
446 // Prevent the default constructor from being used.
448 // Pad the cursor to the right number of dimensions.
449 void padCursor();
450 // Check if the cursor shape is a factor of the Lattice shape.
451 Bool niceFit() const;
452
453
454 LatticeIndexer itsIndexer;//# Knows about the (sub)-Lattice shape and how
455 //# to traverse it.
456 IPosition itsCursorAxes; //# the cursor axes
457 IPosition itsCursorShape; //# The shape of the cursor
458 IPosition itsCursorPos; //# The current position of the iterator.
459 IPosition itsAxisPath; //# the heading to follow for the cursor
460 uInt itsNsteps; //# the number of iterator steps taken thus far;
461 //# set to 0 on reset ()
462 Bool itsEnd; //# is the cursor beyond the end?
463 Bool itsStart; //# is the cursor at the beginning?
464 Bool itsNiceFit; //# if the cursor shape is a sub-multiple of the
465 //# Lattice shape then set this to True. Used to
466 //# avoid needing to test for a cursor hanging
467 //# over the edge of the lattice.
468 Bool itsHangover; //# this data member is set by the increment and
469 //# decrement operators if itsNiceFit == False. It
470 //# is used to tell if the cursor "Hangs over"
471 //# the edge of the lattice shape.
472 uInt itsPolicy; //# what to do if the cursor does hang over
473};
474
475
476
477} //# NAMESPACE CASACORE - END
478
479#endif
virtual void subSection(const IPosition &blc, const IPosition &trc, const IPosition &inc)
virtual Bool atEnd() const
Function which returns "True" if an attempt has been made to increment the cursor beyond the end of t...
virtual Bool ok() const
Function which checks the internal data of this class for correct dimensionality and consistant value...
virtual uInt nsteps() const
Function to return the number of steps (increments & decrements) taken since construction (or since l...
virtual IPosition increment() const
virtual LatticeNavigator * clone() const
Function which returns a pointer to dynamic memory of an exact copy of this instance.
virtual IPosition latticeShape() const
Functions which return the shape of the Lattice being iterated through.
virtual IPosition subLatticeShape() const
LatticeStepper(const LatticeStepper &other)
The copy constructor uses copy semantics.
LatticeStepper(const IPosition &latticeShape, const IPosition &cursorShape, const IPosition &cursorAxes, const IPosition &axisPath, const uInt hangOverPolicy=PAD)
Same as the above constructor except that the cursor axes are explicitly specified.
virtual Bool hangOver() const
Function which returns "True" if the increment/decrement operators have moved the cursor position suc...
void setCursorShape(const IPosition &cursorShape, const IPosition &cursorAxes)
virtual void reset()
Function to move the cursor to the beginning of the (sub)-Lattice.
virtual void subSection(const IPosition &blc, const IPosition &trc)
Functions to specify a "section" of the Lattice to step over.
virtual Bool operator++(int)
Increment operator (postfix version) - move the cursor forward one step.
virtual IPosition endPosition() const
Functions which return the current position of the end of the cursor.
virtual uInt calcCacheSize(const IPosition &cubeShape, const IPosition &tileShape, uInt maxCacheSize, uInt bucketSize) const
Calculate the cache size (in tiles) for this type of access to a lattice in the given row of the tile...
virtual Bool operator--(int)
Decrement operator (postfix version) - move the cursor backwards one step.
hangOverPolicy
The hangOverPolicy enumerator is used in the constructors to indicate what this class should do when ...
@ RESIZE
RESIZE means that the cursor shape is adjusted whenever it approaches the edges of the Lattice so tha...
@ PAD
PAD is the default and means that the cursor size supplied by the user is kept fixed.
virtual IPosition position() const
Functions which return the current position of the beginning of the cursor.
virtual IPosition cursorShape() const
Function which returns the shape of the cursor.
LatticeStepper & operator=(const LatticeStepper &other)
The assignment operator uses copy semantics.
Bool niceFit() const
Check if the cursor shape is a factor of the Lattice shape.
virtual IPosition blc() const
Return the bottom left hand corner (blc), top right corner (trc) or step size (increment) used by the...
virtual IPosition cursorAxes() const
Function which returns the axes of the cursor.
virtual IPosition relativePosition() const
virtual IPosition relativeEndPosition() const
LatticeStepper()
Prevent the default constructor from being used.
LatticeStepper(const IPosition &latticeShape, const IPosition &cursorShape, const IPosition &axisPath, const uInt hangOverPolicy=PAD)
Same as the above constructor except that the axis path is explicitly specified.
void padCursor()
Pad the cursor to the right number of dimensions.
LatticeStepper(const IPosition &latticeShape, const IPosition &cursorShape, const uInt hangOverPolicy=PAD)
The first argument is the shape of the Lattice to be iterated and the second argument is the shape of...
void setCursorShape(const IPosition &cursorShape)
Functions to change the cursor shape to a new one.
virtual Bool atStart() const
Function which returns "True" if the cursor is at the beginning of the (sub)-Lattice,...
virtual IPosition trc() const
virtual const IPosition & axisPath() const
Return the axis path.
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40