LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
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 
29 #ifndef LSST_AFW_GEOM_COORDINATEBASE_H
30 #define LSST_AFW_GEOM_COORDINATEBASE_H
31 
32 #include <utility>
33 
34 #include "Eigen/Core"
35 #include <iostream>
36 #include "boost/tuple/tuple.hpp"
37 
38 namespace lsst { namespace afw { namespace geom {
39 
40 template <typename T, int N=2> class Point;
41 template <typename T, int N=2> class Extent;
42 
48 template <typename Derived, typename T, int N>
50 public:
51  typedef T Element;
52  static int const dimensions = N;
53  typedef Eigen::Matrix<T,N,1,Eigen::DontAlign> EigenVector;
54 
55 #ifndef SWIG
56  T & operator[](int n) { return _vector[n]; }
57  T const & operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
58  T & coeffRef(int n) { return _vector.coeffRef(n); }
59  T const & coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
60 
67  EigenVector const & asEigen() const { return _vector; }
68 #endif
69 
70 
71 protected:
72 
78  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
79 
85  template <typename Vector>
86  explicit CoordinateBase(Eigen::MatrixBase<Vector> const & vector) : _vector(vector) {}
87 
88  void _swap(CoordinateBase & other) {_vector.swap(other._vector);}
90 };
91 
92 template <typename Derived, typename T, int N>
93 bool allclose(
95  T rtol = static_cast<T>(1E-5),
96  T atol = static_cast<T>(1E-8)
97 );
98 
102 template <typename Derived, typename T>
103 class CoordinateBase<Derived,T,2> {
104 public:
105  typedef T Element;
106  static int const dimensions = 2;
107  typedef Eigen::Matrix<T,2,1,Eigen::DontAlign> EigenVector;
108 
109 #ifndef SWIG
110  T & operator[](int n) { return _vector[n]; }
111  T const & operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
112  T & coeffRef(int n) { return _vector.coeffRef(n); }
113  T const & coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
114 
121  EigenVector const & asEigen() const { return _vector; }
122 #endif
123 
124  T const & getX() const { return _vector.x(); }
125  T const & getY() const { return _vector.y(); }
126  T & getX() { return _vector.x(); }
127  T & getY() { return _vector.y(); }
128  void setX(T x) { _vector.x() = x; }
129  void setY(T y) { _vector.y() = y; }
130 
132  std::pair<T,T> asPair() const { return std::make_pair(_vector.x(),_vector.y()); }
133 
135  boost::tuple<T,T> asTuple() const { return boost::make_tuple(_vector.x(),_vector.y()); }
136 
137 protected:
138 
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);}
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 #ifndef SWIG
158  T & operator[](int n) { return _vector[n]; }
159  T const & operator[](int n) const { return const_cast<EigenVector&>(_vector)[n]; }
160  T & coeffRef(int n) { return _vector.coeffRef(n); }
161  T const & coeffRef(int n) const { return const_cast<EigenVector&>(_vector).coeffRef(n); }
162 
169  EigenVector const & asEigen() const { return _vector; }
170 
171 #endif
172 
173  T const & getX() const { return _vector.x(); }
174  T const & getY() const { return _vector.y(); }
175  T const & getZ() const { return _vector.z(); }
176  T & getX() { return _vector.x(); }
177  T & getY() { return _vector.y(); }
178  T & getZ() { return _vector.z(); }
179  void setX(T x) { _vector.x() = x; }
180  void setY(T y) { _vector.y() = y; }
181  void setZ(T z) { _vector.z() = z; }
182 
184  boost::tuple<T,T,T> asTuple() const {
185  return boost::make_tuple(_vector.x(), _vector.y(), _vector.z());
186  }
187 
188 protected:
189 
190  explicit CoordinateBase(T val = static_cast<T>(0)) : _vector(EigenVector::Constant(val)) {}
191 
192  template <typename Vector>
193  explicit CoordinateBase(Eigen::MatrixBase<Vector> const & vector) : _vector(vector) {}
194  void _swap(CoordinateBase & other) {_vector.swap(other._vector);}
196 };
197 
198 template <typename Derived, typename T, int N>
199 std::ostream & operator<<(std::ostream & os, CoordinateBase<Derived,T,N> const & coordinate) {
200  os << "(" << coordinate[0];
201  for (int n=1; n<N; ++n) os << ", " << coordinate[n];
202  return os << ")";
203 }
204 
205 }}}
206 
207 #endif
int y
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
Initialize all elements from an N-d Eigen vector.
Eigen::Matrix< T, 2, 1, Eigen::DontAlign > EigenVector
Eigen::Matrix< T, 3, 1, Eigen::DontAlign > EigenVector
A coordinate class intended to represent offsets and dimensions.
std::pair< T, T > asPair() const
Return a std::pair representation of the coordinate object.
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
boost::tuple< T, T > asTuple() const
Return a boost::tuple representation of the coordinate object.
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
A CRTP base class for coordinate objects.
A coordinate class intended to represent absolute positions.
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
bool val
T const & coeffRef(int n) const
CoordinateBase(T val=static_cast< T >(0))
Initialize all elements to a scalar.
boost::tuple< T, T, T > asTuple() const
Return a boost::tuple 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))
double x
void _swap(CoordinateBase &other)
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
afw::table::Key< double > b
T const & operator[](int n) const
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)