Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:28

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2020 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/tools/output_test_stream.hpp>
0011 #include <boost/test/unit_test.hpp>
0012 
0013 #include "Acts/Definitions/Algebra.hpp"
0014 #include "Acts/Definitions/Tolerance.hpp"
0015 #include "Acts/Surfaces/detail/IntersectionHelper2D.hpp"
0016 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0017 #include "Acts/Utilities/Intersection.hpp"
0018 
0019 #include <array>
0020 
0021 namespace Acts {
0022 
0023 namespace Test {
0024 
0025 BOOST_AUTO_TEST_SUITE(Surfaces)
0026 
0027 void basicChecks(bool circleCase = false) {
0028   double rY = 10.;
0029   double rX = circleCase ? rY : 5.;
0030 
0031   // Line along x - not intersecting
0032   Vector2 start(12., 0.);
0033   Vector2 direction(0., -1.);
0034 
0035   auto nosol = circleCase ? detail::IntersectionHelper2D::intersectCircle(
0036                                 rY, start, direction)
0037                           : detail::IntersectionHelper2D::intersectEllipse(
0038                                 rX, rY, start, direction);
0039   BOOST_CHECK(!nosol[0]);
0040   BOOST_CHECK(!nosol[1]);
0041 
0042   start = Vector2(4., -4.);
0043   auto twosol = circleCase ? detail::IntersectionHelper2D::intersectCircle(
0044                                  rY, start, direction)
0045                            : detail::IntersectionHelper2D::intersectEllipse(
0046                                  rX, rY, start, direction);
0047 
0048   BOOST_CHECK(twosol[0]);
0049   BOOST_CHECK(twosol[1]);
0050 
0051   start = Vector2(-4., 10.);
0052   direction = Vector2(1., 0.);
0053 
0054   auto onesolY = circleCase ? detail::IntersectionHelper2D::intersectCircle(
0055                                   rY, start, direction)
0056                             : detail::IntersectionHelper2D::intersectEllipse(
0057                                   rX, rY, start, direction);
0058 
0059   BOOST_CHECK(onesolY[0]);
0060   CHECK_CLOSE_ABS(onesolY[0].position().x(), 0., s_epsilon);
0061   BOOST_CHECK(!onesolY[1]);
0062 
0063   start = Vector2(rX, -4);
0064   direction = Vector2(0., 1.);
0065 
0066   auto onesolX = circleCase ? detail::IntersectionHelper2D::intersectCircle(
0067                                   rY, start, direction)
0068                             : detail::IntersectionHelper2D::intersectEllipse(
0069                                   rX, rY, start, direction);
0070 
0071   BOOST_CHECK(onesolX[0]);
0072   CHECK_CLOSE_ABS(onesolX[0].position().y(), 0., s_epsilon);
0073   BOOST_CHECK(!onesolX[1]);
0074 }
0075 
0076 /// Unit test for creating Ellipse intersection
0077 BOOST_AUTO_TEST_CASE(LineLineIntersection) {
0078   Vector2 start(1., 1.);
0079   Vector2 dir(1., 1.);
0080 
0081   // Not possible
0082   auto solution = detail::IntersectionHelper2D::intersectSegment(
0083       Vector2(5., 3.), Vector2(6., 4), start, dir.normalized());
0084 
0085   BOOST_CHECK(!solution);
0086 
0087   // Possible
0088   solution = detail::IntersectionHelper2D::intersectSegment(
0089       Vector2(5., 3.), Vector2(3., -1.), start, dir.normalized());
0090 
0091   BOOST_CHECK(solution);
0092 
0093   // In principle possible, but out of bound
0094   start = Vector2(2, 3);
0095   dir = Vector2(2, 1).normalized();
0096 
0097   solution = detail::IntersectionHelper2D::intersectSegment(
0098       Vector2(-1., -2.5), Vector2(3., 2.5), start, dir);
0099 
0100   BOOST_CHECK(solution);
0101 
0102   solution = detail::IntersectionHelper2D::intersectSegment(
0103       Vector2(-1., -2.5), Vector2(3., 2.5), start, dir, true);
0104 
0105   BOOST_CHECK(!solution);
0106 }
0107 
0108 /// Unit test for creating Ellipse intersection
0109 BOOST_AUTO_TEST_CASE(EllipseIntersection) {
0110   // Basic checks for ellipse
0111   basicChecks();
0112 
0113   // Specific checks for ellipse
0114   double radiusX = 450.;
0115   double radiusY = 275.;
0116 
0117   Vector2 start(-500., -300.);
0118   Vector2 direction = Vector2(10., 4.).normalized();
0119 
0120   auto solution = detail::IntersectionHelper2D::intersectEllipse(
0121       radiusX, radiusY, start, direction);
0122 
0123   // Numerically checked / per hand calculated
0124   BOOST_CHECK(solution[0]);
0125 
0126   CHECK_CLOSE_ABS(solution[0].position().x(), -283.68, 0.01);
0127   CHECK_CLOSE_ABS(solution[0].position().y(), -213.47, 0.01);
0128   BOOST_CHECK_GT(solution[0].pathLength(), 0.);
0129 
0130   BOOST_CHECK(solution[1]);
0131 
0132   CHECK_CLOSE_ABS(solution[1].position().x(), 433.65, 0.01);
0133   CHECK_CLOSE_ABS(solution[1].position().y(), 73.46, 0.01);
0134   BOOST_CHECK_GT(solution[1].pathLength(), 0.);
0135 
0136   // Reverse checks will be done with circle (same code)
0137 }
0138 
0139 /// Unit test for creating Circle intersection
0140 BOOST_AUTO_TEST_CASE(CircleIntersection) {
0141   // Basic checks for circle
0142   basicChecks(true);
0143 
0144   // Specific checks for circle
0145   double radius = 275.;
0146 
0147   Vector2 start(-500., -300.);
0148   Vector2 direction = Vector2(1., 1.).normalized();
0149 
0150   auto solution =
0151       detail::IntersectionHelper2D::intersectCircle(radius, start, direction);
0152 
0153   // Numerically checked / per hand calculated
0154   BOOST_CHECK(solution[0]);
0155 
0156   CHECK_CLOSE_ABS(solution[0].position().x(), -266.771, 0.001);
0157   CHECK_CLOSE_ABS(solution[0].position().y(), -66.771, 0.001);
0158   BOOST_CHECK_GT(solution[0].pathLength(), 0.);
0159 
0160   BOOST_CHECK(solution[1]);
0161 
0162   CHECK_CLOSE_ABS(solution[1].position().x(), 66.771, 0.001);
0163   CHECK_CLOSE_ABS(solution[1].position().y(), 266.771, 0.001);
0164   BOOST_CHECK_GT(solution[1].pathLength(), 0.);
0165 
0166   // Reverse
0167   start = Vector2(1500., 1700.);
0168   direction = Vector2(1., 1.).normalized();
0169   solution =
0170       detail::IntersectionHelper2D::intersectCircle(radius, start, direction);
0171 
0172   BOOST_CHECK(solution[0]);
0173   CHECK_CLOSE_ABS(solution[0].position().x(), 66.771, 0.001);
0174   CHECK_CLOSE_ABS(solution[0].position().y(), 266.771, 0.001);
0175   BOOST_CHECK_LT(solution[0].pathLength(), 0.);
0176 
0177   BOOST_CHECK(solution[1]);
0178   CHECK_CLOSE_ABS(solution[1].position().x(), -266.771, 0.001);
0179   CHECK_CLOSE_ABS(solution[1].position().y(), -66.771, 0.001);
0180   BOOST_CHECK_LT(solution[1].pathLength(), 0.);
0181 
0182   // Reverse with reverse direction
0183   direction = Vector2(-1., -1.).normalized();
0184   solution =
0185       detail::IntersectionHelper2D::intersectCircle(radius, start, direction);
0186 
0187   BOOST_CHECK(solution[0]);
0188   CHECK_CLOSE_ABS(solution[0].position().x(), 66.771, 0.001);
0189   CHECK_CLOSE_ABS(solution[0].position().y(), 266.771, 0.001);
0190   BOOST_CHECK_GT(solution[0].pathLength(), 0.);
0191 
0192   BOOST_CHECK(solution[1]);
0193   CHECK_CLOSE_ABS(solution[1].position().x(), -266.771, 0.001);
0194   CHECK_CLOSE_ABS(solution[1].position().y(), -66.771, 0.001);
0195   BOOST_CHECK_GT(solution[1].pathLength(), 0.);
0196 }
0197 
0198 BOOST_AUTO_TEST_SUITE_END()
0199 
0200 }  // namespace Test
0201 }  // namespace Acts