File indexing completed on 2026-11-21 10:32:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #ifndef VECGEOM_VOLUMES_KERNEL_GENTRAPIMPLEMENTATION_H_
0024 #define VECGEOM_VOLUMES_KERNEL_GENTRAPIMPLEMENTATION_H_
0025
0026 #include "VecGeom/base/Global.h"
0027 #include "VecGeom/base/Vector2D.h"
0028
0029 #include "VecGeom/volumes/kernel/GenericKernels.h"
0030 #include "VecGeom/volumes/kernel/BoxImplementation.h"
0031 #include "VecGeom/volumes/SurfaceHitView.h"
0032 #include "VecGeom/volumes/UnplacedGenTrap.h"
0033
0034 #include <iostream>
0035
0036 namespace vecgeom {
0037
0038 VECGEOM_DEVICE_FORWARD_DECLARE(struct GenTrapImplementation;);
0039 VECGEOM_DEVICE_DECLARE_CONV(struct, GenTrapImplementation);
0040
0041 inline namespace VECGEOM_IMPL_NAMESPACE {
0042
0043 class PlacedGenTrap;
0044 class UnplacedGenTrap;
0045
0046 template <typename T>
0047 struct GenTrapStruct;
0048
0049 struct GenTrapImplementation {
0050
0051 using Vertex_t = Vector3D<Precision>;
0052 using PlacedShape_t = PlacedGenTrap;
0053 using UnplacedStruct_t = GenTrapStruct<Precision>;
0054 using UnplacedVolume_t = UnplacedGenTrap;
0055
0056
0057 enum class ExitSurface : signed char { kNone = -1, kZMin, kZMax, kLateral0, kLateral1, kLateral2, kLateral3 };
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 template <typename Real_v>
0072 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Contains(UnplacedStruct_t const &unplaced,
0073 Vector3D<Real_v> const &point, bool &inside);
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 template <typename Real_v>
0087 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Inside(UnplacedStruct_t const &unplaced,
0088 Vector3D<Real_v> const &point, Inside_t &inside);
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 template <typename Real_v, bool ForInside>
0106 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void GenericKernelForContainsAndInside(
0107 UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, bool &completelyinside, bool &completelyoutside);
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122 template <typename Real_v>
0123 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToIn(UnplacedStruct_t const &unplaced,
0124 Vector3D<Real_v> const &point,
0125 Vector3D<Real_v> const &direction,
0126 Real_v const &stepMax, Real_v &distance);
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 template <typename Real_v>
0148 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOut(UnplacedStruct_t const &unplaced,
0149 Vector3D<Real_v> const &point,
0150 Vector3D<Real_v> const &direction,
0151 Real_v const &stepMax, Real_v &distance,
0152 SurfaceHitView<Real_v> *hitInfo = nullptr);
0153
0154
0155 template <typename Real_v, bool RecordHitInfo>
0156 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOutImpl(UnplacedStruct_t const &unplaced,
0157 Vector3D<Real_v> const &point,
0158 Vector3D<Real_v> const &direction,
0159 Real_v const &stepMax, Real_v &distance,
0160 SurfaceHitView<Real_v> *hitInfo);
0161
0162
0163 template <typename Real_v>
0164 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SetExitHit(UnplacedStruct_t const &unplaced,
0165 ExitSurface surface,
0166 Vector3D<Real_v> const &hitPoint,
0167 SurfaceHitView<Real_v> *hitInfo);
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180 template <typename Real_v>
0181 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToIn(UnplacedStruct_t const &unplaced,
0182 Vector3D<Real_v> const &point, Real_v &safety);
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 template <typename Real_v>
0196 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToOut(UnplacedStruct_t const &unplaced,
0197 Vector3D<Real_v> const &point, Real_v &safety);
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 template <typename Real_v>
0213 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void NormalKernel(UnplacedStruct_t const &unplaced,
0214 Vector3D<Real_v> const &point,
0215 Vector3D<Real_v> &normal, bool &valid);
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 template <typename Real_v>
0238 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static Real_v DistanceToSurf(
0239 UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &dir, int i, bool in,
0240 Real_v lmin = Real_v(0), Real_v lmax = InfinityLength<Real_v>(), bool assumeInsideStart = false);
0241
0242
0243
0244
0245
0246
0247
0248
0249 template <typename Real_v>
0250 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void FillVerticesAtZ(UnplacedStruct_t const &unplaced, Real_v z,
0251 Vector2D<Real_v> vertices[4]);
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 template <class Real_v>
0262 VECCORE_ATT_HOST_DEVICE static void GetClosestEdge(Vector3D<Real_v> const &point, Vector2D<Real_v> const vertxy[4],
0263 int &iseg);
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275 template <typename Real_v>
0276 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static bool InsideSection(Vector3D<Real_v> const &point,
0277 Vector3D<Real_v> const v[4]);
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290 template <typename Real_v>
0291 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static bool InSurfLimits(UnplacedStruct_t const &unplaced,
0292 Vector3D<Real_v> const &point, int isurf);
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307 template <typename Real_v>
0308 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static Real_v SafetyArb4(UnplacedStruct_t const &unplaced,
0309 Vector3D<Real_v> const &point, Real_v safmax,
0310 bool inside = true);
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330 template <typename Real_v>
0331 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void UNormal(UnplacedStruct_t const &unplaced,
0332 Vector3D<Real_v> const &point, int i,
0333 Vector3D<Real_v> &unorm, Real_v &u, Real_v &v);
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 template <typename Real_v>
0347 VECCORE_ATT_HOST_DEVICE static Vector2D<Real_v> XYZtoUV(UnplacedStruct_t const &unplaced,
0348 Vector3D<Real_v> const &point, int i);
0349 };
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375 template <typename Real_v>
0376 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE bool GenTrap_VInterval(Real_v ptZ, Real_v dirZ, Real_v Dz2, Real_v limit,
0377 Real_v &tmin, Real_v &tmax)
0378 {
0379
0380 const Real_v kv = dirZ * Dz2;
0381 const Real_v v0 = ptZ * Dz2 + Real_v(0.5);
0382 Real_v t0 = Real_v(0), t1 = limit;
0383 if (Abs(kv) < kToleranceDist<Real_v>) {
0384
0385 if (v0 < Real_v(0) - kToleranceDist<Real_v> || v0 > Real_v(1) + kToleranceDist<Real_v>) return false;
0386 tmin = t0;
0387 tmax = t1;
0388 return true;
0389 }
0390 const Real_v t_at_0 = (Real_v(0) - v0) / kv;
0391 const Real_v t_at_1 = (Real_v(1) - v0) / kv;
0392 Real_v a = Min(t_at_0, t_at_1), b = Max(t_at_0, t_at_1);
0393 t0 = Max(t0, a);
0394 t1 = Min(t1, b);
0395 if (t1 < t0) return false;
0396 tmin = t0;
0397 tmax = t1;
0398 return true;
0399 }
0400
0401
0402 template <typename Real_v>
0403 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Real_v GenTrapImplementation::DistanceToSurf(
0404 UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &dir, int i, bool in,
0405 Real_v lmin, Real_v lmax, bool assumeInsideStart)
0406 {
0407
0408 Real_v big = InfinityLength<Real_v>();
0409 VECGEOM_ASSERT(!unplaced.IsDegenerated(i) && "DistanceToSurf called with degenerated face");
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419 if (!unplaced.IsTwisted(i)) {
0420
0421
0422
0423
0424
0425 const Real_v safety = unplaced.fSurf[i].EvaluatePlanar(point);
0426 const Real_v dn = unplaced.fSurf[i].DotPlaneNormal(dir);
0427
0428
0429
0430
0431
0432
0433 const bool facing_ok = in ? dn > Real_v(0.) : dn < -kToleranceDist<Real_v>;
0434 const bool side_ok = (in && safety < Real_v(kTolerance)) || (!in && safety > Real_v(-kTolerance));
0435 if (!facing_ok || !side_ok) return big;
0436
0437
0438
0439 Real_v snext = Max(-safety / NonZero(dn), Real_v(0.));
0440
0441 if ((snext < lmin - kToleranceDist<Real_v>) || (snext >= lmax)) return big;
0442
0443
0444
0445 if (assumeInsideStart && safety < Real_v(-kTolerance)) return snext;
0446
0447
0448 return InSurfLimits<Real_v>(unplaced, point + snext * dir, i) ? snext : big;
0449 } else {
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459 #if GENTRAP_USE_HYBRID_COEFFS
0460 const auto pointXY = point.XY();
0461 const auto dirXY = dir.XY();
0462 const auto z = point[2];
0463 const auto dz = dir[2];
0464
0465
0466 const auto &dMid = unplaced.fDmid[i];
0467 const auto &dT = unplaced.fDT[i];
0468
0469
0470 const Real_v TT = unplaced.fTT[i];
0471 const Real_v MM = unplaced.fMM[i];
0472 const Real_v MT = unplaced.fMT[i];
0473
0474
0475 auto xz = [](auto const &a, auto const &b) -> Real_v { return a.x() * b.y() - a.y() * b.x(); };
0476
0477
0478 const Real_v dT_x_dir = xz(dT, dirXY);
0479 const Real_v dT_x_point = xz(dT, pointXY);
0480 const Real_v dMid_x_dir = xz(dMid, dirXY);
0481 const Real_v dMid_x_pt = xz(dMid, pointXY);
0482
0483
0484 const Real_v zTT = z * TT;
0485 const Real_v pointTerms = dT_x_point + MT + zTT;
0486 Real_v a = dz * (dT_x_dir + TT * dz);
0487 Real_v b = dMid_x_dir + z * dT_x_dir + (pointTerms + zTT) * dz;
0488 Real_v c = dMid_x_pt + MM + z * pointTerms;
0489 #else
0490 int j = (i + 1) % 4;
0491
0492
0493 auto const pointXY = point.XY();
0494 auto const dirXY = dir.XY();
0495 auto const t = unplaced.fT;
0496
0497 auto const s1 = unplaced.fMidXY[i] + t[i] * point[2];
0498 auto const s2 = unplaced.fMidXY[j] + t[j] * point[2];
0499 auto ds = s2 - s1;
0500
0501
0502 Real_v a = ((t[j] - t[i]).CrossZ(dirXY) + t[i].CrossZ(t[j]) * dir[2]) * dir[2];
0503 Real_v b = ds.CrossZ(dirXY) + ((t[j] - t[i]).CrossZ(pointXY) + s1.CrossZ(t[j]) - s2.CrossZ(t[i])) * dir[2];
0504 Real_v c = ds.CrossZ(pointXY) + s1.CrossZ(s2);
0505 #endif
0506
0507 constexpr Real_v tolerance = kToleranceArb4<Real_v>;
0508 if (assumeInsideStart) {
0509
0510
0511
0512 const bool regularInterior = c < -tolerance;
0513 if (a == Real_v(0.)) {
0514 if (b == Real_v(0.)) return big;
0515 const Real_v root = -c / b;
0516 if (!regularInterior && b <= Real_v(0.)) return big;
0517 if (root < lmin - tolerance || root >= lmax) return big;
0518 const Real_v candidate = Max(root, Real_v(0.));
0519 return regularInterior || InSurfLimits(unplaced, point + candidate * dir, i) ? candidate : big;
0520 }
0521
0522 const Real_v halfB = Real_v(0.5) * b;
0523 const Real_v delta = halfB * halfB - a * c;
0524 if (delta < Real_v(0.)) return big;
0525 const Real_v q = -halfB - CopySign(Sqrt(delta), halfB);
0526 if (regularInterior && delta > Real_v(0.) && lmin == Real_v(0.)) {
0527
0528
0529
0530 const bool smallRoot = q < Real_v(0.);
0531 const Real_v root = (smallRoot ? c : q) / (smallRoot ? NonZero(q) : a);
0532 return root >= Real_v(0.) && root < lmax ? root : big;
0533 }
0534 const Real_v r1 = q / a;
0535 const Real_v r2 = c / NonZero(q);
0536 Real_v root = InfinityLength<Real_v>();
0537 const bool r1InWindow = r1 >= lmin || (r1 >= lmin - tolerance && b > Real_v(0.));
0538 const bool r2InWindow = r2 >= lmin || (r2 >= lmin - tolerance && b > Real_v(0.));
0539 if (r1InWindow && (regularInterior || Real_v(2.) * a * r1 + b > Real_v(0.))) root = r1;
0540 if (r2InWindow && (regularInterior || Real_v(2.) * a * r2 + b > Real_v(0.))) root = Min(root, r2);
0541 if (root >= lmax) return big;
0542 const Real_v candidate = Max(root, Real_v(0.));
0543 return regularInterior || InSurfLimits(unplaced, point + candidate * dir, i) ? candidate : big;
0544 }
0545
0546 auto acceptExteriorRoot = [&](Real_v root, Real_v derivative) {
0547 if (derivative >= Real_v(0.) || root < lmin - kToleranceDist<Real_v> || root >= lmax) return big;
0548 const Real_v candidate = Max(root, Real_v(0.));
0549 constexpr Real_v uvTolerance = Real_v(8.) * kEpsilonT<Real_v>;
0550 const auto uv = XYZtoUV(unplaced, point + candidate * dir, i);
0551 return (uv[0] > -uvTolerance) && (uv[0] < Real_v(1.) + uvTolerance) && (uv[1] > -uvTolerance) &&
0552 (uv[1] < Real_v(1.) + uvTolerance)
0553 ? candidate
0554 : big;
0555 };
0556
0557 if (!in && a == Real_v(0.)) {
0558 if (b >= Real_v(0.)) return big;
0559 return acceptExteriorRoot(-c / b, b);
0560 }
0561
0562 Real_v dist[2] = {-InfinityLength<Real_v>(), -InfinityLength<Real_v>()};
0563 auto nroots = QuadraticSolver(a, b, c, dist);
0564
0565
0566
0567
0568 const Real_v discardedRoot = nroots < 2 ? dist[1 - nroots] : -InfinityLength<Real_v>();
0569 if (discardedRoot > -tolerance && InSurfLimits<Real_v>(unplaced, point, i)) {
0570 const auto grad = unplaced.fSurf[i].EvaluateGradient(point);
0571 const Real_v projection = dir.Dot(grad);
0572 const bool nonGrazing = projection * projection > kToleranceSquared * grad.Mag2();
0573 if (nonGrazing && (in ^ (projection < Real_v(0.)))) {
0574
0575
0576 if (!in) {
0577 for (auto k = 0; k < 4; ++k) {
0578 if (unplaced.IsDegenerated(k) || unplaced.IsTwisted(k)) continue;
0579 if (unplaced.fSurf[k].EvaluatePlanar(point) > Real_v(-kTolerance) &&
0580 unplaced.fSurf[k].DotPlaneNormal(dir) > kToleranceDist<Real_v>)
0581 return big;
0582 }
0583 }
0584 return Real_v(0.);
0585 }
0586 }
0587
0588
0589 if (!in && nroots == 2 && Abs(dist[0]) < tolerance && Abs(dist[1]) < tolerance) return big;
0590
0591
0592
0593
0594
0595 if (!in) {
0596 if (nroots == 0) return big;
0597 const Real_v root = a > Real_v(0.) ? dist[0] : dist[1];
0598 return acceptExteriorRoot(root, Real_v(2.) * a * root + b);
0599 }
0600
0601 for (auto k = 2 - nroots; k < 2; ++k) {
0602 const Real_v troot = dist[k];
0603
0604
0605 if ((troot < lmin - kToleranceDist<Real_v>) || (troot >= lmax)) continue;
0606
0607
0608 if (in && troot > tolerance) return troot;
0609
0610
0611
0612 constexpr Real_v uvTolerance = Real_v(8.) * kEpsilonT<Real_v>;
0613 auto hit = point + troot * dir;
0614 auto uv = XYZtoUV(unplaced, hit, i);
0615 auto u = uv[0];
0616 auto v = uv[1];
0617 bool in_patch =
0618 (u > -uvTolerance) && (u < Real_v(1.) + uvTolerance) && (v > -uvTolerance) && (v < Real_v(1.) + uvTolerance);
0619 if (!in_patch) continue;
0620
0621
0622 bool crosses_ok = in ^ (Real_v(2.) * a * troot + b < Real_v(0.));
0623 if (crosses_ok) return troot;
0624 }
0625 }
0626 return big;
0627 }
0628
0629
0630 template <typename Real_v, bool ForInside>
0631 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::GenericKernelForContainsAndInside(
0632 UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, bool &completelyinside, bool &completelyoutside)
0633 {
0634
0635
0636
0637
0638
0639
0640
0641 Vector3D<Real_v> halfsize(unplaced.fBBdimensions[0], unplaced.fBBdimensions[1], unplaced.fBBdimensions[2]);
0642 BoxImplementation::GenericKernelForContainsAndInside<Real_v, true>(halfsize, point - unplaced.fBBorigin,
0643 completelyinside, completelyoutside);
0644
0645 constexpr Real_v tolerance = kToleranceArb4<Real_v>;
0646 if (completelyoutside) return;
0647
0648
0649 auto on_surfz = (unplaced.fDz - Abs(point.z())) < Real_v(kTolerance);
0650
0651
0652 VECGEOM_PRAGMA_UNROLL(4)
0653 for (int i = 0; i < 4; i++) {
0654 if (unplaced.IsDegenerated(i)) continue;
0655 auto eqeval = unplaced.IsTwisted(i) ? unplaced.fSurf[i].Evaluate(point) : unplaced.fSurf[i].EvaluatePlanar(point);
0656 completelyoutside = eqeval > tolerance;
0657 if (completelyoutside) {
0658 completelyinside = false;
0659 return;
0660 }
0661 if (ForInside && !on_surfz) completelyinside &= eqeval < -tolerance;
0662 }
0663 }
0664
0665
0666 template <typename Real_v>
0667 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::Contains(UnplacedStruct_t const &unplaced,
0668 Vector3D<Real_v> const &point,
0669 bool &inside)
0670 {
0671
0672
0673
0674
0675
0676
0677 if (unplaced.fTslHelper) {
0678
0679 inside = false;
0680 if (Abs(point.z()) > unplaced.fDz) return;
0681 inside = unplaced.fTslHelper->Contains(point);
0682 return;
0683 }
0684
0685 bool unused = false;
0686 bool outside = false;
0687 GenericKernelForContainsAndInside<Real_v, false>(unplaced, point, unused, outside);
0688 inside = !outside;
0689 }
0690
0691
0692 template <typename Real_v>
0693 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::Inside(UnplacedStruct_t const &unplaced,
0694 Vector3D<Real_v> const &point,
0695 Inside_t &inside)
0696 {
0697 if (unplaced.fTslHelper) {
0698
0699 inside = EInside::kOutside;
0700 Precision safZ = Abs(point.z()) - unplaced.fDz;
0701 if (safZ > kTolerance) return;
0702 bool insideZ = safZ < -kTolerance;
0703 inside = unplaced.fTslHelper->Inside(point);
0704 if (insideZ || inside == EInside::kOutside) return;
0705 inside = EInside::kSurface;
0706 return;
0707 }
0708 bool completelyinside;
0709 bool completelyoutside;
0710 GenericKernelForContainsAndInside<Real_v, true>(unplaced, point, completelyinside, completelyoutside);
0711
0712
0713 inside = EInside::kSurface;
0714 if (completelyoutside) inside = EInside::kOutside;
0715 if (completelyinside) inside = EInside::kInside;
0716 }
0717
0718
0719 template <typename Real_v>
0720 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::DistanceToIn(UnplacedStruct_t const &unplaced,
0721 Vector3D<Real_v> const &point,
0722 Vector3D<Real_v> const &direction,
0723 Real_v const &stepMax,
0724 Real_v &distance)
0725 {
0726
0727
0728
0729 #ifndef VECCORE_CUDA
0730 if (unplaced.fTslHelper && (unplaced.fFlags & 0xF0u) == 0) {
0731 const Real_v invdirz = Real_v(1.) / NonZero(direction.z());
0732 const Real_v pz = point.z();
0733 distance = InfinityLength<Real_v>();
0734 if ((Abs(pz) - unplaced.fDz) > -kTolerance && pz * direction.z() >= Real_v(0.)) return;
0735
0736 const Real_v ddz = CopySign(unplaced.fDz, invdirz);
0737 const Real_v distz = -(pz + ddz) * invdirz;
0738 Real_v entry = distz;
0739 Real_v limit = Min(-(pz - ddz) * invdirz, stepMax);
0740
0741 VECGEOM_PRAGMA_UNROLL(4)
0742 for (int i = 0; i < 4; ++i) {
0743 const Real_v projection = unplaced.fSurf[i].DotPlaneNormal(direction);
0744 const Real_v planeDist = unplaced.fSurf[i].EvaluatePlanar(point);
0745 const bool outside = planeDist > -kTolerance;
0746 if (outside) {
0747 if (projection > -kTolerance) return;
0748 entry = Max(entry, -planeDist / projection);
0749 } else if (projection > kTolerance) {
0750 limit = Min(limit, -planeDist / projection);
0751 }
0752 }
0753 if (entry >= limit - kTolerance) return;
0754 distance = entry;
0755 return;
0756 }
0757 #endif
0758
0759
0760 if (unplaced.fTslHelper) {
0761 Precision invdirz = 1. / NonZero(direction.z());
0762
0763
0764 if ((unplaced.fFlags & 0xF0u) == 0)
0765 distance = unplaced.fTslHelper->DistanceToIn<false, 4>(point, direction, invdirz, stepMax);
0766 else
0767 distance = unplaced.fTslHelper->DistanceToIn<false>(point, direction, invdirz, stepMax);
0768 return;
0769 }
0770
0771
0772
0773
0774
0775
0776 #ifndef VECGEOM_NO_WRONGSIDE_CONV
0777 bool completelyinside, completelyoutside;
0778 GenericKernelForContainsAndInside<Real_v, true>(unplaced, point, completelyinside, completelyoutside);
0779
0780
0781 if (completelyinside) {
0782 distance = Real_v(-1.);
0783 return;
0784 }
0785 #endif
0786
0787
0788 Real_v bbdistance = Real_v(kInfLength);
0789 BoxImplementation::DistanceToIn(BoxStruct<Precision>(unplaced.fBBdimensions), point - unplaced.fBBorigin, direction,
0790 stepMax, bbdistance);
0791
0792
0793 distance = InfinityLength<Real_v>();
0794
0795
0796 bool done = bbdistance >= InfinityLength<Real_v>();
0797 if (done) return;
0798
0799
0800 Real_v zsafety = Abs(point.z()) - unplaced.fDz;
0801 bool canhitz = (zsafety > -kToleranceDist<Real_v>) && (point.z() * direction.z() < 0);
0802
0803 if (canhitz) {
0804
0805 Real_v next = zsafety / Abs(direction.z());
0806
0807
0808 auto const vertices = (direction.z() > 0) ? &unplaced.fVertices[0] : &unplaced.fVertices[4];
0809 auto hitz = InsideSection(point + next * direction, vertices);
0810 if (hitz) {
0811 distance = next;
0812 return;
0813 }
0814 }
0815
0816
0817
0818 Real_v t_approach = Max(Real_v(0), bbdistance - kToleranceArb4<Real_v>);
0819 Vector3D<Real_v> p0 = point + t_approach * direction;
0820 Real_v step_budget = stepMax - t_approach;
0821 if (step_budget <= Real_v(0)) {
0822 distance = InfinityLength<Real_v>();
0823 return;
0824 }
0825
0826
0827
0828
0829
0830
0831 Real_v tmin_v, tmax_v;
0832 if (!GenTrap_VInterval(p0.z(), direction.z(), unplaced.fDz2, step_budget, tmin_v, tmax_v)) {
0833
0834 return;
0835 }
0836
0837
0838 Real_v lmin = Max(tmin_v, (t_approach > Real_v(0)) ? kToleranceArb4<Real_v> : Real_v(0));
0839 Real_v lmax = tmax_v;
0840 if (lmin > lmax) return;
0841
0842
0843 Real_v best_local = InfinityLength<Real_v>();
0844
0845
0846
0847 VECGEOM_PRAGMA_UNROLL(4)
0848 for (auto i = 0; i < 4; ++i) {
0849 if (!unplaced.IsDegenerated(i) && !unplaced.IsTwisted(i)) {
0850
0851 Real_v dn = unplaced.fSurf[i].DotPlaneNormal(direction);
0852 if (dn >= -kToleranceDist<Real_v>) continue;
0853
0854
0855
0856 Real_v face_limit = Min(best_local, lmax);
0857 best_local = Min(best_local, DistanceToSurf(unplaced, p0, direction, i, false, lmin, face_limit));
0858 }
0859 }
0860
0861 VECGEOM_PRAGMA_UNROLL(4)
0862 for (auto i = 0; i < 4; ++i) {
0863 if (!unplaced.IsDegenerated(i) && unplaced.IsTwisted(i)) {
0864
0865
0866
0867 Real_v face_limit = Min(best_local, lmax);
0868 best_local = Min(best_local, DistanceToSurf(unplaced, p0, direction, i, false, lmin, face_limit));
0869 }
0870 }
0871
0872
0873 distance = (best_local >= InfinityLength<Real_v>()) ? InfinityLength<Real_v>() : (t_approach + best_local);
0874 }
0875
0876
0877 template <typename Real_v>
0878 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::SetExitHit(UnplacedStruct_t const &unplaced,
0879 ExitSurface surface,
0880 Vector3D<Real_v> const &hitPoint,
0881 SurfaceHitView<Real_v> *hitInfo)
0882 {
0883 if (surface == ExitSurface::kNone) return;
0884
0885 if (surface == ExitSurface::kZMin || surface == ExitSurface::kZMax) {
0886 hitInfo->SetLeftBehind(true);
0887 if (hitInfo->WantsNormal()) {
0888 const Real_v z = surface == ExitSurface::kZMin ? Real_v(-1.) : Real_v(1.);
0889 hitInfo->SetNormal(Vector3D<Real_v>(Real_v(0.), Real_v(0.), z));
0890 }
0891 return;
0892 }
0893
0894 const int face = static_cast<int>(surface) - static_cast<int>(ExitSurface::kLateral0);
0895
0896
0897
0898
0899 const bool twisted = unplaced.IsTwisted(face);
0900 hitInfo->SetLeftBehind(!twisted);
0901 if (!hitInfo->WantsNormal()) return;
0902 if (twisted)
0903 hitInfo->SetNormal(unplaced.fSurf[face].EvaluateGradient(hitPoint).Unit());
0904 else
0905 hitInfo->SetNormal(Vector3D<Real_v>(unplaced.fSurf[face].GetPlaneNormal()));
0906 }
0907
0908
0909 template <typename Real_v, bool RecordHitInfo>
0910 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::DistanceToOutImpl(
0911 UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &direction,
0912 Real_v const & , Real_v &distance, SurfaceHitView<Real_v> *hitInfo)
0913 {
0914 if constexpr (RecordHitInfo) hitInfo->Clear();
0915
0916 if (unplaced.fTslHelper) {
0917 if constexpr (RecordHitInfo) {
0918 const Real_v safz = Abs(point.z()) - unplaced.fDz;
0919 if (safz > Real_v(kTolerance)) {
0920 distance = Real_v(-kTolerance);
0921 return;
0922 }
0923
0924 const Real_v vz = direction.z();
0925 const Real_v capDistance = vz == Real_v(0.)
0926 ? InfinityLength<Real_v>()
0927 : Max((CopySign(Real_v(unplaced.fDz), vz) - point.z()) / vz, Real_v(0.));
0928 int face = -1;
0929 const Real_v sideDistance = unplaced.fTslHelper->DistanceToOut(point, direction, face);
0930 if (face < 0 && sideDistance < Real_v(0.)) {
0931 distance = sideDistance;
0932 return;
0933 }
0934
0935 if (sideDistance < capDistance) {
0936 distance = sideDistance;
0937 hitInfo->SetLeftBehind(true);
0938 if (hitInfo->WantsNormal())
0939 hitInfo->SetNormal(Vector3D<Real_v>(unplaced.fTslHelper->GetFacet(face).GetNormal()));
0940 } else {
0941 distance = capDistance;
0942 SetExitHit(unplaced, vz < Real_v(0.) ? ExitSurface::kZMin : ExitSurface::kZMax, point, hitInfo);
0943 }
0944 } else {
0945 distance = unplaced.fTslHelper->DistanceToOut(point, direction);
0946 }
0947 return;
0948 }
0949
0950
0951
0952
0953
0954 bool assumeInsideStart = true;
0955 #ifndef VECGEOM_NO_WRONGSIDE_CONV
0956 distance = Real_v(-1.);
0957 bool completelyoutside;
0958
0959
0960 GenericKernelForContainsAndInside<Real_v, true>(unplaced, point, assumeInsideStart, completelyoutside);
0961 if (completelyoutside) return;
0962 #endif
0963
0964
0965 Real_v sign = (direction.z() < 0) ? Real_v(-1.) : Real_v(1.);
0966 Real_v limit = Real_v(1.);
0967 bool skipZ = Abs(direction.z()) * limit < kToleranceDist<Real_v>;
0968 distance = skipZ ? InfinityLength<Real_v>() : (sign * unplaced.fDz - point.z()) / NonZero(direction.z());
0969 ExitSurface surface = skipZ ? ExitSurface::kNone : (sign < Real_v(0.) ? ExitSurface::kZMin : ExitSurface::kZMax);
0970
0971
0972
0973 VECGEOM_PRAGMA_UNROLL(4)
0974 for (auto i = 0; i < 4; ++i) {
0975 if (!unplaced.IsDegenerated(i) && !unplaced.IsTwisted(i)) {
0976 if constexpr (RecordHitInfo) {
0977 const Real_v candidate =
0978 DistanceToSurf(unplaced, point, direction, i, true, Real_v(0), distance, assumeInsideStart);
0979 if (candidate < distance) {
0980 distance = candidate;
0981 surface = static_cast<ExitSurface>(static_cast<int>(ExitSurface::kLateral0) + i);
0982 }
0983 } else {
0984 distance =
0985 Min(distance, DistanceToSurf(unplaced, point, direction, i, true, Real_v(0), distance, assumeInsideStart));
0986 }
0987 }
0988 }
0989
0990 VECGEOM_PRAGMA_UNROLL(4)
0991 for (auto i = 0; i < 4; ++i) {
0992 if (!unplaced.IsDegenerated(i) && unplaced.IsTwisted(i)) {
0993 if constexpr (RecordHitInfo) {
0994 const Real_v candidate =
0995 DistanceToSurf(unplaced, point, direction, i, true, Real_v(0), distance, assumeInsideStart);
0996 if (candidate < distance) {
0997 distance = candidate;
0998 surface = static_cast<ExitSurface>(static_cast<int>(ExitSurface::kLateral0) + i);
0999 }
1000 } else {
1001 distance =
1002 Min(distance, DistanceToSurf(unplaced, point, direction, i, true, Real_v(0), distance, assumeInsideStart));
1003 }
1004 }
1005 }
1006
1007 if constexpr (RecordHitInfo) SetExitHit(unplaced, surface, point + distance * direction, hitInfo);
1008 }
1009
1010
1011 template <typename Real_v>
1012 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::DistanceToOut(
1013 UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &direction,
1014 Real_v const &stepMax, Real_v &distance, SurfaceHitView<Real_v> *hitInfo)
1015 {
1016 if (hitInfo)
1017 DistanceToOutImpl<Real_v, true>(unplaced, point, direction, stepMax, distance, hitInfo);
1018 else
1019 DistanceToOutImpl<Real_v, false>(unplaced, point, direction, stepMax, distance, nullptr);
1020 }
1021
1022
1023 template <typename Real_v>
1024 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::SafetyToIn(UnplacedStruct_t const &unplaced,
1025 Vector3D<Real_v> const &point,
1026 Real_v &safety)
1027 {
1028 if (unplaced.fTslHelper) {
1029 safety = unplaced.fTslHelper->SafetyToIn(point);
1030 return;
1031 }
1032
1033 bool inside;
1034 BoxImplementation::Contains(BoxStruct<Precision>(unplaced.fBBdimensions), point - unplaced.fBBorigin, inside);
1035 if (!inside) {
1036
1037 BoxImplementation::SafetyToIn(BoxStruct<Precision>(unplaced.fBBdimensions), point - unplaced.fBBorigin, safety);
1038 return;
1039 }
1040
1041
1042 safety = Abs(point[2]) - unplaced.fDz;
1043 safety = SafetyArb4(unplaced, point, safety, false);
1044 }
1045
1046
1047 template <typename Real_v>
1048 VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::SafetyToOut(UnplacedStruct_t const &unplaced,
1049 Vector3D<Real_v> const &point, Real_v &safety)
1050 {
1051
1052 if (unplaced.fTslHelper) {
1053 safety = unplaced.fTslHelper->SafetyToOut(point);
1054 return;
1055 }
1056
1057
1058
1059
1060
1061
1062
1063 safety = Abs(point[2]) - unplaced.fDz;
1064
1065
1066 if (safety > -kTolerance) {
1067 safety = Real_v(-1.);
1068 return;
1069 }
1070
1071
1072 safety = SafetyArb4(unplaced, point, safety, true);
1073 }
1074
1075
1076 template <typename Real_v>
1077 VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::NormalKernel(UnplacedStruct_t const &unplaced,
1078 Vector3D<Real_v> const &point,
1079 Vector3D<Real_v> &normal, bool &valid)
1080 {
1081
1082
1083
1084
1085
1086
1087
1088 valid = true;
1089 normal.Set(0.);
1090
1091
1092 Real_v safz = Abs(unplaced.fDz - Abs(point.z()));
1093 if (safz < kTolerance) {
1094 normal[2] = Sign(point.z());
1095 return;
1096 }
1097
1098
1099
1100 Vector2D<Real_v> vert[4];
1101 FillVerticesAtZ(unplaced, point.z(), vert);
1102 int surfaceCount = 0;
1103 bool firstIsTwisted = false;
1104 if (Abs(point.z()) < unplaced.fDz) {
1105 for (int i = 0; i < 4; ++i) {
1106 if (unplaced.IsDegenerated(i)) continue;
1107
1108 const int next = (i + 1) % 4;
1109 const Real_v tolerance = unplaced.IsTwisted(i) ? kToleranceArb4<Real_v> : Real_v(kTolerance);
1110 const Real_v minx = Min(vert[i].x(), vert[next].x()) - tolerance;
1111 const Real_v maxx = Max(vert[i].x(), vert[next].x()) + tolerance;
1112 const Real_v miny = Min(vert[i].y(), vert[next].y()) - tolerance;
1113 const Real_v maxy = Max(vert[i].y(), vert[next].y()) + tolerance;
1114 if (!(point.x() > minx && point.x() < maxx && point.y() > miny && point.y() < maxy)) continue;
1115
1116 const bool twisted = unplaced.IsTwisted(i);
1117 if (!twisted) {
1118 if (!(Abs(unplaced.fSurf[i].EvaluatePlanar(point)) < tolerance)) continue;
1119 } else {
1120 const Real_v dx = vert[next].x() - vert[i].x();
1121 const Real_v dy = vert[next].y() - vert[i].y();
1122 const Real_v dpx = point.x() - vert[i].x();
1123 const Real_v dpy = point.y() - vert[i].y();
1124 const Real_v side = dx * dpy - dy * dpx;
1125 const Real_v lensq = dx * dx + dy * dy;
1126 if (!(side * side < tolerance * tolerance * lensq)) continue;
1127 }
1128
1129 const auto faceNormal = twisted ? unplaced.fSurf[i].EvaluateGradient(point) : unplaced.fSurf[i].GetPlaneNormal();
1130 if (surfaceCount == 0) {
1131 normal = faceNormal;
1132 firstIsTwisted = twisted;
1133 } else {
1134
1135
1136 if (surfaceCount == 1 && firstIsTwisted) normal.Normalize();
1137 normal += twisted ? faceNormal.Normalized() : faceNormal;
1138 }
1139 ++surfaceCount;
1140 }
1141 }
1142 if (surfaceCount != 0) {
1143 if (surfaceCount > 1 || firstIsTwisted) normal.Normalize();
1144 return;
1145 }
1146
1147 int iseg;
1148 GetClosestEdge<Real_v>(point, vert, iseg);
1149
1150 if (!unplaced.IsTwisted(iseg)) {
1151
1152 normal = unplaced.fSurf[iseg].GetPlaneNormal();
1153 return;
1154 }
1155
1156 Real_v u, v;
1157 UNormal(unplaced, point, iseg, normal, u, v);
1158 normal.Normalize();
1159
1160
1161 valid = (u > -kToleranceDist<Real_v>) && (u < 1. + kToleranceDist<Real_v>) && (v > -kToleranceDist<Real_v>) &&
1162 (v < 1. + kToleranceDist<Real_v>);
1163 }
1164
1165
1166 template <typename Real_v>
1167 VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::GetClosestEdge(Vector3D<Real_v> const &point,
1168 Vector2D<Real_v> const vertxy[4], int &iseg)
1169 {
1170
1171 iseg = 0;
1172 Real_v lsq, dx, dy, dpx, dpy, u;
1173 Real_v safe = InfinityLength<Real_v>();
1174 Real_v ssq = InfinityLength<Real_v>();
1175
1176 for (int i = 0; i < 4; ++i) {
1177 int j = (i + 1) % 4;
1178 dx = vertxy[j].x() - vertxy[i].x();
1179 dy = vertxy[j].y() - vertxy[i].y();
1180 dpx = point.x() - vertxy[i].x();
1181 dpy = point.y() - vertxy[i].y();
1182 lsq = dx * dx + dy * dy;
1183
1184
1185 if (lsq < kToleranceDistSquared<Real_v>) continue;
1186
1187
1188 u = (dpx * dx + dpy * dy);
1189 if (u > lsq) {
1190
1191 dpx = point.x() - vertxy[j].x();
1192 dpy = point.y() - vertxy[j].y();
1193 } else if (u >= 0) {
1194
1195 auto invlsq = Real_v(1.) / lsq;
1196 dpx -= u * dx * invlsq;
1197 dpy -= u * dy * invlsq;
1198 }
1199
1200 ssq = dpx * dpx + dpy * dpy;
1201 if (ssq < safe) {
1202 safe = ssq;
1203 iseg = i;
1204 }
1205 }
1206 }
1207
1208
1209 template <typename Real_v>
1210 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::UNormal(UnplacedStruct_t const &unplaced,
1211 Vector3D<Real_v> const &point, int i,
1212 Vector3D<Real_v> &unorm, Real_v &u,
1213 Real_v &v)
1214 {
1215
1216 auto uv = XYZtoUV(unplaced, point, i);
1217 u = uv[0];
1218 v = uv[1];
1219
1220
1221
1222 #if GENTRAP_USE_HYBRID_COEFFS
1223 const auto &E0 = unplaced.fE0[i];
1224 const auto &E1 = unplaced.fE1[i];
1225 const auto &G0 = unplaced.fG0[i];
1226 const auto &G1 = unplaced.fG1[i];
1227 auto dPdu = -(Real_v(1.) - v) * E0 - v * E1;
1228 auto dPdv = (Real_v(1.) - u) * G0 + u * G1;
1229 #else
1230 auto j = (i + 1) % 4;
1231
1232 auto const &A = unplaced.fVertices[i];
1233 auto const &C = unplaced.fVertices[j];
1234 auto const &B = unplaced.fVertices[i + 4];
1235 auto const &D = unplaced.fVertices[j + 4];
1236 auto dPdu = -(Real_v(1.) - v) * (C - A) - v * (D - B);
1237 auto dPdv = (Real_v(1.) - u) * (B - A) + u * (D - C);
1238 #endif
1239
1240
1241 unorm = dPdu.Cross(dPdv);
1242 }
1243
1244
1245 template <typename Real_v>
1246 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE bool GenTrapImplementation::InsideSection(Vector3D<Real_v> const &point,
1247 Vector3D<Real_v> const v[4])
1248 {
1249 bool inside = true;
1250 bool degen = true;
1251
1252
1253 for (int i = 0; i < 4; ++i) {
1254 const auto vij = v[(i + 1) % 4] - v[i];
1255 auto len2 = vij.Mag2();
1256 if (len2 < kToleranceDistSquared<Real_v>) continue;
1257 degen = false;
1258 auto cross = (point - v[i]).CrossZ(vij);
1259 inside = cross * cross >= kToleranceDistSquared<Real_v> * len2 ? (cross >= 0) : false;
1260 if (!inside) break;
1261 }
1262 if (degen) return false;
1263 return inside;
1264 }
1265
1266
1267 template <typename Real_v>
1268 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void GenTrapImplementation::FillVerticesAtZ(
1269 UnplacedStruct_t const &unplaced, Real_v z, Vector2D<Real_v> vertices[4])
1270 {
1271
1272 for (int i = 0; i < 4; i++)
1273 vertices[i] = unplaced.fMidXY[i] + unplaced.fT[i] * z;
1274 }
1275
1276
1277 template <typename Real_v>
1278 VECCORE_ATT_HOST_DEVICE Vector2D<Real_v> GenTrapImplementation::XYZtoUV(UnplacedStruct_t const &unplaced,
1279 Vector3D<Real_v> const &point, int i)
1280 {
1281
1282 Real_v v = unplaced.fDz2 * (point[2] + unplaced.fDz);
1283
1284
1285 #if GENTRAP_USE_HYBRID_COEFFS
1286 const auto &A = unplaced.fVertices[i];
1287 const auto &G0 = unplaced.fG0[i];
1288 const auto &E0 = unplaced.fE0[i];
1289 const auto &E1 = unplaced.fE1[i];
1290 auto E = A + v * G0;
1291 auto EF = (Real_v(1.) - v) * E0 + v * E1;
1292 #else
1293 auto j = (i + 1) % 4;
1294 auto E = (1. - v) * unplaced.fVertices[i] + v * unplaced.fVertices[i + 4];
1295 auto F = (1. - v) * unplaced.fVertices[j] + v * unplaced.fVertices[j + 4];
1296 auto EF = F - E;
1297 #endif
1298 auto EP = point - E;
1299
1300
1301 auto u = EP.Dot(EF) / NonZero(EF.Dot(EF));
1302 return Vector2D<Real_v>(u, v);
1303 }
1304
1305
1306 template <typename Real_v>
1307 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE bool GenTrapImplementation::InSurfLimits(UnplacedStruct_t const &unplaced,
1308 Vector3D<Real_v> const &point,
1309 int isurf)
1310 {
1311 auto uv = XYZtoUV(unplaced, point, isurf);
1312
1313
1314 bool inside = (uv[0] > -kToleranceDist<Real_v>) && (uv[0] < Real_v(1.) + kToleranceDist<Real_v>) &&
1315 (uv[1] > -kToleranceDist<Real_v>) && (uv[1] < Real_v(1.) + kToleranceDist<Real_v>);
1316 return inside;
1317 }
1318
1319
1320 template <typename Real_v>
1321 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Real_v GenTrapImplementation::SafetyArb4(UnplacedStruct_t const &unplaced,
1322 Vector3D<Real_v> const &point,
1323 Real_v safmax, bool inside)
1324 {
1325
1326 Real_v safety = safmax;
1327
1328
1329 VECGEOM_PRAGMA_UNROLL(4)
1330 for (int i = 0; i < 4; i++) {
1331 if (!unplaced.IsDegenerated(i) && !unplaced.IsTwisted(i)) {
1332
1333 Real_v sface = unplaced.fSurf[i].EvaluatePlanar(point);
1334 safety = Max(safety, sface);
1335 }
1336 }
1337
1338 VECGEOM_PRAGMA_UNROLL(4)
1339
1340
1341
1342 for (int i = 0; i < 4; i++) {
1343 if (!unplaced.IsDegenerated(i) && unplaced.IsTwisted(i)) {
1344
1345 if (!inside && unplaced.fSurf[i].Evaluate(point) <= Real_v(0)) continue;
1346 Real_v sface = unplaced.fSurf[i].SafetyLipschitz(point);
1347 safety = Max(safety, sface);
1348 }
1349 }
1350
1351
1352 return (inside) ? -safety : safety;
1353 }
1354
1355 }
1356 }
1357
1358 #endif