casacore
Loading...
Searching...
No Matches
Slicer.h
Go to the documentation of this file.
1//# Slicer.h: specify which elements to extract from an n-dimensional array
2//# Copyright (C) 1994,1995,1997,1999
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_SLICER_2_H
27#define CASA_SLICER_2_H
28
29
30//# Includes
31#include "IPosition.h"
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35//# Forward Declarations
36class Slice;
37
38
39// <summary>
40// Specify which elements to extract from an n-dimensional array
41// </summary>
42
43// <reviewed reviewer="Paul Shannon" date="1994/07/07" tests="tSlicer">
44// The review and modification of this class were undertaken, in part,
45// with the aim of making this class header an example -- this is what
46// the Casacore project thinks a class header should look like.
47// </reviewed>
48
49// <prerequisite>
50// You should have at least a preliminary understanding of these classes:
51// <li> <linkto class=IPosition>IPosition</linkto>
52// <li> <linkto class=Array>Array</linkto>
53// <li> <linkto class=Slice>Slice</linkto>
54// </prerequisite>
55
56// <etymology>
57// The class name "Slicer" may be thought of as a short form
58// of "n-Dimensional Slice Specifier." Some confusion is possible
59// between class "Slice" and this class.
60// </etymology>
61//
62// <synopsis>
63// If you need to extract or operate upon a portion of an array,
64// the Slicer class is the best way to specify the subarray you are
65// interested in.
66//
67// Slicer has many constructors. Of these, some require that the
68// programmer supply a full specification of the array elements he
69// wants to extract; other constructors make do with partial information.
70// In the latter case, the constructor will assume sensible default values or,
71// when directed, infer missing information from the array that's getting
72// sliced (hereafter, the "source" array).
73//
74// <h4> Constructing With Full Information </h4>
75//
76// To fully specify a subarray, you must supply three pieces of information
77// for each axis of the subarray:
78//
79// <ol>
80// <li> where to start
81// <li> how many elements to extract
82// <li> what stride (or "increment" or "interval") to use: a stride of
83// "n" means pick extract only every "nth" element along an axis
84// </ol>
85//
86// The most basic constructor for Slicer illustrates this. To create
87// an Slicer for getting selected elements from a 3D array:
88//
89// <srcblock>
90// IPosition start (3,0,0,0), length (3,10,10,10), stride (3,3,3,3);
91// Slicer slicer (start, length, stride);
92// // assume proper declarations, and meaningful values in the source array
93// subArray = sourceArray (slicer);
94// </srcblock>
95// It gets elements 0,3,6,9,12,15,18,21,24,27 for each dimension.
96//
97// <note role=caution> If you wish to extract elements from the array
98// at intervals, these intervals must be regular. The interval is one
99// constant integer for each dimension of the array: it cannot be a function.
100// </note>
101//
102// <note role=caution> "length", the second parameter to the Slicer
103// constructor above, may actually be used in two ways. In normal
104// (and default) use, it specifies how many elements to select from the
105// source. In the alternative use, it specifies the index of the last element
106// to extract from the source array. This ambiguity (does "end" mean
107// "length" or does it mean "last index"?) is handled by a default
108// fourth parameter to the constructor. This code fragment will
109// extract the same subarray as the example above:
110// <srcblock>
111// IPosition start (3,0,0,0), end (3,27,27,27), stride (3,3,3,3);
112// Slicer slicer (start, end, stride, Slicer::endIsLast);
113// subArray = sourceArray (slicer);
114// </srcblock>
115// Note that in this example end(3,28,29,28) gives the same result.
116// (We use "end" as the name of the formal parameter because it supports
117// both meanings -- "last index" or "length." You may wish to use a
118// clarifying name for the actual parameter in your code, as we have
119// above when we used "length".)
120// </note>
121// Similar to Python it is possible to address the start and/or end value
122// from the end by giving a negative value (-1 means the last value).
123// However, a length and stride cannot be negative.
124// Unlike Python the end value is inclusive (as discussed above).
125// For example,
126// <srcblock>
127// Slicer slicer (IPosition(1,-4), IPosition(1,-2), Slicer::endIsLast)
128// Slicer slicer (IPosition(1,6), IPosition(1,8), Slicer::endIsLast)
129// </srcblock>
130// Both Slicers give the same result when used on a Vector with length 10.
131//
132// <h4> Constructing with Partial Information </h4>
133//
134// Some of the constructors don't require complete information: Slicer
135// either calculates sensible default values or deduces them from the
136// source array. If you do not specify a "stride" argument, for example,
137// a value of 1 will be used for all dimensions. If you specify a "start"
138// but nothing else, a stride of 1, and (perhaps against expectation)
139// a length of 1 will be used.
140//
141// Note that using a negative start or end is also partial information.
142// The actual array shape is needed to derive the exact start or end value.
143//
144// To instruct the Slicer to get otherwise unspecified information
145// from the source array, you can create an IPosition like "end"
146// as shown here:
147//
148// <srcblock>
149// IPosition start (3,0,0,0), stride (3,3,3,3);
150// IPosition end (3,Slicer::MimicSource, Slicer::MimicSource,
151// Slicer::MimicSource);
152// Slicer smartSlicer (start, end, stride);
153// // assume proper declarations...
154// subArray = sourceArray (smartSlicer)
155// </srcblock>
156//
157// If you are a library programmer, and write a class that can be sliced
158// by the Slicer class, you need to understand the mechanism for
159// completing the information which the application programmer, in using
160// your class, specified incompletely. (If you are an application
161// programmer, who wants to slice a library class, this explanation will
162// be only of academic interest.)
163//
164// When the source array (the library class you provide) gets the Slicer --
165// which typically comes when the source array is asked to return a
166// reference to a subarray -- the source does a callback to the Slicer
167// object. The source array passes its own shape as one of the arguments
168// to the Slicer callback and asks the Slicer to fill in the missing
169// values from that shape.
170//
171// In use, and with an imagined class "MyVector", code would look
172// like this:
173// <srcblock>
174// // first, a fragment from the application program:
175// IPosition start (1,10), end (1, Slicer::MimicSource);
176// Slicer slicer (start, end);
177// MyVector <int> v0 (100);
178// MyVector <int> v1 = v0 (slicer);
179// //....
180// // second, a fragment from a constructor of the library class "MyVector":
181// // the MyVector class will construct v1 as a reference to
182// // selected elements of v0, using (among other things) a
183// // callback to the slicer it was passed (above, in the
184// // construction of v1.
185// //
186// IPosition start, end, stride;
187// fullSliceInformation =
188// slicer.inferShapeFromSource (MyVector::shape(), start, end, stride);
189// // now the MyVector instance knows everything it needs to
190// // construct the instance.
191// </srcblock>
192// Please note that v1 will have a length of 90, and refer to elements
193// 10-99 of v0.
194//
195// <note role=warning> An exception will be thrown if the positions
196// defined in the Slicer exceed the source array's shape.
197// </note>
198// </synopsis>
199//
200// <example>
201// Given a large image, 4k on a side, extract (by sampling) an image
202// 1k on a side, but covering the same region as the original.
203//
204// <srcblock>
205// Image <float> image ("N5364.fits"); // a 4-d VLA map, 4096 x 4096 x 3 x 1
206// IPosition start (4,0,0,0,0), stride (4,4,4,1,1);
207// IPosition end (4, Slicer::MimicSource, Slicer::MimicSource,
208// Slicer::MimicSource, Slicer::MimicSource);
209// Slicer smartSlicer (start, end, stride);
210// // assume proper declarations...
211// Image <float> subImage = image (smartSlicer);
212// </srcblock>
213//
214// </example>
215
216// <motivation>
217// Slicer is particularly convenient for designers of other library
218// classes: Array and Image, for example. (In fact, this convenience
219// was the original motivation for the class.) The benefit
220// is this: the application programmer, who needs a slice of an Array,
221// may provide slicing specifications in many different ways, but the
222// Array class author needs to provide only one member function to
223// return the slice. The Slicer class, in effect, and with its
224// many constructors, provides a way to funnel all of the variety
225// into a single member function call to the array or image class.
226//
227// For example, imagine a 100 x 100 x 100 array from which you want to
228// extract various subarrays. Here are some of the ways you might
229// specify the the subarray in the -absence- of Slicer.
230//
231// <srcblock>
232// // preliminaries: create a cube and assign values to all elements --
233// // this will be "source" array
234// Cube <int> bigCube (IPosition (3, 100, 100, 100));
235// assignValues (bigCube);
236// // declare a smaller cube, the destination array.
237// Cube <int> smallCube (IPosition (3, 10, 10, 10));
238//
239// // example 1: use Slice objects to extract a subcube -- the first
240// // ten elements along each axis
241// Slice xIndices (0,10,1), yIndices (0,10,1), zIndices (0,10,1);
242// smallCube = bigCube (xIndices, yIndices, zIndices);
243//
244// // example 2: get the same subcube using three IPosition arguments
245// IPosition start (3,0,0,0), end (3,10,10,10), stride (3,1,1,1);
246// smallCube = bigCube (start, end, stride);
247//
248// // example 3: use 2 IPositions, letting the 3rd (stride) default to
249// // IPosition (3,1,1,1)
250// smallCube = bigCube (start, end);
251// </srcblock>
252//
253// So the Cube class (together with its base class) must define three separate
254// member functions for the essentially identical operation of
255// extracting a subcube. The same replication is also required of
256// Image, Array, and the other Array subclasses (Matrix and Vector).
257//
258// The Slicer class collapses all of this into a single member
259// function per class:
260//
261// <srcblock>
262// Slicer slicer = (call the constructor that best suits your problem)
263// smallCube = bigCube (slicer);
264// </srcblock>
265//
266// Since there are many constructors available for Slicer, you
267// can still specify the subarray that you may want in a number of
268// different ways, by constructing the Slicer in the way most natural
269// to your circumstances. You then pass the Slicer to the array, and
270// you will get back the slice you want.
271//
272// This class also offers the application programmer considerable
273// flexibility by allowing the shape of the source array to determine
274// some of the slice specification. This benefit is explained and
275// demonstrated above.
276// </motivation>
277
278// <todo asof="1994/07/01">
279// <li> This class, and the TableArray, Array and Image classes,
280// could allow for the extraction of a subarray with fewer axes than the
281// source array. At present, for example, you cannot, directly slice
282// a matrix from a cube.
283// </todo>
284
285
287{
288public:
289
290 // Define the "MimicSource" value which defines the open start or end.
291 // This value should be different from MIN_INT in IPosition.h.
292 // It should also not be the lowest possible value, since that
293 // will probably be used as an undefined value.
294 // It must be a negative number.
295 enum {MimicSource= -2147483646};
296
297 // Define the possible interpretations of the end-value.
299 // The end-values given in the constructor define the lengths.
301 // The end-values given in the constructor define the trc.
303 };
304
305 // Construct a 1-dimensional Slicer.
306 // Start and end are inferred from the source; stride=1.
307 // "endIsLength" and "endIsLast" are identical here, so there's
308 // no need to discriminate between them by using a default parameter.
310
311 // The member function <src>inferShapeFromSource</src>
312 // (invoked as a callback by the
313 // source array) will use the shape of the source array for the
314 // unspecified values: IPosition elements with the value
315 // Slicer::MimicSource
316 // <thrown>
317 // <li> ArraySlicerError
318 // </thrown>
319 // Create a Slicer with a given start, end (or length), and stride.
320 // An exception will be thrown if a negative length or non-positive
321 // stride is given or if the IPositions start, end, and stride
322 // do not have the same dimensionality.
323 // If length or stride is not given, they default to 1.
324 // <br> It is possible to leave values in start and end undefined
325 // by giving the value <src>MimicSource</src>. They can be filled
326 // in later with the actual array shape using function
327 // <src>inferShapeFromSource</src>.
328 // <group>
330 const IPosition& stride,
331 LengthOrLast endInterpretation = endIsLength);
333 LengthOrLast endInterpretation = endIsLength);
334 explicit Slicer (const IPosition& start);
335 // </group>
336
337 // Create a Slicer object from Slice objects.
338 // In a Slice object one defines the start, length, and stride for
339 // one axis.
340 // The default Slice constructor (called with no arguments) creates
341 // a Slice with start and length equal to zero, and an undefined stride.
342 // <group>
343 // Create a Slicer for a 1-dimensional array.
344 Slicer (const Slice& x, LengthOrLast endInterpretation = endIsLength);
345
346 // Create a Slicer for a 2-dim array.
347 Slicer (const Slice& x, const Slice& y,
348 LengthOrLast endInterpretation = endIsLength);
349
350 // Create a Slicer for a 3-dim array.
351 Slicer (const Slice& x, const Slice& y, const Slice& z,
352 LengthOrLast endInterpretation = endIsLength);
353 // </group>
354
355 // Equality
356 bool operator==(const Slicer&) const;
357
358 // Return the number of dimensions of the Slicer.
359 size_t ndim() const;
360
361 // This function checks all of the start, length (or end),
362 // and stride IPositions, and fills in missing values by
363 // getting the corresponding values from the shape of the
364 // source array.
365 // These will first be resized, if necessary.
366 // If, for a given axis, (end < start) , it means that a
367 // length of zero was specified.
368 // An exception is thrown if the
369 // start, end, or length exceeds the array shape or if the
370 // dimensionality of the array and Slicer do not conform.
371 // <thrown>
372 // <li> ArraySlicerError
373 // </thrown>
375 (const IPosition& shape, IPosition& startResult,
376 IPosition& endResult, IPosition& strideResult) const;
377
378 // Report the defined starting position.
379 const IPosition& start() const;
380
381 // Report the defined ending position.
382 const IPosition& end() const;
383
384 // Report the defined stride.
385 const IPosition& stride() const;
386
387 // Report the length of the resulting axes.
388 const IPosition& length() const;
389
390 // Are all values fixed (i.e., no MimicSource given)?
391 bool isFixed() const;
392
393 // Set the start and end positions. No explicit checking is done that
394 // the input parameters make sense, so you must be certain if you
395 // call these. These are useful if you have a loop with many iterations
396 // and you do not wish the overhead of creating a new Slicer object
397 // for each iteration if the only thing you are doing is adjusting
398 // the start and end positions. Other than for performance reasons,
399 // these methods should not be called and you should prefer the
400 // error checking provided by constructing a new Slicer object.
401 // Note that the length is not updated, so in principle care should
402 // be taken that the length does not change.
403 // <group>
405 { start_p = start; }
406 void setEnd (const IPosition& end)
407 { end_p = end; }
408 // </group>
409
410
411private:
416 IPosition len_p; // Length of input
417 bool fixed_p; // no MimicSource used
418
419 // Define a private constructor taking an ssize_t.
420 // This is to prevent the user from the unexpected and meaningless
421 // Slicer that would result when the ssize_t argument is promoted to
422 // an IPosition.
423 // Slicer (ssize_t);
424
425 // Check the given start, end/length and stride.
426 // Fill in the length or end.
427 // It also calls <src>fillFixed</src> to fill the fixed flag.
429
430 // Fill in start, len and stride from a Slice.
431 void fillSlice (const Slice&, ssize_t& start, ssize_t& length,
432 ssize_t& stride);
433
434 // Fill the fixed flag.
435 void fillFixed();
436};
437
438
439// <summary>IO functions for Slicer's</summary>
440// <group name="Slicer IO">
441// Print the contents of the specified Slicer to the specified stream.
442std::ostream& operator << (std::ostream& stream, const Slicer& slicer);
443// </group>
444std::string to_string(const Slicer& slicer);
445
447inline size_t Slicer::ndim() const
448 { return start_p.nelements(); }
449
450inline const IPosition& Slicer::start() const
451 { return start_p; }
452
453inline const IPosition& Slicer::end() const
454 { return end_p; }
455
456inline const IPosition& Slicer::stride() const
457 { return stride_p; }
458
459inline const IPosition& Slicer::length() const
460 { return len_p; }
461
462inline bool Slicer::isFixed() const
463 { return fixed_p; }
464
465
466
467} //# NAMESPACE CASACORE - END
468
469#endif
470
size_t nelements() const
The number of elements in this IPosition.
Definition IPosition.h:566
const IPosition & length() const
Report the length of the resulting axes.
Definition Slicer.h:459
void setEnd(const IPosition &end)
Definition Slicer.h:406
const IPosition & end() const
Report the defined ending position.
Definition Slicer.h:453
Slicer(const IPosition &start, const IPosition &end, LengthOrLast endInterpretation=endIsLength)
Slicer(const Slice &x, const Slice &y, const Slice &z, LengthOrLast endInterpretation=endIsLength)
Create a Slicer for a 3-dim array.
Slicer(const Slice &x, LengthOrLast endInterpretation=endIsLength)
Create a Slicer object from Slice objects.
IPosition stride_p
Definition Slicer.h:415
const IPosition & stride() const
Report the defined stride.
Definition Slicer.h:456
Slicer(const Slice &x, const Slice &y, LengthOrLast endInterpretation=endIsLength)
Create a Slicer for a 2-dim array.
void fillSlice(const Slice &, ssize_t &start, ssize_t &length, ssize_t &stride)
Fill in start, len and stride from a Slice.
void fillEndLen()
Define a private constructor taking an ssize_t.
LengthOrLast asEnd_p
Definition Slicer.h:412
Slicer(const IPosition &start, const IPosition &end, const IPosition &stride, LengthOrLast endInterpretation=endIsLength)
The member function inferShapeFromSource (invoked as a callback by the source array) will use the sha...
IPosition start_p
Definition Slicer.h:413
const IPosition & start() const
Report the defined starting position.
Definition Slicer.h:450
void fillFixed()
Fill the fixed flag.
Slicer(const IPosition &start)
bool isFixed() const
Are all values fixed (i.e., no MimicSource given)?
Definition Slicer.h:462
IPosition len_p
Definition Slicer.h:416
Slicer()
Construct a 1-dimensional Slicer.
LengthOrLast
Define the possible interpretations of the end-value.
Definition Slicer.h:298
@ endIsLast
The end-values given in the constructor define the trc.
Definition Slicer.h:302
@ endIsLength
The end-values given in the constructor define the lengths.
Definition Slicer.h:300
bool operator==(const Slicer &) const
Equality.
IPosition end_p
Definition Slicer.h:414
size_t ndim() const
Return the number of dimensions of the Slicer.
Definition Slicer.h:447
IPosition inferShapeFromSource(const IPosition &shape, IPosition &startResult, IPosition &endResult, IPosition &strideResult) const
This function checks all of the start, length (or end), and stride IPositions, and fills in missing v...
void setStart(const IPosition &start)
Set the start and end positions.
Definition Slicer.h:404
this file contains all the compiler specific defines
Definition mainpage.dox:28
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991
std::string to_string(const IPosition &ip)
std::ostream & operator<<(std::ostream &stream, const Slicer &slicer)
Print the contents of the specified Slicer to the specified stream.