lsst.afw  13.0-102-g6486c126f
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 possibe 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:
104  using type = typename std::iterator_traits<T>::value_type;
105  explicit IterGetter(T iter) : _iter(iter) {}
106 
107  // There is no good way to check the extents of a generic iterator, so make
108  // a no-op function to satisfy api
109  void checkExtents(Box2I const& bbox, int area) const {}
110 
111  void setSpan(Span const& span) {}
112 
113  void increment() { ++_iter; }
114 
115  typename std::iterator_traits<T>::reference get() const { return *_iter; }
116 
117 private:
118  T _iter;
119 };
120 
121 template <typename T>
123  // Getter class which takes in a constant value, and simply returns that value
124  // for each iteration
125 public:
126  explicit ConstantGetter(T constant) : _const(constant) {}
127 
128  // Constants are simply repeated, so no need to check extents, make no-op
129  // function
130  void checkExtents(Box2I const& bbox, int area) const {}
131 
132  void setSpan(Span const& span) {}
133 
134  // No need to increment, make a no-op function
135  void increment() {}
136 
137  T get() const { return _const; }
138 
139 private:
140  T _const;
141 };
142 
143 template <typename T, int N, int C>
145  // Getter class to manage iterating though an ndarray which is interpreted as a 2D image
146 public:
147  using Reference = typename ndarray::Array<T, N, C>::Reference::Reference;
148 
149  ImageNdGetter(ndarray::Array<T, N, C> const& array, geom::Point2I const& xy0)
150  : _array(array), _xy0(xy0) {}
151 
152  void checkExtents(Box2I const& bbox, int area) const {
153  // If the bounding box lays outside the are of the image, throw an error
154  geom::Box2I arrayBBox(_xy0,
155  geom::Extent2I(_array.template getSize<1>(), _array.template getSize<0>()));
156  if (!arrayBBox.contains(bbox)) {
157  throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError,
158  "SpanSet bounding box lands outside array");
159  }
160  }
161 
162  void setSpan(Span const& span) {
163  auto _iterY = _array.begin() + (span.getY() - _xy0.getY());
164  _iterX = (*_iterY).begin() + (span.getMinX() - _xy0.getX());
165  }
166 
167  void increment() { ++_iterX; }
168 
169  Reference get() { return *_iterX; }
170 
171 private:
172  ndarray::Array<T, N, C> _array;
173  geom::Point2I _xy0;
174  typename ndarray::Array<T, N, C>::Reference::Iterator _iterX;
175 };
176 
177 template <typename T, int inA, int inC>
179  // Getter class to manage iterating though an ndarray which is interpreted as a 1D image
180 public:
181  using Reference = typename ndarray::Array<T, inA, inC>::Reference;
182 
183  explicit FlatNdGetter(ndarray::Array<T, inA, inC> const& array) : _array(array), _iter(_array.begin()) {}
184 
185  void checkExtents(Box2I const& bbox, int area) const {
186  // If the area of the array is greater than the size of the array, throw an error
187  // as the iteration dimensions will not match
188  auto shape = _array.getShape();
189  if (area > static_cast<int>(shape[0])) {
190  throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError,
191  "SpanSet has dimensionality greater than array");
192  }
193  }
194 
195  void setSpan(Span const& span) {}
196 
197  void increment() { ++_iter; }
198 
199  Reference get() const { return *_iter; }
200 
201 private:
202  ndarray::Array<T, inA, inC> _array;
203  typename ndarray::Array<T, inA, inC>::Iterator _iter;
204 };
205 
206 /*
207  * The MakeGetter function serves as a dispatch agent for the applyFunctor method.
208  * The overloaded function accepts various types of objects and initialized the
209  * corresponding "Getter" classes. These classes exist to structure calls to the
210  * various data-types in such a way that they all share a common API. This allows
211  * the applyFunctor method to operate on each of these data-types as if they were all
212  * the same. (aka they all correspond to the same duck-type)
213  */
214 
215 template <typename T, int inA, int inC>
217  // This function simply passes through any FlatNdGetter passed to it
218  return getter;
219 }
220 
221 template <typename T, int inA, int inB>
223  // This function simply passes though any imageNdGetter passed to it
224  return getter;
225 }
226 
227 template <typename T>
229  // Function to create a ndarray getter from an afw image
230  return ImageNdGetter<T, 2, 1>(image.getArray(), image.getXY0());
231 }
232 
233 template <typename T>
235  // Function to create a ndarray getter from an afw image
236  return ImageNdGetter<T const, 2, 1>(image.getArray(), image.getXY0());
237 }
238 
239 template <typename T>
241  // Function to create a ndarray getter from an afw image
242  return ImageNdGetter<T, 2, 1>(image.getArray(), image.getXY0());
243 }
244 
245 template <typename T>
247  // Function to create a ndarray getter from an afw image
248  return ImageNdGetter<T const, 2, 1>(image.getArray(), image.getXY0());
249 }
250 
251 template <typename T>
252 ConstantGetter<T> makeGetter(T num, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0) {
253  // Function to create a getter for constant numeric types. Use template type checking to ensure the
254  // type is an integral type, or floating point type
255  return ConstantGetter<T>(num);
256 }
257 
258 // There is no type trait in the standard library to check for iterator types, so we declare one here
259 // Template specialization is used here. If type T can be mapped to an iterator trait, then it should
260 // be considered as an iterator. If c++11 supported concepts this would be a perfect place for it. this
261 // is essentially a duck-type type checking mechanism
262 template <typename T, typename = void>
263 struct is_iterator : std::false_type {};
264 
265 template <typename T>
266 struct is_iterator<T, typename std::enable_if<
267  !std::is_same<typename std::iterator_traits<T>::value_type, void>::value>::type>
268  : std::true_type {};
269 
270 template <typename T>
271 IterGetter<T> makeGetter(T iter, typename std::enable_if<is_iterator<T>::value, int>::type = 0) {
272  // Use defined type trait checker to create an iterator getter if the template is an iterator type
273  return IterGetter<T>(iter);
274 }
275 }
276 }
277 }
278 } // Close namespace lsst::afw::geom::details
279 
280 namespace ndarray {
281 // These function are placed in the ndarray namespace to enable function argument namespace lookup
282 // This means the function can be used on an ndarray without the need to specify the namespace of
283 // the function itself
285 namespace afwGeom = lsst::afw::geom;
286 template <typename T, int inA, int inB>
287 
296 details::FlatNdGetter<T, inA, inB> ndFlat(ndarray::Array<T, inA, inB> const& array) {
297  // Function to mark a ndarray to be treated as a flat vector by the applyFunctor method
299 }
300 
310 template <typename T, int inA, int inB>
311 details::ImageNdGetter<T, inA, inB> ndImage(ndarray::Array<T, inA, inB> const& array,
313  // Function to mark a ndarray to be treated as a 2D image by the applyFunctor method
314  return details::ImageNdGetter<T, inA, inB>(array, xy0);
315 }
316 } // Close namespace ndarray
317 
318 #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
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.
Definition: cameraGeom.dox:3
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
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
void checkExtents(Box2I const &bbox, int area) const
int getY() const
Return the y-value.
Definition: Span.h:80
int getMinX() const
Minimum x-value.
Definition: Span.h:83
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:68
void checkExtents(Box2I const &bbox, int area) const
geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: ImageBase.h:324