lsst.afw  14.0-22-gc76603d1b
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 
25 /*
26  * A coordinate class intended to represent absolute positions.
27  */
28 #ifndef LSST_AFW_GEOM_POINT_H
29 #define LSST_AFW_GEOM_POINT_H
30 
31 #include <tuple>
32 
35 #include "lsst/afw/geom/Extent.h"
36 
37 namespace lsst {
38 namespace afw {
39 namespace geom {
40 
41 template <typename T, int N>
42 class PointBase : public CoordinateBase<Point<T, N>, T, N> {
43  typedef CoordinateBase<Point<T, N>, T, N> Super;
44 
45 public:
51  bool operator==(Point<T, N> const &other) const { return all(this->eq(other)); }
52 
58  bool operator!=(Point<T, N> const &other) const { return any(this->ne(other)); }
59 
73  CoordinateExpr<N> eq(Point<T, N> const &other) const;
74  CoordinateExpr<N> ne(Point<T, N> const &other) const;
75  CoordinateExpr<N> lt(Point<T, N> const &other) const;
76  CoordinateExpr<N> le(Point<T, N> const &other) const;
77  CoordinateExpr<N> gt(Point<T, N> const &other) const;
78  CoordinateExpr<N> ge(Point<T, N> const &other) const;
79  CoordinateExpr<N> eq(T scalar) const { return this->eq(Point<T, N>(scalar)); }
80  CoordinateExpr<N> ne(T scalar) const { return this->ne(Point<T, N>(scalar)); }
81  CoordinateExpr<N> lt(T scalar) const { return this->lt(Point<T, N>(scalar)); }
82  CoordinateExpr<N> le(T scalar) const { return this->le(Point<T, N>(scalar)); }
83  CoordinateExpr<N> gt(T scalar) const { return this->gt(Point<T, N>(scalar)); }
84  CoordinateExpr<N> ge(T scalar) const { return this->ge(Point<T, N>(scalar)); }
86 
93  Extent<T, N> operator-(Point<T, N> const &other) const {
94  return Extent<T, N>(this->_vector - other._vector);
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) const {
100  return Point<T, N>(this->_vector - other.asEigen());
101  }
103  this->_vector += other.asEigen();
104  return static_cast<Point<T, N> &>(*this);
105  }
107  this->_vector -= other.asEigen();
108  return static_cast<Point<T, N> &>(*this);
109  }
111 
113  Extent<T, N> asExtent() const { return Extent<T, N>(static_cast<Point<T, N> const &>(*this)); }
114 
116  void shift(Extent<T, N> const &offset) { this->_vector += offset.asEigen(); }
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 
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  explicit PointBase(T val = static_cast<T>(0)) : Super(val) {}
140 
141  template <typename Vector>
142  explicit PointBase(Eigen::MatrixBase<Vector> const &vector) : Super(vector) {}
143 };
144 
150 template <typename T, int N>
151 class Point : public PointBase<T, N> {
152  typedef PointBase<T, N> Super;
153 
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> {
186  typedef PointBase<T, 2> Super;
187 
188 public:
189  typedef typename Super::EigenVector EigenVector;
190 
192  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
193 
201  template <typename U>
202  explicit Point(Point<U, 2> const &other);
203 
205  explicit Point(EigenVector const &vector) : Super(vector) {}
206 
208  explicit Point(Extent<T, 2> const &other) : Super(other.asEigen()) {}
209 
211  explicit Point(T x, T y) : Super(EigenVector(x, y)) {}
212 
214  explicit Point(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
215 
217  explicit Point(std::pair<T, T> const &xy) : Super(EigenVector(xy.first, xy.second)) {}
218 
220  explicit Point(std::tuple<T, T> const &xy) : Super(EigenVector(std::get<0>(xy), std::get<1>(xy))) {}
221 
222  void swap(Point &other) { this->_swap(other); }
223 };
224 
230 template <typename T>
231 class Point<T, 3> : public PointBase<T, 3> {
232  typedef PointBase<T, 3> Super;
233 
234 public:
235  typedef typename Super::EigenVector EigenVector;
236 
238  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
239 
247  template <typename U>
248  explicit Point(Point<U, 3> const &other);
249 
251  explicit Point(EigenVector const &vector) : Super(vector) {}
252 
254  explicit Point(Extent<T, 3> const &other) : Super(other.asEigen()) {}
255 
257  explicit Point(T x, T y, T z) : Super(EigenVector(x, y, z)) {}
258 
260  explicit Point(T const xyz[3]) : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
261 
263  explicit Point(std::tuple<T, T, T> const &xyz)
264  : Super(EigenVector(std::get<0>(xyz), std::get<1>(xyz), std::get<2>(xyz))) {}
265 
266  void swap(Point &other) { this->_swap(other); }
267 };
268 
275 
276 template <int N>
278  return lhs + Extent<double, N>(rhs);
279 }
280 
281 template <int N>
283  return Point<double, N>(lhs) + rhs;
284 }
285 
286 template <int N>
288  return lhs += Extent<double, N>(rhs);
289 }
290 
291 template <int N>
293  return Point<double, N>(lhs) + rhs;
294 }
295 
296 template <int N>
298  return lhs - Extent<double, N>(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 - Point<double, N>(rhs);
314 }
315 
316 template <int N>
318  return Point<double, N>(lhs) - rhs;
319 }
320 }
321 }
322 }
323 
324 #endif
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:192
A boolean coordinate.
Point(T x, T y, T z)
Explicit constructor from a sequence of doubles.
Definition: Point.h:257
Point< int, 2 > PointI
Definition: Point.h:269
CoordinateExpr< N > le(Point< T, N > const &other) const
Point< T, N > operator+(Extent< T, N > const &other) const
Definition: Point.h:96
CoordinateExpr< N > gt(T scalar) const
Definition: Point.h:83
std::string toString() const
Definition: Point.h:125
Super::EigenVector EigenVector
Definition: Point.h:235
Point(std::tuple< T, T, T > const &xyz)
Construct from std::tuple.
Definition: Point.h:263
PointBase(T val=static_cast< T >(0))
Definition: Point.h:139
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
void scale(double factor)
Definition: Point.h:118
Point< T, N > & operator+=(Extent< T, N > const &other)
Definition: Point.h:102
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:251
Point< T, N > operator-(Extent< T, N > const &other) const
Definition: Point.h:99
void swap(Point &other)
Definition: Point.h:176
A coordinate class intended to represent offsets and dimensions.
CoordinateExpr< N > ge(T scalar) const
Definition: Point.h:84
Extent< T, N > asExtent() const
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:113
bool operator==(Point< T, N > const &other) const
Standard equality comparison.
Definition: Point.h:51
Super::EigenVector EigenVector
Definition: Point.h:155
Point(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Point.h:217
STL namespace.
CoordinateExpr< N > gt(Point< T, N > const &other) const
Point(std::tuple< T, T > const &xy)
Construct from std::tuple.
Definition: Point.h:220
Point< double, 2 > PointD
Definition: Point.h:272
CoordinateExpr< N > lt(T scalar) const
Definition: Point.h:81
Point< T, N > & operator-=(Extent< T, N > const &other)
Definition: Point.h:106
PointBase(Eigen::MatrixBase< Vector > const &vector)
Definition: Point.h:142
Point< int, 3 > Point3I
Definition: Point.h:271
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:208
STL class.
Super::EigenVector EigenVector
Definition: Point.h:189
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:158
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
Point(Extent< T, N > const &other)
Explicit constructor from Extent.
Definition: Point.h:174
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
A base class for image defects.
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:171
CoordinateExpr< N > eq(Point< T, N > const &other) const
Point< double, 2 > Point2D
Definition: Point.h:273
void shift(Extent< T, N > const &offset)
Shift the point by the given offset.
Definition: Point.h:116
CoordinateExpr< N > eq(T scalar) const
Definition: Point.h:79
void swap(Point &other)
Definition: Point.h:266
T str(T... args)
double distanceSquared(PointBase< T, N > const &other) const
Definition: Point.h:120
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:265
CoordinateExpr< N > ne(Point< T, N > const &other) const
Extent< T, N > operator-(Point< T, N > const &other) const
Definition: Point.h:93
double x
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:223
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
void swap(Point &other)
Definition: Point.h:222
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:238
CoordinateExpr< N > le(T scalar) const
Definition: Point.h:82
bool operator!=(Point< T, N > const &other) const
Standard inequality comparison.
Definition: Point.h:58
CoordinateExpr< N > ge(Point< T, N > const &other) const
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:205
Point(T x, T y)
Explicit constructor from a pair of doubles.
Definition: Point.h:211
CoordinateExpr< N > lt(Point< T, N > const &other) const
Point< int, 2 > Point2I
Definition: Point.h:270
Point(Extent< T, 3 > const &other)
Explicit constructor from Extent.
Definition: Point.h:254
Point< double, 3 > Point3D
Definition: Point.h:274
Point(T const xy[2])
Construct from a two-element array.
Definition: Point.h:214
CoordinateExpr< N > ne(T scalar) const
Definition: Point.h:80
Point(T const xyz[3])
Construct from a two-element array.
Definition: Point.h:260