File indexing completed on 2026-11-24 10:41:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef VECGEOM_VOLUMES_KERNEL_HYPEIMPLEMENTATION_H_
0014 #define VECGEOM_VOLUMES_KERNEL_HYPEIMPLEMENTATION_H_
0015
0016 #include "VecGeom/base/Vector3D.h"
0017 #include "VecGeom/volumes/HypeStruct.h"
0018 #include "VecGeom/volumes/SurfaceHitView.h"
0019 #include "VecGeom/volumes/kernel/GenericKernels.h"
0020 #include <VecCore/VecCore>
0021 #include "VecGeom/volumes/HypeUtilities.h"
0022 #include "VecGeom/volumes/kernel/shapetypes/HypeTypes.h"
0023
0024
0025
0026 #define ACCURATE_BC
0027
0028 namespace vecgeom {
0029
0030 VECGEOM_DEVICE_DECLARE_CONV_TEMPLATE(struct, HypeImplementation, typename);
0031
0032 inline namespace VECGEOM_IMPL_NAMESPACE {
0033
0034 template <typename T>
0035 struct HypeStruct;
0036
0037 template <typename T>
0038 class SPlacedHype;
0039 template <typename T>
0040 class SUnplacedHype;
0041
0042
0043
0044
0045
0046 template <typename hypeTypeT>
0047 struct HypeImplementation {
0048
0049 using UnplacedStruct_t = HypeStruct<Precision>;
0050 using UnplacedVolume_t = SUnplacedHype<hypeTypeT>;
0051 using PlacedShape_t = SPlacedHype<UnplacedVolume_t>;
0052
0053
0054
0055
0056
0057 template <typename Real_v>
0058 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SetExitHit(
0059 UnplacedStruct_t const &hype, Vector3D<Real_v> const &point, Vector3D<Real_v> const &direction, Real_v distance,
0060 HypeUtilities::ExitSurface surface, SurfaceHitView<Real_v> *hitInfo)
0061 {
0062 bool const outerCylinder = surface == HypeUtilities::ExitSurface::kOuter && hype.fTOut2 == 0.;
0063 hitInfo->SetLeftBehind(surface == HypeUtilities::ExitSurface::kZ || outerCylinder);
0064 if (!hitInfo->WantsNormal()) return;
0065
0066 if (surface == HypeUtilities::ExitSurface::kZ) {
0067 hitInfo->SetNormal(Vector3D<Real_v>(Real_v(0.), Real_v(0.), Sign(direction.z())));
0068 } else if (outerCylinder) {
0069 Vector3D<Real_v> const hit = point + distance * direction;
0070 Real_v const invRmax = Real_v(1.) / hype.fRmax;
0071 hitInfo->SetNormal(Vector3D<Real_v>(hit.x() * invRmax, hit.y() * invRmax, Real_v(0.)));
0072 } else {
0073 Vector3D<Real_v> const hit = point + distance * direction;
0074 bool const inner = surface == HypeUtilities::ExitSurface::kInner;
0075 Real_v const t2 = inner ? Real_v(hype.fTIn2) : Real_v(hype.fTOut2);
0076 Vector3D<Real_v> normal(hit.x(), hit.y(), -t2 * hit.z());
0077 if (inner) normal = -normal;
0078
0079
0080 const Real_v mag2 = normal.Mag2();
0081 hitInfo->SetNormal(mag2 > Real_v(0.) ? normal / Sqrt(mag2) : direction);
0082 }
0083 }
0084
0085
0086
0087
0088
0089
0090 template <typename Real_v>
0091 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Contains(UnplacedStruct_t const &hype,
0092 Vector3D<Real_v> const &point, bool &inside)
0093 {
0094 bool unused(false), outside(false);
0095 GenericKernelForContainsAndInside<Real_v, false>(hype, point, unused, outside);
0096 inside = !outside;
0097 }
0098
0099
0100
0101
0102
0103
0104 template <typename Real_v>
0105 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Inside(UnplacedStruct_t const &hype,
0106 Vector3D<Real_v> const &point, Inside_t &inside)
0107 {
0108 inside = EInside::kSurface;
0109 bool completelyinside(false), completelyoutside(false);
0110 GenericKernelForContainsAndInside<Real_v, true>(hype, point, completelyinside, completelyoutside);
0111 if (completelyoutside) inside = EInside::kOutside;
0112 if (completelyinside) inside = EInside::kInside;
0113 }
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 template <typename Real_v, bool ForInside>
0126 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void GenericKernelForContainsAndInside(
0127 UnplacedStruct_t const &hype, Vector3D<Real_v> const &point, bool &completelyinside, bool &completelyoutside)
0128 {
0129 using namespace ::vecgeom::HypeTypes;
0130 Real_v r2 = point.Perp2();
0131 Real_v oRad2 = (hype.fRmax2 + hype.fTOut2 * point.z() * point.z());
0132 Real_v iRad2(0.);
0133
0134 completelyoutside = (Abs(point.z()) > (hype.fDz + hype.zToleranceLevel));
0135 if (completelyoutside) return;
0136 completelyoutside |= (r2 > oRad2 + hype.outerRadToleranceLevel);
0137 if (completelyoutside) return;
0138 if (checkInnerSurfaceTreatment<hypeTypeT>(hype)) {
0139 iRad2 = (hype.fRmin2 + hype.fTIn2 * point.z() * point.z());
0140 completelyoutside |= (r2 < (iRad2 - hype.innerRadToleranceLevel));
0141 }
0142 if (completelyoutside) return;
0143
0144 if (ForInside) {
0145 completelyinside =
0146 (Abs(point.z()) < (hype.fDz - hype.zToleranceLevel)) && (r2 < oRad2 - hype.outerRadToleranceLevel);
0147
0148 if (checkInnerSurfaceTreatment<hypeTypeT>(hype)) completelyinside &= (r2 > (iRad2 + hype.innerRadToleranceLevel));
0149 }
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162 template <typename Real_v>
0163 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToIn(UnplacedStruct_t const &hype,
0164 Vector3D<Real_v> const &point,
0165 Vector3D<Real_v> const &direction,
0166 Real_v const & , Real_v &distance)
0167 {
0168 using namespace ::vecgeom::HypeTypes;
0169 Real_v absZ = Abs(point.z());
0170 distance = kInfLength;
0171 Real_v zDist(kInfLength), dist(kInfLength);
0172 Real_v r = point.Perp2();
0173
0174 bool surfaceCond = HypeUtilities::IsPointOnSurfaceAndMovingInside<Real_v, hypeTypeT>(hype, point, direction);
0175 if (surfaceCond) {
0176 distance = Real_v(0.0);
0177 return;
0178 }
0179
0180 bool cond = HypeUtilities::IsCompletelyInside<Real_v, hypeTypeT>(hype, point);
0181 if (cond) {
0182 distance = Real_v(-1.0);
0183 return;
0184 }
0185
0186 bool isPointAboveOrBelowHypeAndGoingInside = (absZ > hype.fDz) && (point.z() * direction.z() < Real_v(0.));
0187 Real_v rp2(0.);
0188 if (isPointAboveOrBelowHypeAndGoingInside) {
0189 bool hittingZPlane =
0190 HypeUtilities::GetPointOfIntersectionWithZPlane<Real_v, hypeTypeT, true>(hype, point, direction, zDist);
0191 if (hittingZPlane) {
0192 distance = zDist;
0193 return;
0194 }
0195 Real_v x = point.x() + zDist * direction.x();
0196 Real_v y = point.y() + zDist * direction.y();
0197 rp2 = x * x + y * y;
0198 }
0199
0200 bool hittingOuterSurfaceFromOutsideZRange = isPointAboveOrBelowHypeAndGoingInside && (rp2 >= hype.fEndOuterRadius2);
0201 bool hittingOuterSurfaceFromWithinZRange = ((r > ((hype.fRmax2 + hype.fTOut2 * absZ * absZ) + kHalfTolerance))) &&
0202 (absZ >= Real_v(0.)) && (absZ <= hype.fDz);
0203
0204 cond = (hittingOuterSurfaceFromOutsideZRange || hittingOuterSurfaceFromWithinZRange ||
0205 (HypeUtilities::IsPointOnOuterSurfaceAndMovingOutside<Real_v>(hype, point, direction))) &&
0206 HypeHelpers<Real_v, true, false>::GetPointOfIntersectionWithHyperbolicSurface(hype, point, direction, dist);
0207 if (cond) distance = dist;
0208
0209 if (checkInnerSurfaceTreatment<hypeTypeT>(hype)) {
0210 if (cond) return;
0211 bool hittingInnerSurfaceFromOutsideZRange =
0212 isPointAboveOrBelowHypeAndGoingInside && (rp2 <= hype.fEndInnerRadius2);
0213 bool hittingInnerSurfaceFromWithinZRange = (r < ((hype.fRmin2 + hype.fTIn2 * absZ * absZ) - kHalfTolerance)) &&
0214 (absZ >= Real_v(0.)) && (absZ <= hype.fDz);
0215
0216
0217
0218
0219 cond = (hittingInnerSurfaceFromOutsideZRange || hittingInnerSurfaceFromWithinZRange ||
0220 (HypeUtilities::IsPointOnInnerSurfaceAndMovingOutside<Real_v, hypeTypeT>(hype, point, direction))) &&
0221 HypeHelpers<Real_v, true, true>::GetPointOfIntersectionWithHyperbolicSurface(hype, point, direction, dist);
0222 if (cond) distance = dist;
0223 }
0224 }
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 template <typename Real_v, bool RecordHitInfo>
0238 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOutImpl(
0239 UnplacedStruct_t const &hype, Vector3D<Real_v> const &point, Vector3D<Real_v> const &direction,
0240 Real_v const & , Real_v &distance, SurfaceHitView<Real_v> *hitInfo)
0241 {
0242 using namespace ::vecgeom::HypeTypes;
0243 if constexpr (RecordHitInfo) hitInfo->Clear();
0244 distance = kInfLength;
0245 Real_v zDist(kInfLength), dist(kInfLength);
0246
0247 HypeUtilities::ExitSurface hitSurface = HypeUtilities::ExitSurface::kNone;
0248 bool cond;
0249 if constexpr (RecordHitInfo) {
0250 hitSurface = HypeUtilities::SelectSurfaceExit<Real_v, hypeTypeT>(hype, point, direction);
0251 cond = hitSurface != HypeUtilities::ExitSurface::kNone;
0252 } else {
0253 cond = HypeUtilities::IsPointOnSurfaceAndMovingOutside<Real_v, hypeTypeT>(hype, point, direction);
0254 }
0255 if (cond) {
0256 distance = Real_v(0.);
0257 if constexpr (RecordHitInfo) {
0258
0259
0260 if (Abs(point.z()) < hype.fDz + hype.zToleranceLevel)
0261 SetExitHit(hype, point, direction, distance, hitSurface, hitInfo);
0262 }
0263 return;
0264 }
0265
0266 cond = HypeUtilities::IsCompletelyOutside<Real_v, hypeTypeT>(hype, point);
0267 if (cond) {
0268 distance = Real_v(-1.);
0269 return;
0270 }
0271
0272 HypeUtilities::GetPointOfIntersectionWithZPlane<Real_v, hypeTypeT, false>(hype, point, direction, zDist);
0273 if (zDist < Real_v(0.)) zDist = InfinityLength<Real_v>();
0274
0275 HypeHelpers<Real_v, false, false>::GetPointOfIntersectionWithHyperbolicSurface(hype, point, direction, dist);
0276 if (dist < Real_v(0.)) dist = InfinityLength<Real_v>();
0277 distance = Min(zDist, dist);
0278 if constexpr (RecordHitInfo)
0279 hitSurface = zDist <= dist ? HypeUtilities::ExitSurface::kZ : HypeUtilities::ExitSurface::kOuter;
0280
0281 if (checkInnerSurfaceTreatment<hypeTypeT>(hype)) {
0282 HypeHelpers<Real_v, false, true>::GetPointOfIntersectionWithHyperbolicSurface(hype, point, direction, dist);
0283 if (dist < Real_v(0.)) dist = InfinityLength<Real_v>();
0284 if constexpr (RecordHitInfo) {
0285 if (dist < distance) hitSurface = HypeUtilities::ExitSurface::kInner;
0286 }
0287 distance = Min(distance, dist);
0288 }
0289
0290 if constexpr (RecordHitInfo) SetExitHit(hype, point, direction, distance, hitSurface, hitInfo);
0291 }
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303 template <typename Real_v>
0304 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOut(UnplacedStruct_t const &hype,
0305 Vector3D<Real_v> const &point,
0306 Vector3D<Real_v> const &direction,
0307 Real_v const &stepMax, Real_v &distance,
0308 SurfaceHitView<Real_v> *hitInfo = nullptr)
0309 {
0310 if (hitInfo)
0311 DistanceToOutImpl<Real_v, true>(hype, point, direction, stepMax, distance, hitInfo);
0312 else
0313 DistanceToOutImpl<Real_v, false>(hype, point, direction, stepMax, distance, nullptr);
0314 }
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 template <class Real_v>
0325 VECCORE_ATT_HOST_DEVICE static void SafetyToIn(UnplacedStruct_t const &hype, Vector3D<Real_v> const &point,
0326 Real_v &safety)
0327 {
0328 Real_v absZ = Abs(point.z());
0329 Real_v r2 = point.Perp2();
0330 Real_v r = Sqrt(r2);
0331
0332
0333 safety = 0.;
0334
0335 bool compIn(false), compOut(false);
0336 GenericKernelForContainsAndInside<Real_v, true>(hype, point, compIn, compOut);
0337 if (!compIn && !compOut) return;
0338
0339 if (compIn) {
0340 safety = Real_v(-1.0);
0341 return;
0342 }
0343
0344 Real_v sigz = absZ - hype.fDz;
0345 if (sigz > kHalfTolerance) {
0346 if (r < hype.fEndOuterRadius && r > hype.fEndInnerRadius) {
0347 safety = sigz;
0348 return;
0349 }
0350 if (r > hype.fEndOuterRadius) {
0351 Real_v dr = r - hype.fEndOuterRadius;
0352 safety = Sqrt(dr * dr + sigz * sigz);
0353 return;
0354 }
0355 if (r < hype.fEndInnerRadius) {
0356 Real_v dr = r - hype.fEndInnerRadius;
0357 Real_v innerNz = hype.fDz * hype.fTIn2;
0358
0359
0360
0361 if (sigz * hype.fEndInnerRadius < -dr * innerNz)
0362 safety = (sigz * innerNz - dr * hype.fEndInnerRadius) * hype.fEndInnerNormalInv;
0363 else
0364 safety = Sqrt(dr * dr + sigz * sigz);
0365 return;
0366 }
0367 }
0368
0369 if (absZ > Real_v(0.) && absZ < hype.fDz) {
0370 Real_v outerRad2 = hype.fRmax2 + hype.fTOut2 * absZ * absZ;
0371 if (r2 > outerRad2 + kHalfTolerance) {
0372 safety = HypeUtilities::ApproxDistOutside<Real_v>(r, absZ, hype.fRmax, hype.fTOut);
0373 return;
0374 }
0375
0376 Real_v innerRad2 = hype.fRmin2 + hype.fTIn2 * absZ * absZ;
0377 if (r2 < innerRad2 - kHalfTolerance) {
0378 safety = HypeUtilities::ApproxDistInside<Real_v>(r, absZ, hype.fRmin, hype.fTIn2);
0379 }
0380 }
0381 }
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391 template <class Real_v>
0392 VECCORE_ATT_HOST_DEVICE static void SafetyToOut(UnplacedStruct_t const &hype, Vector3D<Real_v> const &point,
0393 Real_v &safety)
0394 {
0395 using namespace ::vecgeom::HypeTypes;
0396 safety = Real_v(0.);
0397 Real_v r = Sqrt(point.x() * point.x() + point.y() * point.y());
0398 Real_v absZ = Abs(point.z());
0399 bool inside(false), outside(false);
0400
0401 GenericKernelForContainsAndInside<Real_v, true>(hype, point, inside, outside);
0402 if (!inside && !outside) return;
0403
0404 if (outside) {
0405 safety = Real_v(-1.0);
0406 return;
0407 }
0408
0409 Real_v distZ = Abs(absZ - hype.fDz);
0410 Real_v distInner = Real_v(0.);
0411 const bool hasInnerSurface = checkInnerSurfaceTreatment<hypeTypeT>(hype);
0412 if (hasInnerSurface) {
0413 distInner =
0414 hype.fStIn ? HypeUtilities::ApproxDistOutside<Real_v>(r, absZ, hype.fRmin, hype.fTIn) : (r - hype.fRmin);
0415 } else if (!hype.fStIn) {
0416 distInner = InfinityLength<Real_v>();
0417 }
0418
0419 Real_v distOuter = HypeUtilities::ApproxDistInside<Real_v>(r, absZ, hype.fRmax, hype.fTOut2);
0420 safety = Min(distInner, distOuter);
0421 safety = Min(safety, distZ);
0422 }
0423 };
0424 }
0425 }
0426
0427 #endif