Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-17 09:24:47

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/tools/old/interface.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/Alignment.hpp"
0015 #include "Acts/Definitions/Tolerance.hpp"
0016 #include "Acts/Definitions/TrackParametrization.hpp"
0017 #include "Acts/Definitions/Units.hpp"
0018 #include "Acts/Geometry/Extent.hpp"
0019 #include "Acts/Geometry/GeometryContext.hpp"
0020 #include "Acts/Geometry/Polyhedron.hpp"
0021 #include "Acts/Material/BinnedSurfaceMaterial.hpp"
0022 #include "Acts/Material/HomogeneousSurfaceMaterial.hpp"
0023 #include "Acts/Material/Material.hpp"
0024 #include "Acts/Material/MaterialSlab.hpp"
0025 #include "Acts/Surfaces/PlaneSurface.hpp"
0026 #include "Acts/Surfaces/RectangleBounds.hpp"
0027 #include "Acts/Surfaces/Surface.hpp"
0028 #include "Acts/Surfaces/SurfaceBounds.hpp"
0029 #include "Acts/Surfaces/SurfaceMergingException.hpp"
0030 #include "Acts/Surfaces/TrapezoidBounds.hpp"
0031 #include "Acts/Utilities/AxisDefinitions.hpp"
0032 #include "Acts/Utilities/BinUtility.hpp"
0033 #include "Acts/Utilities/BinningType.hpp"
0034 #include "Acts/Utilities/Diagnostics.hpp"
0035 #include "Acts/Utilities/Intersection.hpp"
0036 #include "Acts/Utilities/Result.hpp"
0037 #include "Acts/Utilities/ThrowAssert.hpp"
0038 #include "Acts/Utilities/TransformHelpers.hpp"
0039 #include "ActsTests/CommonHelpers/DetectorElementStub.hpp"
0040 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0041 #include "ActsTests/CommonHelpers/SurfaceDerivatives.hpp"
0042 
0043 #include <cmath>
0044 #include <memory>
0045 #include <numbers>
0046 #include <string>
0047 
0048 using namespace Acts;
0049 using namespace Acts::UnitLiterals;
0050 
0051 namespace ActsTests {
0052 
0053 // Create a test context
0054 GeometryContext tgContext = GeometryContext::dangerouslyDefaultConstruct();
0055 
0056 BOOST_AUTO_TEST_SUITE(SurfacesSuite)
0057 
0058 /// Unit test for creating compliant/non-compliant PlaneSurface object
0059 BOOST_AUTO_TEST_CASE(PlaneSurfaceConstruction) {
0060   /// Test default construction
0061   // default construction is deleted
0062 
0063   // bounds object, rectangle type
0064   auto rBounds = std::make_shared<const RectangleBounds>(3., 4.);
0065   /// Constructor with transform and bounds
0066   Translation3 translation{0., 1., 2.};
0067   auto pTransform = Transform3(translation);
0068 
0069   /// Constructor with transform
0070   BOOST_CHECK_EQUAL(
0071       Surface::makeShared<PlaneSurface>(pTransform, rBounds)->type(),
0072       Surface::Plane);
0073 
0074   /// Copy constructor
0075   auto planeSurfaceObject =
0076       Surface::makeShared<PlaneSurface>(pTransform, rBounds);
0077   auto copiedPlaneSurface =
0078       Surface::makeShared<PlaneSurface>(*planeSurfaceObject);
0079   BOOST_CHECK_EQUAL(copiedPlaneSurface->type(), Surface::Plane);
0080   BOOST_CHECK(*copiedPlaneSurface == *planeSurfaceObject);
0081 
0082   /// Copied and transformed
0083   auto copiedTransformedPlaneSurface = Surface::makeShared<PlaneSurface>(
0084       tgContext, *planeSurfaceObject, pTransform);
0085   BOOST_CHECK_EQUAL(copiedTransformedPlaneSurface->type(), Surface::Plane);
0086 
0087   /// Construct with nullptr bounds
0088   DetectorElementStub detElem;
0089   BOOST_CHECK_THROW(
0090       auto nullBounds = Surface::makeShared<PlaneSurface>(nullptr, detElem),
0091       AssertionFailureException);
0092 }
0093 
0094 /// Unit test for testing PlaneSurface properties
0095 BOOST_AUTO_TEST_CASE(PlaneSurfaceProperties) {
0096   // bounds object, rectangle type
0097   auto rBounds = std::make_shared<const RectangleBounds>(3., 4.);
0098 
0099   /// Test clone method
0100   Translation3 translation{0., 1., 2.};
0101   auto pTransform = Transform3(translation);
0102   auto planeSurfaceObject =
0103       Surface::makeShared<PlaneSurface>(pTransform, rBounds);
0104   // Is it in the right place?
0105   Translation3 translation2{0., 2., 4.};
0106   auto pTransform2 = Transform3(translation2);
0107   auto planeSurfaceObject2 =
0108       Surface::makeShared<PlaneSurface>(pTransform2, rBounds);
0109 
0110   /// Test type (redundant)
0111   BOOST_CHECK_EQUAL(planeSurfaceObject->type(), Surface::Plane);
0112 
0113   /// Test referencePosition
0114   Vector3 referencePosition{0., 1., 2.};
0115   BOOST_CHECK_EQUAL(
0116       planeSurfaceObject->referencePosition(tgContext, AxisDirection::AxisX),
0117       referencePosition);
0118 
0119   /// Test referenceFrame
0120   Vector3 arbitraryGlobalPosition{2., 2., 2.};
0121   Vector3 momentum{1.e6, 1.e6, 1.e6};
0122   RotationMatrix3 expectedFrame;
0123   expectedFrame << 1., 0., 0., 0., 1., 0., 0., 0., 1.;
0124 
0125   CHECK_CLOSE_OR_SMALL(planeSurfaceObject->referenceFrame(
0126                            tgContext, arbitraryGlobalPosition, momentum),
0127                        expectedFrame, 1e-6, 1e-9);
0128 
0129   /// Test normal, given 3D position
0130   Vector3 normal3D(0., 0., 1.);
0131   BOOST_CHECK_EQUAL(planeSurfaceObject->normal(tgContext), normal3D);
0132 
0133   /// Test bounds
0134   BOOST_CHECK_EQUAL(planeSurfaceObject->bounds().type(),
0135                     SurfaceBounds::eRectangle);
0136 
0137   /// Test localToGlobal
0138   Vector2 localPosition{1.5, 1.7};
0139   Vector3 globalPosition =
0140       planeSurfaceObject->localToGlobal(tgContext, localPosition, momentum);
0141   // expected position is the translated one
0142   Vector3 expectedPosition{1.5 + translation.x(), 1.7 + translation.y(),
0143                            translation.z()};
0144 
0145   CHECK_CLOSE_REL(globalPosition, expectedPosition, 1e-2);
0146 
0147   /// Testing globalToLocal
0148   localPosition =
0149       planeSurfaceObject->globalToLocal(tgContext, globalPosition, momentum)
0150           .value();
0151   Vector2 expectedLocalPosition{1.5, 1.7};
0152 
0153   CHECK_CLOSE_REL(localPosition, expectedLocalPosition, 1e-2);
0154 
0155   Vector3 globalPositionOff =
0156       globalPosition +
0157       planeSurfaceObject->normal(tgContext, localPosition) * 0.1;
0158 
0159   BOOST_CHECK(
0160       planeSurfaceObject->globalToLocal(tgContext, globalPositionOff, momentum)
0161           .error());
0162   BOOST_CHECK(planeSurfaceObject
0163                   ->globalToLocal(tgContext, globalPositionOff, momentum, 0.05)
0164                   .error());
0165   BOOST_CHECK(planeSurfaceObject
0166                   ->globalToLocal(tgContext, globalPositionOff, momentum, 0.2)
0167                   .ok());
0168 
0169   /// Test isOnSurface
0170   Vector3 offSurface{0, 1, -2.};
0171   BOOST_CHECK(planeSurfaceObject->isOnSurface(
0172       tgContext, globalPosition, momentum, BoundaryTolerance::None()));
0173   BOOST_CHECK(planeSurfaceObject->isOnSurface(tgContext, globalPosition,
0174                                               BoundaryTolerance::None()));
0175   BOOST_CHECK(!planeSurfaceObject->isOnSurface(tgContext, offSurface, momentum,
0176                                                BoundaryTolerance::None()));
0177   BOOST_CHECK(!planeSurfaceObject->isOnSurface(tgContext, offSurface,
0178                                                BoundaryTolerance::None()));
0179 
0180   /// Test intersection
0181   Vector3 direction{0., 0., 1.};
0182   Intersection3D sfIntersection =
0183       planeSurfaceObject
0184           ->intersect(tgContext, offSurface, direction,
0185                       BoundaryTolerance::None())
0186           .closest();
0187   Intersection3D expectedIntersect{Vector3{0, 1, 2}, 4.,
0188                                    IntersectionStatus::reachable};
0189   BOOST_CHECK(sfIntersection.isValid());
0190   BOOST_CHECK_EQUAL(sfIntersection.position(), expectedIntersect.position());
0191   BOOST_CHECK_EQUAL(sfIntersection.pathLength(),
0192                     expectedIntersect.pathLength());
0193 
0194   /// Test pathCorrection
0195   CHECK_CLOSE_REL(planeSurfaceObject->pathCorrection(tgContext, offSurface,
0196                                                      momentum.normalized()),
0197                   std::numbers::sqrt3, 0.01);
0198 
0199   /// Test name
0200   BOOST_CHECK_EQUAL(planeSurfaceObject->name(),
0201                     std::string("Acts::PlaneSurface"));
0202 
0203   /// Test dump
0204   boost::test_tools::output_test_stream dumpOutput;
0205   dumpOutput << planeSurfaceObject->toStream(tgContext);
0206   BOOST_CHECK(dumpOutput.is_equal(
0207       "Acts::PlaneSurface\n"
0208       "     Center position  (x, y, z) = (0.0000, 1.0000, 2.0000)\n"
0209       "     Rotation:             colX = (1.000000, 0.000000, 0.000000)\n"
0210       "                           colY = (0.000000, 1.000000, 0.000000)\n"
0211       "                           colZ = (0.000000, 0.000000, 1.000000)\n"
0212       "     Bounds  : Acts::RectangleBounds:  (hlX, hlY) = (3.0000000, "
0213       "4.0000000)\n"
0214       "(lower left, upper right):\n"
0215       "-3.0000000 -4.0000000\n"
0216       "3.0000000 4.0000000"));
0217 }
0218 
0219 BOOST_AUTO_TEST_CASE(PlaneSurfaceEqualityOperators) {
0220   // rectangle bounds
0221   auto rBounds = std::make_shared<const RectangleBounds>(3., 4.);
0222   Translation3 translation{0., 1., 2.};
0223   auto pTransform = Transform3(translation);
0224   auto planeSurfaceObject =
0225       Surface::makeShared<PlaneSurface>(pTransform, rBounds);
0226   auto planeSurfaceObject2 =
0227       Surface::makeShared<PlaneSurface>(pTransform, rBounds);
0228 
0229   /// Test equality operator
0230   BOOST_CHECK(*planeSurfaceObject == *planeSurfaceObject2);
0231 
0232   BOOST_TEST_CHECKPOINT(
0233       "Create and then assign a PlaneSurface object to the existing one");
0234 
0235   /// Test assignment
0236   auto assignedPlaneSurface =
0237       Surface::makeShared<PlaneSurface>(Transform3::Identity(), nullptr);
0238   // TODO: surface copy-assignment is deprecated; switch to a makeShared copy
0239   // when it is removed (SonarCloud cpp:S3624).
0240   ACTS_PUSH_IGNORE_DEPRECATED()
0241   *assignedPlaneSurface = *planeSurfaceObject;
0242   ACTS_POP_IGNORE_DEPRECATED()
0243 
0244   /// Test equality of assigned to original
0245   BOOST_CHECK(*assignedPlaneSurface == *planeSurfaceObject);
0246 }
0247 
0248 /// Unit test for testing PlaneSurface extent via Polyhedron representation
0249 BOOST_AUTO_TEST_CASE(PlaneSurfaceExtent) {
0250   // First test - non-rotated
0251   static const Transform3 planeZX =
0252       AngleAxis3(-std::numbers::pi / 2., Vector3::UnitX()) *
0253       AngleAxis3(-std::numbers::pi / 2., Vector3::UnitZ()) *
0254       Transform3::Identity();
0255 
0256   double rHx = 2.;
0257   double rHy = 4.;
0258   double yPs = 3.;
0259   auto rBounds = std::make_shared<RectangleBounds>(rHx, rHy);
0260 
0261   auto plane = Surface::makeShared<PlaneSurface>(
0262       Transform3(Translation3(Vector3(0., yPs, 0.)) * planeZX), rBounds);
0263 
0264   auto planeExtent = plane->polyhedronRepresentation(tgContext, 1).extent();
0265 
0266   CHECK_CLOSE_ABS(planeExtent.min(AxisDirection::AxisZ), -rHx,
0267                   s_onSurfaceTolerance);
0268   CHECK_CLOSE_ABS(planeExtent.max(AxisDirection::AxisZ), rHx,
0269                   s_onSurfaceTolerance);
0270   CHECK_CLOSE_ABS(planeExtent.min(AxisDirection::AxisX), -rHy,
0271                   s_onSurfaceTolerance);
0272   CHECK_CLOSE_ABS(planeExtent.max(AxisDirection::AxisX), rHy,
0273                   s_onSurfaceTolerance);
0274   CHECK_CLOSE_ABS(planeExtent.min(AxisDirection::AxisY), yPs,
0275                   s_onSurfaceTolerance);
0276   CHECK_CLOSE_ABS(planeExtent.max(AxisDirection::AxisY), yPs,
0277                   s_onSurfaceTolerance);
0278   CHECK_CLOSE_ABS(planeExtent.min(AxisDirection::AxisR), yPs,
0279                   s_onSurfaceTolerance);
0280   CHECK_CLOSE_ABS(planeExtent.max(AxisDirection::AxisR), std::hypot(yPs, rHy),
0281                   s_onSurfaceTolerance);
0282 
0283   // Now rotate
0284   double alpha = 0.123;
0285   auto planeRot = Surface::makeShared<PlaneSurface>(
0286       Transform3(Translation3(Vector3(0., yPs, 0.)) *
0287                  AngleAxis3(alpha, Vector3(0., 0., 1.)) * planeZX),
0288       rBounds);
0289 
0290   auto planeExtentRot =
0291       planeRot->polyhedronRepresentation(tgContext, 1).extent();
0292   CHECK_CLOSE_ABS(planeExtentRot.min(AxisDirection::AxisZ), -rHx,
0293                   s_onSurfaceTolerance);
0294   CHECK_CLOSE_ABS(planeExtentRot.max(AxisDirection::AxisZ), rHx,
0295                   s_onSurfaceTolerance);
0296   CHECK_CLOSE_ABS(planeExtentRot.min(AxisDirection::AxisX),
0297                   -rHy * std::cos(alpha), s_onSurfaceTolerance);
0298   CHECK_CLOSE_ABS(planeExtentRot.max(AxisDirection::AxisX),
0299                   rHy * std::cos(alpha), s_onSurfaceTolerance);
0300   CHECK_CLOSE_ABS(planeExtentRot.min(AxisDirection::AxisY),
0301                   yPs - rHy * std::sin(alpha), s_onSurfaceTolerance);
0302   CHECK_CLOSE_ABS(planeExtentRot.max(AxisDirection::AxisY),
0303                   yPs + rHy * std::sin(alpha), s_onSurfaceTolerance);
0304   CHECK_CLOSE_ABS(planeExtentRot.min(AxisDirection::AxisR),
0305                   yPs * std::cos(alpha), s_onSurfaceTolerance);
0306 }
0307 
0308 BOOST_AUTO_TEST_CASE(RotatedTrapezoid) {
0309   const double shortHalfX = 100.;
0310   const double longHalfX = 200.;
0311   const double halfY = 300.;
0312   const double rotAngle = 45._degree;
0313 
0314   Vector2 edgePoint{longHalfX - 10., halfY};
0315 
0316   std::shared_ptr<TrapezoidBounds> bounds =
0317       std::make_shared<TrapezoidBounds>(shortHalfX, longHalfX, halfY);
0318 
0319   BOOST_CHECK(bounds->inside(edgePoint, BoundaryTolerance::None()));
0320   BOOST_CHECK(!bounds->inside(Eigen::Rotation2D(-rotAngle) * edgePoint,
0321                               BoundaryTolerance::None()));
0322 
0323   std::shared_ptr<TrapezoidBounds> rotatedBounds =
0324       std::make_shared<TrapezoidBounds>(shortHalfX, longHalfX, halfY, rotAngle);
0325 
0326   BOOST_CHECK(!rotatedBounds->inside(edgePoint, BoundaryTolerance::None()));
0327   BOOST_CHECK(rotatedBounds->inside(Eigen::Rotation2D(-rotAngle) * edgePoint,
0328                                     BoundaryTolerance::None()));
0329 }
0330 
0331 /// Unit test for testing PlaneSurface alignment derivatives
0332 BOOST_AUTO_TEST_CASE(PlaneSurfaceAlignment) {
0333   // bounds object, rectangle type
0334   auto rBounds = std::make_shared<const RectangleBounds>(3., 4.);
0335   // Test clone method
0336   Translation3 translation{0., 1., 2.};
0337   const double rotationAngle = std::numbers::pi / 2.;
0338   AngleAxis3 rotation(rotationAngle, Vector3::UnitY());
0339   RotationMatrix3 rotationMat = rotation.toRotationMatrix();
0340 
0341   auto pTransform = makeTransform3(rotationMat, translation.vector());
0342   auto planeSurfaceObject =
0343       Surface::makeShared<PlaneSurface>(pTransform, rBounds);
0344 
0345   // The local frame z axis
0346   const Vector3 localZAxis = rotationMat.col(2);
0347   // Check the local z axis is aligned to global x axis
0348   CHECK_CLOSE_ABS(localZAxis, Vector3(1., 0., 0.), 1e-15);
0349 
0350   // Define the track (local) position and direction
0351   Vector2 localPosition{1, 2};
0352   Vector3 momentum{1, 0, 0};
0353   Vector3 direction = momentum.normalized();
0354   // Get the global position
0355   Vector3 globalPosition =
0356       planeSurfaceObject->localToGlobal(tgContext, localPosition, momentum);
0357 
0358   // (a) Test the derivative of path length w.r.t. alignment parameters
0359   const AlignmentToPathMatrix& alignToPath =
0360       planeSurfaceObject->alignmentToPathDerivative(tgContext, globalPosition,
0361                                                     direction);
0362   // The expected results (local translations along local x/y/z)
0363   AlignmentToPathMatrix expAlignToPath = AlignmentToPathMatrix::Zero();
0364   expAlignToPath << 0, 0, 1, 2, -1, 0;
0365 
0366   // Check if the calculated derivative is as expected
0367   CHECK_CLOSE_ABS(alignToPath, expAlignToPath, 1e-10);
0368 
0369   // (b) Test the derivative of bound track parameters local position w.r.t.
0370   // position in local 3D Cartesian coordinates
0371   const auto& loc3DToLocBound =
0372       planeSurfaceObject->localCartesianToBoundLocalDerivative(
0373           tgContext, globalPosition, direction);
0374   // For plane surface, this should be identity matrix
0375   CHECK_CLOSE_ABS(loc3DToLocBound, (Matrix<2, 3>::Identity()), 1e-10);
0376 
0377   // (c) Test the derivative of bound parameters (only test loc0, loc1 here)
0378   // w.r.t. alignment parameters
0379   FreeVector derivatives = FreeVector::Zero();
0380   derivatives.head<3>() = direction;
0381   const AlignmentToBoundMatrix& alignToBound =
0382       planeSurfaceObject->alignmentToBoundDerivative(tgContext, globalPosition,
0383                                                      direction, derivatives);
0384   const AlignmentToPathMatrix alignToloc0 =
0385       alignToBound.block<1, 6>(eBoundLoc0, eAlignmentCenter0);
0386   const AlignmentToPathMatrix alignToloc1 =
0387       alignToBound.block<1, 6>(eBoundLoc1, eAlignmentCenter0);
0388   // The expected results (local translations along local x/y/z)
0389   AlignmentToPathMatrix expAlignToloc0;
0390   expAlignToloc0 << -1, 0, 0, 0, 0, 2;
0391   AlignmentToPathMatrix expAlignToloc1;
0392   expAlignToloc1 << 0, -1, 0, 0, 0, -1;
0393   // Check if the calculated derivatives are as expected
0394   CHECK_CLOSE_ABS(alignToloc0, expAlignToloc0, 1e-10);
0395   CHECK_CLOSE_ABS(alignToloc1, expAlignToloc1, 1e-10);
0396 
0397   checkSurfaceDerivatives(tgContext, *planeSurfaceObject, pTransform,
0398                           globalPosition, direction, [&](const Transform3& tf) {
0399                             return Surface::makeShared<PlaneSurface>(tf,
0400                                                                      rBounds);
0401                           });
0402 }
0403 
0404 BOOST_AUTO_TEST_SUITE(PlaneSurfaceMerging)
0405 
0406 auto logger = Acts::getDefaultLogger("UnitTests", Acts::Logging::VERBOSE);
0407 
0408 // Create a test context
0409 GeometryContext gctx = GeometryContext::dangerouslyDefaultConstruct();
0410 
0411 auto rBounds = std::make_shared<const RectangleBounds>(1., 2.);
0412 
0413 BOOST_AUTO_TEST_CASE(SurfaceOverlap) {
0414   // Correct orientation, overlapping along merging direction
0415   Translation3 offsetX{4., 0., 0.};
0416   Translation3 offsetY{0., 2., 0.};
0417 
0418   Transform3 base(Translation3::Identity());
0419   Transform3 otherX = base * offsetX;
0420   Transform3 otherY = base * offsetY;
0421 
0422   auto plane = Surface::makeShared<PlaneSurface>(base, rBounds);
0423   auto planeX = Surface::makeShared<PlaneSurface>(otherX, rBounds);
0424   auto planeY = Surface::makeShared<PlaneSurface>(otherY, rBounds);
0425 
0426   BOOST_CHECK_THROW(plane->mergedWith(*planeX, Acts::AxisDirection::AxisX),
0427                     SurfaceMergingException);
0428   BOOST_CHECK_THROW(plane->mergedWith(*planeY, Acts::AxisDirection::AxisY),
0429                     SurfaceMergingException);
0430 
0431   BOOST_CHECK_THROW(planeX->mergedWith(*plane, Acts::AxisDirection::AxisX),
0432                     SurfaceMergingException);
0433   BOOST_CHECK_THROW(planeY->mergedWith(*plane, Acts::AxisDirection::AxisY),
0434                     SurfaceMergingException);
0435 }
0436 
0437 BOOST_AUTO_TEST_CASE(SurfaceMisalignmentShift) {
0438   // Correct orientation, not aligned along orthogonal to merging direction
0439   Translation3 offsetX{2., 1., 0.};
0440   Translation3 offsetY{-1., 4., 0.};
0441   Translation3 offsetZ{0., 4., 1.};
0442 
0443   Transform3 base(Translation3::Identity());
0444   Transform3 otherX = base * offsetX;
0445   Transform3 otherY = base * offsetY;
0446   Transform3 otherZ = base * offsetZ;
0447 
0448   auto plane = Surface::makeShared<PlaneSurface>(base, rBounds);
0449   auto planeX = Surface::makeShared<PlaneSurface>(otherX, rBounds);
0450   auto planeY = Surface::makeShared<PlaneSurface>(otherY, rBounds);
0451   auto planeZ = Surface::makeShared<PlaneSurface>(otherZ, rBounds);
0452 
0453   BOOST_CHECK_THROW(plane->mergedWith(*planeX, Acts::AxisDirection::AxisX),
0454                     SurfaceMergingException);
0455   BOOST_CHECK_THROW(plane->mergedWith(*planeY, Acts::AxisDirection::AxisY),
0456                     SurfaceMergingException);
0457   BOOST_CHECK_THROW(plane->mergedWith(*planeZ, Acts::AxisDirection::AxisX),
0458                     SurfaceMergingException);
0459 
0460   BOOST_CHECK_THROW(planeX->mergedWith(*plane, Acts::AxisDirection::AxisX),
0461                     SurfaceMergingException);
0462   BOOST_CHECK_THROW(planeY->mergedWith(*plane, Acts::AxisDirection::AxisY),
0463                     SurfaceMergingException);
0464   BOOST_CHECK_THROW(planeZ->mergedWith(*plane, Acts::AxisDirection::AxisX),
0465                     SurfaceMergingException);
0466 }
0467 
0468 BOOST_AUTO_TEST_CASE(SurfaceMisalignedAngle) {
0469   // Correct positioning, rotated in different directions
0470   Translation3 offsetX{2., 0., 0.};
0471   Translation3 offsetY{0., 4., 0.};
0472 
0473   double angle = std::numbers::pi / 12;
0474   Transform3 base(Translation3::Identity());
0475   Transform3 otherX = base * offsetX * AngleAxis3(angle, Vector3::UnitZ());
0476   Transform3 otherY = base * offsetY * AngleAxis3(angle, Vector3::UnitY());
0477   Transform3 otherZ = base * offsetY * AngleAxis3(angle, Vector3::UnitZ());
0478 
0479   auto plane = Surface::makeShared<PlaneSurface>(base, rBounds);
0480   auto planeX = Surface::makeShared<PlaneSurface>(otherX, rBounds);
0481   auto planeY = Surface::makeShared<PlaneSurface>(otherY, rBounds);
0482   auto planeZ = Surface::makeShared<PlaneSurface>(otherZ, rBounds);
0483 
0484   BOOST_CHECK_THROW(plane->mergedWith(*planeX, Acts::AxisDirection::AxisX),
0485                     SurfaceMergingException);
0486   BOOST_CHECK_THROW(plane->mergedWith(*planeY, Acts::AxisDirection::AxisY),
0487                     SurfaceMergingException);
0488   BOOST_CHECK_THROW(plane->mergedWith(*planeZ, Acts::AxisDirection::AxisY),
0489                     SurfaceMergingException);
0490 
0491   BOOST_CHECK_THROW(planeX->mergedWith(*plane, Acts::AxisDirection::AxisX),
0492                     SurfaceMergingException);
0493   BOOST_CHECK_THROW(planeY->mergedWith(*plane, Acts::AxisDirection::AxisY),
0494                     SurfaceMergingException);
0495   BOOST_CHECK_THROW(planeZ->mergedWith(*plane, Acts::AxisDirection::AxisY),
0496                     SurfaceMergingException);
0497 }
0498 
0499 BOOST_AUTO_TEST_CASE(SurfaceDifferentBounds) {
0500   // Correct orientation and alignment, different bounds lengths along
0501   // orthogonal to merging direction
0502   Translation3 offset{2., 0., 0.};
0503 
0504   Transform3 base(Translation3::Identity());
0505   Transform3 other = base * offset;
0506 
0507   auto plane = Surface::makeShared<PlaneSurface>(base, rBounds);
0508 
0509   auto rBoundsOther = std::make_shared<const RectangleBounds>(2., 4.);
0510   auto planeOther = Surface::makeShared<PlaneSurface>(other, rBoundsOther);
0511 
0512   BOOST_CHECK_THROW(plane->mergedWith(*planeOther, Acts::AxisDirection::AxisX),
0513                     SurfaceMergingException);
0514 }
0515 
0516 BOOST_AUTO_TEST_CASE(XYDirection) {
0517   double angle = std::numbers::pi / 12;
0518   Translation3 offsetX{2., 0., 0.};
0519   Translation3 offsetY{0., 4., 0.};
0520 
0521   Transform3 base =
0522       AngleAxis3(angle, Vector3::UnitX()) * Translation3::Identity();
0523   Transform3 otherX = base * offsetX;
0524   Transform3 otherY = base * offsetY;
0525 
0526   auto plane = Surface::makeShared<PlaneSurface>(base, rBounds);
0527   auto planeX = Surface::makeShared<PlaneSurface>(otherX, rBounds);
0528   auto planeY = Surface::makeShared<PlaneSurface>(otherY, rBounds);
0529 
0530   BOOST_CHECK_THROW(plane->mergedWith(*planeX, Acts::AxisDirection::AxisZ),
0531                     SurfaceMergingException);
0532 
0533   auto expectedBoundsX = std::make_shared<const RectangleBounds>(2, 2);
0534   auto [planeXMerged, reversedX] =
0535       plane->mergedWith(*planeX, Acts::AxisDirection::AxisX, *logger);
0536   BOOST_REQUIRE_NE(planeXMerged, nullptr);
0537   BOOST_CHECK(!reversedX);
0538   BOOST_CHECK_EQUAL(planeXMerged->bounds(), *expectedBoundsX);
0539   BOOST_CHECK_EQUAL(planeXMerged->center(gctx), base * Vector3::UnitX() * 1);
0540 
0541   auto expectedBoundsY = std::make_shared<const RectangleBounds>(1, 4);
0542   auto [planeYMerged, reversedY] =
0543       plane->mergedWith(*planeY, Acts::AxisDirection::AxisY, *logger);
0544   BOOST_REQUIRE_NE(planeYMerged, nullptr);
0545   BOOST_CHECK(!reversedY);
0546   BOOST_CHECK_EQUAL(planeYMerged->bounds(), *expectedBoundsY);
0547   BOOST_CHECK_EQUAL(planeYMerged->center(gctx), base * Vector3::UnitY() * 2);
0548 
0549   auto [planeXMerged2, reversedX2] =
0550       planeX->mergedWith(*plane, Acts::AxisDirection::AxisX, *logger);
0551   BOOST_REQUIRE_NE(planeXMerged2, nullptr);
0552   BOOST_CHECK(planeXMerged->bounds() == planeXMerged2->bounds());
0553   BOOST_CHECK(reversedX2);
0554   BOOST_CHECK_EQUAL(planeXMerged2->bounds(), *expectedBoundsX);
0555   BOOST_CHECK_EQUAL(planeXMerged2->center(gctx), base * Vector3::UnitX() * 1);
0556 
0557   auto [planeYMerged2, reversedY2] =
0558       planeY->mergedWith(*plane, Acts::AxisDirection::AxisY, *logger);
0559   BOOST_REQUIRE_NE(planeYMerged2, nullptr);
0560   BOOST_CHECK(planeYMerged->bounds() == planeYMerged2->bounds());
0561   BOOST_CHECK(reversedY2);
0562   BOOST_CHECK_EQUAL(planeYMerged2->bounds(), *expectedBoundsY);
0563   BOOST_CHECK_EQUAL(planeYMerged2->center(gctx), base * Vector3::UnitY() * 2);
0564 }
0565 
0566 BOOST_AUTO_TEST_SUITE_END()
0567 
0568 BOOST_AUTO_TEST_CASE(PlaneSurfaceMaterialAssignment) {
0569   auto rBounds = std::make_shared<RectangleBounds>(100., 100.);
0570   auto surface =
0571       Surface::makeShared<PlaneSurface>(Transform3::Identity(), rBounds);
0572   MaterialSlab slab(Material::fromMolarDensity(1., 2., 3., 4., 5.), 0.1);
0573 
0574   // HomogeneousSurfaceMaterial is always valid
0575   auto homMat = std::make_shared<HomogeneousSurfaceMaterial>(slab);
0576   BOOST_CHECK_NO_THROW(surface->assignSurfaceMaterial(homMat));
0577   BOOST_CHECK_NE(surface->surfaceMaterial(), nullptr);
0578 
0579   // nullptr clears the material
0580   BOOST_CHECK_NO_THROW(surface->assignSurfaceMaterial(nullptr));
0581   BOOST_CHECK_EQUAL(surface->surfaceMaterial(), nullptr);
0582 
0583   // 1D AxisX - valid for plane
0584   BinUtility buX(10, -100.f, 100.f, Acts::open, AxisDirection::AxisX);
0585   auto matX = std::make_shared<BinnedSurfaceMaterial>(
0586       buX, MaterialSlabVector(10, slab));
0587   BOOST_CHECK_NO_THROW(surface->assignSurfaceMaterial(matX));
0588 
0589   // 1D AxisY - valid for plane
0590   BinUtility buY(10, -100.f, 100.f, Acts::open, AxisDirection::AxisY);
0591   auto matY = std::make_shared<BinnedSurfaceMaterial>(
0592       buY, MaterialSlabVector(10, slab));
0593   BOOST_CHECK_NO_THROW(surface->assignSurfaceMaterial(matY));
0594 
0595   // 2D {AxisX, AxisY} - valid for plane
0596   BinUtility bu2D(5, -100.f, 100.f, Acts::open, AxisDirection::AxisX);
0597   bu2D += BinUtility(3, -100.f, 100.f, Acts::open, AxisDirection::AxisY);
0598   auto mat2D = std::make_shared<BinnedSurfaceMaterial>(
0599       bu2D, MaterialSlabMatrix(3, MaterialSlabVector(5, slab)));
0600   BOOST_CHECK_NO_THROW(surface->assignSurfaceMaterial(mat2D));
0601 
0602   // Wrong axis directions - should throw
0603   for (auto badDir : {AxisDirection::AxisRPhi, AxisDirection::AxisZ,
0604                       AxisDirection::AxisR, AxisDirection::AxisPhi}) {
0605     BinUtility buBad(10, 0.f, 10.f, Acts::open, badDir);
0606     auto matBad = std::make_shared<BinnedSurfaceMaterial>(
0607         buBad, MaterialSlabVector(10, slab));
0608     BOOST_CHECK_THROW(surface->assignSurfaceMaterial(matBad),
0609                       std::invalid_argument);
0610   }
0611 }
0612 
0613 BOOST_AUTO_TEST_SUITE_END()
0614 
0615 }  // namespace ActsTests