File indexing completed on 2026-11-29 09:43:59
0001
0002
0003
0004
0005
0006
0007
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
0024
0025
0026
0027
0028
0029
0030
0031
0032 class AdditionalSurfaceNavigation {
0033
0034 struct AdditionalSurfaceState {
0035
0036 AdditionalSurface surface;
0037
0038 bool reached = false;
0039
0040
0041 std::array<bool, s_maximumNumberOfIntersections> pending{};
0042 };
0043
0044
0045 std::vector<AdditionalSurfaceState> m_additionalSurfaces;
0046
0047
0048
0049
0050 NavigationTarget m_heldBack = NavigationTarget::None();
0051
0052
0053 NavigationTarget m_handedOut = NavigationTarget::None();
0054
0055 bool m_handedOutIsAdditional = false;
0056
0057 public:
0058
0059
0060
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
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
0083
0084
0085
0086
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
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
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
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
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
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
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
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
0183
0184 m_heldBack = m_handedOut;
0185 m_handedOut = NavigationTarget::None();
0186 return false;
0187 }
0188 return true;
0189 }
0190
0191
0192
0193
0194
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
0209 NavigationTarget handOut(const NavigationTarget& target, bool isAdditional) {
0210 m_handedOut = target;
0211 m_handedOutIsAdditional = isAdditional && !target.isNone();
0212 return target;
0213 }
0214
0215
0216
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
0234
0235
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
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
0269
0270
0271
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 }