ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
fastor_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/fastor_matrix_wrapper.hpp"
13#include "algebra/impl/detail/fastor_vector_wrapper.hpp"
14#include "algebra/impl/fastor_matrix.hpp"
15#include "detray/algebra/utils/approximately_equal.hpp"
16#include "detray/definitions/detail/qualifiers.hpp"
17
18// Fastor include(s).
19#ifdef _MSC_VER
20#pragma warning(disable : 4244 4701 4702)
21#endif // MSVC
22#include <Fastor/Fastor.h>
23#ifdef _MSC_VER
24#pragma warning(default : 4244 4701 4702)
25#endif // MSVC
26
27// System include(s).
28#include <cassert>
29#include <concepts>
30#include <cstddef>
31#include <limits>
32
33namespace detray::algebra::fastor::math {
34
36template <concepts::scalar scalar_t>
37struct transform3 {
40
42 using scalar_type = scalar_t;
43
45 template <std::size_t N>
46 using array_type = fastor::Vector<scalar_t, N>;
47
53 using point2 = vector2;
55 using point3 = vector3;
56
58 using matrix44 = fastor::Matrix<scalar_type, 4UL, 4UL>;
59
61 template <concepts::scalar o_scalar_t>
63
65
68
71
73
75 DETRAY_HOST_DEVICE
76 constexpr transform3() {
77 _data.eye();
78 _data_inv.eye();
79 }
80
88 DETRAY_HOST_DEVICE
89 transform3(const vector3 &t, const vector3 &x, const vector3 &y,
90 const vector3 &z, bool get_inverse = true) {
91 // The matrix needs to be initialized to the identity matrix first. We
92 // only modify the top 4x3 portion of the matrix, so it doesn't matter
93 // what values it initially had. However, the bottom row is required to
94 // have the values of [0, 0, 0, 1], so that's why we set `_data` to the
95 // identity matrix first.
96 _data.eye2();
97
98 _data(Fastor::fseq<0, 3>(), 0) = x;
99 _data(Fastor::fseq<0, 3>(), 1) = y;
100 _data(Fastor::fseq<0, 3>(), 2) = z;
101 _data(Fastor::fseq<0, 3>(), 3) = t;
102
103 if (get_inverse) {
104 _data_inv = Fastor::inverse(_data);
105 }
106 }
107
114 DETRAY_HOST
115 transform3(const vector3 &t, const vector3 &z, const vector3 &x,
116 bool get_inverse = true)
117 : transform3(t, x, Fastor::cross(z, x), z, get_inverse) {}
118
122 DETRAY_HOST
123 explicit transform3(const vector3 &t) {
124 // The matrix needs to be initialized to the identity matrix first. In
125 // this case, the `transform3` requires `_data` to look just like an
126 // identity matrix except for the third column, which is the one we are
127 // modifying here.
128 _data.eye2();
129
130 _data(Fastor::fseq<0, 3>(), 3) = t;
131
132 _data_inv = Fastor::inverse(_data);
133 }
134
138 DETRAY_HOST
139 explicit transform3(const matrix44 &m) : _data{m} {
140 _data_inv = Fastor::inverse(_data);
141 }
142
147 DETRAY_HOST
148 transform3(const matrix44 &m, const matrix44 &m_inv)
149 : _data{m}, _data_inv{m_inv} {
150 // The assertion will not hold for (casts to) int
151 if constexpr (std::floating_point<scalar_type>) {
152 [[maybe_unused]] constexpr auto epsilon{
153 std::numeric_limits<scalar_type>::epsilon()};
154
155 [[maybe_unused]] matrix44 identity_matrix;
156 identity_matrix.eye2();
157 assert(algebra::approx_equal(m * m_inv, identity_matrix, 16.f * epsilon,
158 1e-6f));
159 }
160 }
161
166 DETRAY_HOST
167 explicit transform3(const array_type<16> &ma) : _data{ma} {
168 _data_inv = Fastor::inverse(_data);
169 }
170
173 transform3(const transform3 &rhs) = default;
174 ~transform3() = default;
175
179 DETRAY_HOST
180 constexpr bool operator==(const transform3 &rhs) const {
181 return Fastor::isequal(_data, rhs._data);
182 }
183
186 DETRAY_HOST
187 constexpr auto rotation() const {
188 return Fastor::Tensor<scalar_t, 3, 3>(
189 _data(Fastor::fseq<0, 3>(), Fastor::fseq<0, 3>()));
190 }
191
194 DETRAY_HOST_DEVICE
195 constexpr point3 x() const { return _data(Fastor::fseq<0, 3>(), 0); }
196
199 DETRAY_HOST_DEVICE
200 constexpr point3 y() const { return _data(Fastor::fseq<0, 3>(), 1); }
201
204 DETRAY_HOST_DEVICE
205 constexpr point3 z() const { return _data(Fastor::fseq<0, 3>(), 2); }
206
209 DETRAY_HOST
210 constexpr vector3 translation() const {
211 return _data(Fastor::fseq<0, 3>(), 3);
212 }
213
216 DETRAY_HOST
217 constexpr matrix44 matrix() const { return _data; }
218
221 DETRAY_HOST
222 constexpr matrix44 matrix_inverse() const { return _data_inv; }
223
228 DETRAY_HOST
229 constexpr point3 point_to_global(const point2 &v) const {
230 Fastor::Tensor<scalar_type, 4> vector_4;
231 vector_4(Fastor::fseq<0, 2>()) = v;
232 vector_4[2] = static_cast<scalar_type>(0);
233 vector_4[3] = static_cast<scalar_type>(1);
234 return Fastor::Tensor<scalar_type, 3>(
235 Fastor::matmul(_data, vector_4)(Fastor::fseq<0, 3>()));
236 }
237
242 DETRAY_HOST
243 constexpr point3 point_to_global(const point3 &v) const {
244 Fastor::Tensor<scalar_type, 4> vector_4;
245 vector_4(Fastor::fseq<0, 3>()) = v;
246 vector_4[3] = static_cast<scalar_type>(1);
247 return Fastor::Tensor<scalar_type, 3>(
248 Fastor::matmul(_data, vector_4)(Fastor::fseq<0, 3>()));
249 }
250
254 DETRAY_HOST
255 constexpr point3 point_to_local(const point3 &v) const {
256 Fastor::Tensor<scalar_type, 4> vector_4;
257 vector_4(Fastor::fseq<0, 3>()) = v;
258 vector_4[3] = static_cast<scalar_type>(1);
259 return Fastor::Tensor<scalar_type, 3>(
260 Fastor::matmul(_data_inv, vector_4)(Fastor::fseq<0, 3>()));
261 }
262
267 DETRAY_HOST
268 constexpr point3 vector_to_global(const vector2 &v) const {
269 Fastor::Tensor<scalar_type, 4> vector_4;
270 vector_4(Fastor::fseq<0, 2>()) = v;
271 vector_4[2] = static_cast<scalar_type>(0);
272 vector_4[3] = static_cast<scalar_type>(0);
273 return Fastor::Tensor<scalar_type, 3>(
274 Fastor::matmul(_data, vector_4)(Fastor::fseq<0, 3>()));
275 }
276
281 DETRAY_HOST
282 constexpr point3 vector_to_global(const vector3 &v) const {
283 Fastor::Tensor<scalar_type, 4> vector_4;
284 vector_4(Fastor::fseq<0, 3>()) = v;
285 vector_4[3] = static_cast<scalar_type>(0);
286 return Fastor::Tensor<scalar_type, 3>(
287 Fastor::matmul(_data, vector_4)(Fastor::fseq<0, 3>()));
288 }
289
294 DETRAY_HOST
295 constexpr point3 vector_to_local(const vector3 &v) const {
296 Fastor::Tensor<scalar_type, 4> vector_4;
297 vector_4(Fastor::fseq<0, 3>()) = v;
298 vector_4[3] = static_cast<scalar_type>(0);
299 return Fastor::Tensor<scalar_type, 3>(
300 Fastor::matmul(_data_inv, vector_4)(Fastor::fseq<0, 3>()));
301 }
302}; // struct transform3
303
304} // namespace detray::algebra::fastor::math
constexpr point3 z() const
Definition fastor_transform3.hpp:205
constexpr point3 vector_to_global(const vector2 &v) const
This method transform from a vector from the local 2D cartesian frame to the global 3D cartesian fram...
Definition fastor_transform3.hpp:268
matrix44 _data_inv
Definition fastor_transform3.hpp:70
constexpr transform3()
Default constructor: identity.
Definition fastor_transform3.hpp:76
constexpr point3 vector_to_local(const vector3 &v) const
This method transform from a vector from the global 3D cartesian frame into the local 3D cartesian fr...
Definition fastor_transform3.hpp:295
array_type< 2 > vector2
Vector and point types.
Definition fastor_transform3.hpp:49
transform3(const vector3 &t, const vector3 &z, const vector3 &x, bool get_inverse=true)
Constructor with arguments: t, z, x.
Definition fastor_transform3.hpp:115
constexpr point3 point_to_global(const point2 &v) const
This method transform from a point from the local 3D cartesian frame to the global 3D cartesian frame...
Definition fastor_transform3.hpp:229
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 fastor_transform3.hpp:89
transform3< o_scalar_t > other_type
Helper type to cast this to another floating point precision.
Definition fastor_transform3.hpp:62
constexpr point3 y() const
Definition fastor_transform3.hpp:200
constexpr point3 point_to_local(const point3 &v) const
Transform a global point to local coordinates.
Definition fastor_transform3.hpp:255
fastor::Matrix< scalar_type, 4UL, 4UL > matrix44
4x4 matrix type
Definition fastor_transform3.hpp:58
vector3 point3
Three-dimensional point representation.
Definition fastor_transform3.hpp:55
constexpr matrix44 matrix_inverse() const
This method retrieves the 4x4 matrix of an inverse transform.
Definition fastor_transform3.hpp:222
vector2 point2
Two-dimensional point representation.
Definition fastor_transform3.hpp:53
constexpr bool operator==(const transform3 &rhs) const
Equality operator.
Definition fastor_transform3.hpp:180
constexpr auto rotation() const
This method retrieves the rotation of a transform.
Definition fastor_transform3.hpp:187
transform3(const vector3 &t)
Constructor with arguments: translation.
Definition fastor_transform3.hpp:123
constexpr point3 vector_to_global(const vector3 &v) const
This method transform from a vector from the local 3D cartesian frame to the global 3D cartesian fram...
Definition fastor_transform3.hpp:282
constexpr point3 x() const
Definition fastor_transform3.hpp:195
constexpr vector3 translation() const
This method retrieves the translation of a transform.
Definition fastor_transform3.hpp:210
transform3(const transform3 &rhs)=default
Copy an existing transform.
fastor::Vector< scalar_t, N > array_type
Array type used by the transform.
Definition fastor_transform3.hpp:46
transform3(const matrix44 &m)
Constructor with arguments: matrix.
Definition fastor_transform3.hpp:139
constexpr point3 point_to_global(const point3 &v) const
This method transform from a point from the local 3D cartesian frame to the global 3D cartesian frame...
Definition fastor_transform3.hpp:243
constexpr matrix44 matrix() const
This method retrieves the 4x4 matrix of a transform.
Definition fastor_transform3.hpp:217
matrix44 _data
Definition fastor_transform3.hpp:69
transform3(const matrix44 &m, const matrix44 &m_inv)
Constructor with arguments: matrix and its inverse.
Definition fastor_transform3.hpp:148
array_type< 3 > vector3
Three-component vector type used for spatial quantities.
Definition fastor_transform3.hpp:51
scalar_t scalar_type
Scalar type used by the transform.
Definition fastor_transform3.hpp:42
transform3(const array_type< 16 > &ma)
Constructor with arguments: matrix as Fastor::Tensor<scalar_t, 16> of scalars.
Definition fastor_transform3.hpp:167