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 
14 
15 #include <chrono>
16 #include <iostream>
17 #include <memory>
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  // rectangular poly
74  vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
75  poly<4> quad(vertices);
76 
77  bb = quad.boundingBox();
78  BOOST_CHECK_EQUAL(bb.min(), Vector2D(0, 0));
79  BOOST_CHECK_EQUAL(bb.max(), Vector2D(1, 1.2));
80 
81  BOOST_CHECK(quad.inside({0.2, 0.2}, bc));
82  BOOST_CHECK(!quad.inside({0.4, 0.9}, bc));
83  BOOST_CHECK(quad.inside({0.8, 0.8}, bc));
84  BOOST_CHECK(!quad.inside({0.3, -0.2}, bc));
85 }
86 
87 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsRecreation) {
88  // rectangular poly
89  std::vector<vec2> vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
90  poly<4> original(vertices);
91 
92  auto valvector = original.values();
93  std::array<double, poly<4>::eSize> values;
94  std::copy_n(valvector.begin(), poly<4>::eSize, values.begin());
95  poly<4> recreated(values);
96  BOOST_CHECK_EQUAL(original, recreated);
97 }
98 
99 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsDynamicTest) {
101 
102  std::vector<vec2> vertices;
103 
104  // triangle
105  vertices = {{0, 0}, {1, 0}, {0.5, 1}};
106  poly triangle(vertices);
107 
108  RectangleBounds bb = triangle.boundingBox();
109  BOOST_CHECK_EQUAL(bb.min(), Vector2D(0, 0));
110  BOOST_CHECK_EQUAL(bb.max(), Vector2D(1., 1));
111 
112  BoundaryCheck bc(true);
113 
114  BOOST_CHECK(triangle.inside({0.2, 0.2}, bc));
115  BOOST_CHECK(!triangle.inside({0.4, 0.9}, bc));
116  BOOST_CHECK(!triangle.inside({0.8, 0.8}, bc));
117  BOOST_CHECK(!triangle.inside({0.3, -0.2}, bc));
118 }
119 
120 BOOST_AUTO_TEST_SUITE_END()
121 } // namespace Test
122 } // namespace Acts