LSSTApplications  12.1-5-gbdcc3ab+2,15.0+14,15.0+30,15.0-1-g19261fa+21,15.0-1-g417ea41,15.0-1-g60afb23+30,15.0-1-g615e0bb+22,15.0-1-g788a293+30,15.0-1-ga91101e+30,15.0-1-gae1598d+13,15.0-1-gd076f1f+28,15.0-1-gdf18595+5,15.0-12-g3681e7a+8,15.0-12-g7952b551+2,15.0-16-g6f0eb036+3,15.0-17-g076ea75+3,15.0-2-g100d730+23,15.0-2-g8aea5f4+1,15.0-2-gf38729e+25,15.0-2-gf60f3cf,15.0-3-g707930d+3,15.0-3-g9103c06+12,15.0-30-g9378914ca+1,15.0-4-g9ee0f43+3,15.0-4-gf6f1c6c+3,15.0-4-gf906033+2,15.0-5-g23e394c+18,15.0-5-g4be42a9+4,15.0-5-gae1eaf0+3,15.0-6-g09241ba+6,15.0-6-g69628aa+4,15.0-6-g81517ef+3,15.0-6-gc1213af+3,15.0-6-gfa9b38f+8,15.0-7-ged79c87+3,15.0-8-g13fca11+3,15.0-8-g67a62d3+5,15.0-8-gcf05001+5,15.0-9-g1e7c341+2,w.2018.25
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 { return all(this->eq(other)); }
54 
60  bool operator!=(Point<T, N> const &other) const { return any(this->ne(other)); }
61 
75  CoordinateExpr<N> eq(Point<T, N> const &other) const;
76  CoordinateExpr<N> ne(Point<T, N> const &other) const;
77  CoordinateExpr<N> lt(Point<T, N> const &other) const;
78  CoordinateExpr<N> le(Point<T, N> const &other) const;
79  CoordinateExpr<N> gt(Point<T, N> const &other) const;
80  CoordinateExpr<N> ge(Point<T, N> const &other) const;
81  CoordinateExpr<N> eq(T scalar) const { return this->eq(Point<T, N>(scalar)); }
82  CoordinateExpr<N> ne(T scalar) const { return this->ne(Point<T, N>(scalar)); }
83  CoordinateExpr<N> lt(T scalar) const { return this->lt(Point<T, N>(scalar)); }
84  CoordinateExpr<N> le(T scalar) const { return this->le(Point<T, N>(scalar)); }
85  CoordinateExpr<N> gt(T scalar) const { return this->gt(Point<T, N>(scalar)); }
86  CoordinateExpr<N> ge(T scalar) const { return this->ge(Point<T, N>(scalar)); }
88 
95  Extent<T, N> operator-(Point<T, N> const &other) const {
96  return Extent<T, N>(this->_vector - other._vector);
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) const {
102  return Point<T, N>(this->_vector - other.asEigen());
103  }
105  this->_vector += other.asEigen();
106  return static_cast<Point<T, N> &>(*this);
107  }
109  this->_vector -= other.asEigen();
110  return static_cast<Point<T, N> &>(*this);
111  }
113 
115  Extent<T, N> asExtent() const { return Extent<T, N>(static_cast<Point<T, N> const &>(*this)); }
116 
118  void shift(Extent<T, N> const &offset) { this->_vector += offset.asEigen(); }
119 
120  void scale(double factor) { this->_vector *= factor; }
121 
122  double distanceSquared(PointBase<T, N> const &other) const {
123  // the cast to double is lame but Eigen seems to require they be the same type
124  return (this->asEigen() - other.asEigen()).squaredNorm();
125  }
126 
128  std::stringstream out;
129  out << "Point(";
130  for (size_t i = 0; i < N; ++i) {
131  if (i != 0) {
132  out << ",";
133  }
134  out << (*this)[i];
135  }
136  out << ")";
137  return out.str();
138  }
139 
140 protected:
141  explicit PointBase(T val = static_cast<T>(0)) : Super(val) {}
142 
143  template <typename Vector>
144  explicit PointBase(Eigen::MatrixBase<Vector> const &vector) : Super(vector) {}
145 };
146 
152 template <typename T, int N>
153 class Point : public PointBase<T, N> {
154  typedef PointBase<T, N> Super;
155 
156 public:
157  typedef typename Super::EigenVector EigenVector;
158 
160  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
161 
162  Point(Point const &) = default;
163  Point(Point &&) = default;
164  ~Point() = default;
165 
166  Point &operator=(Point const &) = default;
167  Point &operator=(Point &&) = default;
168 
176  template <typename U>
177  explicit Point(Point<U, N> const &other);
178 
180  explicit Point(EigenVector const &vector) : Super(vector) {}
181 
183  explicit Point(Extent<T, N> const &other) : Super(other.asEigen()) {}
184 
185  void swap(Point &other) { this->_swap(other); }
186 };
187 
193 template <typename T>
194 class Point<T, 2> : public PointBase<T, 2> {
195  typedef PointBase<T, 2> Super;
196 
197 public:
198  typedef typename Super::EigenVector EigenVector;
199 
201  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
202 
203  Point(Point const &) = default;
204  Point(Point &&) = default;
205  ~Point() = default;
206 
207  Point &operator=(Point const &) = default;
208  Point &operator=(Point &&) = default;
209 
217  template <typename U>
218  explicit Point(Point<U, 2> const &other);
219 
221  explicit Point(EigenVector const &vector) : Super(vector) {}
222 
224  explicit Point(Extent<T, 2> const &other) : Super(other.asEigen()) {}
225 
227  explicit Point(T x, T y) : Super(EigenVector(x, y)) {}
228 
230  explicit Point(T const xy[2]) : Super(EigenVector(xy[0], xy[1])) {}
231 
233  explicit Point(std::pair<T, T> const &xy) : Super(EigenVector(xy.first, xy.second)) {}
234 
236  explicit Point(std::tuple<T, T> const &xy) : Super(EigenVector(std::get<0>(xy), std::get<1>(xy))) {}
237 
238  void swap(Point &other) { this->_swap(other); }
239 };
240 
246 template <typename T>
247 class Point<T, 3> : public PointBase<T, 3> {
248  typedef PointBase<T, 3> Super;
249 
250 public:
251  typedef typename Super::EigenVector EigenVector;
252 
254  explicit Point(T val = static_cast<T>(0)) : Super(val) {}
255 
256  Point(Point const &) = default;
257  Point(Point &&) = default;
258  ~Point() = default;
259 
260  Point &operator=(Point const &) = default;
261  Point &operator=(Point &&) = default;
262 
270  template <typename U>
271  explicit Point(Point<U, 3> const &other);
272 
274  explicit Point(EigenVector const &vector) : Super(vector) {}
275 
277  explicit Point(Extent<T, 3> const &other) : Super(other.asEigen()) {}
278 
280  explicit Point(T x, T y, T z) : Super(EigenVector(x, y, z)) {}
281 
283  explicit Point(T const xyz[3]) : Super(EigenVector(xyz[0], xyz[1], xyz[2])) {}
284 
286  explicit Point(std::tuple<T, T, T> const &xyz)
287  : Super(EigenVector(std::get<0>(xyz), std::get<1>(xyz), std::get<2>(xyz))) {}
288 
289  void swap(Point &other) { this->_swap(other); }
290 };
291 
292 // Hash functions
293 template <typename T, int N>
294 std::size_t hash_value(Point<T, N> const &point);
295 
302 
303 template <int N>
305  return lhs + Extent<double, N>(rhs);
306 }
307 
308 template <int N>
310  return Point<double, N>(lhs) + rhs;
311 }
312 
313 template <int N>
315  return lhs += Extent<double, N>(rhs);
316 }
317 
318 template <int N>
320  return Point<double, N>(lhs) + rhs;
321 }
322 
323 template <int N>
325  return lhs - Extent<double, N>(rhs);
326 }
327 
328 template <int N>
330  return lhs -= Extent<double, N>(rhs);
331 }
332 
333 template <int N>
335  return Point<double, N>(lhs) - rhs;
336 }
337 
338 template <int N>
340  return lhs - Point<double, N>(rhs);
341 }
342 
343 template <int N>
345  return Point<double, N>(lhs) - rhs;
346 }
347 
348 } // namespace geom
349 } // namespace lsst
350 
351 #endif
Point(std::pair< T, T > const &xy)
Construct from a std::pair.
Definition: Point.h:233
A CRTP base class for coordinate objects.
Point(T const xyz[3])
Construct from a two-element array.
Definition: Point.h:283
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:160
void shift(Extent< T, N > const &offset)
Shift the point by the given offset.
Definition: Point.h:118
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:274
std::size_t hash_value(Point< T, N > const &point)
Definition: Point.cc:118
Point< T, N > operator+(Extent< T, N > const &other) const
Definition: Point.h:98
CoordinateExpr< N > gt(Point< T, N > const &other) const
Definition: Point.cc:104
Point(T x, T y)
Explicit constructor from a pair of doubles.
Definition: Point.h:227
Super::EigenVector EigenVector
Definition: Point.h:198
Extent< T, N > asExtent() const
Cast this object to an Extent of the same numeric type and dimensionality.
Definition: Point.h:115
A coordinate class intended to represent absolute positions.
CoordinateExpr< N > ne(T scalar) const
Definition: Point.h:82
int y
Definition: SpanSet.cc:44
STL namespace.
ImageT val
Definition: CR.cc:146
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:232
Point(T const xy[2])
Construct from a two-element array.
Definition: Point.h:230
std::string toString() const
Definition: Point.h:127
void scale(double factor)
Definition: Point.h:120
CoordinateExpr< N > gt(T scalar) const
Definition: Point.h:85
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:180
Point< int, 2 > PointI
Definition: Point.h:296
double distanceSquared(PointBase< T, N > const &other) const
Definition: Point.h:122
Point< double, 2 > Point2D
Definition: Point.h:300
STL class.
Point< int, 3 > Point3I
Definition: Point.h:298
A coordinate class intended to represent offsets and dimensions (3-d specialization).
Definition: Extent.h:281
PointBase(T val=static_cast< T >(0))
Definition: Point.h:141
CoordinateExpr< N > ge(T scalar) const
Definition: Point.h:86
CoordinateExpr< N > ne(Point< T, N > const &other) const
Definition: Point.cc:83
bool all(CoordinateExpr< N > const &expr)
Return true if all elements are true.
Eigen::Matrix< int, N, 1, Eigen::DontAlign > EigenVector
A base class for image defects.
Definition: cameraGeom.dox:3
void swap(Point &other)
Definition: Point.h:289
bool operator!=(Point< T, N > const &other) const
Standard inequality comparison.
Definition: Point.h:60
A boolean coordinate.
PointBase & operator=(PointBase const &)=default
T str(T... args)
Point(std::tuple< T, T, T > const &xyz)
Construct from std::tuple.
Definition: Point.h:286
Super::EigenVector EigenVector
Definition: Point.h:157
A coordinate class intended to represent offsets and dimensions.
Point< int, 2 > Point2I
Definition: Point.h:297
double x
CoordinateExpr< N > eq(Point< T, N > const &other) const
Definition: Point.cc:76
void swap(Point &other)
Definition: Point.h:185
CoordinateExpr< N > ge(Point< T, N > const &other) const
Definition: Point.cc:111
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:254
void swap(Point &other)
Definition: Point.h:238
Point(T x, T y, T z)
Explicit constructor from a sequence of doubles.
Definition: Point.h:280
Super::EigenVector EigenVector
Definition: Point.h:251
Point(EigenVector const &vector)
Construct a Point from an Eigen vector.
Definition: Point.h:221
Point< double, 3 > Point3D
Definition: Point.h:301
Point< T, N > & operator-=(Extent< T, N > const &other)
Definition: Point.h:108
Point(Extent< T, 2 > const &other)
Explicit constructor from Extent.
Definition: Point.h:224
bool operator==(Point< T, N > const &other) const
Standard equality comparison.
Definition: Point.h:53
CoordinateExpr< N > le(Point< T, N > const &other) const
Definition: Point.cc:97
CoordinateExpr< N > lt(T scalar) const
Definition: Point.h:83
Point(Extent< T, N > const &other)
Explicit constructor from Extent.
Definition: Point.h:183
ItemVariant const * other
Definition: Schema.cc:55
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
Extent< T, N > operator-(Point< T, N > const &other) const
Definition: Point.h:95
PointBase(Eigen::MatrixBase< Vector > const &vector)
Definition: Point.h:144
Point(Extent< T, 3 > const &other)
Explicit constructor from Extent.
Definition: Point.h:277
Point(std::tuple< T, T > const &xy)
Construct from std::tuple.
Definition: Point.h:236
Point< T, N > & operator+=(Extent< T, N > const &other)
Definition: Point.h:104
CoordinateExpr< N > lt(Point< T, N > const &other) const
Definition: Point.cc:90
CoordinateExpr< N > le(T scalar) const
Definition: Point.h:84
CoordinateExpr< N > eq(T scalar) const
Definition: Point.h:81
Point(T val=static_cast< T >(0))
Construct a Point with all elements set to the same scalar value.
Definition: Point.h:201
PointBase(PointBase const &)=default
Point< T, N > operator-(Extent< T, N > const &other) const
Definition: Point.h:101
Point< double, 2 > PointD
Definition: Point.h:299
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
double z
Definition: Match.cc:44