Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:22:18

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/Utilities/Frustum.hpp"
0012 #include "Acts/Utilities/Ray.hpp"
0013 #include "Acts/Visualization/IVisualization3D.hpp"
0014 
0015 #include <functional>
0016 #include <memory>
0017 #include <utility>
0018 #include <vector>
0019 
0020 namespace Acts {
0021 
0022 /// Implementation of an Axis Aligned Bounding Box. This type is compatible
0023 /// with 2D and 3D boxes
0024 template <typename entity_t, typename value_t, std::size_t DIM>
0025 class AxisAlignedBoundingBox {
0026  private:
0027   /// Private self type to capture template parameters
0028   using self_t = AxisAlignedBoundingBox<entity_t, value_t, DIM>;
0029 
0030   /// Strong type helper, not public
0031   /// This is only used to provide sensible tag-dispatch below.
0032   template <typename T, typename P>
0033   class NamedType {
0034    public:
0035     explicit NamedType(const T& value) : m_value(value) {}
0036     explicit NamedType(T&& value) : m_value(std::move(value)) {}
0037     T& get() { return m_value; }
0038     const T& get() const { return m_value; }
0039 
0040    private:
0041     T m_value;
0042   };
0043 
0044   /// SizeParameter Tag
0045   struct SizeParameter {};
0046 
0047  public:
0048   /// The value type used by this class
0049   using value_type = value_t;
0050 
0051   /// Re-export vertex type based on value type given
0052   using VertexType = Eigen::Matrix<value_t, DIM, 1>;
0053 
0054   /// Associated array value to `VertexType`
0055   using vertex_array_type = Eigen::Array<value_t, DIM, 1>;
0056 
0057   /// Type of stored entity
0058   using entity_type = entity_t;
0059 
0060   /// The transform type based on the `value_type`
0061   using transform_type = Eigen::Transform<value_type, DIM, Eigen::Affine>;
0062 
0063   /// Strong type to select the correct constructor
0064   using Size = NamedType<VertexType, struct SizeParameter>;
0065 
0066   /// Re-export dimension from template parameter
0067   static const std::size_t dim = DIM;
0068 
0069   /// Copy constructor from other bounding box.
0070   /// @param other The other bounding box to copy from
0071   AxisAlignedBoundingBox(const self_t& other) = default;
0072 
0073   /// Copy assignment operator from other bounding box.
0074   /// @param other The other AABB
0075   /// @return Reference to this bounding box after copying
0076   AxisAlignedBoundingBox& operator=(const self_t& other) = default;
0077 
0078   /// Constructor from an entity pointer, and the min and max vertices.
0079   /// @param entity The entity to store
0080   /// @param vmin The minimum vertex.
0081   /// @param vmax The maximum vertex.
0082   AxisAlignedBoundingBox(const entity_t* entity, const VertexType& vmin,
0083                          const VertexType& vmax);
0084 
0085   /// Constructor from a center position, and a width and height.
0086   /// @param entity The entity to store
0087   /// @param center The center position
0088   /// @param size The size (width and height) of the box.
0089   /// @note The special type @c size is required to disambiguate this constructor
0090   /// from the other one above. It is a wrapper around a simple @c Vector3.
0091   AxisAlignedBoundingBox(const entity_t* entity, const VertexType& center,
0092                          const Size& size);
0093 
0094   /// Constructor from a list of child boxes. This box will wrap around all
0095   /// boxes
0096   /// contained in @p boxes, and additional envelope can be given.
0097   /// @param boxes Vector of child boxes to store in this bounding box.
0098   /// @param envelope Envelope that will be added/subtracted to the dimension.
0099   explicit AxisAlignedBoundingBox(
0100       const std::vector<self_t*>& boxes,
0101       vertex_array_type envelope = vertex_array_type::Zero());
0102 
0103   /// Helper function to calculate the size of a bounding box enclosing @p boxes.
0104   /// @param boxes The boxes to wrap (const pointers)
0105   /// @param envelope Optional envelop to add/subtract to dimension.
0106   /// @return Pair of vertices: min and max.
0107   static std::pair<VertexType, VertexType> wrap(
0108       const std::vector<const self_t*>& boxes,
0109       vertex_array_type envelope = vertex_array_type::Zero());
0110 
0111   /// Helper function to calculate the size of a bounding box enclosing @p boxes.
0112   /// Overload which accepts non-const boxes in @p boxes.
0113   /// @param boxes The boxes to wrap (non-const pointers)
0114   /// @param envelope Optional envelop to add/subtract to dimension.
0115   /// @return Pair of vertices: min and max.
0116   static std::pair<VertexType, VertexType> wrap(
0117       const std::vector<self_t*>& boxes,
0118       vertex_array_type envelope = vertex_array_type::Zero());
0119 
0120   /// Helper function to calculate the size of a bounding box enclosing @p boxes.
0121   /// Overload which accepts a vector in @p boxes which owns the instances
0122   /// @param boxes The boxes to wrap (by-value vector)
0123   /// @param envelope Optional envelop to add/subtract to dimension.
0124   /// @return Pair of vertices: min and max.
0125   static std::pair<VertexType, VertexType> wrap(
0126       const std::vector<self_t>& boxes,
0127       vertex_array_type envelope = vertex_array_type::Zero());
0128 
0129   /// Calculate whether a point is inside this box.
0130   /// @param point The point to test.
0131   /// @return Whether the point is inside or not.
0132   bool intersect(const VertexType& point) const;
0133 
0134   /// @brief Implements the slab method for Ray/AABB intersections.
0135   ///
0136   /// @sa https://tavianator.com/2011/ray_box.html
0137   /// @sa https://tavianator.com/2015/ray_box_nan.html
0138   /// @sa https://medium.com/@bromanz/another-view-on-the-classic-ray-aabb-intersection-algorithm-for-bvh-traversal-41125138b525
0139   /// @sa The original algorithm is described in "Graphics Gems (1990)" @cite GraphicsGems1990
0140   ///
0141   /// @note This implementation may treat parallel rays on any of the slabs
0142   ///       as **outside** due to how @c NaNs are handled by Eigen.
0143   ///       See [this
0144   ///       issue](https://eigen.tuxfamily.org/bz/show_bug.cgi?id=564).
0145   /// @param ray The ray to intersect with
0146   /// @return Whether the ray intersects this AABB
0147   bool intersect(const Ray<value_type, DIM>& ray) const;
0148 
0149   /// Check if a frustum intersects with this bounding box.
0150   ///
0151   /// This method implements an algorithm similar to the one described in
0152   /// "Optimized View Frustum Culling Algorithms for Bounding Boxes (2012)" [2]
0153   /// (https://doi.org/10.1080/10867651.2000.10487517), but drops some of the
0154   /// more sophisticated optimization.
0155   ///
0156   /// @param fr The frustum
0157   /// @return Whether the frustum intersects this AABB
0158   template <std::size_t sides>
0159   bool intersect(const Frustum<value_type, DIM, sides>& fr) const;
0160 
0161   /// Set the skip node (bounding box)
0162   /// @param skip The target skip node pointer
0163   void setSkip(self_t* skip);
0164 
0165   /// Get the skip node for this box
0166   /// @return The skip node pointer
0167   const self_t* getSkip() const;
0168 
0169   /// Get the left child (i.e. the first of the children that are inside this
0170   /// bounding box).
0171   /// @return The lest most child.
0172   const self_t* getLeftChild() const;
0173 
0174   /// Check whether this node as an associated entity. If it does not have one,
0175   /// this is a purely abstract container box.
0176   /// @return Whether the box has an entity attached.
0177   bool hasEntity() const;
0178 
0179   /// Return the entity associated with this box. This might be nullptr if there
0180   /// is no entity attached.
0181   /// @return The entity pointer, might be nullptr
0182   const entity_t* entity() const;
0183 
0184   /// Set the entity associated with with this box.
0185   /// @param entity The entity
0186   void setEntity(const entity_t* entity);
0187 
0188   /// Get the center position of this bounding box.
0189   /// @return The center position
0190   const VertexType& center() const;
0191 
0192   /// Get the minimum vertex
0193   /// @return The minimum vertex
0194   const VertexType& min() const;
0195 
0196   /// Get the maximum vertex
0197   /// @return The maximum vertex
0198   const VertexType& max() const;
0199 
0200   /// Write information about this bounding box to a stream.
0201   /// @param os The output stream.
0202   /// @return The stream given as an argument.
0203   std::ostream& toStream(std::ostream& os) const;
0204 
0205   /// Transforms this bounding box using the given transform. This method
0206   /// modifies the box it is called on.
0207   /// @param trf The transform
0208   void transform(const transform_type& trf);
0209 
0210   /// Transforms this bounding box using the given transform. This method
0211   /// returns a copy of this box, with the transformation applied, and leaves
0212   /// this instance unchanged.
0213   /// @param trf The transform
0214   /// @return The transformed bounding box
0215   self_t transformed(const transform_type& trf) const;
0216 
0217   /// Draw this bounding box using the given visualization helper. This method
0218   /// is only available for the 3D case.
0219   /// @param helper The visualization helper to write to
0220   /// @param color The color to use for drawing
0221   /// @param trf An optional transform to apply first.
0222   void draw(IVisualization3D& helper, Color color = {120, 120, 120},
0223             const transform_type& trf = transform_type::Identity()) const
0224     requires(DIM == 3);
0225 
0226   /// Draw this bounding box as SVG. This method is only available for the 2D
0227   /// case.
0228   /// @param os The output stream to write to
0229   /// @param w The width of the output SVG.
0230   /// @param h The height of the output SVG.
0231   /// @param unit A scale factor to apply before drawing
0232   /// @param label A label to put next to the box.
0233   /// @param fillcolor Color to fill the box with.
0234   /// @return The outstream given in @p os.
0235   std::ostream& svg(std::ostream& os, value_type w, value_type h,
0236                     value_type unit = 10, const std::string& label = "",
0237                     const std::string& fillcolor = "grey") const
0238     requires(DIM == 2);
0239 
0240  private:
0241   std::pair<VertexType, VertexType> transformVertices(
0242       const transform_type& trf) const
0243     requires(DIM == 2);
0244 
0245   std::pair<VertexType, VertexType> transformVertices(
0246       const transform_type& trf) const
0247     requires(DIM == 3);
0248 
0249   const entity_t* m_entity;
0250   VertexType m_vmin;
0251   VertexType m_vmax;
0252   VertexType m_center;
0253   vertex_array_type m_width;
0254   vertex_array_type m_iwidth;
0255 
0256   self_t* m_leftChild{nullptr};
0257   self_t* m_rightChild{nullptr};
0258   self_t* m_skip{nullptr};
0259 };
0260 
0261 namespace BoundingBoxHierarchy {
0262 
0263 /// Visit all entities whose bounding boxes intersect an object.
0264 ///
0265 /// The hierarchy is traversed without allocating an intermediate container.
0266 /// The visitor is called once for every intersecting leaf entity.
0267 ///
0268 /// @tparam object_t Type of object to intersect with the bounding boxes.
0269 /// @tparam box_t Bounding box type.
0270 /// @tparam visitor_t Callable accepting a const reference to an entity.
0271 /// @param object Object to intersect with the hierarchy.
0272 /// @param root Root node of the bounding box hierarchy. May be `nullptr`.
0273 /// @param visitor Callable invoked for each intersecting entity.
0274 template <typename object_t, typename box_t, typename visitor_t>
0275 void visitIntersecting(const object_t& object, const box_t* root,
0276                        visitor_t&& visitor);
0277 
0278 /// Build an octree from a list of bounding boxes.
0279 /// @note @p store and @p prims do not need to contain the same objects. @p store
0280 /// is only used to pass ownership back to the caller while preserving memory
0281 /// location.
0282 /// @tparam box_t Works will all box types.
0283 /// @param store Owns the created boxes by means of `std::unique_ptr`.
0284 /// @param prims Boxes to store. This is a read only vector.
0285 /// @param maxDepth No subdivisions beyond this level.
0286 /// @param envelope Envelope to add/subtract to dimensions in all directions.
0287 /// @return Pointer to the top most bounding box, containing the entire octree
0288 template <typename box_t>
0289 box_t* makeOctree(std::vector<std::unique_ptr<box_t>>& store,
0290                   const std::vector<box_t*>& prims, std::size_t maxDepth = 1,
0291                   typename box_t::value_type envelope = 0);
0292 
0293 }  // namespace BoundingBoxHierarchy
0294 
0295 /// Build an octree from a list of bounding boxes.
0296 /// @tparam box_t Works with all box types.
0297 /// @param store Owns the created boxes by means of `std::unique_ptr`.
0298 /// @param prims Boxes to store. This is a read-only vector.
0299 /// @param maxDepth No subdivisions beyond this level.
0300 /// @param envelope Envelope to add/subtract to dimensions in all directions.
0301 /// @return Pointer to the topmost bounding box, containing the entire octree.
0302 /// @deprecated Use Acts::BoundingBoxHierarchy::makeOctree instead.
0303 template <typename box_t>
0304 [[deprecated("Use BoundingBoxHierarchy::makeOctree instead")]] box_t*
0305 make_octree(std::vector<std::unique_ptr<box_t>>& store,
0306             const std::vector<box_t*>& prims, std::size_t maxDepth = 1,
0307             typename box_t::value_type envelope = 0);
0308 
0309 /// Overload of the << operator for bounding boxes.
0310 /// @tparam T entity type
0311 /// @tparam U value type
0312 /// @tparam V dimension
0313 /// @param os The output stream
0314 /// @param box The bounding box
0315 /// @return The given output stream.
0316 template <typename T, typename U, std::size_t V>
0317 std::ostream& operator<<(std::ostream& os,
0318                          const AxisAlignedBoundingBox<T, U, V>& box);
0319 
0320 }  // namespace Acts
0321 
0322 #include "Acts/Utilities/BoundingBox.ipp"