LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Point.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_POINT_H
30 #define LSST_AFW_GEOM_POINT_H
31 
34 #include "lsst/afw/geom/Extent.h"
35 
36 namespace lsst { namespace afw { namespace geom {
37 
38 template <typename T, int N>
39 class PointBase : public CoordinateBase<Point<T,N>,T,N> {
41 public:
42 
48  bool operator==(Point<T,N> const & other) const { return all(this->eq(other)); }
49 
55  bool operator!=(Point<T,N> const & other) const { return any(this->ne(other)); }
56 
70  CoordinateExpr<N> eq(Point<T,N> const & other) const;
71  CoordinateExpr<N> ne(Point<T,N> const & other) const;
72  CoordinateExpr<N> lt(Point<T,N> const & other) const;
73  CoordinateExpr<N> le(Point<T,N> const & other) const;
74  CoordinateExpr<N> gt(Point<T,N> const & other) const;
75  CoordinateExpr<N> ge(Point<T,N> const & other) const;
76  CoordinateExpr<N> eq(T scalar) const { return this->eq(Point<T,N>(scalar)); }
77  CoordinateExpr<N> ne(T scalar) const { return this->ne(Point<T,N>(scalar)); }
78  CoordinateExpr<N> lt(T scalar) const { return this->lt(Point<T,N>(scalar)); }
79  CoordinateExpr<N> le(T scalar) const { return this->le(Point<T,N>(scalar)); }
80  CoordinateExpr<N> gt(T scalar) const { return this->gt(Point<T,N>(scalar)); }
81  CoordinateExpr<N> ge(T scalar) const { return this->ge(Point<T,N>(scalar)); }
83 
90  Extent<T,N> operator-(Point<T,N> const & other) const {
91  return Extent<T,N>(this->_vector - other._vector);
92  }
93  Point<T,N> operator+(Extent<T,N> const & other) const {
94  return Point<T,N>(this->_vector + other.asEigen());
95  }
96  Point<T,N> operator-(Extent<T,N> const & other) const {
97  return Point<T,N>(this->_vector - other.asEigen());
98  }
99  Point<T,N> & operator+=(Extent<T,N> const & other) {
100  this->_vector += other.asEigen();
101  return static_cast<Point<T,N> &>(*this);
102  }
103  Point<T,N> & operator-=(Extent<T,N> const & other) {
104  this->_vector -= other.asEigen();
105  return static_cast<Point<T,N> &>(*this);
106  }
108 
110  Extent<T,N> asExtent() const { return Extent<T,N>(static_cast<Point<T,N> const &>(*this)); }
111 
113  void shift(Extent<T,N> const & offset) { this->_vector += offset.asEigen(); }
114 
115 
116  void scale(double factor) { this->_vector *= factor; }
117 
118  double distanceSquared(PointBase<T,N> const & other) const {
119  // the cast to double is lame but Eigen seems to require they be the same type
120  return (this->asEigen() - other.asEigen()).squaredNorm();
121  }
122 
123  std::string toString() const {
124  std::stringstream out;
125  out << "Point(";
126  for (size_t i = 0; i < N; ++i) {
127  if (i != 0) {
128  out << ",";
129  }
130  out << (*this)[i];
131  }
132  out << ")";
133  return out.str();
134  }
135 
136 protected:
137 
138  explicit PointBase(T val = static_cast<T>(0)) : Super(val) {}
139 
140  template <typename Vector>
141  explicit PointBase(Eigen::MatrixBase<Vector> const & vector) : Super(vector) {}
142 };
143 
149 template<typename T, int N>
150 class Point : public PointBase<T,N> {
152 public:
153  typedef typename Super::EigenVector EigenVector;
154 
156  explicit Point(T val=static_cast<T>(0)) : Super(val) {}
157 
165  template <typename U>
166  explicit Point(Point<U,N> const & other);
167 
169  explicit Point(EigenVector const & vector) : Super(vector) {}
170 
172  explicit Point(Extent<T,N> const & other) : Super(other.asEigen()) {}
173 
174  void swap(Point & other) { this->_swap(other); }
175 };
176 
182 template<typename T>
183 class Point<T,2> : public PointBase<T,2> {
185 public:
186  typedef typename Super::EigenVector EigenVector;
187 
189  explicit Point(T val=static_cast<T>(0)) : Super(val) {}
190 
198  template <typename U>
199  explicit Point(Point<U,2> const & other);
200 
202  explicit Point(EigenVector const & vector) : Super(vector) {}
203 
205  explicit Point(Extent<T,2> const & other) : Super(other.asEigen()) {}
206 
208  explicit Point(T x, T y) : Super(EigenVector(x,y)) {}
209 
211  explicit Point(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
212 
214  explicit Point(std::pair<T,T> const & xy) : Super(EigenVector(xy.first, xy.second)) {}
215 
217  explicit Point(boost::tuple<T,T> const & xy) :
218  Super(EigenVector(xy.template get<0>(), xy.template get<1>())) {}
219 
220 #ifdef SWIG
221  T getX() const;
222  T getY() const;
223  void setX(T x);
224  void setY(T y);
225 #endif
226 
227  void swap(Point & other) { this->_swap(other); }
228 };
229 
235 template<typename T>
236 class Point<T,3> : public PointBase<T,3> {
238 public:
239  typedef typename Super::EigenVector EigenVector;
240 
242  explicit Point(T val=static_cast<T>(0)) : Super(val) {}
243 
251  template <typename U>
252  explicit Point(Point<U,3> const & other);
253 
255  explicit Point(EigenVector const & vector) : Super(vector) {}
256 
258  explicit Point(Extent<T,3> const & other) : Super(other.asEigen()) {}
259 
261  explicit Point(T x, T y, T z) : Super(EigenVector(x,y,z)) {}
262 
264  explicit Point(T const xyz[3]) : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
265 
267  explicit Point(boost::tuple<T,T,T> const & xyz) :
268  Super(EigenVector(xyz.template get<0>(), xyz.template get<1>(), xyz.template get<2>())) {}
269 
270 #ifdef SWIG
271  T getX() const;
272  T getY() const;
273  T getZ() const;
274  void setX(T x);
275  void setY(T y);
276  void setZ(T z);
277 #endif
278 
279  void swap(Point & other) { this->_swap(other); }
280 };
281 
288 
289 template <int N>
291  return lhs + Extent<double,N>(rhs);
292 }
293 
294 template <int N>
296  return Point<double,N>(lhs) + rhs;
297 }
298 
299 template <int N>
301  return lhs += Extent<double,N>(rhs);
302 }
303 
304 template <int N>
306  return Point<double,N>(lhs) + rhs;
307 }
308 
309 template <int N>
311  return lhs - Extent<double,N>(rhs);
312 }
313 
314 template <int N>
316  return lhs -= Extent<double,N>(rhs);
317 }
318 
319 template <int N>
321  return Point<double,N>(lhs) - rhs;
322 }
323 
324 template <int N>
326  return lhs - Point<double,N>(rhs);
327 }
328 
329 template <int N>
331  return Point<double,N>(lhs) - rhs;
332 }
333 
334 }}}
335 
336 #endif
int y
CoordinateExpr< N > gt(Point< T, N > const &other) const
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:189
A boolean coordinate.
double distanceSquared(PointBase< T, N > const &other) const
Definition: Point.h:118
Point(T x, T y, T z)
Explicit constructor from a sequence of doubles.
Definition: Point.h:261
Super::EigenVector EigenVector
Definition: Point.h:239
PointBase(T val=static_cast< T >(0))
Definition: Point.h:138
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
void scale(double factor)
Definition: Point.h:116
PointBase< T, 3 > Super
Definition: Point.h:237
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:255
bool operator==(Point< T, N > const &other) const
Standard equality comparison.
Definition: Point.h:48
CoordinateExpr< N > ge(T scalar) const
Definition: Point.h:81
void swap(Point &other)
Definition: Point.h:174
A coordinate class intended to represent offsets and dimensions.
CoordinateExpr< N > lt(Point< T, N > const &other) const
Super::EigenVector EigenVector
Definition: Point.h:153
Point(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Point.h:214
Point< double, 2 > Point2D
Definition: Point.h:286
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:429
A boolean pair class used to express the output of spatial predicates on Point and Extent...
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:439
Point< int, 2 > Point2I
Definition: Point.h:283
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
CoordinateBase< Point< T, N >, T, N > Super
Definition: Point.h:40
CoordinateExpr< N > ge(Point< T, N > const &other) const
Point< double, 3 > Point3D
Definition: Point.h:287
PointBase(Eigen::MatrixBase< Vector > const &vector)
Definition: Point.h:141
A CRTP base class for coordinate objects.
A coordinate class intended to represent absolute positions.
Point(Extent< T, 2 > const &other)
Explicit constructor from Extent.
Definition: Point.h:205
bool val
Point(boost::tuple< T, T, T > const &xyz)
Construct from boost::tuple.
Definition: Point.h:267
Point< T, N > operator-(Extent< T, N > const &other) const
Definition: Point.h:96
const Angle operator+(Angle const a, Angle const d)
Definition: Angle.h:264
Point(boost::tuple< T, T > const &xy)
Construct from boost::tuple.
Definition: Point.h:217
Super::EigenVector EigenVector
Definition: Point.h:186
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:156
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
PointBase< T, N > Super
Definition: Point.h:151
Point(Extent< T, N > const &other)
Explicit constructor from Extent.
Definition: Point.h:172
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:169
CoordinateExpr< N > eq(T scalar) const
Definition: Point.h:76
void shift(Extent< T, N > const &offset)
Shift the point by the given offset.
Definition: Point.h:113
bool operator!=(Point< T, N > const &other) const
Standard inequality comparison.
Definition: Point.h:55
std::string toString() const
Definition: Point.h:123
void swap(Point &other)
Definition: Point.h:279
PointBase< T, 2 > Super
Definition: Point.h:184
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:271
double x
CoordinateExpr< N > ne(T scalar) const
Definition: Point.h:77
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:222
Point< int, 2 > PointI
Definition: Point.h:282
void swap(Point &other)
Definition: Point.h:227
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:242
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
Point< int, 3 > Point3I
Definition: Point.h:284
CoordinateExpr< N > le(Point< T, N > const &other) const
CoordinateExpr< N > gt(T scalar) const
Definition: Point.h:80
Point< T, N > & operator-=(Extent< T, N > const &other)
Definition: Point.h:103
Extent< T, N > asExtent() const
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:110
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:202
CoordinateExpr< N > le(T scalar) const
Definition: Point.h:79
Point(T x, T y)
Explicit constructor from a pair of doubles.
Definition: Point.h:208
Point< T, N > & operator+=(Extent< T, N > const &other)
Definition: Point.h:99
A CRTP base class for coordinate objects, providing partial specializations for 2D and 3D...
Point(Extent< T, 3 > const &other)
Explicit constructor from Extent.
Definition: Point.h:258
Point(T const xy[2])
Construct from a two-element array.
Definition: Point.h:211
A coordinate class intended to represent offsets and dimensions.
CoordinateExpr< N > lt(T scalar) const
Definition: Point.h:78
const Angle operator-(Angle const a, Angle const d)
Definition: Angle.h:265
dictionary Point
Definition: __init__.py:39
Point(T const xyz[3])
Construct from a two-element array.
Definition: Point.h:264
Extent< T, N > operator-(Point< T, N > const &other) const
Definition: Point.h:90
CoordinateExpr< N > eq(Point< T, N > const &other) const
Point< T, N > operator+(Extent< T, N > const &other) const
Definition: Point.h:93
CoordinateExpr< N > ne(Point< T, N > const &other) const
Point< double, 2 > PointD
Definition: Point.h:285