ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
rectangle2D.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/cartesian2D.hpp"
18#include "detray/geometry/detail/shape_utils.hpp"
19
20// System include(s)
21#include <limits>
22#include <ostream>
23#include <string_view>
24
25namespace detray {
26
31 public:
33 static constexpr std::string_view name = "rectangle2D";
34
36 enum boundaries : unsigned int {
37 e_half_x = 0u,
38 e_half_y = 1u,
39 e_size = 2u,
40 };
41
43 template <concepts::scalar scalar_t>
45
47 template <concepts::algebra algebra_t>
49
51 template <typename bool_t>
52 using result_type = detail::boundary_check_result<bool_t>;
53
55 static constexpr std::size_t dim{2u};
56
66 template <concepts::scalar scalar_t, concepts::point point_t>
67 DETRAY_HOST_DEVICE inline scalar_t min_dist_to_boundary(
68 const bounds_type<scalar_t> &bounds, const point_t &loc_p) const {
69 return math::min(math::fabs(math::fabs(loc_p[0]) - bounds[e_half_x]),
70 math::fabs(math::fabs(loc_p[1]) - bounds[e_half_y]));
71 }
72
82 template <concepts::algebra algebra_t>
83 DETRAY_HOST_DEVICE constexpr result_type<dbool<algebra_t>> check_boundaries(
84 const bounds_type<dscalar<algebra_t>> &bounds,
85 const dtransform3D<algebra_t> &trf, const dpoint3D<algebra_t> &glob_p,
86 const dscalar<algebra_t> tol =
87 std::numeric_limits<dscalar<algebra_t>>::epsilon(),
88 const dscalar<algebra_t> edge_tol = 0.f) const {
89 // Get the full local position
90 const dpoint2D<algebra_t> loc_p =
92
93 return check_boundaries(bounds, loc_p, tol, edge_tol);
94 }
95
106 template <concepts::scalar scalar_t, concepts::point point_t>
107 DETRAY_HOST_DEVICE constexpr auto check_boundaries(
108 const bounds_type<scalar_t> &bounds, const point_t &loc_p,
109 const scalar_t tol = std::numeric_limits<scalar_t>::epsilon(),
110 const scalar_t edge_tol = 0.f) const {
111 const scalar_t loc_0{math::fabs(loc_p[0])};
112 const scalar_t loc_1{math::fabs(loc_p[1])};
113
114 auto inside_mask{(loc_0 <= (bounds[e_half_x] + tol) &&
115 loc_1 <= (bounds[e_half_y] + tol))};
116
117 decltype(inside_mask) inside_edge{false};
118 if (detail::any_of(edge_tol > 0.f)) {
119 const scalar_t full_tol{tol + edge_tol};
120
121 inside_edge = (loc_0 <= (bounds[e_half_x] + full_tol) &&
122 loc_1 <= (bounds[e_half_y] + full_tol));
123 }
124
125 return result_type<decltype(inside_mask)>{inside_mask, inside_edge};
126 }
127
128
134 template <concepts::scalar scalar_t>
135 DETRAY_HOST_DEVICE constexpr scalar_t measure(
136 const bounds_type<scalar_t> &bounds) const {
137 return area(bounds);
138 }
139
145 template <concepts::scalar scalar_t>
146 DETRAY_HOST_DEVICE constexpr scalar_t area(
147 const bounds_type<scalar_t> &bounds) const {
148 return 4.f * bounds[e_half_x] * bounds[e_half_y];
149 }
150
157 template <concepts::scalar scalar_t>
158 DETRAY_HOST_DEVICE constexpr bounds_type<scalar_t> merge(
159 const bounds_type<scalar_t> &bounds,
160 const bounds_type<scalar_t> &o_bounds) const {
161 bounds_type<scalar_t> new_bounds{};
162
163 new_bounds[e_half_x] = math::max(bounds[e_half_x], o_bounds[e_half_x]);
164 new_bounds[e_half_y] = math::max(bounds[e_half_y], o_bounds[e_half_y]);
165
166 return new_bounds;
167 }
168
178 template <concepts::algebra algebra_t>
179 DETRAY_HOST_DEVICE inline darray<dscalar<algebra_t>, 6> local_min_bounds(
180 const bounds_type<dscalar<algebra_t>> &bounds,
181 const dscalar<algebra_t> env =
182 std::numeric_limits<dscalar<algebra_t>>::epsilon()) const {
183 using scalar_t = dscalar<algebra_t>;
184
185 assert(env > 0.f);
186 const scalar_t x_bound{bounds[e_half_x] + env};
187 const scalar_t y_bound{bounds[e_half_y] + env};
188 return {-x_bound, -y_bound, -env, x_bound, y_bound, env};
189 }
190
192 template <concepts::algebra algebra_t>
193 DETRAY_HOST_DEVICE dpoint3D<algebra_t> centroid(
194 const bounds_type<dscalar<algebra_t>> & /*unused*/) const {
195 return {0.f, 0.f, 0.f};
196 }
197
205 template <concepts::algebra algebra_t>
207 const bounds_type<dscalar<algebra_t>> &bounds, dindex /*ignored*/) const {
208 using point3_t = dpoint3D<algebra_t>;
209
210 // left hand lower corner
211 point3_t lh_lc{-bounds[e_half_x], -bounds[e_half_y],
212 static_cast<dscalar<algebra_t>>(0.f)};
213 // right hand lower corner
214 point3_t rh_lc{bounds[e_half_x], -bounds[e_half_y],
215 static_cast<dscalar<algebra_t>>(0.f)};
216 // right hand upper corner
217 point3_t rh_uc{bounds[e_half_x], bounds[e_half_y],
218 static_cast<dscalar<algebra_t>>(0.f)};
219 // left hand upper corner
220 point3_t lh_uc{-bounds[e_half_x], bounds[e_half_y],
221 static_cast<dscalar<algebra_t>>(0.f)};
222
223 // Return the confining vertices
224 return {lh_lc, rh_lc, rh_uc, lh_uc};
225 }
226
233 template <concepts::scalar scalar_t>
234 DETRAY_HOST constexpr bool check_consistency(
235 const bounds_type<scalar_t> &bounds, std::ostream &os) const {
236 if (constexpr auto tol{10.f * std::numeric_limits<scalar_t>::epsilon()};
237 bounds[e_half_x] < tol || bounds[e_half_y] < tol) {
238 os << "DETRAY ERROR (HOST): Half lengths must be in the range (0, "
239 "numeric_max)"
240 << std::endl;
241 return false;
242 }
243
244 return true;
245 }
246};
247
248} // namespace detray
Geometrical shape of a rectangle2D.
Definition rectangle2D.hpp:30
cartesian2D< algebra_t > local_frame_type
Local coordinate frame for boundary checks.
Definition rectangle2D.hpp:48
darray< scalar_t, boundaries::e_size > bounds_type
Container definition for the shape boundary values.
Definition rectangle2D.hpp:44
constexpr bool check_consistency(const bounds_type< scalar_t > &bounds, std::ostream &os) const
Check consistency of boundary values.
Definition rectangle2D.hpp:234
dvector< dpoint3D< algebra_t > > vertices(const bounds_type< dscalar< algebra_t > > &bounds, dindex) const
Generate vertices in local cartesian frame.
Definition rectangle2D.hpp:206
static constexpr std::string_view name
The name for this shape.
Definition rectangle2D.hpp:33
constexpr scalar_t area(const bounds_type< scalar_t > &bounds) const
The area of a the shape.
Definition rectangle2D.hpp:146
static constexpr std::size_t dim
Dimension of the local coordinate system.
Definition rectangle2D.hpp:55
dpoint3D< algebra_t > centroid(const bounds_type< dscalar< algebra_t > > &) const
Definition rectangle2D.hpp:193
boundaries
Indices describing the entries in the shape boundary array.
Definition rectangle2D.hpp:36
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 rectangle2D.hpp:67
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 edge_tol=0.f) const
Definition rectangle2D.hpp:107
detail::boundary_check_result< bool_t > result_type
Result type of a boundary check.
Definition rectangle2D.hpp:52
constexpr scalar_t measure(const bounds_type< scalar_t > &bounds) const
Measure of the shape: Area.
Definition rectangle2D.hpp:135
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 rectangle2D.hpp:179
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 > edge_tol=0.f) const
Check boundary values for a local point.
Definition rectangle2D.hpp:83
constexpr bounds_type< scalar_t > merge(const bounds_type< scalar_t > &bounds, const bounds_type< scalar_t > &o_bounds) const
Merge two rectangle shapes.
Definition rectangle2D.hpp:158
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
get_point2D_t< A, T > dpoint2D
Two-dimensional point type for algebra A and value type T.
Definition algebra.hpp:68
Projection into a 2D cartesian coordinate frame.
Definition cartesian2D.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 2D cartesian point.
Definition cartesian2D.hpp:54