Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-29 09:44:02

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/Geometry/GeometryIdentifier.hpp"
0012 #include "Acts/Geometry/Layer.hpp"
0013 #include "Acts/Geometry/TrackingGeometry.hpp"
0014 #include "Acts/Geometry/TrackingVolume.hpp"
0015 #include "Acts/Navigation/INavigationPolicy.hpp"
0016 #include "Acts/Navigation/NavigationStream.hpp"
0017 #include "Acts/Propagator/NavigationTarget.hpp"
0018 #include "Acts/Propagator/NavigatorInitializeArguments.hpp"
0019 #include "Acts/Propagator/NavigatorOptions.hpp"
0020 #include "Acts/Propagator/NavigatorStatistics.hpp"
0021 #include "Acts/Propagator/detail/AdditionalSurfaceNavigation.hpp"
0022 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0023 #include "Acts/Surfaces/Surface.hpp"
0024 #include "Acts/Utilities/Logger.hpp"
0025 #include "Acts/Utilities/Result.hpp"
0026 
0027 #include <limits>
0028 #include <optional>
0029 #include <string>
0030 
0031 #include <boost/container/small_vector.hpp>
0032 
0033 namespace Acts {
0034 
0035 /// @brief The navigation options for the tracking geometry
0036 ///
0037 /// @tparam object_t Type of the object for navigation to check against
0038 template <typename object_t>
0039 struct NavigationOptions final {
0040   /// The boundary check directive
0041   BoundaryTolerance boundaryTolerance = BoundaryTolerance::None();
0042 
0043   // How to resolve the geometry
0044   /// Always look for sensitive
0045   bool resolveSensitive = true;
0046   /// Always look for material
0047   bool resolveMaterial = true;
0048   /// always look for passive
0049   bool resolvePassive = false;
0050 
0051   /// Hint for start object
0052   const object_t* startObject = nullptr;
0053   /// Hint for end object
0054   const object_t* endObject = nullptr;
0055 
0056   /// Boundary tolerance to use instead of @c boundaryTolerance, for the
0057   /// surfaces named by their identifier
0058   std::vector<std::pair<GeometryIdentifier, BoundaryTolerance>>
0059       extendedSurfaces = {};
0060 
0061   /// The minimum distance for a surface to be considered
0062   double nearLimit = 0;
0063   /// The maximum distance for a surface to be considered
0064   double farLimit = std::numeric_limits<double>::max();
0065 };
0066 
0067 /// @brief Steers the propagation through the geometry by providing the next
0068 /// surface to be targeted.
0069 ///
0070 /// The Navigator is part of the propagation and responsible for steering
0071 /// the surface sequence to encounter all the relevant surfaces which are
0072 /// intersected by the trajectory.
0073 ///
0074 /// The current navigation stage is cached in the state struct and updated
0075 /// when necessary. If any surface in the extrapolation flow is hit, it is
0076 /// set to the navigation state, such that other actors can deal with it.
0077 ///
0078 /// The current target surface is referenced by an index which points into
0079 /// the navigation candidates. The navigation candidates are ordered by the
0080 /// path length to the surface. If a surface is hit, the
0081 /// `state.currentSurface` pointer is set. This actors to observe
0082 /// that we are on a surface.
0083 ///
0084 class Navigator final {
0085  public:
0086   /// Type alias for navigation surface candidates container
0087   using NavigationSurfaces =
0088       boost::container::small_vector<NavigationTarget, 10>;
0089 
0090   /// Type alias for navigation layer candidates container
0091   using NavigationLayers = boost::container::small_vector<NavigationTarget, 10>;
0092 
0093   /// Type alias for navigation boundary candidates container
0094   using NavigationBoundaries =
0095       boost::container::small_vector<NavigationTarget, 4>;
0096 
0097   /// Type alias for geometry version enumeration
0098   using GeometryVersion = TrackingGeometry::GeometryVersion;
0099 
0100   /// The navigation stage
0101   enum struct Stage : int {
0102     initial = 0,
0103     surfaceTarget = 1,
0104     layerTarget = 2,
0105     boundaryTarget = 3,
0106   };
0107 
0108   /// The navigator configuration
0109   struct Config {
0110     /// Tracking Geometry for this Navigator
0111     std::shared_ptr<const TrackingGeometry> trackingGeometry{nullptr};
0112 
0113     /// stop at every sensitive surface (whether it has material or not)
0114     bool resolveSensitive = true;
0115     /// stop at every material surface (whether it is passive or not)
0116     bool resolveMaterial = true;
0117     /// stop at every surface regardless what it is
0118     bool resolvePassive = false;
0119     /// Number of candidates to reserve memory for in the navigation stream
0120     std::size_t candidatePreReserve = 50;
0121   };
0122 
0123   /// The navigator options
0124   struct Options : public NavigatorPlainOptions {
0125     /// Constructor with geometry context
0126     /// @param gctx The geometry context for the navigation
0127     explicit Options(const GeometryContext& gctx)
0128         : NavigatorPlainOptions(gctx) {}
0129 
0130     /// Set the plain navigation options
0131     /// @param options The plain navigator options to set
0132     void setPlainOptions(const NavigatorPlainOptions& options) {
0133       static_cast<NavigatorPlainOptions&>(*this) = options;
0134     }
0135   };
0136 
0137   /// @brief Nested State struct
0138   ///
0139   /// It acts as an internal state which is created for every propagation and
0140   /// meant to keep thread-local navigation information.
0141   struct State {
0142     /// Constructor with navigation options
0143     /// @param options_ The navigation options for this state
0144     explicit State(const Options& options_) : options(options_) {}
0145 
0146     /// Navigation options configuration
0147     Options options;
0148 
0149     /// Latest global geometric curvature supplied at a public navigation entry
0150     /// point. Forwarded to policy creation, validity, and candidate queries.
0151     Vector3 curvature = Vector3::Zero();
0152 
0153     /// Management of policy state allocation and deallocation
0154     NavigationPolicyStateManager policyStateManager;
0155 
0156     /// Whether the current volume's policy state carries no validity
0157     /// constraint. Sourced from INavigationPolicy::isStateless() (probed once
0158     /// at construction, fixed thereafter) and cached at each volume transition,
0159     /// so the per-step checks read this local bool instead of chasing the
0160     /// policy pointer or re-deriving defaultness from the type-erased state.
0161     bool policyStateIsDefault = true;
0162 
0163     // Navigation on surface level
0164     /// the vector of navigation surfaces to work through
0165     NavigationSurfaces navSurfaces = {};
0166     /// the current surface index of the navigation state
0167     std::optional<std::size_t> navSurfaceIndex;
0168 
0169     // Navigation on layer level
0170     /// the vector of navigation layers to work through
0171     NavigationLayers navLayers = {};
0172     /// the current layer index of the navigation state
0173     std::optional<std::size_t> navLayerIndex;
0174 
0175     // Navigation on volume level
0176     /// the vector of boundary surfaces to work through
0177     NavigationBoundaries navBoundaries = {};
0178     /// the current boundary index of the navigation state
0179     std::optional<std::size_t> navBoundaryIndex;
0180 
0181     /// far limit applied to the stream candidates, set during candidate
0182     /// resolution
0183     double navCandidatesFarLimit = std::numeric_limits<double>::max();
0184 
0185     /// An extended surface with its volume resolved
0186     struct ResolvedExtendedSurface {
0187       /// The surface
0188       const Surface* surface{};
0189       /// Tolerance used to intersect the surface
0190       BoundaryTolerance boundaryTolerance = BoundaryTolerance::Infinite();
0191       /// Volume that holds the surface. Null for Gen1, which scopes through
0192       /// the layer.
0193       const TrackingVolume* volume{};
0194     };
0195 
0196     /// Extended surfaces resolved once at initialization
0197     std::vector<ResolvedExtendedSurface> extendedSurfaces{};
0198 
0199     /// Additional surfaces and candidate history
0200     detail::AdditionalSurfaceNavigation additional;
0201 
0202     /// Get reference to current navigation surface
0203     /// @return Reference to current navigation target
0204     NavigationTarget& navSurface() {
0205       return navSurfaces.at(navSurfaceIndex.value());
0206     }
0207 
0208     /// Get reference to current navigation layer
0209     /// @return Reference to current layer intersection
0210     NavigationTarget& navLayer() { return navLayers.at(navLayerIndex.value()); }
0211 
0212     /// Get reference to current navigation boundary
0213     /// @return Reference to current boundary intersection
0214     NavigationTarget& navBoundary() {
0215       return navBoundaries.at(navBoundaryIndex.value());
0216     }
0217 
0218     /// Get reference to current navigation candidate
0219     /// @return Reference to current boundary intersection
0220     NavigationTarget& navCandidate() { return stream.currentCandidate(); }
0221 
0222     /// Volume where the navigation started
0223     const TrackingVolume* startVolume = nullptr;
0224     /// Layer where the navigation started
0225     const Layer* startLayer = nullptr;
0226     /// Surface where the navigation started
0227     const Surface* startSurface = nullptr;
0228     /// Current volume during navigation
0229     const TrackingVolume* currentVolume = nullptr;
0230     /// Current layer during navigation
0231     const Layer* currentLayer = nullptr;
0232     /// Current surface during navigation
0233     const Surface* currentSurface = nullptr;
0234     /// Target surface for navigation
0235     const Surface* targetSurface = nullptr;
0236 
0237     /// Flag to break navigation loop
0238     bool navigationBreak = false;
0239     /// Current navigation stage in the state machine
0240     Stage navigationStage = Stage::initial;
0241 
0242     /// Statistics collection for navigation performance
0243     NavigatorStatistics statistics;
0244 
0245     /// Stream for navigation debugging and monitoring
0246     NavigationStream stream;
0247 
0248     /// Reset navigation state after switching layers
0249     void resetAfterLayerSwitch() {
0250       navSurfaces.clear();
0251       navSurfaceIndex.reset();
0252     }
0253 
0254     /// Reset navigation state after switching volumes
0255     void resetAfterVolumeSwitch() {
0256       resetAfterLayerSwitch();
0257 
0258       navLayers.clear();
0259       navLayerIndex.reset();
0260       navBoundaries.clear();
0261       navBoundaryIndex.reset();
0262 
0263       currentLayer = nullptr;
0264 
0265       policyStateManager.reset();
0266       policyStateIsDefault = true;
0267 
0268       stream.reset();
0269     }
0270 
0271     /// Reset navigation state for renavigating within the same navigation run
0272     ///
0273     /// @note Keeps the start and target information, which actors rely on for
0274     ///       the whole run.
0275     void resetForRenavigation() {
0276       resetAfterVolumeSwitch();
0277 
0278       currentVolume = nullptr;
0279       currentSurface = nullptr;
0280 
0281       navigationBreak = false;
0282       navigationStage = Stage::initial;
0283     }
0284 
0285     /// Completely reset navigation state for a new navigation run
0286     ///
0287     /// In contrast to @c resetForRenavigation this also drops the start and
0288     /// target information.
0289     void resetForInitialization() {
0290       resetForRenavigation();
0291 
0292       startVolume = nullptr;
0293       startLayer = nullptr;
0294       startSurface = nullptr;
0295       targetSurface = nullptr;
0296 
0297       statistics = {};
0298     }
0299   };
0300 
0301   /// Constructor with configuration object
0302   ///
0303   /// @param cfg The navigator configuration
0304   /// @param _logger a logger instance
0305   explicit Navigator(Config cfg,
0306                      std::shared_ptr<const Logger> _logger =
0307                          getDefaultLogger("Navigator", Logging::Level::INFO));
0308 
0309   /// Create a navigation state from options
0310   /// @param options The navigation options
0311   /// @return A new navigation state
0312   State makeState(const Options& options) const;
0313 
0314   /// Get the current surface from navigation state
0315   /// @param state The navigation state
0316   /// @return Pointer to current surface, or nullptr if none
0317   const Surface* currentSurface(const State& state) const;
0318 
0319   /// Get the current volume from navigation state
0320   /// @param state The navigation state
0321   /// @return Pointer to current volume, or nullptr if none
0322   const TrackingVolume* currentVolume(const State& state) const;
0323 
0324   /// Get material properties of the current volume
0325   /// @param state The navigation state
0326   /// @return Pointer to volume material, or nullptr if no volume or material
0327   const IVolumeMaterial* currentVolumeMaterial(const State& state) const;
0328 
0329   /// Get the starting surface from navigation state
0330   /// @param state The navigation state
0331   /// @return Pointer to start surface, or nullptr if none
0332   const Surface* startSurface(const State& state) const;
0333 
0334   /// Get the target surface from navigation state
0335   /// @param state The navigation state
0336   /// @return Pointer to target surface, or nullptr if none
0337   const Surface* targetSurface(const State& state) const;
0338 
0339   /// Check if navigation has reached the end of the world (no current volume)
0340   /// @param state The navigation state
0341   /// @return True if end of world is reached
0342   bool endOfWorldReached(const State& state) const;
0343 
0344   /// Check if navigation should be interrupted
0345   /// @param state The navigation state
0346   /// @return True if navigation break flag is set
0347   bool navigationBreak(const State& state) const;
0348 
0349   /// @brief Initialize the navigator state
0350   ///
0351   /// This function initializes the navigator state for a new propagation. All
0352   /// inputs specific to this run are passed here, so a state can be reused
0353   /// without carrying anything over.
0354   ///
0355   /// @param state The navigation state
0356   /// @param args The initialization arguments of this navigation run
0357   ///
0358   /// @return Indication if the initialization was successful
0359   [[nodiscard]] Result<void> initialize(
0360       State& state, const NavigatorInitializeArguments& args) const;
0361 
0362   /// @brief Get the next target surface
0363   ///
0364   /// This function gets the next target surface for the propagation.
0365   ///
0366   /// @param state The navigation state
0367   /// @param position The current position
0368   /// @param direction The current direction
0369   /// @param curvature Current global geometric curvature (inverse length)
0370   ///
0371   /// @return The next target surface
0372   NavigationTarget nextTarget(State& state, const Vector3& position,
0373                               const Vector3& direction,
0374                               const Vector3& curvature = Vector3::Zero()) const;
0375 
0376   /// @brief Check if the current target is still valid
0377   ///
0378   /// This function checks if the target is valid.
0379   ///
0380   /// @param state The navigation state
0381   /// @param position The current position
0382   /// @param direction The current direction
0383   /// @param curvature Current global geometric curvature (inverse length)
0384   ///
0385   /// @return True if the target is valid
0386   bool checkTargetValid(State& state, const Vector3& position,
0387                         const Vector3& direction,
0388                         const Vector3& curvature = Vector3::Zero()) const;
0389 
0390   /// @brief Handle the surface reached
0391   ///
0392   /// This function handles the surface reached.
0393   ///
0394   /// @param state The navigation state
0395   /// @param position The current position
0396   /// @param direction The current direction
0397   /// @param curvature Current global geometric curvature (inverse length)
0398   /// @param surface The surface reached
0399   void handleSurfaceReached(State& state, const Vector3& position,
0400                             const Vector3& direction, const Surface& surface,
0401                             const Vector3& curvature = Vector3::Zero()) const;
0402 
0403  private:
0404   /// @brief Resolve the extended surfaces of the options
0405   ///
0406   /// Throws if the tracking geometry does not hold a surface.
0407   ///
0408   /// @param state The navigation state
0409   void resolveExtendedSurfaces(State& state) const;
0410 
0411   /// @brief Whether the staged candidates are still valid, without the
0412   ///        additional surfaces
0413   ///
0414   /// @param state The navigation state
0415   /// @param position The current position
0416   /// @param direction The current direction
0417   /// @return True if the staged candidates are still valid
0418   bool checkStagedTargetValid(State& state, const Vector3& position,
0419                               const Vector3& direction) const;
0420 
0421   /// @brief Get the next target of the staged navigation, without the
0422   ///        additional surfaces
0423   ///
0424   /// @param state The navigation state
0425   /// @param position The current position
0426   /// @param direction The current direction
0427   /// @return The next staged target
0428   NavigationTarget nextStagedTarget(State& state, const Vector3& position,
0429                                     const Vector3& direction) const;
0430 
0431   /// @brief NextTarget helper function for Gen1 geometry configuration
0432   ///
0433   /// @param state The navigation state
0434   /// @param position The current position
0435   /// @param direction The current direction
0436   NavigationTarget getNextTargetGen1(State& state, const Vector3& position,
0437                                      const Vector3& direction) const;
0438 
0439   /// @brief NextTarget helper function for Gen3 geometry configuration
0440   ///
0441   /// @param state The navigation state
0442   /// @param position The current position
0443   /// @param direction The current direction
0444   NavigationTarget getNextTargetGen3(State& state, const Vector3& position,
0445                                      const Vector3& direction) const;
0446 
0447   /// @brief NextTarget helper function
0448   /// This function is called for returning the next target
0449   /// and checks gen1/gen3 case in order to sub-call the proper functions
0450   ///
0451   /// @param state The navigation state
0452   /// @param position The current position
0453   /// @param direction The current direction
0454   NavigationTarget tryGetNextTarget(State& state, const Vector3& position,
0455                                     const Vector3& direction) const;
0456 
0457   /// @brief Resolve compatible candidates (surfaces or portals) for gen3
0458   /// navigation
0459   ///
0460   /// This function is called when gen3 configuration is found and it resolves
0461   /// at the same time for portals and surfaces
0462   /// @param state The navigation state
0463   /// @param position The current position
0464   /// @param direction The current direction
0465   void resolveCandidates(State& state, const Vector3& position,
0466                          const Vector3& direction) const;
0467 
0468   /// @brief Create the navigation policy state for the given volume
0469   ///
0470   /// Volumes whose navigation policy is known to push only default states
0471   /// (probed at geometry construction) skip the state creation entirely; the
0472   /// matching popState on volume exit is skipped under the same condition.
0473   /// The caller must ensure the volume has a navigation policy.
0474   ///
0475   /// @param state The navigation state
0476   /// @param volume The volume to create the policy state for
0477   /// @param position The current position
0478   /// @param direction The current direction
0479   void createPolicyState(State& state, const TrackingVolume& volume,
0480                          const Vector3& position,
0481                          const Vector3& direction) const;
0482 
0483   /// @brief Resolve compatible surfaces
0484   ///
0485   /// This function resolves the compatible surfaces for the navigation.
0486   ///
0487   /// @param state The navigation state
0488   /// @param position The current position
0489   /// @param direction The current direction
0490   void resolveSurfaces(State& state, const Vector3& position,
0491                        const Vector3& direction) const;
0492 
0493   /// @brief Resolve compatible layers
0494   ///
0495   /// This function resolves the compatible layers for the navigation.
0496   ///
0497   /// @param state The navigation state
0498   /// @param position The current position
0499   /// @param direction The current direction
0500   void resolveLayers(State& state, const Vector3& position,
0501                      const Vector3& direction) const;
0502 
0503   /// @brief Resolve compatible boundaries
0504   ///
0505   /// This function resolves the compatible boundaries for the navigation.
0506   ///
0507   /// @param state The navigation state
0508   /// @param position The current position
0509   /// @param direction The current direction
0510   void resolveBoundaries(State& state, const Vector3& position,
0511                          const Vector3& direction) const;
0512 
0513   /// @brief Check if the navigator is inactive
0514   ///
0515   /// This function checks if the navigator is inactive.
0516   ///
0517   /// @param state The navigation state
0518   ///
0519   /// @return True if the navigator is inactive
0520   bool inactive(const State& state) const;
0521 
0522   /// @brief Get volume info string for logging
0523   ///
0524   /// @tparam propagator_state_t The propagator state type
0525   /// @param state The state containing current volume info
0526   /// @return String with volume name for logging
0527   std::string volInfo(const State& state) const;
0528 
0529   /// @brief Get volume info string for logging
0530   ///
0531   /// Used where the volume is not reflected in the navigation state yet.
0532   ///
0533   /// @param volume The volume to report on, may be nullptr
0534   /// @return String with volume name for logging
0535   std::string volInfo(const TrackingVolume* volume) const;
0536 
0537   const Logger& logger() const { return *m_logger; }
0538 
0539   Config m_cfg;
0540 
0541   // Cached so we don't have to query the TrackingGeometry constantly.
0542   TrackingGeometry::GeometryVersion m_geometryVersion{};
0543 
0544   std::shared_ptr<const Logger> m_logger;
0545 };
0546 
0547 }  // namespace Acts