Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-02 08:54:32

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0013 #include "Acts/Surfaces/ConvexPolygonBounds.hpp"
0014 #include "Acts/Surfaces/RectangleBounds.hpp"
0015 
0016 #include <algorithm>
0017 #include <array>
0018 #include <cmath>
0019 #include <stdexcept>
0020 #include <vector>
0021 
0022 using vec2 = Acts::Vector2;
0023 template <int N>
0024 using poly = Acts::ConvexPolygonBounds<N>;
0025 
0026 using namespace Acts;
0027 
0028 namespace ActsTests {
0029 
0030 BOOST_AUTO_TEST_SUITE(SurfacesSuite)
0031 
0032 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConvexity) {
0033   std::vector<vec2> vertices;
0034   vertices = {{0, 0}, {1, 0}, {0.2, 0.2}, {0, 1}};
0035   { BOOST_CHECK_THROW(poly<4> quad(vertices), std::logic_error); }
0036 
0037   vertices = {{0, 0}, {1, 0}, {0.8, 0.8}, {0, 1}};
0038   {
0039     // wrong number of vertices
0040     BOOST_CHECK_THROW(poly<3> trip{vertices}, AssertionFailureException);
0041   }
0042   { poly<4> quad(vertices); }
0043 
0044   // this one is self intersecting
0045   vertices = {{0, 0}, {1, 0}, {0.5, 1}, {0.9, 1.2}};
0046   { BOOST_CHECK_THROW(poly<4> quad{vertices}, std::logic_error); }
0047 
0048   // this one is not
0049   vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0050   { poly<4> quad(vertices); }
0051 
0052   vertices = {{0, 0}, {1, 0}, {0.8, 0.5}, {1, 1}, {0, 1}};
0053   { BOOST_CHECK_THROW(poly<5> pent{vertices}, std::logic_error); }
0054 
0055   vertices = {{0, 0}, {1, 0}, {1.1, 0.5}, {1, 1}, {0, 1}};
0056   { poly<5> pent(vertices); }
0057 }
0058 
0059 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConstruction) {
0060   std::vector<vec2> vertices;
0061 
0062   // triangle
0063   vertices = {{0, 0}, {1, 0}, {0.5, 1}};
0064   poly<3> triangle(vertices);
0065 
0066   RectangleBounds bb = triangle.boundingBox();
0067   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0068   BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
0069 
0070   BoundaryTolerance tolerance = BoundaryTolerance::None();
0071 
0072   BOOST_CHECK(triangle.inside({0.2, 0.2}, tolerance));
0073   BOOST_CHECK(!triangle.inside({0.4, 0.9}, tolerance));
0074   BOOST_CHECK(!triangle.inside({0.8, 0.8}, tolerance));
0075   BOOST_CHECK(!triangle.inside({0.3, -0.2}, tolerance));
0076 
0077   // rectangular poly
0078   vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0079   poly<4> quad(vertices);
0080 
0081   bb = quad.boundingBox();
0082   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0083   BOOST_CHECK_EQUAL(bb.max(), Vector2(1, 1.2));
0084 
0085   BOOST_CHECK(quad.inside({0.2, 0.2}, tolerance));
0086   BOOST_CHECK(!quad.inside({0.4, 0.9}, tolerance));
0087   BOOST_CHECK(quad.inside({0.8, 0.8}, tolerance));
0088   BOOST_CHECK(!quad.inside({0.3, -0.2}, tolerance));
0089 }
0090 
0091 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsRecreation) {
0092   // rectangular poly
0093   std::vector<vec2> vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0094   poly<4> original(vertices);
0095 
0096   auto valvector = original.values();
0097   std::array<double, poly<4>::eSize> values{};
0098   std::copy_n(valvector.begin(), poly<4>::eSize, values.begin());
0099   poly<4> recreated(values);
0100   BOOST_CHECK_EQUAL(original, recreated);
0101 
0102   // Get the vertices back
0103   auto rvertices = original.vertices();
0104   BOOST_CHECK_EQUAL(rvertices.size(), 4u);
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   // Too few vertices
0117   vertices = {{0, 0}, {1, 1}};
0118   BOOST_CHECK_THROW(poly{vertices}, std::invalid_argument);
0119 
0120   RectangleBounds bb = triangle.boundingBox();
0121   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0122   BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
0123 
0124   BoundaryTolerance tolerance = BoundaryTolerance::None();
0125 
0126   BOOST_CHECK(triangle.inside({0.2, 0.2}, tolerance));
0127   BOOST_CHECK(!triangle.inside({0.4, 0.9}, tolerance));
0128   BOOST_CHECK(!triangle.inside({0.8, 0.8}, tolerance));
0129   BOOST_CHECK(!triangle.inside({0.3, -0.2}, tolerance));
0130 }
0131 
0132 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsCenterTest) {
0133   // Test center calculation for fixed-size polygons
0134 
0135   // Triangle with vertices at (0,0), (3,0), (1.5,3)
0136   // Expected center: (1.5, 1.0)
0137   std::vector<vec2> triangleVertices = {{0, 0}, {3, 0}, {1.5, 3}};
0138   poly<3> triangle(triangleVertices);
0139   vec2 triangleCenter = triangle.center();
0140   BOOST_CHECK_CLOSE(triangleCenter.x(), 1.5, 1e-10);
0141   BOOST_CHECK_CLOSE(triangleCenter.y(), 1.0, 1e-10);
0142 
0143   // Square with vertices at (0,0), (2,0), (2,2), (0,2)
0144   // Expected center: (1.0, 1.0)
0145   std::vector<vec2> squareVertices = {{0, 0}, {2, 0}, {2, 2}, {0, 2}};
0146   poly<4> square(squareVertices);
0147   vec2 squareCenter = square.center();
0148   BOOST_CHECK_CLOSE(squareCenter.x(), 1.0, 1e-10);
0149   BOOST_CHECK_CLOSE(squareCenter.y(), 1.0, 1e-10);
0150 
0151   // Pentagon with vertices at (0,0), (2,0), (3,1.5), (1,3), (-1,1.5)
0152   // Expected center: (1.0, 1.2)
0153   std::vector<vec2> pentVertices = {
0154       {0, 0}, {2, 0}, {3, 1.5}, {1, 3}, {-1, 1.5}};
0155   poly<5> pentagon(pentVertices);
0156   vec2 pentCenter = pentagon.center();
0157   BOOST_CHECK_CLOSE(pentCenter.x(), 1.0, 1e-10);
0158   BOOST_CHECK_CLOSE(pentCenter.y(), 1.2, 1e-10);
0159 
0160   // Test center calculation for dynamic polygons
0161   using polyDyn = ConvexPolygonBounds<PolygonDynamic>;
0162 
0163   // Triangle (same as above)
0164   polyDyn triangleDyn(triangleVertices);
0165   vec2 triangleCenterDyn = triangleDyn.center();
0166   BOOST_CHECK_CLOSE(triangleCenterDyn.x(), 1.5, 1e-10);
0167   BOOST_CHECK_CLOSE(triangleCenterDyn.y(), 1.0, 1e-10);
0168 
0169   // Hexagon with vertices forming a regular hexagon centered at origin with
0170   // radius 2
0171   std::vector<vec2> hexVertices;
0172   for (int i = 0; i < 6; ++i) {
0173     double angle = i * std::numbers::pi / 3.0;  // 60 degrees in radians
0174     hexVertices.push_back({2.0 * std::cos(angle), 2.0 * std::sin(angle)});
0175   }
0176   polyDyn hexagon(hexVertices);
0177   vec2 hexCenter = hexagon.center();
0178   BOOST_CHECK_SMALL(hexCenter.x(), 1e-10);  // Should be approximately 0
0179   BOOST_CHECK_SMALL(hexCenter.y(), 1e-10);  // Should be approximately 0
0180 }
0181 
0182 BOOST_AUTO_TEST_SUITE_END()
0183 
0184 }  // namespace ActsTests