ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
cuboid3D.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/containers.hpp"
14#include "detray/definitions/detail/qualifiers.hpp"
15#include "detray/definitions/indexing.hpp"
16#include "detray/definitions/math.hpp"
17#include "detray/geometry/coordinates/cartesian3D.hpp"
18#include "detray/geometry/detail/shape_utils.hpp"
19
20// System include(s)
21#include <limits>
22#include <ostream>
23#include <stdexcept>
24#include <string_view>
25
26namespace detray {
27
32class cuboid3D {
33 public:
35 static constexpr std::string_view name = "cuboid3D";
36
38 enum boundaries : unsigned int {
39 e_min_x = 0u,
40 e_min_y = 1u,
41 e_min_z = 2u,
42 e_max_x = 3u,
43 e_max_y = 4u,
44 e_max_z = 5u,
45 e_size = 6u,
46 };
47
49 template <concepts::scalar scalar_t>
51
53 template <concepts::algebra algebra_t>
55
57 template <typename bool_t>
58 using result_type = bool_t;
59
61 static constexpr std::size_t dim{3u};
62
72 template <concepts::scalar scalar_t, concepts::point point_t>
73 DETRAY_HOST_DEVICE inline scalar_t min_dist_to_boundary(
74 const bounds_type<scalar_t> &bounds, const point_t &loc_p) const {
75 const scalar_t min_x_dist =
76 math::min(math::fabs(loc_p[0] - bounds[e_min_x]),
77 math::fabs(bounds[e_max_x] - loc_p[0]));
78 const scalar_t min_y_dist =
79 math::min(math::fabs(loc_p[1] - bounds[e_min_y]),
80 math::fabs(bounds[e_max_y] - loc_p[1]));
81 const scalar_t min_z_dist =
82 math::min(math::fabs(loc_p[2] - bounds[e_min_z]),
83 math::fabs(bounds[e_max_z] - loc_p[2]));
84
85 // Use the chord for the phi distance
86 return math::min(math::min(min_x_dist, min_y_dist), min_z_dist);
87 }
88
97 template <concepts::algebra algebra_t>
98 DETRAY_HOST_DEVICE constexpr result_type<dbool<algebra_t>> check_boundaries(
99 const bounds_type<dscalar<algebra_t>> &bounds,
100 const dtransform3D<algebra_t> &trf, const dpoint3D<algebra_t> &glob_p,
101 const dscalar<algebra_t> tol =
102 std::numeric_limits<dscalar<algebra_t>>::epsilon(),
103 const dscalar<algebra_t> /*edge_tol*/ = 0.f) const {
104 // Get the full local position
105 const dpoint3D<algebra_t> loc_p =
107
108 return check_boundaries(bounds, loc_p, tol);
109 }
110
121 template <concepts::scalar scalar_t, concepts::point3D point_t>
122 DETRAY_HOST_DEVICE constexpr auto check_boundaries(
123 const bounds_type<scalar_t> &bounds, const point_t &loc_p,
124 const scalar_t tol = std::numeric_limits<scalar_t>::epsilon(),
125 const scalar_t /*edge_tol*/ = 0.f) const {
126 return ((bounds[e_min_x] - tol) <= loc_p[0] &&
127 (bounds[e_min_y] - tol) <= loc_p[1] &&
128 (bounds[e_min_x] - tol) <= loc_p[2] &&
129 loc_p[0] <= (bounds[e_max_x] + tol) &&
130 loc_p[1] <= (bounds[e_max_y] + tol) &&
131 loc_p[2] <= (bounds[e_max_z] + tol));
132 }
133
134
140 template <concepts::scalar scalar_t>
141 DETRAY_HOST_DEVICE constexpr scalar_t measure(
142 const bounds_type<scalar_t> &bounds) const {
143 return volume(bounds);
144 }
145
151 template <concepts::scalar scalar_t>
152 DETRAY_HOST_DEVICE constexpr scalar_t volume(
153 const bounds_type<scalar_t> &bounds) const {
154 return (bounds[e_max_x] - bounds[e_min_x]) *
155 (bounds[e_max_y] - bounds[e_min_y]) *
156 (bounds[e_max_z] - bounds[e_min_z]);
157 }
158
165 template <concepts::scalar scalar_t>
166 DETRAY_HOST_DEVICE constexpr bounds_type<scalar_t> merge(
167 const bounds_type<scalar_t> &bounds,
168 const bounds_type<scalar_t> &o_bounds) const {
169 bounds_type<scalar_t> new_bounds{};
170
171 new_bounds[e_min_x] = math::min(bounds[e_min_x], o_bounds[e_min_x]);
172 new_bounds[e_min_y] = math::min(bounds[e_min_y], o_bounds[e_min_y]);
173 new_bounds[e_min_z] = math::min(bounds[e_min_z], o_bounds[e_min_z]);
174 new_bounds[e_max_x] = math::max(bounds[e_max_x], o_bounds[e_max_x]);
175 new_bounds[e_max_y] = math::max(bounds[e_max_y], o_bounds[e_max_y]);
176 new_bounds[e_max_z] = math::max(bounds[e_max_z], o_bounds[e_max_z]);
177
178 return new_bounds;
179 }
180
190 template <concepts::algebra algebra_t>
191 DETRAY_HOST_DEVICE inline darray<dscalar<algebra_t>, 6> local_min_bounds(
192 const bounds_type<dscalar<algebra_t>> &bounds,
193 const dscalar<algebra_t> env =
194 std::numeric_limits<dscalar<algebra_t>>::epsilon()) const {
195 assert(env > 0.f);
196 bounds_type<dscalar<algebra_t>> o_bounds{bounds};
197 for (unsigned int i{0}; i < 3u; ++i) {
198 o_bounds[i] -= env;
199 o_bounds[i + 3u] += env;
200 }
201 return o_bounds;
202 }
203
206 template <concepts::algebra algebra_t>
207 DETRAY_HOST_DEVICE dpoint3D<algebra_t> centroid(
208 const bounds_type<dscalar<algebra_t>> &bounds) const {
209 return 0.5f * dpoint3D<algebra_t>{bounds[e_min_x] + bounds[e_max_x],
210 bounds[e_min_y] + bounds[e_max_y],
211 bounds[e_min_z] + bounds[e_max_z]};
212 }
213
220 template <concepts::algebra algebra_t>
222 const bounds_type<dscalar<algebra_t>> & /*unused*/,
223 dindex /*unused*/) const {
224 throw std::runtime_error(
225 "Vertex generation for cuboids is not implemented");
226 return {};
227 }
228
235 template <concepts::scalar scalar_t>
236 DETRAY_HOST constexpr bool check_consistency(
237 const bounds_type<scalar_t> &bounds, std::ostream &os) const {
238 constexpr auto tol{10.f * std::numeric_limits<scalar_t>::epsilon()};
239
240 if (bounds[e_min_x] >= bounds[e_max_x] ||
241 math::fabs(bounds[e_min_x] - bounds[e_max_x]) < tol) {
242 os << "DETRAY ERROR (HOST): Min x must be smaller than max x.";
243 return false;
244 }
245 if (bounds[e_min_y] >= bounds[e_max_y] ||
246 math::fabs(bounds[e_min_y] - bounds[e_max_y]) < tol) {
247 os << "DETRAY ERROR (HOST): Min y must be smaller than max y.";
248 return false;
249 }
250 if (bounds[e_min_z] >= bounds[e_max_z] ||
251 math::fabs(bounds[e_min_z] - bounds[e_max_z]) < tol) {
252 os << "DETRAY ERROR (HOST): Min z must be smaller than max z.";
253 return false;
254 }
255
256 return true;
257 }
258};
259
260} // namespace detray
Geometrical shape of a full 3D cuboid.
Definition cuboid3D.hpp:32
dpoint3D< algebra_t > centroid(const bounds_type< dscalar< algebra_t > > &bounds) const
Definition cuboid3D.hpp:207
constexpr bool check_consistency(const bounds_type< scalar_t > &bounds, std::ostream &os) const
Check consistency of boundary values.
Definition cuboid3D.hpp:236
static constexpr std::string_view name
The name for this shape.
Definition cuboid3D.hpp:35
constexpr scalar_t measure(const bounds_type< scalar_t > &bounds) const
Measure of the shape: Volume.
Definition cuboid3D.hpp:141
bool_t result_type
Result type of a boundary check.
Definition cuboid3D.hpp:58
scalar_t min_dist_to_boundary(const bounds_type< scalar_t > &bounds, const point_t &loc_p) const
Find the minimum distance to any boundary.
Definition cuboid3D.hpp:73
constexpr auto check_boundaries(const bounds_type< scalar_t > &bounds, const point_t &loc_p, const scalar_t tol=std::numeric_limits< scalar_t >::epsilon(), const scalar_t=0.f) const
Definition cuboid3D.hpp:122
dvector< dpoint3D< algebra_t > > vertices(const bounds_type< dscalar< algebra_t > > &, dindex) const
Generate vertices in local cartesian frame.
Definition cuboid3D.hpp:221
darray< scalar_t, boundaries::e_size > bounds_type
Container definition for the shape boundary values.
Definition cuboid3D.hpp:50
boundaries
Indices describing the entries in the shape boundary array.
Definition cuboid3D.hpp:38
cartesian3D< algebra_t > local_frame_type
Local coordinate frame for boundary checks.
Definition cuboid3D.hpp:54
static constexpr std::size_t dim
Dimension of the local coordinate system.
Definition cuboid3D.hpp:61
constexpr result_type< dbool< algebra_t > > check_boundaries(const bounds_type< dscalar< algebra_t > > &bounds, const dtransform3D< algebra_t > &trf, const dpoint3D< algebra_t > &glob_p, const dscalar< algebra_t > tol=std::numeric_limits< dscalar< algebra_t > >::epsilon(), const dscalar< algebra_t >=0.f) const
Check boundary values for a local point.
Definition cuboid3D.hpp:98
constexpr bounds_type< scalar_t > merge(const bounds_type< scalar_t > &bounds, const bounds_type< scalar_t > &o_bounds) const
Merge two 3D cuboid shapes.
Definition cuboid3D.hpp:166
darray< dscalar< algebra_t >, 6 > local_min_bounds(const bounds_type< dscalar< algebra_t > > &bounds, const dscalar< algebra_t > env=std::numeric_limits< dscalar< algebra_t > >::epsilon()) const
Lower and upper point for minimal axis aligned bounding box.
Definition cuboid3D.hpp:191
constexpr scalar_t volume(const bounds_type< scalar_t > &bounds) const
The volume of a the shape.
Definition cuboid3D.hpp:152
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
std::array< value_t, kDIM > darray
Fixed-size array container.
Definition containers.hpp:30
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
Projection into a 3D cartesian coordinate frame.
Definition cartesian3D.hpp:19
static loc_point global_to_local(const transform3_type &trf, const point3_type &p, const vector3_type &)
This method transforms a point from a global cartesian 3D frame to a local 3D cartesian point Convert...
Definition cartesian3D.hpp:57