ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
eigen_getter.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 "detray/algebra/common/math.hpp"
13#include "detray/algebra/concepts.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 20012
23#endif // __NVCC_DIAG_PRAGMA_SUPPORT__
24#include <Eigen/Core>
25#ifdef _MSC_VER
26#pragma warning(pop)
27#endif // MSVC
28#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
29#pragma nv_diagnostic pop
30#endif // __NVCC_DIAG_PRAGMA_SUPPORT__
31
32// System include(s).
33#include <type_traits>
34
35namespace detray::algebra::eigen::storage {
36
44 template <typename derived_type, concepts::index index_t_1,
45 concepts::index index_t_2>
46 requires std::is_base_of_v<
47 Eigen::DenseCoeffsBase<derived_type, Eigen::WriteAccessors>,
48 Eigen::MatrixBase<derived_type>>
49 DETRAY_HOST_DEVICE constexpr auto &operator()(
50 Eigen::MatrixBase<derived_type> &m, index_t_1 row, index_t_2 col) const {
51 return m(static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
52 }
53
58 template <typename derived_type, concepts::index index_t_1,
59 concepts::index index_t_2>
60 DETRAY_HOST_DEVICE constexpr auto operator()(
61 const Eigen::MatrixBase<derived_type> &m, index_t_1 row,
62 index_t_2 col) const {
63 return m(static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
64 }
65
69 template <typename derived_type, concepts::index index_t>
70 requires std::is_base_of_v<
71 Eigen::DenseCoeffsBase<derived_type, Eigen::WriteAccessors>,
72 Eigen::MatrixBase<derived_type>>
73 DETRAY_HOST_DEVICE constexpr auto &operator()(
74 Eigen::MatrixBase<derived_type> &m, index_t row) const {
75 return m(static_cast<Eigen::Index>(row));
76 }
77
81 template <typename derived_type, concepts::index index_t>
82 DETRAY_HOST_DEVICE constexpr auto operator()(
83 const Eigen::MatrixBase<derived_type> &m, index_t row) const {
84 return m(static_cast<Eigen::Index>(row));
85 }
86}; // struct element_getter
87
89template <typename derived_type>
90DETRAY_HOST_DEVICE constexpr decltype(auto) element(
91 const Eigen::MatrixBase<derived_type> &m, std::size_t row,
92 std::size_t col) {
93 return element_getter()(m, static_cast<Eigen::Index>(row),
94 static_cast<Eigen::Index>(col));
95}
96
98template <typename derived_type>
99 requires std::is_base_of_v<
100 Eigen::DenseCoeffsBase<derived_type, Eigen::WriteAccessors>,
101 Eigen::MatrixBase<derived_type>>
102DETRAY_HOST_DEVICE constexpr decltype(auto) element(
103 Eigen::MatrixBase<derived_type> &m, std::size_t row, std::size_t col) {
104 return element_getter()(m, static_cast<Eigen::Index>(row),
105 static_cast<Eigen::Index>(col));
106}
107
109template <typename derived_type>
110DETRAY_HOST_DEVICE constexpr decltype(auto) element(
111 const Eigen::MatrixBase<derived_type> &m, std::size_t row) {
112 return element_getter()(m, static_cast<Eigen::Index>(row));
113}
114
116template <typename derived_type>
117 requires std::is_base_of_v<
118 Eigen::DenseCoeffsBase<derived_type, Eigen::WriteAccessors>,
119 Eigen::MatrixBase<derived_type>>
120DETRAY_HOST_DEVICE constexpr decltype(auto) element(
121 Eigen::MatrixBase<derived_type> &m, std::size_t row) {
122 return element_getter()(m, static_cast<Eigen::Index>(row));
123}
124
125template <std::size_t I, std::size_t J, concepts::matrix M>
126DETRAY_HOST_DEVICE decltype(auto) element(M &matrix) {
127 if constexpr (concepts::has_compile_time_2d_access<M>) {
128 return matrix.template element<I, J>();
129 } else {
130 using index_t = detray::traits::index_t<std::decay_t<M>>;
131 return element(matrix, static_cast<index_t>(I), static_cast<index_t>(J));
132 }
133}
134
135template <std::size_t I, concepts::vector V>
136DETRAY_HOST_DEVICE decltype(auto) element(V &vector) {
137 if constexpr (concepts::has_compile_time_1d_access<V>) {
138 return vector.template element<I>();
139 } else {
140 using index_t = detray::traits::index_t<std::decay_t<V>>;
141 return element(vector, static_cast<index_t>(I));
142 }
143}
144
152 template <int kROWS, int kCOLS, typename derived_type,
153 concepts::index index_t_1, concepts::index index_t_2>
154 DETRAY_HOST_DEVICE decltype(auto) operator()(
155 const Eigen::MatrixBase<derived_type> &m, const index_t_1 row,
156 const index_t_2 col) const {
157 return m.template block<kROWS, kCOLS>(row, col);
158 }
159
165 template <int kROWS, int kCOLS, typename derived_type,
166 concepts::index index_t_1, concepts::index index_t_2>
167 DETRAY_HOST_DEVICE decltype(auto) operator()(
168 Eigen::MatrixBase<derived_type> &m, const index_t_1 row,
169 const index_t_2 col) const {
170 return m.template block<kROWS, kCOLS>(row, col);
171 }
172
178 template <int SIZE, typename derived_type, concepts::index index_t_1,
179 concepts::index index_t_2>
180 DETRAY_HOST_DEVICE decltype(auto) vector(Eigen::MatrixBase<derived_type> &m,
181 const index_t_1 row,
182 const index_t_2 col) const {
183 return m.template block<SIZE, 1>(row, col);
184 }
185
191 template <int SIZE, typename derived_type, concepts::index index_t_1,
192 concepts::index index_t_2>
193 DETRAY_HOST_DEVICE decltype(auto) vector(
194 const Eigen::MatrixBase<derived_type> &m, const index_t_1 row,
195 const index_t_2 col) const {
196 return m.template block<SIZE, 1>(row, col);
197 }
198
204 template <typename derived_type1, typename derived_type2,
205 concepts::index index_t_1, concepts::index index_t_2>
206 DETRAY_HOST_DEVICE void set(Eigen::MatrixBase<derived_type1> &m,
207 const Eigen::MatrixBase<derived_type2> &b,
208 const index_t_1 row, const index_t_2 col) const {
209 using block_t = Eigen::MatrixBase<derived_type2>;
210 constexpr auto R{block_t::RowsAtCompileTime};
211 constexpr auto C{block_t::ColsAtCompileTime};
212 m.template block<R, C>(static_cast<Eigen::Index>(row),
213 static_cast<Eigen::Index>(col)) = b;
214 }
215
216}; // struct block_getter
217
219template <int ROWS, int COLS, class derived_type>
220DETRAY_HOST_DEVICE decltype(auto) block(
221 const Eigen::MatrixBase<derived_type> &m, std::size_t row,
222 std::size_t col) {
223 return block_getter{}.template operator()<ROWS, COLS>(
224 m, static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
225}
226
228template <int ROWS, int COLS, class derived_type>
229DETRAY_HOST_DEVICE decltype(auto) block(Eigen::MatrixBase<derived_type> &m,
230 std::size_t row, std::size_t col) {
231 return block_getter{}.template operator()<ROWS, COLS>(
232 m, static_cast<Eigen::Index>(row), static_cast<Eigen::Index>(col));
233}
234
236template <int SIZE, typename derived_type>
237DETRAY_HOST_DEVICE constexpr decltype(auto) vector(
238 const Eigen::MatrixBase<derived_type> &m, std::size_t row,
239 std::size_t col) {
240 return block_getter{}.template vector<SIZE>(m, static_cast<Eigen::Index>(row),
241 static_cast<Eigen::Index>(col));
242}
243
245template <typename derived_type1, typename derived_type2>
246DETRAY_HOST_DEVICE void set_block(Eigen::MatrixBase<derived_type1> &m,
247 const Eigen::MatrixBase<derived_type2> &b,
248 std::size_t row, std::size_t col) {
249 block_getter{}.set(m, b, static_cast<Eigen::Index>(row),
250 static_cast<Eigen::Index>(col));
251}
252
253} // namespace detray::algebra::eigen::storage
Index concept to access vector/matrix elements.
Definition concepts.hpp:54
Functor used to extract a block from Eigen matrices.
Definition eigen_getter.hpp:146
decltype(auto) vector(Eigen::MatrixBase< derived_type > &m, const index_t_1 row, const index_t_2 col) const
Return a mutable column segment from a matrix.
Definition eigen_getter.hpp:180
decltype(auto) vector(const Eigen::MatrixBase< derived_type > &m, const index_t_1 row, const index_t_2 col) const
Return a const column segment from a matrix.
Definition eigen_getter.hpp:193
void set(Eigen::MatrixBase< derived_type1 > &m, const Eigen::MatrixBase< derived_type2 > &b, const index_t_1 row, const index_t_2 col) const
Operator setting a block.
Definition eigen_getter.hpp:206
Functor used to access elements of Eigen matrices.
Definition eigen_getter.hpp:38
constexpr auto operator()(const Eigen::MatrixBase< derived_type > &m, index_t row) const
Get const access to a matrix element.
Definition eigen_getter.hpp:82
constexpr auto & operator()(Eigen::MatrixBase< derived_type > &m, index_t_1 row, index_t_2 col) const
Get non-const access to a matrix element.
Definition eigen_getter.hpp:49
constexpr auto operator()(const Eigen::MatrixBase< derived_type > &m, index_t_1 row, index_t_2 col) const
Get const access to a matrix element.
Definition eigen_getter.hpp:60
constexpr auto & operator()(Eigen::MatrixBase< derived_type > &m, index_t row) const
Get non-const access to a matrix element.
Definition eigen_getter.hpp:73