Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-16 07:37:13

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/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/Definitions/Units.hpp"
0016 #include "Acts/Geometry/Extent.hpp"
0017 #include "Acts/Geometry/GeometryContext.hpp"
0018 #include "Acts/Geometry/Polyhedron.hpp"
0019 #include "Acts/Surfaces/CylinderBounds.hpp"
0020 #include "Acts/Surfaces/CylinderSurface.hpp"
0021 #include "Acts/Surfaces/Surface.hpp"
0022 #include "Acts/Surfaces/SurfaceBounds.hpp"
0023 #include "Acts/Surfaces/SurfaceMergingException.hpp"
0024 #include "Acts/Utilities/Intersection.hpp"
0025 #include "Acts/Utilities/Logger.hpp"
0026 #include "Acts/Utilities/Result.hpp"
0027 #include "Acts/Utilities/ThrowAssert.hpp"
0028 #include "Acts/Utilities/detail/periodic.hpp"
0029 #include "ActsTests/CommonHelpers/DetectorElementStub.hpp"
0030 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0031 
0032 #include <cmath>
0033 #include <memory>
0034 #include <numbers>
0035 #include <string>
0036 
0037 using namespace Acts;
0038 using namespace Acts::UnitLiterals;
0039 
0040 namespace ActsTests {
0041 
0042 auto logger = Acts::getDefaultLogger("UnitTests", Acts::Logging::VERBOSE);
0043 
0044 // Create a test context
0045 GeometryContext testContext = GeometryContext::dangerouslyDefaultConstruct();
0046 
0047 BOOST_AUTO_TEST_SUITE(SurfacesSuite)
0048 /// Unit test for creating compliant/non-compliant CylinderSurface object
0049 BOOST_AUTO_TEST_CASE(CylinderSurfaceConstruction) {
0050   /// Test default construction
0051   // default construction is deleted
0052 
0053   /// Constructor with transform, radius and halfZ
0054   const double radius = 1.;
0055   const double halfZ = 10.;
0056   const double halfPhiSector = std::numbers::pi / 8.;
0057   const Translation3 translation{0., 1., 2.};
0058 
0059   auto pTransform = Transform3(translation);
0060   BOOST_CHECK_EQUAL(
0061       Surface::makeShared<CylinderSurface>(pTransform, radius, halfZ)->type(),
0062       Surface::Cylinder);
0063 
0064   /// Constructor with transform pointer, radius, halfZ and halfPhiSector
0065   BOOST_CHECK_EQUAL(Surface::makeShared<CylinderSurface>(pTransform, radius,
0066                                                          halfZ, halfPhiSector)
0067                         ->type(),
0068                     Surface::Cylinder);
0069 
0070   /// Constructor with transform and CylinderBounds pointer
0071   auto pCylinderBounds = std::make_shared<const CylinderBounds>(radius, halfZ);
0072   BOOST_CHECK_EQUAL(
0073       Surface::makeShared<CylinderSurface>(pTransform, pCylinderBounds)->type(),
0074       Surface::Cylinder);
0075 
0076   /// Copy constructor
0077   auto cylinderSurfaceObject =
0078       Surface::makeShared<CylinderSurface>(pTransform, radius, halfZ);
0079   auto copiedCylinderSurface =
0080       Surface::makeShared<CylinderSurface>(*cylinderSurfaceObject);
0081   BOOST_CHECK_EQUAL(copiedCylinderSurface->type(), Surface::Cylinder);
0082   BOOST_CHECK(*copiedCylinderSurface == *cylinderSurfaceObject);
0083 
0084   /// Copied and transformed
0085   auto copiedTransformedCylinderSurface = Surface::makeShared<CylinderSurface>(
0086       testContext, *cylinderSurfaceObject, pTransform);
0087   BOOST_CHECK_EQUAL(copiedTransformedCylinderSurface->type(),
0088                     Surface::Cylinder);
0089 
0090   /// Construct with nullptr bounds
0091   BOOST_CHECK_THROW(auto nullBounds = Surface::makeShared<CylinderSurface>(
0092                         Transform3::Identity(), nullptr),
0093                     AssertionFailureException);
0094 }
0095 
0096 /// Unit test for testing CylinderSurface properties
0097 BOOST_AUTO_TEST_CASE(CylinderSurfaceProperties) {
0098   /// Test clone method
0099   const double radius = 1.;
0100   const double halfZ = 10.;
0101   const Translation3 translation{0., 1., 2.};
0102 
0103   auto pTransform = Transform3(translation);
0104   auto cylinderSurfaceObject =
0105       Surface::makeShared<CylinderSurface>(pTransform, radius, halfZ);
0106 
0107   /// Test type (redundant)
0108   BOOST_CHECK_EQUAL(cylinderSurfaceObject->type(), Surface::Cylinder);
0109 
0110   /// Test referencePosition
0111   Vector3 referencePosition{0., 1., 2.};
0112   CHECK_CLOSE_ABS(cylinderSurfaceObject->referencePosition(
0113                       testContext, AxisDirection::AxisPhi),
0114                   referencePosition, 1e-9);
0115 
0116   /// Test referenceFrame
0117   const double invSqrt2 = 1. / std::numbers::sqrt2;
0118   Vector3 globalPosition{invSqrt2, 1. - invSqrt2, 0.};
0119   Vector3 globalPositionZ{invSqrt2, 1. - invSqrt2, 2.};
0120   Vector3 momentum{15., 15., 15.};
0121   Vector3 momentum2{6.6, -3., 2.};
0122   RotationMatrix3 expectedFrame;
0123   expectedFrame << invSqrt2, 0., invSqrt2, invSqrt2, 0., -invSqrt2, 0., 1., 0.;
0124   // check without shift
0125   CHECK_CLOSE_OR_SMALL(cylinderSurfaceObject->referenceFrame(
0126                            testContext, globalPosition, momentum),
0127                        expectedFrame, 1e-6, 1e-9);
0128   // check with shift and different momentum
0129   CHECK_CLOSE_OR_SMALL(cylinderSurfaceObject->referenceFrame(
0130                            testContext, globalPositionZ, momentum2),
0131                        expectedFrame, 1e-6, 1e-9);
0132 
0133   /// Test normal, given 3D position
0134   Vector3 origin{0., 0., 0.};
0135   Vector3 normal3D = {0., -1., 0.};
0136   CHECK_CLOSE_ABS(cylinderSurfaceObject->normal(testContext, origin), normal3D,
0137                   1e-9);
0138 
0139   Vector3 pos45deg = {invSqrt2, 1 + invSqrt2, 0.};
0140   Vector3 pos45degZ = {invSqrt2, 1 + invSqrt2, 4.};
0141   Vector3 normal45deg = {invSqrt2, invSqrt2, 0.};
0142   // test the normal vector
0143   CHECK_CLOSE_ABS(cylinderSurfaceObject->normal(testContext, pos45deg),
0144                   normal45deg, 1e-6 * invSqrt2);
0145   // test that the normal vector is independent of z coordinate
0146   CHECK_CLOSE_ABS(cylinderSurfaceObject->normal(testContext, pos45degZ),
0147                   normal45deg, 1e-6 * invSqrt2);
0148 
0149   /// Test normal given 2D rphi position
0150   Vector2 positionPiBy2(1., 0.);
0151   Vector3 normalAtPiBy2{std::cos(1.), std::sin(1.), 0.};
0152   CHECK_CLOSE_ABS(cylinderSurfaceObject->normal(testContext, positionPiBy2),
0153                   normalAtPiBy2, 1e-9);
0154 
0155   /// Test rotational symmetry axis
0156   Vector3 symmetryAxis{0., 0., 1.};
0157   CHECK_CLOSE_ABS(cylinderSurfaceObject->rotSymmetryAxis(testContext),
0158                   symmetryAxis, 1e-9);
0159 
0160   /// Test bounds
0161   BOOST_CHECK_EQUAL(cylinderSurfaceObject->bounds().type(),
0162                     SurfaceBounds::eCylinder);
0163 
0164   /// Test localToGlobal
0165   Vector2 localPosition{0., 0.};
0166   globalPosition = cylinderSurfaceObject->localToGlobal(
0167       testContext, localPosition, momentum);
0168   Vector3 expectedPosition{1, 1, 2};
0169   BOOST_CHECK_EQUAL(globalPosition, expectedPosition);
0170 
0171   /// Testing globalToLocal
0172   localPosition = cylinderSurfaceObject
0173                       ->globalToLocal(testContext, globalPosition, momentum)
0174                       .value();
0175   Vector2 expectedLocalPosition{0., 0.};
0176   BOOST_CHECK_EQUAL(localPosition, expectedLocalPosition);
0177 
0178   /// Test isOnSurface
0179   Vector3 offSurface{100, 1, 2};
0180   BOOST_CHECK(cylinderSurfaceObject->isOnSurface(
0181       testContext, globalPosition, momentum, BoundaryTolerance::None()));
0182   BOOST_CHECK(cylinderSurfaceObject->isOnSurface(testContext, globalPosition,
0183                                                  BoundaryTolerance::None()));
0184   BOOST_CHECK(!cylinderSurfaceObject->isOnSurface(
0185       testContext, offSurface, momentum, BoundaryTolerance::None()));
0186   BOOST_CHECK(!cylinderSurfaceObject->isOnSurface(testContext, offSurface,
0187                                                   BoundaryTolerance::None()));
0188 
0189   /// Intersection test
0190   Vector3 direction{-1., 0, 0};
0191   auto sfIntersection = cylinderSurfaceObject->intersect(
0192       testContext, offSurface, direction, BoundaryTolerance::Infinite());
0193   Intersection3D expectedIntersect{Vector3{1, 1, 2}, 99.,
0194                                    IntersectionStatus::reachable};
0195   BOOST_CHECK(sfIntersection[0].isValid());
0196   CHECK_CLOSE_ABS(sfIntersection[0].position(), expectedIntersect.position(),
0197                   1e-9);
0198   CHECK_CLOSE_ABS(sfIntersection[0].pathLength(),
0199                   expectedIntersect.pathLength(), 1e-9);
0200   // there is a second solution & and it should be valid
0201   BOOST_CHECK(sfIntersection[1].isValid());
0202   // And it's path should be further away then the primary solution
0203   double pn = sfIntersection[0].pathLength();
0204   double pa = sfIntersection[1].pathLength();
0205   BOOST_CHECK_LT(std::abs(pn), std::abs(pa));
0206 
0207   /// Test pathCorrection
0208   CHECK_CLOSE_REL(cylinderSurfaceObject->pathCorrection(testContext, offSurface,
0209                                                         momentum.normalized()),
0210                   std::numbers::sqrt3, 0.01);
0211 
0212   /// Test name
0213   BOOST_CHECK_EQUAL(cylinderSurfaceObject->name(),
0214                     std::string("Acts::CylinderSurface"));
0215 
0216   /// Test dump
0217   boost::test_tools::output_test_stream dumpOutput;
0218   std::string expected =
0219       "Acts::CylinderSurface\n\
0220      Center position  (x, y, z) = (0.0000, 1.0000, 2.0000)\n\
0221      Rotation:             colX = (1.000000, 0.000000, 0.000000)\n\
0222                            colY = (0.000000, 1.000000, 0.000000)\n\
0223                            colZ = (0.000000, 0.000000, 1.000000)\n\
0224      Bounds  : Acts::CylinderBounds: (radius, halfLengthZ, halfPhiSector, averagePhi, bevelMinZ, bevelMaxZ) = (1.0000000, 10.0000000, 3.1415927, 0.0000000, 0.0000000, 0.0000000)";
0225   dumpOutput << cylinderSurfaceObject->toStream(testContext);
0226   BOOST_CHECK(dumpOutput.is_equal(expected));
0227 }
0228 
0229 BOOST_AUTO_TEST_CASE(CylinderSurfaceEqualityOperators) {
0230   const double radius = 1.;
0231   const double halfZ = 10.;
0232   const Translation3 translation{0., 1., 2.};
0233 
0234   auto pTransform = Transform3(translation);
0235   auto cylinderSurfaceObject =
0236       Surface::makeShared<CylinderSurface>(pTransform, radius, halfZ);
0237 
0238   auto cylinderSurfaceObject2 =
0239       Surface::makeShared<CylinderSurface>(pTransform, radius, halfZ);
0240 
0241   /// Test equality operator
0242   BOOST_CHECK(*cylinderSurfaceObject == *cylinderSurfaceObject2);
0243 
0244   BOOST_TEST_CHECKPOINT(
0245       "Create and then assign a CylinderSurface object to the existing one");
0246   /// Test assignment
0247   auto assignedCylinderSurface =
0248       Surface::makeShared<CylinderSurface>(Transform3::Identity(), 6.6, 5.4);
0249   *assignedCylinderSurface = *cylinderSurfaceObject;
0250   /// Test equality of assigned to original
0251   BOOST_CHECK(*assignedCylinderSurface == *cylinderSurfaceObject);
0252 }
0253 
0254 /// Unit test for testing CylinderSurface properties
0255 BOOST_AUTO_TEST_CASE(CylinderSurfaceExtent) {
0256   using enum AxisDirection;
0257 
0258   // Some radius and half length
0259   const double radius = 1.;
0260   const double halfZ = 10.;
0261   const Translation3 translation{0., 0., 2.};  // != {0., 1., 2.}
0262 
0263   auto pTransform = Transform3(translation);
0264   auto cylinderSurface =
0265       Surface::makeShared<CylinderSurface>(pTransform, radius, halfZ);
0266   // The Extent, let's measure it
0267   auto cylinderExtent =
0268       cylinderSurface->polyhedronRepresentation(testContext, 1).extent();
0269 
0270   CHECK_CLOSE_ABS(-8, cylinderExtent.min(AxisZ), s_onSurfaceTolerance);
0271   CHECK_CLOSE_ABS(12, cylinderExtent.max(AxisZ), s_onSurfaceTolerance);
0272   CHECK_CLOSE_ABS(radius, cylinderExtent.min(AxisR), s_onSurfaceTolerance);
0273   CHECK_CLOSE_ABS(radius, cylinderExtent.max(AxisR), s_onSurfaceTolerance);
0274   CHECK_CLOSE_ABS(-radius, cylinderExtent.min(AxisX), s_onSurfaceTolerance);
0275   CHECK_CLOSE_ABS(radius, cylinderExtent.max(AxisX), s_onSurfaceTolerance);
0276   CHECK_CLOSE_ABS(-radius, cylinderExtent.min(AxisY), s_onSurfaceTolerance);
0277   CHECK_CLOSE_ABS(radius, cylinderExtent.max(AxisY), s_onSurfaceTolerance);
0278 }
0279 
0280 /// Unit test for testing CylinderSurface alignment derivatives
0281 BOOST_AUTO_TEST_CASE(CylinderSurfaceAlignment) {
0282   const double radius = 1.;
0283   const double halfZ = 10.;
0284   const Translation3 translation{0., 1., 2.};
0285 
0286   auto pTransform = Transform3(translation);
0287   auto cylinderSurfaceObject =
0288       Surface::makeShared<CylinderSurface>(pTransform, radius, halfZ);
0289 
0290   const auto& rotation = pTransform.rotation();
0291   // The local frame z axis
0292   const Vector3 localZAxis = rotation.col(2);
0293   // Check the local z axis is aligned to global z axis
0294   CHECK_CLOSE_ABS(localZAxis, Vector3(0., 0., 1.), 1e-15);
0295 
0296   /// Define the track (global) position and direction
0297   Vector3 globalPosition{0, 2, 2};
0298 
0299   // Test the derivative of bound track parameters local position w.r.t.
0300   // position in local 3D Cartesian coordinates
0301   const auto& loc3DToLocBound =
0302       cylinderSurfaceObject->localCartesianToBoundLocalDerivative(
0303           testContext, globalPosition);
0304   // Check if the result is as expected
0305   Matrix<2, 3> expLoc3DToLocBound = Matrix<2, 3>::Zero();
0306   expLoc3DToLocBound << -1, 0, 0, 0, 0, 1;
0307   CHECK_CLOSE_ABS(loc3DToLocBound, expLoc3DToLocBound, 1e-10);
0308 }
0309 
0310 BOOST_AUTO_TEST_CASE(CylinderSurfaceBinningPosition) {
0311   using namespace Acts::UnitLiterals;
0312   Vector3 s{5_mm, 7_mm, 10_cm};
0313   Transform3 trf;
0314   trf = Translation3(s) * AngleAxis3{0.5, Vector3::UnitZ()};
0315 
0316   double r = 300;
0317   double halfZ = 330;
0318   double averagePhi = 0.1;
0319 
0320   auto bounds = std::make_shared<CylinderBounds>(r, halfZ, std::numbers::pi / 8,
0321                                                  averagePhi);
0322   auto cylinder = Acts::Surface::makeShared<CylinderSurface>(trf, bounds);
0323 
0324   Vector3 exp = Vector3{r * std::cos(averagePhi), r * std::sin(averagePhi), 0};
0325   exp = trf * exp;
0326 
0327   Vector3 bp = cylinder->referencePosition(testContext, AxisDirection::AxisR);
0328   CHECK_CLOSE_ABS(bp, exp, 1e-10);
0329   CHECK_CLOSE_ABS(
0330       cylinder->referencePositionValue(testContext, AxisDirection::AxisR),
0331       VectorHelpers::perp(exp), 1e-10);
0332 
0333   bp = cylinder->referencePosition(testContext, AxisDirection::AxisRPhi);
0334   CHECK_CLOSE_ABS(bp, exp, 1e-10);
0335   CHECK_CLOSE_ABS(
0336       cylinder->referencePositionValue(testContext, AxisDirection::AxisRPhi),
0337       VectorHelpers::phi(exp) * VectorHelpers::perp(exp), 1e-10);
0338 
0339   for (auto b : {AxisDirection::AxisX, AxisDirection::AxisY,
0340                  AxisDirection::AxisZ, AxisDirection::AxisEta,
0341                  AxisDirection::AxisTheta, AxisDirection::AxisMag}) {
0342     BOOST_TEST_CONTEXT("binValue: " << b) {
0343       BOOST_CHECK_EQUAL(cylinder->referencePosition(testContext, b),
0344                         cylinder->center(testContext));
0345     }
0346   }
0347 }
0348 
0349 BOOST_AUTO_TEST_SUITE(CylinderSurfaceMerging)
0350 
0351 BOOST_AUTO_TEST_CASE(InvalidDetectorElement) {
0352   DetectorElementStub detElem;
0353 
0354   auto bounds = std::make_shared<CylinderBounds>(100_mm, 100_mm);
0355   auto cyl1 = Surface::makeShared<CylinderSurface>(bounds, detElem);
0356   auto cyl2 = Surface::makeShared<CylinderSurface>(bounds, detElem);
0357 
0358   BOOST_CHECK_THROW(
0359       cyl1->mergedWith(*cyl2, Acts::AxisDirection::AxisR, false, *logger),
0360       SurfaceMergingException);
0361 }
0362 
0363 BOOST_DATA_TEST_CASE(IncompatibleZDirection,
0364                      (boost::unit_test::data::xrange(-135, 180, 45) *
0365                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
0366                                                    Vector3{20_mm, 0_mm, 0_mm},
0367                                                    Vector3{0_mm, 20_mm, 0_mm},
0368                                                    Vector3{20_mm, 20_mm, 0_mm},
0369                                                    Vector3{0_mm, 0_mm, 20_mm})),
0370                      angle, offset) {
0371   Logging::ScopedFailureThreshold ft{Logging::FATAL};
0372 
0373   Transform3 base =
0374       AngleAxis3(angle * 1_degree, Vector3::UnitX()) * Translation3(offset);
0375 
0376   auto cyl = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm);
0377   auto cyl2 = Surface::makeShared<CylinderSurface>(
0378       base * Translation3{Vector3::UnitZ() * 200_mm}, 30_mm, 100_mm);
0379 
0380   BOOST_CHECK_THROW(
0381       cyl->mergedWith(*cyl2, Acts::AxisDirection::AxisPhi, false, *logger),
0382       SurfaceMergingException);
0383 
0384   auto cylShiftedXy = Surface::makeShared<CylinderSurface>(
0385       base * Translation3{Vector3{1_mm, 2_mm, 200_mm}}, 30_mm, 100_mm);
0386   BOOST_CHECK_THROW(cyl->mergedWith(*cylShiftedXy, Acts::AxisDirection::AxisZ,
0387                                     false, *logger),
0388                     SurfaceMergingException);
0389 
0390   auto cylRotatedX = Surface::makeShared<CylinderSurface>(
0391       base * AngleAxis3{10_degree, Vector3::UnitX()} *
0392           Translation3{Vector3::UnitZ() * 200_mm},
0393       30_mm, 100_mm);
0394   BOOST_CHECK_THROW(
0395       cyl->mergedWith(*cylRotatedX, Acts::AxisDirection::AxisZ, false, *logger),
0396       SurfaceMergingException);
0397 
0398   // Cylinder with different radius
0399   auto cyl3 = Surface::makeShared<CylinderSurface>(
0400       base * Translation3{Vector3::UnitZ() * 200_mm}, 35_mm, 100_mm);
0401   BOOST_CHECK_THROW(
0402       cyl->mergedWith(*cyl3, Acts::AxisDirection::AxisZ, false, *logger),
0403       SurfaceMergingException);
0404 
0405   // Cylinder with bevel
0406   auto cyl4 = Surface::makeShared<CylinderSurface>(
0407       base * Translation3{Vector3::UnitZ() * 200_mm}, 30_mm, 100_mm,
0408       std::numbers::pi, 0, std::numbers::pi / 8.);
0409   BOOST_CHECK_THROW(
0410       cyl->mergedWith(*cyl4, Acts::AxisDirection::AxisZ, false, *logger),
0411       SurfaceMergingException);
0412 
0413   auto cyl5 = Surface::makeShared<CylinderSurface>(
0414       base * Translation3{Vector3::UnitZ() * 200_mm}, 30_mm, 100_mm,
0415       std::numbers::pi, 0, 0, std::numbers::pi / 8.);
0416   BOOST_CHECK_THROW(
0417       cyl->mergedWith(*cyl5, Acts::AxisDirection::AxisZ, false, *logger),
0418       SurfaceMergingException);
0419 
0420   // Cylinder with overlap in z
0421   auto cyl6 = Surface::makeShared<CylinderSurface>(
0422       base * Translation3{Vector3::UnitZ() * 150_mm}, 30_mm, 100_mm);
0423   BOOST_CHECK_THROW(
0424       cyl->mergedWith(*cyl6, Acts::AxisDirection::AxisZ, false, *logger),
0425       SurfaceMergingException);
0426 
0427   // Cylinder with gap in z
0428   auto cyl7 = Surface::makeShared<CylinderSurface>(
0429       base * Translation3{Vector3::UnitZ() * 250_mm}, 30_mm, 100_mm);
0430   BOOST_CHECK_THROW(
0431       cyl->mergedWith(*cyl7, Acts::AxisDirection::AxisZ, false, *logger),
0432       SurfaceMergingException);
0433 
0434   // Cylinder with phi sector and relative z rotation
0435   auto cyl8 = Surface::makeShared<CylinderSurface>(
0436       base * AngleAxis3(14_degree, Vector3::UnitZ()) *
0437           Translation3{Vector3::UnitZ() * 200_mm},
0438       30_mm, 100_mm, 10_degree, 40_degree);
0439   BOOST_CHECK_THROW(
0440       cyl->mergedWith(*cyl8, Acts::AxisDirection::AxisZ, false, *logger),
0441       SurfaceMergingException);
0442 
0443   auto cylPhi1 = Surface::makeShared<CylinderSurface>(Transform3::Identity(),
0444                                                       30_mm, 100_mm, 45_degree);
0445   auto cylPhi2 = Surface::makeShared<CylinderSurface>(
0446       Transform3{Translation3{Vector3::UnitZ() * 150_mm}}, 30_mm, 50_mm,
0447       55_degree);
0448   BOOST_CHECK_THROW(
0449       cylPhi1->mergedWith(*cylPhi2, Acts::AxisDirection::AxisZ, false, *logger),
0450       SurfaceMergingException);
0451 }
0452 
0453 BOOST_DATA_TEST_CASE(ZDirection,
0454                      (boost::unit_test::data::xrange(-135, 180, 45) *
0455                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
0456                                                    Vector3{20_mm, 0_mm, 0_mm},
0457                                                    Vector3{0_mm, 20_mm, 0_mm},
0458                                                    Vector3{20_mm, 20_mm, 0_mm},
0459                                                    Vector3{0_mm, 0_mm, 20_mm})),
0460                      angle, offset) {
0461   Transform3 base =
0462       AngleAxis3(angle * 1_degree, Vector3::UnitX()) * Translation3(offset);
0463 
0464   auto cyl = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm);
0465 
0466   auto cyl2 = Surface::makeShared<CylinderSurface>(
0467       base * AngleAxis3(14_degree, Vector3::UnitZ()) *
0468           Translation3{Vector3::UnitZ() * 200_mm},
0469       30_mm, 100_mm);
0470 
0471   auto [cyl3, reversed] =
0472       cyl->mergedWith(*cyl2, Acts::AxisDirection::AxisZ, false, *logger);
0473   BOOST_REQUIRE_NE(cyl3, nullptr);
0474   BOOST_CHECK(!reversed);
0475 
0476   auto [cyl3Reversed, reversed2] =
0477       cyl2->mergedWith(*cyl, Acts::AxisDirection::AxisZ, false, *logger);
0478   BOOST_REQUIRE_NE(cyl3Reversed, nullptr);
0479   BOOST_CHECK(cyl3->bounds() == cyl3Reversed->bounds());
0480   BOOST_CHECK(reversed2);
0481 
0482   auto bounds = cyl3->bounds();
0483 
0484   BOOST_CHECK_EQUAL(bounds.get(CylinderBounds::eR), 30_mm);
0485   BOOST_CHECK_EQUAL(bounds.get(CylinderBounds::eHalfLengthZ), 200_mm);
0486   BOOST_CHECK_EQUAL(bounds.get(CylinderBounds::eAveragePhi), 0_degree);
0487   BOOST_CHECK_EQUAL(bounds.get(CylinderBounds::eHalfPhiSector), 180_degree);
0488 
0489   // Rotation in z depends on the ordering, the left side "wins"
0490   Transform3 expected12 = base * Translation3{Vector3::UnitZ() * 100_mm};
0491   BOOST_CHECK_EQUAL(expected12.matrix(),
0492                     cyl3->localToGlobalTransform(testContext).matrix());
0493 
0494   Transform3 expected21 = base * AngleAxis3(14_degree, Vector3::UnitZ()) *
0495                           Translation3{Vector3::UnitZ() * 100_mm};
0496   CHECK_CLOSE_OR_SMALL(
0497       cyl3Reversed->localToGlobalTransform(testContext).matrix(),
0498       expected21.matrix(), 1e-6, 1e-10);
0499 
0500   auto cylPhi1 = Surface::makeShared<CylinderSurface>(Transform3::Identity(),
0501                                                       30_mm, 100_mm, 45_degree);
0502   auto cylPhi2 = Surface::makeShared<CylinderSurface>(
0503       Transform3{Translation3{Vector3::UnitZ() * 150_mm}}, 30_mm, 50_mm,
0504       45_degree);
0505 
0506   auto [cylPhi12, reversedPhy12] =
0507       cylPhi1->mergedWith(*cylPhi2, Acts::AxisDirection::AxisZ, false, *logger);
0508 
0509   BOOST_REQUIRE_NE(cylPhi12, nullptr);
0510   auto boundsPhi12 = cylPhi12->bounds();
0511   BOOST_CHECK_EQUAL(boundsPhi12.get(CylinderBounds::eR), 30_mm);
0512   BOOST_CHECK_EQUAL(boundsPhi12.get(CylinderBounds::eHalfLengthZ), 150_mm);
0513   BOOST_CHECK_EQUAL(boundsPhi12.get(CylinderBounds::eAveragePhi), 0_degree);
0514   BOOST_CHECK_EQUAL(boundsPhi12.get(CylinderBounds::eHalfPhiSector), 45_degree);
0515 }
0516 
0517 BOOST_DATA_TEST_CASE(IncompatibleRPhiDirection,
0518                      (boost::unit_test::data::xrange(-135, 180, 45) *
0519                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
0520                                                    Vector3{20_mm, 0_mm, 0_mm},
0521                                                    Vector3{0_mm, 20_mm, 0_mm},
0522                                                    Vector3{20_mm, 20_mm, 0_mm},
0523                                                    Vector3{0_mm, 0_mm, 20_mm}) *
0524                       boost::unit_test::data::xrange(-1300, 1300, 104)),
0525                      angle, offset, phiShift) {
0526   Logging::ScopedFailureThreshold ft{Logging::FATAL};
0527   Transform3 base =
0528       AngleAxis3(angle * 1_degree, Vector3::UnitX()) * Translation3(offset);
0529 
0530   auto a = [phiShift](double v) {
0531     return detail::radian_sym(v + phiShift * 1_degree);
0532   };
0533 
0534   auto cylPhi = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0535                                                      10_degree, a(40_degree));
0536 
0537   // Cylinder with overlap in phi
0538   auto cylPhi2 = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0539                                                       45_degree, a(85_degree));
0540   BOOST_CHECK_THROW(cylPhi->mergedWith(*cylPhi2, Acts::AxisDirection::AxisRPhi,
0541                                        false, *logger),
0542                     SurfaceMergingException);
0543 
0544   // Cylinder with gap in phi
0545   auto cylPhi3 = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0546                                                       45_degree, a(105_degree));
0547   BOOST_CHECK_THROW(cylPhi->mergedWith(*cylPhi3, Acts::AxisDirection::AxisRPhi,
0548                                        false, *logger),
0549                     SurfaceMergingException);
0550 
0551   // Cylinder with a z shift
0552   auto cylPhi4 = Surface::makeShared<CylinderSurface>(
0553       base * Translation3{Vector3::UnitZ() * 20_mm}, 30_mm, 100_mm, 45_degree,
0554       a(95_degree));
0555   BOOST_CHECK_THROW(cylPhi->mergedWith(*cylPhi4, Acts::AxisDirection::AxisRPhi,
0556                                        false, *logger),
0557                     SurfaceMergingException);
0558 
0559   // Test phi sector with different z halflengths
0560   auto cylPhi5 = Surface::makeShared<CylinderSurface>(base, 30_mm, 110_mm,
0561                                                       45_degree, a(95_degree));
0562   BOOST_CHECK_THROW(cylPhi->mergedWith(*cylPhi5, Acts::AxisDirection::AxisRPhi,
0563                                        false, *logger),
0564                     SurfaceMergingException);
0565 }
0566 
0567 BOOST_DATA_TEST_CASE(RPhiDirection,
0568                      (boost::unit_test::data::xrange(-135, 180, 45) *
0569                       boost::unit_test::data::make(Vector3{0_mm, 0_mm, 0_mm},
0570                                                    Vector3{20_mm, 0_mm, 0_mm},
0571                                                    Vector3{0_mm, 20_mm, 0_mm},
0572                                                    Vector3{20_mm, 20_mm, 0_mm},
0573                                                    Vector3{0_mm, 0_mm, 20_mm}) *
0574                       boost::unit_test::data::xrange(-1300, 1300, 104)),
0575                      angle, offset, phiShift) {
0576   Transform3 base =
0577       AngleAxis3(angle * 1_degree, Vector3::UnitX()) * Translation3(offset);
0578 
0579   auto a = [phiShift](double v) {
0580     return detail::radian_sym(v + phiShift * 1_degree);
0581   };
0582 
0583   BOOST_TEST_CONTEXT("Internal rotation") {
0584     auto cyl = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0585                                                     10_degree, a(40_degree));
0586     auto cyl2 = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0587                                                      45_degree, a(95_degree));
0588 
0589     auto [cyl3, reversed] =
0590         cyl->mergedWith(*cyl2, Acts::AxisDirection::AxisRPhi, false, *logger);
0591     BOOST_REQUIRE_NE(cyl3, nullptr);
0592     BOOST_CHECK_EQUAL(base.matrix(),
0593                       cyl3->localToGlobalTransform(testContext).matrix());
0594     BOOST_CHECK(reversed);
0595 
0596     auto [cyl3Reversed, reversed2] =
0597         cyl2->mergedWith(*cyl, Acts::AxisDirection::AxisRPhi, false, *logger);
0598     BOOST_REQUIRE_NE(cyl3Reversed, nullptr);
0599     BOOST_CHECK(*cyl3 == *cyl3Reversed);
0600     BOOST_CHECK(!reversed2);
0601 
0602     const auto& bounds = cyl3->bounds();
0603 
0604     BOOST_CHECK_SMALL(
0605         detail::difference_periodic(bounds.get(CylinderBounds::eAveragePhi),
0606                                     a(85_degree), 2 * std::numbers::pi),
0607         1e-6);
0608     BOOST_CHECK_CLOSE(bounds.get(CylinderBounds::eHalfPhiSector), 55_degree,
0609                       0.1);
0610 
0611     auto cyl4 = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0612                                                      20_degree, a(170_degree));
0613     auto cyl5 = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0614                                                      10_degree, a(-160_degree));
0615     auto [cyl45, reversed45] =
0616         cyl4->mergedWith(*cyl5, Acts::AxisDirection::AxisRPhi, false, *logger);
0617     BOOST_REQUIRE_NE(cyl45, nullptr);
0618     BOOST_CHECK_EQUAL(base.matrix(),
0619                       cyl45->localToGlobalTransform(testContext).matrix());
0620     BOOST_CHECK(reversed45);
0621 
0622     auto [cyl54, reversed54] =
0623         cyl5->mergedWith(*cyl4, Acts::AxisDirection::AxisRPhi, false, *logger);
0624     BOOST_REQUIRE_NE(cyl54, nullptr);
0625     BOOST_CHECK(!reversed54);
0626 
0627     BOOST_CHECK(*cyl54 == *cyl45);
0628 
0629     BOOST_CHECK_SMALL(detail::difference_periodic(
0630                           cyl45->bounds().get(CylinderBounds::eAveragePhi),
0631                           a(180_degree), 2 * std::numbers::pi),
0632                       1e-6);
0633     BOOST_CHECK_CLOSE(cyl45->bounds().get(CylinderBounds::eHalfPhiSector),
0634                       30_degree, 1e-6);
0635 
0636     auto cyl6 = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0637                                                      90_degree, a(90_degree));
0638     auto cyl7 = Surface::makeShared<CylinderSurface>(base, 30_mm, 100_mm,
0639                                                      90_degree, a(-90_degree));
0640 
0641     auto [cyl67, reversed67] =
0642         cyl6->mergedWith(*cyl7, Acts::AxisDirection::AxisRPhi, false, *logger);
0643     BOOST_REQUIRE_NE(cyl67, nullptr);
0644     BOOST_CHECK_EQUAL(base.matrix(),
0645                       cyl67->localToGlobalTransform(testContext).matrix());
0646 
0647     auto [cyl76, reversed76] =
0648         cyl7->mergedWith(*cyl6, Acts::AxisDirection::AxisRPhi, false, *logger);
0649     BOOST_REQUIRE_NE(cyl76, nullptr);
0650     BOOST_CHECK_EQUAL(base.matrix(),
0651                       cyl76->localToGlobalTransform(testContext).matrix());
0652 
0653     // The ordering in this case is effectively arbitrary, you get the ordering
0654     // you put in
0655     BOOST_CHECK(!reversed67);
0656     BOOST_CHECK(!reversed76);
0657 
0658     BOOST_CHECK_SMALL(detail::difference_periodic(
0659                           cyl67->bounds().get(CylinderBounds::eAveragePhi),
0660                           a(180_degree), 2 * std::numbers::pi),
0661                       1e-6);
0662     BOOST_CHECK_CLOSE(cyl67->bounds().get(CylinderBounds::eHalfPhiSector),
0663                       180_degree, 1e-6);
0664   }
0665 
0666   BOOST_TEST_CONTEXT("External rotation") {
0667     Transform3 trf1 = base * AngleAxis3(a(40_degree), Vector3::UnitZ());
0668     auto cyl1 = Surface::makeShared<CylinderSurface>(trf1, 30_mm, 100_mm,
0669                                                      10_degree, 0_degree);
0670 
0671     Transform3 trf2 = base * AngleAxis3(a(95_degree), Vector3::UnitZ());
0672     auto cyl2 = Surface::makeShared<CylinderSurface>(trf2, 30_mm, 100_mm,
0673                                                      45_degree, 0_degree);
0674 
0675     auto [cyl3, reversed] =
0676         cyl1->mergedWith(*cyl2, Acts::AxisDirection::AxisRPhi, true, *logger);
0677 
0678     BOOST_REQUIRE_NE(cyl3, nullptr);
0679     Transform3 trfExpected12 =
0680         base * AngleAxis3(a(85_degree), Vector3::UnitZ());
0681     CHECK_CLOSE_OR_SMALL(cyl3->localToGlobalTransform(testContext).matrix(),
0682                          trfExpected12.matrix(), 1e-6, 1e-10);
0683     BOOST_CHECK(reversed);
0684 
0685     BOOST_CHECK_EQUAL(cyl3->bounds().get(CylinderBounds::eAveragePhi), 0);
0686     BOOST_CHECK_CLOSE(cyl3->bounds().get(CylinderBounds::eHalfPhiSector),
0687                       55_degree, 1e-6);
0688 
0689     Transform3 trf4 = base * AngleAxis3(a(170_degree), Vector3::UnitZ());
0690     auto cyl4 = Surface::makeShared<CylinderSurface>(trf4, 30_mm, 100_mm,
0691                                                      20_degree, 0_degree);
0692     Transform3 trf5 = base * AngleAxis3(a(-160_degree), Vector3::UnitZ());
0693     auto cyl5 = Surface::makeShared<CylinderSurface>(trf5, 30_mm, 100_mm,
0694                                                      10_degree, 0_degree);
0695     auto [cyl45, reversed45] =
0696         cyl4->mergedWith(*cyl5, Acts::AxisDirection::AxisRPhi, true, *logger);
0697     BOOST_REQUIRE_NE(cyl45, nullptr);
0698     Transform3 trfExpected45 =
0699         base * AngleAxis3(a(180_degree), Vector3::UnitZ());
0700     CHECK_CLOSE_OR_SMALL(cyl45->localToGlobalTransform(testContext).matrix(),
0701                          trfExpected45.matrix(), 1e-6, 1e-10);
0702     BOOST_CHECK(reversed45);
0703 
0704     auto [cyl54, reversed54] =
0705         cyl5->mergedWith(*cyl4, Acts::AxisDirection::AxisRPhi, true, *logger);
0706     BOOST_REQUIRE_NE(cyl54, nullptr);
0707     BOOST_CHECK(!reversed54);
0708 
0709     BOOST_CHECK(*cyl54 == *cyl45);
0710 
0711     BOOST_CHECK_EQUAL(cyl45->bounds().get(CylinderBounds::eAveragePhi), 0);
0712     BOOST_CHECK_CLOSE(cyl45->bounds().get(CylinderBounds::eHalfPhiSector),
0713                       30_degree, 1e-6);
0714 
0715     Transform3 trf6 = base * AngleAxis3(a(90_degree), Vector3::UnitZ());
0716     auto cyl6 = Surface::makeShared<CylinderSurface>(trf6, 30_mm, 100_mm,
0717                                                      90_degree, 0_degree);
0718     Transform3 trf7 = base * AngleAxis3(a(-90_degree), Vector3::UnitZ());
0719     auto cyl7 = Surface::makeShared<CylinderSurface>(trf7, 30_mm, 100_mm,
0720                                                      90_degree, 0_degree);
0721 
0722     auto [cyl67, reversed67] =
0723         cyl6->mergedWith(*cyl7, Acts::AxisDirection::AxisRPhi, true, *logger);
0724     BOOST_REQUIRE_NE(cyl67, nullptr);
0725     Transform3 expected67 = trf6 * AngleAxis3(90_degree, Vector3::UnitZ());
0726     CHECK_CLOSE_OR_SMALL(cyl67->localToGlobalTransform(testContext).matrix(),
0727                          expected67.matrix(), 1e-6, 1e-10);
0728 
0729     auto [cyl76, reversed76] =
0730         cyl7->mergedWith(*cyl6, Acts::AxisDirection::AxisRPhi, true, *logger);
0731     BOOST_REQUIRE_NE(cyl76, nullptr);
0732     Transform3 expected76 = trf7 * AngleAxis3(90_degree, Vector3::UnitZ());
0733     CHECK_CLOSE_OR_SMALL(cyl76->localToGlobalTransform(testContext).matrix(),
0734                          expected76.matrix(), 1e-6, 1e-10);
0735 
0736     // The ordering in this case is effectively arbitrary, you get the ordering
0737     // you put in
0738     BOOST_CHECK(!reversed67);
0739     BOOST_CHECK(!reversed76);
0740 
0741     BOOST_CHECK_EQUAL(cyl67->bounds().get(CylinderBounds::eAveragePhi), 0);
0742     BOOST_CHECK_CLOSE(cyl67->bounds().get(CylinderBounds::eHalfPhiSector),
0743                       180_degree, 0.1);
0744   }
0745 }
0746 
0747 BOOST_AUTO_TEST_SUITE_END()
0748 BOOST_AUTO_TEST_SUITE_END()
0749 
0750 }  // namespace ActsTests