File indexing completed on 2025-08-06 08:10:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/Utilities/ThrowAssert.hpp"
0010
0011 template <typename coll_t>
0012 void Acts::ConvexPolygonBoundsBase::convex_impl(
0013 const coll_t& vertices) noexcept(false) {
0014 static_assert(std::is_same<typename coll_t::value_type, Vector2>::value,
0015 "Must be collection of Vector2");
0016
0017 const std::size_t N = vertices.size();
0018 for (std::size_t i = 0; i < N; i++) {
0019 std::size_t j = (i + 1) % N;
0020 const Vector2& a = vertices[i];
0021 const Vector2& b = vertices[j];
0022
0023 const Vector2 ab = b - a;
0024 const Vector2 normal = Vector2(ab.y(), -ab.x()).normalized();
0025
0026 bool first = true;
0027 bool ref = false;
0028
0029 for (std::size_t k = 0; k < N; k++) {
0030 if (k == i || k == j) {
0031 continue;
0032 }
0033
0034 const Vector2& c = vertices[k];
0035 double dot = normal.dot(c - a);
0036
0037 if (first) {
0038 ref = std::signbit(dot);
0039 first = false;
0040 continue;
0041 }
0042
0043 if (std::signbit(dot) != ref) {
0044 throw std::logic_error(
0045 "ConvexPolygon: Given vertices do not form convex hull");
0046 }
0047 }
0048 }
0049 }
0050
0051 template <typename coll_t>
0052 Acts::RectangleBounds Acts::ConvexPolygonBoundsBase::makeBoundingBox(
0053 const coll_t& vertices) {
0054 Vector2 vmax, vmin;
0055 vmax = vertices[0];
0056 vmin = vertices[0];
0057
0058 for (std::size_t i = 1; i < vertices.size(); i++) {
0059 vmax = vmax.cwiseMax(vertices[i]);
0060 vmin = vmin.cwiseMin(vertices[i]);
0061 }
0062
0063 return {vmin, vmax};
0064 }
0065
0066 template <int N>
0067 Acts::ConvexPolygonBounds<N>::ConvexPolygonBounds(
0068 const std::vector<Acts::Vector2>& vertices) noexcept(false)
0069 : m_vertices(), m_boundingBox(makeBoundingBox(vertices)) {
0070 throw_assert(vertices.size() == N,
0071 "Size and number of given vertices do not match.");
0072 for (std::size_t i = 0; i < N; i++) {
0073 m_vertices[i] = vertices[i];
0074 }
0075 checkConsistency();
0076 }
0077
0078 template <int N>
0079 Acts::ConvexPolygonBounds<N>::ConvexPolygonBounds(
0080 const vertex_array& vertices) noexcept(false)
0081 : m_vertices(vertices), m_boundingBox(makeBoundingBox(vertices)) {
0082 checkConsistency();
0083 }
0084
0085 template <int N>
0086 Acts::ConvexPolygonBounds<N>::ConvexPolygonBounds(
0087 const value_array& values) noexcept(false)
0088 : m_vertices(), m_boundingBox(0., 0.) {
0089 for (std::size_t i = 0; i < N; i++) {
0090 m_vertices[i] = Vector2(values[2 * i], values[2 * i + 1]);
0091 }
0092 makeBoundingBox(m_vertices);
0093 checkConsistency();
0094 }
0095
0096 template <int N>
0097 Acts::SurfaceBounds::BoundsType Acts::ConvexPolygonBounds<N>::type() const {
0098 return SurfaceBounds::eConvexPolygon;
0099 }
0100
0101 template <int N>
0102 bool Acts::ConvexPolygonBounds<N>::inside(
0103 const Acts::Vector2& lposition, const Acts::BoundaryCheck& bcheck) const {
0104 return bcheck.isInside(lposition, m_vertices);
0105 }
0106
0107 template <int N>
0108 std::vector<Acts::Vector2> Acts::ConvexPolygonBounds<N>::vertices(
0109 unsigned int ) const {
0110 return {m_vertices.begin(), m_vertices.end()};
0111 }
0112
0113 template <int N>
0114 const Acts::RectangleBounds& Acts::ConvexPolygonBounds<N>::boundingBox() const {
0115 return m_boundingBox;
0116 }
0117
0118 template <int N>
0119 void Acts::ConvexPolygonBounds<N>::checkConsistency() const noexcept(false) {
0120 convex_impl(m_vertices);
0121 }