Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-23 09:21:40

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/Geometry/TrackingVolume.hpp"
0010 
0011 #include "Acts/Definitions/Direction.hpp"
0012 #include "Acts/Geometry/GeometryIdentifier.hpp"
0013 #include "Acts/Geometry/GlueVolumesDescriptor.hpp"
0014 #include "Acts/Geometry/Portal.hpp"
0015 #include "Acts/Geometry/TrackingGeometryVisitor.hpp"
0016 #include "Acts/Geometry/VolumeBounds.hpp"
0017 #include "Acts/Material/IMaterialDecorator.hpp"
0018 #include "Acts/Material/IVolumeMaterial.hpp"
0019 #include "Acts/Navigation/INavigationPolicy.hpp"
0020 #include "Acts/Navigation/NavigationStream.hpp"
0021 #include "Acts/Propagator/NavigationTarget.hpp"
0022 #include "Acts/Propagator/Navigator.hpp"
0023 #include "Acts/Surfaces/RegularSurface.hpp"
0024 #include "Acts/Surfaces/Surface.hpp"
0025 #include "Acts/Surfaces/SurfaceArray.hpp"
0026 #include "Acts/Utilities/Enumerate.hpp"
0027 #include "Acts/Utilities/Intersection.hpp"
0028 
0029 #include <algorithm>
0030 #include <memory>
0031 #include <ostream>
0032 #include <string>
0033 #include <utility>
0034 
0035 #include <boost/container/small_vector.hpp>
0036 
0037 #include "VolumeLookupImpl.hpp"
0038 
0039 namespace Acts {
0040 
0041 TrackingVolume::TrackingVolume(
0042     const Transform3& transform, std::shared_ptr<VolumeBounds> volumeBounds,
0043     std::shared_ptr<const IVolumeMaterial> volumeMaterial,
0044     std::unique_ptr<const LayerArray> staticLayerArray,
0045     std::shared_ptr<const TrackingVolumeArray> containedVolumeArray,
0046     MutableTrackingVolumeVector denseVolumeVector,
0047     const std::string& volumeName)
0048     : Volume(transform, std::move(volumeBounds)),
0049       m_confinedLayers(std::move(staticLayerArray)),
0050       m_confinedVolumes(std::move(containedVolumeArray)),
0051       m_confinedDenseVolumes({}),
0052       m_volumeMaterial(std::move(volumeMaterial)),
0053       m_name(volumeName) {
0054   createBoundarySurfaces();
0055   interlinkLayers();
0056   connectDenseBoundarySurfaces(denseVolumeVector);
0057 
0058   m_navigationDelegate.connect<&INavigationPolicy::noopInitializeCandidates>();
0059 }
0060 
0061 TrackingVolume::TrackingVolume(const Volume& volume,
0062                                const std::string& volumeName)
0063     : Volume{volume}, m_name{volumeName} {
0064   m_navigationDelegate.connect<&INavigationPolicy::noopInitializeCandidates>();
0065 }
0066 
0067 TrackingVolume::TrackingVolume(const Transform3& transform,
0068                                std::shared_ptr<VolumeBounds> volbounds,
0069                                const std::string& volumeName)
0070     : Volume{transform, std::move(volbounds)}, m_name{volumeName} {
0071   m_navigationDelegate.connect<&INavigationPolicy::noopInitializeCandidates>();
0072 }
0073 
0074 TrackingVolume::TrackingVolume(VolumePlacementBase& placement,
0075                                std::shared_ptr<VolumeBounds> volbounds,
0076                                const std::string& volumeName)
0077     : Volume{placement, std::move(volbounds)}, m_name{volumeName} {
0078   m_navigationDelegate.connect<&INavigationPolicy::noopInitializeCandidates>();
0079 }
0080 
0081 TrackingVolume::~TrackingVolume() = default;
0082 TrackingVolume::TrackingVolume(TrackingVolume&&) noexcept = default;
0083 TrackingVolume& TrackingVolume::operator=(TrackingVolume&&) noexcept = default;
0084 
0085 const TrackingVolume* TrackingVolume::lowestTrackingVolume(
0086     const GeometryContext& gctx, const Vector3& position,
0087     const double tol) const {
0088   return m_volumeLookup ? m_volumeLookup->findLowest(gctx, position, tol)
0089                         : detail::linearVolumeScan(*this, gctx, position, tol);
0090 }
0091 
0092 void TrackingVolume::setVolumeLookup(
0093     std::unique_ptr<const IVolumeLookup> lookup) {
0094   m_volumeLookup = std::move(lookup);
0095 }
0096 
0097 const IVolumeLookup* TrackingVolume::volumeLookup() const {
0098   return m_volumeLookup.get();
0099 }
0100 
0101 const TrackingVolumeBoundaries& TrackingVolume::boundarySurfaces() const {
0102   return m_boundarySurfaces;
0103 }
0104 
0105 void TrackingVolume::connectDenseBoundarySurfaces(
0106     MutableTrackingVolumeVector& confinedDenseVolumes) {
0107   if (!confinedDenseVolumes.empty()) {
0108     Direction dir = Direction::Positive();
0109     // Walk over each dense volume
0110     for (auto& confDenseVol : confinedDenseVolumes) {
0111       // Walk over each boundary surface of the volume
0112       auto& boundSur = confDenseVol->boundarySurfaces();
0113       for (std::size_t i = 0; i < boundSur.size(); i++) {
0114         // Skip empty entries since we do not know the shape of the dense volume
0115         // and therewith the used indices
0116         if (boundSur.at(i) == nullptr) {
0117           continue;
0118         }
0119 
0120         // Use mother volume as the opposite direction of the already used
0121         // direction
0122         auto mutableBs =
0123             std::const_pointer_cast<BoundarySurfaceT<TrackingVolume>>(
0124                 boundSur.at(i));
0125         if (mutableBs->m_oppositeVolume != nullptr &&
0126             mutableBs->m_alongVolume == nullptr) {
0127           dir = Direction::Positive();
0128           mutableBs->attachVolume(this, dir);
0129         } else {
0130           if (mutableBs->m_oppositeVolume == nullptr &&
0131               mutableBs->m_alongVolume != nullptr) {
0132             dir = Direction::Negative();
0133             mutableBs->attachVolume(this, dir);
0134           }
0135         }
0136 
0137         // Update the boundary
0138         confDenseVol->updateBoundarySurface(static_cast<BoundarySurfaceFace>(i),
0139                                             mutableBs);
0140       }
0141       // Store the volume
0142       m_confinedDenseVolumes.push_back(std::move(confDenseVol));
0143     }
0144   }
0145 }
0146 
0147 void TrackingVolume::createBoundarySurfaces() {
0148   using Boundary = BoundarySurfaceT<TrackingVolume>;
0149 
0150   // Transform Surfaces To BoundarySurfaces
0151   if (isAlignable()) {
0152     throw std::runtime_error(
0153         "createBoundarySurfaces() - Alignable volumes cannot have boundary "
0154         "surfaces");
0155   }
0156   const GeometryContext gctx = GeometryContext::dangerouslyDefaultConstruct();
0157   auto orientedSurfaces =
0158       volumeBounds().orientedSurfaces(localToGlobalTransform(gctx));
0159 
0160   m_boundarySurfaces.reserve(orientedSurfaces.size());
0161   for (auto& osf : orientedSurfaces) {
0162     TrackingVolume* opposite = nullptr;
0163     TrackingVolume* along = nullptr;
0164     if (osf.direction == Direction::OppositeNormal()) {
0165       opposite = this;
0166     } else {
0167       along = this;
0168     }
0169     m_boundarySurfaces.push_back(std::make_shared<const Boundary>(
0170         std::move(osf.surface), opposite, along));
0171   }
0172 }
0173 
0174 void TrackingVolume::clearBoundarySurfaces() {
0175   m_boundarySurfaces.clear();
0176 }
0177 
0178 void TrackingVolume::glueTrackingVolume(const GeometryContext& gctx,
0179                                         BoundarySurfaceFace bsfMine,
0180                                         TrackingVolume* neighbor,
0181                                         BoundarySurfaceFace bsfNeighbor) {
0182   // Find the connection of the two tracking volumes: AxisDirection::AxisR
0183   // returns the center except for cylindrical volumes
0184   Vector3 bPosition(referencePosition(gctx, AxisDirection::AxisR));
0185   Vector3 distance =
0186       neighbor->referencePosition(gctx, AxisDirection::AxisR) - bPosition;
0187   // glue to the face
0188   std::shared_ptr<const BoundarySurfaceT<TrackingVolume>> bSurfaceMine =
0189       boundarySurfaces().at(bsfMine);
0190   // @todo - complex glueing could be possible with actual intersection for the
0191   // normal vector
0192   // Coerce the arbitrary position bPosition to be on the surface repr so we can
0193   // get a normal
0194   Vector3 nvector =
0195       bSurfaceMine->surfaceRepresentation().normal(gctx, bPosition);
0196   // estimate the orientation
0197   Direction dir = Direction::fromScalar(nvector.dot(distance));
0198   // The easy case :
0199   // - no glue volume descriptors on either side
0200   if ((m_glueVolumeDescriptor == nullptr) ||
0201       m_glueVolumeDescriptor->glueVolumes(bsfMine) == nullptr) {
0202     // the boundary orientation
0203     auto mutableBSurfaceMine =
0204         std::const_pointer_cast<BoundarySurfaceT<TrackingVolume>>(bSurfaceMine);
0205     mutableBSurfaceMine->attachVolume(neighbor, dir);
0206     // Make sure you keep the boundary material if there
0207     const Surface& neighborSurface =
0208         neighbor->m_boundarySurfaces.at(bsfNeighbor)->surfaceRepresentation();
0209     auto neighborMaterial = neighborSurface.surfaceMaterialSharedPtr();
0210     const Surface& mySurface = bSurfaceMine->surfaceRepresentation();
0211 
0212     // Keep the neighbor material
0213     if (auto myMaterial = mySurface.surfaceMaterialSharedPtr();
0214         myMaterial == nullptr && neighborMaterial != nullptr) {
0215       auto* myMutbableSurface = const_cast<Surface*>(&mySurface);
0216       myMutbableSurface->assignSurfaceMaterial(neighborMaterial);
0217     }
0218     // Now set it to the neighbor volume
0219     (neighbor->m_boundarySurfaces).at(bsfNeighbor) = bSurfaceMine;
0220   }
0221 }
0222 
0223 void TrackingVolume::glueTrackingVolumes(
0224     const GeometryContext& gctx, BoundarySurfaceFace bsfMine,
0225     const std::shared_ptr<TrackingVolumeArray>& neighbors,
0226     BoundarySurfaceFace bsfNeighbor) {
0227   // find the connection of the two tracking volumes : AxisDirection::AxisR
0228   // returns the center except for cylindrical volumes
0229   std::shared_ptr<const TrackingVolume> nRefVolume =
0230       neighbors->arrayObjects().at(0);
0231   // get the distance
0232   Vector3 bPosition(referencePosition(gctx, AxisDirection::AxisR));
0233   Vector3 distance(nRefVolume->referencePosition(gctx, AxisDirection::AxisR) -
0234                    bPosition);
0235   // take the normal at the binning positio
0236   std::shared_ptr<const BoundarySurfaceT<TrackingVolume>> bSurfaceMine =
0237       boundarySurfaces().at(bsfMine);
0238   // @todo - complex glueing could be possible with actual intersection for the
0239   // normal vector
0240   // Coerce the arbitrary position bPosition to be on the surface repr so we can
0241   // get a normal
0242   Vector3 nvector =
0243       bSurfaceMine->surfaceRepresentation().normal(gctx, bPosition);
0244   // estimate the orientation
0245   Direction dir = Direction::fromScalar(nvector.dot(distance));
0246   // the easy case :
0247   // - no glue volume descriptors on either side
0248   if ((m_glueVolumeDescriptor == nullptr) ||
0249       !m_glueVolumeDescriptor->glueVolumes(bsfMine)) {
0250     // the boundary orientation
0251     auto mutableBSurfaceMine =
0252         std::const_pointer_cast<BoundarySurfaceT<TrackingVolume>>(bSurfaceMine);
0253     mutableBSurfaceMine->attachVolumeArray(neighbors, dir);
0254     // now set it to the neighbor volumes - the optised way
0255     for (auto& nVolume : neighbors->arrayObjects()) {
0256       auto mutableNVolume = std::const_pointer_cast<TrackingVolume>(nVolume);
0257       (mutableNVolume->m_boundarySurfaces).at(bsfNeighbor) = bSurfaceMine;
0258     }
0259   }
0260 }
0261 
0262 void TrackingVolume::assignBoundaryMaterial(
0263     std::shared_ptr<const ISurfaceMaterial> surfaceMaterial,
0264     BoundarySurfaceFace bsFace) {
0265   auto bSurface = m_boundarySurfaces.at(bsFace);
0266   auto* surface =
0267       const_cast<RegularSurface*>(&bSurface->surfaceRepresentation());
0268   surface->assignSurfaceMaterial(std::move(surfaceMaterial));
0269 }
0270 
0271 void TrackingVolume::updateBoundarySurface(
0272     BoundarySurfaceFace bsf,
0273     std::shared_ptr<const BoundarySurfaceT<TrackingVolume>> bs,
0274     bool checkmaterial) {
0275   if (checkmaterial) {
0276     auto cMaterialPtr = m_boundarySurfaces.at(bsf)
0277                             ->surfaceRepresentation()
0278                             .surfaceMaterialSharedPtr();
0279     auto bsMaterial = bs->surfaceRepresentation().surfaceMaterial();
0280     if (cMaterialPtr != nullptr && bsMaterial == nullptr) {
0281       auto* surface = const_cast<RegularSurface*>(&bs->surfaceRepresentation());
0282       surface->assignSurfaceMaterial(cMaterialPtr);
0283     }
0284   }
0285   m_boundarySurfaces.at(bsf) = std::move(bs);
0286 }
0287 
0288 void TrackingVolume::registerGlueVolumeDescriptor(
0289     std::unique_ptr<GlueVolumesDescriptor> gvd) {
0290   m_glueVolumeDescriptor = std::move(gvd);
0291 }
0292 
0293 GlueVolumesDescriptor& TrackingVolume::glueVolumesDescriptor() {
0294   if (m_glueVolumeDescriptor == nullptr) {
0295     m_glueVolumeDescriptor = std::make_unique<GlueVolumesDescriptor>();
0296   }
0297   return *m_glueVolumeDescriptor;
0298 }
0299 
0300 void TrackingVolume::synchronizeLayers(double envelope) const {
0301   // container volume -> step down
0302   if (m_confinedVolumes) {
0303     for (auto& cVolumesIter : m_confinedVolumes->arrayObjects()) {
0304       cVolumesIter->synchronizeLayers(envelope);
0305     }
0306   }
0307 }
0308 
0309 void TrackingVolume::interlinkLayers() {
0310   if (!m_confinedLayers) {
0311     return;
0312   }
0313   auto& layers = m_confinedLayers->arrayObjects();
0314 
0315   // forward register the last one as the previous one
0316   //  first <- | -> second, first <- | -> second, first <- | -> second
0317   const Layer* lastLayer = nullptr;
0318   for (auto& layerPtr : layers) {
0319     // we'll need to mutate our confined layers to perform this operation
0320     Layer& mutableLayer = *(std::const_pointer_cast<Layer>(layerPtr));
0321     // register the layers
0322     mutableLayer.m_nextLayerUtility = m_confinedLayers->binUtility();
0323     mutableLayer.m_nextLayers.first = lastLayer;
0324     // register the volume
0325     mutableLayer.encloseTrackingVolume(*this);
0326     // remember the last layer
0327     lastLayer = &mutableLayer;
0328   }
0329   // backward loop
0330   lastLayer = nullptr;
0331   for (auto layerIter = layers.rbegin(); layerIter != layers.rend();
0332        ++layerIter) {
0333     // set the other next volume
0334     Layer& mutableLayer = *(std::const_pointer_cast<Layer>(*layerIter));
0335     mutableLayer.m_nextLayers.second = lastLayer;
0336     lastLayer = &mutableLayer;
0337   }
0338 }
0339 
0340 // Returns the boundary surfaces ordered in probability to hit them based on
0341 boost::container::small_vector<NavigationTarget, 4>
0342 TrackingVolume::compatibleBoundaries(const GeometryContext& gctx,
0343                                      const Vector3& position,
0344                                      const Vector3& direction,
0345                                      const NavigationOptions<Surface>& options,
0346                                      const Logger& logger) const {
0347   ACTS_VERBOSE("Finding compatibleBoundaries");
0348 
0349   boost::container::small_vector<NavigationTarget, 4> targets;
0350 
0351   // The limits for this navigation step
0352   double nearLimit = options.nearLimit;
0353   double farLimit = options.farLimit;
0354 
0355   // Helper function to test intersection
0356   auto checkIntersection =
0357       [&](MultiIntersection3D& candidates,
0358           const BoundarySurface* boundary) -> NavigationTarget {
0359     for (auto [intersectionIndex, intersection] : Acts::enumerate(candidates)) {
0360       if (!intersection.isValid()) {
0361         continue;
0362       }
0363 
0364       ACTS_VERBOSE("Check intersection with surface "
0365                    << boundary->surfaceRepresentation().geometryId());
0366       if (detail::checkPathLength(intersection.pathLength(), nearLimit,
0367                                   farLimit, logger)) {
0368         return {intersection, intersectionIndex, *boundary,
0369                 options.boundaryTolerance};
0370       }
0371     }
0372 
0373     ACTS_VERBOSE("No intersection accepted");
0374     return NavigationTarget::None();
0375   };
0376 
0377   /// Helper function to process boundary surfaces
0378   auto processBoundaries = [&](const TrackingVolumeBoundaries& boundaries) {
0379     // Loop over the boundary surfaces
0380     for (const auto& boundary : boundaries) {
0381       // Get the boundary surface pointer
0382       const auto& surface = boundary->surfaceRepresentation();
0383       ACTS_VERBOSE("Consider boundary surface " << surface.geometryId());
0384 
0385       // Exclude the boundary where you are on
0386       // TODO this is not optimal as we might exit via the same boundary (e.g.
0387       // cylinder)
0388       if (&surface == options.startObject) {
0389         ACTS_VERBOSE(" - Surface is excluded surface");
0390         continue;
0391       }
0392 
0393       auto intersections = surface.intersect(gctx, position, direction,
0394                                              options.boundaryTolerance);
0395       // Intersect and continue
0396       NavigationTarget candidate =
0397           checkIntersection(intersections, boundary.get());
0398       if (candidate.isValid()) {
0399         ACTS_VERBOSE(" - Proceed with surface");
0400         targets.push_back(candidate);
0401       } else {
0402         ACTS_VERBOSE(" - Surface intersection invalid");
0403       }
0404     }
0405   };
0406 
0407   // Process the boundaries of the current volume
0408   const auto& surfaces = boundarySurfaces();
0409   ACTS_VERBOSE("Volume reports " << surfaces.size() << " boundary surfaces");
0410   processBoundaries(surfaces);
0411 
0412   // Process potential boundaries of contained volumes
0413   auto confinedDenseVolumes = denseVolumes();
0414   ACTS_VERBOSE("Volume reports " << confinedDenseVolumes.size()
0415                                  << " confined dense volumes");
0416   for (const auto& volume : confinedDenseVolumes) {
0417     const auto& surfacesConfined = volume->boundarySurfaces();
0418     ACTS_VERBOSE(" -> " << surfacesConfined.size() << " boundary surfaces");
0419     processBoundaries(surfacesConfined);
0420   }
0421 
0422   return targets;
0423 }
0424 
0425 boost::container::small_vector<NavigationTarget, 10>
0426 TrackingVolume::compatibleLayers(
0427     const GeometryContext& gctx, const Vector3& position,
0428     const Vector3& direction, const NavigationOptions<Layer>& options) const {
0429   // the layer intersections which are valid
0430   boost::container::small_vector<NavigationTarget, 10> targets;
0431 
0432   // the confinedLayers
0433   if (m_confinedLayers == nullptr) {
0434     return {};
0435   }
0436 
0437   // start layer given or not - test layer
0438   const Layer* layer = options.startObject != nullptr
0439                            ? options.startObject
0440                            : associatedLayer(gctx, position);
0441   while (layer != nullptr) {
0442     // check if the layer needs resolving
0443     // - resolveSensitive -> always take layer if it has a surface array
0444     // - resolveMaterial -> always take layer if it has material
0445     // - resolvePassive -> always take, unless it's a navigation layer
0446     // skip the start object
0447     if (layer != options.startObject && layer->resolve(options)) {
0448       // if it's a resolveable start layer, you are by definition on it
0449       // layer on approach intersection
0450       auto candidate =
0451           layer->surfaceOnApproach(gctx, position, direction, options);
0452       // Intersection is ok - take it (move to surface on approach)
0453       if (candidate.isValid()) {
0454         // create a layer intersection
0455         targets.push_back(candidate);
0456       }
0457     }
0458     // move to next one or break because you reached the end layer
0459     layer = layer == options.endObject
0460                 ? nullptr
0461                 : layer->nextLayer(gctx, position, direction);
0462   }
0463 
0464   // In case of cylindrical layers we might resolve far intersection solutions
0465   // which are not valid for navigation. These are discarded here by checking
0466   // against the minimum path length.
0467   auto min =
0468       std::ranges::min_element(targets, NavigationTarget::pathLengthOrder);
0469   std::ranges::rotate(targets, min);
0470   targets.resize(std::distance(min, targets.end()), NavigationTarget::None());
0471 
0472   return targets;
0473 }
0474 
0475 const std::string& TrackingVolume::volumeName() const {
0476   return m_name;
0477 }
0478 
0479 void TrackingVolume::setVolumeName(std::string_view volumeName) {
0480   m_name = volumeName;
0481 }
0482 
0483 bool TrackingVolume::hasMaterial() const {
0484   return m_volumeMaterial != nullptr;
0485 }
0486 
0487 const IVolumeMaterial* TrackingVolume::volumeMaterial() const {
0488   return m_volumeMaterial.get();
0489 }
0490 
0491 const std::shared_ptr<const IVolumeMaterial>&
0492 TrackingVolume::volumeMaterialPtr() const {
0493   return m_volumeMaterial;
0494 }
0495 
0496 void TrackingVolume::assignVolumeMaterial(
0497     std::shared_ptr<const IVolumeMaterial> material) {
0498   m_volumeMaterial = std::move(material);
0499 }
0500 
0501 const LayerArray* TrackingVolume::confinedLayers() const {
0502   return m_confinedLayers.get();
0503 }
0504 
0505 MutableTrackingVolumeVector TrackingVolume::denseVolumes() const {
0506   return m_confinedDenseVolumes;
0507 }
0508 
0509 std::shared_ptr<const TrackingVolumeArray> TrackingVolume::confinedVolumes()
0510     const {
0511   return m_confinedVolumes;
0512 }
0513 
0514 const TrackingVolume* TrackingVolume::motherVolume() const {
0515   return m_motherVolume;
0516 }
0517 
0518 TrackingVolume* TrackingVolume::motherVolume() {
0519   return m_motherVolume;
0520 }
0521 
0522 void TrackingVolume::setMotherVolume(TrackingVolume* mvol) {
0523   m_motherVolume = mvol;
0524 }
0525 
0526 const Layer* TrackingVolume::associatedLayer(const GeometryContext& /*gctx*/,
0527                                              const Vector3& position) const {
0528   // confined static layers - highest hierarchy
0529   if (m_confinedLayers != nullptr) {
0530     return (m_confinedLayers->object(position).get());
0531   }
0532 
0533   // return the null pointer
0534   return nullptr;
0535 }
0536 
0537 TrackingVolume::VolumeRange TrackingVolume::volumes() const {
0538   return VolumeRange{m_volumes};
0539 }
0540 
0541 TrackingVolume::MutableVolumeRange TrackingVolume::volumes() {
0542   return MutableVolumeRange{m_volumes};
0543 }
0544 
0545 TrackingVolume& TrackingVolume::addVolume(
0546     std::unique_ptr<TrackingVolume> volume) {
0547   if (volume->motherVolume() != nullptr) {
0548     throw std::invalid_argument("Volume already has a mother volume");
0549   }
0550 
0551   volume->setMotherVolume(this);
0552   m_volumes.push_back(std::move(volume));
0553   return *m_volumes.back();
0554 }
0555 
0556 std::span<const TrackingVolume::PlacementOwnPtr> TrackingVolume::placements()
0557     const {
0558   return m_placements;
0559 }
0560 
0561 TrackingVolume::PortalRange TrackingVolume::portals() const {
0562   return PortalRange{m_portals};
0563 }
0564 
0565 TrackingVolume::MutablePortalRange TrackingVolume::portals() {
0566   return MutablePortalRange{m_portals};
0567 }
0568 
0569 void TrackingVolume::addPortal(std::shared_ptr<Portal> portal) {
0570   if (portal == nullptr) {
0571     throw std::invalid_argument("Portal is nullptr");
0572   }
0573   m_portals.push_back(std::move(portal));
0574 }
0575 
0576 TrackingVolume::SurfaceRange TrackingVolume::surfaces() const {
0577   return SurfaceRange{m_surfaces};
0578 }
0579 
0580 TrackingVolume::MutableSurfaceRange TrackingVolume::surfaces() {
0581   return MutableSurfaceRange{m_surfaces};
0582 }
0583 
0584 void TrackingVolume::addSurface(std::shared_ptr<Surface> surface) {
0585   if (surface == nullptr) {
0586     throw std::invalid_argument("Surface is nullptr");
0587   }
0588   m_surfaces.push_back(std::move(surface));
0589 }
0590 
0591 void TrackingVolume::visualize(IVisualization3D& helper,
0592                                const GeometryContext& gctx,
0593                                const ViewConfig& viewConfig,
0594                                const ViewConfig& portalViewConfig,
0595                                const ViewConfig& sensitiveViewConfig) const {
0596   visualize(helper, gctx, [&](const GeometryObject& geoObj) {
0597     if (geoObj.geometryId().boundary() != 0) {
0598       return portalViewConfig;
0599     }
0600     if (geoObj.geometryId().sensitive() != 0) {
0601       return sensitiveViewConfig;
0602     }
0603     return viewConfig;
0604   });
0605 }
0606 
0607 void TrackingVolume::visualize(IVisualization3D& helper,
0608                                const GeometryContext& gctx,
0609                                const ViewConfigFunc& viewConfigFactory) const {
0610   helper.object(volumeName());
0611   {
0612     auto viewConfig = viewConfigFactory(*this);
0613     Volume::visualize(helper, gctx, viewConfig);
0614   }
0615 
0616   if (!surfaces().empty()) {
0617     helper.object(volumeName() + "_sensitives");
0618     for (const auto& surface : surfaces()) {
0619       auto viewConfig = viewConfigFactory(surface);
0620       surface.visualize(helper, gctx, viewConfig);
0621     }
0622   }
0623 
0624   if (!portals().empty()) {
0625     helper.object(volumeName() + "_portals");
0626     for (const auto& portal : portals()) {
0627       auto viewConfig = viewConfigFactory(portal.surface());
0628       portal.surface().visualize(helper, gctx, viewConfig);
0629     }
0630   }
0631 
0632   for (const auto& child : volumes()) {
0633     child.visualize(helper, gctx, viewConfigFactory);
0634   }
0635 }
0636 
0637 const INavigationPolicy* TrackingVolume::navigationPolicy() const {
0638   return m_navigationPolicy.get();
0639 }
0640 
0641 INavigationPolicy* TrackingVolume::navigationPolicy() {
0642   return m_navigationPolicy.get();
0643 }
0644 
0645 void TrackingVolume::setNavigationPolicy(
0646     std::unique_ptr<INavigationPolicy> policy) {
0647   if (policy == nullptr) {
0648     throw std::invalid_argument("Navigation policy is nullptr");
0649   }
0650 
0651   m_navigationPolicy = std::move(policy);
0652   m_navigationPolicy->connect(m_navigationDelegate);
0653 }
0654 
0655 void TrackingVolume::initializeNavigationCandidates(
0656     const GeometryContext& gctx, const NavigationArguments& args,
0657     NavigationPolicyState& state, AppendOnlyNavigationStream& stream,
0658     const Logger& logger) const {
0659   m_navigationDelegate(gctx, args, state, stream, logger);
0660 }
0661 
0662 namespace {
0663 
0664 void visitLayer(const Layer& layer, TrackingGeometryVisitor& visitor) {
0665   visitor.visitLayer(layer);
0666   // Surfaces contained in the surface array
0667   if (layer.surfaceArray() != nullptr) {
0668     for (const auto& srf : layer.surfaceArray()->surfaces()) {
0669       visitor.visitSurface(*srf);
0670     }
0671   }
0672   visitor.visitSurface(layer.surfaceRepresentation());
0673   if (layer.approachDescriptor() != nullptr) {
0674     for (const auto& srf : layer.approachDescriptor()->containedSurfaces()) {
0675       visitor.visitSurface(*srf);
0676     }
0677   }
0678 }
0679 
0680 }  // namespace
0681 
0682 // @TODO: Unify once Gen1 is removed: should share most code between mutable and const
0683 void TrackingVolume::apply(TrackingGeometryVisitor& visitor) const {
0684   visitor.visitVolume(*this);
0685 
0686   // Visit the boundary surfaces
0687   for (const auto& bs : m_boundarySurfaces) {
0688     visitor.visitBoundarySurface(*bs);
0689   }
0690 
0691   for (const auto& portal : portals()) {
0692     visitor.visitPortal(portal);
0693   }
0694 
0695   // Internal structure
0696   if (m_confinedLayers != nullptr) {
0697     std::ranges::for_each(
0698         m_confinedLayers->arrayObjects(),
0699         [&](const auto& layer) { visitLayer(*layer, visitor); });
0700   }
0701 
0702   if (m_confinedVolumes != nullptr) {
0703     // contains sub volumes
0704     for (const auto& volume : m_confinedVolumes->arrayObjects()) {
0705       volume->apply(visitor);
0706     }
0707   }
0708 
0709   for (const auto& surface : surfaces()) {
0710     visitor.visitSurface(surface);
0711   }
0712 
0713   for (const auto& volume : volumes()) {
0714     volume.apply(visitor);
0715   }
0716 }
0717 
0718 void TrackingVolume::apply(TrackingGeometryMutableVisitor& visitor) {
0719   // if the visitor is configured for inner--->outer volume visiting we visit
0720   // the children first
0721   if (visitor.visitDepthFirst()) {
0722     for (auto& volume : volumes()) {
0723       volume.apply(visitor);
0724     }
0725   }
0726 
0727   visitor.visitVolume(*this);
0728 
0729   // Visit the boundary surfaces
0730   // This does const casts because Gen1 substructure does not have transitive
0731   // const-ness
0732 
0733   // @TODO: Remove this when Gen1 is remoeved
0734 
0735   for (const auto& bs : m_boundarySurfaces) {
0736     visitor.visitBoundarySurface(
0737         const_cast<BoundarySurfaceT<TrackingVolume>&>(*bs));
0738     visitor.visitSurface(
0739         const_cast<RegularSurface&>(bs->surfaceRepresentation()));
0740   }
0741 
0742   for (auto& portal : portals()) {
0743     visitor.visitPortal(portal);
0744     visitor.visitSurface(portal.surface());
0745   }
0746 
0747   // Internal structure
0748   // This does const casts because Gen1 substructure does not have transitive
0749   // const-ness
0750   // @TODO: Remove this when Gen1 is remoeved
0751   if (m_confinedVolumes == nullptr) {
0752     // no sub volumes => loop over the confined layers
0753     if (m_confinedLayers != nullptr) {
0754       for (const auto& layer : m_confinedLayers->arrayObjects()) {
0755         visitor.visitLayer(const_cast<Layer&>(*layer));
0756         // Surfaces contained in the surface array
0757         if (layer->surfaceArray() != nullptr) {
0758           for (const auto& srf : layer->surfaceArray()->surfaces()) {
0759             visitor.visitSurface(const_cast<Surface&>(*srf));
0760           }
0761         }
0762         // Surfaces of the layer
0763         visitor.visitSurface(
0764             const_cast<Surface&>(layer->surfaceRepresentation()));
0765         // Approach surfaces of the layer
0766         if (layer->approachDescriptor() != nullptr) {
0767           for (const auto& srf :
0768                layer->approachDescriptor()->containedSurfaces()) {
0769             visitor.visitSurface(const_cast<Surface&>(*srf));
0770           }
0771         }
0772       }
0773     }
0774   } else {
0775     // contains sub volumes
0776     for (const auto& volume : m_confinedVolumes->arrayObjects()) {
0777       const_cast<TrackingVolume&>(*volume).apply(visitor);
0778     }
0779   }
0780 
0781   for (auto& surface : surfaces()) {
0782     visitor.visitSurface(surface);
0783   }
0784 
0785   if (!visitor.visitDepthFirst()) {
0786     // if the visitor is configured for outer--->inner volume visiting we visit
0787     // the children last
0788     for (auto& volume : volumes()) {
0789       volume.apply(visitor);
0790     }
0791   }
0792 }
0793 
0794 }  // namespace Acts