ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConvexPolygonBoundsTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ConvexPolygonBoundsTests.cpp
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 
9 #include <boost/test/unit_test.hpp>
10 
11 #include <chrono>
12 #include <iostream>
13 #include <memory>
14 
18 
20 template <int N>
22 
23 namespace Acts {
24 namespace Test {
25 
26 BOOST_AUTO_TEST_SUITE(Surfaces)
27 
28 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConvexity) {
29  std::vector<vec2> vertices;
30  vertices = {{0, 0}, {1, 0}, {0.2, 0.2}, {0, 1}};
31  { BOOST_CHECK_THROW(poly<4> quad(vertices), std::logic_error); }
32 
33  vertices = {{0, 0}, {1, 0}, {0.8, 0.8}, {0, 1}};
34  {
35  // wrong number of vertices
36  BOOST_CHECK_THROW(poly<3> trip{vertices}, AssertionFailureException);
37  }
38  { poly<4> quad = {vertices}; }
39 
40  // this one is self intersecting
41  vertices = {{0, 0}, {1, 0}, {0.5, 1}, {0.9, 1.2}};
42  { BOOST_CHECK_THROW(poly<4> quad{vertices}, std::logic_error); }
43 
44  // this one is not
45  vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
46  { poly<4> quad = {vertices}; }
47 
48  vertices = {{0, 0}, {1, 0}, {0.8, 0.5}, {1, 1}, {0, 1}};
49  { BOOST_CHECK_THROW(poly<5> pent(vertices), std::logic_error); }
50 
51  vertices = {{0, 0}, {1, 0}, {1.1, 0.5}, {1, 1}, {0, 1}};
52  { poly<5> pent{vertices}; }
53 }
54 
55 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConstruction) {
56  std::vector<vec2> vertices;
57 
58  // triangle
59  vertices = {{0, 0}, {1, 0}, {0.5, 1}};
60  poly<3> triangle(vertices);
61 
62  RectangleBounds bb = triangle.boundingBox();
63  BOOST_CHECK_EQUAL(bb.min(), Vector2D(0, 0));
64  BOOST_CHECK_EQUAL(bb.max(), Vector2D(1., 1));
65 
66  BoundaryCheck bc(true);
67 
68  BOOST_CHECK(triangle.inside({0.2, 0.2}, bc));
69  BOOST_CHECK(!triangle.inside({0.4, 0.9}, bc));
70  BOOST_CHECK(!triangle.inside({0.8, 0.8}, bc));
71  BOOST_CHECK(!triangle.inside({0.3, -0.2}, bc));
72 
73  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.2, 0.2}), -0.0894427, 1e-6);
74  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.4, 0.9}), 0.0447213, 1e-6);
75  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.8, 0.8}), 0.1788854, 1e-6);
76  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.3, -0.2}), 0.2, 1e-6);
77 
78  // rectangular poly
79  vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
80  poly<4> quad(vertices);
81 
82  bb = quad.boundingBox();
83  BOOST_CHECK_EQUAL(bb.min(), Vector2D(0, 0));
84  BOOST_CHECK_EQUAL(bb.max(), Vector2D(1, 1.2));
85 
86  BOOST_CHECK(quad.inside({0.2, 0.2}, bc));
87  BOOST_CHECK(!quad.inside({0.4, 0.9}, bc));
88  BOOST_CHECK(quad.inside({0.8, 0.8}, bc));
89  BOOST_CHECK(!quad.inside({0.3, -0.2}, bc));
90 
91  CHECK_CLOSE_ABS(quad.distanceToBoundary({0.2, 0.2}), -0.089442, 1e-6);
92  CHECK_CLOSE_ABS(quad.distanceToBoundary({0.4, 0.9}), 0.044721, 1e-6);
93  CHECK_CLOSE_ABS(quad.distanceToBoundary({0.8, 0.8}), -0.132872, 1e-6);
94  CHECK_CLOSE_ABS(quad.distanceToBoundary({0.3, -0.2}), 0.2, 1e-6);
95 }
96 
97 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsRecreation) {
98  // rectangular poly
99  std::vector<vec2> vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
100  poly<4> original(vertices);
101 
102  auto valvector = original.values();
103  std::array<double, poly<4>::eSize> values;
104  std::copy_n(valvector.begin(), poly<4>::eSize, values.begin());
105  poly<4> recreated(values);
106  BOOST_CHECK_EQUAL(original, recreated);
107 }
108 
109 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsDynamicTest) {
111 
112  std::vector<vec2> vertices;
113 
114  // triangle
115  vertices = {{0, 0}, {1, 0}, {0.5, 1}};
116  poly triangle(vertices);
117 
118  RectangleBounds bb = triangle.boundingBox();
119  BOOST_CHECK_EQUAL(bb.min(), Vector2D(0, 0));
120  BOOST_CHECK_EQUAL(bb.max(), Vector2D(1., 1));
121 
122  BoundaryCheck bc(true);
123 
124  BOOST_CHECK(triangle.inside({0.2, 0.2}, bc));
125  BOOST_CHECK(!triangle.inside({0.4, 0.9}, bc));
126  BOOST_CHECK(!triangle.inside({0.8, 0.8}, bc));
127  BOOST_CHECK(!triangle.inside({0.3, -0.2}, bc));
128 
129  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.2, 0.2}), -0.0894427, 1e-6);
130  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.4, 0.9}), 0.0447213, 1e-6);
131  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.8, 0.8}), 0.1788854, 1e-6);
132  CHECK_CLOSE_ABS(triangle.distanceToBoundary({0.3, -0.2}), 0.2, 1e-6);
133 }
134 
135 BOOST_AUTO_TEST_SUITE_END()
136 } // namespace Test
137 } // namespace Acts