LSSTApplications  12.1-5-gbdcc3ab+2,15.0+13,15.0+26,15.0-1-g19261fa+17,15.0-1-g60afb23+26,15.0-1-g615e0bb+18,15.0-1-g788a293+26,15.0-1-ga91101e+26,15.0-1-gae1598d+12,15.0-1-gd076f1f+24,15.0-1-gdf18595+5,15.0-1-gf4f1c34+12,15.0-11-g7db6e543+4,15.0-12-g3681e7a+4,15.0-15-gc15de322,15.0-16-g83b84f4,15.0-2-g100d730+19,15.0-2-g1f9c9cf+4,15.0-2-g8aea5f4+1,15.0-2-gf38729e+21,15.0-29-ga12a2b06e,15.0-3-g11fe1a0+14,15.0-3-g707930d+3,15.0-3-g9103c06+12,15.0-3-gd3cbb57+3,15.0-4-g2d82b59,15.0-4-g535e784+10,15.0-4-g92ca6c3+4,15.0-4-gf906033+2,15.0-5-g23e394c+14,15.0-5-g4be42a9,15.0-6-g69628aa,15.0-6-g86e3f3d+1,15.0-6-gfa9b38f+4,15.0-7-g949993c+3,15.0-8-g67a62d3+1,15.0-8-gcf05001+1,15.0-9-g1e7c341+1,w.2018.21
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 
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:
46  PointBase(PointBase const &) = default;
47  PointBase(PointBase &&) = default;
48  PointBase &operator=(PointBase const &) = default;
49  PointBase &operator=(PointBase &&) = default;
50  ~PointBase() = default;
51 
57  bool operator==(Point<T, N> const &other) const { return all(this->eq(other)); }
58 
64  bool operator!=(Point<T, N> const &other) const { return any(this->ne(other)); }
65 
79  CoordinateExpr<N> eq(Point<T, N> const &other) const;
80  CoordinateExpr<N> ne(Point<T, N> const &other) const;
81  CoordinateExpr<N> lt(Point<T, N> const &other) const;
82  CoordinateExpr<N> le(Point<T, N> const &other) const;
83  CoordinateExpr<N> gt(Point<T, N> const &other) const;
84  CoordinateExpr<N> ge(Point<T, N> const &other) const;
85  CoordinateExpr<N> eq(T scalar) const { return this->eq(Point<T, N>(scalar)); }
86  CoordinateExpr<N> ne(T scalar) const { return this->ne(Point<T, N>(scalar)); }
87  CoordinateExpr<N> lt(T scalar) const { return this->lt(Point<T, N>(scalar)); }
88  CoordinateExpr<N> le(T scalar) const { return this->le(Point<T, N>(scalar)); }
89  CoordinateExpr<N> gt(T scalar) const { return this->gt(Point<T, N>(scalar)); }
90  CoordinateExpr<N> ge(T scalar) const { return this->ge(Point<T, N>(scalar)); }
92 
99  Extent<T, N> operator-(Point<T, N> const &other) const {
100  return Extent<T, N>(this->_vector - other._vector);
101  }
102  Point<T, N> operator+(Extent<T, N> const &other) const {
103  return Point<T, N>(this->_vector + other.asEigen());
104  }
105  Point<T, N> operator-(Extent<T, N> const &other) const {
106  return Point<T, N>(this->_vector - other.asEigen());
107  }
109  this->_vector += other.asEigen();
110  return static_cast<Point<T, N> &>(*this);
111  }
113  this->_vector -= other.asEigen();
114  return static_cast<Point<T, N> &>(*this);
115  }
117 
119  Extent<T, N> asExtent() const { return Extent<T, N>(static_cast<Point<T, N> const &>(*this)); }
120 
122  void shift(Extent<T, N> const &offset) { this->_vector += offset.asEigen(); }
123 
124  void scale(double factor) { this->_vector *= factor; }
125 
126  double distanceSquared(PointBase<T, N> const &other) const {
127  // the cast to double is lame but Eigen seems to require they be the same type
128  return (this->asEigen() - other.asEigen()).squaredNorm();
129  }
130 
132  std::stringstream out;
133  out << "Point(";
134  for (size_t i = 0; i < N; ++i) {
135  if (i != 0) {
136  out << ",";
137  }
138  out << (*this)[i];
139  }
140  out << ")";
141  return out.str();
142  }
143 
144 protected:
145  explicit PointBase(T val = static_cast<T>(0)) : Super(val) {}
146 
147  template <typename Vector>
148  explicit PointBase(Eigen::MatrixBase<Vector> const &vector) : Super(vector) {}
149 };
150 
156 template <typename T, int N>
157 class Point : public PointBase<T, N> {
158  typedef PointBase<T, N> Super;
159 
160 public:
161  typedef typename Super::EigenVector EigenVector;
162 
164  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
165 
166  Point(Point const &) = default;
167  Point(Point &&) = default;
168  ~Point() = default;
169 
170  Point &operator=(Point const &) = default;
171  Point &operator=(Point &&) = default;
172 
180  template <typename U>
181  explicit Point(Point<U, N> const &other);
182 
184  explicit Point(EigenVector const &vector) : Super(vector) {}
185 
187  explicit Point(Extent<T, N> const &other) : Super(other.asEigen()) {}
188 
189  void swap(Point &other) { this->_swap(other); }
190 };
191 
197 template <typename T>
198 class Point<T, 2> : public PointBase<T, 2> {
199  typedef PointBase<T, 2> Super;
200 
201 public:
202  typedef typename Super::EigenVector EigenVector;
203 
205  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
206 
207  Point(Point const &) = default;
208  Point(Point &&) = default;
209  ~Point() = default;
210 
211  Point &operator=(Point const &) = default;
212  Point &operator=(Point &&) = default;
213 
221  template <typename U>
222  explicit Point(Point<U, 2> const &other);
223 
225  explicit Point(EigenVector const &vector) : Super(vector) {}
226 
228  explicit Point(Extent<T, 2> const &other) : Super(other.asEigen()) {}
229 
231  explicit Point(T x, T y) : Super(EigenVector(x, y)) {}
232 
234  explicit Point(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
235 
237  explicit Point(std::pair<T, T> const &xy) : Super(EigenVector(xy.first, xy.second)) {}
238 
240  explicit Point(std::tuple<T, T> const &xy) : Super(EigenVector(std::get<0>(xy), std::get<1>(xy))) {}
241 
242  void swap(Point &other) { this->_swap(other); }
243 };
244 
250 template <typename T>
251 class Point<T, 3> : public PointBase<T, 3> {
252  typedef PointBase<T, 3> Super;
253 
254 public:
255  typedef typename Super::EigenVector EigenVector;
256 
258  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
259 
260  Point(Point const &) = default;
261  Point(Point &&) = default;
262  ~Point() = default;
263 
264  Point &operator=(Point const &) = default;
265  Point &operator=(Point &&) = default;
266 
274  template <typename U>
275  explicit Point(Point<U, 3> const &other);
276 
278  explicit Point(EigenVector const &vector) : Super(vector) {}
279 
281  explicit Point(Extent<T, 3> const &other) : Super(other.asEigen()) {}
282 
284  explicit Point(T x, T y, T z) : Super(EigenVector(x, y, z)) {}
285 
287  explicit Point(T const xyz[3]) : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
288 
290  explicit Point(std::tuple<T, T, T> const &xyz)
291  : Super(EigenVector(std::get<0>(xyz), std::get<1>(xyz), std::get<2>(xyz))) {}
292 
293  void swap(Point &other) { this->_swap(other); }
294 };
295 
296 // Hash functions
297 template <typename T, int N>
298 std::size_t hash_value(Point<T, N> const& point);
299 
306 
307 template <int N>
309  return lhs + Extent<double, N>(rhs);
310 }
311 
312 template <int N>
314  return Point<double, N>(lhs) + rhs;
315 }
316 
317 template <int N>
319  return lhs += Extent<double, N>(rhs);
320 }
321 
322 template <int N>
324  return Point<double, N>(lhs) + rhs;
325 }
326 
327 template <int N>
329  return lhs - Extent<double, N>(rhs);
330 }
331 
332 template <int N>
334  return lhs -= Extent<double, N>(rhs);
335 }
336 
337 template <int N>
339  return Point<double, N>(lhs) - rhs;
340 }
341 
342 template <int N>
344  return lhs - Point<double, N>(rhs);
345 }
346 
347 template <int N>
349  return Point<double, N>(lhs) - rhs;
350 }
351 }
352 }
353 }
354 
355 #endif
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:205
A boolean coordinate.
Point(T x, T y, T z)
Explicit constructor from a sequence of doubles.
Definition: Point.h:284
Point< int, 2 > PointI
Definition: Point.h:300
Point< T, N > operator+(Extent< T, N > const &other) const
Definition: Point.h:102
CoordinateExpr< N > gt(T scalar) const
Definition: Point.h:89
std::string toString() const
Definition: Point.h:131
Super::EigenVector EigenVector
Definition: Point.h:255
Point(std::tuple< T, T, T > const &xyz)
Construct from std::tuple.
Definition: Point.h:290
PointBase(T val=static_cast< T >(0))
Definition: Point.h:145
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
void scale(double factor)
Definition: Point.h:124
CoordinateExpr< N > gt(Point< T, N > const &other) const
Definition: Point.cc:106
Point< T, N > & operator+=(Extent< T, N > const &other)
Definition: Point.h:108
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:278
Point< T, N > operator-(Extent< T, N > const &other) const
Definition: Point.h:105
void swap(Point &other)
Definition: Point.h:189
A coordinate class intended to represent offsets and dimensions.
CoordinateExpr< N > ge(T scalar) const
Definition: Point.h:90
Extent< T, N > asExtent() const
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:119
bool operator==(Point< T, N > const &other) const
Standard equality comparison.
Definition: Point.h:57
int y
Definition: SpanSet.cc:44
Super::EigenVector EigenVector
Definition: Point.h:161
Point(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Point.h:237
STL namespace.
ImageT val
Definition: CR.cc:158
Point(std::tuple< T, T > const &xy)
Construct from std::tuple.
Definition: Point.h:240
Point< double, 2 > PointD
Definition: Point.h:303
CoordinateExpr< N > lt(T scalar) const
Definition: Point.h:87
CoordinateExpr< N > ne(Point< T, N > const &other) const
Definition: Point.cc:85
PointBase(PointBase const &)=default
Point< T, N > & operator-=(Extent< T, N > const &other)
Definition: Point.h:112
PointBase(Eigen::MatrixBase< Vector > const &vector)
Definition: Point.h:148
Point< int, 3 > Point3I
Definition: Point.h:302
CoordinateExpr< N > eq(Point< T, N > const &other) const
Definition: Point.cc:78
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:228
STL class.
CoordinateExpr< N > ge(Point< T, N > const &other) const
Definition: Point.cc:113
Super::EigenVector EigenVector
Definition: Point.h:202
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:164
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:187
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
A base class for image defects.
Definition: cameraGeom.dox:3
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:184
Point< double, 2 > Point2D
Definition: Point.h:304
void shift(Extent< T, N > const &offset)
Shift the point by the given offset.
Definition: Point.h:122
CoordinateExpr< N > eq(T scalar) const
Definition: Point.h:85
void swap(Point &other)
Definition: Point.h:293
T str(T... args)
double distanceSquared(PointBase< T, N > const &other) const
Definition: Point.h:126
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:285
Extent< T, N > operator-(Point< T, N > const &other) const
Definition: Point.h:99
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:236
double x
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
CoordinateExpr< N > le(Point< T, N > const &other) const
Definition: Point.cc:99
void swap(Point &other)
Definition: Point.h:242
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:258
CoordinateExpr< N > le(T scalar) const
Definition: Point.h:88
bool operator!=(Point< T, N > const &other) const
Standard inequality comparison.
Definition: Point.h:64
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:225
Point(T x, T y)
Explicit constructor from a pair of doubles.
Definition: Point.h:231
ItemVariant const * other
Definition: Schema.cc:55
Point< int, 2 > Point2I
Definition: Point.h:301
CoordinateExpr< N > lt(Point< T, N > const &other) const
Definition: Point.cc:92
Point(Extent< T, 3 > const &other)
Explicit constructor from Extent.
Definition: Point.h:281
Point< double, 3 > Point3D
Definition: Point.h:305
Point(T const xy[2])
Construct from a two-element array.
Definition: Point.h:234
PointBase & operator=(PointBase const &)=default
CoordinateExpr< N > ne(T scalar) const
Definition: Point.h:86
Point(T const xyz[3])
Construct from a two-element array.
Definition: Point.h:287
std::size_t hash_value(Point< T, N > const &point)
Definition: Point.cc:120
double z
Definition: Match.cc:44