lsst.afw  14.0-63-g921e36d6a
CoordinateBase.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 CRTP base class for coordinate objects, providing partial specializations for 2D and 3D.
27  */
28 #ifndef LSST_AFW_GEOM_COORDINATEBASE_H
29 #define LSST_AFW_GEOM_COORDINATEBASE_H
30 
31 #include <iostream>
32 #include <tuple>
33 #include <utility>
34 
35 #include "Eigen/Core"
36 
37 namespace lsst {
38 namespace afw {
39 namespace geom {
40 
41 template <typename T, int N = 2>
42 class Point;
43 template <typename T, int N = 2>
44 class Extent;
45 
51 template <typename Derived, typename T, int N>
53 public:
54  typedef T Element;
55  static int const dimensions = N;
56  typedef Eigen::Matrix<T, N, 1, Eigen::DontAlign> EigenVector;
57 
58  CoordinateBase(CoordinateBase const&) = default;
59  CoordinateBase(CoordinateBase&&) = default;
60  CoordinateBase& operator=(CoordinateBase const&) = default;
62  ~CoordinateBase() = default;
63 
64  T& operator[](int n) { return _vector[n]; }
65  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
66  T& coeffRef(int n) { return _vector.coeffRef(n); }
67  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
68 
75  EigenVector const& asEigen() const { return _vector; }
76 
77 protected:
83  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
84 
90  template <typename Vector>
91  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
92 
93  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
94  EigenVector _vector;
95 };
96 
104 template <typename Derived, typename T, int N>
106  T rtol = static_cast<T>(1E-5), T atol = static_cast<T>(1E-8));
107 
111 template <typename Derived, typename T>
112 class CoordinateBase<Derived, T, 2> {
113 public:
114  typedef T Element;
115  static int const dimensions = 2;
116  typedef Eigen::Matrix<T, 2, 1, Eigen::DontAlign> EigenVector;
117 
118  CoordinateBase(CoordinateBase const&) = default;
119  CoordinateBase(CoordinateBase&&) = default;
120  CoordinateBase& operator=(CoordinateBase const&) = default;
122  ~CoordinateBase() = default;
123 
124  T& operator[](int n) { return _vector[n]; }
125  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
126  T& coeffRef(int n) { return _vector.coeffRef(n); }
127  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
128 
135  EigenVector const& asEigen() const { return _vector; }
136 
137  T const& getX() const { return _vector.x(); }
138  T const& getY() const { return _vector.y(); }
139  T& getX() { return _vector.x(); }
140  T& getY() { return _vector.y(); }
141  void setX(T x) { _vector.x() = x; }
142  void setY(T y) { _vector.y() = y; }
143 
145  std::pair<T, T> asPair() const { return std::make_pair(_vector.x(), _vector.y()); }
146 
149 
150 protected:
151  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
152 
153  template <typename Vector>
154  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
155  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
156  EigenVector _vector;
157 };
158 
162 template <typename Derived, typename T>
163 class CoordinateBase<Derived, T, 3> {
164 public:
165  typedef T Element;
166  static int const dimensions = 3;
167  typedef Eigen::Matrix<T, 3, 1, Eigen::DontAlign> EigenVector;
168 
169  CoordinateBase(CoordinateBase const&) = default;
170  CoordinateBase(CoordinateBase&&) = default;
171  CoordinateBase& operator=(CoordinateBase const&) = default;
173  ~CoordinateBase() = default;
174 
175  T& operator[](int n) { return _vector[n]; }
176  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
177  T& coeffRef(int n) { return _vector.coeffRef(n); }
178  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
179 
186  EigenVector const& asEigen() const { return _vector; }
187 
188  T const& getX() const { return _vector.x(); }
189  T const& getY() const { return _vector.y(); }
190  T const& getZ() const { return _vector.z(); }
191  T& getX() { return _vector.x(); }
192  T& getY() { return _vector.y(); }
193  T& getZ() { return _vector.z(); }
194  void setX(T x) { _vector.x() = x; }
195  void setY(T y) { _vector.y() = y; }
196  void setZ(T z) { _vector.z() = z; }
197 
200 
201 protected:
202  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
203 
204  template <typename Vector>
205  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
206  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
207  EigenVector _vector;
208 };
209 
210 template <typename Derived, typename T, int N>
211 std::ostream& operator<<(std::ostream& os, CoordinateBase<Derived, T, N> const& coordinate) {
212  os << "(" << coordinate[0];
213  for (int n = 1; n < N; ++n) os << ", " << coordinate[n];
214  return os << ")";
215 }
216 }
217 }
218 }
219 
220 #endif
CoordinateBase(CoordinateBase const &)=default
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
Initialize all elements from an N-d Eigen vector.
Eigen::Matrix< T, 2, 1, Eigen::DontAlign > EigenVector
A coordinate class intended to represent offsets and dimensions.
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
T make_tuple(T... args)
CoordinateBase & operator=(CoordinateBase const &)=default
A CRTP base class for coordinate objects.
A coordinate class intended to represent absolute positions.
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
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.
A base class for image defects.
std::pair< T, T > asPair() const
Return a std::pair representation of the coordinate object.
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.
table::Key< int > b
T make_pair(T... args)
double x
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
void _swap(CoordinateBase &other)
Eigen::Matrix< T, 3, 1, Eigen::DontAlign > EigenVector
T const & coeffRef(int n) const
std::tuple< T, T > asTuple() const
Return a std::tuple representation of the coordinate object.
T const & operator[](int n) const
table::Key< int > a
STL class.
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
std::tuple< T, T, T > asTuple() const
Return a std::tuple representation of the coordinate object.
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.