ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
eigen_transform3.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s).
12#include "algebra/impl/detail/eigen_array.hpp"
13#include "detray/algebra/utils/approximately_equal.hpp"
14#include "detray/definitions/detail/qualifiers.hpp"
15
16// Eigen include(s).
17#ifdef _MSC_VER
18#pragma warning(push, 0)
19#endif // MSVC
20#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
21#pragma nv_diagnostic push
22#pragma nv_diag_suppress 20011
23#pragma nv_diag_suppress 20012
24#pragma nv_diag_suppress 20014
25#endif // __NVCC_DIAG_PRAGMA_SUPPORT__
26#include <Eigen/Core>
27#include <Eigen/Geometry>
28#ifdef _MSC_VER
29#pragma warning(pop)
30#endif // MSVC
31#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
32#pragma nv_diagnostic pop
33#endif // __NVCC_DIAG_PRAGMA_SUPPORT__
34
35// System include(s).
36#include <cassert>
37#include <concepts>
38#include <limits>
39#include <type_traits>
40
41namespace detray::algebra::eigen::math {
42
44template <concepts::scalar scalar_t>
45struct transform3 {
48
50 using scalar_type = scalar_t;
51
53 template <int N>
54 using array_type = eigen::array<scalar_type, N>;
55
61 using point2 = vector2;
63 using point3 = vector3;
64
66 using matrix44 =
67 typename Eigen::Transform<scalar_type, 3, Eigen::Affine>::MatrixType;
68
70 template <concepts::scalar o_scalar_t>
72
74
77
79 Eigen::Transform<scalar_type, 3, Eigen::Affine> _data;
81 Eigen::Transform<scalar_type, 3, Eigen::Affine> _data_inv;
82
84
92 DETRAY_HOST_DEVICE
93 transform3(const vector3 &t, const vector3 &x, const vector3 &y,
94 const vector3 &z, bool get_inverse = true) {
95 _data.setIdentity();
96
97 auto &matrix = _data.matrix();
98 matrix.template block<3, 1>(0, 0) = x;
99 matrix.template block<3, 1>(0, 1) = y;
100 matrix.template block<3, 1>(0, 2) = z;
101 matrix.template block<3, 1>(0, 3) = t;
102
103 if (get_inverse) {
104 _data_inv = _data.inverse();
105 } else {
106 _data_inv.setIdentity();
107 }
108 }
109
116 DETRAY_HOST_DEVICE
117 transform3(const vector3 &t, const vector3 &z, const vector3 &x,
118 bool get_inverse = true)
119 : transform3(t, x, z.cross(x), z, get_inverse) {}
120
124 DETRAY_HOST_DEVICE
125 explicit transform3(const vector3 &t) {
126 _data.setIdentity();
127
128 auto &matrix = _data.matrix();
129 matrix.template block<3, 1>(0, 3) = t;
130
131 _data_inv = _data.inverse();
132 }
133
137 DETRAY_HOST_DEVICE
138 explicit transform3(const matrix44 &m) {
139 _data.matrix() = m;
140
141 _data_inv = _data.inverse();
142 }
143
148 DETRAY_HOST_DEVICE
149 transform3(const matrix44 &m, const matrix44 &m_inv)
150 : _data{m}, _data_inv{m_inv} {
151 // The assertion will not hold for (casts to) int
152 if constexpr (std::floating_point<scalar_type>) {
153 [[maybe_unused]] constexpr auto epsilon{
154 std::numeric_limits<scalar_type>::epsilon()};
155 assert(algebra::approx_equal(matrix44(m * m_inv),
156 matrix44(matrix44::Identity()),
157 16.f * epsilon, 1e-6f));
158 }
159 }
160
164 DETRAY_HOST_DEVICE
165 explicit transform3(const array_type<16> &ma) {
166 _data.matrix() << ma[0], ma[1], ma[2], ma[3], ma[4], ma[5], ma[6], ma[7],
167 ma[8], ma[9], ma[10], ma[11], ma[12], ma[13], ma[14], ma[15];
168
169 _data_inv = _data.inverse();
170 }
171
173 DETRAY_HOST_DEVICE
175 _data.setIdentity();
176 _data_inv.setIdentity();
177 }
178
181 transform3(const transform3 &rhs) = default;
182 ~transform3() = default;
183
187 DETRAY_HOST_DEVICE
188 constexpr bool operator==(const transform3 &rhs) const {
189 return (_data.isApprox(rhs._data));
190 }
191
197 template <typename derived_type>
198 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 2 &&
199 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
200 DETRAY_HOST_DEVICE static constexpr auto rotate(
201 const Eigen::Transform<scalar_type, 3, Eigen::Affine> &m,
202 const Eigen::MatrixBase<derived_type> &v) {
203 return m.matrix().template block<3, 2>(0, 0) * v;
204 }
205
211 template <typename derived_type>
212 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 3 &&
213 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
214 DETRAY_HOST_DEVICE static constexpr auto rotate(
215 const Eigen::Transform<scalar_type, 3, Eigen::Affine> &m,
216 const Eigen::MatrixBase<derived_type> &v) {
217 return m.matrix().template block<3, 3>(0, 0) * v;
218 }
219
222 DETRAY_HOST_DEVICE
223 constexpr auto rotation() const {
224 return _data.matrix().template block<3, 3>(0, 0);
225 }
226
229 DETRAY_HOST_DEVICE
230 constexpr point3 x() const {
231 return _data.matrix().template block<3, 1>(0, 0);
232 }
233
236 DETRAY_HOST_DEVICE
237 constexpr point3 y() const {
238 return _data.matrix().template block<3, 1>(0, 1);
239 }
240
243 DETRAY_HOST_DEVICE
244 constexpr point3 z() const {
245 return _data.matrix().template block<3, 1>(0, 2);
246 }
247
250 DETRAY_HOST_DEVICE
251 constexpr point3 translation() const {
252 return _data.matrix().template block<3, 1>(0, 3);
253 }
254
257 DETRAY_HOST_DEVICE
258 constexpr const matrix44 &matrix() const { return _data.matrix(); }
259
262 DETRAY_HOST_DEVICE
263 constexpr const matrix44 &matrix_inverse() const {
264 return _data_inv.matrix();
265 }
266
271 template <typename derived_type>
272 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 2 &&
273 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
274 DETRAY_HOST_DEVICE constexpr auto point_to_global(
275 const Eigen::MatrixBase<derived_type> &v) const {
276 return (_data.matrix().template block<3, 2>(0, 0) * v +
277 _data.matrix().template block<3, 1>(0, 3));
278 }
279
284 template <typename derived_type>
285 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 3 &&
286 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
287 DETRAY_HOST_DEVICE constexpr auto point_to_global(
288 const Eigen::MatrixBase<derived_type> &v) const {
289 return (_data * v);
290 }
291
296 template <typename derived_type>
297 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 3 &&
298 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
299 DETRAY_HOST_DEVICE constexpr auto point_to_local(
300 const Eigen::MatrixBase<derived_type> &v) const {
301 return (_data_inv * v);
302 }
303
308 template <typename derived_type>
309 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 2 &&
310 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
311 DETRAY_HOST_DEVICE constexpr auto vector_to_global(
312 const Eigen::MatrixBase<derived_type> &v) const {
313 return (_data.linear().matrix().template block<3, 2>(0, 0) * v);
314 }
315
320 template <typename derived_type>
321 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 3 &&
322 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
323 DETRAY_HOST_DEVICE constexpr auto vector_to_global(
324 const Eigen::MatrixBase<derived_type> &v) const {
325 return (_data.linear() * v);
326 }
327
332 template <typename derived_type>
333 requires(Eigen::MatrixBase<derived_type>::RowsAtCompileTime == 3 &&
334 Eigen::MatrixBase<derived_type>::ColsAtCompileTime == 1)
335 DETRAY_HOST_DEVICE constexpr auto vector_to_local(
336 const Eigen::MatrixBase<derived_type> &v) const {
337 return (_data_inv.linear() * v);
338 }
339}; // struct transform3
340
341} // namespace detray::algebra::eigen::math
scalar_t scalar_type
Scalar type used by the transform.
Definition eigen_transform3.hpp:50
constexpr auto vector_to_global(const Eigen::MatrixBase< derived_type > &v) const
This method transform from a vector from the local 3D cartesian frame to the global 3D cartesian fram...
Definition eigen_transform3.hpp:323
vector3 point3
Three-dimensional point representation.
Definition eigen_transform3.hpp:63
vector2 point2
Two-dimensional point representation.
Definition eigen_transform3.hpp:61
constexpr const matrix44 & matrix_inverse() const
This method retrieves the 4x4 matrix of an inverse transform.
Definition eigen_transform3.hpp:263
constexpr point3 z() const
Definition eigen_transform3.hpp:244
transform3(const transform3 &rhs)=default
Copy an existing transform.
constexpr auto vector_to_local(const Eigen::MatrixBase< derived_type > &v) const
This method transform from a vector from the global 3D cartesian frame into the local 3D cartesian fr...
Definition eigen_transform3.hpp:335
transform3()
Default constructor: set contents to identity matrices.
Definition eigen_transform3.hpp:174
transform3< o_scalar_t > other_type
Helper type to cast this to another floating point precision.
Definition eigen_transform3.hpp:71
transform3(const matrix44 &m, const matrix44 &m_inv)
Constructor with arguments: matrix and its inverse.
Definition eigen_transform3.hpp:149
constexpr const matrix44 & matrix() const
Definition eigen_transform3.hpp:258
constexpr point3 x() const
Definition eigen_transform3.hpp:230
Eigen::Transform< scalar_type, 3, Eigen::Affine > _data_inv
Definition eigen_transform3.hpp:81
array_type< 3 > vector3
Three-component vector type used for spatial quantities.
Definition eigen_transform3.hpp:59
constexpr auto rotation() const
This method retrieves the rotation of a transform.
Definition eigen_transform3.hpp:223
static constexpr auto rotate(const Eigen::Transform< scalar_type, 3, Eigen::Affine > &m, const Eigen::MatrixBase< derived_type > &v)
Rotate a vector into / from a frame.
Definition eigen_transform3.hpp:200
transform3(const vector3 &t, const vector3 &x, const vector3 &y, const vector3 &z, bool get_inverse=true)
Constructor with arguments: t, x, y, z.
Definition eigen_transform3.hpp:93
constexpr auto vector_to_global(const Eigen::MatrixBase< derived_type > &v) const
This method transform from a vector from the local 3D cartesian frame to the global 3D cartesian fram...
Definition eigen_transform3.hpp:311
transform3(const vector3 &t, const vector3 &z, const vector3 &x, bool get_inverse=true)
Constructor with arguments: t, z, x.
Definition eigen_transform3.hpp:117
transform3(const array_type< 16 > &ma)
Constructor with arguments: matrix as std::array of scalar.
Definition eigen_transform3.hpp:165
static constexpr auto rotate(const Eigen::Transform< scalar_type, 3, Eigen::Affine > &m, const Eigen::MatrixBase< derived_type > &v)
Rotate a vector into / from a frame.
Definition eigen_transform3.hpp:214
transform3(const matrix44 &m)
Constructor with arguments: matrix.
Definition eigen_transform3.hpp:138
eigen::array< scalar_type, N > array_type
Array type used by the transform.
Definition eigen_transform3.hpp:54
constexpr auto point_to_global(const Eigen::MatrixBase< derived_type > &v) const
This method transform from a point from the local 2D cartesian frame to the global 3D cartesian frame...
Definition eigen_transform3.hpp:274
constexpr point3 y() const
Definition eigen_transform3.hpp:237
constexpr point3 translation() const
This method retrieves the translation of a transform.
Definition eigen_transform3.hpp:251
constexpr auto point_to_global(const Eigen::MatrixBase< derived_type > &v) const
This method transform from a point from the local 3D cartesian frame to the global 3D cartesian frame...
Definition eigen_transform3.hpp:287
typename Eigen::Transform< scalar_type, 3, Eigen::Affine >::MatrixType matrix44
4x4 matrix type
Definition eigen_transform3.hpp:66
Eigen::Transform< scalar_type, 3, Eigen::Affine > _data
Definition eigen_transform3.hpp:79
constexpr auto point_to_local(const Eigen::MatrixBase< derived_type > &v) const
This method transform from a vector from the global 3D cartesian frame into the local 3D cartesian fr...
Definition eigen_transform3.hpp:299
transform3(const vector3 &t)
Constructor with arguments: translation.
Definition eigen_transform3.hpp:125
array_type< 2 > vector2
Vector and point types.
Definition eigen_transform3.hpp:57
constexpr bool operator==(const transform3 &rhs) const
Equality operator.
Definition eigen_transform3.hpp:188