Analysis Software
Documentation for sPHENIX simulation 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 template <typename coll_t>
13  const coll_t& vertices) noexcept(false) {
15  "Must be collection of Vector2");
16 
17  const size_t N = vertices.size();
18  for (size_t i = 0; i < N; i++) {
19  size_t j = (i + 1) % N;
20  const Vector2& a = vertices[i];
21  const Vector2& b = vertices[j];
22 
23  const Vector2 ab = b - a;
24  const Vector2 normal = Vector2(ab.y(), -ab.x()).normalized();
25 
26  bool first = true;
27  bool ref = false;
28  // loop over all other vertices
29  for (size_t k = 0; k < N; k++) {
30  if (k == i || k == j) {
31  continue;
32  }
33 
34  const Vector2& c = vertices[k];
35  double dot = normal.dot(c - a);
36 
37  if (first) {
38  ref = std::signbit(dot);
39  first = false;
40  continue;
41  }
42 
43  if (std::signbit(dot) != ref) {
44  throw std::logic_error(
45  "ConvexPolygon: Given vertices do not form convex hull");
46  }
47  }
48  }
49 }
50 
51 template <typename coll_t>
53  const coll_t& vertices) {
54  Vector2 vmax, vmin;
55  vmax = vertices[0];
56  vmin = vertices[0];
57 
58  for (size_t i = 1; i < vertices.size(); i++) {
59  vmax = vmax.cwiseMax(vertices[i]);
60  vmin = vmin.cwiseMin(vertices[i]);
61  }
62 
63  return {vmin, vmax};
64 }
65 
66 template <int N>
68  const std::vector<Acts::Vector2>& vertices) noexcept(false)
69  : m_vertices(), m_boundingBox(makeBoundingBox(vertices)) {
70  throw_assert(vertices.size() == N,
71  "Size and number of given vertices do not match.");
72  for (size_t i = 0; i < N; i++) {
73  m_vertices[i] = vertices[i];
74  }
75  checkConsistency();
76 }
77 
78 template <int N>
80  const vertex_array& vertices) noexcept(false)
81  : m_vertices(vertices), m_boundingBox(makeBoundingBox(vertices)) {
82  checkConsistency();
83 }
84 
85 template <int N>
87  const value_array& values) noexcept(false)
88  : m_vertices(), m_boundingBox(0., 0.) {
89  for (size_t i = 0; i < N; i++) {
90  m_vertices[i] = Vector2(values[2 * i], values[2 * i + 1]);
91  }
92  makeBoundingBox(m_vertices);
93  checkConsistency();
94 }
95 
96 template <int N>
99 }
100 
101 template <int N>
103  const Acts::Vector2& lposition, const Acts::BoundaryCheck& bcheck) const {
104  return bcheck.isInside(lposition, m_vertices);
105 }
106 
107 template <int N>
108 std::vector<Acts::Vector2> Acts::ConvexPolygonBounds<N>::vertices(
109  unsigned int /*lseg*/) const {
110  return {m_vertices.begin(), m_vertices.end()};
111 }
112 
113 template <int N>
115  return m_boundingBox;
116 }
117 
118 template <int N>
120  convex_impl(m_vertices);
121 }