File indexing completed on 2026-11-30 10:40:43
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BVH_NAVIGATOR_H
0010 #define BVH_NAVIGATOR_H
0011
0012 #include "VecGeom/base/Global.h"
0013 #include "VecGeom/base/Math.h"
0014 #include "VecGeom/base/Vector3D.h"
0015 #include "VecGeom/navigation/NavigationState.h"
0016 #include "VecGeom/volumes/LogicalVolume.h"
0017 #include "VecGeom/management/BVHManager.h"
0018 #include "VecGeom/management/GeoManager.h"
0019
0020 #ifdef VECGEOM_ENABLE_CUDA
0021 #include "VecGeom/backend/cuda/Interface.h"
0022 #endif
0023
0024 #include <type_traits>
0025
0026 namespace vgbrep {
0027 namespace protonav {
0028 template <typename Real_t>
0029 class BVHSurfNavigator;
0030 }
0031 }
0032
0033 namespace vecgeom {
0034
0035 inline namespace VECGEOM_IMPL_NAMESPACE {
0036 class BVHSafetyEstimator;
0037 }
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 class BVHNavigator {
0049
0050 public:
0051
0052
0053
0054
0055
0056 static constexpr Precision kBoundaryPush = 10 * vecgeom::kTolerance;
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 VECCORE_ATT_HOST_DEVICE
0068 static VECGEOM_FORCE_INLINE Daughter LocatePointIn(vecgeom::VPlacedVolume const *vol,
0069 Vector3D<Precision> const &point, vecgeom::NavigationState &path,
0070 bool top, vecgeom::VPlacedVolume const *exclude = nullptr)
0071 {
0072 return LocatePointInImpl(vol, point, path, top, exclude);
0073 }
0074
0075
0076
0077
0078
0079
0080
0081 VECCORE_ATT_HOST_DEVICE
0082 static VECGEOM_FORCE_INLINE Daughter RelocatePoint(Vector3D<Precision> const &localpoint,
0083 vecgeom::NavigationState &path)
0084 {
0085 return RelocatePointImpl(localpoint, path);
0086 }
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096 VECCORE_ATT_HOST_DEVICE
0097 static VECGEOM_FORCE_INLINE Daughter LocatePointInNavState(Vector3D<Precision> const &localpoint,
0098 vecgeom::NavigationState &path, bool top,
0099 vecgeom::VPlacedVolume const *exclude = nullptr)
0100 {
0101 return LocatePointInNavStateImpl(localpoint, path, top, exclude);
0102 }
0103
0104
0105
0106
0107
0108
0109
0110
0111 VECCORE_ATT_HOST_DEVICE
0112 static VECGEOM_FORCE_INLINE Precision ComputeSafety(Vector3D<Precision> const &globalpoint,
0113 vecgeom::NavigationState const &state,
0114 Precision limit = InfinityLength<Precision>())
0115 {
0116 return ComputeSafetyImpl(globalpoint, state, limit);
0117 }
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 VECCORE_ATT_HOST_DEVICE
0129 static VECGEOM_FORCE_INLINE Precision ComputeStepAndPropagatedState(Vector3D<Precision> const &globalpoint,
0130 Vector3D<Precision> const &globaldir,
0131 Precision step_limit,
0132 vecgeom::NavigationState const &in_state,
0133 vecgeom::NavigationState &out_state)
0134 {
0135 return ComputeStepAndPropagatedStateImpl(globalpoint, globaldir, step_limit, in_state, out_state);
0136 }
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 VECCORE_ATT_HOST_DEVICE
0153 static VECGEOM_FORCE_INLINE Precision ComputeStepAndNextVolume(Vector3D<Precision> const &globalpoint,
0154 Vector3D<Precision> const &globaldir,
0155 Precision step_limit,
0156 vecgeom::NavigationState const &in_state,
0157 vecgeom::NavigationState &out_state)
0158 {
0159 return ComputeStepAndNextVolumeImpl(globalpoint, globaldir, step_limit, in_state, out_state);
0160 }
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179 VECCORE_ATT_HOST_DEVICE
0180 static bool ComputeNextNormal(Vector3D<Precision> const &globalpoint, vecgeom::NavigationState const ¤t,
0181 vecgeom::NavigationState const &next, Vector3D<Precision> &normal)
0182 {
0183 VECGEOM_ASSERT(!current.IsOutside() && next.IsOnBoundary());
0184 VECGEOM_ASSERT(!current.HasSamePathAsOther(next));
0185 Transformation3D transform;
0186 current.TopMatrix(transform);
0187 auto const localpoint = transform.Transform(globalpoint);
0188 bool valid;
0189
0190
0191 if (!next.IsOutside() && next.GetLevel() > current.GetLevel()) {
0192 valid = next.Top()->Normal(localpoint, normal);
0193 if (valid) normal = -normal;
0194 } else {
0195 valid = current.Top()->GetUnplacedVolume()->Normal(localpoint, normal);
0196 }
0197 if (valid) normal = transform.InverseTransformDirection(normal);
0198 return valid;
0199 }
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209 VECCORE_ATT_HOST_DEVICE
0210 static VECGEOM_FORCE_INLINE Precision ComputeStepToApproachNextVolume(Vector3D<Precision> const &globalpoint,
0211 Vector3D<Precision> const &globaldir,
0212 Precision step_limit,
0213 vecgeom::NavigationState const &in_state)
0214 {
0215 return ComputeStepToApproachNextVolumeImpl(globalpoint, globaldir, step_limit, in_state);
0216 }
0217
0218
0219
0220
0221
0222
0223
0224 VECCORE_ATT_HOST_DEVICE
0225 static VECGEOM_FORCE_INLINE void RelocateToNextVolume(Vector3D<Precision> const &globalpoint,
0226 Vector3D<Precision> const &globaldir,
0227 vecgeom::NavigationState &state)
0228 {
0229 RelocateToNextVolumeImpl(globalpoint, globaldir, state);
0230 }
0231
0232
0233
0234 private:
0235 template <typename Real_t>
0236 friend class ::vgbrep::protonav::BVHSurfNavigator;
0237 friend class BVHSafetyEstimator;
0238
0239
0240
0241 #ifdef VECGEOM_BVH_SINGLE
0242 using Real_t = float;
0243 #else
0244 using Real_t = double;
0245 #endif
0246
0247 using Self_t = BVHNavigator;
0248
0249
0250 static constexpr int kDirectSafetyBoxThreshold = 8;
0251
0252
0253 struct IntersectStatistics {
0254 long primitiveBoxTests{0};
0255 long visitedLeaves{0};
0256 long cutNodes{0};
0257 long scheduledNodes{0};
0258 long intersectedBoxes{0};
0259 long deferredNodes{0};
0260 long prunableDeferredNodes{0};
0261 long maximumStackDepth{0};
0262 };
0263
0264
0265 struct SafetyStatistics {
0266 long visitedNodes{0};
0267 long visitedLeaves{0};
0268 long primitiveBoxTests{0};
0269 long relevantBoxes{0};
0270 long cutNodes{0};
0271 };
0272
0273 VECCORE_ATT_HOST_DEVICE
0274 static auto GetBVH(int logicalVolumeId) { return vecgeom::BVHManager::GetBVH(logicalVolumeId); }
0275
0276
0277
0278 template <bool collect_statistics, typename Navigator, bool conditional_exclusion = false, typename BVHReal_t,
0279 typename Real_i>
0280 VECCORE_ATT_HOST_DEVICE static void CheckDaughterIntersectionsImpl(
0281 const vecgeom::IndexedBVH<BVHReal_t> &bvh, const Vector3D<Real_i> &localpoint, const Vector3D<Real_i> &localdir,
0282 Real_i &step, long const last_exited_id, long &hitcandidate_index, IntersectStatistics *statistics = nullptr,
0283 long *candidate_calls = nullptr)
0284 {
0285 struct StackItem {
0286 unsigned int node;
0287 BVHReal_t approach;
0288 };
0289 using StackEntry = std::conditional_t<collect_statistics, StackItem, unsigned int>;
0290 StackEntry stack[vecgeom::IndexedBVH<BVHReal_t>::kTraversalStackSize];
0291 int stackSize = 0;
0292
0293 if constexpr (collect_statistics) {
0294 VECGEOM_ASSERT(statistics != nullptr);
0295 *statistics = IntersectStatistics{};
0296 }
0297
0298 const bool mapped = bvh.HasDaughterMap();
0299 const Vector3D<BVHReal_t> point(static_cast<BVHReal_t>(localpoint[0]), static_cast<BVHReal_t>(localpoint[1]),
0300 static_cast<BVHReal_t>(localpoint[2]));
0301 const Vector3D<BVHReal_t> invDirection(static_cast<BVHReal_t>(1.) / vecgeom::NonZero(localdir[0]),
0302 static_cast<BVHReal_t>(1.) / vecgeom::NonZero(localdir[1]),
0303 static_cast<BVHReal_t>(1.) / vecgeom::NonZero(localdir[2]));
0304
0305
0306 const unsigned int octant = (invDirection[0] < BVHReal_t(0.) ? 1u : 0u) |
0307 (invDirection[1] < BVHReal_t(0.) ? 2u : 0u) |
0308 (invDirection[2] < BVHReal_t(0.) ? 4u : 0u);
0309 BVHReal_t bvhStep = static_cast<BVHReal_t>(step);
0310 BVHReal_t rootApproach;
0311 if (!bvh.IntersectNode(0, point, invDirection, octant, bvhStep, rootApproach)) {
0312 if constexpr (collect_statistics) ++statistics->cutNodes;
0313 return;
0314 }
0315
0316 auto const *nodes = bvh.GetNodes();
0317 auto const *primId = bvh.GetPrimId();
0318 unsigned int top = 0;
0319 for (;;) {
0320 while (!nodes[top].IsLeaf()) {
0321 const unsigned int childL = nodes[top].first;
0322 const unsigned int childR = childL + 1;
0323 BVHReal_t approachL, approachR;
0324 const bool hitL = bvh.IntersectNode(childL, point, invDirection, octant, bvhStep, approachL);
0325 const bool hitR = bvh.IntersectNode(childR, point, invDirection, octant, bvhStep, approachR);
0326 if constexpr (collect_statistics)
0327 statistics->scheduledNodes += static_cast<long>(hitL) + static_cast<long>(hitR);
0328
0329 if (hitL) {
0330
0331 unsigned int near = childL;
0332 if (hitR) {
0333 unsigned int far = childR;
0334 BVHReal_t farEntry = approachR;
0335 if (approachR < approachL) {
0336 near = childR;
0337 far = childL;
0338 farEntry = approachL;
0339 }
0340 if constexpr (collect_statistics) {
0341 stack[stackSize++] = StackItem{far, farEntry};
0342 ++statistics->deferredNodes;
0343 statistics->maximumStackDepth = vecgeom::Max(statistics->maximumStackDepth, long(stackSize));
0344 } else {
0345 stack[stackSize++] = far;
0346 }
0347 }
0348 top = near;
0349 } else if (hitR) {
0350 top = childR;
0351 } else {
0352 goto pop;
0353 }
0354 }
0355
0356 if constexpr (collect_statistics) ++statistics->visitedLeaves;
0357 for (int i = 0; i < nodes[top].count; ++i) {
0358 if constexpr (collect_statistics) ++statistics->primitiveBoxTests;
0359 const int boxId = primId[nodes[top].first + i];
0360 BVHReal_t boxApproach;
0361 if (!bvh.IntersectBox(boxId, point, invDirection, bvhStep, boxApproach)) continue;
0362 if constexpr (collect_statistics) ++statistics->intersectedBoxes;
0363
0364 const int prim = bvh.GetDaughterId(boxId);
0365 const bool excluded = last_exited_id >= 0 && Navigator::SkipItem(bvh.GetRootId(), prim, last_exited_id);
0366 if constexpr (!conditional_exclusion) {
0367 if (excluded) continue;
0368 }
0369 if constexpr (collect_statistics) {
0370 if (candidate_calls) ++*candidate_calls;
0371 }
0372
0373 const auto approach = static_cast<Real_i>(boxApproach);
0374 Real_i dist;
0375 if (!mapped) {
0376 dist =
0377 Navigator::CandidateDistanceToIn(bvh.GetRootId(), prim, localpoint + approach * localdir, localdir, step);
0378 if (dist > Real_i(0.)) dist += approach;
0379 } else if (conditional_exclusion) {
0380
0381
0382 dist = Navigator::CandidateDistanceToIn(bvh.GetRootId(), prim, localpoint, localdir, step);
0383 } else {
0384 dist = Navigator::CandidateDistanceToIn(bvh.GetRootId(), prim, localpoint + approach * localdir, localdir,
0385 vecgeom::Max(step - approach, Real_i(0.)));
0386 if (dist < Real_i(0.)) dist = Real_i(0.);
0387 dist += approach;
0388 }
0389 if (dist < step) {
0390 if constexpr (conditional_exclusion) {
0391
0392
0393 if (dist <= Real_i(0.) &&
0394 (excluded || Navigator::CandidateLeaves(bvh.GetRootId(), prim, localpoint, localdir)))
0395 continue;
0396 }
0397 dist = vecgeom::Max(dist, Real_i(0.));
0398 step = dist;
0399 bvhStep = static_cast<BVHReal_t>(dist);
0400 hitcandidate_index = prim;
0401 }
0402 }
0403
0404 pop:
0405 if (stackSize == 0) return;
0406 if constexpr (collect_statistics) {
0407 const auto deferred = stack[--stackSize];
0408 if (deferred.approach > bvhStep) ++statistics->prunableDeferredNodes;
0409 top = deferred.node;
0410 } else {
0411 top = stack[--stackSize];
0412 }
0413 }
0414 }
0415
0416 template <bool collect_statistics, typename Navigator, typename BVHReal_t>
0417 VECCORE_ATT_HOST_DEVICE static Precision ComputeBVHSafetyImpl(const vecgeom::IndexedBVH<BVHReal_t> &bvh,
0418 Vector3D<Precision> const &localpoint, Precision safety,
0419 Precision limit, Precision min_valid_safety,
0420 SafetyStatistics *statistics = nullptr,
0421 long *limit_shortcuts = nullptr,
0422 long *candidate_calls = nullptr)
0423 {
0424 const Vector3D<BVHReal_t> point(static_cast<BVHReal_t>(localpoint[0]), static_cast<BVHReal_t>(localpoint[1]),
0425 static_cast<BVHReal_t>(localpoint[2]));
0426 if constexpr (collect_statistics) {
0427 VECGEOM_ASSERT(statistics != nullptr);
0428 *statistics = SafetyStatistics{};
0429 }
0430
0431 #ifndef VECCORE_CUDA_DEVICE_COMPILATION
0432 if constexpr (!collect_statistics) {
0433
0434
0435 if (bvh.GetRootNChild() <= kDirectSafetyBoxThreshold) {
0436 if (static_cast<Precision>(bvh.NodeSafetySqr(0, point)) > safety * safety) return safety;
0437 for (int index = 0; index < bvh.GetRootNChild(); ++index) {
0438 const int boxId = bvh.GetPrimId()[index];
0439 const Precision boxSafety = bvh.BoxSafety(boxId, localpoint);
0440 if (boxSafety >= safety) continue;
0441 if (boxSafety > limit) {
0442 safety = vecgeom::Min(safety, boxSafety);
0443 continue;
0444 }
0445 const int prim = bvh.GetDaughterId(boxId);
0446 const Precision dist = Navigator::CandidateSafetyToIn(bvh.GetRootId(), prim, localpoint);
0447 if (dist > min_valid_safety) safety = vecgeom::Min(safety, dist);
0448 }
0449 return safety;
0450 }
0451 }
0452 #endif
0453
0454 unsigned int stack[vecgeom::IndexedBVH<BVHReal_t>::kTraversalStackSize];
0455 int stackSize = 1;
0456 stack[0] = 0;
0457 auto const *nodes = bvh.GetNodes();
0458 auto const *primId = bvh.GetPrimId();
0459
0460 while (stackSize > 0) {
0461 const unsigned int id = stack[--stackSize];
0462 if constexpr (collect_statistics) ++statistics->visitedNodes;
0463 if (static_cast<Precision>(bvh.NodeSafetySqr(id, point)) > safety * safety) {
0464 if constexpr (collect_statistics) ++statistics->cutNodes;
0465 continue;
0466 }
0467
0468 if (nodes[id].IsLeaf()) {
0469 if constexpr (collect_statistics) ++statistics->visitedLeaves;
0470 for (int i = 0; i < nodes[id].count; ++i) {
0471 if constexpr (collect_statistics) ++statistics->primitiveBoxTests;
0472 const int boxId = primId[nodes[id].first + i];
0473 const Precision boxSafety = bvh.BoxSafety(boxId, localpoint);
0474 if (boxSafety >= safety) continue;
0475 if constexpr (collect_statistics) ++statistics->relevantBoxes;
0476 if (boxSafety > limit) {
0477 if constexpr (collect_statistics) {
0478 if (limit_shortcuts) ++*limit_shortcuts;
0479 }
0480 safety = vecgeom::Min(safety, boxSafety);
0481 continue;
0482 }
0483 if constexpr (collect_statistics) {
0484 if (candidate_calls) ++*candidate_calls;
0485 }
0486 const int prim = bvh.GetDaughterId(boxId);
0487 const Precision dist = Navigator::CandidateSafetyToIn(bvh.GetRootId(), prim, localpoint);
0488 if (dist > min_valid_safety) safety = vecgeom::Min(safety, dist);
0489 }
0490 } else {
0491 const unsigned int childL = nodes[id].first;
0492 const unsigned int childR = childL + 1;
0493 const BVHReal_t safetyL = bvh.NodeSafetySqr(childL, point);
0494 const BVHReal_t safetyR = bvh.NodeSafetySqr(childR, point);
0495 const Precision limitSqr = safety * safety;
0496 const bool traverseL = static_cast<Precision>(safetyL) < limitSqr;
0497 const bool traverseR = static_cast<Precision>(safetyR) < limitSqr;
0498 if (safetyR < safetyL) {
0499 if (traverseL) stack[stackSize++] = childL;
0500 if (traverseR) stack[stackSize++] = childR;
0501 } else {
0502 if (traverseR) stack[stackSize++] = childR;
0503 if (traverseL) stack[stackSize++] = childL;
0504 }
0505 }
0506 }
0507 return safety;
0508 }
0509
0510 protected:
0511
0512
0513
0514
0515
0516
0517
0518 VECCORE_ATT_HOST_DEVICE
0519 static VECGEOM_FORCE_INLINE Daughter GetPlacedVolume(int aLVIndex, int index)
0520 {
0521 #ifdef VECCORE_CUDA_DEVICE_COMPILATION
0522 VECGEOM_VALIDATE(vecgeom::globaldevicegeomdata::gDeviceLogicalVolumes != nullptr,
0523 << "Logical volumes not copied to device");
0524 return vecgeom::globaldevicegeomdata::gDeviceLogicalVolumes[aLVIndex].GetDaughters()[index];
0525 #else
0526 #ifndef VECCORE_CUDA
0527 return vecgeom::GeoManager::Instance().GetLogicalVolume(aLVIndex)->GetDaughters()[index];
0528 #else
0529
0530 VECGEOM_VALIDATE(false, << "reached unimplement code");
0531 (void)index;
0532 (void)aLVIndex;
0533 return nullptr;
0534 #endif
0535 #endif
0536 }
0537
0538
0539
0540
0541
0542 VECCORE_ATT_HOST_DEVICE
0543 static VECGEOM_FORCE_INLINE vecgeom::VPlacedVolume *GetPlacedVolume(int global_index)
0544 {
0545 #ifdef VECCORE_CUDA_DEVICE_COMPILATION
0546 VECGEOM_VALIDATE(vecgeom::globaldevicegeomdata::gCompactPlacedVolBuffer != nullptr,
0547 << "Placed volumes not copied to device");
0548 return &vecgeom::globaldevicegeomdata::gCompactPlacedVolBuffer[global_index];
0549 #else
0550 #ifndef VECCORE_CUDA
0551 return vecgeom::GeoManager::Instance().GetPlacedVolume(global_index);
0552 #else
0553
0554 VECGEOM_VALIDATE(false, << "reached unimplement code");
0555 (void)global_index;
0556 return nullptr;
0557 #endif
0558 #endif
0559 }
0560
0561
0562
0563
0564
0565
0566
0567 VECCORE_ATT_HOST_DEVICE
0568 static Precision CandidateSafetyToIn(int aLVIndex, int index, Vector3D<Precision> localpoint)
0569 {
0570 return GetPlacedVolume(aLVIndex, index)->SafetyToIn(localpoint);
0571 };
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582 VECCORE_ATT_HOST_DEVICE
0583 static Precision CandidateDistanceToIn(int aLVIndex, int index, Vector3D<Precision> localpoint,
0584 Vector3D<Precision> localdir, Precision step)
0585 {
0586 Daughter vol = GetPlacedVolume(aLVIndex, index);
0587 return vol->DistanceToIn(localpoint, localdir, step);
0588 };
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598 VECCORE_ATT_HOST_DEVICE
0599 static bool CandidateLeaves(int aLVIndex, int index, Vector3D<Precision> localpoint, Vector3D<Precision> localdir)
0600 {
0601 Vector3D<Precision> normal;
0602 GetPlacedVolume(aLVIndex, index)->Normal(localpoint, normal);
0603 return normal.Dot(localdir) > 0.;
0604 }
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614 VECCORE_ATT_HOST_DEVICE
0615 static vecgeom::Inside_t CandidateInside(int aLVIndex, int index, Vector3D<Precision> const &localpoint,
0616 Vector3D<Precision> &daughterlocalpoint)
0617 {
0618 auto daughter = GetPlacedVolume(aLVIndex, index);
0619 daughterlocalpoint = daughter->GetTransformation()->template Transform<Precision>(localpoint);
0620 return daughter->GetUnplacedVolume()->Inside(daughterlocalpoint);
0621 };
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631 VECCORE_ATT_HOST_DEVICE
0632 static bool CandidateContains(int aLVIndex, int index, Vector3D<Precision> const &localpoint,
0633 Vector3D<Precision> &daughterlocalpoint)
0634 {
0635 auto inside = CandidateInside(aLVIndex, index, localpoint, daughterlocalpoint);
0636 return inside != EnumInside::kOutside;
0637 };
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647 VECCORE_ATT_HOST_DEVICE
0648 static Precision CandidateApproachSolid(int aLVIndex, int index, Vector3D<Precision> localpoint,
0649 Vector3D<Precision> localdir)
0650 {
0651 auto vol = GetPlacedVolume(aLVIndex, index);
0652 vecgeom::Transformation3D const *tr = vol->GetTransformation();
0653 Vector3D<Precision> pv_localpoint = tr->Transform(localpoint);
0654 Vector3D<Precision> pv_localdir = tr->TransformDirection(localdir);
0655 Vector3D<Precision> pv_invlocaldir(1.0 / vecgeom::NonZero(pv_localdir[0]), 1.0 / vecgeom::NonZero(pv_localdir[1]),
0656 1.0 / vecgeom::NonZero(pv_localdir[2]));
0657 return vol->GetUnplacedVolume()->ApproachSolid(pv_localpoint, pv_invlocaldir);
0658 };
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668 VECCORE_ATT_HOST_DEVICE
0669 static VECGEOM_FORCE_INLINE bool SkipItem(int aLVIndex, int index, long const global_id)
0670 {
0671 return (global_id == GetPlacedVolume(aLVIndex, index)->id());
0672 }
0673
0674 public:
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698 template <typename Navigator, typename BVHReal_t, typename Real_i>
0699 VECCORE_ATT_HOST_DEVICE static void CheckDaughterIntersections(const vecgeom::IndexedBVH<BVHReal_t> &bvh,
0700 const Vector3D<Real_i> &localpoint,
0701 const Vector3D<Real_i> &localdir, Real_i &step,
0702 long const last_exited_id, long &hitcandidate_index)
0703 {
0704 CheckDaughterIntersectionsImpl<false, Navigator>(bvh, localpoint, localdir, step, last_exited_id,
0705 hitcandidate_index);
0706 }
0707
0708
0709
0710
0711
0712
0713 template <typename Navigator, typename BVHReal_t, typename Real_i>
0714 VECCORE_ATT_HOST_DEVICE static void CheckDaughterIntersectionsBlocked(const vecgeom::IndexedBVH<BVHReal_t> &bvh,
0715 const Vector3D<Real_i> &localpoint,
0716 const Vector3D<Real_i> &localdir, Real_i &step,
0717 long const blocked_id, long &hitcandidate_index)
0718 {
0719 CheckDaughterIntersectionsImpl<false, Navigator, true>(bvh, localpoint, localdir, step, blocked_id,
0720 hitcandidate_index);
0721 }
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748 template <typename Navigator, typename BVHReal_t, typename Real_i>
0749 VECCORE_ATT_HOST_DEVICE static void CheckDaughterIntersectionsBenchmark(
0750 const vecgeom::IndexedBVH<BVHReal_t> &bvh, const Vector3D<Real_i> &localpoint, const Vector3D<Real_i> &localdir,
0751 Real_i &step, long const last_exited_id, long &hitcandidate_index, long &total_visited_children,
0752 long &total_visited_leaves, long &total_cut_nodes, long &total_scheduled_nodes,
0753 long *total_intersected_children = nullptr, long *total_candidate_calls = nullptr,
0754 long *total_deferred_nodes = nullptr, long *total_prunable_deferred_nodes = nullptr,
0755 long *maximum_stack_depth = nullptr)
0756 {
0757 IntersectStatistics statistics;
0758 if (total_candidate_calls) *total_candidate_calls = 0;
0759 CheckDaughterIntersectionsImpl<true, Navigator>(bvh, localpoint, localdir, step, last_exited_id, hitcandidate_index,
0760 &statistics, total_candidate_calls);
0761
0762 total_visited_children = statistics.primitiveBoxTests;
0763 total_visited_leaves = statistics.visitedLeaves;
0764 total_cut_nodes = statistics.cutNodes;
0765 total_scheduled_nodes = statistics.scheduledNodes;
0766 if (total_intersected_children) *total_intersected_children = statistics.intersectedBoxes;
0767 if (total_deferred_nodes) *total_deferred_nodes = statistics.deferredNodes;
0768 if (total_prunable_deferred_nodes) *total_prunable_deferred_nodes = statistics.prunableDeferredNodes;
0769 if (maximum_stack_depth) *maximum_stack_depth = statistics.maximumStackDepth;
0770 }
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786 template <typename Navigator, typename BVHReal_t>
0787 VECCORE_ATT_HOST_DEVICE static void ApproachNextDaughter(const vecgeom::IndexedBVH<BVHReal_t> &bvh,
0788 Vector3D<Precision> const &localpoint,
0789 Vector3D<Precision> const &localdir, Precision &step,
0790 long const last_exited_id)
0791 {
0792 unsigned int stack[vecgeom::IndexedBVH<BVHReal_t>::kTraversalStackSize];
0793 int stackSize = 0;
0794 const Vector3D<BVHReal_t> point(static_cast<BVHReal_t>(localpoint[0]), static_cast<BVHReal_t>(localpoint[1]),
0795 static_cast<BVHReal_t>(localpoint[2]));
0796 const Vector3D<BVHReal_t> invDirection(static_cast<BVHReal_t>(1.) / vecgeom::NonZero(localdir[0]),
0797 static_cast<BVHReal_t>(1.) / vecgeom::NonZero(localdir[1]),
0798 static_cast<BVHReal_t>(1.) / vecgeom::NonZero(localdir[2]));
0799 const unsigned int octant = (invDirection[0] < BVHReal_t(0.) ? 1u : 0u) |
0800 (invDirection[1] < BVHReal_t(0.) ? 2u : 0u) |
0801 (invDirection[2] < BVHReal_t(0.) ? 4u : 0u);
0802 BVHReal_t bvhStep = static_cast<BVHReal_t>(step);
0803 BVHReal_t approach;
0804 if (!bvh.IntersectNode(0, point, invDirection, octant, bvhStep, approach)) return;
0805
0806 auto const *nodes = bvh.GetNodes();
0807 auto const *primId = bvh.GetPrimId();
0808 unsigned int top = 0;
0809 for (;;) {
0810 while (!nodes[top].IsLeaf()) {
0811 const unsigned int childL = nodes[top].first;
0812 const unsigned int childR = childL + 1;
0813 BVHReal_t approachL, approachR;
0814 const bool hitL = bvh.IntersectNode(childL, point, invDirection, octant, bvhStep, approachL);
0815 const bool hitR = bvh.IntersectNode(childR, point, invDirection, octant, bvhStep, approachR);
0816 if (hitL) {
0817 unsigned int near = childL;
0818 if (hitR) {
0819 unsigned int far = childR;
0820 if (approachR < approachL) {
0821 near = childR;
0822 far = childL;
0823 }
0824 stack[stackSize++] = far;
0825 }
0826 top = near;
0827 } else if (hitR) {
0828 top = childR;
0829 } else {
0830 goto pop;
0831 }
0832 }
0833
0834 for (int i = 0; i < nodes[top].count; ++i) {
0835 const int boxId = primId[nodes[top].first + i];
0836 if (!bvh.IntersectBox(boxId, point, invDirection, bvhStep, approach)) continue;
0837 const int prim = bvh.GetDaughterId(boxId);
0838 const auto dist = Navigator::CandidateApproachSolid(bvh.GetRootId(), prim, localpoint, localdir);
0839 if (dist < step && !(dist <= Precision(0.) && Navigator::SkipItem(bvh.GetRootId(), prim, last_exited_id))) {
0840 step = dist;
0841 bvhStep = static_cast<BVHReal_t>(dist);
0842 }
0843 }
0844
0845 pop:
0846 if (stackSize == 0) return;
0847 top = stack[--stackSize];
0848 }
0849 }
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867 template <typename Navigator, typename BVHReal_t>
0868 VECCORE_ATT_HOST_DEVICE static Precision ComputeBVHSafety(const vecgeom::IndexedBVH<BVHReal_t> &bvh,
0869 Vector3D<Precision> const &localpoint, Precision safety,
0870 Precision limit = InfinityLength<Precision>(),
0871 Precision min_valid_safety = -InfinityLength<Precision>())
0872 {
0873 return ComputeBVHSafetyImpl<false, Navigator>(bvh, localpoint, safety, limit, min_valid_safety);
0874 }
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898 template <typename Navigator, typename BVHReal_t>
0899 VECCORE_ATT_HOST_DEVICE static Precision ComputeSafetyBenchmark(
0900 const vecgeom::IndexedBVH<BVHReal_t> &bvh, Vector3D<Precision> const &localpoint, Precision safety,
0901 Precision limit, Precision min_valid_safety, long &visited_nodes, long &visited_leaves, long &primitive_box_tests,
0902 long &relevant_boxes, long &limit_shortcuts, long &candidate_calls, long &cut_nodes)
0903 {
0904 SafetyStatistics statistics;
0905 limit_shortcuts = 0;
0906 candidate_calls = 0;
0907 safety = ComputeBVHSafetyImpl<true, Navigator>(bvh, localpoint, safety, limit, min_valid_safety, &statistics,
0908 &limit_shortcuts, &candidate_calls);
0909 visited_nodes = statistics.visitedNodes;
0910 visited_leaves = statistics.visitedLeaves;
0911 primitive_box_tests = statistics.primitiveBoxTests;
0912 relevant_boxes = statistics.relevantBoxes;
0913 cut_nodes = statistics.cutNodes;
0914 return safety;
0915 }
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929 template <typename Navigator, typename BVHReal_t>
0930 VECCORE_ATT_HOST_DEVICE static vecgeom::Inside_t LevelInside(const vecgeom::IndexedBVH<BVHReal_t> &bvh,
0931 long const exclude_item_id,
0932 Vector3D<Precision> const &localpoint,
0933 long &container_id,
0934 Vector3D<Precision> &daughterlocalpoint)
0935 {
0936 vecgeom::Inside_t result = kOutside;
0937 const Vector3D<BVHReal_t> point(static_cast<BVHReal_t>(localpoint[0]), static_cast<BVHReal_t>(localpoint[1]),
0938 static_cast<BVHReal_t>(localpoint[2]));
0939 if (!bvh.NodeContains(0, point)) return result;
0940
0941 unsigned int stack[vecgeom::IndexedBVH<BVHReal_t>::kTraversalStackSize];
0942 int stackSize = 0;
0943 auto const *nodes = bvh.GetNodes();
0944 auto const *primId = bvh.GetPrimId();
0945 unsigned int top = 0;
0946 for (;;) {
0947 while (!nodes[top].IsLeaf()) {
0948 const unsigned int childL = nodes[top].first;
0949 const unsigned int childR = childL + 1;
0950 const bool inL = bvh.NodeContains(childL, point);
0951 const bool inR = bvh.NodeContains(childR, point);
0952 if (inL) {
0953 if (inR) stack[stackSize++] = childR;
0954 top = childL;
0955 } else if (inR) {
0956 top = childR;
0957 } else {
0958 goto pop;
0959 }
0960 }
0961
0962 for (int i = 0; i < nodes[top].count; ++i) {
0963 const int boxId = primId[nodes[top].first + i];
0964 if (!bvh.BoxContains(boxId, point)) continue;
0965 const int prim = bvh.GetDaughterId(boxId);
0966 if (Navigator::SkipItem(bvh.GetRootId(), prim, exclude_item_id)) continue;
0967 const auto inside = Navigator::CandidateInside(bvh.GetRootId(), prim, localpoint, daughterlocalpoint);
0968 if (inside == kOutside) continue;
0969 container_id = Navigator::ItemId(bvh.GetRootId(), prim);
0970 return inside;
0971 }
0972
0973 pop:
0974 if (stackSize == 0) return result;
0975 top = stack[--stackSize];
0976 }
0977 }
0978
0979
0980
0981 protected:
0982
0983
0984
0985
0986
0987 VECCORE_ATT_HOST_DEVICE
0988 static long TestBVHCheckDaughterIntersections(const vecgeom::IndexedBVH<Real_t> &bvh, Vector3D<Precision> &localpoint,
0989 Vector3D<Precision> &localdir, Precision &bvhstep)
0990 {
0991 long hitcandidate_index = -1;
0992 long last_exited_id = -1;
0993 CheckDaughterIntersections<Self_t>(bvh, localpoint, localdir, bvhstep, last_exited_id, hitcandidate_index);
0994 return hitcandidate_index;
0995 }
0996
0997 VECCORE_ATT_HOST_DEVICE
0998 static long TestCheckDaughterIntersections(int logicalVolumeId, Vector3D<Precision> const &localpoint,
0999 Vector3D<Precision> const &localdir, Precision &bvhstep)
1000 {
1001 long hitcandidate_index = -1;
1002 auto const *bvh = GetBVH(logicalVolumeId);
1003 CheckDaughterIntersections<Self_t>(*bvh, localpoint, localdir, bvhstep, -1, hitcandidate_index);
1004 return hitcandidate_index;
1005 }
1006
1007
1008
1009
1010
1011
1012
1013
1014 VECCORE_ATT_HOST_DEVICE
1015 static uint ItemId(int aLVIndex, int index) { return GetPlacedVolume(aLVIndex, index)->id(); }
1016
1017 private:
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046 VECCORE_ATT_HOST_DEVICE
1047 static Daughter LocatePointInImpl(vecgeom::VPlacedVolume const *vol, Vector3D<Precision> const &point,
1048 vecgeom::NavigationState &path, bool top,
1049 vecgeom::VPlacedVolume const *exclude = nullptr)
1050 {
1051
1052
1053 if (top) {
1054
1055 VECGEOM_ASSERT(vol != nullptr);
1056 auto inside = vol->Inside(point);
1057 if (inside == kOutside) return nullptr;
1058
1059 if (inside == kSurface) path.SetBoundaryState(true);
1060 }
1061
1062 path.Push(vol);
1063
1064
1065 Vector3D<Precision> currentpoint(vol->GetTransformation()->Transform<Precision>(point));
1066 Vector3D<Precision> daughterlocalpoint;
1067 long exclude_id = -1;
1068 long vol_id = -1;
1069
1070 for (auto v = vol; v->GetDaughters().size() > 0;) {
1071 auto bvh = GetBVH(v->GetLogicalVolume()->id());
1072
1073 exclude_id = -1;
1074 if (exclude != nullptr) {
1075 exclude_id = exclude->id();
1076 }
1077 vol_id = -1;
1078
1079 const auto inside = LevelInside<Self_t>(*bvh, exclude_id, currentpoint, vol_id, daughterlocalpoint);
1080 if (inside == kOutside) break;
1081 if (inside == kSurface) path.SetBoundaryState(true);
1082
1083 currentpoint = daughterlocalpoint;
1084
1085 v = GetPlacedVolume(vol_id);
1086 path.Push(v);
1087
1088
1089
1090 exclude = nullptr;
1091 }
1092
1093 return path.Top();
1094 }
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 VECCORE_ATT_HOST_DEVICE
1105 static Daughter RelocatePointImpl(Vector3D<Precision> const &localpoint, vecgeom::NavigationState &path)
1106 {
1107 vecgeom::VPlacedVolume const *currentmother = path.Top();
1108 Daughter skip = nullptr;
1109 Vector3D<Precision> transformed = localpoint;
1110
1111 do {
1112 skip = currentmother;
1113 path.Pop();
1114 transformed = currentmother->GetTransformation()->InverseTransform(transformed);
1115 currentmother = path.Top();
1116 } while (currentmother && (currentmother->IsAssembly() || !currentmother->UnplacedContains(transformed)));
1117
1118
1119
1120 if (currentmother) {
1121 return LocatePointInNavState(transformed, path, false, skip);
1122 }
1123 return currentmother;
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 VECCORE_ATT_HOST_DEVICE
1140 static Daughter LocatePointInNavStateImpl(Vector3D<Precision> const &localpoint, vecgeom::NavigationState &path,
1141 bool top, vecgeom::VPlacedVolume const *exclude = nullptr)
1142 {
1143
1144 VECGEOM_ASSERT(path.Top() != nullptr);
1145
1146 auto currentLogical = path.Top()->GetLogicalVolume();
1147 VECGEOM_ASSERT(currentLogical != nullptr);
1148
1149
1150 if (top) {
1151 auto inside = currentLogical->GetUnplacedVolume()->Inside(localpoint);
1152 if (inside == kOutside) return nullptr;
1153
1154 if (inside == kSurface) path.SetBoundaryState(true);
1155 }
1156
1157 Vector3D<Precision> currentpoint(localpoint);
1158 Vector3D<Precision> daughterlocalpoint;
1159
1160 long exclude_id = exclude ? static_cast<long>(exclude->id()) : -1;
1161 long pvol_id = -1;
1162
1163 while (currentLogical->GetDaughters().size() > 0) {
1164 auto *bvh = GetBVH(currentLogical->id());
1165
1166 const auto inside = LevelInside<Self_t>(*bvh, exclude_id, currentpoint, pvol_id, daughterlocalpoint);
1167
1168 if (inside == kOutside) break;
1169 if (inside == kSurface) path.SetBoundaryState(true);
1170
1171 auto *placed = GetPlacedVolume(pvol_id);
1172 path.Push(placed);
1173
1174
1175 currentpoint = daughterlocalpoint;
1176 currentLogical = placed->GetLogicalVolume();
1177
1178
1179
1180 exclude_id = -1;
1181 exclude = nullptr;
1182 }
1183
1184 return path.Top();
1185 }
1186
1187
1188
1189
1190
1191
1192
1193 VECCORE_ATT_HOST_DEVICE
1194 static Precision ComputeStepAndHit(Vector3D<Precision> const &localpoint, Vector3D<Precision> const &localdir,
1195 Precision step_limit, vecgeom::NavigationState const &in_state,
1196 vecgeom::NavigationState &out_state, Daughter &hitcandidate)
1197 {
1198 in_state.CopyTo(&out_state);
1199
1200 if (step_limit <= 0) return 0.;
1201
1202 Precision step = step_limit;
1203 Daughter pvol = in_state.Top();
1204
1205 long hitcandidate_index = -1;
1206 long last_exited_id = -1;
1207
1208
1209 step = pvol->DistanceToOut(localpoint, localdir, step_limit);
1210
1211 if (step == vecgeom::kInfLength) step = 0.;
1212
1213 step = Min(step, step_limit);
1214
1215 if (step < kTolerance) {
1216 step = Max(Precision(0.), step);
1217
1218 out_state.SetBoundaryState(true);
1219 return step;
1220 }
1221
1222
1223 if (pvol->GetDaughters().size() > 0) {
1224 auto bvh = GetBVH(pvol->GetLogicalVolume()->id());
1225
1226 hitcandidate_index = -1;
1227
1228
1229 last_exited_id = -1;
1230
1231
1232 CheckDaughterIntersections<Self_t>(*bvh, localpoint, localdir, step, last_exited_id, hitcandidate_index);
1233
1234 if (hitcandidate_index >= 0) {
1235
1236 step = Max(Precision(0.), step);
1237 hitcandidate = pvol->GetLogicalVolume()->GetDaughters()[hitcandidate_index];
1238 out_state.SetBoundaryState(true);
1239 return step;
1240 }
1241 }
1242
1243
1244 if (step >= step_limit) {
1245
1246 out_state.SetBoundaryState(false);
1247 return step_limit;
1248 }
1249
1250
1251 out_state.SetBoundaryState(true);
1252 return step;
1253 }
1254
1255
1256
1257 VECCORE_ATT_HOST_DEVICE
1258 static Precision ApproachNextVolume(Vector3D<Precision> const &localpoint, Vector3D<Precision> const &localdir,
1259 Precision step_limit, vecgeom::NavigationState const &in_state)
1260 {
1261 Precision step = step_limit;
1262 Daughter pvol = in_state.Top();
1263
1264
1265 if (pvol->GetDaughters().size() > 0) {
1266 auto bvh = GetBVH(pvol->GetLogicalVolume()->id());
1267
1268
1269
1270 long last_exited_id = -1;
1271
1272
1273 ApproachNextDaughter<Self_t>(*bvh, localpoint, localdir, step, last_exited_id);
1274
1275 step -= 10 * vecgeom::kTolerance;
1276 }
1277
1278 if (step == vecgeom::kInfLength && step_limit > 0) return 0;
1279
1280
1281 if (step > step_limit) {
1282
1283 return step_limit;
1284 }
1285
1286 if (step < 0) {
1287 step = 0;
1288 }
1289
1290 return step;
1291 }
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304 VECCORE_ATT_HOST_DEVICE
1305 static Precision ComputeSafetyImpl(Vector3D<Precision> const &globalpoint, vecgeom::NavigationState const &state,
1306 Precision limit)
1307 {
1308 Daughter pvol = state.Top();
1309 if (pvol == nullptr) return kInfLength;
1310 vecgeom::Transformation3D m;
1311 state.TopMatrix(m);
1312 Vector3D<Precision> localpoint = m.Transform(globalpoint);
1313
1314
1315 Precision safety = pvol->SafetyToOut(localpoint);
1316 limit = Min(safety, limit);
1317
1318 if (safety > 0 && pvol->GetDaughters().size() > 0) {
1319 auto bvh = GetBVH(pvol->GetLogicalVolume()->id());
1320 safety = ComputeBVHSafety<Self_t>(*bvh, localpoint, safety, limit);
1321 }
1322
1323 return safety;
1324 }
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335 VECCORE_ATT_HOST_DEVICE
1336 static Precision ComputeStepAndPropagatedStateImpl(Vector3D<Precision> const &globalpoint,
1337 Vector3D<Precision> const &globaldir, Precision step_limit,
1338 vecgeom::NavigationState const &in_state,
1339 vecgeom::NavigationState &out_state)
1340 {
1341 if (in_state.Top() == nullptr) return kInfLength;
1342 const Precision push = in_state.IsOnBoundary() ? kBoundaryPush : 0.;
1343
1344 if (step_limit < push) {
1345
1346
1347 in_state.CopyTo(&out_state);
1348 out_state.SetBoundaryState(false);
1349 return step_limit;
1350 }
1351 step_limit -= push;
1352
1353
1354 Vector3D<Precision> localpoint;
1355 Vector3D<Precision> localdir;
1356
1357 vecgeom::Transformation3D m;
1358 in_state.TopMatrix(m);
1359 localpoint = m.Transform(globalpoint);
1360 localdir = m.TransformDirection(globaldir);
1361
1362 Daughter hitcandidate = nullptr;
1363
1364 Precision step =
1365 ComputeStepAndHit(localpoint + push * localdir, localdir, step_limit, in_state, out_state, hitcandidate);
1366 step += push;
1367
1368 if (out_state.IsOnBoundary()) {
1369
1370 localpoint += (step + kBoundaryPush) * localdir;
1371
1372 if (!hitcandidate) {
1373
1374 RelocatePoint(localpoint, out_state);
1375 } else {
1376
1377 LocatePointIn(hitcandidate, localpoint, out_state, false);
1378 }
1379
1380 if (out_state.Top() != nullptr) {
1381 while (out_state.Top()->IsAssembly() || out_state.HasSamePathAsOther(in_state)) {
1382 out_state.Pop();
1383 }
1384 VECGEOM_ASSERT(!out_state.Top()->GetLogicalVolume()->GetUnplacedVolume()->IsAssembly());
1385 }
1386 }
1387
1388 return step;
1389 }
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405 VECCORE_ATT_HOST_DEVICE
1406 static Precision ComputeStepAndNextVolumeImpl(Vector3D<Precision> const &globalpoint,
1407 Vector3D<Precision> const &globaldir, Precision step_limit,
1408 vecgeom::NavigationState const &in_state,
1409 vecgeom::NavigationState &out_state)
1410 {
1411 if (in_state.Top() == nullptr) return kInfLength;
1412 const Precision push = in_state.IsOnBoundary() ? kBoundaryPush : 0.;
1413
1414 if (step_limit < push) {
1415
1416
1417 in_state.CopyTo(&out_state);
1418 if (step_limit > kTolerance) out_state.SetBoundaryState(false);
1419 return step_limit;
1420 }
1421 step_limit -= push;
1422
1423
1424 Vector3D<Precision> localpoint;
1425 Vector3D<Precision> localdir;
1426
1427 vecgeom::Transformation3D m;
1428 in_state.TopMatrix(m);
1429 localpoint = m.Transform(globalpoint);
1430 localdir = m.TransformDirection(globaldir);
1431
1432 Daughter hitcandidate = nullptr;
1433
1434 Precision step =
1435 ComputeStepAndHit(localpoint + push * localdir, localdir, step_limit, in_state, out_state, hitcandidate);
1436 step += (step > 0.) * push;
1437
1438 if (out_state.IsOnBoundary()) {
1439 if (!hitcandidate) {
1440 vecgeom::VPlacedVolume const *currentmother = out_state.Top();
1441 Vector3D<Precision> transformed = localpoint;
1442
1443 transformed += (step + kBoundaryPush) * localdir;
1444 do {
1445 out_state.SetLastExited();
1446 out_state.Pop();
1447 transformed = currentmother->GetTransformation()->InverseTransform(transformed);
1448 currentmother = out_state.Top();
1449 } while (currentmother &&
1450 (currentmother->IsAssembly() || currentmother->GetUnplacedVolume()->Inside(transformed) != kInside));
1451 } else {
1452 out_state.Push(hitcandidate);
1453 }
1454 }
1455
1456 return step;
1457 }
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467 VECCORE_ATT_HOST_DEVICE
1468 static Precision ComputeStepToApproachNextVolumeImpl(Vector3D<Precision> const &globalpoint,
1469 Vector3D<Precision> const &globaldir, Precision step_limit,
1470 vecgeom::NavigationState const &in_state)
1471 {
1472 if (in_state.Top() == nullptr) return kInfLength;
1473
1474 Vector3D<Precision> localpoint;
1475 Vector3D<Precision> localdir;
1476
1477 vecgeom::Transformation3D m;
1478 in_state.TopMatrix(m);
1479 localpoint = m.Transform(globalpoint);
1480 localdir = m.TransformDirection(globaldir);
1481
1482 Precision step = ApproachNextVolume(localpoint, localdir, step_limit, in_state);
1483
1484 return step;
1485 }
1486
1487
1488
1489
1490
1491
1492
1493 VECCORE_ATT_HOST_DEVICE
1494 static void RelocateToNextVolumeImpl(Vector3D<Precision> const &globalpoint, Vector3D<Precision> const &globaldir,
1495 vecgeom::NavigationState &state)
1496 {
1497
1498 if (state.IsOutside()) return;
1499
1500
1501
1502 Vector3D<Precision> pushed = globalpoint ;
1503
1504
1505 vecgeom::Transformation3D m;
1506 state.TopMatrix(m);
1507 Vector3D<Precision> localpoint = m.Transform(pushed);
1508
1509
1510 LocatePointInNavState(localpoint, state, false, state.GetLastExited());
1511
1512 if (state.Top() != nullptr) {
1513 while (state.Top()->IsAssembly()) {
1514 state.Pop();
1515 }
1516 VECGEOM_ASSERT(!state.Top()->GetLogicalVolume()->GetUnplacedVolume()->IsAssembly());
1517 }
1518 }
1519
1520
1521 };
1522
1523
1524 using IndexedBVHNavigator = BVHNavigator;
1525
1526 }
1527
1528 #endif