File indexing completed on 2026-05-27 07:24:26
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011
0012 #include "detray/definitions/algebra.hpp"
0013 #include "detray/definitions/detail/qualifiers.hpp"
0014 #include "detray/geometry/coordinates/cartesian2D.hpp"
0015 #include "detray/geometry/detail/shape_utils.hpp"
0016
0017
0018 #include <limits>
0019 #include <string_view>
0020
0021 namespace detray::tutorial {
0022
0023
0024
0025
0026
0027 class square2D {
0028 public:
0029
0030 static constexpr std::string_view name = "square2D";
0031
0032 enum boundaries : unsigned int {
0033 e_half_length = 0,
0034 e_size = 1u,
0035 };
0036
0037
0038 template <concepts::scalar scalar_t>
0039 using bounds_type = darray<scalar_t, boundaries::e_size>;
0040
0041
0042 template <concepts::algebra algebra_t>
0043 using local_frame_type = cartesian2D<algebra_t>;
0044
0045
0046 template <typename bool_t>
0047 using result_type = bool_t;
0048
0049
0050 static constexpr std::size_t dim{2u};
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 template <concepts::scalar scalar_t, concepts::point point_t>
0062 DETRAY_HOST_DEVICE inline scalar_t min_dist_to_boundary(
0063 const bounds_type<scalar_t> &bounds, const point_t &loc_p) const {
0064 return math::min(math::fabs(math::fabs(loc_p[0]) - bounds[e_half_length]),
0065 math::fabs(math::fabs(loc_p[1]) - bounds[e_half_length]));
0066 }
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 template <concepts::algebra algebra_t>
0077 DETRAY_HOST_DEVICE constexpr result_type<dbool<algebra_t>> check_boundaries(
0078 const bounds_type<dscalar<algebra_t>> &bounds,
0079 const dtransform3D<algebra_t> &trf, const dpoint3D<algebra_t> &glob_p,
0080 const dscalar<algebra_t> tol =
0081 std::numeric_limits<dscalar<algebra_t>>::epsilon(),
0082 const dscalar<algebra_t> edge_tol = 0.f) const {
0083
0084 const dpoint2D<algebra_t> loc_p =
0085 local_frame_type<algebra_t>::global_to_local(trf, glob_p, {});
0086
0087 return check_boundaries(bounds, loc_p, tol, edge_tol);
0088 }
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 template <concepts::scalar scalar_t, concepts::point point_t>
0102 DETRAY_HOST_DEVICE inline auto check_boundaries(
0103 const bounds_type<scalar_t> &bounds, const point_t &loc_p,
0104 const scalar_t tol = std::numeric_limits<scalar_t>::epsilon()) const {
0105 return (math::fabs(loc_p[0]) <= bounds[e_half_length] + tol &&
0106 math::fabs(loc_p[1]) <= bounds[e_half_length] + tol);
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 template <concepts::algebra algebra_t>
0119 DETRAY_HOST_DEVICE inline darray<dscalar<algebra_t>, 6> local_min_bounds(
0120 const bounds_type<dscalar<algebra_t>> &bounds,
0121 const dscalar<algebra_t> env =
0122 std::numeric_limits<dscalar<algebra_t>>::epsilon()) const {
0123 assert(env > 0.f);
0124 const dscalar<algebra_t> bound{bounds[e_half_length] + env};
0125 return {-bound, -bound, -env, bound, bound, env};
0126 }
0127
0128
0129
0130
0131
0132
0133 template <concepts::scalar scalar_t>
0134 DETRAY_HOST_DEVICE constexpr scalar_t measure(
0135 const bounds_type<scalar_t> &bounds) const {
0136 return area(bounds);
0137 }
0138
0139
0140
0141
0142
0143
0144 template <concepts::scalar scalar_t>
0145 DETRAY_HOST_DEVICE constexpr scalar_t area(
0146 const bounds_type<scalar_t> &bounds) const {
0147 return 4.f * bounds[e_half_length] * bounds[e_half_length];
0148 }
0149
0150
0151
0152
0153
0154
0155
0156 template <concepts::scalar scalar_t>
0157 DETRAY_HOST_DEVICE constexpr bounds_type<scalar_t> merge(
0158 const bounds_type<scalar_t> &bounds,
0159 const bounds_type<scalar_t> &o_bounds) const {
0160 bounds_type<scalar_t> new_bounds{};
0161
0162 new_bounds[e_half_length] =
0163 math::max(bounds[e_half_length], o_bounds[e_half_length]);
0164
0165 return new_bounds;
0166 }
0167
0168
0169 template <concepts::algebra algebra_t>
0170 DETRAY_HOST_DEVICE dpoint3D<algebra_t> centroid(
0171 const bounds_type<dscalar<algebra_t>> & ) const {
0172 return {0.f, 0.f, 0.f};
0173 }
0174
0175
0176
0177
0178
0179
0180
0181 template <concepts::algebra algebra_t>
0182 DETRAY_HOST dvector<dpoint3D<algebra_t>> vertices(
0183 const bounds_type<dscalar<algebra_t>> &bounds, dindex ) const {
0184 using scalar_t = dscalar<algebra_t>;
0185 using point3_t = dpoint3D<algebra_t>;
0186 const scalar_t hl{bounds[e_half_length]};
0187 constexpr scalar_t zero{0.f};
0188
0189
0190 point3_t lh_lc{-hl, -hl, zero};
0191
0192 point3_t rh_lc{hl, -hl, zero};
0193
0194 point3_t rh_uc{hl, hl, zero};
0195
0196 point3_t lh_uc{-hl, hl, zero};
0197
0198
0199 return {lh_lc, rh_lc, rh_uc, lh_uc};
0200 }
0201
0202
0203
0204
0205
0206
0207
0208 template <concepts::scalar scalar_t>
0209 DETRAY_HOST constexpr bool check_consistency(
0210 const bounds_type<scalar_t> &bounds, std::ostream &os) const {
0211 if (constexpr auto tol{10.f * std::numeric_limits<scalar_t>::epsilon()};
0212 bounds[e_half_length] < tol) {
0213 os << "DETRAY ERROR (HOST): Half lengths must be in the range (0, "
0214 "numeric_max)"
0215 << std::endl;
0216 return false;
0217 }
0218
0219 return true;
0220 }
0221 };
0222
0223 }