File indexing completed on 2025-07-06 07:51:52
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Surfaces/ConvexPolygonBounds.hpp"
0012
0013 #include "Acts/Surfaces/detail/BoundaryCheckHelper.hpp"
0014 #include "Acts/Utilities/ThrowAssert.hpp"
0015
0016 template <typename coll_t>
0017 requires std::same_as<typename coll_t::value_type, Acts::Vector2>
0018 void Acts::ConvexPolygonBoundsBase::convex_impl(
0019 const coll_t& vertices) noexcept(false) {
0020 const std::size_t N = vertices.size();
0021 for (std::size_t i = 0; i < N; i++) {
0022 std::size_t j = (i + 1) % N;
0023 const Vector2& a = vertices[i];
0024 const Vector2& b = vertices[j];
0025
0026 const Vector2 ab = b - a;
0027 const Vector2 normal = Vector2(ab.y(), -ab.x()).normalized();
0028
0029 bool first = true;
0030 bool ref = false;
0031
0032 for (std::size_t k = 0; k < N; k++) {
0033 if (k == i || k == j) {
0034 continue;
0035 }
0036
0037 const Vector2& c = vertices[k];
0038 double dot = normal.dot(c - a);
0039
0040 if (first) {
0041 ref = std::signbit(dot);
0042 first = false;
0043 continue;
0044 }
0045
0046 if (std::signbit(dot) != ref) {
0047 throw std::logic_error(
0048 "ConvexPolygon: Given vertices do not form convex hull");
0049 }
0050 }
0051 }
0052 }
0053
0054 template <typename coll_t>
0055 Acts::RectangleBounds Acts::ConvexPolygonBoundsBase::makeBoundingBox(
0056 const coll_t& vertices) {
0057 Vector2 vmax, vmin;
0058 vmax = vertices[0];
0059 vmin = vertices[0];
0060
0061 for (std::size_t i = 1; i < vertices.size(); i++) {
0062 vmax = vmax.cwiseMax(vertices[i]);
0063 vmin = vmin.cwiseMin(vertices[i]);
0064 }
0065
0066 return {vmin, vmax};
0067 }
0068
0069 template <int N>
0070 Acts::ConvexPolygonBounds<N>::ConvexPolygonBounds(
0071 const std::vector<Acts::Vector2>& vertices) noexcept(false)
0072 : m_vertices(), m_boundingBox(makeBoundingBox(vertices)) {
0073 throw_assert(vertices.size() == N,
0074 "Size and number of given vertices do not match.");
0075 for (std::size_t i = 0; i < N; i++) {
0076 m_vertices[i] = vertices[i];
0077 }
0078 checkConsistency();
0079 }
0080
0081 template <int N>
0082 Acts::ConvexPolygonBounds<N>::ConvexPolygonBounds(
0083 const vertex_array& vertices) noexcept(false)
0084 : m_vertices(vertices), m_boundingBox(makeBoundingBox(vertices)) {
0085 checkConsistency();
0086 }
0087
0088 template <int N>
0089 Acts::ConvexPolygonBounds<N>::ConvexPolygonBounds(
0090 const value_array& values) noexcept(false)
0091 : m_vertices(), m_boundingBox(0., 0.) {
0092 for (std::size_t i = 0; i < N; i++) {
0093 m_vertices[i] = Vector2(values[2 * i], values[2 * i + 1]);
0094 }
0095 makeBoundingBox(m_vertices);
0096 checkConsistency();
0097 }
0098
0099 template <int N>
0100 bool Acts::ConvexPolygonBounds<N>::inside(
0101 const Acts::Vector2& lposition,
0102 const Acts::BoundaryTolerance& boundaryTolerance) const {
0103 return detail::insidePolygon(m_vertices, boundaryTolerance, lposition,
0104 std::nullopt);
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 }