File indexing completed on 2025-01-18 10:05:57
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010 #include "corecel/Types.hh"
0011 #include "corecel/cont/Array.hh"
0012 #include "corecel/cont/Span.hh"
0013 #include "corecel/math/ArrayUtils.hh"
0014 #include "orange/OrangeTypes.hh"
0015
0016 #include "detail/QuadraticSolver.hh"
0017
0018 namespace celeritas
0019 {
0020
0021 template<Axis T>
0022 class CylCentered;
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 template<Axis T>
0068 class CylAligned
0069 {
0070 public:
0071
0072
0073 using Intersections = Array<real_type, 2>;
0074 using StorageSpan = Span<real_type const, 3>;
0075
0076
0077 private:
0078 static constexpr Axis U{T == Axis::x ? Axis::y : Axis::x};
0079 static constexpr Axis V{T == Axis::z ? Axis::y : Axis::z};
0080
0081 public:
0082
0083
0084
0085 static CELER_CONSTEXPR_FUNCTION SurfaceType surface_type();
0086
0087
0088 static CELER_CONSTEXPR_FUNCTION bool simple_safety() { return false; }
0089
0090
0091
0092 static CELER_CONSTEXPR_FUNCTION Axis t_axis() { return T; }
0093 static CELER_CONSTEXPR_FUNCTION Axis u_axis() { return U; }
0094 static CELER_CONSTEXPR_FUNCTION Axis v_axis() { return V; }
0095
0096
0097 public:
0098
0099
0100
0101 static CylAligned from_radius_sq(Real3 const& origin, real_type rsq);
0102
0103
0104 inline CELER_FUNCTION CylAligned(Real3 const& origin, real_type radius);
0105
0106
0107 template<class R>
0108 explicit inline CELER_FUNCTION CylAligned(Span<R, StorageSpan::extent>);
0109
0110
0111 explicit CylAligned(CylCentered<T> const& other) noexcept;
0112
0113
0114
0115
0116 CELER_FUNCTION real_type origin_u() const { return origin_u_; }
0117
0118
0119 CELER_FUNCTION real_type origin_v() const { return origin_v_; }
0120
0121
0122 CELER_FUNCTION real_type radius_sq() const { return radius_sq_; }
0123
0124
0125 CELER_FUNCTION StorageSpan data() const { return {&origin_u_, 3}; }
0126
0127
0128 Real3 calc_origin() const;
0129
0130
0131
0132
0133 inline CELER_FUNCTION SignedSense calc_sense(Real3 const& pos) const;
0134
0135
0136 inline CELER_FUNCTION Intersections calc_intersections(
0137 Real3 const& pos, Real3 const& dir, SurfaceState on_surface) const;
0138
0139
0140 inline CELER_FUNCTION Real3 calc_normal(Real3 const& pos) const;
0141
0142 private:
0143
0144 real_type origin_u_;
0145 real_type origin_v_;
0146
0147
0148 real_type radius_sq_;
0149
0150
0151 CylAligned() = default;
0152 };
0153
0154
0155
0156
0157
0158 using CylX = CylAligned<Axis::x>;
0159 using CylY = CylAligned<Axis::y>;
0160 using CylZ = CylAligned<Axis::z>;
0161
0162
0163
0164
0165
0166
0167
0168 template<Axis T>
0169 CELER_CONSTEXPR_FUNCTION SurfaceType CylAligned<T>::surface_type()
0170 {
0171 return T == Axis::x ? SurfaceType::cx
0172 : T == Axis::y ? SurfaceType::cy
0173 : T == Axis::z ? SurfaceType::cz
0174 : SurfaceType::size_;
0175 }
0176
0177
0178
0179
0180
0181 template<Axis T>
0182 CELER_FUNCTION CylAligned<T>::CylAligned(Real3 const& origin, real_type radius)
0183 : origin_u_{origin[to_int(U)]}
0184 , origin_v_{origin[to_int(V)]}
0185 , radius_sq_{ipow<2>(radius)}
0186 {
0187 CELER_EXPECT(radius > 0);
0188 }
0189
0190
0191
0192
0193
0194 template<Axis T>
0195 template<class R>
0196 CELER_FUNCTION CylAligned<T>::CylAligned(Span<R, StorageSpan::extent> data)
0197 : origin_u_{data[0]}, origin_v_{data[1]}, radius_sq_{data[2]}
0198 {
0199 }
0200
0201
0202
0203
0204
0205 template<Axis T>
0206 CELER_FUNCTION SignedSense CylAligned<T>::calc_sense(Real3 const& pos) const
0207 {
0208 real_type const u = pos[to_int(U)] - origin_u_;
0209 real_type const v = pos[to_int(V)] - origin_v_;
0210
0211 return real_to_sense(ipow<2>(u) + ipow<2>(v) - radius_sq_);
0212 }
0213
0214
0215
0216
0217
0218 template<Axis T>
0219 CELER_FUNCTION auto
0220 CylAligned<T>::calc_intersections(Real3 const& pos,
0221 Real3 const& dir,
0222 SurfaceState on_surface) const -> Intersections
0223 {
0224
0225 real_type const a = 1 - ipow<2>(dir[to_int(T)]);
0226
0227 if (a < ipow<2>(Tolerance<>::sqrt_quadratic()))
0228 {
0229
0230 return {no_intersection(), no_intersection()};
0231 }
0232
0233 real_type const u = pos[to_int(U)] - origin_u_;
0234 real_type const v = pos[to_int(V)] - origin_v_;
0235
0236
0237 detail::QuadraticSolver solve_quadric(
0238 a, dir[to_int(U)] * u + dir[to_int(V)] * v);
0239 if (on_surface == SurfaceState::on)
0240 {
0241
0242 return solve_quadric();
0243 }
0244
0245
0246 return solve_quadric(ipow<2>(u) + ipow<2>(v) - radius_sq_);
0247 }
0248
0249
0250
0251
0252
0253 template<Axis T>
0254 CELER_FUNCTION Real3 CylAligned<T>::calc_normal(Real3 const& pos) const
0255 {
0256 Real3 norm{0, 0, 0};
0257
0258 norm[to_int(U)] = pos[to_int(U)] - origin_u_;
0259 norm[to_int(V)] = pos[to_int(V)] - origin_v_;
0260
0261 return make_unit_vector(norm);
0262 }
0263
0264
0265 }