ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConvexPolygonBounds.ipp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ConvexPolygonBounds.ipp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 std::ostream& Acts::ConvexPolygonBoundsBase::toStream(std::ostream& sl) const {
12  std::vector<Vector2D> vtxs = vertices();
13  sl << "Acts::ConvexPolygonBounds<" << vtxs.size() << ">: vertices: [x, y]\n";
14  for (size_t i = 0; i < vtxs.size(); i++) {
15  const auto& vtx = vtxs[i];
16  if (i > 0) {
17  sl << ",";
18  sl << "\n";
19  }
20  sl << "[" << vtx.x() << ", " << vtx.y() << "]";
21  }
22  return sl;
23 }
24 
25 template <typename coll_t>
27  const coll_t& vertices) {
28  Vector2D vmax, vmin;
29  vmax = vertices[0];
30  vmin = vertices[0];
31 
32  for (size_t i = 1; i < vertices.size(); i++) {
33  vmax = vmax.cwiseMax(vertices[i]);
34  vmin = vmin.cwiseMin(vertices[i]);
35  }
36 
37  return {vmin, vmax};
38 }
39 
40 std::vector<double> Acts::ConvexPolygonBoundsBase::values() const {
41  std::vector<double> values;
42  for (const auto& vtx : vertices()) {
43  values.push_back(vtx.x());
44  values.push_back(vtx.y());
45  }
46  return values;
47 }
48 
49 template <typename coll_t>
51  const coll_t& vertices) noexcept(false) {
53  "Must be collection of Vector2D");
54 
55  const size_t N = vertices.size();
56  for (size_t i = 0; i < N; i++) {
57  size_t j = (i + 1) % N;
58  const Vector2D& a = vertices[i];
59  const Vector2D& b = vertices[j];
60 
61  const Vector2D ab = b - a;
62  const Vector2D normal = Vector2D(ab.y(), -ab.x()).normalized();
63 
64  bool first = true;
65  bool ref;
66  // loop over all other vertices
67  for (size_t k = 0; k < N; k++) {
68  if (k == i || k == j) {
69  continue;
70  }
71 
72  const Vector2D& c = vertices[k];
73  double dot = normal.dot(c - a);
74 
75  if (first) {
76  ref = std::signbit(dot);
77  first = false;
78  continue;
79  }
80 
81  if (std::signbit(dot) != ref) {
82  throw std::logic_error(
83  "ConvexPolygon: Given vertices do not form convex hull");
84  }
85  }
86  }
87 }
88 
89 template <int N>
91  const std::vector<Acts::Vector2D>& vertices) noexcept(false)
92  : m_vertices(), m_boundingBox(makeBoundingBox(vertices)) {
93  throw_assert(vertices.size() == N,
94  "Size and number of given vertices do not match.");
95  for (size_t i = 0; i < N; i++) {
96  m_vertices[i] = vertices[i];
97  }
98  checkConsistency();
99 }
100 
101 template <int N>
103  const vertex_array& vertices) noexcept(false)
104  : m_vertices(vertices), m_boundingBox(makeBoundingBox(vertices)) {
105  checkConsistency();
106 }
107 
108 template <int N>
110  const value_array& values) noexcept(false)
111  : m_vertices(), m_boundingBox(0., 0.) {
112  for (size_t i = 0; i < N; i++) {
113  m_vertices[i] = Vector2D(values[2 * i], values[2 * i + 1]);
114  }
115  makeBoundingBox(m_vertices);
116  checkConsistency();
117 }
118 
119 template <int N>
122 }
123 
124 template <int N>
126  const Acts::Vector2D& lposition, const Acts::BoundaryCheck& bcheck) const {
127  return bcheck.isInside(lposition, m_vertices);
128 }
129 
130 template <int N>
132  const Acts::Vector2D& lposition) const {
133  return BoundaryCheck(true).distance(lposition, m_vertices);
134 }
135 
136 template <int N>
137 std::vector<Acts::Vector2D> Acts::ConvexPolygonBounds<N>::vertices(
138  unsigned int /*lseg*/) const {
139  return {m_vertices.begin(), m_vertices.end()};
140 }
141 
142 template <int N>
144  return m_boundingBox;
145 }
146 
147 template <int N>
149  convex_impl(m_vertices);
150 }
151 
153  const std::vector<Vector2D>& vertices)
154  : m_vertices(vertices.begin(), vertices.end()),
155  m_boundingBox(makeBoundingBox(vertices)) {}
156 
160 }
161 
163  const Acts::Vector2D& lposition, const Acts::BoundaryCheck& bcheck) const {
164  return bcheck.isInside(lposition, m_vertices);
165 }
166 
168  const Acts::Vector2D& lposition) const {
169  return BoundaryCheck(true).distance(lposition, m_vertices);
170 }
171 
172 std::vector<Acts::Vector2D> Acts::ConvexPolygonBounds<
173  Acts::PolygonDynamic>::vertices(unsigned int /*lseg*/) const {
174  return {m_vertices.begin(), m_vertices.end()};
175 }
176 
179  return m_boundingBox;
180 }
181 
183  noexcept(false) {
184  convex_impl(m_vertices);
185 }