Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-25 09:02:57

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "Acts/Navigation/NavigationStream.hpp"
0010 
0011 #include "Acts/Geometry/TrackingVolume.hpp"
0012 #include "Acts/Propagator/NavigationTarget.hpp"
0013 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0014 #include "Acts/Surfaces/Surface.hpp"
0015 #include "Acts/Utilities/Enumerate.hpp"
0016 #include "Acts/Utilities/StringHelpers.hpp"
0017 
0018 #include <algorithm>
0019 
0020 namespace Acts {
0021 
0022 bool NavigationStream::initialize(const GeometryContext& gctx,
0023                                   const QueryPoint& queryPoint,
0024                                   const Logger& logger,
0025                                   const double onSurfaceTolerance,
0026                                   const bool candidatesAreUnique) {
0027   // Position and direction from the query point
0028   const Vector3& position = queryPoint.position;
0029   const Vector3& direction = queryPoint.direction;
0030 
0031   // De-duplicate by surface pointer. The first entry wins, so its tolerance is
0032   // the one used.
0033   if (!candidatesAreUnique) {
0034     ACTS_VERBOSE("De-duplicate the candidates:" << m_candidates);
0035     std::size_t writeIdx = 0;
0036     for (std::size_t readIdx = 0; readIdx < m_candidates.size(); ++readIdx) {
0037       const Surface* surface = &m_candidates[readIdx].surface();
0038       bool alreadySeen = false;
0039       for (std::size_t k = 0; k < writeIdx; ++k) {
0040         if (&m_candidates[k].surface() == surface) {
0041           alreadySeen = true;
0042           break;
0043         }
0044       }
0045       if (!alreadySeen) {
0046         if (writeIdx != readIdx) {
0047           m_candidates[writeIdx] = m_candidates[readIdx];
0048         }
0049         ++writeIdx;
0050       }
0051     }
0052     m_candidates.erase(m_candidates.begin() + writeIdx, m_candidates.end());
0053   }
0054 
0055   // Collect additional candidates for the second valid intersection. Reuse the
0056   // member scratch buffer to avoid a heap allocation on every call.
0057   std::vector<NavigationTarget>& additionalCandidates = m_additionalCandidates;
0058   additionalCandidates.clear();
0059   for (auto& candidate : m_candidates) {
0060     // Get the surface from the object intersection
0061     const Surface& surface = candidate.surface();
0062     // Intersect the surface
0063     auto multiIntersection =
0064         surface.intersect(gctx, position, direction,
0065                           candidate.boundaryTolerance(), onSurfaceTolerance);
0066 
0067     bool firstValid = multiIntersection.at(0).isValid();
0068     bool secondValid = multiIntersection.at(1).isValid();
0069     if (firstValid && !secondValid) {
0070       if (multiIntersection.at(0).pathLength() < -onSurfaceTolerance) {
0071         continue;
0072       }
0073       candidate.intersection() = multiIntersection.at(0);
0074       candidate.intersectionIndex() = 0;
0075     } else if (!firstValid && secondValid) {
0076       if (multiIntersection.at(1).pathLength() < -onSurfaceTolerance) {
0077         continue;
0078       }
0079       candidate.intersection() = multiIntersection.at(1);
0080       candidate.intersectionIndex() = 1;
0081     } else {
0082       // Split them into valid intersections, keep track of potentially
0083       // additional candidates
0084       bool originalCandidateUpdated = false;
0085       for (auto [intersectionIndex, intersection] :
0086            enumerate(multiIntersection)) {
0087         // Skip negative solutions, respecting the on surface tolerance
0088         if (intersection.pathLength() < -onSurfaceTolerance) {
0089           continue;
0090         }
0091         // Valid solution is either on surface or updates the distance
0092         if (intersection.isValid()) {
0093           if (!originalCandidateUpdated) {
0094             candidate.intersection() = intersection;
0095             candidate.intersectionIndex() = intersectionIndex;
0096             originalCandidateUpdated = true;
0097           } else {
0098             NavigationTarget additionalCandidate = candidate;
0099             additionalCandidate.intersection() = intersection;
0100             additionalCandidate.intersectionIndex() = intersectionIndex;
0101             additionalCandidates.emplace_back(additionalCandidate);
0102           }
0103         }
0104       }
0105     }
0106   }
0107 
0108   // Drop the candidates without a valid intersection before sorting,
0109   // would sort last and be cut off anyway...
0110   std::erase_if(m_candidates, [](const NavigationTarget& a) {
0111     return !a.intersection().isValid();
0112   });
0113 
0114   // Append the multi intersection candidates (all valid)
0115   m_candidates.insert(m_candidates.end(), additionalCandidates.begin(),
0116                       additionalCandidates.end());
0117 
0118   // Sort the candidates by path length
0119   std::ranges::sort(m_candidates, NavigationTarget::pathLengthOrder);
0120   ACTS_VERBOSE("Sorted candidates:" << m_candidates);
0121 
0122   // Remove duplicate intersections, preserving distinct crossings of the same
0123   // surface. In particular, an on-surface entry must not discard a later exit.
0124   auto nonUniqueRange = std::ranges::unique(
0125       m_candidates.begin(), m_candidates.end(),
0126       [](const NavigationTarget& a, const NavigationTarget& b) {
0127         return &a.surface() == &b.surface() &&
0128                a.intersectionIndex() == b.intersectionIndex();
0129       });
0130   m_candidates.erase(nonUniqueRange.begin(), nonUniqueRange.end());
0131 
0132   m_currentIndex = 0;
0133   return isValid();
0134 }
0135 
0136 bool NavigationStream::update(const GeometryContext& gctx,
0137                               const QueryPoint& queryPoint,
0138                               const Logger& logger, double onSurfaceTolerance) {
0139   ACTS_VERBOSE("Update from position " << toString(queryPoint.position)
0140                                        << " and direction "
0141                                        << toString(queryPoint.direction));
0142   // Loop over the (currently valid) candidates and update
0143   for (; m_currentIndex < m_candidates.size(); ++m_currentIndex) {
0144     // Get the candidate, and resolve the tuple
0145     NavigationTarget& candidate = currentCandidate();
0146     // Get the surface from the object intersection
0147     const Surface& surface = candidate.surface();
0148     // (re-)Intersect the surface
0149     auto multiIntersection =
0150         surface.intersect(gctx, queryPoint.position, queryPoint.direction,
0151                           candidate.boundaryTolerance(), onSurfaceTolerance);
0152     // Split them into valid intersections
0153     for (auto [intersectionIndex, intersection] :
0154          enumerate(multiIntersection)) {
0155       // Skip wrong index solution
0156       if (intersectionIndex != candidate.intersectionIndex()) {
0157         continue;
0158       }
0159       // Valid solution is either on surface or updates the distance
0160       if (intersection.isValid()) {
0161         candidate.intersection() = intersection;
0162         ACTS_VERBOSE("Updated candidate " << candidate);
0163         return true;
0164       }
0165     }
0166   }
0167   // No candidate was reachable
0168   return false;
0169 }
0170 
0171 void NavigationStream::reset() {
0172   m_candidates.clear();
0173   m_currentIndex = 0;
0174 }
0175 
0176 void NavigationStream::addSurfaceCandidate(
0177     const Surface& surface, const BoundaryTolerance& bTolerance) {
0178   m_candidates.emplace_back(Intersection3D::Invalid(), 0, surface, bTolerance);
0179 }
0180 
0181 void NavigationStream::addSurfaceCandidates(
0182     std::span<const Surface*> surfaces, const BoundaryTolerance& bTolerance) {
0183   m_candidates.reserve(m_candidates.size() + surfaces.size());
0184   std::ranges::for_each(surfaces, [&](const Surface* surface) {
0185     m_candidates.emplace_back(Intersection3D::Invalid(), 0, *surface,
0186                               bTolerance);
0187   });
0188 }
0189 
0190 void NavigationStream::addPortalCandidate(const Portal& portal) {
0191   m_candidates.emplace_back(Intersection3D::Invalid(), 0, portal,
0192                             BoundaryTolerance::None());
0193 }
0194 
0195 void NavigationStream::addPortalCandidate(const Portal& portal,
0196                                           const Surface& representation) {
0197   m_candidates.emplace_back(Intersection3D::Invalid(), 0, portal,
0198                             representation, BoundaryTolerance::None());
0199 }
0200 
0201 AppendOnlyNavigationStream::AppendOnlyNavigationStream(
0202     NavigationStream& stream, const TrackingVolume* volume)
0203     : m_stream{&stream}, m_volume{volume} {}
0204 
0205 void AppendOnlyNavigationStream::addPortalCandidate(const Portal& portal) {
0206   const Surface* face =
0207       m_volume != nullptr ? m_volume->portalFace(portal) : nullptr;
0208   if (face != nullptr) {
0209     m_stream->addPortalCandidate(portal, *face);
0210   } else {
0211     m_stream->addPortalCandidate(portal);
0212   }
0213 }
0214 
0215 void AppendOnlyNavigationStream::addSurfaceCandidate(
0216     const Surface& surface, const BoundaryTolerance& bTolerance) {
0217   m_stream->addSurfaceCandidate(surface, bTolerance);
0218 }
0219 
0220 }  // namespace Acts