ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
type_traits.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// System include(s).
12#include <algorithm>
13#include <cmath>
14#include <type_traits>
15
16namespace detray {
17
18namespace traits {
19
22
25template <class M>
26struct index {};
27
28template <class M>
29using index_t = typename index<M>::type;
31
34template <class M>
35struct value {};
36
38template <typename T>
39 requires(std::is_arithmetic_v<T> && std::is_scalar_v<T>)
40struct value<T> {
42 using type = T;
43};
44
45template <class M>
46using value_t = typename value<M>::type;
48
51template <class M>
52struct scalar {
54 using type = value_t<M>;
55};
56
57template <class M>
58using scalar_t = typename scalar<M>::type;
60
63template <class M>
64struct vector {};
65
66template <class M>
67using vector_t = typename vector<M>::type;
68
69template <class M, index_t<M> N, typename T>
70using get_vector_t = typename vector<M>::template other_type<T, N>;
71
72template <class M>
73struct matrix {};
74
75template <class M, index_t<M> ROWS, index_t<M> COLS, typename T>
76using get_matrix_t = typename matrix<M>::template other_type<T, ROWS, COLS>;
78
81template <typename M>
82struct dimensions {
84 using index_type = int;
85
86 // Error case
87 static constexpr index_type _dim{-1};
88 static constexpr index_type _rows{-1};
89 static constexpr index_type _columns{-1};
90};
91
93template <typename M>
94 requires std::is_fundamental_v<M>
95struct dimensions<M> {
97 using index_type = std::size_t;
98
99 static constexpr index_type _dim{0};
100 static constexpr index_type _rows{1};
101 static constexpr index_type _columns{1};
102};
103
104template <class M>
105inline constexpr index_t<M> dim{dimensions<std::remove_cvref_t<M>>::_dim};
106
107template <class M>
108inline constexpr index_t<M> rows{dimensions<std::remove_cvref_t<M>>::_rows};
109
110template <class M>
111inline constexpr index_t<M> columns{
113
114template <class M>
115inline constexpr index_t<M> max_rank{std::min(rows<M>, columns<M>)};
116
117template <class M>
118inline constexpr index_t<M> size{rows<M> * columns<M>};
119
120template <class V>
121inline constexpr bool is_vector{dimensions<std::remove_cvref_t<V>>::_dim == 1};
122
123template <class M>
124inline constexpr bool is_matrix{dimensions<std::remove_cvref_t<M>>::_dim == 2};
125
126template <class M>
127inline constexpr bool is_square{(rows<M> == columns<M>)};
129
132template <class M>
134
135template <class M>
136using element_getter_t = typename element_getter<M>::type;
137
138template <class M>
139struct block_getter {};
140
141template <class M>
142using block_getter_t = typename block_getter<M>::type;
144
147template <typename A, typename = void>
148struct get_algebra {};
149
151template <typename A>
152 requires(!std::is_same_v<typename A::point3D, void>)
155 using algebra_type = A;
156
158 template <typename U>
159 using simd = typename A::template simd<U>;
161 using index_type = typename A::index_type;
163 using boolean = typename A::boolean;
165 using value = typename A::value_type;
167 using scalar = typename A::scalar;
169 using point2D = typename A::point2D;
171 using point3D = typename A::point3D;
173 using vector2D = typename A::vector2D;
175 using vector3D = typename A::vector3D;
177 using transform3D = typename A::transform3D;
179 template <std::size_t ROWS, std::size_t COLS>
180 using matrix = typename A::template matrix<ROWS, COLS>;
181};
182
184template <template <typename> class A, typename W, typename V>
185 requires(!std::is_same_v<typename A<V>::point3D, void> &&
186 !std::is_same_v<typename A<W>::point3D, void>)
187struct get_algebra<A<V>, W> {
189 using algebra_type = A<W>;
190
192 template <typename U>
193 using simd = typename A<W>::template simd<U>;
195 using index_type = typename A<W>::index_type;
197 using boolean = typename A<W>::boolean;
199 using value = typename A<W>::value_type;
201 using scalar = typename A<W>::scalar;
203 using point2D = typename A<W>::point2D;
205 using point3D = typename A<W>::point3D;
207 using vector2D = typename A<W>::vector2D;
209 using vector3D = typename A<W>::vector3D;
211 using transform3D = typename A<W>::transform3D;
213 template <std::size_t ROWS, std::size_t COLS>
214 using matrix = typename A<W>::template matrix<ROWS, COLS>;
215};
216
217} // namespace traits
218
219// Algebra type aliases
221template <typename A, typename T>
223
225template <typename A>
227
229template <typename A>
231
233template <typename A, typename T>
235
237template <typename A>
239
241template <typename A, typename T = get_value_t<A>>
243
245template <typename A, typename T = get_value_t<A>>
247
249template <typename A, typename T = get_value_t<A>>
251
253template <typename A, typename T = get_value_t<A>>
255
257template <typename A, typename T = get_value_t<A>>
259
261template <typename A, typename T = get_value_t<A>>
263
265template <typename A, std::size_t R, std::size_t C, typename T = get_value_t<A>>
267
268} // namespace detray
269
272#define DETRAY_ALGEBRA_DEFINE_TYPE_TRAITS(A) \
273 \
274 namespace traits { \
275 \
276 template <typename T, auto N> \
277 struct index<A::vector_type<T, N>> { \
278 using type = detray::A::index_type; \
279 }; \
280 \
281 template <typename T, auto ROWS, auto COLS> \
282 struct index<A::matrix_type<T, ROWS, COLS>> { \
283 using type = detray::A::index_type; \
284 }; \
285 \
286 template <typename T, auto N> \
287 struct dimensions<A::vector_type<T, N>> { \
288 using index_type = index_t<A::vector_type<T, N>>; \
289 \
290 static constexpr index_type _dim{1}; \
291 static constexpr index_type _rows{N}; \
292 static constexpr index_type _columns{1}; \
293 }; \
294 \
295 template <typename T, auto ROWS, auto COLS> \
296 struct dimensions<A::matrix_type<T, ROWS, COLS>> { \
297 using index_type = index_t<A::matrix_type<T, ROWS, COLS>>; \
298 \
299 static constexpr index_type _dim{2}; \
300 static constexpr index_type _rows{ROWS}; \
301 static constexpr index_type _columns{COLS}; \
302 }; \
303 \
304 template <typename T, auto N> \
305 struct value<A::vector_type<T, N>> { \
306 using type = T; \
307 }; \
308 \
309 template <typename T, auto ROWS, auto COLS> \
310 struct value<A::matrix_type<T, ROWS, COLS>> { \
311 using type = T; \
312 }; \
313 \
314 template <typename T, auto N> \
315 struct vector<A::vector_type<T, N>> { \
316 template <typename other_T, auto other_N> \
317 using other_type = A::vector_type<other_T, other_N>; \
318 \
319 using type = other_type<T, N>; \
320 }; \
321 \
322 template <typename T, auto ROWS, auto COLS> \
323 struct vector<A::matrix_type<T, ROWS, COLS>> { \
324 template <typename other_T, auto other_N> \
325 using other_type = A::vector_type<other_T, other_N>; \
326 \
327 using type = other_type<T, ROWS>; \
328 }; \
329 \
330 template <typename T, auto ROWS, auto COLS> \
331 struct matrix<A::matrix_type<T, ROWS, COLS>> { \
332 template <typename other_T, auto other_ROWS, auto other_COLS> \
333 using other_type = A::matrix_type<other_T, other_ROWS, other_COLS>; \
334 \
335 using type = A::matrix_type<T, ROWS, COLS>; \
336 }; \
337 \
338 template <typename T, auto N> \
339 struct matrix<A::vector_type<T, N>> { \
340 template <typename other_T, auto other_ROWS, auto other_COLS> \
341 using other_type = A::matrix_type<other_T, other_ROWS, other_COLS>; \
342 \
343 using type = other_type<T, N, 1>; \
344 }; \
345 \
346 template <typename T, auto N> \
347 struct element_getter<A::vector_type<T, N>> { \
348 using type = A::element_getter; \
349 }; \
350 \
351 template <typename T, auto ROWS, auto COLS> \
352 struct element_getter<A::matrix_type<T, ROWS, COLS>> { \
353 using type = A::element_getter; \
354 }; \
355 \
356 template <typename T, auto ROWS, auto COLS> \
357 struct block_getter<A::matrix_type<T, ROWS, COLS>> { \
358 using type = A::block_getter; \
359 }; \
360 \
361 } // namespace detray::traits
362
Detray include(s).
Definition array_operators.hpp:18
typename traits::get_algebra< A >::template simd< T > get_simd_t
Get the algebra SIMD type for a lane value type.
Definition type_traits.hpp:234
typename traits::get_algebra< A >::boolean get_boolean_t
Get the algebra boolean type.
Definition type_traits.hpp:230
typename traits::get_algebra< A, T >::scalar get_scalar_t
Get the algebra scalar type, optionally for a replacement value type.
Definition type_traits.hpp:242
typename traits::get_algebra< A, T >::point2D get_point2D_t
Get the 2D point type, optionally for a replacement value type.
Definition type_traits.hpp:246
typename traits::get_algebra< A, T >::algebra_type get_algebra_t
Select the algebra type, optionally replacing its value type.
Definition type_traits.hpp:222
typename traits::get_algebra< A >::index_type get_index_t
Get the algebra index type.
Definition type_traits.hpp:238
typename traits::get_algebra< A, T >::template matrix< R, C > get_matrix_t
Get a matrix type with the requested dimensions and value type.
Definition type_traits.hpp:266
typename traits::get_algebra< A, T >::transform3D get_transform3D_t
Get the 3D transform type, optionally for a replacement value type.
Definition type_traits.hpp:262
typename traits::get_algebra< A, T >::vector2D get_vector2D_t
Get the 2D vector type, optionally for a replacement value type.
Definition type_traits.hpp:254
typename traits::get_algebra< A >::value get_value_t
Get the algebra value type.
Definition type_traits.hpp:226
typename traits::get_algebra< A, T >::point3D get_point3D_t
Get the 3D point type, optionally for a replacement value type.
Definition type_traits.hpp:250
typename traits::get_algebra< A, T >::vector3D get_vector3D_t
Get the 3D vector type, optionally for a replacement value type.
Definition type_traits.hpp:258
Definition type_traits.hpp:139
static constexpr index_type _dim
Scalars have rank zero.
Definition type_traits.hpp:99
static constexpr index_type _rows
Scalars occupy one row.
Definition type_traits.hpp:100
static constexpr index_type _columns
Scalars occupy one column.
Definition type_traits.hpp:101
std::size_t index_type
Index type used to represent dimensions.
Definition type_traits.hpp:97
Matrix dimensions.
Definition type_traits.hpp:82
static constexpr index_type _dim
Invalid dimension sentinel.
Definition type_traits.hpp:87
static constexpr index_type _rows
Invalid row count sentinel.
Definition type_traits.hpp:88
static constexpr index_type _columns
Invalid column count sentinel.
Definition type_traits.hpp:89
int index_type
Index type used to represent dimensions.
Definition type_traits.hpp:84
Getter types.
Definition type_traits.hpp:133
typename A::index_type index_type
Integer type used for indices.
Definition type_traits.hpp:161
A algebra_type
The algebra type itself.
Definition type_traits.hpp:155
typename A::scalar scalar
Scalar type, possibly containing multiple values in an SoA algebra.
Definition type_traits.hpp:167
typename A::boolean boolean
Boolean type used by the algebra.
Definition type_traits.hpp:163
typename A::template simd< U > simd
SIMD type parameterized by the lane value type.
Definition type_traits.hpp:159
typename A::vector2D vector2D
Two-dimensional vector type.
Definition type_traits.hpp:173
typename A::value_type value
Value type used by the algebra.
Definition type_traits.hpp:165
typename A::transform3D transform3D
Three-dimensional transform type.
Definition type_traits.hpp:177
typename A::point3D point3D
Three-dimensional point type.
Definition type_traits.hpp:171
typename A::vector3D vector3D
Three-dimensional vector type.
Definition type_traits.hpp:175
typename A::template matrix< ROWS, COLS > matrix
Matrix type with the requested dimensions.
Definition type_traits.hpp:180
typename A::point2D point2D
Two-dimensional point type.
Definition type_traits.hpp:169
typename A< W >::template simd< U > simd
SIMD type parameterized by the lane value type.
Definition type_traits.hpp:193
A< W > algebra_type
The algebra instantiated with the replacement value type.
Definition type_traits.hpp:189
typename A< W >::boolean boolean
Boolean type used by the algebra.
Definition type_traits.hpp:197
typename A< W >::transform3D transform3D
Three-dimensional transform type.
Definition type_traits.hpp:211
typename A< W >::template matrix< ROWS, COLS > matrix
Matrix type with the requested dimensions.
Definition type_traits.hpp:214
typename A< W >::point3D point3D
Three-dimensional point type.
Definition type_traits.hpp:205
typename A< W >::point2D point2D
Two-dimensional point type.
Definition type_traits.hpp:203
typename A< W >::scalar scalar
Scalar type, possibly containing multiple values in an SoA algebra.
Definition type_traits.hpp:201
typename A< W >::vector3D vector3D
Three-dimensional vector type.
Definition type_traits.hpp:209
typename A< W >::value_type value
Value type used by the algebra.
Definition type_traits.hpp:199
typename A< W >::vector2D vector2D
Two-dimensional vector type.
Definition type_traits.hpp:207
typename A< W >::index_type index_type
Integer type used for indices.
Definition type_traits.hpp:195
The algebra types.
Definition type_traits.hpp:148
Matrix traits.
Definition type_traits.hpp:26
Definition type_traits.hpp:73
Scalar type that is used with the matrix (can be multiple values in SoA).
Definition type_traits.hpp:52
value_t< M > type
By default, the scalar type is the matrix value type.
Definition type_traits.hpp:54
T type
The arithmetic type itself.
Definition type_traits.hpp:42
Value type that is used with the matrix (e.g.
Definition type_traits.hpp:35
Vector type that is compatible with the matrix.
Definition type_traits.hpp:64