Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:57

0001 /// \file BVHSafetyEstimator.h
0002 /// \author Guilherme Amadio
0003 
0004 #ifndef VECGEOM_NAVIGATION_BVHSAFETYESTIMATOR_H_
0005 #define VECGEOM_NAVIGATION_BVHSAFETYESTIMATOR_H_
0006 
0007 #include "VecGeom/management/BVHManager.h"
0008 #include "VecGeom/navigation/VSafetyEstimator.h"
0009 
0010 namespace vecgeom {
0011 inline namespace VECGEOM_IMPL_NAMESPACE {
0012 
0013 /**
0014  * @brief Safety estimator class using the bounding volume hierarchy of each
0015  * logical volume for acceleration.
0016  */
0017 
0018 class BVHSafetyEstimator : public VSafetyEstimatorHelper<BVHSafetyEstimator> {
0019 private:
0020   /** Constructor. Private since this is a singleton class accessed only via the @c Instance() static method. */
0021   VECCORE_ATT_DEVICE
0022   BVHSafetyEstimator() : VSafetyEstimatorHelper<BVHSafetyEstimator>() {}
0023 
0024 public:
0025   static constexpr const char *gClassNameString = "BVHSafetyEstimator";
0026 
0027 #ifndef VECCORE_CUDA
0028   /** Return instance of this singleton class. */
0029   static VSafetyEstimator *Instance()
0030   {
0031     static BVHSafetyEstimator instance;
0032     return &instance;
0033   }
0034 #else
0035   // If used on device, this needs to be implemented in a .cu file rather than in this header
0036   // This hack is used also by NewSimpleNavigator, implemented in LogicalVolume.cpp
0037   // This is now implemented in BVHManager.cu
0038   VECCORE_ATT_DEVICE
0039   static VSafetyEstimator *Instance();
0040 #endif
0041 
0042   /**
0043    * Compute safety of a point given in the local coordinates of the placed volume @p pvol.
0044    * @param[in] localpoint Point in the local coordinates of the placed volume.
0045    * @param[in] pvol Placed volume.
0046    */
0047   VECCORE_ATT_HOST_DEVICE
0048   Precision ComputeSafetyForLocalPoint(Vector3D<Precision> const &localpoint, VPlacedVolume const *pvol) const final
0049   {
0050     Precision safety = pvol->SafetyToOut(localpoint);
0051 
0052     if (safety > 0.0 && pvol->GetDaughters().size() > 0)
0053       safety = BVHManager::GetBVH(pvol->GetLogicalVolume())->ComputeSafety(localpoint, safety);
0054 
0055     return safety;
0056   }
0057 
0058   /**
0059    * Compute safety of a point given in the local coordinates of the logical volume @p lvol against
0060    * all its child volumes. Uses the bounding volume hierarchy associated with the logical volume
0061    * for acceleration.
0062    * @param[in] localpoint Point in the local coordinates of the placed volume.
0063    * @param[in] lvol Logical volume.
0064    */
0065   VECCORE_ATT_HOST_DEVICE
0066   Precision ComputeSafetyToDaughtersForLocalPoint(Vector3D<Precision> const &localpoint,
0067                                                   LogicalVolume const *lvol) const final
0068   {
0069     return BVHManager::GetBVH(lvol)->ComputeSafety(localpoint, kInfLength);
0070   }
0071 
0072   /**
0073    * Compute safety of a point given in the local coordinates of the placed volume @p pvol.
0074    * @param[in] localpoint Points in SIMD layout in the local coordinates of the placed volume.
0075    * @param[in] pvol Placed volume.
0076    * @param[in] m Mask of active SIMD lanes.
0077    */
0078   VECCORE_ATT_HOST_DEVICE
0079   Real_v ComputeSafetyForLocalPoint(Vector3D<Real_v> const &localpoint, VPlacedVolume const *pvol, Bool_v m) const final
0080   {
0081     using vecCore::Get;
0082     using vecCore::Set;
0083     using vecCore::VectorSize;
0084 
0085     Real_v safeties(kInfLength);
0086     for (size_t i = 0; i < VectorSize<Real_v>(); ++i) {
0087       if (Get(m, i) == true) {
0088         Vector3D<Precision> point(Get(localpoint[0], i), Get(localpoint[1], i), Get(localpoint[2], i));
0089         Set(safeties, i, ComputeSafetyForLocalPoint(point, pvol));
0090       }
0091     }
0092     return safeties;
0093   }
0094 
0095   /**
0096    * Vector interface to compute safety for a set of points given in the local coordinates of the placed volume @p pvol.
0097    * @param[in] localpoints Points in the local coordinates of the placed volume.
0098    * @param[in] pvol Placed volume.
0099    * @param[out] safeties Output safeties.
0100    */
0101   void ComputeSafetyForLocalPoints(SOA3D<Precision> const &localpoints, VPlacedVolume const *pvol,
0102                                    Precision *safeties) const final
0103   {
0104     for (size_t i = 0; i < localpoints.size(); ++i)
0105       safeties[i] = ComputeSafetyForLocalPoint({localpoints.x(i), localpoints.y(i), localpoints.z(i)}, pvol);
0106   }
0107 };
0108 
0109 } // namespace VECGEOM_IMPL_NAMESPACE
0110 } // namespace vecgeom
0111 
0112 #endif