LSSTApplications  11.0-24-g0a022a1,14.0+76,14.0-1-g4b114ac+19,14.0-1-g6829aa7+15,14.0-1-g7257b6a+17,14.0-1-g8b7e855+69,14.0-1-gfce6a49+15,14.0-10-g2094653+13,14.0-10-gaad1ee2+1,14.0-11-g7664582+5,14.0-17-g2e0c876,14.0-18-g77a82f3,14.0-2-g14e9bfd,14.0-2-g519ff97+17,14.0-2-ga5af9b6+15,14.0-20-gc5946167,14.0-21-gb9e430a+11,14.0-28-gd4c92d7+3,14.0-4-g3609236+11,14.0-4-gae1598d+1,14.0-54-gb4e84373+2,14.0-58-g91bfccb15,14.0-6-ge2c9487+58,14.0-6-gf4f1c34,14.0-7-g0d69b06+17,14.0-8-g56b2ea8+3,14.0-9-g6668b0b
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 
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 }
349 }
350 
351 #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:296
CoordinateExpr< N > le(Point< T, N > const &other) const
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
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
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.
CoordinateExpr< N > gt(Point< T, N > const &other) const
Point(std::tuple< T, T > const &xy)
Construct from std::tuple.
Definition: Point.h:240
Point< double, 2 > PointD
Definition: Point.h:299
CoordinateExpr< N > lt(T scalar) const
Definition: Point.h:87
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:298
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.
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
CoordinateExpr< N > eq(Point< T, N > const &other) const
Point< double, 2 > Point2D
Definition: Point.h:300
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
CoordinateExpr< N > ne(Point< T, N > const &other) const
Extent< T, N > operator-(Point< T, N > const &other) const
Definition: Point.h:99
double x
A coordinate class intended to represent offsets and dimensions (2-d specialization).
Definition: Extent.h:236
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
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
CoordinateExpr< N > ge(Point< T, N > const &other) const
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
CoordinateExpr< N > lt(Point< T, N > const &other) const
Point< int, 2 > Point2I
Definition: Point.h:297
Point(Extent< T, 3 > const &other)
Explicit constructor from Extent.
Definition: Point.h:281
Point< double, 3 > Point3D
Definition: Point.h:301
Point(T const xy[2])
Construct from a two-element array.
Definition: Point.h:234
PointBase & operator=(PointBase const &)=default
ImageT val
Definition: CR.cc:158
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