Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-24 10:41:51

0001 //===-- kernel/HypeImplementation.h - Instruction class definition -------*- C++ -*-===//
0002 //
0003 //                     GeantV - VecGeom
0004 //
0005 // This file is distributed under the LGPL
0006 // License. See LICENSE.TXT for details.
0007 //
0008 //===----------------------------------------------------------------------===//
0009 /// @file HypeImplementation.h
0010 /// @brief Navigation kernels for the hyperboloid shape.
0011 /// @author Marilena Bandieramonte (marilena.bandieramonte@cern.ch)
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 // different SafetyToIn implementations
0025 // #define ACCURATE_BB
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 /// @brief Implements Hype navigation and classification kernels.
0043 /// @details The template parameter selects whether the inner hyperbolic surface
0044 /// is known to exist at compile time or must be checked from the runtime
0045 /// `HypeStruct`.
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   /// @brief Fill the optional normal and supporting-plane result for a selected exit.
0054   /// @details Caps and zero-stereo outer cylinders support the complete solid.
0055   /// Hyperbolic and inner walls do not, but still report their outward normal
0056   /// (the surface gradient, negated on the inner wall).
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       // The gradient vanishes at the apex of a degenerate (rmin = 0) inner
0079       // surface, where no surface normal exists: fall back to the ray direction.
0080       const Real_v mag2 = normal.Mag2();
0081       hitInfo->SetNormal(mag2 > Real_v(0.) ? normal / Sqrt(mag2) : direction);
0082     }
0083   }
0084 
0085   /// @brief Test whether a local point is contained in or on the Hype.
0086   /// @tparam Real_v Floating-point scalar type.
0087   /// @param hype Hype runtime data.
0088   /// @param point Local point to test.
0089   /// @param[out] inside Set to true unless the point is completely outside.
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   /// @brief Classify a local point as inside, outside, or surface.
0100   /// @tparam Real_v Floating-point scalar type.
0101   /// @param hype Hype runtime data.
0102   /// @param point Local point to classify.
0103   /// @param[out] inside Set to `kInside`, `kOutside`, or `kSurface`.
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   /// @brief Compute strict inside/outside flags for point classification.
0116   /// @details Classification combines z extent, outer hyperbolic surface, and
0117   /// the optional inner hyperbolic surface. Points in tolerance bands can leave
0118   /// both flags false.
0119   /// @tparam Real_v Floating-point scalar type.
0120   /// @tparam ForInside When true, also compute the strict-inside flag.
0121   /// @param hype Hype runtime data.
0122   /// @param point Local point to classify.
0123   /// @param[out] completelyinside Set when the point is strictly inside.
0124   /// @param[out] completelyoutside Set when the point is clearly outside.
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   /// @brief Compute the first valid entry distance.
0153   /// @details Surface starts moving into material return zero, clearly inside
0154   /// starts return `-1`, and misses keep `kInfLength`. Candidate hits are
0155   /// checked against the z caps and active inner/outer hyperbolic surfaces.
0156   /// @tparam Real_v Floating-point scalar type.
0157   /// @param hype Hype runtime data.
0158   /// @param point Local start point.
0159   /// @param direction Unit local direction.
0160   /// @param stepMax Unused by this implementation.
0161   /// @param[out] distance Entry distance, `0`, `-1`, or `kInfLength`.
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 & /*stepMax*/, 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       // If it hits inner hyperbolic surface then distance will be the distance to inner hyperbolic surface
0217       // Or if the point is on the inner Hyperbolic surface but going out then the distance will be the distance to
0218       // opposite inner hyperbolic surface.
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   /// @brief Compute the first valid exit distance.
0227   /// @details Surface starts moving out of material return zero, clearly
0228   /// outside starts return `-1`, and misses keep `kInfLength`. Exit candidates
0229   /// are the z cap, outer hyperbolic surface, and optional inner surface.
0230   /// @tparam Real_v Floating-point scalar type.
0231   /// @param hype Hype runtime data.
0232   /// @param point Local start point.
0233   /// @param direction Unit local direction.
0234   /// @param stepMax Unused by this implementation.
0235   /// @param[out] distance Exit distance, `0`, `-1`, or `kInfLength`.
0236   /// @param[out] hitInfo Optional exit-normal destination and supporting-plane result.
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 & /* stepMax */, 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         // The lateral zero-exit predicate also accepts the infinite wall's
0259         // extension. Only its finite z span can supply boundary-hit output.
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   /// @brief Compute the first valid exit distance and optionally return a supporting normal.
0294   /// @tparam Real_v Floating-point scalar type.
0295   /// @param hype Hype runtime data.
0296   /// @param point Local point, expected inside or on the boundary.
0297   /// @param direction Unit local direction.
0298   /// @param stepMax Unused by this implementation.
0299   /// @param[out] distance Exit distance, or the existing wrong-side sentinel.
0300   /// @param[out] hitInfo Optional normal storage and supporting-plane result.
0301   /// @details Only caps and cylindrical outer walls set `leftBehind`. Every finite exit supplies a requested normal;
0302   /// wrong-side starts leave caller-owned normal storage untouched.
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   /// @brief Compute safety from an exterior point to the Hype.
0317   /// @details Returns `-1` for clearly inside starts, zero for points in the
0318   /// classification tolerance region, and otherwise estimates the nearest cap
0319   /// or hyperbolic-surface approach.
0320   /// @tparam Real_v Floating-point scalar type.
0321   /// @param hype Hype runtime data.
0322   /// @param point Local point, expected outside.
0323   /// @param[out] safety Safety estimate to the solid.
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     // New Simple Algo
0333     safety = 0.;
0334     // If point is inside then safety should be -1.
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         // The rim is nearest when the displacement lies in its outward normal
0359         // cone. Otherwise the endcap tangent to the convex inner r(z) profile
0360         // gives a lower bound without another square root or division.
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   /// @brief Compute safety from an interior point to leave the Hype.
0384   /// @details Returns `-1` for clearly outside starts, zero for points in the
0385   /// classification tolerance region, and otherwise the minimum of cap, outer
0386   /// surface, and optional inner-surface safety estimates.
0387   /// @tparam Real_v Floating-point scalar type.
0388   /// @param hype Hype runtime data.
0389   /// @param point Local point, expected inside.
0390   /// @param[out] safety Safety estimate to the boundary.
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 } // namespace VECGEOM_IMPL_NAMESPACE
0425 } // namespace vecgeom
0426 
0427 #endif // VECGEOM_VOLUMES_KERNEL_HYPEIMPLEMENTATION_H_