lsst.afw  14.0-25-g0e8fc950e+6
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  T& operator[](int n) { return _vector[n]; }
59  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
60  T& coeffRef(int n) { return _vector.coeffRef(n); }
61  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
62 
69  EigenVector const& asEigen() const { return _vector; }
70 
71 protected:
77  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
78 
84  template <typename Vector>
85  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
86 
87  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
88  EigenVector _vector;
89 };
90 
98 template <typename Derived, typename T, int N>
100  T rtol = static_cast<T>(1E-5), T atol = static_cast<T>(1E-8));
101 
105 template <typename Derived, typename T>
106 class CoordinateBase<Derived, T, 2> {
107 public:
108  typedef T Element;
109  static int const dimensions = 2;
110  typedef Eigen::Matrix<T, 2, 1, Eigen::DontAlign> EigenVector;
111 
112  T& operator[](int n) { return _vector[n]; }
113  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
114  T& coeffRef(int n) { return _vector.coeffRef(n); }
115  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
116 
123  EigenVector const& asEigen() const { return _vector; }
124 
125  T const& getX() const { return _vector.x(); }
126  T const& getY() const { return _vector.y(); }
127  T& getX() { return _vector.x(); }
128  T& getY() { return _vector.y(); }
129  void setX(T x) { _vector.x() = x; }
130  void setY(T y) { _vector.y() = y; }
131 
133  std::pair<T, T> asPair() const { return std::make_pair(_vector.x(), _vector.y()); }
134 
137 
138 protected:
139  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
140 
141  template <typename Vector>
142  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
143  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
144  EigenVector _vector;
145 };
146 
150 template <typename Derived, typename T>
151 class CoordinateBase<Derived, T, 3> {
152 public:
153  typedef T Element;
154  static int const dimensions = 3;
155  typedef Eigen::Matrix<T, 3, 1, Eigen::DontAlign> EigenVector;
156 
157  T& operator[](int n) { return _vector[n]; }
158  T const& operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
159  T& coeffRef(int n) { return _vector.coeffRef(n); }
160  T const& coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
161 
168  EigenVector const& asEigen() const { return _vector; }
169 
170  T const& getX() const { return _vector.x(); }
171  T const& getY() const { return _vector.y(); }
172  T const& getZ() const { return _vector.z(); }
173  T& getX() { return _vector.x(); }
174  T& getY() { return _vector.y(); }
175  T& getZ() { return _vector.z(); }
176  void setX(T x) { _vector.x() = x; }
177  void setY(T y) { _vector.y() = y; }
178  void setZ(T z) { _vector.z() = z; }
179 
182 
183 protected:
184  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
185 
186  template <typename Vector>
187  explicit CoordinateBase(Eigen::MatrixBase<Vector> const& vector) : _vector(vector) {}
188  void _swap(CoordinateBase& other) { _vector.swap(other._vector); }
189  EigenVector _vector;
190 };
191 
192 template <typename Derived, typename T, int N>
193 std::ostream& operator<<(std::ostream& os, CoordinateBase<Derived, T, N> const& coordinate) {
194  os << "(" << coordinate[0];
195  for (int n = 1; n < N; ++n) os << ", " << coordinate[n];
196  return os << ")";
197 }
198 }
199 }
200 }
201 
202 #endif
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)
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.
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
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.