Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-29 09:43:59

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 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Propagator/NavigationTarget.hpp"
0013 #include "Acts/Propagator/NavigatorOptions.hpp"
0014 #include "Acts/Surfaces/Surface.hpp"
0015 #include "Acts/Utilities/Enumerate.hpp"
0016 
0017 #include <array>
0018 #include <stdexcept>
0019 #include <vector>
0020 
0021 namespace Acts::detail {
0022 
0023 /// Offers the additional surfaces of the navigator options and the target
0024 /// surface, on top of the candidates of a geometry navigator. Each navigation
0025 /// state owns one instance.
0026 ///
0027 /// The additional surfaces and the candidates of the geometry navigator are two
0028 /// streams. Both are intersected again before every step, and the closer one is
0029 /// the target. An intersection seen ahead stays a candidate when it moves
0030 /// behind, so the propagation can correct backwards until the surface is
0031 /// reached. Unreachable targets are bounded by the propagation limits.
0032 class AdditionalSurfaceNavigation {
0033   /// An additional surface with its bookkeeping
0034   struct AdditionalSurfaceState {
0035     /// The additional surface
0036     AdditionalSurface surface;
0037     /// Whether the propagation reached the surface
0038     bool reached = false;
0039     /// Once seen ahead, keep each intersection available until the surface
0040     /// is reached, even if it moves behind the propagation.
0041     std::array<bool, s_maximumNumberOfIntersections> pending{};
0042   };
0043 
0044   /// Additional surfaces of the options and the target surface
0045   std::vector<AdditionalSurfaceState> m_additionalSurfaces;
0046 
0047   /// The candidate of another navigator an additional surface took
0048   /// precedence over. It is handed out once the additional surface is no
0049   /// longer the closer one.
0050   NavigationTarget m_heldBack = NavigationTarget::None();
0051 
0052   /// The target handed out to the propagation
0053   NavigationTarget m_handedOut = NavigationTarget::None();
0054   /// Whether the target handed out is an additional surface
0055   bool m_handedOutIsAdditional = false;
0056 
0057  public:
0058   /// Reset the surface list and candidate history for a navigation run.
0059   /// @param options The navigator options
0060   /// @param targetSurface Optional target to include with its bounds checked
0061   void initialize(const NavigatorPlainOptions& options,
0062                   const Surface* targetSurface) {
0063     m_additionalSurfaces.clear();
0064     m_additionalSurfaces.reserve(options.additionalSurfaces.size() + 1);
0065     for (const AdditionalSurface& surface : options.additionalSurfaces) {
0066       if (surface.surface == nullptr) {
0067         throw std::invalid_argument("Navigator: additional surface is nullptr");
0068       }
0069       m_additionalSurfaces.push_back({surface});
0070     }
0071     // Propagation targets require a bounds check.
0072     if (targetSurface != nullptr) {
0073       m_additionalSurfaces.push_back(
0074           {{targetSurface, BoundaryTolerance::None()}});
0075     }
0076 
0077     m_heldBack = NavigationTarget::None();
0078     m_handedOut = NavigationTarget::None();
0079     m_handedOutIsAdditional = false;
0080   }
0081 
0082   /// Returns the closest additional surface
0083   /// @param options The navigator options
0084   /// @param position The current position
0085   /// @param direction The current direction
0086   /// @return The next target, or none
0087   NavigationTarget nextTarget(const NavigatorPlainOptions& options,
0088                               const Vector3& position,
0089                               const Vector3& direction) {
0090     return handOut(closest(options, position, direction, nullptr), true);
0091   }
0092 
0093   /// Returns the closer of the next candidate of another navigator and the
0094   /// closest additional surface. The candidate is held back, not skipped,
0095   /// while an additional surface is closer.
0096   ///
0097   /// @param options The navigator options
0098   /// @param position The current position
0099   /// @param direction The current direction
0100   /// @param volume The current volume of the other navigator, read after
0101   ///        @p nextCandidate ran
0102   /// @param nextCandidate Callable returning the next candidate of the other
0103   ///        navigator
0104   /// @return The next target
0105   template <typename next_candidate_t>
0106   NavigationTarget nextTarget(const NavigatorPlainOptions& options,
0107                               const Vector3& position, const Vector3& direction,
0108                               const TrackingVolume* const& volume,
0109                               next_candidate_t&& nextCandidate) {
0110     if (m_additionalSurfaces.empty()) {
0111       return nextCandidate();
0112     }
0113 
0114     // Ask the other navigator again if it had no candidate
0115     if (m_heldBack.isNone()) {
0116       m_heldBack = nextCandidate();
0117     }
0118 
0119     if (const NavigationTarget additional =
0120             closest(options, position, direction, volume);
0121         !additional.isNone()) {
0122       refresh(options, m_heldBack, position, direction);
0123       // A tie goes to the candidate of the other navigator
0124       if (m_heldBack.isNone() ||
0125           additional.pathLength() < m_heldBack.intersection().pathLength()) {
0126         return handOut(additional, true);
0127       }
0128     }
0129 
0130     const NavigationTarget candidate = m_heldBack;
0131     m_heldBack = NavigationTarget::None();
0132     return handOut(candidate, false);
0133   }
0134 
0135   /// Rechecks the selected target against both candidate streams. Pending
0136   /// additional surfaces remain candidates when their intersections move
0137   /// behind.
0138   ///
0139   /// @param options The navigator options
0140   /// @param position The current position
0141   /// @param direction The current direction
0142   /// @param volume The current volume of the other navigator
0143   /// @param candidateValid Whether the other navigator holds its candidates
0144   ///        valid. If not, it is asked for a new one.
0145   /// @return True if the target handed out is still the closest
0146   bool checkTargetValid(const NavigatorPlainOptions& options,
0147                         const Vector3& position, const Vector3& direction,
0148                         const TrackingVolume* volume, bool candidateValid) {
0149     if (!candidateValid) {
0150       m_heldBack = NavigationTarget::None();
0151       handOut(NavigationTarget::None(), false);
0152       return false;
0153     }
0154     if (m_additionalSurfaces.empty() || m_handedOut.isNone()) {
0155       return true;
0156     }
0157 
0158     const NavigationTarget additional =
0159         closest(options, position, direction, volume);
0160 
0161     if (m_handedOutIsAdditional) {
0162       if (additional.isNone() ||
0163           &additional.surface() != &m_handedOut.surface() ||
0164           additional.intersectionIndex() != m_handedOut.intersectionIndex()) {
0165         return false;
0166       }
0167       if (!m_heldBack.isNone()) {
0168         refresh(options, m_heldBack, position, direction);
0169         // A tie goes to the candidate of the other navigator
0170         if (m_heldBack.intersection().pathLength() <= additional.pathLength()) {
0171           return false;
0172         }
0173       }
0174       return true;
0175     }
0176 
0177     if (additional.isNone()) {
0178       return true;
0179     }
0180     refresh(options, m_handedOut, position, direction);
0181     if (additional.pathLength() < m_handedOut.intersection().pathLength()) {
0182       // Hold the candidate back instead of asking the other navigator again,
0183       // which would move on to its next candidate
0184       m_heldBack = m_handedOut;
0185       m_handedOut = NavigationTarget::None();
0186       return false;
0187     }
0188     return true;
0189   }
0190 
0191   /// Handles reaching a surface for another navigator
0192   /// @param surface The surface reached
0193   /// @return True if the surface is an additional surface the other navigator
0194   ///         did not target, so the other navigator has nothing to do
0195   bool handleSurfaceReached(const Surface& surface) {
0196     bool found = false;
0197     for (AdditionalSurfaceState& additional : m_additionalSurfaces) {
0198       if (additional.surface.surface == &surface) {
0199         additional.reached = true;
0200         additional.pending.fill(false);
0201         found = true;
0202       }
0203     }
0204     return found && m_handedOutIsAdditional;
0205   }
0206 
0207  private:
0208   /// Records the target handed out
0209   NavigationTarget handOut(const NavigationTarget& target, bool isAdditional) {
0210     m_handedOut = target;
0211     m_handedOutIsAdditional = isAdditional && !target.isNone();
0212     return target;
0213   }
0214 
0215   /// Intersects a candidate of the other navigator again. Keeps the stored
0216   /// intersection if the straight-line estimate lost the surface.
0217   static void refresh(const NavigatorPlainOptions& options,
0218                       NavigationTarget& candidate, const Vector3& position,
0219                       const Vector3& direction) {
0220     if (candidate.isNone()) {
0221       return;
0222     }
0223     const Intersection3D refreshed =
0224         candidate.surface()
0225             .intersect(options.geoContext, position, direction,
0226                        candidate.boundaryTolerance(), options.surfaceTolerance)
0227             .at(candidate.intersectionIndex());
0228     if (refreshed.isValid()) {
0229       candidate.intersection() = refreshed;
0230     }
0231   }
0232 
0233   /// Closest additional surface in the current volume, ahead or pending.
0234   /// Intersections are tracked by index, which for a surface with two
0235   /// solutions is their order in signed path length.
0236   NavigationTarget closest(const NavigatorPlainOptions& options,
0237                            const Vector3& position, const Vector3& direction,
0238                            const TrackingVolume* volume) {
0239     NavigationTarget closest = NavigationTarget::None();
0240 
0241     for (AdditionalSurfaceState& additional : m_additionalSurfaces) {
0242       const AdditionalSurface& surface = additional.surface;
0243       if (additional.reached && surface.dropAfterReached) {
0244         continue;
0245       }
0246       if (surface.volume != nullptr && surface.volume != volume) {
0247         continue;
0248       }
0249 
0250       // The closer solution can be behind the propagation, so check all
0251       const MultiIntersection3D multiIntersection = surface.surface->intersect(
0252           options.geoContext, position, direction, surface.boundaryTolerance,
0253           options.surfaceTolerance);
0254 
0255       for (const auto [intersectionIndex, intersection] :
0256            enumerate(multiIntersection)) {
0257         if (!intersection.isValid() ||
0258             intersection.pathLength() > options.farLimit) {
0259           continue;
0260         }
0261         const double pathLength = intersection.pathLength();
0262         const bool onSurface =
0263             intersection.status() == IntersectionStatus::onSurface;
0264         if (!onSurface && pathLength > 0) {
0265           additional.pending[intersectionIndex] = true;
0266         }
0267 
0268         // Retain an earlier forward candidate even if it is now behind, as a
0269         // turn can change the order of the candidates. The stepper corrects
0270         // backwards, just as for a direct navigation target. A kept surface
0271         // is not reached twice at the same position.
0272         const bool offer = onSurface ? !additional.reached
0273                                      : additional.pending[intersectionIndex];
0274         if (!offer) {
0275           continue;
0276         }
0277         if (closest.isNone() || pathLength < closest.pathLength()) {
0278           closest = NavigationTarget(
0279               intersection, static_cast<IntersectionIndex>(intersectionIndex),
0280               *surface.surface, surface.boundaryTolerance);
0281         }
0282       }
0283     }
0284 
0285     return closest;
0286   }
0287 };
0288 
0289 }  // namespace Acts::detail