Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-17 07:36:25

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   {
0036     BOOST_CHECK_THROW(poly<4> quad(vertices), std::logic_error);
0037   }
0038 
0039   vertices = {{0, 0}, {1, 0}, {0.8, 0.8}, {0, 1}};
0040   {
0041     // wrong number of vertices
0042     BOOST_CHECK_THROW(poly<3> trip{vertices}, AssertionFailureException);
0043   }
0044   {
0045     poly<4> quad(vertices);
0046   }
0047 
0048   // this one is self intersecting
0049   vertices = {{0, 0}, {1, 0}, {0.5, 1}, {0.9, 1.2}};
0050   {
0051     BOOST_CHECK_THROW(poly<4> quad{vertices}, std::logic_error);
0052   }
0053 
0054   // this one is not
0055   vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0056   {
0057     poly<4> quad(vertices);
0058   }
0059 
0060   vertices = {{0, 0}, {1, 0}, {0.8, 0.5}, {1, 1}, {0, 1}};
0061   {
0062     BOOST_CHECK_THROW(poly<5> pent{vertices}, std::logic_error);
0063   }
0064 
0065   vertices = {{0, 0}, {1, 0}, {1.1, 0.5}, {1, 1}, {0, 1}};
0066   {
0067     poly<5> pent(vertices);
0068   }
0069 }
0070 
0071 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsConstruction) {
0072   std::vector<vec2> vertices;
0073 
0074   // triangle
0075   vertices = {{0, 0}, {1, 0}, {0.5, 1}};
0076   poly<3> triangle(vertices);
0077 
0078   RectangleBounds bb = triangle.boundingBox();
0079   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0080   BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
0081 
0082   BoundaryTolerance tolerance = BoundaryTolerance::None();
0083 
0084   BOOST_CHECK(triangle.inside({0.2, 0.2}, tolerance));
0085   BOOST_CHECK(!triangle.inside({0.4, 0.9}, tolerance));
0086   BOOST_CHECK(!triangle.inside({0.8, 0.8}, tolerance));
0087   BOOST_CHECK(!triangle.inside({0.3, -0.2}, tolerance));
0088 
0089   // rectangular poly
0090   vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0091   poly<4> quad(vertices);
0092 
0093   bb = quad.boundingBox();
0094   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0095   BOOST_CHECK_EQUAL(bb.max(), Vector2(1, 1.2));
0096 
0097   BOOST_CHECK(quad.inside({0.2, 0.2}, tolerance));
0098   BOOST_CHECK(!quad.inside({0.4, 0.9}, tolerance));
0099   BOOST_CHECK(quad.inside({0.8, 0.8}, tolerance));
0100   BOOST_CHECK(!quad.inside({0.3, -0.2}, tolerance));
0101 }
0102 
0103 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsRecreation) {
0104   // rectangular poly
0105   std::vector<vec2> vertices = {{0, 0}, {1, 0}, {0.9, 1.2}, {0.5, 1}};
0106   poly<4> original(vertices);
0107 
0108   auto valvector = original.values();
0109   std::array<double, poly<4>::eSize> values{};
0110   std::copy_n(valvector.begin(), poly<4>::eSize, values.begin());
0111   poly<4> recreated(values);
0112   BOOST_CHECK_EQUAL(original, recreated);
0113 
0114   // Get the vertices back
0115   auto rvertices = original.vertices();
0116   BOOST_CHECK_EQUAL(rvertices.size(), 4u);
0117 }
0118 
0119 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsDynamicTest) {
0120   using poly = ConvexPolygonBounds<PolygonDynamic>;
0121 
0122   std::vector<vec2> vertices;
0123 
0124   // triangle
0125   vertices = {{0, 0}, {1, 0}, {0.5, 1}};
0126   poly triangle(vertices);
0127 
0128   // Too few vertices
0129   vertices = {{0, 0}, {1, 1}};
0130   BOOST_CHECK_THROW(poly{vertices}, std::invalid_argument);
0131 
0132   RectangleBounds bb = triangle.boundingBox();
0133   BOOST_CHECK_EQUAL(bb.min(), Vector2(0, 0));
0134   BOOST_CHECK_EQUAL(bb.max(), Vector2(1., 1));
0135 
0136   BoundaryTolerance tolerance = BoundaryTolerance::None();
0137 
0138   BOOST_CHECK(triangle.inside({0.2, 0.2}, tolerance));
0139   BOOST_CHECK(!triangle.inside({0.4, 0.9}, tolerance));
0140   BOOST_CHECK(!triangle.inside({0.8, 0.8}, tolerance));
0141   BOOST_CHECK(!triangle.inside({0.3, -0.2}, tolerance));
0142 }
0143 
0144 BOOST_AUTO_TEST_CASE(ConvexPolygonBoundsCenterTest) {
0145   // Test center calculation for fixed-size polygons
0146 
0147   // Triangle with vertices at (0,0), (3,0), (1.5,3)
0148   // Expected center: (1.5, 1.0)
0149   std::vector<vec2> triangleVertices = {{0, 0}, {3, 0}, {1.5, 3}};
0150   poly<3> triangle(triangleVertices);
0151   vec2 triangleCenter = triangle.center();
0152   BOOST_CHECK_CLOSE(triangleCenter.x(), 1.5, 1e-10);
0153   BOOST_CHECK_CLOSE(triangleCenter.y(), 1.0, 1e-10);
0154 
0155   // Square with vertices at (0,0), (2,0), (2,2), (0,2)
0156   // Expected center: (1.0, 1.0)
0157   std::vector<vec2> squareVertices = {{0, 0}, {2, 0}, {2, 2}, {0, 2}};
0158   poly<4> square(squareVertices);
0159   vec2 squareCenter = square.center();
0160   BOOST_CHECK_CLOSE(squareCenter.x(), 1.0, 1e-10);
0161   BOOST_CHECK_CLOSE(squareCenter.y(), 1.0, 1e-10);
0162 
0163   // Pentagon with vertices at (0,0), (2,0), (3,1.5), (1,3), (-1,1.5)
0164   // Expected center: (1.0, 1.2)
0165   std::vector<vec2> pentVertices = {
0166       {0, 0}, {2, 0}, {3, 1.5}, {1, 3}, {-1, 1.5}};
0167   poly<5> pentagon(pentVertices);
0168   vec2 pentCenter = pentagon.center();
0169   BOOST_CHECK_CLOSE(pentCenter.x(), 1.0, 1e-10);
0170   BOOST_CHECK_CLOSE(pentCenter.y(), 1.2, 1e-10);
0171 
0172   // Test center calculation for dynamic polygons
0173   using polyDyn = ConvexPolygonBounds<PolygonDynamic>;
0174 
0175   // Triangle (same as above)
0176   polyDyn triangleDyn(triangleVertices);
0177   vec2 triangleCenterDyn = triangleDyn.center();
0178   BOOST_CHECK_CLOSE(triangleCenterDyn.x(), 1.5, 1e-10);
0179   BOOST_CHECK_CLOSE(triangleCenterDyn.y(), 1.0, 1e-10);
0180 
0181   // Hexagon with vertices forming a regular hexagon centered at origin with
0182   // radius 2
0183   std::vector<vec2> hexVertices;
0184   for (int i = 0; i < 6; ++i) {
0185     double angle = i * std::numbers::pi / 3.0;  // 60 degrees in radians
0186     hexVertices.push_back({2.0 * std::cos(angle), 2.0 * std::sin(angle)});
0187   }
0188   polyDyn hexagon(hexVertices);
0189   vec2 hexCenter = hexagon.center();
0190   BOOST_CHECK_SMALL(hexCenter.x(), 1e-10);  // Should be approximately 0
0191   BOOST_CHECK_SMALL(hexCenter.y(), 1e-10);  // Should be approximately 0
0192 }
0193 
0194 BOOST_AUTO_TEST_SUITE_END()
0195 
0196 }  // namespace ActsTests