Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-21 08:54:00

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/BlueprintNode.hpp"
0012 #include "Acts/Geometry/BlueprintOptions.hpp"
0013 #include "Acts/Geometry/PortalShell.hpp"
0014 #include "Acts/Geometry/TrackingVolume.hpp"
0015 #include "Acts/Geometry/VolumeAttachmentStrategy.hpp"
0016 #include "Acts/Geometry/VolumeResizeStrategy.hpp"
0017 #include "Acts/Geometry/VolumeStack.hpp"
0018 #include "Acts/Utilities/AxisDefinitions.hpp"
0019 #include "Acts/Utilities/GraphViz.hpp"
0020 #include "Acts/Utilities/Logger.hpp"
0021 #include "Acts/Utilities/ThrowAssert.hpp"
0022 
0023 #include <map>
0024 
0025 namespace Acts::Experimental {
0026 
0027 /// @class ContainerBlueprintNode
0028 ///
0029 /// A blueprint node that can contain multiple child volumes. It is responsible
0030 /// for managing the child volumes and their shells. The child volumes can be
0031 /// either gap volumes or volumes from child nodes.
0032 ///
0033 /// The container node is responsible for:
0034 /// 1. Managing the child volumes and their shells
0035 /// 2. Creating gap volumes between child volumes
0036 /// 3. Collecting shells from child nodes and gap volumes
0037 /// 4. Building the volume stack
0038 ///
0039 /// The container node is an abstract base class. Derived classes must
0040 /// implement:
0041 /// 1. makeStack - to create the appropriate volume stack
0042 /// 2. typeName - to provide the type name for debug output
0043 ///
0044 class ContainerBlueprintNode : public BlueprintNode {
0045  public:
0046   /// Main constructor for the container node.
0047   /// @param name The name of the node (for debug only)
0048   /// @param axis The stacking axis direction in local reference frame
0049   /// @param attachmentStrategy The attachment strategy for the stack
0050   /// @param resizeStrategy The resize strategy for the stack
0051   ContainerBlueprintNode(
0052       const std::string& name, AxisDirection axis,
0053       VolumeAttachmentStrategy attachmentStrategy =
0054           VolumeAttachmentStrategy::Midpoint,
0055       VolumeResizeStrategy resizeStrategy = VolumeResizeStrategy::Expand);
0056 
0057   /// Main constructor for the container node.
0058   /// @param name The name of the node (for debug only)
0059   /// @param axis The stacking axis direction in local reference frame
0060   /// @param attachmentStrategy The attachment strategy for the stack
0061   /// @param resizeStrategies The resize strategies for the stack
0062   ContainerBlueprintNode(
0063       const std::string& name, AxisDirection axis,
0064       VolumeAttachmentStrategy attachmentStrategy,
0065       std::pair<VolumeResizeStrategy, VolumeResizeStrategy> resizeStrategies);
0066 
0067   /// @copydoc BlueprintNode::name
0068   const std::string& name() const override;
0069 
0070   /// This participates in the construction of the geometry via the blueprint
0071   /// tree. The steps are approximately as follows:
0072   /// -# Collect all child volumes
0073   /// -# Package them into a VolumeStack (cuboid or cylinder), which performs
0074   ///    sizing and/or gap creation
0075   /// -# Return the VolumeStack as a volume up the tree
0076   ///
0077   /// @param options The global blueprint options
0078   /// @param gctx The geometry context (nominal usually)
0079   /// @param logger The logger to use
0080   /// @return The combined VolumeStack
0081   Volume& build(const Experimental::BlueprintOptions& options,
0082                 const GeometryContext& gctx,
0083                 const Logger& logger = Acts::getDummyLogger()) override;
0084 
0085   /// This participates in the construction of the geometry via the blueprint
0086   /// tree. The steps are approximately as follows:
0087   /// -# Register portals created for gap volumes, as they're not handled by
0088   ///    dedicated nodes
0089   /// -# Register gap volumes in the @p parent volume
0090   /// -# Create a configured @ref Acts::INavigationPolicy for the gap
0091   /// -# Call `finalize` on all children while passing through @p parent.
0092   ///
0093   /// @param options The global blueprint options
0094   /// @param gctx The geometry context (nominal usually)
0095   /// @param parent The parent volume
0096   /// @param logger The logger to use
0097   void finalize(const Experimental::BlueprintOptions& options,
0098                 const GeometryContext& gctx, TrackingVolume& parent,
0099                 const Logger& logger) override;
0100 
0101   /// Setter for the stacking direction
0102   /// @param direction The stacking direction
0103   /// @return This node for chaining
0104   ContainerBlueprintNode& setDirection(AxisDirection direction);
0105 
0106   /// Setter for the attachment strategy
0107   /// @param attachmentStrategy The attachment strategy
0108   /// @return This node for chaining
0109   ContainerBlueprintNode& setAttachmentStrategy(
0110       VolumeAttachmentStrategy attachmentStrategy);
0111 
0112   /// Setter for the resize strategy
0113   /// @param resizeStrategy The resize strategy
0114   /// @note @p resizeStrategy is used for both sides of the container
0115   /// @return This node for chaining
0116   ContainerBlueprintNode& setResizeStrategy(
0117       VolumeResizeStrategy resizeStrategy);
0118 
0119   /// Setter for the resize strategies
0120   /// @param inner The inner resize strategy
0121   /// @param outer The outer resize strategy
0122   /// @return This node for chaining
0123   ContainerBlueprintNode& setResizeStrategies(VolumeResizeStrategy inner,
0124                                               VolumeResizeStrategy outer);
0125 
0126   /// Accessor to the stacking direction
0127   /// @return The stacking direction
0128   AxisDirection direction() const;
0129 
0130   /// Accessor to the attachment strategy
0131   /// @return The attachment strategy
0132   VolumeAttachmentStrategy attachmentStrategy() const;
0133 
0134   /// Accessor to the resize strategy
0135   /// @return The resize strategy
0136   [[deprecated("Use resizeStrategies() instead")]]
0137   VolumeResizeStrategy resizeStrategy() const;
0138 
0139   /// Accessor to the resize strategies
0140   /// @return The resize strategies
0141   std::pair<VolumeResizeStrategy, VolumeResizeStrategy> resizeStrategies()
0142       const;
0143 
0144   /// @copydoc BlueprintNode::addToGraphviz
0145   void addToGraphviz(std::ostream& os) const override;
0146 
0147  protected:
0148   /// Make the volume stack for the container. This is called by the build
0149   /// method and is implemented by the derived classes.
0150   /// @param volumes The volumes to stack
0151   /// @param logger The logger to use
0152   /// @return The volume stack
0153   virtual std::unique_ptr<VolumeStack> makeStack(std::vector<Volume*>& volumes,
0154                                                  const Logger& logger) = 0;
0155 
0156   /// Get the type name of the container. This is used for the debug output
0157   /// of the container and encoding the volume shape in the dot graph.
0158   /// @return The type name
0159   virtual const std::string& typeName() const = 0;
0160 
0161   /// Collect shells from child nodes and gap volumes
0162   ///
0163   /// This function is responsible for collecting shells from child nodes and
0164   /// creating shells for gap volumes. It is used by the connect method to
0165   /// prepare the shells for the volume stack.
0166   ///
0167   /// The function processes each volume in m_childVolumes in two ways:
0168   /// 1. For gap volumes:
0169   ///    - Creates a TrackingVolume from the gap volume
0170   ///    - Assigns a unique name (ContainerName::GapN)
0171   ///    - Creates a single shell for the gap volume
0172   ///    - Stores both the shell and gap volume in m_gaps for later use
0173   ///
0174   /// 2. For child volumes:
0175   ///    - Looks up the corresponding child node in m_volumeToNode
0176   ///    - Calls connect() on the child node to get its shell
0177   ///    - Validates that the shell type matches the expected type
0178   ///    - Ensures the shell is valid
0179   ///
0180   /// The function maintains the order of volumes as they appear in
0181   /// m_childVolumes, which is important for the final stack shell construction.
0182   ///
0183   /// @tparam BaseShell The base shell type (e.g. CylinderPortalShell)
0184   /// @tparam SingleShell The single shell type (e.g. SingleCylinderPortalShell)
0185   /// @param options The blueprint options
0186   /// @param gctx The geometry context
0187   /// @param stack The volume stack
0188   /// @param prefix The prefix for debug output
0189   /// @param logger The logger to use
0190   /// @return A vector of shells in the same order as m_childVolumes
0191   template <typename BaseShell, typename SingleShell>
0192   std::vector<BaseShell*> collectChildShells(
0193       const Experimental::BlueprintOptions& options,
0194       const GeometryContext& gctx, VolumeStack& stack,
0195       const std::string& prefix, const Logger& logger);
0196 
0197   /// Implementation of the connect method for container nodes
0198   ///
0199   /// This method is responsible for:
0200   /// 1. Collecting shells from child nodes and gap volumes
0201   /// 2. Validating that the number of shells matches the number of child
0202   /// volumes
0203   /// 3. Ensuring all shells are valid
0204   /// 4. Creating a merged stack shell from all collected shells
0205   ///
0206   /// @tparam BaseShell The base shell type (e.g. CylinderPortalShell)
0207   /// @tparam SingleShell The single shell type (e.g. SingleCylinderPortalShell)
0208   /// @tparam ShellStack The stack shell type (e.g. StackCylinderPortalShell)
0209   /// @param options The blueprint options
0210   /// @param gctx The geometry context
0211   /// @param stack The volume stack
0212   /// @param prefix The prefix for debug output
0213   /// @param logger The logger to use
0214   /// @return The merged stack shell
0215   template <typename BaseShell, typename SingleShell, typename ShellStack>
0216   PortalShellBase& connectImpl(const Experimental::BlueprintOptions& options,
0217                                const GeometryContext& gctx, VolumeStack* stack,
0218                                const std::string& prefix, const Logger& logger);
0219 
0220   /// Name of the container node for debugging purposes
0221   std::string m_name;
0222   /// Stacking axis direction in local reference frame
0223   AxisDirection m_direction = AxisDirection::AxisZ;
0224   /// Volume attachment strategy for connecting volumes in the stack
0225   VolumeAttachmentStrategy m_attachmentStrategy{
0226       VolumeAttachmentStrategy::Midpoint};
0227 
0228   /// Resize strategies for inner and outer sides of the container
0229   std::pair<VolumeResizeStrategy, VolumeResizeStrategy> m_resizeStrategies{
0230       VolumeResizeStrategy::Expand, VolumeResizeStrategy::Expand};
0231 
0232   /// Container of child volumes managed by this blueprint node
0233   std::vector<Volume*> m_childVolumes;
0234   /// Volume stack instance created by derived classes during build phase
0235   /// @note This is populated during the build process by makeStack implementations
0236   std::unique_ptr<VolumeStack> m_stack{nullptr};
0237   /// Mapping from child volumes to their corresponding blueprint nodes
0238   std::map<const Volume*, BlueprintNode*> m_volumeToNode;
0239 
0240   /// Portal shell representation of this container for geometry connection
0241   std::unique_ptr<PortalShellBase> m_shell{nullptr};
0242   /// Container of gap volumes and their portal shells created between child
0243   /// volumes
0244   std::vector<std::pair<std::unique_ptr<PortalShellBase>,
0245                         std::unique_ptr<TrackingVolume>>>
0246       m_gaps;
0247 };
0248 
0249 class CylinderContainerBlueprintNode final : public ContainerBlueprintNode {
0250  public:
0251   using ContainerBlueprintNode::ContainerBlueprintNode;
0252 
0253   /// This participates in the construction of the geometry via the blueprint
0254   /// tree. The steps are approximately as follows:
0255   /// -# Walk through all volumes that were created by the build phase
0256   /// -# Check if they are: *real* child volumes or gap volumes
0257   ///   - If gap volume: produce a @ref Acts::TrackingVolume, and wrap it in a single use shell
0258   ///   - If child volume: locate the right child node it came from, call
0259   ///   ` connect` and collect the returned shell
0260   /// -# Produce a combined StackPortalShell (cuboid or cylinder) from all the
0261   /// shells
0262   /// -# Return that shell representation
0263   ///
0264   /// @param options The global blueprint options
0265   /// @param gctx The geometry context (nominal usually)
0266   /// @param logger The logger to use
0267   /// @return The combined StackPortalShell (cuboid or cylinder)
0268   PortalShellBase& connect(
0269       const Experimental::BlueprintOptions& options,
0270       const GeometryContext& gctx,
0271       const Logger& logger = Acts::getDummyLogger()) override;
0272 
0273   std::unique_ptr<VolumeStack> makeStack(std::vector<Volume*>& volumes,
0274                                          const Logger& logger) override;
0275 
0276  protected:
0277   inline static const std::string s_typeName = "Cylinder";
0278   const std::string& typeName() const override;
0279 };
0280 
0281 class CuboidContainerBlueprintNode final : public ContainerBlueprintNode {
0282  public:
0283   using ContainerBlueprintNode::ContainerBlueprintNode;
0284 
0285   /// This participates in the construction of the geometry via the blueprint
0286   /// tree. The steps are approximately as follows:
0287   /// -# Walk through all volumes that were created by the build phase
0288   /// -# Check if they are: *real* child volumes or gap volumes
0289   ///   - If gap volume: produce a @ref Acts::TrackingVolume, and wrap it in a single use shell
0290   ///   - If child volume: locate the right child node it came from, call
0291   ///   ` connect` and collect the returned shell
0292   /// -# Produce a combined StackPortalShell (cuboid or cylinder) from all the
0293   /// shells
0294   /// -# Return that shell representation
0295   ///
0296   /// @param options The global blueprint options
0297   /// @param gctx The geometry context (nominal usually)
0298   /// @param logger The logger to use
0299   /// @return The combined StackPortalShell (cuboid or cylinder)
0300   PortalShellBase& connect(
0301       const Experimental::BlueprintOptions& options,
0302       const GeometryContext& gctx,
0303       const Logger& logger = Acts::getDummyLogger()) override;
0304 
0305   std::unique_ptr<VolumeStack> makeStack(std::vector<Volume*>& volumes,
0306                                          const Logger& logger) override;
0307 
0308  protected:
0309   inline static const std::string s_typeName = "Cuboid";
0310   const std::string& typeName() const override;
0311 };
0312 
0313 }  // namespace Acts::Experimental