File indexing completed on 2026-11-19 10:43:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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
0043
0044
0045
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
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
0089 VgReal3 daughterlocalpoint;
0090 vol = nullptr;
0091 if (!bvh->LevelLocate(
0092 exclude, currentpoint, vol, daughterlocalpoint))
0093 {
0094
0095 break;
0096 }
0097
0098 CELER_ASSERT(vol);
0099 path.Push(vol);
0100
0101
0102 currentpoint = daughterlocalpoint;
0103
0104
0105
0106 exclude = nullptr;
0107 }
0108 }
0109
0110
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
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
0133
0134
0135
0136
0137
0138
0139
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
0148 vg_real_type push = in_state.IsOnBoundary() ? kBoundaryPush : 0;
0149
0150 if (step_limit < push)
0151 {
0152
0153 in_state.CopyTo(&out_state);
0154 out_state.SetBoundaryState(false);
0155 return step_limit;
0156 }
0157 step_limit -= push;
0158
0159
0160 VgReal3 localpoint;
0161 VgReal3 localdir;
0162
0163
0164 vecgeom::Transformation3D m;
0165 in_state.TopMatrix(m);
0166 localpoint = m.Transform(globalpoint);
0167 localdir = m.TransformDirection(globaldir);
0168
0169
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
0182 VgPlacedVol const* currentmother = out_state.Top();
0183 VgReal3 transformed = localpoint;
0184
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
0208
0209
0210
0211
0212
0213
0214
0215
0216
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
0229 VgReal3 pushed = globalpoint + kBoundaryPush * globaldir;
0230
0231
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
0254
0255
0256
0257
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
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
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
0288 bvh->CheckDaughterIntersections(
0289 localpoint, localdir, step, pvol, hitcandidate);
0290 }
0291
0292
0293 in_state.CopyTo(&out_state);
0294 if (step == vecgeom::kInfLength && step_limit > 0)
0295 {
0296
0297
0298
0299
0300 out_state.SetBoundaryState(true);
0301 return vecgeom::kTolerance;
0302 }
0303
0304
0305 if (step > step_limit)
0306 {
0307
0308 out_state.SetBoundaryState(false);
0309 return step_limit;
0310 }
0311
0312
0313
0314 out_state.SetBoundaryState(true);
0315
0316 if (step < 0)
0317 {
0318 step = 0;
0319 }
0320
0321 return step;
0322 }
0323 };
0324
0325
0326 }
0327 }