ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
identifier.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#include "detray/utils/bit_encoder.hpp"
16
17// System include(s)
18#include <cstdint>
19#include <ostream>
20#include <utility>
21
22namespace detray::geometry {
23
37 public:
39 using value_t = std::uint_least64_t;
42
45 DETRAY_HOST_DEVICE
46 constexpr explicit identifier(value_t encoded) : m_value(encoded) {}
47
49 constexpr identifier() = default;
50
52 DETRAY_HOST_DEVICE
53 constexpr value_t value() const noexcept { return m_value; }
54
56 DETRAY_HOST_DEVICE
57 constexpr dindex volume() const {
58 return static_cast<dindex>(
59 encoder::template get_bits<k_volume_mask>(m_value));
60 }
61
63 DETRAY_HOST_DEVICE
64 constexpr surface_id id() const {
65 return static_cast<surface_id>(
66 encoder::template get_bits<k_id_mask>(m_value));
67 }
68
70 DETRAY_HOST_DEVICE
71 constexpr dindex index() const {
72 return static_cast<dindex>(
73 encoder::template get_bits<k_index_mask>(m_value));
74 }
75
77 DETRAY_HOST_DEVICE
78 constexpr dindex transform() const {
79 return static_cast<dindex>(
80 encoder::template get_bits<k_transform_mask>(m_value));
81 }
82
84 DETRAY_HOST_DEVICE
85 constexpr dindex extra() const {
86 return static_cast<dindex>(
87 encoder::template get_bits<k_extra_mask>(m_value));
88 }
89
93 DETRAY_HOST_DEVICE
95 encoder::template set_bits<k_volume_mask>(m_value, volume);
96 return *this;
97 }
98
102 DETRAY_HOST_DEVICE
103 constexpr identifier& set_id(surface_id id) {
104 encoder::template set_bits<k_id_mask>(
105 m_value, static_cast<value_t>(
106 static_cast<std::underlying_type_t<surface_id>>(id)));
107 return *this;
108 }
109
113 DETRAY_HOST_DEVICE
115 encoder::template set_bits<k_index_mask>(m_value, index);
116 return *this;
117 }
118
122 DETRAY_HOST_DEVICE
124 encoder::template set_bits<k_transform_mask>(m_value, index);
125 return *this;
126 }
127
131 DETRAY_HOST_DEVICE
133 encoder::template set_bits<k_extra_mask>(m_value, extra);
134 return *this;
135 }
136
140 DETRAY_HOST_DEVICE
141 constexpr bool is_invalid() const noexcept {
142 return encoder::template is_invalid<k_volume_mask, k_id_mask, k_index_mask,
143 k_transform_mask>(m_value);
144 }
145
149 bool operator==(const identifier& rhs) const = default;
150
152 DETRAY_HOST_DEVICE
153 friend constexpr auto operator<=>(const identifier lhs,
154 const identifier rhs) noexcept {
155 const auto l{lhs.index()};
156 const auto r{rhs.index()};
157 if (l < r || (l == r && l < r)) {
158 return std::strong_ordering::less;
159 }
160 if (l > r || (l == r && l > r)) {
161 return std::strong_ordering::greater;
162 }
163 return std::strong_ordering::equivalent;
164 }
165
166 private:
167 // clang-format off
168 static constexpr value_t k_volume_mask = 0xfff0000000000000; // (2^12)-1 = 4095 volumes
169 static constexpr value_t k_id_mask = 0x000f000000000000; // (2^4)-1 = 15 surface categories
170 static constexpr value_t k_index_mask = 0x0000fffff8000000; // (2^21)-1 = 2097151 surfaces
171 static constexpr value_t k_transform_mask = 0x0000000007ffffc0; // (2^21)-1 = 2097151 transforms
172 static constexpr value_t k_extra_mask = 0x000000000000003f; // (2^6)-1 = 63 extra tags
173 // clang-format on
174
175 // The masks together must cover all bits
176 static_assert(k_volume_mask + k_id_mask + k_index_mask + k_transform_mask +
177 k_extra_mask ==
178 ~static_cast<value_t>(0));
179
181 value_t m_value{~static_cast<value_t>(0)};
182
184 DETRAY_HOST
185 friend std::ostream& operator<<(std::ostream& os, const identifier c) {
186 if (c.is_invalid()) {
187 os << "INVALID: ";
188 }
189
190 os << "vol = " << c.volume();
191 os << " | id = " << c.id() << "(" << static_cast<int>(c.id()) << ")";
192 os << " | index = " << c.index();
193 os << " | trf = " << c.transform();
194 os << " | extra = " << c.extra();
195
196 return os;
197 }
198};
199
200} // namespace detray::geometry
201
202namespace std {
203
204// specialize std::hash so identifier can be used e.g. in an unordered_map
205template <>
206struct hash<detray::geometry::identifier> {
207 auto operator()(detray::geometry::identifier gid) const noexcept {
208 return std::hash<detray::geometry::identifier::value_t>()(gid.value());
209 }
210};
211
212} // namespace std
Sets masked bits to a given value.
Definition bit_encoder.hpp:28
Unique identifier for geometry objects, based on the ACTS GeometryIdentifier.
Definition identifier.hpp:36
friend std::ostream & operator<<(std::ostream &os, const identifier c)
Print the surface identifier.
Definition identifier.hpp:185
constexpr identifier & set_extra(value_t extra)
Set the extra identifier.
Definition identifier.hpp:132
constexpr surface_id id() const
Definition identifier.hpp:64
constexpr dindex volume() const
Definition identifier.hpp:57
constexpr dindex extra() const
Definition identifier.hpp:85
detail::bit_encoder< value_t > encoder
Bit encoder used to read and update the packed fields.
Definition identifier.hpp:41
std::uint_least64_t value_t
Unsigned storage type for the packed identifier fields.
Definition identifier.hpp:39
constexpr bool is_invalid() const noexcept
Check whether the identifier is valid to use.
Definition identifier.hpp:141
constexpr identifier & set_id(surface_id id)
Set the surface id.
Definition identifier.hpp:103
constexpr value_t value() const noexcept
Definition identifier.hpp:53
constexpr identifier & set_index(value_t index)
Set the surface index.
Definition identifier.hpp:114
constexpr identifier()=default
Construct default identifier with all values set to 1.
constexpr identifier & set_transform(value_t index)
Set the transform index.
Definition identifier.hpp:123
constexpr identifier & set_volume(value_t volume)
Set the volume index.
Definition identifier.hpp:94
constexpr dindex index() const
Definition identifier.hpp:71
friend constexpr auto operator<=>(const identifier lhs, const identifier rhs) noexcept
Comparison operators.
Definition identifier.hpp:153
constexpr identifier(value_t encoded)
Construct from an already encoded value.
Definition identifier.hpp:46
bool operator==(const identifier &rhs) const =default
Compare the complete packed identifier value.
constexpr dindex transform() const
Definition identifier.hpp:78
Detray include(s).
Definition array_operators.hpp:18
unsigned int dindex
Default index type.
Definition indexing.hpp:18
surface_id
surface type, resolved during navigation.
Definition geometry.hpp:41