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