LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
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
35namespace lsst {
36namespace geom {
37
38template <typename T, int N = 2>
39class Point;
40template <typename T, int N = 2>
41class Extent;
42
44template <typename T, typename U>
45bool constexpr IS_NOTHROW_CONVERTIBLE =
46 std::is_nothrow_copy_constructible<T>::value&& noexcept(static_cast<T>(std::declval<U>()));
47
53template <typename Derived, typename T, int N>
55public:
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
91protected:
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
119template <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
128template <typename Derived, typename T>
129class CoordinateBase<Derived, T, 2> {
130public:
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
182protected:
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
195template <typename Derived, typename T>
196class CoordinateBase<Derived, T, 3> {
197public:
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
247protected:
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
257template <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
double x
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:48
double z
Definition: Match.cc:44
std::ostream * os
Definition: Schema.cc:557
int y
Definition: SpanSet.cc:48
table::Key< int > b
table::Key< int > a
void _swap(CoordinateBase &other) noexcept
CoordinateBase & operator=(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
Eigen::Matrix< T, 2, 1, Eigen::DontAlign > EigenVector
CoordinateBase(T val=static_cast< T >(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
std::pair< T, T > asPair() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a std::pair representation of the coordinate object.
CoordinateBase(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
void setX(T x) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
void setY(T y) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
CoordinateBase & operator=(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
std::tuple< T, T > asTuple() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a std::tuple representation of the coordinate object.
CoordinateBase(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
void setX(T x) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
void setY(T y) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Eigen::Matrix< T, 3, 1, Eigen::DontAlign > EigenVector
void setZ(T z) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
CoordinateBase(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
void _swap(CoordinateBase &other) noexcept
std::tuple< T, T, T > asTuple() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a std::tuple representation of the coordinate object.
CoordinateBase(T val=static_cast< T >(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
CoordinateBase & operator=(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
CoordinateBase & operator=(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
CoordinateBase(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
A CRTP base class for coordinate objects.
CoordinateBase & operator=(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
void _swap(CoordinateBase &other) noexcept
CoordinateBase(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Eigen::Matrix< T, N, 1, Eigen::DontAlign > EigenVector
EigenVector const & asEigen() const noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Return a fixed-size Eigen representation of the coordinate object.
T const & coeffRef(int n) const
~CoordinateBase() noexcept=default
CoordinateBase(T val=static_cast< T >(0)) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
Initialize all elements to a scalar.
CoordinateBase(CoordinateBase const &other) noexcept(IS_ELEMENT_NOTHROW_COPYABLE)
T const & operator[](int n) const
static constexpr bool IS_ELEMENT_NOTHROW_COPYABLE
static int const dimensions
CoordinateBase & operator=(CoordinateBase &&other) noexcept(IS_ELEMENT_NOTHROW_ASSIGNABLE)
CoordinateBase(Eigen::MatrixBase< Vector > const &vector)
Initialize all elements from an N-d Eigen vector.
static constexpr bool IS_ELEMENT_NOTHROW_ASSIGNABLE
T make_pair(T... args)
T make_tuple(T... args)
T move(T... args)
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.
constexpr bool IS_NOTHROW_CONVERTIBLE
Test that a type is nothrow-copy-convertible from U to T.
std::ostream & operator<<(std::ostream &os, lsst::geom::AffineTransform const &transform)
A base class for image defects.
STL namespace.
ImageT val
Definition: CR.cc:146