LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Point.h
Go to the documentation of this file.
1 /*
2  * Developed for the LSST Data Management System.
3  * This product includes software developed by the LSST Project
4  * (https://www.lsst.org).
5  * See the COPYRIGHT file at the top-level directory of this distribution
6  * for details of code ownership.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 /*
23  * A coordinate class intended to represent absolute positions.
24  */
25 #ifndef LSST_GEOM_POINT_H
26 #define LSST_GEOM_POINT_H
27 
28 #include <tuple>
29 
32 #include "lsst/geom/Extent.h"
33 
34 namespace lsst {
35 namespace geom {
36 
37 template <typename T, int N>
38 class PointBase : public CoordinateBase<Point<T, N>, T, N> {
39  typedef CoordinateBase<Point<T, N>, T, N> Super;
40 
41 public:
42  PointBase(PointBase const &) = default;
43  PointBase(PointBase &&) = default;
44  PointBase &operator=(PointBase const &) = default;
45  PointBase &operator=(PointBase &&) = default;
46  ~PointBase() = default;
47 
53  bool operator==(Point<T, N> const &other) const noexcept { return all(this->eq(other)); }
54 
60  bool operator!=(Point<T, N> const &other) const noexcept { return any(this->ne(other)); }
61 
75  CoordinateExpr<N> eq(Point<T, N> const &other) const noexcept;
76  CoordinateExpr<N> ne(Point<T, N> const &other) const noexcept;
77  CoordinateExpr<N> lt(Point<T, N> const &other) const noexcept;
78  CoordinateExpr<N> le(Point<T, N> const &other) const noexcept;
79  CoordinateExpr<N> gt(Point<T, N> const &other) const noexcept;
80  CoordinateExpr<N> ge(Point<T, N> const &other) const noexcept;
82  return this->eq(Point<T, N>(scalar));
83  }
85  return this->ne(Point<T, N>(scalar));
86  }
88  return this->lt(Point<T, N>(scalar));
89  }
91  return this->le(Point<T, N>(scalar));
92  }
94  return this->gt(Point<T, N>(scalar));
95  }
97  return this->ge(Point<T, N>(scalar));
98  }
100 
108  return Extent<T, N>(this->_vector - other._vector);
109  }
111  return Point<T, N>(this->_vector + other.asEigen());
112  }
114  return Point<T, N>(this->_vector - other.asEigen());
115  }
117  this->_vector += other.asEigen();
118  return static_cast<Point<T, N> &>(*this);
119  }
121  this->_vector -= other.asEigen();
122  return static_cast<Point<T, N> &>(*this);
123  }
125 
128  return Extent<T, N>(static_cast<Point<T, N> const &>(*this));
129  }
130 
132  void shift(Extent<T, N> const &offset) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) {
133  this->_vector += offset.asEigen();
134  }
135 
136  void scale(double factor) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) { this->_vector *= factor; }
137 
139  // the cast to double is lame but Eigen seems to require they be the same type
140  return (this->asEigen() - other.asEigen()).squaredNorm();
141  }
142 
144  std::stringstream out;
145  out << "Point(";
146  for (size_t i = 0; i < N; ++i) {
147  if (i != 0) {
148  out << ",";
149  }
150  out << (*this)[i];
151  }
152  out << ")";
153  return out.str();
154  }
155 
156 protected:
157  explicit PointBase(T val = static_cast<T>(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(val) {}
158 
159  template <typename Vector>
160  explicit PointBase(Eigen::MatrixBase<Vector> const &vector) : Super(vector) {}
161 };
162 
168 template <typename T, int N>
169 class Point : public PointBase<T, N> {
170  typedef PointBase<T, N> Super;
171 
172 public:
173  typedef typename Super::EigenVector EigenVector;
174 
176  explicit Point(T val = static_cast<T>(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(val) {}
177 
178  Point(Point const &) = default;
179  Point(Point &&) = default;
180  ~Point() = default;
181 
182  Point &operator=(Point const &) = default;
183  Point &operator=(Point &&) = default;
184 
192  template <typename U>
193  explicit Point(Point<U, N> const &other) noexcept(IS_NOTHROW_CONVERTIBLE<T, U>);
194 
196  explicit Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(vector) {}
197 
199  explicit Point(Extent<T, N> const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
200  : Super(other.asEigen()) {}
201 
202  void swap(Point &other) noexcept { this->_swap(other); }
203 };
204 
210 template <typename T>
211 class Point<T, 2> : public PointBase<T, 2> {
212  typedef PointBase<T, 2> Super;
213 
214 public:
215  typedef typename Super::EigenVector EigenVector;
216 
218  explicit Point(T val = static_cast<T>(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(val) {}
219 
220  Point(Point const &) = default;
221  Point(Point &&) = default;
222  ~Point() = default;
223 
224  Point &operator=(Point const &) = default;
225  Point &operator=(Point &&) = default;
226 
234  template <typename U>
235  explicit Point(Point<U, 2> const &other) noexcept(IS_NOTHROW_CONVERTIBLE<T, U>);
236 
238  explicit Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(vector) {}
239 
241  explicit Point(Extent<T, 2> const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
242  : Super(other.asEigen()) {}
243 
246 
248  explicit Point(T const xy[2]) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
249  : Super(EigenVector(xy[0], xy[1])) {}
250 
253  : Super(EigenVector(xy.first, xy.second)) {}
254 
257  : Super(EigenVector(std::get<0>(xy), std::get<1>(xy))) {}
258 
259  void swap(Point &other) noexcept { this->_swap(other); }
260 };
261 
267 template <typename T>
268 class Point<T, 3> : public PointBase<T, 3> {
269  typedef PointBase<T, 3> Super;
270 
271 public:
272  typedef typename Super::EigenVector EigenVector;
273 
275  explicit Point(T val = static_cast<T>(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(val) {}
276 
277  Point(Point const &) = default;
278  Point(Point &&) = default;
279  ~Point() = default;
280 
281  Point &operator=(Point const &) = default;
282  Point &operator=(Point &&) = default;
283 
291  template <typename U>
292  explicit Point(Point<U, 3> const &other) noexcept(IS_NOTHROW_CONVERTIBLE<T, U>);
293 
295  explicit Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(vector) {}
296 
298  explicit Point(Extent<T, 3> const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
299  : Super(other.asEigen()) {}
300 
303  : Super(EigenVector(x, y, z)) {}
304 
306  explicit Point(T const xyz[3]) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
307  : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
308 
311  : Super(EigenVector(std::get<0>(xyz), std::get<1>(xyz), std::get<2>(xyz))) {}
312 
313  void swap(Point &other) noexcept { this->_swap(other); }
314 };
315 
316 // Hash functions
317 template <typename T, int N>
318 std::size_t hash_value(Point<T, N> const &point) noexcept;
319 
326 
327 template <int N>
328 Point<double, N> operator+(Point<double, N> const &lhs, Extent<int, N> const &rhs) noexcept {
329  return lhs + Extent<double, N>(rhs);
330 }
331 
332 template <int N>
333 Point<double, N> operator+(Extent<int, N> const &rhs, Point<double, N> const &lhs) noexcept {
334  return Point<double, N>(lhs) + rhs;
335 }
336 
337 template <int N>
339  return lhs += Extent<double, N>(rhs);
340 }
341 
342 template <int N>
343 Point<double, N> operator+(Point<int, N> const &lhs, Extent<double, N> const &rhs) noexcept {
344  return Point<double, N>(lhs) + rhs;
345 }
346 
347 template <int N>
348 Point<double, N> operator-(Point<double, N> const &lhs, Extent<int, N> const &rhs) noexcept {
349  return lhs - Extent<double, N>(rhs);
350 }
351 
352 template <int N>
354  return lhs -= Extent<double, N>(rhs);
355 }
356 
357 template <int N>
358 Point<double, N> operator-(Point<int, N> const &lhs, Extent<double, N> const &rhs) noexcept {
359  return Point<double, N>(lhs) - rhs;
360 }
361 
362 template <int N>
363 Extent<double, N> operator-(Point<double, N> const &lhs, Point<int, N> const &rhs) noexcept {
364  return lhs - Point<double, N>(rhs);
365 }
366 
367 template <int N>
368 Extent<double, N> operator-(Point<int, N> const &lhs, Point<double, N> const &rhs) noexcept {
369  return Point<double, N>(lhs) - rhs;
370 }
371 
372 } // namespace geom
373 } // namespace lsst
374 
375 namespace std {
376 template <typename T, int N>
377 struct hash<lsst::geom::Point<T, N>> {
380  result_type operator()(argument_type const &x) const noexcept { return lsst::geom::hash_value(x); }
381 };
382 } // namespace std
383 
384 #endif
double x
double z
Definition: Match.cc:44
int y
Definition: SpanSet.cc:48
A CRTP base class for coordinate objects.
void _swap(CoordinateBase &other) noexcept
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
A boolean coordinate.
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:254
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:307
A coordinate class intended to represent offsets and dimensions.
Definition: Extent.h:210
Point(T val=static_cast< T >(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:218
Point(std::tuple< T, T > const &xy) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from std::tuple.
Definition: Point.h:256
Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point from an Eigen vector.
Definition: Point.h:238
Point(T x, T y) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from a pair of doubles.
Definition: Point.h:245
Super::EigenVector EigenVector
Definition: Point.h:215
Point(std::pair< T, T > const &xy) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from a std::pair.
Definition: Point.h:252
Point & operator=(Point &&)=default
Point(T const xy[2]) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from a two-element array.
Definition: Point.h:248
void swap(Point &other) noexcept
Definition: Point.h:259
Point(Point const &)=default
Point(Extent< T, 2 > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from Extent.
Definition: Point.h:241
Point(Point &&)=default
Point & operator=(Point const &)=default
Point(T const xyz[3]) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from a two-element array.
Definition: Point.h:306
Point(Point const &)=default
Point(T val=static_cast< T >(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:275
Point & operator=(Point &&)=default
Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point from an Eigen vector.
Definition: Point.h:295
Point(std::tuple< T, T, T > const &xyz) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from std::tuple.
Definition: Point.h:310
Point(Extent< T, 3 > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from Extent.
Definition: Point.h:298
void swap(Point &other) noexcept
Definition: Point.h:313
Super::EigenVector EigenVector
Definition: Point.h:272
Point(T x, T y, T z) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from a sequence of doubles.
Definition: Point.h:302
Point(Point &&)=default
Point & operator=(Point const &)=default
Point< T, N > operator+(Extent< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:110
CoordinateExpr< N > ne(Point< T, N > const &other) const noexcept
Definition: Point.cc:83
Extent< T, N > asExtent() const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:127
Point< T, N > operator-(Extent< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:113
double distanceSquared(PointBase< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:138
PointBase(PointBase &&)=default
CoordinateExpr< N > lt(Point< T, N > const &other) const noexcept
Definition: Point.cc:90
CoordinateExpr< N > le(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:90
CoordinateExpr< N > le(Point< T, N > const &other) const noexcept
Definition: Point.cc:97
CoordinateExpr< N > ne(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:84
bool operator!=(Point< T, N > const &other) const noexcept
Standard inequality comparison.
Definition: Point.h:60
CoordinateExpr< N > gt(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:93
void scale(double factor) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:136
PointBase & operator=(PointBase const &)=default
CoordinateExpr< N > lt(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:87
CoordinateExpr< N > ge(Point< T, N > const &other) const noexcept
Definition: Point.cc:111
CoordinateExpr< N > gt(Point< T, N > const &other) const noexcept
Definition: Point.cc:104
CoordinateExpr< N > eq(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:81
PointBase(PointBase const &)=default
void shift(Extent< T, N > const &offset) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Shift the point by the given offset.
Definition: Point.h:132
PointBase(Eigen::MatrixBase< Vector > const &vector)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:160
Point< T, N > & operator+=(Extent< T, N > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_ASSIGNABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:116
bool operator==(Point< T, N > const &other) const noexcept
Standard equality comparison.
Definition: Point.h:53
Extent< T, N > operator-(Point< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:107
PointBase(T val=static_cast< T >(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:157
CoordinateExpr< N > eq(Point< T, N > const &other) const noexcept
Definition: Point.cc:76
PointBase & operator=(PointBase &&)=default
Point< T, N > & operator-=(Extent< T, N > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_ASSIGNABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:120
CoordinateExpr< N > ge(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:96
std::string toString() const
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:143
A coordinate class intended to represent absolute positions.
Definition: Point.h:169
Point(Point const &)=default
Point(Extent< T, N > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from Extent.
Definition: Point.h:199
Point(T val=static_cast< T >(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:176
Super::EigenVector EigenVector
Definition: Point.h:173
Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point from an Eigen vector.
Definition: Point.h:196
void swap(Point &other) noexcept
Definition: Point.h:202
Point & operator=(Point const &)=default
Point(Point &&)=default
Point & operator=(Point &&)=default
Point< double, 2 > PointD
Definition: Point.h:323
constexpr Angle operator+(Angle a, Angle d) noexcept
Sum of two angles.
Definition: Angle.h:308
std::size_t hash_value(Extent< T, N > const &extent) noexcept
Definition: Extent.cc:127
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs) noexcept
Definition: Extent.h:470
bool any(CoordinateExpr< N > const &expr) noexcept
Return true if any elements are true.
constexpr Angle operator-(Angle a, Angle d) noexcept
Difference of two angles.
Definition: Angle.h:314
bool all(CoordinateExpr< N > const &expr) noexcept
Return true if all elements are true.
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs) noexcept
Definition: Extent.h:480
Point< int, 2 > PointI
Definition: Point.h:320
Point< double, 3 > Point3D
Definition: Point.h:325
Point< double, 2 > Point2D
Definition: Point.h:324
Point< int, 3 > Point3I
Definition: Point.h:322
Point< int, 2 > Point2I
Definition: Point.h:321
A base class for image defects.
STL namespace.
ImageT val
Definition: CR.cc:146
T str(T... args)
result_type operator()(argument_type const &x) const noexcept
Definition: Point.h:380