Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-06 07:51:52

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/Definitions/Algebra.hpp"
0012 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0013 #include "Acts/Surfaces/PlanarBounds.hpp"
0014 #include "Acts/Surfaces/RectangleBounds.hpp"
0015 #include "Acts/Surfaces/SurfaceBounds.hpp"
0016 
0017 #include <array>
0018 #include <cstddef>
0019 #include <iosfwd>
0020 #include <vector>
0021 
0022 #include <boost/container/small_vector.hpp>
0023 
0024 namespace Acts {
0025 
0026 /// @brief base class for convex polygon bounds
0027 ///
0028 /// This class serves as a base class for the actual bounds class.
0029 /// The only deriving type is the templated `ConvexPolygonBounds`.
0030 ///
0031 class ConvexPolygonBoundsBase : public PlanarBounds {
0032  public:
0033   /// Output Method for std::ostream
0034   /// @param sl is the ostream to be written into
0035   std::ostream& toStream(std::ostream& sl) const final;
0036 
0037   /// Return the bound values as dynamically sized vector
0038   ///
0039   /// @return this returns a copy of the internal values
0040   std::vector<double> values() const final;
0041 
0042  protected:
0043   /// Return a rectangle bounds instance that encloses a set of vertices.
0044   /// @param vertices A collection of vertices to enclose.
0045   /// @return Enclosing rectangle.
0046   template <typename coll_t>
0047   static RectangleBounds makeBoundingBox(const coll_t& vertices);
0048 
0049   /// Calculates whether a set of vertices forms a convex polygon. This is
0050   /// generic over the number of vertices, so it's factored out of the concrete
0051   /// classes and into this base class.
0052   /// @param vertices A collection of vertices.
0053   /// throws a logic error if this is not the case
0054   template <typename coll_t>
0055     requires std::same_as<typename coll_t::value_type, Acts::Vector2>
0056   static void convex_impl(const coll_t& vertices) noexcept(false);
0057 };
0058 
0059 /// This is the actual implementation of the bounds.
0060 /// It is templated on the number of vertices, but there is a specialization for
0061 /// *dynamic* number of vertices, where the underlying storage is then a vector.
0062 ///
0063 /// @tparam N Number of vertices
0064 template <int N>
0065 class ConvexPolygonBounds : public ConvexPolygonBoundsBase {
0066  public:
0067   /// Expose number of vertices given as template parameter.
0068   ///
0069   static constexpr std::size_t num_vertices = N;
0070   /// Type that's used to store the vertices, in this case a fixed size array.
0071   ///
0072   using vertex_array = std::array<Vector2, num_vertices>;
0073   /// Expose number of parameters as a template parameter
0074   ///
0075   static constexpr std::size_t eSize = 2 * N;
0076   /// Type that's used to store the vertices, in this case a fixed size array.
0077   ///
0078   using value_array = std::array<double, eSize>;
0079 
0080   static_assert(N >= 3, "ConvexPolygonBounds needs at least 3 sides.");
0081 
0082   /// Constructor from a vector of vertices, to facilitate construction.
0083   /// This will throw if the vector size does not match `num_vertices`.
0084   /// This will throw if the vertices do not form a convex polygon.
0085   /// @param vertices The list of vertices.
0086   explicit ConvexPolygonBounds(const std::vector<Vector2>& vertices) noexcept(
0087       false);
0088 
0089   /// Constructor from a fixed size array of vertices.
0090   /// This will throw if the vertices do not form a convex polygon.
0091   /// @param vertices The vertices
0092   explicit ConvexPolygonBounds(const vertex_array& vertices) noexcept(false);
0093 
0094   /// Constructor from a fixed size array of parameters
0095   /// This will throw if the vertices do not form a convex polygon.
0096   /// @param values The values to build up the vertices
0097   explicit ConvexPolygonBounds(const value_array& values) noexcept(false);
0098 
0099   BoundsType type() const final { return SurfaceBounds::eConvexPolygon; }
0100 
0101   /// Return whether a local 2D point lies inside of the bounds defined by this
0102   /// object.
0103   /// @param lposition The local position to check
0104   /// @param boundaryTolerance The `BoundaryTolerance` object handling tolerances.
0105   /// @return Whether the points is inside
0106   bool inside(const Vector2& lposition,
0107               const BoundaryTolerance& boundaryTolerance) const final;
0108 
0109   /// Return the vertices
0110   ///
0111   /// @param ignoredSegments the number of segments used to approximate
0112   /// and eventually curved line
0113   ///
0114   /// @note the number of segments is ignored in this representation
0115   ///
0116   /// @return vector for vertices in 2D
0117   std::vector<Vector2> vertices(unsigned int ignoredSegments = 0u) const final;
0118 
0119   /// Return a rectangle bounds object that encloses this polygon.
0120   /// @return The rectangular bounds
0121   const RectangleBounds& boundingBox() const final;
0122 
0123  private:
0124   vertex_array m_vertices;
0125   RectangleBounds m_boundingBox;
0126 
0127   /// Return whether this bounds class is in fact convex
0128   /// throws a log error if not
0129   void checkConsistency() const noexcept(false);
0130 };
0131 
0132 /// Tag to trigger specialization of a dynamic polygon
0133 constexpr int PolygonDynamic = -1;
0134 
0135 /// This is the specialization handling a polygon with a dynamic number of
0136 /// points. It can accept any number of points.
0137 ///
0138 template <>
0139 class ConvexPolygonBounds<PolygonDynamic> : public ConvexPolygonBoundsBase {
0140  public:
0141   constexpr static int eSize = -1;
0142 
0143   /// Constructor from a vector of vertices, to facilitate construction.
0144   /// This will throw if the vertices do not form a convex polygon.
0145   /// @param vertices The list of vertices.
0146   explicit ConvexPolygonBounds(const std::vector<Vector2>& vertices);
0147 
0148   /// Return the bounds type of this bounds object.
0149   /// @return The bounds type
0150   BoundsType type() const final { return SurfaceBounds::eConvexPolygon; }
0151 
0152   /// Return whether a local 2D point lies inside of the bounds defined by this
0153   /// object.
0154   /// @param lposition The local position to check
0155   /// @param boundaryTolerance The `BoundaryTolerance` object handling tolerances.
0156   /// @return Whether the points is inside
0157   bool inside(const Vector2& lposition,
0158               const BoundaryTolerance& boundaryTolerance) const final;
0159 
0160   /// Return the vertices
0161   ///
0162   /// @param lseg the number of segments used to approximate
0163   /// and eventually curved line
0164   ///
0165   /// @note the number of segments is ignored in this representation
0166   ///
0167   /// @return vector for vertices in 2D
0168   std::vector<Vector2> vertices(unsigned int lseg = 1) const final;
0169 
0170   ///
0171   /// Return a rectangle bounds object that encloses this polygon.
0172   /// @return The rectangular bounds
0173   ///
0174   const RectangleBounds& boundingBox() const final;
0175 
0176  private:
0177   boost::container::small_vector<Vector2, 10> m_vertices;
0178   RectangleBounds m_boundingBox;
0179 
0180   /// Return whether this bounds class is in fact convex
0181   /// thorws a logic error if not
0182   void checkConsistency() const noexcept(false);
0183 };
0184 
0185 }  // namespace Acts
0186 
0187 #include "Acts/Surfaces/ConvexPolygonBounds.ipp"