lsst.geom  15.0-1-g417ea41+6
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 <tuple>
30 #include <utility>
31 
32 #include "Eigen/Core"
33 
34 namespace lsst {
35 namespace geom {
36 
37 template <typename T, int N = 2>
38 class Point;
39 template <typename T, int N = 2>
40 class Extent;
41 
47 template <typename Derived, typename T, int N>
49 public:
50  typedef T Element;
51  static int const dimensions = N;
52  typedef Eigen::Matrix<T, N, 1, Eigen::DontAlign> EigenVector;
53 
54  CoordinateBase(CoordinateBase const&) = default;
55  CoordinateBase(CoordinateBase&&) = default;
56  CoordinateBase& operator=(CoordinateBase const&) = default;
58  ~CoordinateBase() = default;
59 
60  T& operator[](int n) { return _vector[n]; }
61  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
62  T& coeffRef(int n) { return _vector.coeffRef(n); }
63  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
64 
71  EigenVector const& asEigen() const { return _vector; }
72 
73 protected:
79  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
80 
86  template <typename Vector>
87  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
88 
89  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
90  EigenVector _vector;
91 };
92 
100 template <typename Derived, typename T, int N>
102  T rtol = static_cast<T>(1E-5), T atol = static_cast<T>(1E-8));
103 
107 template <typename Derived, typename T>
108 class CoordinateBase<Derived, T, 2> {
109 public:
110  typedef T Element;
111  static int const dimensions = 2;
112  typedef Eigen::Matrix<T, 2, 1, Eigen::DontAlign> EigenVector;
113 
114  CoordinateBase(CoordinateBase const&) = default;
115  CoordinateBase(CoordinateBase&&) = default;
116  CoordinateBase& operator=(CoordinateBase const&) = default;
118  ~CoordinateBase() = default;
119 
120  T& operator[](int n) { return _vector[n]; }
121  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
122  T& coeffRef(int n) { return _vector.coeffRef(n); }
123  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
124 
131  EigenVector const& asEigen() const { return _vector; }
132 
133  T const& getX() const { return _vector.x(); }
134  T const& getY() const { return _vector.y(); }
135  T& getX() { return _vector.x(); }
136  T& getY() { return _vector.y(); }
137  void setX(T x) { _vector.x() = x; }
138  void setY(T y) { _vector.y() = y; }
139 
141  std::pair<T, T> asPair() const { return std::make_pair(_vector.x(), _vector.y()); }
142 
145 
146 protected:
147  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
148 
149  template <typename Vector>
150  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
151  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
152  EigenVector _vector;
153 };
154 
158 template <typename Derived, typename T>
159 class CoordinateBase<Derived, T, 3> {
160 public:
161  typedef T Element;
162  static int const dimensions = 3;
163  typedef Eigen::Matrix<T, 3, 1, Eigen::DontAlign> EigenVector;
164 
165  CoordinateBase(CoordinateBase const&) = default;
166  CoordinateBase(CoordinateBase&&) = default;
167  CoordinateBase& operator=(CoordinateBase const&) = default;
169  ~CoordinateBase() = default;
170 
171  T& operator[](int n) { return _vector[n]; }
172  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
173  T& coeffRef(int n) { return _vector.coeffRef(n); }
174  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
175 
182  EigenVector const& asEigen() const { return _vector; }
183 
184  T const& getX() const { return _vector.x(); }
185  T const& getY() const { return _vector.y(); }
186  T const& getZ() const { return _vector.z(); }
187  T& getX() { return _vector.x(); }
188  T& getY() { return _vector.y(); }
189  T& getZ() { return _vector.z(); }
190  void setX(T x) { _vector.x() = x; }
191  void setY(T y) { _vector.y() = y; }
192  void setZ(T z) { _vector.z() = z; }
193 
196 
197 protected:
198  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
199 
200  template <typename Vector>
201  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
202  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
203  EigenVector _vector;
204 };
205 
206 template <typename Derived, typename T, int N>
207 std::ostream& operator<<(std::ostream& os, CoordinateBase<Derived, T, N> const& coordinate) {
208  os << "(" << coordinate[0];
209  for (int n = 1; n < N; ++n) os << ", " << coordinate[n];
210  return os << ")";
211 }
212 
213 } // namespace geom
214 } // namespace lsst
215 
216 #endif
Eigen::Matrix< T, 3, 1, Eigen::DontAlign > EigenVector
A CRTP base class for coordinate objects.
std::tuple< T, T, T > asTuple() const
Return a std::tuple representation of the coordinate object.
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
A coordinate class intended to represent absolute positions.
std::pair< T, T > asPair() const
Return a std::pair representation of the coordinate object.
T const & operator[](int n) const
T make_tuple(T... args)
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
T const & coeffRef(int n) const
Eigen::Matrix< T, 2, 1, Eigen::DontAlign > EigenVector
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
CoordinateBase(CoordinateBase const &)=default
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
void _swap(CoordinateBase &other)
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
Initialize all elements from an N-d Eigen vector.
CoordinateBase(T val=static_cast< T >(0))
T make_pair(T... args)
A coordinate class intended to represent offsets and dimensions.
static int const dimensions
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))
Floating-point comparison with tolerance.
CoordinateBase(T val=static_cast< T >(0))
std::tuple< T, T > asTuple() const
Return a std::tuple representation of the coordinate object.
CoordinateBase(T val=static_cast< T >(0))
Initialize all elements to a scalar.
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
STL class.
CoordinateBase & operator=(CoordinateBase const &)=default