Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-28 08:19:31

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