ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
smatrix_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/smatrix_errorcheck.hpp"
13#include "detray/algebra/utils/approximately_equal.hpp"
14#include "detray/definitions/detail/qualifiers.hpp"
15
16// ROOT/Smatrix include(s).
17#include "Math/SMatrix.h"
18#include "Math/SVector.h"
19
20// System include(s)
21#include <cassert>
22#include <concepts>
23#include <limits>
24
25namespace detray::algebra::smatrix::math {
26
28template <concepts::scalar scalar_t>
29struct transform3 {
32
34 using scalar_type = scalar_t;
35
37 template <unsigned int N>
38 using array_type = ROOT::Math::SVector<scalar_t, N>;
39
45 using point2 = vector2;
47 using point3 = vector3;
48
50 using matrix44 = ROOT::Math::SMatrix<scalar_type, 4, 4>;
51
53 template <concepts::scalar o_scalar_t>
55
57
60
61 matrix44 _data = ROOT::Math::SMatrixIdentity();
64 ROOT::Math::SMatrixIdentity();
66
68
76 DETRAY_HOST_DEVICE
77 transform3(const vector3 &t, const vector3 &x, const vector3 &y,
78 const vector3 &z, bool get_inverse = true) {
79 _data(0, 0) = x[0];
80 _data(1, 0) = x[1];
81 _data(2, 0) = x[2];
82 _data(0, 1) = y[0];
83 _data(1, 1) = y[1];
84 _data(2, 1) = y[2];
85 _data(0, 2) = z[0];
86 _data(1, 2) = z[1];
87 _data(2, 2) = z[2];
88 _data(0, 3) = t[0];
89 _data(1, 3) = t[1];
90 _data(2, 3) = t[2];
91
92 if (get_inverse) {
93 int ifail = 0;
94 _data_inv = _data.Inverse(ifail);
95 SMATRIX_CHECK(ifail);
96 }
97 }
98
105 DETRAY_HOST
106 transform3(const vector3 &t, const vector3 &z, const vector3 &x,
107 bool get_inverse = true)
108 : transform3(t, x, ROOT::Math::Cross(z, x), z, get_inverse) {}
109
113 DETRAY_HOST
114 explicit transform3(const vector3 &t) {
115 _data(0, 3) = t[0];
116 _data(1, 3) = t[1];
117 _data(2, 3) = t[2];
118
119 int ifail = 0;
120 _data_inv = _data.Inverse(ifail);
121 SMATRIX_CHECK(ifail);
122 }
123
127 DETRAY_HOST
128 explicit transform3(const matrix44 &m) {
129 _data = m;
130
131 int ifail = 0;
132 _data_inv = _data.Inverse(ifail);
133 SMATRIX_CHECK(ifail);
134 }
135
140 DETRAY_HOST
141 transform3(const matrix44 &m, const matrix44 &m_inv)
142 : _data{m}, _data_inv{m_inv} {
143 // The assertion will not hold for (casts to) int
144 if constexpr (std::floating_point<scalar_type>) {
145 [[maybe_unused]] constexpr auto epsilon{
146 std::numeric_limits<scalar_type>::epsilon()};
147 assert(algebra::approx_equal(matrix44(m * m_inv),
148 matrix44(ROOT::Math::SMatrixIdentity()),
149 16.f * epsilon, 1e-6f));
150 }
151 }
152
157 DETRAY_HOST
158 explicit transform3(const array_type<16> &ma) {
159 _data(0, 0) = ma[0];
160 _data(1, 0) = ma[4];
161 _data(2, 0) = ma[8];
162 _data(3, 0) = ma[12];
163 _data(0, 1) = ma[1];
164 _data(1, 1) = ma[5];
165 _data(2, 1) = ma[9];
166 _data(3, 1) = ma[13];
167 _data(0, 2) = ma[2];
168 _data(1, 2) = ma[6];
169 _data(2, 2) = ma[10];
170 _data(3, 2) = ma[14];
171 _data(0, 3) = ma[3];
172 _data(1, 3) = ma[7];
173 _data(2, 3) = ma[11];
174 _data(3, 3) = ma[15];
175
176 int ifail = 0;
177 _data_inv = _data.Inverse(ifail);
178 // Ignore failures here, since the unit test does manage to trigger an
179 // error from ROOT in this place...
180 }
181
183 transform3() = default;
186 transform3(const transform3 &rhs) = default;
187 ~transform3() = default;
188
192 DETRAY_HOST
193 constexpr bool operator==(const transform3 &rhs) const {
194 return _data == rhs._data;
195 }
196
199 DETRAY_HOST
200 constexpr auto rotation() const {
201 return (_data.template Sub<ROOT::Math::SMatrix<scalar_type, 3, 3> >(0, 0));
202 }
203
206 DETRAY_HOST_DEVICE
207 constexpr point3 x() const { return (_data.template SubCol<vector3>(0, 0)); }
208
211 DETRAY_HOST_DEVICE
212 constexpr point3 y() const { return (_data.template SubCol<vector3>(1, 0)); }
213
216 DETRAY_HOST_DEVICE
217 constexpr point3 z() const { return (_data.template SubCol<vector3>(2, 0)); }
218
221 DETRAY_HOST
222 constexpr vector3 translation() const {
223 return (_data.template SubCol<vector3>(3, 0));
224 }
225
228 DETRAY_HOST
229 constexpr matrix44 matrix() const { return _data; }
230
233 DETRAY_HOST
234 constexpr matrix44 matrix_inverse() const { return _data_inv; }
235
240 DETRAY_HOST
241 constexpr point3 point_to_global(const point2 &v) const {
242 ROOT::Math::SVector<scalar_type, 4> vector_4;
243 vector_4.Place_at(v, 0);
244 vector_4[3] = static_cast<scalar_type>(1);
245 return ROOT::Math::SVector<scalar_type, 4>(_data * vector_4)
246 .template Sub<point3>(0);
247 }
248
253 DETRAY_HOST
254 constexpr point3 point_to_global(const point3 &v) const {
255 ROOT::Math::SVector<scalar_type, 4> vector_4;
256 vector_4.Place_at(v, 0);
257 vector_4[3] = static_cast<scalar_type>(1);
258 return ROOT::Math::SVector<scalar_type, 4>(_data * vector_4)
259 .template Sub<point3>(0);
260 }
261
265 DETRAY_HOST
266 constexpr point3 point_to_local(const point3 &v) const {
267 ROOT::Math::SVector<scalar_type, 4> vector_4;
268 vector_4.Place_at(v, 0);
269 vector_4[3] = static_cast<scalar_type>(1);
270 return ROOT::Math::SVector<scalar_type, 4>(_data_inv * vector_4)
271 .template Sub<point3>(0);
272 }
273
278 DETRAY_HOST
279 constexpr point3 vector_to_global(const vector2 &v) const {
280 ROOT::Math::SVector<scalar_type, 4> vector_4;
281 vector_4.Place_at(v, 0);
282 return ROOT::Math::SVector<scalar_type, 4>(_data * vector_4)
283 .template Sub<point3>(0);
284 }
285
290 DETRAY_HOST
291 constexpr point3 vector_to_global(const vector3 &v) const {
292 ROOT::Math::SVector<scalar_type, 4> vector_4;
293 vector_4.Place_at(v, 0);
294 return ROOT::Math::SVector<scalar_type, 4>(_data * vector_4)
295 .template Sub<point3>(0);
296 }
297
302 DETRAY_HOST
303 constexpr point3 vector_to_local(const vector3 &v) const {
304 ROOT::Math::SVector<scalar_type, 4> vector_4;
305 vector_4.Place_at(v, 0);
306 return ROOT::Math::SVector<scalar_type, 4>(_data_inv * vector_4)
307 .template Sub<point3>(0);
308 }
309}; // struct transform3
310
311} // namespace detray::algebra::smatrix::math
constexpr point3 z() const
Definition smatrix_transform3.hpp:217
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 smatrix_transform3.hpp:303
ROOT::Math::SMatrix< scalar_type, 4, 4 > matrix44
4x4 matrix type
Definition smatrix_transform3.hpp:50
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 smatrix_transform3.hpp:254
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 smatrix_transform3.hpp:241
transform3(const vector3 &t, const vector3 &z, const vector3 &x, bool get_inverse=true)
Constructor with arguments: t, z, x.
Definition smatrix_transform3.hpp:106
vector3 point3
Three-dimensional point representation.
Definition smatrix_transform3.hpp:47
constexpr matrix44 matrix() const
This method retrieves the 4x4 matrix of a transform.
Definition smatrix_transform3.hpp:229
transform3< o_scalar_t > other_type
Helper type to cast this to another floating point precision.
Definition smatrix_transform3.hpp:54
matrix44 _data
Definition smatrix_transform3.hpp:61
transform3(const vector3 &t)
Constructor with arguments: translation.
Definition smatrix_transform3.hpp:114
transform3(const matrix44 &m, const matrix44 &m_inv)
Constructor with arguments: matrix and its inverse.
Definition smatrix_transform3.hpp:141
transform3(const array_type< 16 > &ma)
Constructor with arguments: matrix as ROOT::Math::SVector<scalar_t, 16> of scalars.
Definition smatrix_transform3.hpp:158
array_type< 3 > vector3
Three-component vector type used for spatial quantities.
Definition smatrix_transform3.hpp:43
matrix44 _data_inv
Definition smatrix_transform3.hpp:63
constexpr bool operator==(const transform3 &rhs) const
Equality operator.
Definition smatrix_transform3.hpp:193
constexpr vector3 translation() const
This method retrieves the translation of a transform.
Definition smatrix_transform3.hpp:222
constexpr point3 x() const
Definition smatrix_transform3.hpp:207
constexpr matrix44 matrix_inverse() const
This method retrieves the 4x4 matrix of an inverse transform.
Definition smatrix_transform3.hpp:234
constexpr auto rotation() const
This method retrieves the rotation of a transform.
Definition smatrix_transform3.hpp:200
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 smatrix_transform3.hpp:291
constexpr point3 y() const
Definition smatrix_transform3.hpp:212
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 smatrix_transform3.hpp:77
transform3(const transform3 &rhs)=default
Copy a transform and its cached inverse.
transform3()=default
Default constructors.
scalar_t scalar_type
Scalar type used by the transform.
Definition smatrix_transform3.hpp:34
vector2 point2
Two-dimensional point representation.
Definition smatrix_transform3.hpp:45
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 smatrix_transform3.hpp:279
constexpr point3 point_to_local(const point3 &v) const
Transform a global point to local coordinates.
Definition smatrix_transform3.hpp:266
array_type< 2 > vector2
Vector and point types.
Definition smatrix_transform3.hpp:41
ROOT::Math::SVector< scalar_t, N > array_type
Array type used by the transform.
Definition smatrix_transform3.hpp:38
transform3(const matrix44 &m)
Constructor with arguments: matrix.
Definition smatrix_transform3.hpp:128