File indexing completed on 2026-11-26 10:34:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef VECGEOM_VOLUMES_KERNEL_TRAPEZOIDIMPLEMENTATION_H_
0013 #define VECGEOM_VOLUMES_KERNEL_TRAPEZOIDIMPLEMENTATION_H_
0014
0015 #include "VecGeom/base/Vector3D.h"
0016 #include "VecGeom/volumes/SurfaceHitView.h"
0017 #include "VecGeom/volumes/TrapezoidStruct.h"
0018 #include "VecGeom/volumes/kernel/GenericKernels.h"
0019 #include <VecCore/VecCore>
0020
0021 #include <cstdio>
0022
0023 namespace vecgeom {
0024
0025 VECGEOM_DEVICE_FORWARD_DECLARE(struct TrapezoidImplementation;);
0026 VECGEOM_DEVICE_DECLARE_CONV(struct, TrapezoidImplementation);
0027
0028 inline namespace VECGEOM_IMPL_NAMESPACE {
0029
0030 class PlacedTrapezoid;
0031 class UnplacedTrapezoid;
0032
0033
0034
0035
0036
0037
0038 struct TrapezoidImplementation {
0039
0040 using PlacedShape_t = PlacedTrapezoid;
0041 using UnplacedStruct_t = TrapezoidStruct<Precision>;
0042 using UnplacedVolume_t = UnplacedTrapezoid;
0043 #ifndef VECGEOM_PLANESHELL
0044 using TrapSidePlane = TrapezoidStruct<Precision>::TrapSidePlane;
0045 #endif
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 template <typename Real_v>
0056 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void EvaluateTrack(UnplacedStruct_t const &unplaced,
0057 Vector3D<Real_v> const &point,
0058 Vector3D<Real_v> const &dir, Real_v *pdist,
0059 Real_v *proj, Real_v *vdist)
0060 {
0061 #ifdef VECGEOM_PLANESHELL
0062 auto const *fPlanes = unplaced.GetPlanes();
0063 for (unsigned int i = 0; i < 4; ++i) {
0064 pdist[i] = fPlanes->fA[i] * point.x() + fPlanes->fB[i] * point.y() + fPlanes->fC[i] * point.z() + fPlanes->fD[i];
0065 proj[i] = fPlanes->fA[i] * dir.x() + fPlanes->fB[i] * dir.y() + fPlanes->fC[i] * dir.z();
0066 vdist[i] = -pdist[i] / NonZero(proj[i]);
0067 }
0068 #else
0069 TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0070
0071
0072 for (unsigned int i = 0; i < 4; ++i) {
0073
0074
0075 pdist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0076
0077
0078
0079 proj[i] = fPlanes[i].fA * dir.x() + fPlanes[i].fB * dir.y() + fPlanes[i].fC * dir.z();
0080
0081 vdist[i] = -pdist[i] / NonZero(proj[i]);
0082 }
0083 #endif
0084 }
0085
0086
0087
0088
0089
0090
0091 template <typename Real_v>
0092 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Contains(UnplacedStruct_t const &unplaced,
0093 Vector3D<Real_v> const &point, bool &inside)
0094 {
0095 bool unused = false, outside = false;
0096 GenericKernelForContainsAndInside<Real_v, false>(unplaced, point, unused, outside);
0097 inside = !outside;
0098 }
0099
0100
0101
0102
0103
0104
0105 template <typename Real_v>
0106 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Inside(UnplacedStruct_t const &unplaced,
0107 Vector3D<Real_v> const &point, Inside_t &inside)
0108 {
0109 bool completelyInside = false, completelyOutside = false;
0110 GenericKernelForContainsAndInside<Real_v, true>(unplaced, point, completelyInside, completelyOutside);
0111
0112 inside = Inside_t(EInside::kSurface);
0113 if (completelyOutside) inside = Inside_t(EInside::kOutside);
0114 if (completelyInside) inside = Inside_t(EInside::kInside);
0115 }
0116
0117
0118
0119
0120
0121
0122
0123
0124 template <typename Real_v, bool ForInside>
0125 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void GenericKernelForContainsAndInside(
0126 UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, bool &completelyInside, bool &completelyOutside)
0127 {
0128
0129 completelyOutside = Abs(point[2]) > MakePlusTolerant<true>(unplaced.fDz);
0130 if (ForInside) {
0131 completelyInside = Abs(point[2]) < MakeMinusTolerant<true>(unplaced.fDz);
0132 }
0133 if (ForInside && completelyOutside) return;
0134
0135 Real_v dist[4];
0136 #ifdef VECGEOM_PLANESHELL
0137 if (!ForInside) {
0138 unplaced.GetPlanes()->GenericKernelForContainsAndInside<Real_v, true>(point, completelyInside, completelyOutside);
0139 return;
0140 }
0141 unplaced.GetPlanes()->DistanceToPoint(point, dist);
0142 #else
0143
0144 TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0145 for (unsigned int i = 0; i < 4; ++i) {
0146 dist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0147 }
0148
0149 #endif
0150 if (ForInside) {
0151
0152 const Real_v maxSide = Max(Max(dist[0], dist[1]), Max(dist[2], dist[3]));
0153 completelyOutside = maxSide > Real_v(MakePlusTolerant<true>(0.));
0154 completelyInside = completelyInside && maxSide < Real_v(MakeMinusTolerant<true>(0.));
0155 } else {
0156 for (unsigned int i = 0; i < 4; ++i)
0157 completelyOutside = completelyOutside || dist[i] > Real_v(MakePlusTolerant<true>(0.));
0158 }
0159 }
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 template <typename Real_v>
0175 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToIn(UnplacedStruct_t const &unplaced,
0176 Vector3D<Real_v> const &point,
0177 Vector3D<Real_v> const &dir,
0178 Real_v const &stepMax, Real_v &distance)
0179 {
0180 (void)stepMax;
0181 distance = kInfLength;
0182
0183
0184
0185
0186
0187
0188 Real_v signZdir = Sign(dir.z());
0189 Real_v max = signZdir * unplaced.fDz - point.z();
0190
0191
0192
0193 bool done = signZdir * max < Real_v(MakePlusTolerant<true>(0.0));
0194
0195
0196 if (done) return;
0197
0198
0199
0200
0201 Real_v invdir = Real_v(1.0) / NonZero(dir.z());
0202 Real_v smax = max * invdir;
0203 Real_v smin = -(signZdir * unplaced.fDz + point.z()) * invdir;
0204
0205
0206
0207
0208
0209 #ifdef VECGEOM_PLANESHELL
0210
0211 Real_v disttoplanes = unplaced.GetPlanes()->DistanceToIn(point, dir, smin, smax);
0212 if (!done) distance = disttoplanes;
0213
0214 #else
0215
0216
0217
0218
0219 Real_v pdist[4], comp[4], vdist[4];
0220
0221
0222
0223 TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0224 for (unsigned int i = 0; i < 4; ++i) {
0225
0226
0227 pdist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0228
0229
0230
0231 comp[i] = fPlanes[i].fA * dir.x() + fPlanes[i].fB * dir.y() + fPlanes[i].fC * dir.z();
0232
0233 vdist[i] = -pdist[i] / NonZero(comp[i]);
0234 }
0235
0236
0237 for (int i = 0; i < 4; ++i) {
0238
0239 done = done || (pdist[i] > Real_v(MakePlusTolerant<true>(0.)) && comp[i] >= Real_v(0.));
0240
0241 done = done || (pdist[i] > Real_v(MakeMinusTolerant<true>(0.)) && comp[i] > Real_v(0.));
0242 }
0243
0244 if (done) return;
0245
0246
0247 for (unsigned int i = 0; i < 4; ++i) {
0248
0249 bool posPoint = pdist[i] > Real_v(MakeMinusTolerant<true>(0.));
0250 bool posDir = comp[i] > 0;
0251
0252
0253 bool interceptFromInside = (!posPoint && posDir);
0254 bool interceptFromOutside = (posPoint && !posDir);
0255
0256
0257 if (interceptFromInside && vdist[i] < smax) smax = vdist[i];
0258 if (interceptFromOutside && vdist[i] > smin) smin = vdist[i];
0259 }
0260
0261 if (!done && smin <= smax) distance = smin;
0262 if (distance < Real_v(MakeMinusTolerant<true>(0.0))) distance = Real_v(-1.);
0263 #endif
0264 }
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278 template <typename Real_v>
0279 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOut(UnplacedStruct_t const &unplaced,
0280 Vector3D<Real_v> const &point,
0281 Vector3D<Real_v> const &dir,
0282 Real_v const &stepMax, Real_v &distance,
0283 SurfaceHitView<Real_v> *hitInfo = nullptr)
0284 {
0285 (void)stepMax;
0286 const Real_v distanceTolerance = kToleranceDist<Real_v>;
0287 const Real_v directionTolerance = kToleranceStrict<Real_v> * Real_v(unplaced.fInvProjectionScale);
0288 int exitFace = -1;
0289 if (hitInfo) hitInfo->Clear();
0290
0291
0292 bool outside = Abs(point.z()) > unplaced.fDz + distanceTolerance;
0293 distance = InfinityLength<Real_v>();
0294
0295
0296
0297
0298
0299 if (Abs(dir.z()) > directionTolerance) {
0300 if (Abs(point.z()) - unplaced.fDz >= -distanceTolerance && point.z() * dir.z() > Real_v(0.)) {
0301 distance = Real_v(0.);
0302 exitFace = point.z() < Real_v(0.) ? 4 : 5;
0303 } else {
0304
0305 distance = (Sign(dir.z()) * unplaced.fDz - point.z()) / dir.z();
0306 exitFace = dir.z() < Real_v(0.) ? 4 : 5;
0307 }
0308 }
0309
0310
0311
0312
0313
0314 #if defined(VECGEOM_PLANESHELL) && defined(VECGEOM_VC) && defined(VECGEOM_QUADRILATERAL_ACCELERATION)
0315 unplaced.GetPlanes()->DistanceToOut(point, dir, distanceTolerance, directionTolerance, outside, distance,
0316 hitInfo ? &exitFace : nullptr);
0317 #else
0318 #ifdef VECGEOM_PLANESHELL
0319 auto const *planes = unplaced.GetPlanes();
0320
0321 for (unsigned int i = 0; i < 2; ++i) {
0322 Real_v pdist = planes->fB[i] * point.y() + planes->fC[i] * point.z() + planes->fD[i];
0323 Real_v proj = planes->fB[i] * dir.y() + planes->fC[i] * dir.z();
0324
0325 outside |= pdist > distanceTolerance;
0326 if (pdist < -distanceTolerance) {
0327 if (proj > directionTolerance) {
0328 Real_v vdist = -pdist / proj;
0329 if (vdist < distance) {
0330 distance = vdist;
0331 exitFace = i;
0332 }
0333 }
0334 } else if (pdist <= distanceTolerance && proj > directionTolerance) {
0335 distance = Real_v(0.);
0336 exitFace = i;
0337 }
0338 }
0339 for (unsigned int i = 2; i < 4; ++i) {
0340 Real_v pdist = planes->fA[i] * point.x() + planes->fB[i] * point.y() + planes->fC[i] * point.z() + planes->fD[i];
0341 Real_v proj = planes->fA[i] * dir.x() + planes->fB[i] * dir.y() + planes->fC[i] * dir.z();
0342
0343 outside |= pdist > distanceTolerance;
0344 if (pdist < -distanceTolerance) {
0345 if (proj > directionTolerance) {
0346 Real_v vdist = -pdist / proj;
0347 if (vdist < distance) {
0348 distance = vdist;
0349 exitFace = i;
0350 }
0351 }
0352 } else if (pdist <= distanceTolerance && proj > directionTolerance) {
0353 distance = Real_v(0.);
0354 exitFace = i;
0355 }
0356 }
0357 #else
0358 Real_v pdist[4], proj[4], vdist[4];
0359 EvaluateTrack<Real_v>(unplaced, point, dir, pdist, proj, vdist);
0360
0361 for (unsigned int i = 0; i < 4; ++i) {
0362 outside |= pdist[i] > distanceTolerance;
0363 if (pdist[i] < -distanceTolerance) {
0364 if (proj[i] > directionTolerance && vdist[i] < distance) {
0365 distance = vdist[i];
0366 exitFace = i;
0367 }
0368 } else if (pdist[i] <= distanceTolerance && proj[i] > directionTolerance) {
0369 distance = Real_v(0.);
0370 exitFace = i;
0371 }
0372 }
0373 #endif
0374 #endif
0375
0376 if (outside) {
0377 distance = Real_v(-1.);
0378 return;
0379 }
0380 if (hitInfo && exitFace >= 0) {
0381 hitInfo->SetLeftBehind(true);
0382 if (hitInfo->WantsNormal()) hitInfo->SetNormal(unplaced.normals[exitFace]);
0383 }
0384 }
0385
0386
0387
0388
0389
0390
0391 template <typename Real_v>
0392 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToIn(UnplacedStruct_t const &unplaced,
0393 Vector3D<Real_v> const &point, Real_v &safety)
0394 {
0395 safety = Abs(point.z()) - unplaced.fDz;
0396
0397 #ifdef VECGEOM_PLANESHELL
0398
0399 unplaced.GetPlanes()->SafetyToIn(point, safety);
0400 #else
0401
0402 TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0403 Real_v dist[4];
0404 for (int i = 0; i < 4; ++i) {
0405 dist[i] = fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD;
0406 }
0407
0408 Real_v safmax = Max(Max(dist[0], dist[1]), Max(dist[2], dist[3]));
0409 if (safmax > safety) safety = safmax;
0410 #endif
0411 }
0412
0413
0414
0415
0416
0417
0418 template <typename Real_v>
0419 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToOut(UnplacedStruct_t const &unplaced,
0420 Vector3D<Real_v> const &point, Real_v &safety)
0421 {
0422
0423 safety = unplaced.fDz - Abs(point.z());
0424
0425 #ifdef VECGEOM_PLANESHELL
0426
0427 unplaced.GetPlanes()->SafetyToOut(point, safety);
0428 #else
0429
0430 TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0431
0432
0433 Real_v dist[4];
0434 for (int i = 0; i < 4; ++i) {
0435 dist[i] = -(fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD);
0436 }
0437
0438 Real_v safmin = Min(Min(dist[0], dist[1]), Min(dist[2], dist[3]));
0439 if (safmin < safety) safety = safmin;
0440 #endif
0441 }
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453 template <typename Real_v>
0454 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static Vector3D<Real_v> NormalKernel(UnplacedStruct_t const &unplaced,
0455 Vector3D<Real_v> const &point,
0456 bool &valid)
0457 {
0458
0459 VECGEOM_CONST Precision delta = 1000. * kTolerance;
0460 Vector3D<Real_v> normal, cornerNormal;
0461 Real_v safety = InfinityLength<Real_v>();
0462 bool edge = false;
0463
0464 #ifdef VECGEOM_PLANESHELL
0465
0466 safety = unplaced.GetPlanes()->NormalKernel(point, normal, edge);
0467
0468 #else
0469
0470 unsigned char surfaces = 0;
0471
0472 TrapSidePlane const *fPlanes = unplaced.GetPlanes();
0473 Real_v dist[4];
0474 for (int i = 0; i < 4; ++i) {
0475 dist[i] = Abs(fPlanes[i].fA * point.x() + fPlanes[i].fB * point.y() + fPlanes[i].fC * point.z() + fPlanes[i].fD);
0476
0477 if (dist[i] < safety) {
0478 normal = unplaced.normals[i];
0479 safety = dist[i];
0480 }
0481
0482 if (dist[i] < kTolerance) {
0483 surfaces++;
0484 cornerNormal += unplaced.normals[i];
0485 }
0486 }
0487
0488 if (surfaces > 1) {
0489
0490 normal = cornerNormal;
0491 edge = true;
0492 }
0493 #endif
0494
0495
0496 Real_v safz = Abs(Abs(point[2]) - unplaced.fDz);
0497 if (edge && safz < kTolerance) {
0498
0499 normal += Vector3D<Real_v>(0., 0., Sign(point.z()));
0500 } else {
0501 if (safz < safety) {
0502 normal.Set(0., 0., Sign(point.z()));
0503 safety = safz;
0504 }
0505 }
0506 valid = Abs(safety) <= delta;
0507
0508 normal.Normalize();
0509 return normal;
0510 }
0511 };
0512
0513 }
0514 }
0515
0516 #endif