lsst.afw  15.0-25-gacd707fca
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:
64  ExtentBase(ExtentBase const &) = default;
65  ExtentBase(ExtentBase &&) = default;
66  ExtentBase &operator=(ExtentBase const &) = default;
67  ExtentBase &operator=(ExtentBase &&) = default;
68  ~ExtentBase() = default;
69 
71  T computeSquaredNorm() const { return this->asEigen().squaredNorm(); }
72 
74  T computeNorm() const { return detail::computeExtentNorm(static_cast<Extent<T, N> const &>(*this)); }
75 
81  bool operator==(Extent<T, N> const &other) const { return all(this->eq(other)); }
82 
88  bool operator!=(Extent<T, N> const &other) const { return any(this->ne(other)); }
89 
103  CoordinateExpr<N> eq(Extent<T, N> const &other) const;
104  CoordinateExpr<N> ne(Extent<T, N> const &other) const;
105  CoordinateExpr<N> lt(Extent<T, N> const &other) const;
106  CoordinateExpr<N> le(Extent<T, N> const &other) const;
107  CoordinateExpr<N> gt(Extent<T, N> const &other) const;
108  CoordinateExpr<N> ge(Extent<T, N> const &other) const;
109  CoordinateExpr<N> eq(T scalar) const { return this->eq(Extent<T, N>(scalar)); }
110  CoordinateExpr<N> ne(T scalar) const { return this->ne(Extent<T, N>(scalar)); }
111  CoordinateExpr<N> lt(T scalar) const { return this->lt(Extent<T, N>(scalar)); }
112  CoordinateExpr<N> le(T scalar) const { return this->le(Extent<T, N>(scalar)); }
113  CoordinateExpr<N> gt(T scalar) const { return this->gt(Extent<T, N>(scalar)); }
114  CoordinateExpr<N> ge(T scalar) const { return this->ge(Extent<T, N>(scalar)); }
116 
123  Point<T, N> operator+(Point<T, N> const &other) const;
124  Extent<T, N> operator+(Extent<T, N> const &other) const {
125  return Extent<T, N>(this->_vector + other._vector);
126  }
127  Extent<T, N> operator-(Extent<T, N> const &other) const {
128  return Extent<T, N>(this->_vector - other._vector);
129  }
131  this->_vector += other._vector;
132  return static_cast<Extent<T, N> &>(*this);
133  }
135  this->_vector -= other._vector;
136  return static_cast<Extent<T, N> &>(*this);
137  }
138  Extent<T, N> operator+() const { return static_cast<Extent<T, N> const &>(*this); }
139  Extent<T, N> operator-() const { return Extent<T, N>(-this->_vector); }
141 
148  Extent<T, N> operator*(T scalar) const { return Extent<T, N>(this->_vector * scalar); }
150  this->_vector *= scalar;
151  return static_cast<Extent<T, N> &>(*this);
152  }
153  Extent<T, N> operator/(T scalar) const { return Extent<T, N>(this->_vector / scalar); }
155  this->_vector /= scalar;
156  return static_cast<Extent<T, N> &>(*this);
157  }
159 
161  Point<T, N> asPoint() const;
162 
164  std::stringstream out;
165  out << "Extent(";
166  for (size_t i = 0; i < N; ++i) {
167  if (i != 0) {
168  out << ",";
169  }
170  out << (*this)[i];
171  }
172  out << ")";
173  return out.str();
174  }
175 
176 protected:
178  explicit ExtentBase(T val = static_cast<T>(0)) : Super(val) {}
179 
181  template <typename Vector>
182  explicit ExtentBase(Eigen::MatrixBase<Vector> const &vector) : Super(vector) {}
183 };
184 
192 template <typename T, int N>
193 class Extent : public ExtentBase<T, N> {
194  typedef ExtentBase<T, N> Super;
195 
196 public:
197  typedef typename Super::EigenVector EigenVector;
198 
200  explicit Extent(T val = static_cast<T>(0)) : Super(val) {}
201 
203  explicit Extent(EigenVector const &vector) : Super(vector) {}
204 
206  explicit Extent(Point<T, N> const &other);
207 
209  template <typename U>
210  explicit Extent(Extent<U, N> const &other);
211  template <typename U>
212  explicit Extent(Point<U, N> const &other);
213 
214  Extent(Extent const &other);
215  Extent(Extent &&other);
216  ~Extent() = default;
217 
218  Extent &operator=(Extent const &other) = default;
219  Extent &operator=(Extent &&other) = default;
220 
222  T computeSquaredNorm() const { return this->asEigen().squaredNorm(); }
223 
225  T computeNorm() const { return this->asEigen().norm(); }
226 
227  void swap(Extent &other) { this->_swap(other); }
228 };
229 
235 template <typename T>
236 class Extent<T, 2> : public ExtentBase<T, 2> {
237  typedef ExtentBase<T, 2> Super;
238 
239 public:
240  typedef typename Super::EigenVector EigenVector;
241 
243  explicit Extent(T val = static_cast<T>(0)) : Super(val) {}
244 
246  explicit Extent(EigenVector const &vector) : Super(vector) {}
247 
249  explicit Extent(Point<T, 2> const &other);
250 
252  template <typename U>
253  explicit Extent(Extent<U, 2> const &other);
254  template <typename U>
255  explicit Extent(Point<U, 2> const &other);
256 
258  explicit Extent(T x, T y) : Super(EigenVector(x, y)) {}
259 
261  explicit Extent(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
262 
264  explicit Extent(std::pair<T, T> const &xy) : Super(EigenVector(xy.first, xy.second)) {}
265 
267  explicit Extent(std::tuple<T, T> const &xy) : Super(EigenVector(std::get<0>(xy), std::get<1>(xy))) {}
268 
269  Extent(Extent const &other);
270  Extent(Extent &&other);
271  ~Extent() = default;
272 
273  Extent &operator=(Extent const &other) = default;
274  Extent &operator=(Extent &&other) = default;
275 
276  void swap(Extent &other) { this->_swap(other); }
277 };
278 
284 template <typename T>
285 class Extent<T, 3> : public ExtentBase<T, 3> {
286  typedef ExtentBase<T, 3> Super;
287 
288 public:
289  typedef typename Super::EigenVector EigenVector;
290 
292  explicit Extent(T val = static_cast<T>(0)) : Super(val) {}
293 
295  explicit Extent(EigenVector const &vector) : Super(vector) {}
296 
298  explicit Extent(Point<T, 3> const &other);
299 
301  template <typename U>
302  explicit Extent(Extent<U, 3> const &other);
303  template <typename U>
304  explicit Extent(Point<U, 3> const &other);
305 
307  explicit Extent(T x, T y, T z) : Super(EigenVector(x, y, z)) {}
308 
310  explicit Extent(T const xyz[3]) : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
311 
313  explicit Extent(std::tuple<T, T, T> const &xyz)
314  : Super(EigenVector(std::get<0>(xyz), std::get<1>(xyz), std::get<2>(xyz))) {}
315 
316  Extent(Extent const &other);
317  Extent(Extent &&other);
318  ~Extent() = default;
319 
320  Extent &operator=(Extent const &other) = default;
321  Extent &operator=(Extent &&other) = default;
322 
323  void swap(Extent &other) { this->_swap(other); }
324 };
325 
326 // Constructor for any 2D type from 2I type
327 template <typename T>
328 template <typename U>
331  "can only construct from Extent of different but integral type");
332  this->setX(static_cast<T>(other.getX()));
333  this->setY(static_cast<T>(other.getY()));
334 };
335 
336 // Should be consistent with converting constructor
337 template <typename T>
338 Extent<T, 2>::Extent(Extent<T, 2> const &) = default;
339 template <typename T>
340 Extent<T, 2>::Extent(Extent<T, 2> &&) = default;
341 
342 template <typename T>
343 template <typename U>
346  "can only construct from Extent of different but integral type");
347  this->setX(static_cast<T>(other.getX()));
348  this->setY(static_cast<T>(other.getY()));
349 };
350 
351 // Constructor for any 3D type from 3I type
352 template <typename T>
353 template <typename U>
356  "can only construct from Extent of different but integral type");
357  this->setX(static_cast<T>(other.getX()));
358  this->setY(static_cast<T>(other.getY()));
359  this->setZ(static_cast<T>(other.getZ()));
360 };
361 
362 // Should be consistent with converting constructor
363 template <typename T>
364 Extent<T, 3>::Extent(Extent<T, 3> const &) = default;
365 template <typename T>
366 Extent<T, 3>::Extent(Extent<T, 3> &&) = default;
367 
368 // Constructor for any 3D type from 3I type
369 template <typename T>
370 template <typename U>
373  "can only construct from Extent of different but integral type");
374  this->setX(static_cast<T>(other.getX()));
375  this->setY(static_cast<T>(other.getY()));
376  this->setZ(static_cast<T>(other.getZ()));
377 };
378 
379 typedef Extent<int, 2> ExtentI;
385 
391 template <int N>
393 
399 template <int N>
401 
407 template <int N>
409 
410 // Some operators below need to take ExtentBase arguments rather than Extent to
411 // avoid ambiguous overloads (since some competing operators are defined as member
412 // functions on ExtentBase).
413 
414 template <typename T, int N>
415 Extent<T, N> operator*(T scalar, ExtentBase<T, N> const &rhs) {
416  return rhs * scalar;
417 }
418 
419 template <int N>
421  return Extent<double, N>(static_cast<Extent<int, N> const &>(lhs)) * rhs;
422 }
423 
424 template <int N>
425 void operator*=(ExtentBase<int, N> &lhs, double rhs) {
426  // use "N < 0" so assertion is dependent on template instantiation, instead of triggering all the time
427  static_assert(N < 0, "In-place multiplication of Extent<int,N> by double would truncate.");
428 }
429 
430 template <int N>
432  return Extent<double, N>(static_cast<Extent<int, N> const &>(lhs)) / rhs;
433 }
434 
435 template <int N>
436 void operator/=(ExtentBase<int, N> &lhs, double rhs) {
437  // use "N < 0" so assertion is dependent on template instantiation, instead of triggering all the time
438  static_assert(N < 0, "In-place division of Extent<int,N> by double would truncate.");
439 }
440 
441 template <int N>
443  return lhs * Extent<double, N>(static_cast<Extent<int, N> const &>(rhs));
444 }
445 
446 template <int N>
448  return lhs + Extent<double, N>(rhs);
449 }
450 
451 template <int N>
453  return lhs += Extent<double, N>(rhs);
454 }
455 
456 template <int N>
458  return lhs - Extent<double, N>(rhs);
459 }
460 
461 template <int N>
463  return lhs -= Extent<double, N>(rhs);
464 }
465 
466 template <int N>
468  return Extent<double, N>(lhs) + rhs;
469 }
470 
471 template <int N>
473  return Extent<double, N>(lhs) - rhs;
474 }
475 }
476 }
477 }
478 
479 #endif
Extent< T, N > & operator+=(Extent< T, N > const &other)
Definition: Extent.h:130
A boolean coordinate.
Extent(std::tuple< T, T > const &xy)
Construct from std::tuple.
Definition: Extent.h:267
CoordinateExpr< N > ge(T scalar) const
Definition: Extent.h:114
Extent(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Extent.h:264
A coordinate class intended to represent absolute positions (2-d specialization). ...
Definition: Point.h:198
Extent(T x, T y)
Construct from two scalars.
Definition: Extent.h:258
void swap(Extent &other)
Definition: Extent.h:323
Extent< T, N > operator*(T scalar) const
Definition: Extent.h:148
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:292
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
CoordinateExpr< N > ne(T scalar) const
Definition: Extent.h:110
Extent(T const xy[2])
Construct from a two-element array.
Definition: Extent.h:261
CoordinateExpr< N > lt(T scalar) const
Definition: Extent.h:111
Extent< double, 2 > Extent2D
Definition: Extent.h:383
CoordinateExpr< N > eq(T scalar) const
Definition: Extent.h:109
Super::EigenVector EigenVector
Definition: Extent.h:240
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:295
Extent(std::tuple< T, T, T > const &xyz)
Construct from std::tuple.
Definition: Extent.h:313
void operator/=(ExtentBase< int, N > &lhs, double rhs)
Definition: Extent.h:436
bool operator==(Extent< T, N > const &other) const
Standard equality comparison.
Definition: Extent.h:81
A coordinate class intended to represent offsets and dimensions.
Extent(T const xyz[3])
Construct from a two-element array.
Definition: Extent.h:310
Super::EigenVector EigenVector
Definition: Extent.h:289
STL namespace.
Extent< T, N > operator+() const
Definition: Extent.h:138
T computeNorm() const
Return the L2 norm of the Extent (sqrt(x^2 + y^2 + ...)).
Definition: Extent.h:225
T computeSquaredNorm() const
Return the squared L2 norm of the Extent (x^2 + y^2 + ...).
Definition: Extent.h:222
void swap(Extent &other)
Definition: Extent.h:227
Extent< int, 2 > Extent2I
Definition: Extent.h:380
std::string toString() const
Definition: Extent.h:163
ItemVariant const * other
Definition: Schema.cc:55
constexpr Angle operator+(Angle a, Angle d) noexcept
Sum of two angles.
Definition: Angle.h:277
double z
Definition: Match.cc:44
CoordinateExpr< N > le(T scalar) const
Definition: Extent.h:112
Extent< double, 2 > ExtentD
Definition: Extent.h:382
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:71
STL class.
Extent< T, N > operator/(T scalar) const
Definition: Extent.h:153
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:318
void operator*=(ExtentBase< int, N > &lhs, double rhs)
Definition: Extent.h:425
A base class for image defects.
Eigen::Vector3d asEigen(sphgeom::Vector3d const &vector)
Definition: sphgeomUtils.h:38
T computeNorm() const
Return the L2 norm of the Extent (sqrt(x^2 + y^2 + ...)).
Definition: Extent.h:74
Extent< T, N > & operator*=(T scalar)
Definition: Extent.h:149
T str(T... args)
Extent< T, N > & operator/=(T scalar)
Definition: Extent.h:154
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:285
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:246
Extent< int, N > truncate(Extent< double, N > const &input)
Return the component-wise truncation (round towards zero).
Definition: Extent.cc:97
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:236
Eigen::Matrix< int, N, 1, Eigen::DontAlign > EigenVector
Extent< int, 2 > ExtentI
Definition: Extent.h:377
Extent< int, N > ceil(Extent< double, N > const &input)
Return the component-wise ceil (round towards more positive).
Definition: Extent.cc:115
ExtentBase(Eigen::MatrixBase< Vector > const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:182
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:452
Extent< double, 3 > Extent3D
Definition: Extent.h:384
#define LSST_EXCEPT(type,...)
CoordinateExpr< N > gt(T scalar) const
Definition: Extent.h:113
Extent< T, N > operator-(Extent< T, N > const &other) const
Definition: Extent.h:127
Super::EigenVector EigenVector
Definition: Extent.h:197
constexpr Angle operator*(Angle a, Angle d) noexcept
Product of two angles.
Definition: Angle.h:291
constexpr Angle operator-(Angle a, Angle d) noexcept
Difference of two angles.
Definition: Angle.h:283
bool operator!=(Extent< T, N > const &other) const
Standard inequality comparison.
Definition: Extent.h:88
Extent(T x, T y, T z)
Construct from three scalars.
Definition: Extent.h:307
Extent< int, 3 > Extent3I
Definition: Extent.h:381
Extent< int, N > floor(Extent< double, N > const &input)
Return the component-wise floor (round towards more negative).
Definition: Extent.cc:106
Extent< T, N > operator-() const
Definition: Extent.h:139
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:462
ExtentBase(T val=static_cast< T >(0))
Construct an Extent<T,N> with all elements set to the same scalar value.
Definition: Extent.h:178
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:200
Extent< T, N > & operator-=(Extent< T, N > const &other)
Definition: Extent.h:134
void swap(Extent &other)
Definition: Extent.h:276
int y
Definition: SpanSet.cc:44
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:203
Extent< T, N > operator+(Extent< T, N > const &other) const
Definition: Extent.h:124
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:243
A coordinate class intended to represent absolute positions (3-d specialization). ...
Definition: Point.h:251