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