Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:22:22

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 strategies
0135   /// @return The resize strategies
0136   std::pair<VolumeResizeStrategy, VolumeResizeStrategy> resizeStrategies()
0137       const;
0138 
0139   /// @copydoc BlueprintNode::addToGraphviz
0140   void addToGraphviz(std::ostream& os) const override;
0141 
0142  protected:
0143   /// Make the volume stack for the container. This is called by the build
0144   /// method and is implemented by the derived classes.
0145   /// @param volumes The volumes to stack
0146   /// @param logger The logger to use
0147   /// @return The volume stack
0148   virtual std::unique_ptr<VolumeStack> makeStack(std::vector<Volume*>& volumes,
0149                                                  const Logger& logger) = 0;
0150 
0151   /// Get the type name of the container. This is used for the debug output
0152   /// of the container and encoding the volume shape in the dot graph.
0153   /// @return The type name
0154   virtual const std::string& typeName() const = 0;
0155 
0156   /// Collect shells from child nodes and gap volumes
0157   ///
0158   /// This function is responsible for collecting shells from child nodes and
0159   /// creating shells for gap volumes. It is used by the connect method to
0160   /// prepare the shells for the volume stack.
0161   ///
0162   /// The function processes each volume in m_childVolumes in two ways:
0163   /// 1. For gap volumes:
0164   ///    - Creates a TrackingVolume from the gap volume
0165   ///    - Assigns a unique name (ContainerName::GapN)
0166   ///    - Creates a single shell for the gap volume
0167   ///    - Stores both the shell and gap volume in m_gaps for later use
0168   ///
0169   /// 2. For child volumes:
0170   ///    - Looks up the corresponding child node in m_volumeToNode
0171   ///    - Calls connect() on the child node to get its shell
0172   ///    - Validates that the shell type matches the expected type
0173   ///    - Ensures the shell is valid
0174   ///
0175   /// The function maintains the order of volumes as they appear in
0176   /// m_childVolumes, which is important for the final stack shell construction.
0177   ///
0178   /// @tparam BaseShell The base shell type (e.g. CylinderPortalShell)
0179   /// @tparam SingleShell The single shell type (e.g. SingleCylinderPortalShell)
0180   /// @param options The blueprint options
0181   /// @param gctx The geometry context
0182   /// @param stack The volume stack
0183   /// @param prefix The prefix for debug output
0184   /// @param logger The logger to use
0185   /// @return A vector of shells in the same order as m_childVolumes
0186   template <typename BaseShell, typename SingleShell>
0187   std::vector<BaseShell*> collectChildShells(
0188       const Experimental::BlueprintOptions& options,
0189       const GeometryContext& gctx, VolumeStack& stack,
0190       const std::string& prefix, const Logger& logger);
0191 
0192   /// Implementation of the connect method for container nodes
0193   ///
0194   /// This method is responsible for:
0195   /// 1. Collecting shells from child nodes and gap volumes
0196   /// 2. Validating that the number of shells matches the number of child
0197   /// volumes
0198   /// 3. Ensuring all shells are valid
0199   /// 4. Creating a merged stack shell from all collected shells
0200   ///
0201   /// @tparam BaseShell The base shell type (e.g. CylinderPortalShell)
0202   /// @tparam SingleShell The single shell type (e.g. SingleCylinderPortalShell)
0203   /// @tparam ShellStack The stack shell type (e.g. StackCylinderPortalShell)
0204   /// @param options The blueprint options
0205   /// @param gctx The geometry context
0206   /// @param stack The volume stack
0207   /// @param prefix The prefix for debug output
0208   /// @param logger The logger to use
0209   /// @return The merged stack shell
0210   template <typename BaseShell, typename SingleShell, typename ShellStack>
0211   PortalShellBase& connectImpl(const Experimental::BlueprintOptions& options,
0212                                const GeometryContext& gctx, VolumeStack* stack,
0213                                const std::string& prefix, const Logger& logger);
0214 
0215   /// Name of the container node for debugging purposes
0216   std::string m_name;
0217   /// Stacking axis direction in local reference frame
0218   AxisDirection m_direction = AxisDirection::AxisZ;
0219   /// Volume attachment strategy for connecting volumes in the stack
0220   VolumeAttachmentStrategy m_attachmentStrategy{
0221       VolumeAttachmentStrategy::Midpoint};
0222 
0223   /// Resize strategies for inner and outer sides of the container
0224   std::pair<VolumeResizeStrategy, VolumeResizeStrategy> m_resizeStrategies{
0225       VolumeResizeStrategy::Expand, VolumeResizeStrategy::Expand};
0226 
0227   /// Container of child volumes managed by this blueprint node
0228   std::vector<Volume*> m_childVolumes;
0229   /// Volume stack instance created by derived classes during build phase
0230   /// @note This is populated during the build process by makeStack implementations
0231   std::unique_ptr<VolumeStack> m_stack{nullptr};
0232   /// Mapping from child volumes to their corresponding blueprint nodes
0233   std::map<const Volume*, BlueprintNode*> m_volumeToNode;
0234 
0235   /// Portal shell representation of this container for geometry connection
0236   std::unique_ptr<PortalShellBase> m_shell{nullptr};
0237   /// Container of gap volumes and their portal shells created between child
0238   /// volumes
0239   std::vector<std::pair<std::unique_ptr<PortalShellBase>,
0240                         std::unique_ptr<TrackingVolume>>>
0241       m_gaps;
0242 };
0243 
0244 class CylinderContainerBlueprintNode final : public ContainerBlueprintNode {
0245  public:
0246   using ContainerBlueprintNode::ContainerBlueprintNode;
0247 
0248   /// This participates in the construction of the geometry via the blueprint
0249   /// tree. The steps are approximately as follows:
0250   /// -# Walk through all volumes that were created by the build phase
0251   /// -# Check if they are: *real* child volumes or gap volumes
0252   ///   - If gap volume: produce a @ref Acts::TrackingVolume, and wrap it in a single use shell
0253   ///   - If child volume: locate the right child node it came from, call
0254   ///   ` connect` and collect the returned shell
0255   /// -# Produce a combined StackPortalShell (cuboid or cylinder) from all the
0256   /// shells
0257   /// -# Return that shell representation
0258   ///
0259   /// @param options The global blueprint options
0260   /// @param gctx The geometry context (nominal usually)
0261   /// @param logger The logger to use
0262   /// @return The combined StackPortalShell (cuboid or cylinder)
0263   PortalShellBase& connect(
0264       const Experimental::BlueprintOptions& options,
0265       const GeometryContext& gctx,
0266       const Logger& logger = Acts::getDummyLogger()) override;
0267 
0268   std::unique_ptr<VolumeStack> makeStack(std::vector<Volume*>& volumes,
0269                                          const Logger& logger) override;
0270 
0271  protected:
0272   inline static const std::string s_typeName = "Cylinder";
0273   const std::string& typeName() const override;
0274 };
0275 
0276 class CuboidContainerBlueprintNode final : public ContainerBlueprintNode {
0277  public:
0278   using ContainerBlueprintNode::ContainerBlueprintNode;
0279 
0280   /// This participates in the construction of the geometry via the blueprint
0281   /// tree. The steps are approximately as follows:
0282   /// -# Walk through all volumes that were created by the build phase
0283   /// -# Check if they are: *real* child volumes or gap volumes
0284   ///   - If gap volume: produce a @ref Acts::TrackingVolume, and wrap it in a single use shell
0285   ///   - If child volume: locate the right child node it came from, call
0286   ///   ` connect` and collect the returned shell
0287   /// -# Produce a combined StackPortalShell (cuboid or cylinder) from all the
0288   /// shells
0289   /// -# Return that shell representation
0290   ///
0291   /// @param options The global blueprint options
0292   /// @param gctx The geometry context (nominal usually)
0293   /// @param logger The logger to use
0294   /// @return The combined StackPortalShell (cuboid or cylinder)
0295   PortalShellBase& connect(
0296       const Experimental::BlueprintOptions& options,
0297       const GeometryContext& gctx,
0298       const Logger& logger = Acts::getDummyLogger()) override;
0299 
0300   std::unique_ptr<VolumeStack> makeStack(std::vector<Volume*>& volumes,
0301                                          const Logger& logger) override;
0302 
0303  protected:
0304   inline static const std::string s_typeName = "Cuboid";
0305   const std::string& typeName() const override;
0306 };
0307 
0308 }  // namespace Acts::Experimental