lsst.afw  14.0-17-gfc02bf727
Extent.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2016 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * A coordinate class intended to represent offsets and dimensions.
27  */
28 #ifndef LSST_AFW_GEOM_EXTENT_H
29 #define LSST_AFW_GEOM_EXTENT_H
30 
31 #include <tuple>
32 #include <type_traits>
33 
34 #include "lsst/pex/exceptions.h"
36 
37 namespace lsst {
38 namespace afw {
39 namespace geom {
40 
41 // These are present to avoid a static assertion for instantiating computeNorm() on integer types.
42 namespace detail {
43 
44 template <int N>
46  return s.asEigen().norm();
47 }
48 
49 template <int N>
51  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot compute norm of integer extent");
52 #if 1 // make compilers happy in non-void function
53  return -1;
54 #endif
55 }
56 
57 } // namespace detail
58 
59 template <typename T, int N>
60 class ExtentBase : public CoordinateBase<Extent<T, N>, T, N> {
61  typedef CoordinateBase<Extent<T, N>, T, N> Super;
62 
63 public:
65  T computeSquaredNorm() const { return this->asEigen().squaredNorm(); }
66 
68  T computeNorm() const { return detail::computeExtentNorm(static_cast<Extent<T, N> const &>(*this)); }
69 
75  bool operator==(Extent<T, N> const &other) const { return all(this->eq(other)); }
76 
82  bool operator!=(Extent<T, N> const &other) const { return any(this->ne(other)); }
83 
97  CoordinateExpr<N> eq(Extent<T, N> const &other) const;
98  CoordinateExpr<N> ne(Extent<T, N> const &other) const;
99  CoordinateExpr<N> lt(Extent<T, N> const &other) const;
100  CoordinateExpr<N> le(Extent<T, N> const &other) const;
101  CoordinateExpr<N> gt(Extent<T, N> const &other) const;
102  CoordinateExpr<N> ge(Extent<T, N> const &other) const;
103  CoordinateExpr<N> eq(T scalar) const { return this->eq(Extent<T, N>(scalar)); }
104  CoordinateExpr<N> ne(T scalar) const { return this->ne(Extent<T, N>(scalar)); }
105  CoordinateExpr<N> lt(T scalar) const { return this->lt(Extent<T, N>(scalar)); }
106  CoordinateExpr<N> le(T scalar) const { return this->le(Extent<T, N>(scalar)); }
107  CoordinateExpr<N> gt(T scalar) const { return this->gt(Extent<T, N>(scalar)); }
108  CoordinateExpr<N> ge(T scalar) const { return this->ge(Extent<T, N>(scalar)); }
110 
117  Point<T, N> operator+(Point<T, N> const &other) const;
118  Extent<T, N> operator+(Extent<T, N> const &other) const {
119  return Extent<T, N>(this->_vector + other._vector);
120  }
121  Extent<T, N> operator-(Extent<T, N> const &other) const {
122  return Extent<T, N>(this->_vector - other._vector);
123  }
125  this->_vector += other._vector;
126  return static_cast<Extent<T, N> &>(*this);
127  }
129  this->_vector -= other._vector;
130  return static_cast<Extent<T, N> &>(*this);
131  }
132  Extent<T, N> operator+() const { return static_cast<Extent<T, N> const &>(*this); }
133  Extent<T, N> operator-() const { return Extent<T, N>(-this->_vector); }
135 
142  Extent<T, N> operator*(T scalar) const { return Extent<T, N>(this->_vector * scalar); }
144  this->_vector *= scalar;
145  return static_cast<Extent<T, N> &>(*this);
146  }
147  Extent<T, N> operator/(T scalar) const { return Extent<T, N>(this->_vector / scalar); }
149  this->_vector /= scalar;
150  return static_cast<Extent<T, N> &>(*this);
151  }
153 
155  Point<T, N> asPoint() const;
156 
158  std::stringstream out;
159  out << "Extent(";
160  for (size_t i = 0; i < N; ++i) {
161  if (i != 0) {
162  out << ",";
163  }
164  out << (*this)[i];
165  }
166  out << ")";
167  return out.str();
168  }
169 
170 protected:
172  explicit ExtentBase(T val = static_cast<T>(0)) : Super(val) {}
173 
175  template <typename Vector>
176  explicit ExtentBase(Eigen::MatrixBase<Vector> const &vector) : Super(vector) {}
177 };
178 
186 template <typename T, int N>
187 class Extent : public ExtentBase<T, N> {
188  typedef ExtentBase<T, N> Super;
189 
190 public:
191  typedef typename Super::EigenVector EigenVector;
192 
194  explicit Extent(T val = static_cast<T>(0)) : Super(val) {}
195 
197  explicit Extent(EigenVector const &vector) : Super(vector) {}
198 
200  explicit Extent(Point<T, N> const &other);
201 
203  template <typename U>
204  explicit Extent(Extent<U, N> const &other);
205  template <typename U>
206  explicit Extent(Point<U, N> const &other);
207 
209  T computeSquaredNorm() const { return this->asEigen().squaredNorm(); }
210 
212  T computeNorm() const { return this->asEigen().norm(); }
213 
214  void swap(Extent &other) { this->_swap(other); }
215 };
216 
222 template <typename T>
223 class Extent<T, 2> : public ExtentBase<T, 2> {
224  typedef ExtentBase<T, 2> Super;
225 
226 public:
227  typedef typename Super::EigenVector EigenVector;
228 
230  explicit Extent(T val = static_cast<T>(0)) : Super(val) {}
231 
233  explicit Extent(EigenVector const &vector) : Super(vector) {}
234 
236  explicit Extent(Point<T, 2> const &other);
237 
239  template <typename U>
240  explicit Extent(Extent<U, 2> const &other);
241  template <typename U>
242  explicit Extent(Point<U, 2> const &other);
243 
245  explicit Extent(T x, T y) : Super(EigenVector(x, y)) {}
246 
248  explicit Extent(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
249 
251  explicit Extent(std::pair<T, T> const &xy) : Super(EigenVector(xy.first, xy.second)) {}
252 
254  explicit Extent(std::tuple<T, T> const &xy) : Super(EigenVector(std::get<0>(xy), std::get<1>(xy))) {}
255 
256  void swap(Extent &other) { this->_swap(other); }
257 };
258 
264 template <typename T>
265 class Extent<T, 3> : public ExtentBase<T, 3> {
266  typedef ExtentBase<T, 3> Super;
267 
268 public:
269  typedef typename Super::EigenVector EigenVector;
270 
272  explicit Extent(T val = static_cast<T>(0)) : Super(val) {}
273 
275  explicit Extent(EigenVector const &vector) : Super(vector) {}
276 
278  explicit Extent(Point<T, 3> const &other);
279 
281  template <typename U>
282  explicit Extent(Extent<U, 3> const &other);
283  template <typename U>
284  explicit Extent(Point<U, 3> const &other);
285 
287  explicit Extent(T x, T y, T z) : Super(EigenVector(x, y, z)) {}
288 
290  explicit Extent(T const xyz[3]) : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
291 
293  explicit Extent(std::tuple<T, T, T> const &xyz)
294  : Super(EigenVector(std::get<0>(xyz), std::get<1>(xyz), std::get<2>(xyz))) {}
295 
296  void swap(Extent &other) { this->_swap(other); }
297 };
298 
299 // Constructor for any 2D type from 2I type
300 template <typename T>
301 template <typename U>
304  "can only construct from Extent of different but integral type");
305  this->setX(static_cast<T>(other.getX()));
306  this->setY(static_cast<T>(other.getY()));
307 };
308 
309 template <typename T>
310 template <typename U>
313  "can only construct from Extent of different but integral type");
314  this->setX(static_cast<T>(other.getX()));
315  this->setY(static_cast<T>(other.getY()));
316 };
317 
318 // Constructor for any 3D type from 3I type
319 template <typename T>
320 template <typename U>
323  "can only construct from Extent of different but integral type");
324  this->setX(static_cast<T>(other.getX()));
325  this->setY(static_cast<T>(other.getY()));
326  this->setZ(static_cast<T>(other.getZ()));
327 };
328 
329 // Constructor for any 3D type from 3I type
330 template <typename T>
331 template <typename U>
334  "can only construct from Extent of different but integral type");
335  this->setX(static_cast<T>(other.getX()));
336  this->setY(static_cast<T>(other.getY()));
337  this->setZ(static_cast<T>(other.getZ()));
338 };
339 
340 typedef Extent<int, 2> ExtentI;
346 
352 template <int N>
354 
360 template <int N>
362 
368 template <int N>
370 
371 // Some operators below need to take ExtentBase arguments rather than Extent to
372 // avoid ambiguous overloads (since some competing operators are defined as member
373 // functions on ExtentBase).
374 
375 template <typename T, int N>
376 Extent<T, N> operator*(T scalar, ExtentBase<T, N> const &rhs) {
377  return rhs * scalar;
378 }
379 
380 template <int N>
382  return Extent<double, N>(static_cast<Extent<int, N> const &>(lhs)) * rhs;
383 }
384 
385 template <int N>
386 void operator*=(ExtentBase<int, N> &lhs, double rhs) {
387  // use "N < 0" so assertion is dependent on template instantiation, instead of triggering all the time
388  static_assert(N < 0, "In-place multiplication of Extent<int,N> by double would truncate.");
389 }
390 
391 template <int N>
393  return Extent<double, N>(static_cast<Extent<int, N> const &>(lhs)) / rhs;
394 }
395 
396 template <int N>
397 void operator/=(ExtentBase<int, N> &lhs, double rhs) {
398  // use "N < 0" so assertion is dependent on template instantiation, instead of triggering all the time
399  static_assert(N < 0, "In-place division of Extent<int,N> by double would truncate.");
400 }
401 
402 template <int N>
404  return lhs * Extent<double, N>(static_cast<Extent<int, N> const &>(rhs));
405 }
406 
407 template <int N>
409  return lhs + Extent<double, N>(rhs);
410 }
411 
412 template <int N>
414  return lhs += Extent<double, N>(rhs);
415 }
416 
417 template <int N>
419  return lhs - Extent<double, N>(rhs);
420 }
421 
422 template <int N>
424  return lhs -= Extent<double, N>(rhs);
425 }
426 
427 template <int N>
429  return Extent<double, N>(lhs) + rhs;
430 }
431 
432 template <int N>
434  return Extent<double, N>(lhs) - rhs;
435 }
436 }
437 }
438 }
439 
440 #endif
Extent< T, N > & operator+=(Extent< T, N > const &other)
Definition: Extent.h:124
A boolean coordinate.
Extent< int, N > truncate(Extent< double, N > const &input)
Return the component-wise truncation (round towards zero).
Extent(std::tuple< T, T > const &xy)
Construct from std::tuple.
Definition: Extent.h:254
CoordinateExpr< N > ge(T scalar) const
Definition: Extent.h:108
Extent(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Extent.h:251
A coordinate class intended to represent absolute positions (2-d specialization). ...
Definition: Point.h:185
Extent(T x, T y)
Construct from two scalars.
Definition: Extent.h:245
void swap(Extent &other)
Definition: Extent.h:296
Extent< T, N > operator*(T scalar) const
Definition: Extent.h:142
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:272
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
CoordinateExpr< N > ne(T scalar) const
Definition: Extent.h:104
Extent(T const xy[2])
Construct from a two-element array.
Definition: Extent.h:248
CoordinateExpr< N > lt(T scalar) const
Definition: Extent.h:105
Extent< double, 2 > Extent2D
Definition: Extent.h:344
CoordinateExpr< N > eq(T scalar) const
Definition: Extent.h:103
Super::EigenVector EigenVector
Definition: Extent.h:227
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:275
Extent(std::tuple< T, T, T > const &xyz)
Construct from std::tuple.
Definition: Extent.h:293
void operator/=(ExtentBase< int, N > &lhs, double rhs)
Definition: Extent.h:397
bool operator==(Extent< T, N > const &other) const
Standard equality comparison.
Definition: Extent.h:75
A coordinate class intended to represent offsets and dimensions.
Extent(T const xyz[3])
Construct from a two-element array.
Definition: Extent.h:290
Extent< int, N > ceil(Extent< double, N > const &input)
Return the component-wise ceil (round towards more positive).
Super::EigenVector EigenVector
Definition: Extent.h:269
STL namespace.
Extent< T, N > operator+() const
Definition: Extent.h:132
T computeNorm() const
Return the L2 norm of the Extent (sqrt(x^2 + y^2 + ...)).
Definition: Extent.h:212
T computeSquaredNorm() const
Return the squared L2 norm of the Extent (x^2 + y^2 + ...).
Definition: Extent.h:209
void swap(Extent &other)
Definition: Extent.h:214
Extent< int, 2 > Extent2I
Definition: Extent.h:341
std::string toString() const
Definition: Extent.h:157
constexpr Angle operator+(Angle a, Angle d) noexcept
Sum of two angles.
Definition: Angle.h:275
CoordinateExpr< N > le(T scalar) const
Definition: Extent.h:106
Extent< double, 2 > ExtentD
Definition: Extent.h:343
A CRTP base class for coordinate objects.
A coordinate class intended to represent absolute positions.
T computeSquaredNorm() const
Return the squared L2 norm of the Extent (x^2 + y^2 + ...).
Definition: Extent.h:65
STL class.
Extent< T, N > operator/(T scalar) const
Definition: Extent.h:147
Extent< int, N > floor(Extent< double, N > const &input)
Return the component-wise floor (round towards more negative).
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
constexpr Angle operator/(Angle a, int d) noexcept
Ratio of an angle and a scalar.
Definition: Angle.h:316
void operator*=(ExtentBase< int, N > &lhs, double rhs)
Definition: Extent.h:386
A base class for image defects.
T computeNorm() const
Return the L2 norm of the Extent (sqrt(x^2 + y^2 + ...)).
Definition: Extent.h:68
Extent< T, N > & operator*=(T scalar)
Definition: Extent.h:143
T str(T... args)
Extent< T, N > & operator/=(T scalar)
Definition: Extent.h:148
double x
double computeExtentNorm(Extent< double, N > const &s)
Definition: Extent.h:45
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:233
Eigen::Matrix< int, N, 1, Eigen::DontAlign > EigenVector
Extent< int, 2 > ExtentI
Definition: Extent.h:338
ExtentBase(Eigen::MatrixBase< Vector > const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:176
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:413
Extent< double, 3 > Extent3D
Definition: Extent.h:345
#define LSST_EXCEPT(type,...)
CoordinateExpr< N > gt(T scalar) const
Definition: Extent.h:107
Extent< T, N > operator-(Extent< T, N > const &other) const
Definition: Extent.h:121
Super::EigenVector EigenVector
Definition: Extent.h:191
constexpr Angle operator*(Angle a, Angle d) noexcept
Product of two angles.
Definition: Angle.h:289
constexpr Angle operator-(Angle a, Angle d) noexcept
Difference of two angles.
Definition: Angle.h:281
bool operator!=(Extent< T, N > const &other) const
Standard inequality comparison.
Definition: Extent.h:82
Extent(T x, T y, T z)
Construct from three scalars.
Definition: Extent.h:287
Extent< int, 3 > Extent3I
Definition: Extent.h:342
Extent< T, N > operator-() const
Definition: Extent.h:133
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:423
ExtentBase(T val=static_cast< T >(0))
Construct an Extent<T,N> with all elements set to the same scalar value.
Definition: Extent.h:172
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:194
Extent< T, N > & operator-=(Extent< T, N > const &other)
Definition: Extent.h:128
void swap(Extent &other)
Definition: Extent.h:256
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:197
Extent< T, N > operator+(Extent< T, N > const &other) const
Definition: Extent.h:118
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:230
A coordinate class intended to represent absolute positions (3-d specialization). ...
Definition: Point.h:231