casacore
Loading...
Searching...
No Matches
Lattice.h
Go to the documentation of this file.
1//# Lattice.h: Lattice is an abstract base class for array-like classes
2//# Copyright (C) 1994,1995,1996,1997,1998,1999,2000,2003
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_LATTICE_H
27#define LATTICES_LATTICE_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Arrays/ArrayFwd.h>
33#include <casacore/lattices/Lattices/LatticeBase.h>
34#include <casacore/casa/Arrays/Slicer.h>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38//# Forward Declarations
39class IPosition;
40class LatticeNavigator;
41template <class T> class COWPtr;
42template <class Domain, class Range> class Functional;
43template <class T> class LatticeIterInterface;
44
45
46// <summary>
47// A templated, abstract base class for array-like objects.
48// </summary>
49
50// <use visibility=export>
51
52// <reviewed reviewer="Peter Barnes" date="1999/10/30" tests="tArrayLattice.cc" demos="dLattice.cc">
53// </reviewed>
54
55// <prerequisite>
56// <li> <linkto class="IPosition"> IPosition </linkto>
57// <li> <linkto class="Array"> Array </linkto>
58// <li> <linkto class="LatticeBase"> LatticeBase </linkto>
59// <li> Abstract Base class Inheritance - try "Advanced C++" by James
60// O. Coplien, Ch. 5.
61// </prerequisite>
62
63// <etymology>
64// Lattice: "A regular, periodic configuration of points, particles,
65// or objects, throughout an area of a space..." (American Heritage Directory)
66// This definition matches our own: an n-dimensional arrangement of items,
67// on regular orthogonal axes.
68// </etymology>
69
70// <synopsis>
71// This pure abstract base class defines the operations which may be performed
72// on any concrete class derived from it. It has only a few non-pure virtual
73// member functions.
74// The fundamental contribution of this class, therefore, is that it
75// defines the operations derived classes must provide:
76// <ul>
77// <li> how to extract a "slice" (or sub-array, or subsection) from
78// a Lattice.
79// <li> how to copy a slice in.
80// <li> how to get and put a single element
81// <li> how to apply a function to all elements
82// <li> various shape related functions.
83// </ul>
84// The base class <linkto class=LatticeBase>LatticeBase</linkto> contains
85// several functions not dependent on the template parameter.
86// <note role=tip> Lattices always have a zero origin. </note>
87// </synopsis>
88
89// <example>
90// Because Lattice is an abstract base class, an actual instance of this
91// class cannot be constructed. However the interface it defines can be used
92// inside a function. This is always recommended as it allows functions
93// which have Lattices as arguments to work for any derived class.
94// <p>
95// I will give a few examples here and then refer the reader to the
96// <linkto class="ArrayLattice">ArrayLattice</linkto> class (a memory resident
97// Lattice) and the <linkto class="PagedArray">PagedArray</linkto> class (a
98// disk based Lattice) which contain further examples with concrete
99// classes (rather than an abstract one). All the examples shown below are used
100// in the <src>dLattice.cc</src> demo program.
101//
102// <h4>Example 1:</h4>
103// This example calculates the mean of the Lattice. Because Lattices can be too
104// large to fit into physical memory it is not good enough to simply use
105// <src>getSlice</src> to read all the elements into an Array. Instead the
106// Lattice is accessed in chunks which can fit into memory (the size is
107// determined by the <src>advisedMaxPixels</src> and <src>niceCursorShape</src>
108// functions). The <src>LatticeIterator::cursor()</src> function then returns
109// each of these chunks as an Array and the standard Array based functions are
110// used to calculate the mean on each of these chunks. Functions like this one
111// are the recommended way to access Lattices as the
112// <linkto class="LatticeIterator">LatticeIterator</linkto> will correctly
113// setup any required caches.
114//
115// <srcblock>
116// Complex latMean(const Lattice<Complex>& lat) {
117// const uInt cursorSize = lat.advisedMaxPixels();
118// const IPosition cursorShape = lat.niceCursorShape(cursorSize);
119// const IPosition latticeShape = lat.shape();
120// Complex currentSum = 0.0f;
121// size_t nPixels = 0u;
122// RO_LatticeIterator<Complex> iter(lat,
123// LatticeStepper(latticeShape, cursorShape));
124// for (iter.reset(); !iter.atEnd(); iter++){
125// currentSum += sum(iter.cursor());
126// nPixels += iter.cursor().nelements();
127// }
128// return currentSum/nPixels;
129// }
130// </srcblock>
131//
132// <h4>Example 2:</h4>
133// Sometimes it will be neccesary to access slices of a Lattice in a nearly
134// random way. Often this can be done using the subSection commands in the
135// <linkto class="LatticeStepper">LatticeStepper</linkto> class. But it is also
136// possible to use the getSlice and putSlice functions. The following example
137// does a two-dimensional Real to Complex Fourier transform. This example is
138// restricted to four-dimensional Arrays (unlike the previous example) and does
139// not set up any caches (caching is currently only used with PagedArrays). So
140// only use getSlice and putSlice when things cannot be done using
141// LatticeIterators.
142//
143// <srcblock>
144// void FFT2DReal2Complex(Lattice<Complex>& result,
145// const Lattice<Float>& input){
146// AlwaysAssert(input.ndim() == 4, AipsError);
147// const IPosition shape = input.shape();
148// const uInt nx = shape(0);
149// AlwaysAssert (nx > 1, AipsError);
150// const uInt ny = shape(1);
151// AlwaysAssert (ny > 1, AipsError);
152// const uInt npol = shape(2);
153// const uInt nchan = shape(3);
154// const IPosition resultShape = result.shape();
155// AlwaysAssert(resultShape.nelements() == 4, AipsError);
156// AlwaysAssert(resultShape(3) == nchan, AipsError);
157// AlwaysAssert(resultShape(2) == npol, AipsError);
158// AlwaysAssert(resultShape(1) == ny, AipsError);
159// AlwaysAssert(resultShape(0) == nx/2 + 1, AipsError);
160//
161// const IPosition inputSliceShape(4,nx,ny,1,1);
162// const IPosition resultSliceShape(4,nx/2+1,ny,1,1);
163// COWPtr<Array<Float>>
164// inputArrPtr(new Array<Float>(inputSliceShape.nonDegenerate()));
165// Array<Complex> resultArray(resultSliceShape.nonDegenerate());
166// FFTServer<Float, Complex> FFT2D(inputSliceShape.nonDegenerate());
167//
168// IPosition start(4,0);
169// Bool isARef;
170// for (uInt c = 0; c < nchan; c++){
171// for (uInt p = 0; p < npol; p++){
172// isARef = input.getSlice(inputArrPtr,
173// Slicer(start,inputSliceShape), True);
174// FFT2D.fft(resultArray, *inputArrPtr);
175// result.putSlice(resultArray, start);
176// start(2) += 1;
177// }
178// start(2) = 0;
179// start(3) += 1;
180// }
181// }
182// </srcblock>
183// Note that the <linkto class=LatticeFFT>LatticeFFT</linkto> class
184// offers a nice way to do lattice based FFTs.
185//
186// <h4>Example 3:</h4>
187// Occasionally you may want to access a few elements of a Lattice without
188// all the difficulty involved in setting up Iterators or calling getSlice
189// and putSlice. This is demonstrated in the example below.
190// Setting a single element can be done with the <src>putAt</src> function,
191// while getting a single element can be done with the parenthesis operator.
192// Using these functions to access many elements of a Lattice is not
193// recommended as this is the slowest access method.
194//
195// In this example an ideal point spread function will be inserted into an
196// empty Lattice. As with the previous examples all the action occurs
197// inside a function because Lattice is an interface (abstract) class.
198//
199// <srcblock>
200// void makePsf(Lattice<Float>& psf) {
201// const IPosition centrePos = psf.shape()/2;
202// psf.set(0.0f); // this sets all the elements to zero
203// // As it uses a LatticeIterator it is efficient
204// psf.putAt (1, centrePos); // This sets just the centre element to one
205// AlwaysAssert(near(psf(centrePos), 1.0f, 1E-6), AipsError);
206// AlwaysAssert(near(psf(centrePos*0), 0.0f, 1E-6), AipsError);
207// }
208// </srcblock>
209// </example>
210
211// <motivation>
212// Creating an abstract base class which provides a common interface between
213// memory and disk based arrays has a number of advantages.
214// <ul>
215// <li> It allows functions common to all arrays to be written independent
216// of the way the data is stored. This is illustrated in the three examples
217// above.
218// <li> It reduces the learning curve for new users who only have to become
219// familiar with one interface (ie. Lattice) rather than distinct interfaces
220// for different array types.
221// </ul>
222// </motivation>
223
224// <todo asof="1996/07/01">
225// <li> Make PagedArray cache functions virtual in this base class.
226// </todo>
227
228
229template <class T> class Lattice : public LatticeBase
230{
231public:
232 // a virtual destructor is needed so that it will use the actual destructor
233 // in the derived class
234 virtual ~Lattice();
235
236 // Make a copy of the derived object (reference semantics).
237 virtual Lattice<T>* clone() const = 0;
238
239 // Get the data type of the lattice.
240 virtual DataType dataType() const;
241
242 // Return the value of the single element located at the argument
243 // IPosition.
244 // <br> The default implementation uses getSlice.
245 // <group>
246 T operator() (const IPosition& where) const;
247 virtual T getAt (const IPosition& where) const;
248 // </group>
249
250 // Put the value of a single element.
251 // <br> The default implementation uses putSlice.
252 virtual void putAt (const T& value, const IPosition& where);
253
254 // Functions which extract an Array of values from a Lattice. All the
255 // IPosition arguments must have the same number of axes as the underlying
256 // Lattice, otherwise, an exception is thrown. <br>
257 // The parameters are:
258 // <ul>
259 // <li> buffer: a <src>COWPtr<Array<T>></src> or an
260 // <src>Array<T></src>. See example 2 above for an example.
261 // <li> start: The starting position (or Bottom Left Corner), within
262 // the Lattice, of the data to be extracted.
263 // <li> shape: The shape of the data to be extracted. This is not a
264 // position within the Lattice but the actual shape the buffer will
265 // have after this function is called. This argument added
266 // to the "start" argument should be the "Top Right Corner".
267 // <li> stride: The increment for each axis. A stride of
268 // one will return every data element, a stride of two will return
269 // every other element. The IPosition elements may be different for
270 // each respective axis. Thus, a stride of IPosition(3,1,2,3) says:
271 // fill the buffer with every element whose position has a first
272 // index between start(0) and start(0)+shape(0), a second index
273 // which is every other element between start(1) and
274 // (start(1)+shape(1))*2, and a third index of every third element
275 // between start(2) and (start(2)+shape(2))*3.
276 // <li> section: Another way of specifying the start, shape and stride
277 // <li> removeDegenerateAxes: a Bool which dictates whether to remove
278 // "empty" axis created in buffer. (e.g. extracting an n-dimensional
279 // from an (n+1)-dimensional will fill 'buffer' with an array that
280 // has a degenerate axis (i.e. one axis will have a length = 1.)
281 // Setting removeDegenerateAxes = True will return a buffer with
282 // a shape that doesn't reflect these superfluous axes.)
283 // </ul>
284 //
285 // The derived implementations of these functions return
286 // 'True' if "buffer" is a reference to Lattice data and 'False' if it
287 // is a copy.
288 // <group>
290 Bool removeDegenerateAxes=False) const;
291 Bool getSlice (COWPtr<Array<T>>& buffer, const Slicer& section,
292 Bool removeDegenerateAxes=False) const;
293 Bool getSlice (COWPtr<Array<T>>& buffer, const IPosition& start,
294 const IPosition& shape,
295 Bool removeDegenerateAxes=False) const;
296 Bool getSlice (COWPtr<Array<T>>& buffer, const IPosition& start,
297 const IPosition& shape, const IPosition& stride,
298 Bool removeDegenerateAxes=False) const;
299 Bool get (Array<T>& buffer,
300 Bool removeDegenerateAxes=False);
301 Bool getSlice (Array<T>& buffer, const Slicer& section,
302 Bool removeDegenerateAxes=False);
303 Bool getSlice (Array<T>& buffer, const IPosition& start,
304 const IPosition& shape,
305 Bool removeDegenerateAxes=False);
306 Bool getSlice (Array<T>& buffer, const IPosition& start,
307 const IPosition& shape, const IPosition& stride,
308 Bool removeDegenerateAxes=False);
309 Array<T> get (Bool removeDegenerateAxes=False) const;
310 Array<T> getSlice (const Slicer& section,
311 Bool removeDegenerateAxes=False) const;
313 const IPosition& shape,
314 Bool removeDegenerateAxes=False) const;
316 const IPosition& shape, const IPosition& stride,
317 Bool removeDegenerateAxes=False) const;
318 // </group>
319
320 // A function which places an Array of values within this instance of the
321 // Lattice at the location specified by the IPosition "where", incrementing
322 // by "stride". All of the IPosition arguments must be of the same
323 // dimensionality as the Lattice. The sourceBuffer array may (and probably
324 // will) have less axes than the Lattice. The stride defaults to one if
325 // not specified.
326 // <group>
327 void putSlice (const Array<T>& sourceBuffer, const IPosition& where,
328 const IPosition& stride)
329 { doPutSlice (sourceBuffer, where, stride); }
330 void putSlice (const Array<T>& sourceBuffer, const IPosition& where);
331 void put (const Array<T>& sourceBuffer);
332
333 // </group>
334
335 // Set all elements in the Lattice to the given value.
336 virtual void set (const T& value);
337
338 // Replace every element, x, of the Lattice with the result of f(x). You
339 // must pass in the address of the function -- so the function must be
340 // declared and defined in the scope of your program. All versions of
341 // apply require a function that accepts a single argument of type T (the
342 // Lattice template type) and return a result of the same type. The first
343 // apply expects a function with an argument passed by value; the second
344 // expects the argument to be passed by const reference; the third
345 // requires an instance of the class <src>Functional<T,T></src>. The
346 // first form ought to run faster for the built-in types, which may be an
347 // issue for large Lattices stored in memory, where disk access is not an
348 // issue.
349 // <group>
350 virtual void apply (T (*function)(T));
351 virtual void apply (T (*function)(const T&));
352 virtual void apply (const Functional<T,T>& function);
353 // </group>
354
355 // Add, subtract, multiple, or divide by another Lattice.
356 // The other Lattice can be a scalar (e.g. the result of LatticeExpr).
357 // Possible masks are not taken into account.
358 // <group>
359 void operator+= (const Lattice<T>& other)
360 { handleMath (other, 0); }
361 void operator-= (const Lattice<T>& other)
362 { handleMath (other, 1); }
363 void operator*= (const Lattice<T>& other)
364 { handleMath (other, 2); }
365 void operator/= (const Lattice<T>& other)
366 { handleMath (other, 3); }
367 // </group>
368
369 // Copy the data from the given lattice to this one.
370 // The default implementation uses function <src>copyDataTo</src>.
371 virtual void copyData (const Lattice<T>& from);
372
373 // Copy the data from this lattice to the given lattice.
374 // The default implementation only copies data (thus no mask, etc.).
375 virtual void copyDataTo (Lattice<T>& to) const;
376
377 // This function returns the advised maximum number of pixels to
378 // include in the cursor of an iterator. The default implementation
379 // returns a number that is a power of two and includes enough pixels to
380 // consume between 4 and 8 MBytes of memory.
381 virtual uInt advisedMaxPixels() const;
382
383 // These functions are used by the LatticeIterator class to generate an
384 // iterator of the correct type for a specified Lattice. Not recommended
385 // for general use.
386 // <br>The default implementation creates a LatticeIterInterface object.
388 Bool useRef) const;
389
390 // The functions (in the derived classes) doing the actual work.
391 // These functions are public, so they can be used internally in the
392 // various Lattice classes, which is especially useful for doGetSlice.
393 // <br>However, doGetSlice does not call Slicer::inferShapeFromSource
394 // to fill in possible unspecified section values. Therefore one
395 // should normally use one of the get(Slice) functions. doGetSlice
396 // should be used with care and only when performance is an issue.
397 // <group>
398 virtual Bool doGetSlice (Array<T>& buffer, const Slicer& section) = 0;
399 virtual void doPutSlice (const Array<T>& buffer, const IPosition& where,
400 const IPosition& stride) = 0;
401 // </group>
402
403protected:
404 // Define default constructor to satisfy compiler.
406
407 // Handle the Math operators (+=, -=, *=, /=).
408 // They work similarly to copyData(To).
409 // However, they are not defined for Bool types, thus specialized below.
410 // <group>
411 virtual void handleMath (const Lattice<T>& from, int oper);
412 virtual void handleMathTo (Lattice<T>& to, int oper) const;
413 // </group>
414
415 // Copy constructor and assignment can only be used by derived classes.
416 // <group>
418 : LatticeBase() {}
420 { return *this; }
421 // </group>
422};
423
424
425template<> inline
427 { throwBoolMath(); }
428
429//# Declare extern templates for often used types.
430 extern template class Lattice<Float>;
431 extern template class Lattice<Complex>;
432
433
434} //# NAMESPACE CASACORE - END
435
436//# There is a problem in including Lattice.tcc, because it needs
437//# LatticeIterator.h which in its turn includes Lattice.h again.
438//# So in a source file including LatticeIterator.h, Lattice::set fails
439//# to compile, because the LatticeIterator declarations are not seen yet.
440//# Therefore LatticeIterator.h is included here, while LatticeIterator.h
441//# includes Lattice.tcc.
442#ifndef CASACORE_NO_AUTO_TEMPLATES
443#include <casacore/lattices/Lattices/LatticeIterator.h>
444#endif //# CASACORE_NO_AUTO_TEMPLATES
445#endif
virtual IPosition shape() const =0
Return the shape of the Lattice including all degenerate axes (ie.
void putSlice(const Array< T > &sourceBuffer, const IPosition &where)
void operator-=(const Lattice< T > &other)
Definition Lattice.h:361
virtual void apply(T(*function)(const T &))
Bool getSlice(COWPtr< Array< T > > &buffer, const Slicer &section, Bool removeDegenerateAxes=False) const
Bool get(COWPtr< Array< T > > &buffer, Bool removeDegenerateAxes=False) const
Functions which extract an Array of values from a Lattice.
virtual void handleMath(const Lattice< T > &from, int oper)
Handle the Math operators (+=, -=, *=, /=).
void put(const Array< T > &sourceBuffer)
Array< T > getSlice(const IPosition &start, const IPosition &shape, const IPosition &stride, Bool removeDegenerateAxes=False) const
virtual void copyData(const Lattice< T > &from)
Copy the data from the given lattice to this one.
Bool getSlice(Array< T > &buffer, const IPosition &start, const IPosition &shape, const IPosition &stride, Bool removeDegenerateAxes=False)
virtual T getAt(const IPosition &where) const
Bool getSlice(COWPtr< Array< T > > &buffer, const IPosition &start, const IPosition &shape, Bool removeDegenerateAxes=False) const
Lattice< T > & operator=(const Lattice< T > &)
Definition Lattice.h:419
Lattice()
Define default constructor to satisfy compiler.
Definition Lattice.h:405
virtual void apply(T(*function)(T))
Replace every element, x, of the Lattice with the result of f(x).
Bool get(Array< T > &buffer, Bool removeDegenerateAxes=False)
virtual ~Lattice()
a virtual destructor is needed so that it will use the actual destructor in the derived class
Lattice(const Lattice< T > &)
Copy constructor and assignment can only be used by derived classes.
Definition Lattice.h:417
virtual Lattice< T > * clone() const =0
Make a copy of the derived object (reference semantics).
virtual Bool doGetSlice(Array< T > &buffer, const Slicer &section)=0
The functions (in the derived classes) doing the actual work.
Bool getSlice(Array< T > &buffer, const IPosition &start, const IPosition &shape, Bool removeDegenerateAxes=False)
virtual LatticeIterInterface< T > * makeIter(const LatticeNavigator &navigator, Bool useRef) const
These functions are used by the LatticeIterator class to generate an iterator of the correct type for...
virtual void putAt(const T &value, const IPosition &where)
Put the value of a single element.
void operator/=(const Lattice< T > &other)
Definition Lattice.h:365
Array< T > getSlice(const IPosition &start, const IPosition &shape, Bool removeDegenerateAxes=False) const
void operator*=(const Lattice< T > &other)
Definition Lattice.h:363
virtual void set(const T &value)
Set all elements in the Lattice to the given value.
virtual void handleMathTo(Lattice< T > &to, int oper) const
void operator+=(const Lattice< T > &other)
Add, subtract, multiple, or divide by another Lattice.
Definition Lattice.h:359
void putSlice(const Array< T > &sourceBuffer, const IPosition &where, const IPosition &stride)
A function which places an Array of values within this instance of the Lattice at the location specif...
Definition Lattice.h:327
Bool getSlice(Array< T > &buffer, const Slicer &section, Bool removeDegenerateAxes=False)
virtual void doPutSlice(const Array< T > &buffer, const IPosition &where, const IPosition &stride)=0
Array< T > getSlice(const Slicer &section, Bool removeDegenerateAxes=False) const
virtual void apply(const Functional< T, T > &function)
virtual void copyDataTo(Lattice< T > &to) const
Copy the data from this lattice to the given lattice.
virtual uInt advisedMaxPixels() const
This function returns the advised maximum number of pixels to include in the cursor of an iterator.
Array< T > get(Bool removeDegenerateAxes=False) const
Bool getSlice(COWPtr< Array< T > > &buffer, const IPosition &start, const IPosition &shape, const IPosition &stride, Bool removeDegenerateAxes=False) const
T operator()(const IPosition &where) const
Return the value of the single element located at the argument IPosition.
virtual DataType dataType() const
Get the data type of the lattice.
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.