ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
volume_descriptor.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/definitions/detail/qualifiers.hpp"
13#include "detray/definitions/geometry.hpp"
14#include "detray/definitions/indexing.hpp"
15
16// System include(s)
17#include <ostream>
18#include <utility>
19
20namespace detray {
21
35template <typename ID, typename acc_link_t = dtyped_index<dindex, dindex>,
36 typename mat_link_t = dtyped_index<dindex, dindex>>
38 public:
40 using object_id = ID;
41
45 static_cast<std::size_t>(
46 static_cast<std::underlying_type_t<surface_id>>(
47 surface_id::e_all))>;
48
59
61 using material_link = mat_link_t;
63 using material_id = typename material_link::id_type;
64
66 constexpr volume_descriptor() = default;
67
71 explicit constexpr volume_descriptor(const volume_id id) : m_id{id} {}
72
75 static consteval bool is_surface_id(const object_id id) {
76 return (id == object_id::e_portal || id == object_id::e_sensitive ||
77 id == object_id::e_passive);
78 }
79
82 static consteval bool is_volume_id(const object_id id) {
83 return (id == object_id::e_volume);
84 }
85
87 DETRAY_HOST_DEVICE
88 constexpr auto id() const -> volume_id { return m_id; }
89
91 DETRAY_HOST_DEVICE
92 constexpr auto index() const -> dindex { return m_index; }
93
95 DETRAY_HOST
96 constexpr auto set_index(const dindex index) -> void { m_index = index; }
97
99 DETRAY_HOST_DEVICE
100 constexpr auto transform() const -> dindex { return m_transform; }
101
103 DETRAY_HOST
104 constexpr auto set_transform(const dindex trf_idx) -> void {
105 m_transform = trf_idx;
106 }
107
109 DETRAY_HOST_DEVICE constexpr auto sf_link() const -> const sf_link_type& {
110 return m_sf_links;
111 }
112
114 template <surface_id id>
115 DETRAY_HOST_DEVICE constexpr auto sf_link() const -> const
116 typename sf_link_type::index_type& {
117 return detail::get<static_cast<uint>(id)>(m_sf_links);
118 }
119
121 template <surface_id id>
122 DETRAY_HOST_DEVICE constexpr auto sf_link() ->
123 typename sf_link_type::index_type& {
124 return detail::get<static_cast<uint>(id)>(m_sf_links);
125 }
126
128 DETRAY_HOST_DEVICE constexpr auto full_sf_range() const ->
129 typename sf_link_type::index_type {
130 using idx_range_t = typename sf_link_type::index_type;
131
132 const auto& pt_range = sf_link<surface_id::e_portal>();
133 const auto& sen_range = sf_link<surface_id::e_sensitive>();
134 const auto& psv_range = sf_link<surface_id::e_passive>();
135
136 // Portal range is never empty
137 dindex min{detail::get<0>(pt_range)};
138 dindex max{detail::get<1>(pt_range)};
139
140 constexpr idx_range_t empty{};
141 if (sen_range != empty) {
142 min = detail::get<0>(sen_range) < min ? detail::get<0>(sen_range) : min;
143 max = detail::get<1>(sen_range) > max ? detail::get<1>(sen_range) : max;
144 }
145
146 if (psv_range != empty) {
147 min = detail::get<0>(psv_range) < min ? detail::get<0>(psv_range) : min;
148 max = detail::get<1>(psv_range) > max ? detail::get<1>(psv_range) : max;
149 }
150
151 return idx_range_t{min, max};
152 }
153
155 DETRAY_HOST_DEVICE constexpr dindex n_surfaces() const {
156 const auto sf_idx_range = full_sf_range();
157
158 assert(sf_idx_range[1] > sf_idx_range[0]);
159 return sf_idx_range[1] - sf_idx_range[0];
160 }
161
164 DETRAY_HOST_DEVICE constexpr dindex to_local_sf_index(dindex sf_idx) const {
165 auto full_range = full_sf_range();
166
167 assert(full_range[0] <= sf_idx);
168 assert(sf_idx < full_range[1]);
169
170 return sf_idx - full_range[0];
171 }
172
175 DETRAY_HOST_DEVICE constexpr dindex to_global_sf_index(dindex sf_idx) const {
176 auto full_range = full_sf_range();
177 dindex glob_index{sf_idx + full_range[0]};
178
179 assert(full_range[0] <= glob_index);
180 assert(glob_index < full_range[1]);
181
182 return glob_index;
183 }
184
192 template <surface_id id>
193 DETRAY_HOST auto update_sf_link(
194 const typename sf_link_type::index_type& other) noexcept -> void {
195 auto& rg = sf_link<id>();
196 // Range not set yet - initialize
197 if (constexpr typename sf_link_type::index_type empty{}; rg == empty) {
198 rg = other;
199 } else {
200 // Update upper border
201 assert(detail::get<1>(rg) == detail::get<0>(other));
202 rg.set_upper(other.upper());
203 }
204 }
205
214 template <surface_id id>
215 DETRAY_HOST auto update_sf_link(std::size_t shift,
216 std::size_t n_surfaces = 0) noexcept -> void {
217 auto& rg = sf_link<id>();
218 // Range not set yet - initialize
219 if (constexpr typename sf_link_type::index_type empty{}; rg == empty) {
220 rg = {0u, static_cast<dindex>(n_surfaces)};
221 }
222 // Update
223 rg.shift(static_cast<dindex>(shift));
224 }
225
227 DETRAY_HOST_DEVICE
228 constexpr auto material() const -> const material_link& { return m_mat_link; }
229
231 DETRAY_HOST
232 constexpr auto set_material(const material_link& mat_link) -> void {
233 m_mat_link = mat_link;
234 }
235
238 DETRAY_HOST
239 constexpr auto set_material(const material_id mat_id, const dindex mat_idx)
240 -> void {
241 m_mat_link = {mat_id,
242 static_cast<typename material_link::index_type>(mat_idx)};
243 }
244
246 DETRAY_HOST_DEVICE
247 constexpr auto has_material() const -> bool {
248 return (m_mat_link.id() != material_link::id_type::e_none) &&
249 !m_mat_link.is_invalid();
250 }
251
253 DETRAY_HOST_DEVICE constexpr auto accel_link() const
254 -> const accel_link_type& {
255 return m_accel_links;
256 }
257
261 DETRAY_HOST_DEVICE constexpr auto accel_link(const ID& id) const
262 -> const acc_link_t& {
263 static_assert(static_cast<std::size_t>(ID::e_size) >= 1);
264 assert(static_cast<std::size_t>(id) < static_cast<std::size_t>(ID::e_size));
265 return accel_link_helper<static_cast<ID>(
266 static_cast<std::size_t>(ID::e_size) - 1)>(id);
267 }
268
270 template <ID obj_id>
271 DETRAY_HOST_DEVICE constexpr auto accel_link() const -> const acc_link_t& {
272 return detail::get<obj_id>(m_accel_links);
273 }
274
276 template <ID obj_id>
277 DETRAY_HOST constexpr auto set_accel_link(const acc_link_t link) -> void {
278 detail::get<obj_id>(m_accel_links) = link;
279 }
280
283 template <ID obj_id>
284 DETRAY_HOST constexpr auto set_accel_link(
285 const typename acc_link_t::id_type id,
286 const typename acc_link_t::index_type index) -> void {
287 detail::get<obj_id>(m_accel_links) = acc_link_t{id, index};
288 }
289
296 DETRAY_HOST constexpr auto set_accel_link(
297 const ID obj_id, const typename acc_link_t::id_type accel_id,
298 const typename acc_link_t::index_type index) -> void {
299 m_accel_links[obj_id] = acc_link_t{accel_id, index};
300 }
301
306 DETRAY_HOST_DEVICE
307 constexpr auto operator==(const volume_descriptor& rhs) const -> bool {
308 return (m_id == rhs.m_id && m_index == rhs.m_index &&
309 m_accel_links == rhs.m_accel_links);
310 }
311
313 DETRAY_HOST
314 friend std::ostream& operator<<(std::ostream& os,
315 const volume_descriptor& v_desc) {
316 os << "id = " << v_desc.id() << "(" << static_cast<int>(v_desc.id()) << ")";
317 os << " | index = " << v_desc.index();
318 os << " | trf. = " << v_desc.transform();
319 os << " | acc link: " << v_desc.accel_link();
320 os << " | sf link: " << v_desc.sf_link();
321 os << " | mat link: " << v_desc.material();
322 return os;
323 }
324
325 private:
328 template <ID obj_id>
329 DETRAY_HOST_DEVICE constexpr auto accel_link_helper(const ID& id) const
330 -> const acc_link_t& {
331 if (id == obj_id) {
332 return accel_link<obj_id>();
333 } else if constexpr (static_cast<std::size_t>(obj_id) > 0) {
334 return accel_link_helper<static_cast<ID>(
335 static_cast<std::size_t>(obj_id) - 1)>(id);
336 } else {
337 __builtin_unreachable();
338 }
339 }
340
342 volume_id m_id{volume_id::e_unknown};
343
345 dindex m_index{dindex_invalid};
346
348 dindex m_transform{dindex_invalid};
349
351 sf_link_type m_sf_links{};
352
354 material_link m_mat_link = {};
355
357 accel_link_type m_accel_links{};
358};
359
360} // namespace detray
constexpr auto set_transform(const dindex trf_idx) -> void
Definition volume_descriptor.hpp:104
constexpr auto index() const -> dindex
Definition volume_descriptor.hpp:92
constexpr auto set_material(const material_id mat_id, const dindex mat_idx) -> void
Set the volume material link using the given material type id.
Definition volume_descriptor.hpp:239
static consteval bool is_volume_id(const object_id id)
Definition volume_descriptor.hpp:82
constexpr auto accel_link() const -> const acc_link_t &
Definition volume_descriptor.hpp:271
constexpr auto full_sf_range() const -> typename sf_link_type::index_type
Definition volume_descriptor.hpp:128
static consteval bool is_surface_id(const object_id id)
Definition volume_descriptor.hpp:75
constexpr auto transform() const -> dindex
Definition volume_descriptor.hpp:100
constexpr auto accel_link(const ID &id) const -> const acc_link_t &
Definition volume_descriptor.hpp:261
constexpr auto accel_link() const -> const accel_link_type &
Definition volume_descriptor.hpp:253
constexpr auto material() const -> const material_link &
Definition volume_descriptor.hpp:228
constexpr auto set_index(const dindex index) -> void
Definition volume_descriptor.hpp:96
constexpr auto sf_link() const -> const sf_link_type &
Definition volume_descriptor.hpp:109
constexpr auto sf_link() const -> const typename sf_link_type::index_type &
Definition volume_descriptor.hpp:115
constexpr auto id() const -> volume_id
Definition volume_descriptor.hpp:88
constexpr dindex to_global_sf_index(dindex sf_idx) const
Definition volume_descriptor.hpp:175
constexpr dindex to_local_sf_index(dindex sf_idx) const
Definition volume_descriptor.hpp:164
constexpr auto set_accel_link(const typename acc_link_t::id_type id, const typename acc_link_t::index_type index) -> void
Set link from.
Definition volume_descriptor.hpp:284
dmulti_index< dindex_range, static_cast< std::size_t >(static_cast< std::underlying_type_t< surface_id > >(surface_id::e_all))> sf_link_type
Definition volume_descriptor.hpp:43
constexpr auto set_accel_link(const ID obj_id, const typename acc_link_t::id_type accel_id, const typename acc_link_t::index_type index) -> void
Set link for a type of surfaces ( obj_id ) from accel_id and index of the acceleration data structure...
Definition volume_descriptor.hpp:296
constexpr volume_descriptor(const volume_id id)
Constructor from shape id.
Definition volume_descriptor.hpp:71
constexpr auto has_material() const -> bool
Definition volume_descriptor.hpp:247
auto update_sf_link(const typename sf_link_type::index_type &other) noexcept -> void
Set or update the index into a geometry container identified by the obj_id.
Definition volume_descriptor.hpp:193
constexpr volume_descriptor()=default
Default constructor builds an ~infinitely long cylinder.
constexpr auto sf_link() -> typename sf_link_type::index_type &
Definition volume_descriptor.hpp:122
material_link material_link
Definition volume_descriptor.hpp:61
constexpr auto set_accel_link(const acc_link_t link) -> void
Set surface finder link from.
Definition volume_descriptor.hpp:277
auto update_sf_link(std::size_t shift, std::size_t n_surfaces=0) noexcept -> void
Set or update the index into a geometry container identified by the obj_id.
Definition volume_descriptor.hpp:215
dmulti_index< accel_link, geo_obj_ids::e_size > accel_link_type
Definition volume_descriptor.hpp:58
friend std::ostream & operator<<(std::ostream &os, const volume_descriptor &v_desc)
Definition volume_descriptor.hpp:314
constexpr auto operator==(const volume_descriptor &rhs) const -> bool
Equality operator.
Definition volume_descriptor.hpp:307
typename material_link::id_type material_id
Definition volume_descriptor.hpp:63
constexpr dindex n_surfaces() const
Definition volume_descriptor.hpp:155
constexpr auto set_material(const material_link &mat_link) -> void
Set the volume material link to.
Definition volume_descriptor.hpp:232
geo_obj_ids object_id
Definition volume_descriptor.hpp:40
Detray include(s).
Definition array_operators.hpp:18
volume_id
Shape of a detector volume.
Definition geometry.hpp:27
detail::index_range< dindex > dindex_range
Index ranges and sequences.
Definition indexing.hpp:24
unsigned int dindex
Default index type.
Definition indexing.hpp:18
constexpr dindex dindex_invalid
Global invalid index definition.
Definition indexing.hpp:21
detail::multi_index< index_t, DIM > dmulti_index
Index that consists of multiple subindices.
Definition indexing.hpp:33