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