Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-20 10:39:07

0001 /*
0002  * CutTubeImplementation.h
0003  *
0004  *  Created on: 03.11.2016
0005  *      Author: mgheata
0006  */
0007 
0008 #ifndef VECGEOM_VOLUMES_KERNEL_CUTTUBEIMPLEMENTATION_H_
0009 #define VECGEOM_VOLUMES_KERNEL_CUTTUBEIMPLEMENTATION_H_
0010 
0011 #include "VecGeom/base/Vector3D.h"
0012 #include "VecGeom/volumes/kernel/GenericKernels.h"
0013 #include "VecGeom/volumes/CutTubeStruct.h"
0014 #include "VecGeom/volumes/SurfaceHitView.h"
0015 #include "TubeImplementation.h"
0016 
0017 namespace vecgeom {
0018 
0019 VECGEOM_DEVICE_FORWARD_DECLARE(struct CutTubeImplementation;);
0020 VECGEOM_DEVICE_DECLARE_CONV(struct, CutTubeImplementation);
0021 
0022 inline namespace VECGEOM_IMPL_NAMESPACE {
0023 
0024 class PlacedCutTube;
0025 class UnplacedCutTube;
0026 
0027 template <typename T>
0028 struct CutTubeStruct;
0029 
0030 struct CutTubeImplementation {
0031 
0032   using PlacedShape_t    = PlacedCutTube;
0033   using UnplacedStruct_t = CutTubeStruct<Precision>;
0034   using UnplacedVolume_t = UnplacedCutTube;
0035 
0036   template <typename Real_v>
0037   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Contains(UnplacedStruct_t const &unplaced,
0038                                                                     Vector3D<Real_v> const &point, bool &inside);
0039 
0040   template <typename Real_v>
0041   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void Inside(UnplacedStruct_t const &unplaced,
0042                                                                   Vector3D<Real_v> const &point, Inside_t &inside);
0043 
0044   template <typename Real_v>
0045   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToIn(UnplacedStruct_t const &unplaced,
0046                                                                         Vector3D<Real_v> const &point,
0047                                                                         Vector3D<Real_v> const &direction,
0048                                                                         Real_v const &stepMax, Real_v &distance);
0049 
0050   template <typename Real_v>
0051   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToInKernel(UnplacedStruct_t const &unplaced,
0052                                                                               Vector3D<Real_v> const &point,
0053                                                                               Vector3D<Real_v> const &direction,
0054                                                                               Real_v const &stepMax, Real_v &distance);
0055 
0056   /**
0057    * @brief Compute the exit distance and optionally return a supporting normal.
0058    * @param unplaced Cut-tube geometry.
0059    * @param point Starting point in the solid frame.
0060    * @param direction Normalized query direction.
0061    * @param stepMax Caller-provided distance limit; existing distance behavior is retained.
0062    * @param[out] distance Exit distance, or the existing wrong-side result.
0063    * @param[out] hit_info Optional supporting-plane flag and caller-owned normal storage.
0064    * @details Only the winning cut plane or tube boundary fills the output. A
0065    * non-supporting exit clears the flag without changing normal storage.
0066    */
0067   template <typename Real_v>
0068   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOut(UnplacedStruct_t const &unplaced,
0069                                                                          Vector3D<Real_v> const &point,
0070                                                                          Vector3D<Real_v> const &direction,
0071                                                                          Real_v const &stepMax, Real_v &distance,
0072                                                                          SurfaceHitView<Real_v> *hit_info = nullptr);
0073 
0074   /// Shared kernel whose ordinary instantiation omits all exit-hit bookkeeping.
0075   template <typename Real_v, bool RecordHitInfo>
0076   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void DistanceToOutImpl(UnplacedStruct_t const &unplaced,
0077                                                                              Vector3D<Real_v> const &point,
0078                                                                              Vector3D<Real_v> const &direction,
0079                                                                              Real_v const &stepMax, Real_v &distance,
0080                                                                              SurfaceHitView<Real_v> *hit_info);
0081 
0082   template <typename Real_v>
0083   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToIn(UnplacedStruct_t const &unplaced,
0084                                                                       Vector3D<Real_v> const &point, Real_v &safety);
0085 
0086   template <typename Real_v>
0087   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void SafetyToOut(UnplacedStruct_t const &unplaced,
0088                                                                        Vector3D<Real_v> const &point, Real_v &safety);
0089 
0090   template <typename Real_v>
0091   VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE static void NormalKernel(UnplacedStruct_t const &unplaced,
0092                                                                         Vector3D<Real_v> const &point,
0093                                                                         Vector3D<Real_v> &normal, bool &valid);
0094 }; // End struct CutTubeImplementation
0095 
0096 //********************************
0097 //**** implementations start here
0098 //********************************/
0099 
0100 //______________________________________________________________________________
0101 template <typename Real_v>
0102 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::Contains(UnplacedStruct_t const &unplaced,
0103                                                                                   Vector3D<Real_v> const &point,
0104                                                                                   bool &contains)
0105 {
0106   contains = false;
0107 
0108   bool inside_cutplanes = false;
0109   unplaced.GetCutPlanes().Contains<Real_v>(point, inside_cutplanes);
0110 
0111   if (!inside_cutplanes) return;
0112 
0113   TubeImplementation<TubeTypes::UniversalTube>::Contains<Real_v>(unplaced.GetTubeStruct(), point, contains);
0114 }
0115 
0116 //______________________________________________________________________________
0117 template <typename Real_v>
0118 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::Inside(UnplacedStruct_t const &unplaced,
0119                                                                                 Vector3D<Real_v> const &point,
0120                                                                                 Inside_t &inside)
0121 {
0122   Inside_t inside_cutplanes = EInside::kOutside;
0123   unplaced.GetCutPlanes().Inside<Real_v>(point, inside_cutplanes);
0124 
0125   if (inside_cutplanes == EInside::kOutside) {
0126     inside = inside_cutplanes;
0127     return;
0128   }
0129 
0130   TubeImplementation<TubeTypes::UniversalTube>::Inside<Real_v>(unplaced.GetTubeStruct(), point, inside);
0131 
0132   if (inside_cutplanes == EInside::kSurface && inside != EInside::kOutside) inside = inside_cutplanes;
0133 }
0134 
0135 //______________________________________________________________________________
0136 template <typename Real_v>
0137 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::DistanceToIn(UnplacedStruct_t const &unplaced,
0138                                                                                       Vector3D<Real_v> const &pointT,
0139                                                                                       Vector3D<Real_v> const &dir,
0140                                                                                       Real_v const &stepMax,
0141                                                                                       Real_v &distance)
0142 {
0143   Vector3D<Real_v> point = pointT;
0144   const Real_v ptDistSq  = point.Mag2();
0145   Real_v distToMove(0.);
0146   const Precision order = 100.;
0147 
0148   // Move very distant points closer before calling the hot kernel to avoid
0149   // unnecessary work on huge coordinates.
0150   if (ptDistSq > Real_v(order * unplaced.fMaxVal) * Real_v(order * unplaced.fMaxVal)) {
0151     const Real_v ptDist = Sqrt(ptDistSq);
0152     distToMove          = ptDist - Real_v(order * unplaced.fMaxVal);
0153     point += distToMove * dir;
0154   }
0155 
0156   DistanceToInKernel<Real_v>(unplaced, point, dir, stepMax, distance);
0157   distance += distToMove;
0158 }
0159 //______________________________________________________________________________
0160 template <typename Real_v>
0161 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::DistanceToInKernel(
0162     UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &direction,
0163     Real_v const &stepMax, Real_v &distance)
0164 {
0165   Vector3D<Real_v> propagated = point;
0166   distance                    = InfinityLength<Real_v>();
0167 
0168   auto const &cutplanes = unplaced.GetCutPlanes();
0169   auto const &plane0    = cutplanes.GetCutPlane(0);
0170   auto const &plane1    = cutplanes.GetCutPlane(1);
0171   const Real_v pdist0   = plane0.DistPlane(point);
0172   const Real_v pdist1   = plane1.DistPlane(point);
0173   const Real_v ndd0     = direction.Dot(Vector3D<Real_v>(plane0.GetNormal()));
0174   const Real_v ndd1     = direction.Dot(Vector3D<Real_v>(plane1.GetNormal()));
0175 
0176   Inside_t cutplane_state = (pdist0 < Real_v(0.0) && pdist1 < Real_v(0.0)) ? EInside::kInside : EInside::kOutside;
0177   if (Abs(pdist0) < Real_v(kTolerance) || Abs(pdist1) < Real_v(kTolerance)) {
0178     cutplane_state = EInside::kSurface;
0179   }
0180 
0181   Inside_t tube_state = EInside::kOutside;
0182   // Outside either cut plane, the tube classification cannot change the result.
0183   if (cutplane_state != EInside::kOutside)
0184     TubeImplementation<TubeTypes::UniversalTube>::Inside<Real_v>(unplaced.GetTubeStruct(), point, tube_state);
0185 
0186   if (cutplane_state == EInside::kSurface && tube_state != EInside::kOutside) {
0187     if ((Abs(pdist0) < Real_v(kTolerance) && pdist1 < Real_v(kTolerance) && ndd0 < Real_v(0.)) ||
0188         (Abs(pdist1) < Real_v(kTolerance) && pdist0 < Real_v(kTolerance) && ndd1 < Real_v(0.))) {
0189       distance = Real_v(0.);
0190       return;
0191     }
0192     tube_state = cutplane_state;
0193   }
0194 
0195   if (tube_state == EInside::kInside) {
0196     distance = Real_v(-1.);
0197     return;
0198   }
0199 
0200   Real_v dplanes = Real_v(0.);
0201   if (cutplane_state != EInside::kInside) {
0202     Real_v dplane0 = -InfinityLength<Real_v>();
0203     if (ndd0 < Real_v(0.) && pdist0 > Real_v(-kTolerance)) dplane0 = -pdist0 / NonZero(ndd0);
0204     Real_v dplane1 = -InfinityLength<Real_v>();
0205     if (ndd1 < Real_v(0.) && pdist1 > Real_v(-kTolerance)) dplane1 = -pdist1 / NonZero(ndd1);
0206     dplanes              = Max(dplane0, dplane1);
0207     const bool hitplanes = Abs(dplanes) < stepMax && dplanes > Real_v(-kTolerance);
0208     if (!hitplanes) return;
0209 
0210     propagated += dplanes * direction;
0211     // Hitting a cut plane does not guarantee that the propagated point is
0212     // already between the two cut planes.
0213     cutplanes.Inside<Real_v>(propagated, cutplane_state);
0214     if (cutplane_state == EInside::kOutside) return;
0215 
0216     TubeImplementation<TubeTypes::UniversalTube>::Inside<Real_v>(unplaced.GetTubeStruct(), propagated, tube_state);
0217     if (tube_state != EInside::kOutside) {
0218       distance = Abs(dplanes) < Real_v(kTolerance) ? Real_v(0.) : dplanes;
0219       return;
0220     }
0221   }
0222 
0223   Real_v dexit = InfinityLength<Real_v>();
0224   cutplanes.DistanceToOut<Real_v>(propagated, direction, dexit);
0225 
0226   Real_v dtube = InfinityLength<Real_v>();
0227   TubeImplementation<TubeTypes::UniversalTube>::DistanceToInKernel<Real_v>(unplaced.GetTubeStruct(), propagated,
0228                                                                            direction, stepMax, dtube);
0229   // Propagation to the cut planes can put the point inside the tube, so
0230   // DistanceToIn may return -1. Treat that as an immediate tube hit.
0231   if (dtube < Real_v(0.)) dtube = Real_v(0.);
0232   if (dexit < dtube) return;
0233 
0234   const Real_v candidate = dtube + dplanes;
0235   if (candidate < stepMax) distance = candidate;
0236 }
0237 
0238 //______________________________________________________________________________
0239 template <typename Real_v, bool RecordHitInfo>
0240 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::DistanceToOutImpl(
0241     UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &direction,
0242     Real_v const &stepMax, Real_v &distance, SurfaceHitView<Real_v> *hit_info)
0243 {
0244   if constexpr (RecordHitInfo) hit_info->Clear();
0245 
0246   auto const &cutplanes = unplaced.GetCutPlanes();
0247   auto const &plane0    = cutplanes.GetCutPlane(0);
0248   auto const &plane1    = cutplanes.GetCutPlane(1);
0249   const Real_v pdist0   = plane0.DistPlane(point);
0250   const Real_v pdist1   = plane1.DistPlane(point);
0251   const Real_v ndd0     = direction.Dot(Vector3D<Real_v>(plane0.GetNormal()));
0252   const Real_v ndd1     = direction.Dot(Vector3D<Real_v>(plane1.GetNormal()));
0253 
0254   [[maybe_unused]] bool outsideCutPlanes = false;
0255   Real_v dist0                           = InfinityLength<Real_v>();
0256   if (pdist0 > Real_v(kTolerance)) {
0257     dist0 = -InfinityLength<Real_v>();
0258     if constexpr (RecordHitInfo) outsideCutPlanes = true;
0259   } else if (ndd0 > Real_v(0.) && pdist0 < Real_v(kTolerance)) {
0260     dist0 = -pdist0 / NonZero(ndd0);
0261   }
0262 
0263   Real_v dist1 = InfinityLength<Real_v>();
0264   if (pdist1 > Real_v(kTolerance)) {
0265     dist1 = -InfinityLength<Real_v>();
0266     if constexpr (RecordHitInfo) outsideCutPlanes = true;
0267   } else if (ndd1 > Real_v(0.) && pdist1 < Real_v(kTolerance)) {
0268     dist1 = -pdist1 / NonZero(ndd1);
0269   }
0270 
0271   [[maybe_unused]] int cutPlaneIndex = dist0 <= dist1 ? 0 : 1;
0272   distance                           = Min(dist0, dist1);
0273   if (distance < Real_v(0.)) {
0274     if ((Abs(pdist0) < Real_v(kTolerance) && pdist1 < Real_v(kTolerance) && ndd0 > -kToleranceDist<Real_v>) ||
0275         (Abs(pdist1) < Real_v(kTolerance) && pdist0 < Real_v(kTolerance) && ndd1 > -kToleranceDist<Real_v>)) {
0276       distance = Real_v(0.);
0277       if constexpr (RecordHitInfo) {
0278         // Preserve the ordinary short-circuit test; identify the cap only on request.
0279         cutPlaneIndex =
0280             Abs(pdist0) < Real_v(kTolerance) && pdist1 < Real_v(kTolerance) && ndd0 > -kToleranceDist<Real_v> ? 0 : 1;
0281       }
0282     }
0283   }
0284 
0285   Real_v dtube = InfinityLength<Real_v>();
0286   if constexpr (RecordHitInfo) {
0287     using TubeImpl                             = TubeImplementation<TubeTypes::UniversalTube>;
0288     typename TubeImpl::ExitSurface tubeSurface = TubeImpl::ExitSurface::kNone;
0289     TubeImpl::template DistanceToOutImpl<Real_v, true, false>(unplaced.GetTubeStruct(), point, direction, stepMax,
0290                                                               dtube, nullptr, &tubeSurface);
0291     if (dtube < distance) {
0292       distance = dtube;
0293       TubeImpl::SetExitHit(unplaced.GetTubeStruct(), point, direction, dtube, tubeSurface, hit_info);
0294       return;
0295     }
0296 
0297     // Use the existing plane rejection, not the sign of a tolerated crossing.
0298     if (!outsideCutPlanes) {
0299       hit_info->SetLeftBehind(true);
0300       if (hit_info->WantsNormal()) hit_info->SetNormal(Vector3D<Real_v>(cutplanes.GetNormal(cutPlaneIndex)));
0301     }
0302   } else {
0303     TubeImplementation<TubeTypes::UniversalTube>::DistanceToOutImpl<Real_v, false, false>(
0304         unplaced.GetTubeStruct(), point, direction, stepMax, dtube, nullptr);
0305     if (dtube < distance) distance = dtube;
0306   }
0307 }
0308 
0309 //______________________________________________________________________________
0310 template <typename Real_v>
0311 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::DistanceToOut(
0312     UnplacedStruct_t const &unplaced, Vector3D<Real_v> const &point, Vector3D<Real_v> const &direction,
0313     Real_v const &stepMax, Real_v &distance, SurfaceHitView<Real_v> *hit_info)
0314 {
0315   if (hit_info)
0316     DistanceToOutImpl<Real_v, true>(unplaced, point, direction, stepMax, distance, hit_info);
0317   else
0318     DistanceToOutImpl<Real_v, false>(unplaced, point, direction, stepMax, distance, nullptr);
0319 }
0320 
0321 //______________________________________________________________________________
0322 template <typename Real_v>
0323 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::SafetyToIn(UnplacedStruct_t const &unplaced,
0324                                                                                     Vector3D<Real_v> const &point,
0325                                                                                     Real_v &safety)
0326 {
0327   unplaced.GetCutPlanes().SafetyToIn<Real_v>(point, safety);
0328 
0329   Real_v saftube;
0330   TubeImplementation<TubeTypes::UniversalTube>::SafetyToIn<Real_v>(unplaced.GetTubeStruct(), point, saftube);
0331 
0332   if (saftube > safety) safety = saftube;
0333 }
0334 
0335 //______________________________________________________________________________
0336 template <typename Real_v>
0337 VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::SafetyToOut(UnplacedStruct_t const &unplaced,
0338                                                                 Vector3D<Real_v> const &point, Real_v &safety)
0339 {
0340   unplaced.GetCutPlanes().SafetyToOut<Real_v>(point, safety);
0341 
0342   Real_v saftube;
0343   TubeImplementation<TubeTypes::UniversalTube>::SafetyToOut<Real_v>(unplaced.GetTubeStruct(), point, saftube);
0344 
0345   if (saftube < safety) safety = saftube;
0346 }
0347 
0348 //______________________________________________________________________________
0349 template <typename Real_v>
0350 VECCORE_ATT_HOST_DEVICE void CutTubeImplementation::NormalKernel(UnplacedStruct_t const &unplaced,
0351                                                                  Vector3D<Real_v> const &point,
0352                                                                  Vector3D<Real_v> &normal, bool &valid)
0353 {
0354   valid = true;
0355 
0356   Real_v safcut;
0357   unplaced.GetCutPlanes().SafetyToOut<Real_v>(point, safcut);
0358 
0359   Real_v saftube;
0360   TubeImplementation<TubeTypes::UniversalTube>::SafetyToOut<Real_v>(unplaced.GetTubeStruct(), point, saftube);
0361 
0362   if (Abs(saftube) < Abs(safcut)) {
0363     TubeImplementation<TubeTypes::UniversalTube>::NormalKernel<Real_v>(unplaced.GetTubeStruct(), point, normal, valid);
0364     return;
0365   }
0366 
0367   normal = unplaced.GetCutPlanes().GetNormal(point.z() < 0 ? 0 : 1);
0368 }
0369 
0370 //*****************************
0371 //**** Implementations end here
0372 //*****************************
0373 } // namespace VECGEOM_IMPL_NAMESPACE
0374 } // namespace vecgeom
0375 
0376 #endif /* VECGEOM_VOLUMES_KERNEL_CUTTUBEIMPLEMENTATION_H_ */