LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
Extent.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
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 
29 #ifndef LSST_AFW_GEOM_EXTENT_H
30 #define LSST_AFW_GEOM_EXTENT_H
31 
32 #include <boost/type_traits/is_integral.hpp>
33 #include <boost/static_assert.hpp>
34 #include <boost/type_traits/is_same.hpp>
35 
36 #include "lsst/pex/exceptions.h"
38 
39 namespace lsst { namespace afw { 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(
52  pex::exceptions::LogicError,
53  "Cannot compute norm of integer extent"
54  );
55 #if 1 // make compilers happy in non-void function
56  return -1;
57 #endif
58 }
59 
60 } // namespace detail
61 
62 
63 template<typename T, int N>
64 class ExtentBase : public CoordinateBase<Extent<T,N>,T,N> {
66 public:
67 
69  T computeSquaredNorm() const { return this->asEigen().squaredNorm(); }
70 
72  T computeNorm() const { return detail::computeExtentNorm(static_cast<Extent<T,N> const &>(*this)); }
73 
79  bool operator==(Extent<T,N> const & other) const { return all(this->eq(other)); }
80 
86  bool operator!=(Extent<T,N> const & other) const { return any(this->ne(other)); }
87 
101  CoordinateExpr<N> eq(Extent<T,N> const & other) const;
102  CoordinateExpr<N> ne(Extent<T,N> const & other) const;
103  CoordinateExpr<N> lt(Extent<T,N> const & other) const;
104  CoordinateExpr<N> le(Extent<T,N> const & other) const;
105  CoordinateExpr<N> gt(Extent<T,N> const & other) const;
106  CoordinateExpr<N> ge(Extent<T,N> const & other) const;
107  CoordinateExpr<N> eq(T scalar) const { return this->eq(Extent<T,N>(scalar)); }
108  CoordinateExpr<N> ne(T scalar) const { return this->ne(Extent<T,N>(scalar)); }
109  CoordinateExpr<N> lt(T scalar) const { return this->lt(Extent<T,N>(scalar)); }
110  CoordinateExpr<N> le(T scalar) const { return this->le(Extent<T,N>(scalar)); }
111  CoordinateExpr<N> gt(T scalar) const { return this->gt(Extent<T,N>(scalar)); }
112  CoordinateExpr<N> ge(T scalar) const { return this->ge(Extent<T,N>(scalar)); }
114 
121  Point<T,N> operator+(Point<T,N> const & other) const;
122  Extent<T,N> operator+(Extent<T,N> const & other) const {
123  return Extent<T,N>(this->_vector + other._vector);
124  }
125  Extent<T,N> operator-(Extent<T,N> const & other) const {
126  return Extent<T,N>(this->_vector - other._vector);
127  }
128  Extent<T,N> & operator+=(Extent<T,N> const & other) {
129  this->_vector += other._vector;
130  return static_cast<Extent<T,N>&>(*this);
131  }
132  Extent<T,N> & operator-=(Extent<T,N> const & other) {
133  this->_vector -= other._vector;
134  return static_cast<Extent<T,N>&>(*this);
135  }
136  Extent<T,N> operator+() const { return static_cast<Extent<T,N> const &>(*this); }
137  Extent<T,N> operator-() const { return Extent<T,N>(-this->_vector); }
139 
146  Extent<T,N> operator*(T scalar) const { return Extent<T,N>(this->_vector * scalar); }
147  Extent<T,N> & operator*=(T scalar) { this->_vector *= scalar; return static_cast<Extent<T,N>&>(*this); }
148  Extent<T,N> operator/(T scalar) const { return Extent<T,N>(this->_vector / scalar); }
149  Extent<T,N> & operator/=(T scalar) { this->_vector /= scalar; return static_cast<Extent<T,N>&>(*this); }
151 
152  std::string toString() const {
153  std::stringstream out;
154  out << "Extent(";
155  for (size_t i = 0; i < N; ++i) {
156  if (i != 0) {
157  out << ",";
158  }
159  out << (*this)[i];
160  }
161  out << ")";
162  return out.str();
163  }
164 
165 protected:
166 
168  explicit ExtentBase(T val=static_cast<T>(0)) : Super(val) {}
169 
171  template <typename Vector>
172  explicit ExtentBase(Eigen::MatrixBase<Vector> const & vector) : Super(vector) {}
173 
174 };
175 
185 template<typename T, int N>
186 class Extent : public ExtentBase<T,N> {
188 public:
189  typedef typename Super::EigenVector EigenVector;
190 
192  explicit Extent(T val=static_cast<T>(0)) : Super(val) {}
193 
195  explicit Extent(EigenVector const & vector) : Super(vector) {}
196 
198  explicit Extent(Point<T,N> const & other);
199 
201  template<typename U>
202  explicit Extent(Extent<U,N> const & other);
203  template<typename U>
204  explicit Extent(Point<U,N> const & other);
205 
207  T computeSquaredNorm() const { return this->asEigen().squaredNorm(); }
208 
210  T computeNorm() const { return this->asEigen().norm(); }
211 
212  void swap(Extent & other) { this->_swap(other); }
213 };
214 
218 template<typename T>
219 class Extent<T,2> : public ExtentBase<T,2> {
221 public:
222  typedef typename Super::EigenVector EigenVector;
223 
225  explicit Extent(T val=static_cast<T>(0)) : Super(val) {}
226 
228  explicit Extent(EigenVector const & vector) : Super(vector) {}
229 
231  explicit Extent(Point<T,2> const & other);
232 
234  template<typename U>
235  explicit Extent(Extent<U,2> const & other);
236  template<typename U>
237  explicit Extent(Point<U,2> const & other);
238 
240  explicit Extent(T x, T y) : Super(EigenVector(x, y)) {}
241 
243  explicit Extent(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
244 
246  explicit Extent(std::pair<T,T> const & xy) : Super(EigenVector(xy.first, xy.second)) {}
247 
249  explicit Extent(boost::tuple<T,T> const & xy) :
250  Super(EigenVector(xy.template get<0>(), xy.template get<1>())) {}
251 
252 #ifdef SWIG
253  T getX() const;
254  T getY() const;
255  void setX(T x);
256  void setY(T y);
257 #endif
258 
259  void swap(Extent & other) { this->_swap(other); }
260 };
261 
265 template<typename T>
266 class Extent<T,3> : public ExtentBase<T,3> {
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(boost::tuple<T,T,T> const & xyz) :
294  Super(EigenVector(xyz.template get<0>(), xyz.template get<1>(), xyz.template get<2>())) {}
295 
296 #ifdef SWIG
297  T getX() const;
298  T getY() const;
299  T getZ() const;
300  void setX(T x);
301  void setY(T y);
302  void setZ(T z);
303 #endif
304 
305  void swap(Extent & other) { this->_swap(other); }
306 };
307 
308 // Constructor for any 2D type from 2I type
309 template<typename T>
310 template<typename U>
312 {
313  BOOST_STATIC_ASSERT( (!boost::is_same<T,U>::value && boost::is_integral<U>::value) );
314  this->setX(static_cast<T>(other.getX()));
315  this->setY(static_cast<T>(other.getY()));
316 };
317 
318 template<typename T>
319 template<typename U>
321 {
322  BOOST_STATIC_ASSERT( (!boost::is_same<T,U>::value && boost::is_integral<U>::value) );
323  this->setX(static_cast<T>(other.getX()));
324  this->setY(static_cast<T>(other.getY()));
325 };
326 
327 // Constructor for any 3D type from 3I type
328 template<typename T>
329 template<typename U>
331 {
332  BOOST_STATIC_ASSERT( (!boost::is_same<T,U>::value && boost::is_integral<U>::value) );
333  this->setX(static_cast<T>(other.getX()));
334  this->setY(static_cast<T>(other.getY()));
335  this->setZ(static_cast<T>(other.getZ()));
336 };
337 
338 // Constructor for any 3D type from 3I type
339 template<typename T>
340 template<typename U>
342 {
343  BOOST_STATIC_ASSERT( (!boost::is_same<T,U>::value && boost::is_integral<U>::value) );
344  this->setX(static_cast<T>(other.getX()));
345  this->setY(static_cast<T>(other.getY()));
346  this->setZ(static_cast<T>(other.getZ()));
347 };
348 
349 typedef Extent<int,2> ExtentI;
355 
356 }}}
357 
358 #endif
Extent< T, N > operator/(T scalar) const
Definition: Extent.h:148
Extent< double, 3 > Extent3D
Definition: Extent.h:354
A boolean coordinate.
Extent(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Extent.h:246
A coordinate class intended to represent absolute positions (2-d specialization). ...
Definition: Point.h:176
Extent(T x, T y)
Construct from two scalars.
Definition: Extent.h:240
Extent(boost::tuple< T, T, T > const &xyz)
Construct from boost::tuple.
Definition: Extent.h:293
void swap(Extent &other)
Definition: Extent.h:305
dictionary Extent
Definition: __init__.py:39
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:272
ExtentBase< T, N > Super
Definition: Extent.h:187
Extent(boost::tuple< T, T > const &xy)
Construct from boost::tuple.
Definition: Extent.h:249
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
CoordinateExpr< N > gt(T scalar) const
Definition: Extent.h:111
Extent(T const xy[2])
Construct from a two-element array.
Definition: Extent.h:243
Super::EigenVector EigenVector
Definition: Extent.h:222
ExtentBase< T, 3 > Super
Definition: Extent.h:267
int y
CoordinateBase< Extent< T, N >, T, N > Super
Definition: Extent.h:65
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:275
A coordinate class intended to represent offsets and dimensions.
Extent< T, N > & operator*=(T scalar)
Definition: Extent.h:147
Extent(T const xyz[3])
Construct from a two-element array.
Definition: Extent.h:290
Extent< T, N > & operator/=(T scalar)
Definition: Extent.h:149
Super::EigenVector EigenVector
Definition: Extent.h:269
Extent< T, N > operator*(T scalar) const
Definition: Extent.h:146
void swap(Extent &other)
Definition: Extent.h:212
A boolean pair class used to express the output of spatial predicates on Point and Extent...
CoordinateExpr< N > gt(Extent< T, N > const &other) const
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
Extent< int, 2 > Extent2I
Definition: Extent.h:350
bool operator!=(Extent< T, N > const &other) const
Standard inequality comparison.
Definition: Extent.h:86
Extent< T, N > operator-(Extent< T, N > const &other) const
Definition: Extent.h:125
A CRTP base class for coordinate objects.
A coordinate class intended to represent absolute positions.
Extent< double, 2 > ExtentD
Definition: Extent.h:352
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
Extent< T, N > & operator+=(Extent< T, N > const &other)
Definition: Extent.h:128
ExtentBase< T, 2 > Super
Definition: Extent.h:220
Include files required for standard LSST Exception handling.
T computeSquaredNorm() const
Return the squared L2 norm of the Extent (x^2 + y^2 + ...).
Definition: Extent.h:69
int x
Extent< T, N > operator-() const
Definition: Extent.h:137
Extent< T, N > operator+(Extent< T, N > const &other) const
Definition: Extent.h:122
CoordinateExpr< N > lt(Extent< T, N > const &other) const
T computeSquaredNorm() const
Return the squared L2 norm of the Extent (x^2 + y^2 + ...).
Definition: Extent.h:207
double computeExtentNorm(Extent< double, N > const &s)
Definition: Extent.h:45
T computeNorm() const
Return the L2 norm of the Extent (sqrt(x^2 + y^2 + ...)).
Definition: Extent.h:72
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:228
std::string toString() const
Definition: Extent.h:152
CoordinateExpr< N > ge(T scalar) const
Definition: Extent.h:112
Extent< int, 3 > Extent3I
Definition: Extent.h:351
Extent< int, 2 > ExtentI
Definition: Extent.h:347
Extent< T, N > operator+() const
Definition: Extent.h:136
bool operator==(Extent< T, N > const &other) const
Standard equality comparison.
Definition: Extent.h:79
ExtentBase(Eigen::MatrixBase< Vector > const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:172
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
CoordinateExpr< N > eq(T scalar) const
Definition: Extent.h:107
Super::EigenVector EigenVector
Definition: Extent.h:189
CoordinateExpr< N > le(Extent< T, N > const &other) const
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
CoordinateExpr< N > ne(T scalar) const
Definition: Extent.h:108
Extent(T x, T y, T z)
Construct from three scalars.
Definition: Extent.h:287
CoordinateExpr< N > ge(Extent< T, N > const &other) const
CoordinateExpr< N > lt(T scalar) const
Definition: Extent.h:109
CoordinateExpr< N > le(T scalar) const
Definition: Extent.h:110
Extent< double, 2 > Extent2D
Definition: Extent.h:353
CoordinateExpr< N > eq(Extent< T, N > const &other) const
T computeNorm() const
Return the L2 norm of the Extent (sqrt(x^2 + y^2 + ...)).
Definition: Extent.h:210
ExtentBase(T val=static_cast< T >(0))
Construct an Extent&lt;T,N&gt; with all elements set to the same scalar value.
Definition: Extent.h:168
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:192
void swap(Extent &other)
Definition: Extent.h:259
ImageT val
Definition: CR.cc:154
Extent(EigenVector const &vector)
Construct an Extent from an Eigen vector.
Definition: Extent.h:195
Extent< T, N > & operator-=(Extent< T, N > const &other)
Definition: Extent.h:132
CoordinateExpr< N > ne(Extent< T, N > const &other) const
Extent(T val=static_cast< T >(0))
Construct an Extent with all elements set to the same scalar value.
Definition: Extent.h:225
A coordinate class intended to represent absolute positions (3-d specialization). ...
Definition: Point.h:227