casa
$Rev:20696$
|
00001 //# Slicer.h: specify which elements to extract from an n-dimensional array 00002 //# Copyright (C) 1994,1995,1997,1999 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: Slicer.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $ 00027 00028 #ifndef CASA_SLICER_H 00029 #define CASA_SLICER_H 00030 00031 00032 //# Includes 00033 #include <casa/aips.h> 00034 #include <casa/Arrays/IPosition.h> 00035 00036 namespace casa { //# NAMESPACE CASA - BEGIN 00037 00038 //# Forward Declarations 00039 class Slice; 00040 00041 00042 // <summary> 00043 // Specify which elements to extract from an n-dimensional array 00044 // </summary> 00045 00046 // <reviewed reviewer="Paul Shannon" date="1994/07/07" tests="tSlicer"> 00047 // The review and modification of this class were undertaken, in part, 00048 // with the aim of making this class header an example -- this is what 00049 // the aips++ project thinks a class header should look like. 00050 // </reviewed> 00051 00052 // <prerequisite> 00053 // You should have at least a preliminary understanding of these classes: 00054 // <li> <linkto class=IPosition>IPosition</linkto> 00055 // <li> <linkto class=Array>Array</linkto> 00056 // <li> <linkto class=Slice>Slice</linkto> 00057 // </prerequisite> 00058 00059 // <etymology> 00060 // The class name "Slicer" may be thought of as a short form 00061 // of "n-Dimensional Slice Specifier." Some confusion is possible 00062 // between class "Slice" and this class. 00063 // </etymology> 00064 // 00065 // <synopsis> 00066 // If you need to extract or operate upon a portion of an array, 00067 // the Slicer class is the best way to specify the subarray you are 00068 // interested in. 00069 // 00070 // Slicer has many constructors. Of these, some require that the 00071 // programmer supply a full specification of the array elements he 00072 // wants to extract; other constructors make do with partial information. 00073 // In the latter case, the constructor will assume sensible default values or, 00074 // when directed, infer missing information from the array that's getting 00075 // sliced (hereafter, the "source" array). 00076 // 00077 // <h4> Constructing With Full Information </h4> 00078 // 00079 // To fully specify a subarray, you must supply three pieces of information 00080 // for each axis of the subarray: 00081 // 00082 // <ol> 00083 // <li> where to start 00084 // <li> how many elements to extract 00085 // <li> what stride (or "increment" or "interval") to use: a stride of 00086 // "n" means pick extract only every "nth" element along an axis 00087 // </ol> 00088 // 00089 // The most basic constructor for Slicer illustrates this. To create 00090 // an Slicer for getting selected elements from a 3D array: 00091 // 00092 // <srcblock> 00093 // IPosition start (3,0,0,0), length (3,10,10,10), stride (3,3,3,3); 00094 // Slicer slicer (start, length, stride); 00095 // // assume proper declarations, and meaningful values in the source array 00096 // subArray = sourceArray (slicer); 00097 // </srcblock> 00098 // 00099 // <note role=caution> If you wish to extract elements from the array 00100 // at intervals, these intervals must be regular. The interval is one 00101 // constant integer for each dimension of the array: it cannot be a function. 00102 // </note> 00103 // 00104 // <note role=caution> "length", the second parameter to the Slicer 00105 // constructor above, may actually be used in two ways. In normal 00106 // (and default) use, it specifies how many elements to select from the 00107 // source. In the alternative use, it specifies the index of the last element 00108 // to extract from the source array. This ambiguity (does "end" mean 00109 // "length" or does it mean "last index"?) is handled by a default 00110 // fourth parameter to the constructor. This code fragment will 00111 // extract the same subarray as the example above: 00112 // <srcblock> 00113 // IPosition start (3,0,0,0), end (3,27,27,27), stride (3,3,3,3); 00114 // Slicer slicer (start, end, stride, Slicer::endIsLast); 00115 // subArray = sourceArray (slicer); 00116 // </srcblock> 00117 // (We use "end" as the name of the formal parameter because it supports 00118 // both meanings -- "last index" or "length." You may wish to use a 00119 // clarifying name for the actual parameter in your code, as we have 00120 // above when we used "length".) 00121 // </note> 00122 // 00123 // <h4> Constructing with Partial Information </h4> 00124 // 00125 // Some of the constructors don't require complete information: Slicer 00126 // either calculates sensible default values or deduces them from the 00127 // source array. If you do not specify a "stride" argument, for example, 00128 // a value of 1 will be used for all dimensions. If you specify a "start" 00129 // but nothing else, a stride of 1, and (perhaps against expectation) 00130 // a length of 1 will be used. 00131 // 00132 // 00133 // To instruct the Slicer to get otherwise unspecified information 00134 // from the source array, you can create an IPosition like "end" 00135 // as shown here: 00136 // 00137 // <srcblock> 00138 // IPosition start (3,0,0,0), stride (3,3,3,3); 00139 // IPosition end (3,Slicer::MimicSource, Slicer::MimicSource, 00140 // Slicer::MimicSource); 00141 // Slicer smartSlicer (start, end, stride); 00142 // // assume proper declarations... 00143 // subArray = sourceArray (smartSlicer) 00144 // </srcblock> 00145 // 00146 // If you are a library programmer, and write a class that can be sliced 00147 // by the Slicer class, you need to understand the mechanism for 00148 // completing the information which the application programmer, in using 00149 // your class, specified incompletely. (If you are an application 00150 // programmer, who wants to slice a library class, this explanation will 00151 // be only of academic interest.) 00152 // 00153 // When the source array (the library class you provide) gets the Slicer -- 00154 // which typically comes when the source array is asked to return a 00155 // reference to a subarray -- the source does a callback to the Slicer 00156 // object. The source array passes its own shape as one of the arguments 00157 // to the Slicer callback and asks the Slicer to fill in the missing 00158 // values from that shape. 00159 // 00160 // In use, and with an imagined class "MyVector", code would look 00161 // like this: 00162 // <srcblock> 00163 // // first, a fragment from the application program: 00164 // IPosition start (1,10), end (1, Slicer::MimicSource); 00165 // Slicer slicer (start, end); 00166 // MyVector <Int> v0 (100); 00167 // MyVector <Int> v1 = v0 (slicer); 00168 // //.... 00169 // // second, a fragment from a constructor of the library class "MyVector": 00170 // // the MyVector class will construct v1 as a reference to 00171 // // selected elements of v0, using (among other things) a 00172 // // callback to the slicer it was passed (above, in the 00173 // // construction of v1. 00174 // // 00175 // IPosition start, end, stride; 00176 // fullSliceInformation = 00177 // slicer.inferShapeFromSource (MyVector::shape(), start, end, stride); 00178 // // now the MyVector instance knows everything it needs to 00179 // // construct the instance. 00180 // </srcblock> 00181 // Please note that v1 will have a length of 90, and refer to elements 00182 // 10-99 of v0. 00183 // 00184 // <note role=warning> An exception will be thrown if the positions 00185 // defined in the Slicer exceed the source array's shape. 00186 // </note> 00187 // </synopsis> 00188 // 00189 // <example> 00190 // Given a large image, 4k on a side, extract (by sampling) an image 00191 // 1k on a side, but covering the same region as the original. 00192 // 00193 // <srcblock> 00194 // Image <Float> image ("N5364.fits"); // a 4-d VLA map, 4096 x 4096 x 3 x 1 00195 // IPosition start (4,0,0,0,0), stride (4,4,4,1,1); 00196 // IPosition end (4, Slicer::MimicSource, Slicer::MimicSource, 00197 // Slicer::MimicSource, Slicer::MimicSource); 00198 // Slicer smartSlicer (start, end, stride); 00199 // // assume proper declarations... 00200 // Image <Float> subImage = image (smartSlicer); 00201 // </srcblock> 00202 // 00203 // </example> 00204 00205 // <motivation> 00206 // Slicer is particularly convenient for designers of other library 00207 // classes: Array and Image, for example. (In fact, this convenience 00208 // was the original motivation for the class.) The benefit 00209 // is this: the application programmer, who needs a slice of an Array, 00210 // may provide slicing specifications in many different ways, but the 00211 // Array class author needs to provide only one member function to 00212 // return the slice. The Slicer class, in effect, and with its 00213 // many constructors, provides a way to funnel all of the variety 00214 // into a single member function call to the array or image class. 00215 // 00216 // For example, imagine a 100 x 100 x 100 array from which you want to 00217 // extract various subarrays. Here are some of the ways you might 00218 // specify the the subarray in the -absence- of Slicer. 00219 // 00220 // <srcblock> 00221 // // preliminaries: create a cube and assign values to all elements -- 00222 // // this will be "source" array 00223 // Cube <Int> bigCube (IPosition (3, 100, 100, 100)); 00224 // assignValues (bigCube); 00225 // // declare a smaller cube, the destination array. 00226 // Cube <Int> smallCube (IPosition (3, 10, 10, 10)); 00227 // 00228 // // example 1: use Slice objects to extract a subcube -- the first 00229 // // ten elements along each axis 00230 // Slice xIndices (0,10,1), yIndices (0,10,1), zIndices (0,10,1); 00231 // smallCube = bigCube (xIndices, yIndices, zIndices); 00232 // 00233 // // example 2: get the same subcube using three IPosition arguments 00234 // IPosition start (3,0,0,0), end (3,10,10,10), stride (3,1,1,1); 00235 // smallCube = bigCube (start, end, stride); 00236 // 00237 // // example 3: use 2 IPositions, letting the 3rd (stride) default to 00238 // // IPosition (3,1,1,1) 00239 // smallCube = bigCube (start, end); 00240 // </srcblock> 00241 // 00242 // So the Cube class (together with its base class) must define three separate 00243 // member functions for the essentially identical operation of 00244 // extracting a subcube. The same replication is also required of 00245 // Image, Array, and the other Array subclasses (Matrix and Vector). 00246 // 00247 // The Slicer class collapses all of this into a single member 00248 // function per class: 00249 // 00250 // <srcblock> 00251 // Slicer slicer = (call the constructor that best suits your problem) 00252 // smallCube = bigCube (slicer); 00253 // </srcblock> 00254 // 00255 // Since there are many constructors available for Slicer, you 00256 // can still specify the subarray that you may want in a number of 00257 // different ways, by constructing the Slicer in the way most natural 00258 // to your circumstances. You then pass the Slicer to the array, and 00259 // you will get back the slice you want. 00260 // 00261 // This class also offers the application programmer considerable 00262 // flexibility by allowing the shape of the source array to determine 00263 // some of the slice specification. This benefit is explained and 00264 // demonstrated above. 00265 // </motivation> 00266 00267 // <todo asof="1994/07/01"> 00268 // <li> This class, and the TableArray, Array and Image classes, 00269 // could allow for the extraction of a subarray with fewer axes than the 00270 // source array. At present, for example, you cannot, directly slice 00271 // a matrix from a cube. 00272 // </todo> 00273 00274 00275 class Slicer 00276 { 00277 public: 00278 00279 // Define the "MimicSource" value which defines the open start or end. 00280 // This value should be different from MIN_INT in IPosition.h. 00281 // It should also not be the lowest possible value, since that 00282 // will probably be used as an undefined value. 00283 enum {MimicSource= -2147483646}; 00284 00285 // Define the possible interpretations of the end-value. 00286 enum LengthOrLast { 00287 // The end-values given in the constructor define the lengths. 00288 endIsLength, 00289 // The end-values given in the constructor define the trc. 00290 endIsLast 00291 }; 00292 00293 // Construct a 1-dimensional Slicer. 00294 // Start and end are inferred from the source; stride=1. 00295 // "endIsLength" and "endIsLast" are identical here, so there's 00296 // no need to discriminate between them by using a default parameter. 00297 Slicer(); 00298 00299 // The member function <src>inferShapeFromSource</src> 00300 // (invoked as a callback by the 00301 // source array) will use the shape of the source array for the 00302 // unspecified values: IPosition elements with the value 00303 // Slicer::MimicSource 00304 // <thrown> 00305 // <li> ArraySlicerError 00306 // </thrown> 00307 // Create a Slicer with a given start, end (or length), and stride. 00308 // An exception will be thrown if a negative length or non-positive 00309 // stride is given or if the IPositions start, end, and stride 00310 // do not have the same dimensionality. 00311 // If length or stride is not given, they default to 1. 00312 // <br> It is possible to leave values in start and end undefined 00313 // by giving the value <src>MimicSource</src>. They can be filled 00314 // in later with the actual array shape using function 00315 // <src>inferShapeFromSource</src>. 00316 // <group> 00317 Slicer (const IPosition& start, const IPosition& end, 00318 const IPosition& stride, 00319 LengthOrLast endInterpretation = endIsLength); 00320 Slicer (const IPosition& start, const IPosition& end, 00321 LengthOrLast endInterpretation = endIsLength); 00322 explicit Slicer (const IPosition& start); 00323 // </group> 00324 00325 // Create a Slicer object from Slice objects. 00326 // In a Slice object one defines the start, length, and stride for 00327 // one axis. 00328 // The default Slice constructor (called with no arguments) creates 00329 // a Slice with start and length equal to zero, and an undefined stride. 00330 // <group> 00331 // Create a Slicer for a 1-dimensional array. 00332 Slicer (const Slice& x, LengthOrLast endInterpretation = endIsLength); 00333 00334 // Create a Slicer for a 2-dim array. 00335 Slicer (const Slice& x, const Slice& y, 00336 LengthOrLast endInterpretation = endIsLength); 00337 00338 // Create a Slicer for a 3-dim array. 00339 Slicer (const Slice& x, const Slice& y, const Slice& z, 00340 LengthOrLast endInterpretation = endIsLength); 00341 // </group> 00342 00343 // Copy constructor (copy semantics). 00344 Slicer (const Slicer&); 00345 00346 // Assignment (copy semantics). 00347 Slicer& operator= (const Slicer&); 00348 00349 // Equality 00350 Bool operator==(const Slicer&) const; 00351 00352 // Return the number of dimensions of the Slicer. 00353 uInt ndim() const; 00354 00355 // This function checks all of the start, length (or end), 00356 // and stride IPositions, and fills in missing values by 00357 // getting the corresponding values from the shape of the 00358 // source array. 00359 // These will first be resized, if necessary. 00360 // If, for a given axis, (end < start) , it means that a 00361 // length of zero was specified. 00362 // An exception is thrown if the 00363 // start, end, or length exceeds the array shape or if the 00364 // dimensionality of the array and Slicer do not conform. 00365 // <thrown> 00366 // <li> ArraySlicerError 00367 // </thrown> 00368 // <group> 00369 // Infer the slicer's shape from an array, using a zero origin. 00370 IPosition inferShapeFromSource 00371 (const IPosition& shape, IPosition& startResult, 00372 IPosition& endResult, IPosition& strideResult) const; 00373 00374 // Infer the slicer shape from an array, with the given origin. 00375 // The returned values are based on a zero origin. 00376 IPosition inferShapeFromSource 00377 (const IPosition& shape, const IPosition& origin, 00378 IPosition& startResult, IPosition& endResult, 00379 IPosition& strideResult) const; 00380 // </group> 00381 00382 // Report the defined starting position. 00383 const IPosition& start() const; 00384 00385 // Report the defined ending position. 00386 const IPosition& end() const; 00387 00388 // Report the defined stride. 00389 const IPosition& stride() const; 00390 00391 // Report the length of the resulting axes. 00392 const IPosition& length() const; 00393 00394 // Are all values fixed (i.e., no MimicSource given)? 00395 Bool isFixed() const; 00396 00397 private: 00398 LengthOrLast asEnd_p; 00399 IPosition start_p; 00400 IPosition end_p; 00401 IPosition stride_p; 00402 IPosition len_p; // Length of input 00403 Bool fixed_p; // no MimicSource used 00404 00405 // Define a private constructor taking an ssize_t. 00406 // This is to prevent the user from the unexpected and meaningless 00407 // Slicer that would result when the ssize_t argument is promoted to 00408 // an IPosition. 00409 Slicer (ssize_t); 00410 00411 // Check the given start, end/length and stride. 00412 // Fill in the length or end. 00413 // It also call <src>fillFixed</src> to fill the fixed flag. 00414 void fillEndLen(); 00415 00416 // Fill in start, len and stride from a Slice. 00417 void fillSlice (const Slice&, ssize_t& start, ssize_t& length, 00418 ssize_t& stride); 00419 00420 // Fill the fixed flag. 00421 void fillFixed(); 00422 }; 00423 00424 00425 // <summary>IO functions for Slicer's</summary> 00426 // <group name="Slicer IO"> 00427 // Print the contents of the specified Slicer to the specified stream. 00428 std::ostream& operator << (std::ostream& stream, const Slicer& slicer); 00429 // </group> 00430 00431 00432 00433 inline uInt Slicer::ndim() const 00434 { return start_p.nelements(); } 00435 00436 inline const IPosition& Slicer::start() const 00437 { return start_p; } 00438 00439 inline const IPosition& Slicer::end() const 00440 { return end_p; } 00441 00442 inline const IPosition& Slicer::stride() const 00443 { return stride_p; } 00444 00445 inline const IPosition& Slicer::length() const 00446 { return len_p; } 00447 00448 inline Bool Slicer::isFixed() const 00449 { return fixed_p; } 00450 00451 00452 00453 } //# NAMESPACE CASA - END 00454 00455 #endif 00456