EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IntersectionHelper2DTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file IntersectionHelper2DTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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/data/test_case.hpp>
10 #include <boost/test/tools/output_test_stream.hpp>
11 #include <boost/test/unit_test.hpp>
12 
16 
17 #include <limits>
18 
19 namespace Acts {
20 
21 namespace Test {
22 
23 BOOST_AUTO_TEST_SUITE(Surfaces)
24 
25 void basicChecks(bool circleCase = false) {
26  double rY = 10.;
27  double rX = circleCase ? rY : 5.;
28 
29  // Line along x - not intersecting
30  Vector2D start(12., 0.);
31  Vector2D direction(0., -1.);
32 
33  auto nosol = circleCase ? detail::IntersectionHelper2D::intersectCircle(
34  rY, start, direction)
36  rX, rY, start, direction);
37  BOOST_CHECK(not nosol.first);
38  BOOST_CHECK(not nosol.second);
39 
40  start = Vector2D(4., -4.);
41  auto twosol = circleCase ? detail::IntersectionHelper2D::intersectCircle(
42  rY, start, direction)
44  rX, rY, start, direction);
45 
46  BOOST_CHECK(twosol.first);
47  BOOST_CHECK(twosol.second);
48 
49  start = Vector2D(-4., 10.);
50  direction = Vector2D(1., 0.);
51 
52  auto onesolY = circleCase ? detail::IntersectionHelper2D::intersectCircle(
53  rY, start, direction)
55  rX, rY, start, direction);
56 
57  BOOST_CHECK(onesolY.first);
58  CHECK_CLOSE_ABS(onesolY.first.position.x(), 0., s_epsilon);
59  BOOST_CHECK(not onesolY.second);
60 
61  start = Vector2D(rX, -4);
62  direction = Vector2D(0., 1.);
63 
64  auto onesolX = circleCase ? detail::IntersectionHelper2D::intersectCircle(
65  rY, start, direction)
67  rX, rY, start, direction);
68 
69  BOOST_CHECK(onesolX.first);
70  CHECK_CLOSE_ABS(onesolX.first.position.y(), 0., s_epsilon);
71  BOOST_CHECK(not onesolX.second);
72 }
73 
75 BOOST_AUTO_TEST_CASE(LineLineIntersection) {
76  Vector2D start(1., 1.);
77  Vector2D dir(1., 1.);
78 
79  // Not possible
81  Vector2D(5., 3.), Vector2D(6., 4), start, dir);
82 
83  BOOST_CHECK(not solution);
84 
85  // Possible
87  Vector2D(5., 3.), Vector2D(3., -1.), start, dir);
88 
89  BOOST_CHECK(solution);
90 }
91 
93 BOOST_AUTO_TEST_CASE(EllipseIntersection) {
94  // Basic checks for ellipse
95  basicChecks();
96 
97  // Specific checks for ellipse
98  double radiusX = 450.;
99  double radiusY = 275.;
100 
101  Vector2D start(-500., -300.);
102  Vector2D direction = Vector2D(10., 4.).normalized();
103 
105  radiusX, radiusY, start, direction);
106 
107  // Numerically checked / per hand calculated
108  BOOST_CHECK(solution.first);
109 
110  CHECK_CLOSE_ABS(solution.first.position.x(), -283.68, 0.01);
111  CHECK_CLOSE_ABS(solution.first.position.y(), -213.47, 0.01);
112  BOOST_CHECK(solution.first.pathLength > 0.);
113 
114  BOOST_CHECK(solution.second);
115 
116  CHECK_CLOSE_ABS(solution.second.position.x(), 433.65, 0.01);
117  CHECK_CLOSE_ABS(solution.second.position.y(), 73.46, 0.01);
118  BOOST_CHECK(solution.second.pathLength > 0.);
119 
120  // Reverse checks will be done with circle (same code)
121 }
122 
124 BOOST_AUTO_TEST_CASE(CircleIntersection) {
125  // Basic checks for circle
126  basicChecks(true);
127 
128  // Specific checks for circle
129  double radius = 275.;
130 
131  Vector2D start(-500., -300.);
132  Vector2D direction = Vector2D(1., 1.).normalized();
133 
134  auto solution =
135  detail::IntersectionHelper2D::intersectCircle(radius, start, direction);
136 
137  // Numerically checked / per hand calculated
138  BOOST_CHECK(solution.first);
139 
140  CHECK_CLOSE_ABS(solution.first.position.x(), -266.771, 0.001);
141  CHECK_CLOSE_ABS(solution.first.position.y(), -66.771, 0.001);
142  BOOST_CHECK(solution.first.pathLength > 0.);
143 
144  BOOST_CHECK(solution.second);
145 
146  CHECK_CLOSE_ABS(solution.second.position.x(), 66.771, 0.001);
147  CHECK_CLOSE_ABS(solution.second.position.y(), 266.771, 0.001);
148  BOOST_CHECK(solution.second.pathLength > 0.);
149 
150  // Reverse
151  start = Vector2D(1500., 1700.);
152  direction = Vector2D(1., 1.).normalized();
153  solution =
154  detail::IntersectionHelper2D::intersectCircle(radius, start, direction);
155 
156  BOOST_CHECK(solution.first);
157  CHECK_CLOSE_ABS(solution.first.position.x(), 66.771, 0.001);
158  CHECK_CLOSE_ABS(solution.first.position.y(), 266.771, 0.001);
159  BOOST_CHECK(solution.first.pathLength < 0.);
160 
161  BOOST_CHECK(solution.second);
162  CHECK_CLOSE_ABS(solution.second.position.x(), -266.771, 0.001);
163  CHECK_CLOSE_ABS(solution.second.position.y(), -66.771, 0.001);
164  BOOST_CHECK(solution.second.pathLength < 0.);
165 
166  // Reverse with reverse direction
167  direction = Vector2D(-1., -1.).normalized();
168  solution =
169  detail::IntersectionHelper2D::intersectCircle(radius, start, direction);
170 
171  BOOST_CHECK(solution.first);
172  CHECK_CLOSE_ABS(solution.first.position.x(), 66.771, 0.001);
173  CHECK_CLOSE_ABS(solution.first.position.y(), 266.771, 0.001);
174  BOOST_CHECK(solution.first.pathLength > 0.);
175 
176  BOOST_CHECK(solution.second);
177  CHECK_CLOSE_ABS(solution.second.position.x(), -266.771, 0.001);
178  CHECK_CLOSE_ABS(solution.second.position.y(), -66.771, 0.001);
179  BOOST_CHECK(solution.second.pathLength > 0.);
180 }
181 
182 BOOST_AUTO_TEST_SUITE_END()
183 
184 } // namespace Test
185 } // namespace Acts