Analysis Software
Documentation for sPHENIX simulation 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 
15 
16 #include <algorithm>
17 #include <array>
18 #include <cstddef>
19 #include <stdexcept>
20 #include <utility>
21 #include <vector>
22 
23 namespace Acts {
24 class AssertionFailureException;
25 } // namespace Acts
26 
28 template <int N>
30 
31 namespace Acts {
32 namespace Test {
33 
34 BOOST_AUTO_TEST_SUITE(Surfaces)
35 
36 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConvexity) {
37  std::vector<vec2> vertices;
38  vertices = {{0, 0}, {1, 0}, {0.2, 0.2}, {0, 1}};
39  { BOOST_CHECK_THROW(poly<4> quad(vertices), std::logic_error); }
40 
41  vertices = {{0, 0}, {1, 0}, {0.8, 0.8}, {0, 1}};
42  {
43  // wrong number of vertices
44  BOOST_CHECK_THROW(poly<3> trip{vertices}, AssertionFailureException);
45  }
46  { poly<4> quad = {vertices}; }
47 
48  // this one is self intersecting
49  vertices = {{0, 0}, {1, 0}, {0.5, 1}, {0.9, 1.2}};
50  { BOOST_CHECK_THROW(poly<4> quad{vertices}, std::logic_error); }
51 
52  // this one is not
53  vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
54  { poly<4> quad = {vertices}; }
55 
56  vertices = {{0, 0}, {1, 0}, {0.8, 0.5}, {1, 1}, {0, 1}};
57  { BOOST_CHECK_THROW(poly<5> pent(vertices), std::logic_error); }
58 
59  vertices = {{0, 0}, {1, 0}, {1.1, 0.5}, {1, 1}, {0, 1}};
60  { poly<5> pent{vertices}; }
61 }
62 
63 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConstruction) {
64  std::vector<vec2> vertices;
65 
66  // triangle
67  vertices = {{0, 0}, {1, 0}, {0.5, 1}};
68  poly<3> triangle(vertices);
69 
70  RectangleBounds bb = triangle.boundingBox();
71  BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
72  BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
73 
74  BoundaryCheck bc(true);
75 
76  BOOST_CHECK(triangle.inside({0.2, 0.2}, bc));
77  BOOST_CHECK(!triangle.inside({0.4, 0.9}, bc));
78  BOOST_CHECK(!triangle.inside({0.8, 0.8}, bc));
79  BOOST_CHECK(!triangle.inside({0.3, -0.2}, bc));
80 
81  // rectangular poly
82  vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
83  poly<4> quad(vertices);
84 
85  bb = quad.boundingBox();
86  BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
87  BOOST_CHECK_EQUAL(bb.max(), Vector2(1, 1.2));
88 
89  BOOST_CHECK(quad.inside({0.2, 0.2}, bc));
90  BOOST_CHECK(!quad.inside({0.4, 0.9}, bc));
91  BOOST_CHECK(quad.inside({0.8, 0.8}, bc));
92  BOOST_CHECK(!quad.inside({0.3, -0.2}, bc));
93 }
94 
95 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsRecreation) {
96  // rectangular poly
97  std::vector<vec2> vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
98  poly<4> original(vertices);
99 
100  auto valvector = original.values();
101  std::array<double, poly<4>::eSize> values{};
102  std::copy_n(valvector.begin(), poly<4>::eSize, values.begin());
103  poly<4> recreated(values);
104  BOOST_CHECK_EQUAL(original, recreated);
105 }
106 
107 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsDynamicTest) {
109 
110  std::vector<vec2> vertices;
111 
112  // triangle
113  vertices = {{0, 0}, {1, 0}, {0.5, 1}};
114  poly triangle(vertices);
115 
116  RectangleBounds bb = triangle.boundingBox();
117  BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
118  BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
119 
120  BoundaryCheck bc(true);
121 
122  BOOST_CHECK(triangle.inside({0.2, 0.2}, bc));
123  BOOST_CHECK(!triangle.inside({0.4, 0.9}, bc));
124  BOOST_CHECK(!triangle.inside({0.8, 0.8}, bc));
125  BOOST_CHECK(!triangle.inside({0.3, -0.2}, bc));
126 }
127 
128 BOOST_AUTO_TEST_SUITE_END()
129 } // namespace Test
130 } // namespace Acts