LSSTApplications  20.0.0
LSSTDataManagementBasePackage
CoordinateBase.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 CRTP base class for coordinate objects, providing partial specializations for 2D and 3D.
24  */
25 #ifndef LSST_GEOM_COORDINATEBASE_H
26 #define LSST_GEOM_COORDINATEBASE_H
27 
28 #include <iostream>
29 #include <type_traits>
30 #include <tuple>
31 #include <utility>
32 
33 #include "Eigen/Core"
34 
35 namespace lsst {
36 namespace geom {
37 
38 template <typename T, int N = 2>
39 class Point;
40 template <typename T, int N = 2>
41 class Extent;
42 
44 template <typename T, typename U>
45 bool constexpr IS_NOTHROW_CONVERTIBLE =
46  std::is_nothrow_copy_constructible<T>::value&& noexcept(static_cast<T>(std::declval<U>()));
47 
53 template <typename Derived, typename T, int N>
55 public:
56  static_assert(N > 0, "CoordinateBase must have a positive length.");
57  typedef T Element;
58  static int const dimensions = N;
59  typedef Eigen::Matrix<T, N, 1, Eigen::DontAlign> EigenVector;
62 
63  // Can't use both =default and noexcept until Eigen supports noexcept
65  : _vector(other._vector) {}
67  : _vector(std::move(other._vector)) {}
69  _vector = other._vector;
70  return *this;
71  }
73  _vector = std::move(other._vector);
74  return *this;
75  }
76  ~CoordinateBase() noexcept = default;
77 
78  T& operator[](int n) { return _vector[n]; }
79  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
80  T& coeffRef(int n) { return _vector.coeffRef(n); }
81  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
82 
89  EigenVector const& asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { return _vector; }
90 
91 protected:
97  explicit CoordinateBase(T val = static_cast<T>(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
98  : _vector(EigenVector::Constant(val)) {}
99 
105  template <typename Vector>
106  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
107 
108  void _swap(CoordinateBase& other) noexcept { _vector.swap(other._vector); }
110 };
111 
119 template <typename Derived, typename T, int N>
121  T rtol = static_cast<T>(1E-5),
122  T atol = static_cast<T>(1E-8)) noexcept(std::is_nothrow_copy_constructible<T>::value&&
123  std::is_nothrow_copy_assignable<T>::value);
124 
128 template <typename Derived, typename T>
129 class CoordinateBase<Derived, T, 2> {
130 public:
131  typedef T Element;
132  static int const dimensions = 2;
133  typedef Eigen::Matrix<T, 2, 1, Eigen::DontAlign> EigenVector;
134  static bool constexpr IS_ELEMENT_NOTHROW_COPYABLE = std::is_nothrow_copy_constructible<T>::value;
135  static bool constexpr IS_ELEMENT_NOTHROW_ASSIGNABLE = std::is_nothrow_copy_assignable<T>::value;
136 
137  // Can't use both =default and noexcept until Eigen supports noexcept
138  CoordinateBase(CoordinateBase const& other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
139  : _vector(other._vector) {}
140  CoordinateBase(CoordinateBase&& other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
141  : _vector(std::move(other._vector)) {}
142  CoordinateBase& operator=(CoordinateBase const& other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE) {
143  _vector = other._vector;
144  return *this;
145  }
146  CoordinateBase& operator=(CoordinateBase&& other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE) {
147  _vector = std::move(other._vector);
148  return *this;
149  }
150  ~CoordinateBase() noexcept = default;
151 
152  T& operator[](int n) { return _vector[n]; }
153  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
154  T& coeffRef(int n) { return _vector.coeffRef(n); }
155  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
156 
163  EigenVector const& asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { return _vector; }
164 
165  T const& getX() const noexcept { return _vector.x(); }
166  T const& getY() const noexcept { return _vector.y(); }
167  T& getX() noexcept { return _vector.x(); }
168  T& getY() noexcept { return _vector.y(); }
169  void setX(T x) noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { _vector.x() = x; }
170  void setY(T y) noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { _vector.y() = y; }
171 
173  std::pair<T, T> asPair() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE) {
174  return std::make_pair(_vector.x(), _vector.y());
175  }
176 
178  std::tuple<T, T> asTuple() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE) {
179  return std::make_tuple(_vector.x(), _vector.y());
180  }
181 
182 protected:
183  explicit CoordinateBase(T val = static_cast<T>(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
184  : _vector(EigenVector::Constant(val)) {}
185 
186  template <typename Vector>
187  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
188  void _swap(CoordinateBase& other) noexcept { _vector.swap(other._vector); }
190 };
191 
195 template <typename Derived, typename T>
196 class CoordinateBase<Derived, T, 3> {
197 public:
198  typedef T Element;
199  static int const dimensions = 3;
200  typedef Eigen::Matrix<T, 3, 1, Eigen::DontAlign> EigenVector;
203 
204  // Can't use both =default and noexcept until Eigen supports noexcept
206  : _vector(other._vector) {}
208  : _vector(std::move(other._vector)) {}
210  _vector = other._vector;
211  return *this;
212  }
214  _vector = std::move(other._vector);
215  return *this;
216  }
217  ~CoordinateBase() noexcept = default;
218 
219  T& operator[](int n) { return _vector[n]; }
220  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
221  T& coeffRef(int n) { return _vector.coeffRef(n); }
222  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
223 
230  EigenVector const& asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { return _vector; }
231 
232  T const& getX() const noexcept { return _vector.x(); }
233  T const& getY() const noexcept { return _vector.y(); }
234  T const& getZ() const noexcept { return _vector.z(); }
235  T& getX() noexcept { return _vector.x(); }
236  T& getY() noexcept { return _vector.y(); }
237  T& getZ() noexcept { return _vector.z(); }
238  void setX(T x) noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { _vector.x() = x; }
239  void setY(T y) noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { _vector.y() = y; }
240  void setZ(T z) noexcept(IS_ELEMENT_NOTHROW_COPYABLE) { _vector.z() = z; }
241 
244  return std::make_tuple(_vector.x(), _vector.y(), _vector.z());
245  }
246 
247 protected:
248  explicit CoordinateBase(T val = static_cast<T>(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
249  : _vector(EigenVector::Constant(val)) {}
250 
251  template <typename Vector>
252  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
253  void _swap(CoordinateBase& other) noexcept { _vector.swap(other._vector); }
255 };
256 
257 template <typename Derived, typename T, int N>
259  os << "(" << coordinate[0];
260  for (int n = 1; n < N; ++n) os << ", " << coordinate[n];
261  return os << ")";
262 }
263 
264 } // namespace geom
265 } // namespace lsst
266 
267 #endif
lsst::geom::CoordinateBase< Derived, T, 2 >::Element
T Element
Definition: CoordinateBase.h:131
y
int y
Definition: SpanSet.cc:49
std::make_tuple
T make_tuple(T... args)
lsst::geom::CoordinateBase< Derived, T, 3 >::CoordinateBase
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
Definition: CoordinateBase.h:252
lsst::geom::CoordinateBase< Derived, T, 3 >::asEigen
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
Definition: CoordinateBase.h:230
lsst::geom::CoordinateBase< Derived, T, 3 >::EigenVector
Eigen::Matrix< T, 3, 1, Eigen::DontAlign > EigenVector
Definition: CoordinateBase.h:200
lsst::geom::CoordinateBase< Derived, T, 2 >::_vector
EigenVector _vector
Definition: CoordinateBase.h:189
lsst::geom::CoordinateBase< Derived, T, 3 >::coeffRef
T const & coeffRef(int n) const
Definition: CoordinateBase.h:222
std::move
T move(T... args)
std::pair
lsst::geom::CoordinateBase< Derived, T, 2 >::CoordinateBase
CoordinateBase(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:138
lsst::geom::CoordinateBase< Derived, T, 2 >::_swap
void _swap(CoordinateBase &other) noexcept
Definition: CoordinateBase.h:188
lsst::geom::CoordinateBase< Derived, T, 3 >::asTuple
std::tuple< T, T, T > asTuple() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a std::tuple representation of the coordinate object.
Definition: CoordinateBase.h:243
lsst::geom::CoordinateBase::operator[]
T const & operator[](int n) const
Definition: CoordinateBase.h:79
lsst::geom::CoordinateBase::operator=
CoordinateBase & operator=(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: CoordinateBase.h:68
lsst::geom::CoordinateBase< Derived, T, 2 >::getX
T & getX() noexcept
Definition: CoordinateBase.h:167
lsst::geom::CoordinateBase< Derived, T, 2 >::operator=
CoordinateBase & operator=(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: CoordinateBase.h:142
lsst::geom::CoordinateBase< Derived, T, 3 >::setY
void setY(T y) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:239
lsst::geom::CoordinateBase< Derived, T, 3 >::setX
void setX(T x) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:238
lsst::geom::CoordinateBase< Derived, T, 2 >::getY
T & getY() noexcept
Definition: CoordinateBase.h:168
lsst::geom::CoordinateBase< Derived, T, 2 >::setY
void setY(T y) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:170
std::tuple
lsst::geom::CoordinateBase< Derived, T, 2 >::getY
T const & getY() const noexcept
Definition: CoordinateBase.h:166
val
ImageT val
Definition: CR.cc:146
lsst::geom::CoordinateBase< Derived, T, 3 >::operator=
CoordinateBase & operator=(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: CoordinateBase.h:213
lsst::geom::CoordinateBase< Derived, T, 3 >::getX
T const & getX() const noexcept
Definition: CoordinateBase.h:232
lsst::geom::allclose
bool allclose(CoordinateBase< Derived, T, N > const &a, CoordinateBase< Derived, T, N > const &b, T rtol=static_cast< T >(1E-5), T atol=static_cast< T >(1E-8)) noexcept(std::is_nothrow_copy_constructible< T >::value &&std::is_nothrow_copy_assignable< T >::value)
Floating-point comparison with tolerance.
Definition: CoordinateBase.cc:30
lsst::geom::IS_NOTHROW_CONVERTIBLE
constexpr bool IS_NOTHROW_CONVERTIBLE
Test that a type is nothrow-copy-convertible from U to T.
Definition: CoordinateBase.h:45
lsst::geom::CoordinateBase< Derived, T, 3 >::getY
T const & getY() const noexcept
Definition: CoordinateBase.h:233
lsst::geom::CoordinateBase< Derived, T, 2 >::operator[]
T const & operator[](int n) const
Definition: CoordinateBase.h:153
lsst::geom::CoordinateBase::coeffRef
T const & coeffRef(int n) const
Definition: CoordinateBase.h:81
lsst::geom::CoordinateBase< Derived, T, 2 >::operator=
CoordinateBase & operator=(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: CoordinateBase.h:146
lsst::geom::CoordinateBase< Derived, T, 2 >::getX
T const & getX() const noexcept
Definition: CoordinateBase.h:165
lsst::geom::CoordinateBase< Derived, T, 2 >::asTuple
std::tuple< T, T > asTuple() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a std::tuple representation of the coordinate object.
Definition: CoordinateBase.h:178
lsst::geom::CoordinateBase::CoordinateBase
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
Initialize all elements from an N-d Eigen vector.
Definition: CoordinateBase.h:106
lsst::geom::CoordinateBase< Derived, T, 2 >::EigenVector
Eigen::Matrix< T, 2, 1, Eigen::DontAlign > EigenVector
Definition: CoordinateBase.h:133
lsst::geom::CoordinateBase< Derived, T, 3 >::CoordinateBase
CoordinateBase(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:207
lsst::geom::CoordinateBase< Derived, T, 3 >::~CoordinateBase
~CoordinateBase() noexcept=default
std::ostream
STL class.
z
double z
Definition: Match.cc:44
lsst::geom::CoordinateBase< Derived, T, 3 >::operator[]
T const & operator[](int n) const
Definition: CoordinateBase.h:220
x
double x
Definition: ChebyshevBoundedField.cc:277
lsst::geom::CoordinateBase::CoordinateBase
CoordinateBase(T val=static_cast< T >(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Initialize all elements to a scalar.
Definition: CoordinateBase.h:97
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::geom::CoordinateBase::operator=
CoordinateBase & operator=(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: CoordinateBase.h:72
lsst::geom::CoordinateBase::_swap
void _swap(CoordinateBase &other) noexcept
Definition: CoordinateBase.h:108
lsst::geom::CoordinateBase< Derived, T, 3 >::getZ
T const & getZ() const noexcept
Definition: CoordinateBase.h:234
lsst::geom::CoordinateBase< Derived, T, 2 >::~CoordinateBase
~CoordinateBase() noexcept=default
dimensions
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
lsst::geom::CoordinateBase< Derived, T, 3 >::getY
T & getY() noexcept
Definition: CoordinateBase.h:236
lsst::geom::CoordinateBase::_vector
EigenVector _vector
Definition: CoordinateBase.h:109
lsst::geom::CoordinateBase< Derived, T, 2 >::CoordinateBase
CoordinateBase(T val=static_cast< T >(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:183
b
table::Key< int > b
Definition: TransmissionCurve.cc:467
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::geom::operator<<
std::ostream & operator<<(std::ostream &os, lsst::geom::AffineTransform const &transform)
Definition: AffineTransform.cc:72
lsst::geom::CoordinateBase::dimensions
static int const dimensions
Definition: CoordinateBase.h:58
lsst::geom::CoordinateBase::CoordinateBase
CoordinateBase(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:64
lsst::geom::CoordinateBase::~CoordinateBase
~CoordinateBase() noexcept=default
std::is_nothrow_copy_constructible
lsst::geom::CoordinateBase::EigenVector
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
Definition: CoordinateBase.h:59
lsst::geom::CoordinateBase< Derived, T, 3 >::operator=
CoordinateBase & operator=(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
Definition: CoordinateBase.h:209
lsst::geom::CoordinateBase< Derived, T, 3 >::coeffRef
T & coeffRef(int n)
Definition: CoordinateBase.h:221
lsst::geom
Definition: geomOperators.dox:4
os
std::ostream * os
Definition: Schema.cc:746
lsst::geom::CoordinateBase< Derived, T, 2 >::coeffRef
T const & coeffRef(int n) const
Definition: CoordinateBase.h:155
lsst::geom::CoordinateBase
A CRTP base class for coordinate objects.
Definition: CoordinateBase.h:54
lsst::geom::CoordinateBase< Derived, T, 2 >::coeffRef
T & coeffRef(int n)
Definition: CoordinateBase.h:154
lsst::geom::CoordinateBase::CoordinateBase
CoordinateBase(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:66
lsst::geom::CoordinateBase< Derived, T, 3 >::getX
T & getX() noexcept
Definition: CoordinateBase.h:235
lsst::geom::CoordinateBase< Derived, T, 2 >::CoordinateBase
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
Definition: CoordinateBase.h:187
lsst::geom::CoordinateBase::coeffRef
T & coeffRef(int n)
Definition: CoordinateBase.h:80
a
table::Key< int > a
Definition: TransmissionCurve.cc:466
lsst::geom::CoordinateBase< Derived, T, 3 >::CoordinateBase
CoordinateBase(T val=static_cast< T >(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:248
std
STL namespace.
lsst::geom::Point
A coordinate class intended to represent absolute positions.
Definition: CoordinateBase.h:39
lsst::geom::CoordinateBase< Derived, T, 2 >::asEigen
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
Definition: CoordinateBase.h:163
lsst::geom::CoordinateBase< Derived, T, 3 >::Element
T Element
Definition: CoordinateBase.h:198
std::make_pair
T make_pair(T... args)
std::is_nothrow_copy_assignable
lsst::geom::CoordinateBase::IS_ELEMENT_NOTHROW_COPYABLE
static constexpr bool IS_ELEMENT_NOTHROW_COPYABLE
Definition: CoordinateBase.h:60
lsst::geom::CoordinateBase::Element
T Element
Definition: CoordinateBase.h:56
lsst::geom::CoordinateBase< Derived, T, 3 >::getZ
T & getZ() noexcept
Definition: CoordinateBase.h:237
lsst::geom::CoordinateBase::asEigen
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
Definition: CoordinateBase.h:89
lsst::geom::CoordinateBase< Derived, T, 2 >::setX
void setX(T x) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:169
lsst::geom::CoordinateBase< Derived, T, 3 >::_swap
void _swap(CoordinateBase &other) noexcept
Definition: CoordinateBase.h:253
lsst::geom::CoordinateBase< Derived, T, 3 >::CoordinateBase
CoordinateBase(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:205
lsst::geom::CoordinateBase< Derived, T, 2 >::CoordinateBase
CoordinateBase(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:140
lsst::geom::Extent
A coordinate class intended to represent offsets and dimensions.
Definition: CoordinateBase.h:41
lsst::geom::CoordinateBase< Derived, T, 3 >::setZ
void setZ(T z) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Definition: CoordinateBase.h:240
lsst::geom::CoordinateBase< Derived, T, 2 >::asPair
std::pair< T, T > asPair() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a std::pair representation of the coordinate object.
Definition: CoordinateBase.h:173
lsst::geom::CoordinateBase::IS_ELEMENT_NOTHROW_ASSIGNABLE
static constexpr bool IS_ELEMENT_NOTHROW_ASSIGNABLE
Definition: CoordinateBase.h:61
lsst::geom::CoordinateBase< Derived, T, 3 >::_vector
EigenVector _vector
Definition: CoordinateBase.h:254