ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
mask.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/algebra.hpp"
13#include "detray/definitions/detail/qualifiers.hpp"
14#include "detray/definitions/indexing.hpp"
15#include "detray/geometry/concepts.hpp"
16#include "detray/geometry/shapes/cuboid3D.hpp"
17#include "detray/navigation/intersection/intersection.hpp"
18
19// System include(s)
20#include <algorithm>
21#include <cassert>
22#include <limits>
23#include <ostream>
24#include <sstream>
25#include <string>
26#include <vector>
27
28namespace detray {
29
43template <typename shape_t, concepts::algebra algebra_t,
44 typename links_t = std::uint_least16_t>
46class mask {
47 public:
49 using algebra_type = algebra_t;
60
62 using links_type = links_t;
64 using shape = shape_t;
66 using boundaries = typename shape::boundaries;
68 using mask_values = typename shape::template bounds_type<scalar_type>;
70 using local_frame = typename shape::template local_frame_type<algebra_t>;
72 using result_type = typename shape::template result_type<dbool<algebra_t>>;
73
75 constexpr mask() = default;
76
79 template <typename... Args>
80 requires(sizeof...(Args) == shape::boundaries::e_size)
81 DETRAY_HOST_DEVICE explicit constexpr mask(const links_type& link,
82 Args&&... args)
83 : _values({{std::forward<Args>(args)...}}), _volume_link(link) {}
84
87 DETRAY_HOST_DEVICE
88 constexpr mask(const mask_values& values, const links_type& link)
89 : _values{values}, _volume_link{link} {}
90
93 DETRAY_HOST mask(const std::vector<scalar_type>& values,
94 const links_type& link)
95 : _volume_link(link) {
96 assert(values.size() == boundaries::e_size &&
97 " Given number of boundaries does not match mask shape.");
98 std::ranges::copy(values, std::begin(_values));
99 }
100
105 DETRAY_HOST
107 _values = rhs;
108 return (*this);
109 }
110
116 bool operator==(const mask& rhs) const = default;
117
122 DETRAY_HOST_DEVICE
123 constexpr auto operator[](const std::size_t value_index) -> scalar_type& {
124 return _values[value_index];
125 }
126
131 DETRAY_HOST_DEVICE
132 constexpr auto operator[](const std::size_t value_index) const
133 -> scalar_type {
134 return _values[value_index];
135 }
136
138 DETRAY_HOST_DEVICE
139 static consteval auto get_shape() -> shape { return shape{}; }
140
142 DETRAY_HOST_DEVICE static consteval local_frame get_local_frame() {
143 return local_frame{};
144 }
145
150 DETRAY_HOST_DEVICE constexpr static auto to_local_frame3D(
151 const transform3_type& trf, const point3_type& glob_p,
152 const point3_type& glob_dir = {}) -> point3_type {
153 return get_local_frame().global_to_local_3D(trf, glob_p, glob_dir);
154 }
155
160 DETRAY_HOST_DEVICE constexpr static auto to_local_frame(
161 const transform3_type& trf, const point3_type& glob_p,
162 const point3_type& glob_dir = {}) -> point2_type {
163 return get_local_frame().global_to_local(trf, glob_p, glob_dir);
164 }
165
169 DETRAY_HOST_DEVICE constexpr static auto to_global_frame(
170 const transform3_type& trf, const point3_type& loc) -> point3_type {
171 return get_local_frame().local_to_global(trf, loc);
172 }
173
178 DETRAY_HOST_DEVICE constexpr auto to_global_frame(
179 const transform3_type& trf, const point2_type& loc,
180 const vector3_type& dir) const -> point3_type {
181 return get_local_frame().local_to_global(trf, *this, loc, dir);
182 }
183
186
199 template <concepts::point point_t>
200 DETRAY_HOST_DEVICE constexpr result_type resolve(
201 const point_t& loc_p,
202 const scalar_type tol = std::numeric_limits<scalar_type>::epsilon(),
203 const scalar_type edge_tol = 0.f) const {
204 return get_shape().check_boundaries(_values, loc_p, tol, edge_tol);
205 }
206
213 DETRAY_HOST_DEVICE constexpr result_type resolve(
214 const transform3_type& trf, const point3_type& glob_p,
215 const scalar_type tol = std::numeric_limits<scalar_type>::epsilon(),
216 const scalar_type edge_tol = 0.f) const {
217 return get_shape().template check_boundaries<algebra_type>(
218 _values, trf, glob_p, tol, edge_tol);
219 }
220
225 template <concepts::point point_t>
226 DETRAY_HOST_DEVICE constexpr dbool<algebra_t> is_inside(
227 const point_t& loc_p,
228 const scalar_type tol =
229 std::numeric_limits<scalar_type>::epsilon()) const {
231 get_shape().check_boundaries(_values, loc_p, tol, scalar_type{0.f}));
232 }
233
239 DETRAY_HOST_DEVICE constexpr dbool<algebra_type> is_inside(
240 const transform3_type& trf, const point3_type& glob_p,
241 const scalar_type tol =
242 std::numeric_limits<scalar_type>::epsilon()) const {
244 get_shape().template check_boundaries<algebra_type>(
245 _values, trf, glob_p, tol, scalar_type{0.f}));
246 }
247
248
250 DETRAY_HOST_DEVICE
251 auto values() const -> const mask_values& { return _values; }
252
254 DETRAY_HOST_DEVICE
255 auto volume_link() const -> const links_type& { return _volume_link; }
256
258 DETRAY_HOST_DEVICE
259 auto volume_link() -> links_type& { return _volume_link; }
260
263 DETRAY_HOST_DEVICE constexpr auto measure() const -> scalar_type {
264 return get_shape().measure(_values);
265 }
266
268 template <typename S = shape_t>
269 requires(S::dim == 2)
270 DETRAY_HOST_DEVICE constexpr auto area() const -> scalar_type {
271 return get_shape().area(_values);
272 }
273
275 template <typename S = shape_t>
276 requires(S::dim == 3)
277 DETRAY_HOST_DEVICE constexpr auto volume() const -> scalar_type {
278 return get_shape().volume(_values);
279 }
280
282 DETRAY_HOST_DEVICE auto centroid() const {
283 return get_shape().template centroid<algebra_t>(_values);
284 }
285
291 DETRAY_HOST_DEVICE
292 auto min_dist_to_boundary(const point3_type& loc_p) const -> scalar_type {
293 return get_shape().min_dist_to_boundary(_values, loc_p);
294 }
295
303 DETRAY_HOST_DEVICE
305 const scalar_type env = std::numeric_limits<scalar_type>::epsilon()) const
306 -> mask<cuboid3D, algebra_t, unsigned int> {
307 const auto bounds =
308 get_shape().template local_min_bounds<algebra_t>(_values, env);
309 static_assert(bounds.size() == cuboid3D::e_size,
310 "Shape returns incompatible bounds for bound box");
311 return {bounds, std::numeric_limits<unsigned int>::max()};
312 }
313
321 DETRAY_HOST
322 auto vertices(const dindex n_seg) const -> dvector<point3_type> {
323 return get_shape().template vertices<algebra_t>(_values, n_seg);
324 }
325
335 DETRAY_HOST
336 friend mask operator+(const mask& left, const mask& right) {
337 links_type vol_link{left.volume_link() == right.volume_link()
338 ? left.volume_link()
339 : detail::invalid_value<links_type>()};
340 mask_values merged_vals =
341 mask::get_shape().merge(left.values(), right.values());
342
343 return {std::move(merged_vals), vol_link};
344 }
345
348 DETRAY_HOST
349 constexpr bool self_check(std::ostream& os) const {
350 const bool result = get_shape().check_consistency(_values, os);
351
352 if (!result) {
353 os << to_string();
354 }
355
356 return result;
357 }
358
360 DETRAY_HOST
361 auto to_string() const -> std::string {
362 std::stringstream ss;
363 ss << std::string(shape::name);
364 for (const auto& v : _values) {
365 ss << ", " << v;
366 }
367 return ss.str();
368 }
369
371 DETRAY_HOST
372 friend std::ostream& operator<<(std::ostream& os, const mask& m) {
373 os << m.to_string();
374 return os;
375 }
376
377 private:
378 mask_values _values{};
379 links_type _volume_link{std::numeric_limits<links_type>::max()};
380};
381
382} // namespace detray
Geometrical shape of a full 3D cuboid.
Definition cuboid3D.hpp:32
constexpr result_type resolve(const point_t &loc_p, const scalar_type tol=std::numeric_limits< scalar_type >::epsilon(), const scalar_type edge_tol=0.f) const
Mask this shape onto a surface.
Definition mask.hpp:200
constexpr auto volume() const -> scalar_type
Definition mask.hpp:277
auto vertices(const dindex n_seg) const -> dvector< point3_type >
Vertices of the mask in local cartesian coordinates.
Definition mask.hpp:322
dscalar< algebra_t > scalar_type
Scalar type used for boundary values.
Definition mask.hpp:51
algebra_t algebra_type
Linear algebra types.
Definition mask.hpp:49
auto centroid() const
Definition mask.hpp:282
constexpr bool self_check(std::ostream &os) const
Definition mask.hpp:349
constexpr auto operator[](const std::size_t value_index) const -> scalar_type
Subscript operator - const.
Definition mask.hpp:132
static constexpr auto to_local_frame(const transform3_type &trf, const point3_type &glob_p, const point3_type &glob_dir={}) -> point2_type
Definition mask.hpp:160
static consteval local_frame get_local_frame()
Definition mask.hpp:142
constexpr dbool< algebra_t > is_inside(const point_t &loc_p, const scalar_type tol=std::numeric_limits< scalar_type >::epsilon()) const
Definition mask.hpp:226
auto operator=(const mask_values &rhs) -> mask< shape_t, algebra_t, links_t > &
Assignment operator from an array, convenience function.
Definition mask.hpp:106
dpoint3D< algebra_t > point3_type
Three-dimensional point type in the algebra backend.
Definition mask.hpp:55
typename shape::template result_type< dbool< algebra_t > > result_type
Result type returned by shape boundary checks.
Definition mask.hpp:72
bool operator==(const mask &rhs) const =default
Equality operator.
static consteval auto get_shape() -> shape
Definition mask.hpp:139
auto volume_link() const -> const links_type &
Definition mask.hpp:255
constexpr dbool< algebra_type > is_inside(const transform3_type &trf, const point3_type &glob_p, const scalar_type tol=std::numeric_limits< scalar_type >::epsilon()) const
Definition mask.hpp:239
constexpr mask(const mask_values &values, const links_type &link)
Constructor from mask boundary array.
Definition mask.hpp:88
auto min_dist_to_boundary(const point3_type &loc_p) const -> scalar_type
Find the minimum distance to any boundary.
Definition mask.hpp:292
typename shape::template local_frame_type< algebra_t > local_frame
Coordinate frame used for shape boundary checks.
Definition mask.hpp:70
constexpr mask()=default
Default constructor.
auto volume_link() -> links_type &
Definition mask.hpp:259
dtransform3D< algebra_t > transform3_type
Transform type used to map between local and global coordinates.
Definition mask.hpp:59
constexpr auto operator[](const std::size_t value_index) -> scalar_type &
Subscript operator - non-const.
Definition mask.hpp:123
static constexpr auto to_global_frame(const transform3_type &trf, const point3_type &loc) -> point3_type
Definition mask.hpp:169
friend mask operator+(const mask &left, const mask &right)
Merge two masks to a bigger one of the same shape.
Definition mask.hpp:336
typename shape::boundaries boundaries
Boundary index enum supplied by the shape.
Definition mask.hpp:66
constexpr mask(const links_type &link, Args &&... args)
Constructor from single mask boundary values.
Definition mask.hpp:81
auto to_string() const -> std::string
Definition mask.hpp:361
static constexpr auto to_local_frame3D(const transform3_type &trf, const point3_type &glob_p, const point3_type &glob_dir={}) -> point3_type
Definition mask.hpp:150
typename shape::template bounds_type< scalar_type > mask_values
Array type that stores the shape boundary values.
Definition mask.hpp:68
dpoint2D< algebra_t > point2_type
Two-dimensional point type in the algebra backend.
Definition mask.hpp:53
dvector3D< algebra_t > vector3_type
Three-dimensional vector type in the algebra backend.
Definition mask.hpp:57
constexpr auto area() const -> scalar_type
Definition mask.hpp:270
constexpr auto measure() const -> scalar_type
Definition mask.hpp:263
auto local_min_bounds(const scalar_type env=std::numeric_limits< scalar_type >::epsilon()) const -> mask< cuboid3D, algebra_t, unsigned int >
Lower and upper point for minimum axis aligned bounding box.
Definition mask.hpp:304
constexpr result_type resolve(const transform3_type &trf, const point3_type &glob_p, const scalar_type tol=std::numeric_limits< scalar_type >::epsilon(), const scalar_type edge_tol=0.f) const
Definition mask.hpp:213
links_t links_type
Type used to link this mask to its volume.
Definition mask.hpp:62
friend std::ostream & operator<<(std::ostream &os, const mask &m)
Definition mask.hpp:372
auto values() const -> const mask_values &
Definition mask.hpp:251
mask(const std::vector< scalar_type > &values, const links_type &link)
Constructor from mask boundary vector.
Definition mask.hpp:93
shape_t shape
Geometric shape whose boundaries define this mask.
Definition mask.hpp:64
constexpr auto to_global_frame(const transform3_type &trf, const point2_type &loc, const vector3_type &dir) const -> point3_type
Definition mask.hpp:178
Algebra plugin concept.
Definition concepts.hpp:147
Concept for a geometrical shape.
Definition concepts.hpp:54
Detray include(s).
Definition array_operators.hpp:18
get_scalar_t< A, T > dscalar
Scalar type for algebra A and value type T.
Definition algebra.hpp:64
vecmem::vector< value_t > dvector
Vector container backed by vecmem.
Definition containers.hpp:34
unsigned int dindex
Default index type.
Definition indexing.hpp:18
get_boolean_t< A > dbool
Boolean type associated with the algebra backend.
Definition algebra.hpp:52
const auto & get(const detray::tuple< Ts... > &t) noexcept
Get a tuple element by index from a const tuple.
Definition tuple.hpp:152
get_vector3D_t< A, T > dvector3D
Three-dimensional vector type for algebra A and value type T.
Definition algebra.hpp:80
get_transform3D_t< A, T > dtransform3D
Three-dimensional transform type for algebra A and value type T.
Definition algebra.hpp:84
get_point3D_t< A, T > dpoint3D
Three-dimensional point type for algebra A and value type T.
Definition algebra.hpp:72
get_point2D_t< A, T > dpoint2D
Two-dimensional point type for algebra A and value type T.
Definition algebra.hpp:68