lsst.afw  14.0-59-g0c7cc230b
SpanSetFunctorGetters.h
Go to the documentation of this file.
1 
2 
3 // -*- lsst-c++ -*-
4 
5 /*
6  * LSST Data Management System
7  * Copyright 2008-2016 AURA/LSST.
8  *
9  * This product includes software developed by the
10  * LSST Project (http://www.lsst.org/).
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the LSST License Statement and
23  * the GNU General Public License along with this program. If not,
24  * see <https://www.lsstcorp.org/LegalNotices/>.
25  */
26 
27 #ifndef LSST_AFW_GEOM_SPANSETFUNCTORGETTERS_H
28 #define LSST_AFW_GEOM_SPANSETFUNCTORGETTERS_H
29 
30 #include <type_traits>
31 #include "lsst/afw/geom/Span.h"
32 #include "lsst/afw/geom/Point.h"
34 #include "lsst/afw/image/Image.h"
35 #include "lsst/afw/image/Mask.h"
36 #include "lsst/pex/exceptions.h"
37 
38 namespace lsst {
39 namespace afw {
40 namespace geom {
41 namespace details {
42 
43 /* These variadic functions exist because of a current limitation in c++ where
44  * calls of the form foo(x)... are not possible unless it is used as a parameter
45  * for a second function. The following functions take in a variadic parameter
46  * pack, and make recursive calls until the corresponding class method has been
47  * called on all the variadic parameters
48  */
49 
50 template <typename T>
51 void variadicSpanSetter(Span const spn, T& x) {
52  x.setSpan(spn);
53 }
54 
55 template <typename T, typename... Args>
56 void variadicSpanSetter(Span const spn, T& first, Args&... x) {
57  variadicSpanSetter(spn, first);
58  variadicSpanSetter(spn, x...);
59 }
60 
61 template <typename T>
62 void variadicBoundChecker(Box2I const box, int area, T const& x) {
63  x.checkExtents(box, area);
64 }
65 
66 template <typename T, typename... Args>
67 void variadicBoundChecker(Box2I const box, int area, T const& first, Args&... x) {
68  variadicBoundChecker(box, area, first);
69  variadicBoundChecker(box, area, x...);
70 }
71 
72 template <typename T>
74  x.increment();
75 }
76 
77 template <typename T, typename... Args>
78 void variadicIncrementPosition(T& first, Args&... x) {
81 }
82 
83 /* Getter classes exist to provide a common API (duck-type) for accessing data from
84  * different data-types. This common API is used by the SpanSets applyFunctor method
85  * for passing the correct references into the supplied functor.
86  */
87 
88 template <typename T>
89 class IterGetter {
90  /* Getter class to manage retrieving values from a generic iterator
91  !!! be careful !!! Because there is no way to easily check bounds
92  for a generic iterator, it is possible to pass an iterator too
93  short, in which case the apply functor will run off the end.
94 
95  Also note that because this is a generic iterator, the iterator
96  must always provide the next desired value when it is incremented.
97  For instance when iterating over a two dimensional object should
98  automatically wrap to the next line when the ++ operator reaches
99  the end of the line. In this way all generic inputs must be treated
100  as 1d arrays, of length at least the number of elements in the
101  SpanSet which originates the applyFunctor call
102  */
103 public:
105  explicit IterGetter(T iter) : _iter(iter) {}
106 
107  IterGetter(IterGetter const&) = default;
108  IterGetter(IterGetter&&) = default;
109  IterGetter& operator=(IterGetter const&) = default;
110  IterGetter& operator=(IterGetter&&) = default;
111  ~IterGetter() = default;
112 
113  // There is no good way to check the extents of a generic iterator, so make
114  // a no-op function to satisfy api
115  void checkExtents(Box2I const& bbox, int area) const {}
116 
117  void setSpan(Span const& span) {}
118 
119  void increment() { ++_iter; }
120 
121  typename std::iterator_traits<T>::reference get() const { return *_iter; }
122 
123 private:
124  T _iter;
125 };
126 
127 template <typename T>
129  // Getter class which takes in a constant value, and simply returns that value
130  // for each iteration
131 public:
132  explicit ConstantGetter(T constant) : _const(constant) {}
133 
134  ConstantGetter(ConstantGetter const&) = default;
135  ConstantGetter(ConstantGetter&&) = default;
136  ConstantGetter& operator=(ConstantGetter const&) = default;
138  ~ConstantGetter() = default;
139 
140  // Constants are simply repeated, so no need to check extents, make no-op
141  // function
142  void checkExtents(Box2I const& bbox, int area) const {}
143 
144  void setSpan(Span const& span) {}
145 
146  // No need to increment, make a no-op function
147  void increment() {}
148 
149  T get() const { return _const; }
150 
151 private:
152  T _const;
153 };
154 
155 template <typename T, int N, int C>
157  // Getter class to manage iterating though an ndarray which is interpreted as a 2D image
158 public:
159  using Reference = typename ndarray::Array<T, N, C>::Reference::Reference;
160 
161  ImageNdGetter(ndarray::Array<T, N, C> const& array, geom::Point2I const& xy0)
162  : _array(array), _xy0(xy0) {}
163 
164  ImageNdGetter(ImageNdGetter const&) = default;
165  ImageNdGetter(ImageNdGetter&&) = default;
166  ImageNdGetter& operator=(ImageNdGetter const&) = default;
167  ImageNdGetter& operator=(ImageNdGetter&&) = default;
168  ~ImageNdGetter() = default;
169 
170  void checkExtents(Box2I const& bbox, int area) const {
171  // If the bounding box lays outside the are of the image, throw an error
172  geom::Box2I arrayBBox(_xy0,
173  geom::Extent2I(_array.template getSize<1>(), _array.template getSize<0>()));
174  if (!arrayBBox.contains(bbox)) {
176  "SpanSet bounding box lands outside array");
177  }
178  }
179 
180  void setSpan(Span const& span) {
181  auto _iterY = _array.begin() + (span.getY() - _xy0.getY());
182  _iterX = (*_iterY).begin() + (span.getMinX() - _xy0.getX());
183  }
184 
185  void increment() { ++_iterX; }
186 
187  Reference get() { return *_iterX; }
188 
189 private:
190  ndarray::Array<T, N, C> _array;
191  geom::Point2I _xy0;
192  typename ndarray::Array<T, N, C>::Reference::Iterator _iterX;
193 };
194 
195 template <typename T, int inA, int inC>
197  // Getter class to manage iterating though an ndarray which is interpreted as a 1D image
198 public:
199  using Reference = typename ndarray::Array<T, inA, inC>::Reference;
200 
201  explicit FlatNdGetter(ndarray::Array<T, inA, inC> const& array) : _array(array), _iter(_array.begin()) {}
202 
203  FlatNdGetter(FlatNdGetter const&) = default;
204  FlatNdGetter(FlatNdGetter&&) = default;
205  FlatNdGetter& operator=(FlatNdGetter const&) = default;
206  FlatNdGetter& operator=(FlatNdGetter&&) = default;
207  ~FlatNdGetter() = default;
208 
209  void checkExtents(Box2I const& bbox, int area) const {
210  // If the area of the array is greater than the size of the array, throw an error
211  // as the iteration dimensions will not match
212  auto shape = _array.getShape();
213  if (area > static_cast<int>(shape[0])) {
215  "SpanSet has dimensionality greater than array");
216  }
217  }
218 
219  void setSpan(Span const& span) {}
220 
221  void increment() { ++_iter; }
222 
223  Reference get() const { return *_iter; }
224 
225 private:
226  ndarray::Array<T, inA, inC> _array;
227  typename ndarray::Array<T, inA, inC>::Iterator _iter;
228 };
229 
230 /*
231  * The MakeGetter function serves as a dispatch agent for the applyFunctor method.
232  * The overloaded function accepts various types of objects and initialized the
233  * corresponding "Getter" classes. These classes exist to structure calls to the
234  * various data-types in such a way that they all share a common API. This allows
235  * the applyFunctor method to operate on each of these data-types as if they were all
236  * the same. (aka they all correspond to the same duck-type)
237  */
238 
239 template <typename T, int inA, int inC>
241  // This function simply passes through any FlatNdGetter passed to it
242  return getter;
243 }
244 
245 template <typename T, int inA, int inB>
247  // This function simply passes though any imageNdGetter passed to it
248  return getter;
249 }
250 
251 template <typename T>
253  // Function to create a ndarray getter from an afw image
254  return ImageNdGetter<T, 2, 1>(image.getArray(), image.getXY0());
255 }
256 
257 template <typename T>
259  // Function to create a ndarray getter from an afw image
260  return ImageNdGetter<T const, 2, 1>(image.getArray(), image.getXY0());
261 }
262 
263 template <typename T>
265  // Function to create a ndarray getter from an afw image
266  return ImageNdGetter<T, 2, 1>(image.getArray(), image.getXY0());
267 }
268 
269 template <typename T>
271  // Function to create a ndarray getter from an afw image
272  return ImageNdGetter<T const, 2, 1>(image.getArray(), image.getXY0());
273 }
274 
275 template <typename T>
277  // Function to create a getter for constant numeric types. Use template type checking to ensure the
278  // type is an integral type, or floating point type
279  return ConstantGetter<T>(num);
280 }
281 
282 // There is no type trait in the standard library to check for iterator types, so we declare one here
283 // Template specialization is used here. If type T can be mapped to an iterator trait, then it should
284 // be considered as an iterator. If c++11 supported concepts this would be a perfect place for it. this
285 // is essentially a duck-type type checking mechanism
286 template <typename T, typename = void>
288 
289 template <typename T>
290 struct is_iterator<T, typename std::enable_if<
291  !std::is_same<typename std::iterator_traits<T>::value_type, void>::value>::type>
292  : std::true_type {};
293 
294 template <typename T>
296  // Use defined type trait checker to create an iterator getter if the template is an iterator type
297  return IterGetter<T>(iter);
298 }
299 }
300 }
301 }
302 } // Close namespace lsst::afw::geom::details
303 
304 namespace ndarray {
305 // These function are placed in the ndarray namespace to enable function argument namespace lookup
306 // This means the function can be used on an ndarray without the need to specify the namespace of
307 // the function itself
309 namespace afwGeom = lsst::afw::geom;
310 template <typename T, int inA, int inB>
311 
320 details::FlatNdGetter<T, inA, inB> ndFlat(ndarray::Array<T, inA, inB> const& array) {
321  // Function to mark a ndarray to be treated as a flat vector by the applyFunctor method
323 }
324 
334 template <typename T, int inA, int inB>
335 details::ImageNdGetter<T, inA, inB> ndImage(ndarray::Array<T, inA, inB> const& array,
337  // Function to mark a ndarray to be treated as a 2D image by the applyFunctor method
338  return details::ImageNdGetter<T, inA, inB>(array, xy0);
339 }
340 } // Close namespace ndarray
341 
342 #endif // LSST_AFW_GEOM_SPANSETFUNCTORGETTERS_H
void variadicSpanSetter(Span const spn, T &x)
void checkExtents(Box2I const &bbox, int area) const
A range of pixels within one row of an Image.
Definition: Span.h:55
IterGetter & operator=(IterGetter const &)=default
typename ndarray::Array< T, N, C >::Reference::Reference Reference
STL namespace.
FlatNdGetter(ndarray::Array< T, inA, inC > const &array)
typename ndarray::Array< T, inA, inC >::Reference Reference
ImageNdGetter(ndarray::Array< T, N, C > const &array, geom::Point2I const &xy0)
An integer coordinate rectangle.
Definition: Box.h:55
typename std::iterator_traits< T >::value_type type
A base class for image defects.
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:84
details::FlatNdGetter< T, inA, inB > ndFlat(ndarray::Array< T, inA, inB > const &array)
Marks a ndarray to be interpreted as a 1D vector when applying a functor from a SpanSet.
FlatNdGetter< T, inA, inC > makeGetter(FlatNdGetter< T, inA, inC > &getter)
double x
bool contains(Point2I const &point) const
Return true if the box contains the point.
void variadicBoundChecker(Box2I const box, int area, T const &x)
details::ImageNdGetter< T, inA, inB > ndImage(ndarray::Array< T, inA, inB > const &array, afwGeom::Point2I xy0=afwGeom::Point2I())
Marks a ndarray to be interpreted as an image when applying a functor from a SpanSet.
void checkExtents(Box2I const &bbox, int area) const
#define LSST_EXCEPT(type,...)
void checkExtents(Box2I const &bbox, int area) const
table::Box2IKey bbox
int getY() const
Return the y-value.
Definition: Span.h:86
int getMinX() const
Minimum x-value.
Definition: Span.h:89
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:69
void checkExtents(Box2I const &bbox, int area) const
geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: ImageBase.h:326