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