Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-19 10:43:56

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // SPDX-FileCopyrightText: 2020 CERN
0003 // SPDX-FileCopyrightText: 2025 Celeritas contributors
0004 // SPDX-License-Identifier: Apache-2.0
0005 //---------------------------------------------------------------------------//
0006 /*!
0007  * \file geocel/vg/detail/BVHNavigator.hh
0008  * \brief Bounding Volume Hierarchy navigator directly derived from AdePT
0009  *
0010  * Original source:
0011  * https://github.com/apt-sim/AdePT/blob/bafab78519faafde0b8e5055128c2a3610d43d77/base/inc/AdePT/BVHNavigator.h
0012  */
0013 //---------------------------------------------------------------------------//
0014 #pragma once
0015 
0016 #include <VecGeom/base/Global.h>
0017 #include <VecGeom/base/Vector3D.h>
0018 #include <VecGeom/management/BVHManager.h>
0019 #include <VecGeom/navigation/NavStateFwd.h>
0020 #include <VecGeom/navigation/NavigationState.h>
0021 
0022 #include "corecel/Macros.hh"
0023 #include "corecel/math/Algorithms.hh"
0024 #include "geocel/vg/VecgeomTypes.hh"
0025 
0026 #ifdef VECGEOM_ENABLE_CUDA
0027 #    include <VecGeom/backend/cuda/Interface.h>
0028 #endif
0029 
0030 #if CELER_VGNAV == CELER_VGNAV_PATH
0031 #    include <VecGeom/navigation/NavStatePath.h>
0032 #else
0033 #    include <VecGeom/navigation/NavStateIndex.h>
0034 #endif
0035 
0036 namespace celeritas
0037 {
0038 namespace detail
0039 {
0040 //---------------------------------------------------------------------------//
0041 /*!
0042  * Navigate VecGeom 1.x geometry using its BVH.
0043  *
0044  * \deprecated This is only used for VecGeom 1.x and will be removed along
0045  * with it.
0046  */
0047 class BVHNavigator
0048 {
0049   public:
0050     using VgPlacedVol = VgPlacedVolume<MemSpace::native>;
0051     using NavState = VgNavState;
0052 
0053 #ifdef VECGEOM_FLOAT_PRECISION
0054     static constexpr vg_real_type kBoundaryPush = 10 * 1e-3f;
0055 #else
0056     static constexpr vg_real_type kBoundaryPush = 10 * 1e-9;
0057 #endif
0058 
0059     //! Update path (which must be reset in advance)
0060     CELER_FUNCTION static void LocatePointIn(
0061         VgPlacedVol const* vol,
0062         VgReal3 const& point,
0063         NavState& path,
0064         bool top,
0065         VgPlacedVol const* exclude = nullptr)
0066     {
0067         if (top)
0068         {
0069             CELER_ASSERT(vol != nullptr);
0070             if (!vol->UnplacedContains(point))
0071             {
0072                 path.Clear();
0073                 return;
0074             }
0075         }
0076 
0077         CELER_ASSERT(vol);
0078         path.Push(vol);
0079 
0080         VgReal3 currentpoint(point);
0081 
0082         while (vol->GetDaughters().size() > 0)
0083         {
0084             auto* bvh
0085                 = vecgeom::BVHManager::GetBVH(vol->GetLogicalVolume()->id());
0086             CELER_ASSERT(bvh);
0087 
0088             // Note: vol *and* daughterlocalpoint are updated by this call
0089             VgReal3 daughterlocalpoint;
0090             vol = nullptr;
0091             if (!bvh->LevelLocate(
0092                     exclude, currentpoint, vol, daughterlocalpoint))
0093             {
0094                 // Not inside any daughter
0095                 break;
0096             }
0097 
0098             CELER_ASSERT(vol);
0099             path.Push(vol);
0100 
0101             // Transform to daughter
0102             currentpoint = daughterlocalpoint;
0103 
0104             // Only exclude the placed volume once since we could enter it
0105             // again via a different volume history.
0106             exclude = nullptr;
0107         }
0108     }
0109 
0110     // Computes the isotropic safety from the globalpoint.
0111     CELER_FUNCTION static double ComputeSafety(
0112         VgReal3 const& globalpoint, NavState const& state, vg_real_type safety)
0113     {
0114         VgPlacedVol const* pvol = state.Top();
0115         vecgeom::Transformation3D m;
0116         state.TopMatrix(m);
0117         VgReal3 localpoint = m.Transform(globalpoint);
0118 
0119         // need to calc DistanceToOut first
0120         safety = min(safety, pvol->SafetyToOut(localpoint));
0121 
0122         if (safety > 0 && pvol->GetDaughters().size() > 0)
0123         {
0124             auto bvh
0125                 = vecgeom::BVHManager::GetBVH(pvol->GetLogicalVolume()->id());
0126             safety = bvh->ComputeSafety(localpoint, safety);
0127         }
0128 
0129         return safety;
0130     }
0131 
0132     // Computes a step from the globalpoint (which must be in the current
0133     // volume) into globaldir, taking step_limit into account. If a volume is
0134     // hit, the function calls out_state.SetBoundaryState(true) and
0135     //  - removes all exited volumes from out_state if the current volume is
0136     //    left, or
0137     //  - adds the hit daughter volume to out_state if one is hit.
0138     // However the function does _NOT_ relocate the state to the next volume,
0139     // that is entering multiple volumes that share a boundary.
0140     CELER_FUNCTION static double ComputeStepAndNextVolume(
0141         VgReal3 const& globalpoint,
0142         VgReal3 const& globaldir,
0143         vg_real_type step_limit,
0144         NavState const& in_state,
0145         NavState& out_state)
0146     {
0147         // If we are on the boundary, push a bit more
0148         vg_real_type push = in_state.IsOnBoundary() ? kBoundaryPush : 0;
0149 
0150         if (step_limit < push)
0151         {
0152             // Preserve the path without reporting the previous boundary again
0153             in_state.CopyTo(&out_state);
0154             out_state.SetBoundaryState(false);
0155             return step_limit;
0156         }
0157         step_limit -= push;
0158 
0159         // calculate local point/dir from global point/dir
0160         VgReal3 localpoint;
0161         VgReal3 localdir;
0162         // Impl::DoGlobalToLocalTransformation(in_state, globalpoint,
0163         // globaldir, localpoint, localdir);
0164         vecgeom::Transformation3D m;
0165         in_state.TopMatrix(m);
0166         localpoint = m.Transform(globalpoint);
0167         localdir = m.TransformDirection(globaldir);
0168         // The user may want to move point from boundary before computing the
0169         // step
0170         localpoint += push * localdir;
0171 
0172         VgPlacedVol const* hitcandidate = nullptr;
0173         vg_real_type step = ComputeStepAndHit(
0174             localpoint, localdir, step_limit, in_state, out_state, hitcandidate);
0175         step += push;
0176 
0177         if (out_state.IsOnBoundary())
0178         {
0179             if (!hitcandidate)
0180             {
0181                 // Pop back up to the parent volume
0182                 VgPlacedVol const* currentmother = out_state.Top();
0183                 VgReal3 transformed = localpoint;
0184                 // Push the point inside the next volume.
0185                 transformed += (step + kBoundaryPush) * localdir;
0186 
0187                 do
0188                 {
0189                     out_state.Pop();
0190                     transformed
0191                         = currentmother->GetTransformation()->InverseTransform(
0192                             transformed);
0193                     currentmother = out_state.Top();
0194                 } while (currentmother
0195                          && (currentmother->IsAssembly()
0196                              || !currentmother->UnplacedContains(transformed)));
0197             }
0198             else
0199             {
0200                 out_state.Push(hitcandidate);
0201             }
0202         }
0203 
0204         return step;
0205     }
0206 
0207     // Relocate a state that was returned from ComputeStepAndNextVolume: It
0208     // recursively locates the pushed point in the containing volume.
0209     //
0210     // If the step left one or more volumes, the outermost exited volume (a
0211     // daughter of the new top volume) is excluded from the first search level
0212     // so that the track cannot reenter it at the boundary. It is found by
0213     // comparing with the pre-step state rather than from the navigation
0214     // state's "last exited" metadata, which the path state does not track
0215     // (it reports the top volume after a pop) and which would otherwise have
0216     // to be cleared after every crossing.
0217     CELER_FUNCTION static void RelocateToNextVolume(VgReal3 const& globalpoint,
0218                                                     VgReal3 const& globaldir,
0219                                                     NavState const& in_state,
0220                                                     NavState& state)
0221     {
0222         VgPlacedVol const* exited = nullptr;
0223         if (state.GetLevel() < in_state.GetLevel())
0224         {
0225             exited = in_state.At(state.GetLevel() + 1);
0226         }
0227 
0228         // Push the point inside the next volume.
0229         VgReal3 pushed = globalpoint + kBoundaryPush * globaldir;
0230 
0231         // Calculate local point from global point.
0232         vecgeom::Transformation3D m;
0233         state.TopMatrix(m);
0234         VgReal3 localpoint = m.Transform(pushed);
0235 
0236         VgPlacedVol const* pvol = state.Top();
0237 
0238         state.Pop();
0239         LocatePointIn(pvol, localpoint, state, false, exited);
0240 
0241         if (state.Top() != nullptr)
0242         {
0243             while (state.Top()->IsAssembly())
0244             {
0245                 state.Pop();
0246             }
0247             CELER_ASSERT(
0248                 !state.Top()->GetLogicalVolume()->GetUnplacedVolume()->IsAssembly());
0249         }
0250     }
0251 
0252   private:
0253     // USED ONLY BY ComputeStepAndNextVolume
0254     // Computes a step in the current volume from the localpoint into localdir,
0255     // taking step_limit into account. If a volume is hit, the function calls
0256     // out_state.SetBoundaryState(true) and hitcandidate is set to the hit
0257     // daughter volume, or kept unchanged if the current volume is left.
0258     CELER_FUNCTION static double ComputeStepAndHit(
0259         VgReal3 const& localpoint,
0260         VgReal3 const& localdir,
0261         vg_real_type step_limit,
0262         NavState const& in_state,
0263         NavState& out_state,
0264         VgPlacedVol const*& hitcandidate)
0265     {
0266         if (step_limit <= 0)
0267         {
0268             // We don't need to ask any solid, step not limited by geometry.
0269             in_state.CopyTo(&out_state);
0270             out_state.SetBoundaryState(false);
0271             return 0;
0272         }
0273 
0274         vg_real_type step = step_limit;
0275         VgPlacedVol const* pvol = in_state.Top();
0276 
0277         // need to calc DistanceToOut first
0278         step = pvol->DistanceToOut(localpoint, localdir, step_limit);
0279 
0280         if (step < 0)
0281             step = 0;
0282 
0283         if (pvol->GetDaughters().size() > 0)
0284         {
0285             auto bvh
0286                 = vecgeom::BVHManager::GetBVH(pvol->GetLogicalVolume()->id());
0287             // TODO: this may recurse into daughters which may be unnecessary
0288             bvh->CheckDaughterIntersections(
0289                 localpoint, localdir, step, pvol, hitcandidate);
0290         }
0291 
0292         // now we have the candidates and we prepare the out_state
0293         in_state.CopyTo(&out_state);
0294         if (step == vecgeom::kInfLength && step_limit > 0)
0295         {
0296             // The point is leaving the current volume. Like any other exit,
0297             // the caller pops up to the containing volume: popping here as
0298             // well would skip a level and transform the point into the wrong
0299             // frame.
0300             out_state.SetBoundaryState(true);
0301             return vecgeom::kTolerance;
0302         }
0303 
0304         // Is geometry further away than physics step?
0305         if (step > step_limit)
0306         {
0307             // Then this is a phyics step and we don't need to do anything.
0308             out_state.SetBoundaryState(false);
0309             return step_limit;
0310         }
0311 
0312         // Otherwise it is a geometry step and we push the point to the
0313         // boundary.
0314         out_state.SetBoundaryState(true);
0315 
0316         if (step < 0)
0317         {
0318             step = 0;
0319         }
0320 
0321         return step;
0322     }
0323 };
0324 
0325 //---------------------------------------------------------------------------//
0326 }  // namespace detail
0327 }  // namespace celeritas