LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
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 
32 #include <tuple>
33 
36 #include "lsst/afw/geom/Extent.h"
37 
38 namespace lsst { namespace afw { namespace geom {
39 
40 template <typename T, int N>
41 class PointBase : public CoordinateBase<Point<T,N>,T,N> {
43 public:
44 
50  bool operator==(Point<T,N> const & other) const { return all(this->eq(other)); }
51 
57  bool operator!=(Point<T,N> const & other) const { return any(this->ne(other)); }
58 
72  CoordinateExpr<N> eq(Point<T,N> const & other) const;
73  CoordinateExpr<N> ne(Point<T,N> const & other) const;
74  CoordinateExpr<N> lt(Point<T,N> const & other) const;
75  CoordinateExpr<N> le(Point<T,N> const & other) const;
76  CoordinateExpr<N> gt(Point<T,N> const & other) const;
77  CoordinateExpr<N> ge(Point<T,N> const & other) const;
78  CoordinateExpr<N> eq(T scalar) const { return this->eq(Point<T,N>(scalar)); }
79  CoordinateExpr<N> ne(T scalar) const { return this->ne(Point<T,N>(scalar)); }
80  CoordinateExpr<N> lt(T scalar) const { return this->lt(Point<T,N>(scalar)); }
81  CoordinateExpr<N> le(T scalar) const { return this->le(Point<T,N>(scalar)); }
82  CoordinateExpr<N> gt(T scalar) const { return this->gt(Point<T,N>(scalar)); }
83  CoordinateExpr<N> ge(T scalar) const { return this->ge(Point<T,N>(scalar)); }
85 
92  Extent<T,N> operator-(Point<T,N> const & other) const {
93  return Extent<T,N>(this->_vector - other._vector);
94  }
95  Point<T,N> operator+(Extent<T,N> const & other) const {
96  return Point<T,N>(this->_vector + other.asEigen());
97  }
98  Point<T,N> operator-(Extent<T,N> const & other) const {
99  return Point<T,N>(this->_vector - other.asEigen());
100  }
101  Point<T,N> & operator+=(Extent<T,N> const & other) {
102  this->_vector += other.asEigen();
103  return static_cast<Point<T,N> &>(*this);
104  }
105  Point<T,N> & operator-=(Extent<T,N> const & other) {
106  this->_vector -= other.asEigen();
107  return static_cast<Point<T,N> &>(*this);
108  }
110 
112  Extent<T,N> asExtent() const { return Extent<T,N>(static_cast<Point<T,N> const &>(*this)); }
113 
115  void shift(Extent<T,N> const & offset) { this->_vector += offset.asEigen(); }
116 
117 
118  void scale(double factor) { this->_vector *= factor; }
119 
120  double distanceSquared(PointBase<T,N> const & other) const {
121  // the cast to double is lame but Eigen seems to require they be the same type
122  return (this->asEigen() - other.asEigen()).squaredNorm();
123  }
124 
125  std::string toString() const {
126  std::stringstream out;
127  out << "Point(";
128  for (size_t i = 0; i < N; ++i) {
129  if (i != 0) {
130  out << ",";
131  }
132  out << (*this)[i];
133  }
134  out << ")";
135  return out.str();
136  }
137 
138 protected:
139 
140  explicit PointBase(T val = static_cast<T>(0)) : Super(val) {}
141 
142  template <typename Vector>
143  explicit PointBase(Eigen::MatrixBase<Vector> const & vector) : Super(vector) {}
144 };
145 
151 template<typename T, int N>
152 class Point : public PointBase<T,N> {
154 public:
155  typedef typename Super::EigenVector EigenVector;
156 
158  explicit Point(T val=static_cast<T>(0)) : Super(val) {}
159 
167  template <typename U>
168  explicit Point(Point<U,N> const & other);
169 
171  explicit Point(EigenVector const & vector) : Super(vector) {}
172 
174  explicit Point(Extent<T,N> const & other) : Super(other.asEigen()) {}
175 
176  void swap(Point & other) { this->_swap(other); }
177 };
178 
184 template<typename T>
185 class Point<T,2> : public PointBase<T,2> {
187 public:
188  typedef typename Super::EigenVector EigenVector;
189 
191  explicit Point(T val=static_cast<T>(0)) : Super(val) {}
192 
200  template <typename U>
201  explicit Point(Point<U,2> const & other);
202 
204  explicit Point(EigenVector const & vector) : Super(vector) {}
205 
207  explicit Point(Extent<T,2> const & other) : Super(other.asEigen()) {}
208 
210  explicit Point(T x, T y) : Super(EigenVector(x,y)) {}
211 
213  explicit Point(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
214 
216  explicit Point(std::pair<T,T> const & xy) : Super(EigenVector(xy.first, xy.second)) {}
217 
219  explicit Point(std::tuple<T,T> const & xy) :
220  Super(EigenVector(std::get<0>(xy), std::get<1>(xy))) {}
221 
222 #ifdef SWIG
223  T getX() const;
224  T getY() const;
225  void setX(T x);
226  void setY(T y);
227 #endif
228 
229  void swap(Point & other) { this->_swap(other); }
230 };
231 
237 template<typename T>
238 class Point<T,3> : public PointBase<T,3> {
240 public:
241  typedef typename Super::EigenVector EigenVector;
242 
244  explicit Point(T val=static_cast<T>(0)) : Super(val) {}
245 
253  template <typename U>
254  explicit Point(Point<U,3> const & other);
255 
257  explicit Point(EigenVector const & vector) : Super(vector) {}
258 
260  explicit Point(Extent<T,3> const & other) : Super(other.asEigen()) {}
261 
263  explicit Point(T x, T y, T z) : Super(EigenVector(x,y,z)) {}
264 
266  explicit Point(T const xyz[3]) : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
267 
269  explicit Point(std::tuple<T,T,T> const & xyz) :
270  Super(EigenVector(std::get<0>(xyz), std::get<1>(xyz), std::get<2>(xyz))) {}
271 
272 #ifdef SWIG
273  T getX() const;
274  T getY() const;
275  T getZ() const;
276  void setX(T x);
277  void setY(T y);
278  void setZ(T z);
279 #endif
280 
281  void swap(Point & other) { this->_swap(other); }
282 };
283 
290 
291 template <int N>
293  return lhs + Extent<double,N>(rhs);
294 }
295 
296 template <int N>
298  return Point<double,N>(lhs) + rhs;
299 }
300 
301 template <int N>
303  return lhs += Extent<double,N>(rhs);
304 }
305 
306 template <int N>
308  return Point<double,N>(lhs) + rhs;
309 }
310 
311 template <int N>
313  return lhs - Extent<double,N>(rhs);
314 }
315 
316 template <int N>
318  return lhs -= Extent<double,N>(rhs);
319 }
320 
321 template <int N>
323  return Point<double,N>(lhs) - rhs;
324 }
325 
326 template <int N>
328  return lhs - Point<double,N>(rhs);
329 }
330 
331 template <int N>
333  return Point<double,N>(lhs) - rhs;
334 }
335 
336 }}}
337 
338 #endif
int y
Point(Extent< T, 2 > const &other)
Explicit constructor from Extent.
Definition: Point.h:207
CoordinateExpr< N > lt(T scalar) const
Definition: Point.h:80
A boolean coordinate.
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:191
CoordinateExpr< N > ge(Point< T, N > const &other) const
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
Extent< T, N > asExtent() const
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:112
Extent< T, N > operator-(Point< T, N > const &other) const
Definition: Point.h:92
Point(T x, T y)
Explicit constructor from a pair of doubles.
Definition: Point.h:210
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:244
A coordinate class intended to represent offsets and dimensions.
bool operator!=(Point< T, N > const &other) const
Standard inequality comparison.
Definition: Point.h:57
CoordinateExpr< N > lt(Point< T, N > const &other) const
PointBase< T, 2 > Super
Definition: Point.h:186
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
CoordinateExpr< N > eq(Point< T, N > const &other) const
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:432
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:442
Point< int, 2 > Point2I
Definition: Point.h:285
void swap(Point &other)
Definition: Point.h:281
CoordinateExpr< N > le(Point< T, N > const &other) const
bool operator==(Point< T, N > const &other) const
Standard equality comparison.
Definition: Point.h:50
Point< T, N > operator-(Extent< T, N > const &other) const
Definition: Point.h:98
A CRTP base class for coordinate objects.
A coordinate class intended to represent absolute positions.
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:158
const Angle operator+(Angle const a, Angle const d)
Definition: Angle.h:263
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:171
Point< T, N > & operator-=(Extent< T, N > const &other)
Definition: Point.h:105
Point(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Point.h:216
CoordinateExpr< N > ne(Point< T, N > const &other) const
CoordinateExpr< N > le(T scalar) const
Definition: Point.h:81
void swap(Point &other)
Definition: Point.h:229
PointBase< T, N > Super
Definition: Point.h:153
CoordinateExpr< N > gt(T scalar) const
Definition: Point.h:82
PointBase< T, 3 > Super
Definition: Point.h:239
Point(T const xyz[3])
Construct from a two-element array.
Definition: Point.h:266
PointBase(Eigen::MatrixBase< Vector > const &vector)
Definition: Point.h:143
CoordinateExpr< N > ge(T scalar) const
Definition: Point.h:83
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:270
Point(T x, T y, T z)
Explicit constructor from a sequence of doubles.
Definition: Point.h:263
double x
Point< int, 2 > PointI
Definition: Point.h:284
CoordinateExpr< N > ne(T scalar) const
Definition: Point.h:79
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:221
Point(T const xy[2])
Construct from a two-element array.
Definition: Point.h:213
Point< double, 3 > Point3D
Definition: Point.h:289
Point(Extent< T, N > const &other)
Explicit constructor from Extent.
Definition: Point.h:174
void shift(Extent< T, N > const &offset)
Shift the point by the given offset.
Definition: Point.h:115
Super::EigenVector EigenVector
Definition: Point.h:188
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
Point(Extent< T, 3 > const &other)
Explicit constructor from Extent.
Definition: Point.h:260
CoordinateExpr< N > eq(T scalar) const
Definition: Point.h:78
Point< int, 3 > Point3I
Definition: Point.h:286
Point< T, N > & operator+=(Extent< T, N > const &other)
Definition: Point.h:101
CoordinateExpr< N > gt(Point< T, N > const &other) const
Super::EigenVector EigenVector
Definition: Point.h:241
CoordinateBase< Point< T, N >, T, N > Super
Definition: Point.h:42
void scale(double factor)
Definition: Point.h:118
Point< double, 2 > Point2D
Definition: Point.h:288
Point< double, 2 > PointD
Definition: Point.h:287
Super::EigenVector EigenVector
Definition: Point.h:155
Point(std::tuple< T, T, T > const &xyz)
Construct from std::tuple.
Definition: Point.h:269
Point< T, N > operator+(Extent< T, N > const &other) const
Definition: Point.h:95
A CRTP base class for coordinate objects, providing partial specializations for 2D and 3D...
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:204
double distanceSquared(PointBase< T, N > const &other) const
Definition: Point.h:120
PointBase(T val=static_cast< T >(0))
Definition: Point.h:140
A coordinate class intended to represent offsets and dimensions.
void swap(Point &other)
Definition: Point.h:176
ImageT val
Definition: CR.cc:159
Point(std::tuple< T, T > const &xy)
Construct from std::tuple.
Definition: Point.h:219
const Angle operator-(Angle const a, Angle const d)
Definition: Angle.h:264
dictionary Point
Definition: __init__.py:39
std::string toString() const
Definition: Point.h:125
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:257