casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Cube.h
Go to the documentation of this file.
1 //# Cube.h: A 3-D Specialization of the Array Class
2 //# Copyright (C) 1993,1994,1995,1996,1999,2000,2001,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: aips2-request@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 //# $Id$
27 
28 #ifndef CASA_CUBE_H
29 #define CASA_CUBE_H
30 
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
35 
36 namespace casacore { //#Begin casa namespace
37 
38 //# Forward Declarations
39 template<class T> class Matrix;
40 
41 
42 // <summary> A 3-D Specialization of the Array class </summary>
43 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
44 // </reviewed>
45 //
46 // Cube objects are three-dimensional specializations (e.g., more convenient
47 // and efficient indexing) of the general Array class. You might also want
48 // to look at the Array documentation to see inherited functionality.
49 //
50 // Generally the member functions of Array are also available in
51 // Cube versions which take a pair of integers where the array
52 // needs an IPosition. Since the Cube
53 // is three-dimensional, the IPositions are overkill, although you may
54 // use those versions if you want to.
55 // <srcblock>
56 // Cube<Int> ci(100,100,100); // Shape is 100x100
57 // ci.resize(50,50,50); // Shape now 50x50
58 // </srcblock>
59 //
60 // Slices may be taken with the Slice class. To take a slice, one "indexes"
61 // with one Slice(start, length, inc) for each axis, where end and inc
62 // are optional. Additionally, there is an xyPlane()
63 // member function which return a Matrix which corresponds to some plane:
64 // <srcblock>
65 // Cube<Float> cube(10,20,30);
66 // for(uInt i=0; i < 30; i++) {
67 // cube.xyPlane(i) = i; // Set every 10x20 plane to its "height"
68 // }
69 // </srcblock>
70 //
71 // Element-by-element arithmetic and logical operations are available (in
72 // aips/ArrayMath.h and aips/ArrayLogical.h).
73 //
74 // As with the Arrays, if the preprocessor symbol AIPS_DEBUG is
75 // defined at compile time invariants will be checked on entry to most
76 // member functions. Additionally, if AIPS_ARRAY_INDEX_CHECK is defined
77 // index operations will be bounds-checked. Neither of these should
78 // be defined for production code.
79 
80 template<class T> class Cube : public Array<T>
81 {
82 public:
83 
84  // A Cube of length zero in each dimension; zero origin.
85  Cube();
86 
87  // A l1xl2xl3 sized cube.
88  Cube(size_t l1, size_t l2, size_t l3);
89 
90  // A l1xl2xl3 sized cube.
91  Cube(size_t l1, size_t l2, size_t l3, ArrayInitPolicy initPolicy);
92 
93  // A l1xl2xl3 sized cube.
94  // Fill it with the initial value.
95  Cube(size_t l1, size_t l2, size_t l3, const T &initialValue);
96 
97  // A Cube where the shape ("len") is defined with IPositions.
98  Cube(const IPosition &len);
99 
100  // A Cube where the shape ("len") is defined with IPositions.
101  Cube(const IPosition &len, ArrayInitPolicy initPolicy);
102 
103  // A Cube where the shape ("len") is defined with IPositions.
104  // Fill it with the initial value.
105  Cube(const IPosition &len, const T &initialValue);
106 
107  // The copy constructor uses reference semantics.
108  Cube(const Cube<T> &);
109 
110  // Construct a cube by reference from "other". "other must have
111  // ndim() of 3 or less. The warning which applies to the copy constructor
112  // is also valid here.
113  Cube(const Array<T> &);
114 
115  // Create an Cube of a given shape from a pointer.
116  Cube(const IPosition &shape, T *storage, StorageInitPolicy policy = COPY);
117  // Create an Cube of a given shape from a pointer.
118  Cube(const IPosition &shape, T *storage, StorageInitPolicy policy, AbstractAllocator<T> const &allocator);
119  // Create an Cube of a given shape from a pointer. Because the pointer
120  // is const, a copy is always made.
121  Cube(const IPosition &shape, const T *storage);
122 
123  // Define a destructor, otherwise the (SUN) compiler makes a static one.
124  virtual ~Cube();
125 
126  // Assign the other array (which must be dimension 3) to this cube.
127  // If the shapes mismatch, this array is resized.
128  virtual void assign (const Array<T>& other);
129 
130  // Make this cube a reference to other. Other must be of dimensionality
131  // 3 or less.
132  virtual void reference(const Array<T> &other);
133 
134  // Resize to the given shape.
135  // Resize without argument is equal to resize(0,0,0).
136  // <group>
137  using Array<T>::resize;
138  void resize(size_t nx, size_t ny, size_t nz, Bool copyValues=False) {
139  Cube<T>::resize(nx, ny, nz, copyValues, Array<T>::defaultArrayInitPolicy());
140  }
141  void resize(size_t nx, size_t ny, size_t nz, Bool copyValues, ArrayInitPolicy policy);
142  virtual void resize();
143  virtual void resize(const IPosition &newShape, Bool copyValues, ArrayInitPolicy policy);
144  // </group>
145 
146  // Copy the values from other to this cube. If this cube has zero
147  // elements then it will resize to be the same shape as other; otherwise
148  // other must conform to this.
149  // Note that the assign function can be used to assign a
150  // non-conforming cube.
151  // <group>
152  Cube<T> &operator=(const Cube<T> &other);
153  virtual Array<T> &operator=(const Array<T> &other);
154  // </group>
155 
156  // Copy val into every element of this cube; i.e. behaves as if
157  // val were a constant conformant cube.
158  Array<T> &operator=(const T &val)
159  { return Array<T>::operator=(val); }
160 
161  // Copy to this those values in marray whose corresponding elements
162  // in marray's mask are True.
164  { Array<T> (*this) = marray; return *this; }
165 
166 
167  // Single-pixel addressing. If AIPS_ARRAY_INDEX_CHECK is defined,
168  // bounds checking is performed.
169  // <group>
170  T &operator()(const IPosition &i)
171  { return Array<T>::operator()(i); }
172  const T &operator()(const IPosition &i) const
173  { return Array<T>::operator()(i); }
174 
175  T &operator()(size_t i1, size_t i2, size_t i3)
176  {
177 #if defined(AIPS_ARRAY_INDEX_CHECK)
178  this->validateIndex(i1, i2, i3); // Throws an exception on failure
179 #endif
180  return this->begin_p[i1*xinc_p + i2*yinc_p + i3*zinc_p];
181  }
182 
183  const T &operator()(size_t i1, size_t i2, size_t i3) const
184  {
185 #if defined(AIPS_ARRAY_INDEX_CHECK)
186  this->validateIndex(i1, i2, i3); // Throws an exception on failure
187 #endif
188  return this->begin_p[i1*xinc_p + i2*yinc_p + i3*zinc_p];
189  }
190 
191  //# Have function at (temporarily) to check if test on contiguous is
192  //# indeed slower than always using multiplication in operator()
193  T &at(size_t i1, size_t i2, size_t i3)
194  {
195 #if defined(AIPS_ARRAY_INDEX_CHECK)
196  this->validateIndex(i1, i2, i3); // Throws an exception on failure
197 #endif
198  return this->contiguous_p ? this->begin_p[i1 + i2*yinc_p + i3*zinc_p] :
199  this->begin_p[i1*xinc_p + i2*yinc_p + i3*zinc_p];
200  }
201 
202  const T &at(size_t i1, size_t i2, size_t i3) const
203  {
204 #if defined(AIPS_ARRAY_INDEX_CHECK)
205  this->validateIndex(i1, i2, i3); // Throws an exception on failure
206 #endif
207  return this->contiguous_p ? this->begin_p[i1 + i2*yinc_p + i3*zinc_p] :
208  this->begin_p[i1*xinc_p + i2*yinc_p + i3*zinc_p];
209  }
210  // </group>
211 
212  // Take a slice of this cube. Slices are always indexed starting
213  // at zero. This uses reference semantics, i.e. changing a value
214  // in the slice changes the original.
215  // <srcblock>
216  // Cube<Double> vd(100,100,100);
217  // //...
218  // vd(Slice(0,10),Slice(10,10,Slice(0,10))) = -1.0; // sub-cube set to -1.0
219  // </srcblock>
220  // <group>
221  Cube<T> operator()(const Slice &sliceX, const Slice &sliceY,
222  const Slice &sliceZ);
223  const Cube<T> operator()(const Slice &sliceX, const Slice &sliceY,
224  const Slice &sliceZ) const;
225  // </group>
226 
227  // Slice using IPositions. Required to be defined, otherwise the base
228  // class versions are hidden.
229  // <group>
230  Array<T> operator()(const IPosition &blc, const IPosition &trc,
231  const IPosition &incr)
232  { return Array<T>::operator()(blc,trc,incr); }
233  const Array<T> operator()(const IPosition &blc, const IPosition &trc,
234  const IPosition &incr) const
235  { return Array<T>::operator()(blc,trc,incr); }
236  Array<T> operator()(const IPosition &blc, const IPosition &trc)
237  { return Array<T>::operator()(blc,trc); }
238  const Array<T> operator()(const IPosition &blc, const IPosition &trc) const
239  { return Array<T>::operator()(blc,trc); }
240  Array<T> operator()(const Slicer& slicer)
241  { return Array<T>::operator()(slicer); }
242  const Array<T> operator()(const Slicer& slicer) const
243  { return Array<T>::operator()(slicer); }
244  // </group>
245 
246 
247  // The array is masked by the input LogicalArray.
248  // This mask must conform to the array.
249  // <group>
250 
251  // Return a MaskedArray.
252  const MaskedArray<T> operator() (const LogicalArray &mask) const
253  { return Array<T>::operator() (mask); }
254 
255  // Return a MaskedArray.
256  MaskedArray<T> operator() (const LogicalArray &mask)
257  { return Array<T>::operator() (mask); }
258 
259  // </group>
260 
261 
262  // The array is masked by the input MaskedLogicalArray.
263  // The mask is effectively the AND of the internal LogicalArray
264  // and the internal mask of the MaskedLogicalArray.
265  // The MaskedLogicalArray must conform to the array.
266  // <group>
267 
268  // Return a MaskedArray.
269  const MaskedArray<T> operator() (const MaskedLogicalArray &mask) const
270  { return Array<T>::operator() (mask); }
271 
272  // Return a MaskedArray.
273  MaskedArray<T> operator() (const MaskedLogicalArray &mask)
274  { return Array<T>::operator() (mask); }
275 
276  // </group>
277 
278 
279  // Extract a plane as a matrix referencing the original data.
280  // Of course you could also use a Matrix
281  // iterator on the cube.
282  // <group>
283  Matrix<T> xyPlane(size_t zplane);
284  const Matrix<T> xyPlane(size_t zplane) const;
285  Matrix<T> xzPlane(size_t yplane);
286  const Matrix<T> xzPlane(size_t yplane) const;
287  Matrix<T> yzPlane(size_t xplane);
288  const Matrix<T> yzPlane(size_t xplane) const;
289  // </group>
290 
291  // The length of each axis of the cube.
292  const IPosition &shape() const
293  { return this->length_p; }
294  void shape(Int &s1, Int &s2, Int &s3) const
295  { s1 = this->length_p(0); s2=this->length_p(1); s3=this->length_p(2); }
296 
297  // The number of rows in the Cube, i.e. the length of the first axis.
298  size_t nrow() const
299  { return this->length_p(0); }
300 
301  // The number of columns in the Cube, i.e. the length of the 2nd axis.
302  size_t ncolumn() const
303  { return this->length_p(1); }
304 
305  // The number of planes in the Cube, i.e. the length of the 3rd axis.
306  size_t nplane() const
307  { return this->length_p(2); }
308 
309  // Checks that the cube is consistent (invariants check out).
310  virtual Bool ok() const;
311 
312 protected:
313  virtual void preTakeStorage(const IPosition &shape);
314  virtual void postTakeStorage();
315  // Remove the degenerate axes from other and store result in this cube.
316  // An exception is thrown if removing degenerate axes does not result
317  // in a cube.
318  virtual void doNonDegenerate(const Array<T> &other,
319  const IPosition &ignoreAxes);
320 
321 private:
322  // Cached constants to improve indexing.
323  size_t xinc_p, yinc_p, zinc_p;
324  // Helper fn to calculate the indexing constants.
325  void makeIndexingConstants();
326 };
327 
328 } //#End casa namespace
329 #ifndef CASACORE_NO_AUTO_TEMPLATES
330 #include <casacore/casa/Arrays/Cube.tcc>
331 #endif //# CASACORE_NO_AUTO_TEMPLATES
332 #endif
Bool contiguous_p
Are the data contiguous?
Definition: ArrayBase.h:268
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
void makeIndexingConstants()
Helper fn to calculate the indexing constants.
Cube< T > & operator=(const Cube< T > &other)
Copy the values from other to this cube.
const Array< T > operator()(const IPosition &blc, const IPosition &trc) const
Definition: Cube.h:238
int Int
Definition: aipstype.h:50
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
T & operator()(const IPosition &i)
Single-pixel addressing.
Definition: Cube.h:170
Cube()
A Cube of length zero in each dimension; zero origin.
T & operator()(size_t i1, size_t i2, size_t i3)
Definition: Cube.h:175
size_t nrow() const
The number of rows in the Cube, i.e.
Definition: Cube.h:298
const IPosition & shape() const
The length of each axis of the cube.
Definition: Cube.h:292
A 3-D Specialization of the Array class.
virtual Bool ok() const
Checks that the cube is consistent (invariants check out).
size_t ncolumn() const
The number of columns in the Cube, i.e.
Definition: Cube.h:302
A 2-D Specialization of the Array class.
void shape(Int &s1, Int &s2, Int &s3) const
Definition: Cube.h:294
TableExprNode marray(const TableExprNode &array, const TableExprNode &mask)
Form a masked array.
Definition: ExprNode.h:1892
virtual void reference(const Array< T > &other)
Make this cube a reference to other.
size_t yinc_p
Definition: Cube.h:323
A global enum used by some Array/Block constructors.
Definition: Allocator.h:54
IPosition length_p
Used to hold the shape, increment into the underlying storage and originalLength of the array...
Definition: ArrayBase.h:271
virtual ~Cube()
Define a destructor, otherwise the (SUN) compiler makes a static one.
Array< T > operator()(const IPosition &blc, const IPosition &trc)
Definition: Cube.h:236
Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr)
Slice using IPositions.
Definition: Cube.h:230
const Array< T > operator()(const Slicer &slicer) const
Definition: Cube.h:242
Class for masking an Array for operations on that Array.
T & at(size_t i1, size_t i2, size_t i3)
Definition: Cube.h:193
void validateIndex(const IPosition &) const
virtual void preTakeStorage(const IPosition &shape)
pre/post processing hook of takeStorage() for subclasses.
size_t xinc_p
Cached constants to improve indexing.
Definition: Cube.h:323
const T & operator()(const IPosition &i) const
Definition: Cube.h:172
define a (start,length,increment) along an axis
Definition: Slice.h:93
virtual void resize()
Make this array a different shape.
virtual Array< T > & operator=(const Array< T > &other)
Copy the values in other to this.
const T & operator()(size_t i1, size_t i2, size_t i3) const
Definition: Cube.h:183
void resize(size_t nx, size_t ny, size_t nz, Bool copyValues=False)
Definition: Cube.h:138
T & operator()(const IPosition &)
Access a single element of the array.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
virtual void assign(const Array< T > &other)
Assign the other array (which must be dimension 3) to this cube.
T * begin_p
This pointer is adjusted to point to the first element of the array.
Definition: Array.h:913
const Bool False
Definition: aipstype.h:44
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
size_t nplane() const
The number of planes in the Cube, i.e.
Definition: Cube.h:306
Specify which elements to extract from an n-dimensional array.
Definition: Slicer.h:289
virtual void postTakeStorage()
size_t zinc_p
Definition: Cube.h:323
Matrix< T > xyPlane(size_t zplane)
Extract a plane as a matrix referencing the original data.
Matrix< T > yzPlane(size_t xplane)
COPY is used when an internal copy of the storage is to be made.
Definition: ArrayBase.h:56
StorageInitPolicy
A global enum used by some Array constructors.
Definition: ArrayBase.h:53
Array< T > operator()(const Slicer &slicer)
Definition: Cube.h:240
Matrix< T > xzPlane(size_t yplane)
virtual void doNonDegenerate(const Array< T > &other, const IPosition &ignoreAxes)
Remove the degenerate axes from other and store result in this cube.
const Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr) const
Definition: Cube.h:233
Array< T > & operator=(const T &val)
Copy val into every element of this cube; i.e.
Definition: Cube.h:158
const T & at(size_t i1, size_t i2, size_t i3) const
Definition: Cube.h:202
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42