LSSTApplications  15.0+21,16.0+1,16.0+10,16.0+3,16.0+4,16.0-1-g2115a9e+4,16.0-1-g4515a79+8,16.0-1-g7bb14cc,16.0-1-g80120d7+6,16.0-1-g98efed3+6,16.0-1-gb7f560d+3,16.0-18-g7a076d417,16.0-2-g2ed7261+3,16.0-2-g311bfd2,16.0-2-g568a347+5,16.0-2-g7adb079,16.0-2-gd4c87cb+5,16.0-3-g099ede0,16.0-3-g150e024+5,16.0-3-g1f513a6+2,16.0-3-g958ce35,16.0-3-gc6a11d1,16.0-4-g84f75fb+7,16.0-4-gcfd1396+6,16.0-4-gde8cee2,16.0-5-g7bc0afb+5,16.0-5-g81851deb,16.0-5-g82b7855+1,16.0-5-gd32631f,16.0-5-gf14cb0b,16.0-6-g2dd73041+6,16.0-6-gcf12234+1,16.0-7-g95fb7bf+2,16.0-7-gc37dbc2+6,w.2018.28
LSSTDataManagementBasePackage
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 
245  explicit Point(T x, T y) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE) : Super(EigenVector(x, y)) {}
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 
302  explicit Point(T x, T y, T z) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
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);
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 #endif
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
A CRTP base class for coordinate objects.
Point< T, N > & operator+=(Extent< T, N > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: Point.h:116
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
void swap(Point &other) noexcept
Definition: Point.h:313
Point(Extent< T, 3 > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from Extent.
Definition: Point.h:298
CoordinateExpr< N > lt(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:87
std::size_t hash_value(Point< T, N > const &point)
Definition: Point.cc:118
bool operator==(Point< T, N > const &other) const noexcept
Standard equality comparison.
Definition: Point.h:53
void shift(Extent< T, N > const &offset) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Shift the point by the given offset.
Definition: Point.h:132
Extent< T, N > operator-(Point< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:107
CoordinateExpr< N > gt(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:93
CoordinateExpr< N > ne(Point< T, N > const &other) const noexcept
Definition: Point.cc:83
Super::EigenVector EigenVector
Definition: Point.h:215
Point(T x, T y, T z) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from a sequence of doubles.
Definition: Point.h:302
A coordinate class intended to represent absolute positions.
CoordinateExpr< N > eq(Point< T, N > const &other) const noexcept
Definition: Point.cc:76
int y
Definition: SpanSet.cc:44
CoordinateExpr< N > ge(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:96
STL namespace.
Point(T x, T y) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from a pair of doubles.
Definition: Point.h:245
ImageT val
Definition: CR.cc:146
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:254
std::string toString() const
Definition: Point.h:143
Point< T, N > & operator-=(Extent< T, N > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: Point.h:120
Point< int, 2 > PointI
Definition: Point.h:320
Point(std::pair< T, T > const &xy) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from a std::pair.
Definition: Point.h:252
void swap(Point &other) noexcept
Definition: Point.h:259
Point(Extent< T, 2 > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from Extent.
Definition: Point.h:241
Point< double, 2 > Point2D
Definition: Point.h:324
bool any(CoordinateExpr< N > const &expr) noexcept
Return true if any elements are true.
void _swap(CoordinateBase &other) noexcept
STL class.
Point< int, 3 > Point3I
Definition: Point.h:322
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:307
CoordinateExpr< N > eq(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:81
bool all(CoordinateExpr< N > const &expr) noexcept
Return true if all elements are true.
Point(T const xy[2]) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from a two-element array.
Definition: Point.h:248
Eigen::Matrix< int, N, 1, Eigen::DontAlign > EigenVector
A base class for image defects.
Definition: cameraGeom.dox:3
CoordinateExpr< N > ge(Point< T, N > const &other) const noexcept
Definition: Point.cc:111
A boolean coordinate.
void scale(double factor) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:136
PointBase & operator=(PointBase const &)=default
T str(T... args)
Super::EigenVector EigenVector
Definition: Point.h:173
Point(T const xyz[3]) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from a two-element array.
Definition: Point.h:306
A coordinate class intended to represent offsets and dimensions.
Point< int, 2 > Point2I
Definition: Point.h:321
Point< T, N > operator-(Extent< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:113
double x
void swap(Point &other) noexcept
Definition: Point.h:202
CoordinateExpr< N > le(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:90
Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point from an Eigen vector.
Definition: Point.h:238
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:272
CoordinateExpr< N > lt(Point< T, N > const &other) const noexcept
Definition: Point.cc:90
Point< double, 3 > Point3D
Definition: Point.h:325
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
Point(std::tuple< T, T > const &xy) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from std::tuple.
Definition: Point.h:256
ItemVariant const * other
Definition: Schema.cc:55
Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point from an Eigen vector.
Definition: Point.h:295
PointBase(Eigen::MatrixBase< Vector > const &vector)
Definition: Point.h:160
Point(std::tuple< T, T, T > const &xyz) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct from std::tuple.
Definition: Point.h:310
Point< T, N > operator+(Extent< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:110
Point(Extent< T, N > const &other) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Explicit constructor from Extent.
Definition: Point.h:199
PointBase(PointBase const &)=default
bool operator!=(Point< T, N > const &other) const noexcept
Standard inequality comparison.
Definition: Point.h:60
double distanceSquared(PointBase< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:138
CoordinateExpr< N > le(Point< T, N > const &other) const noexcept
Definition: Point.cc:97
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
CoordinateExpr< N > ne(T scalar) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:84
Point< double, 2 > PointD
Definition: Point.h:323
CoordinateExpr< N > gt(Point< T, N > const &other) const noexcept
Definition: Point.cc:104
PointBase(T val=static_cast< T >(0)) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Definition: Point.h:157
double z
Definition: Match.cc:44
Point(EigenVector const &vector) noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Construct a Point from an Eigen vector.
Definition: Point.h:196