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) 2019 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/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Surfaces/BoundaryCheck.hpp"
0013 #include "Acts/Surfaces/ConvexPolygonBounds.hpp"
0014 #include "Acts/Surfaces/RectangleBounds.hpp"
0015 
0016 #include <algorithm>
0017 #include <array>
0018 #include <cstddef>
0019 #include <stdexcept>
0020 #include <utility>
0021 #include <vector>
0022 
0023 namespace Acts {
0024 class AssertionFailureException;
0025 }  // namespace Acts
0026 
0027 using vec2 = Acts::Vector2;
0028 template <int N>
0029 using poly = Acts::ConvexPolygonBounds<N>;
0030 
0031 namespace Acts {
0032 namespace Test {
0033 
0034 BOOST_AUTO_TEST_SUITE(Surfaces)
0035 
0036 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConvexity) {
0037   std::vector<vec2> vertices;
0038   vertices = {{0, 0}, {1, 0}, {0.2, 0.2}, {0, 1}};
0039   { BOOST_CHECK_THROW(poly<4> quad(vertices), std::logic_error); }
0040 
0041   vertices = {{0, 0}, {1, 0}, {0.8, 0.8}, {0, 1}};
0042   {
0043     // wrong number of vertices
0044     BOOST_CHECK_THROW(poly<3> trip{vertices}, AssertionFailureException);
0045   }
0046   { poly<4> quad = {vertices}; }
0047 
0048   // this one is self intersecting
0049   vertices = {{0, 0}, {1, 0}, {0.5, 1}, {0.9, 1.2}};
0050   { BOOST_CHECK_THROW(poly<4> quad{vertices}, std::logic_error); }
0051 
0052   // this one is not
0053   vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0054   { poly<4> quad = {vertices}; }
0055 
0056   vertices = {{0, 0}, {1, 0}, {0.8, 0.5}, {1, 1}, {0, 1}};
0057   { BOOST_CHECK_THROW(poly<5> pent(vertices), std::logic_error); }
0058 
0059   vertices = {{0, 0}, {1, 0}, {1.1, 0.5}, {1, 1}, {0, 1}};
0060   { poly<5> pent{vertices}; }
0061 }
0062 
0063 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConstruction) {
0064   std::vector<vec2> vertices;
0065 
0066   // triangle
0067   vertices = {{0, 0}, {1, 0}, {0.5, 1}};
0068   poly<3> triangle(vertices);
0069 
0070   RectangleBounds bb = triangle.boundingBox();
0071   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0072   BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
0073 
0074   BoundaryCheck bc(true);
0075 
0076   BOOST_CHECK(triangle.inside({0.2, 0.2}, bc));
0077   BOOST_CHECK(!triangle.inside({0.4, 0.9}, bc));
0078   BOOST_CHECK(!triangle.inside({0.8, 0.8}, bc));
0079   BOOST_CHECK(!triangle.inside({0.3, -0.2}, bc));
0080 
0081   // rectangular poly
0082   vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0083   poly<4> quad(vertices);
0084 
0085   bb = quad.boundingBox();
0086   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0087   BOOST_CHECK_EQUAL(bb.max(), Vector2(1, 1.2));
0088 
0089   BOOST_CHECK(quad.inside({0.2, 0.2}, bc));
0090   BOOST_CHECK(!quad.inside({0.4, 0.9}, bc));
0091   BOOST_CHECK(quad.inside({0.8, 0.8}, bc));
0092   BOOST_CHECK(!quad.inside({0.3, -0.2}, bc));
0093 }
0094 
0095 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsRecreation) {
0096   // rectangular poly
0097   std::vector<vec2> vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0098   poly<4> original(vertices);
0099 
0100   auto valvector = original.values();
0101   std::array<double, poly<4>::eSize> values{};
0102   std::copy_n(valvector.begin(), poly<4>::eSize, values.begin());
0103   poly<4> recreated(values);
0104   BOOST_CHECK_EQUAL(original, recreated);
0105 }
0106 
0107 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsDynamicTest) {
0108   using poly = ConvexPolygonBounds<PolygonDynamic>;
0109 
0110   std::vector<vec2> vertices;
0111 
0112   // triangle
0113   vertices = {{0, 0}, {1, 0}, {0.5, 1}};
0114   poly triangle(vertices);
0115 
0116   RectangleBounds bb = triangle.boundingBox();
0117   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0118   BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
0119 
0120   BoundaryCheck bc(true);
0121 
0122   BOOST_CHECK(triangle.inside({0.2, 0.2}, bc));
0123   BOOST_CHECK(!triangle.inside({0.4, 0.9}, bc));
0124   BOOST_CHECK(!triangle.inside({0.8, 0.8}, bc));
0125   BOOST_CHECK(!triangle.inside({0.3, -0.2}, bc));
0126 }
0127 
0128 BOOST_AUTO_TEST_SUITE_END()
0129 }  // namespace Test
0130 }  // namespace Acts