Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-30 09:27:22

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/Surfaces/CylinderSurface.hpp"
0012 #include "Acts/Surfaces/DiscSurface.hpp"
0013 #include "Acts/Surfaces/PlaneSurface.hpp"
0014 #include "Acts/Surfaces/PointSurface.hpp"
0015 #include "Acts/Surfaces/RadialBounds.hpp"
0016 #include "Acts/Surfaces/RectangleBounds.hpp"
0017 #include "Acts/Surfaces/detail/CurvatureCorrection.hpp"
0018 #include "Acts/Surfaces/detail/PlanarHelper.hpp"
0019 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0020 
0021 #include <limits>
0022 
0023 #include "SurfaceStub.hpp"
0024 
0025 using namespace Acts;
0026 namespace ActsTests {
0027 const GeometryContext gctx = GeometryContext::dangerouslyDefaultConstruct();
0028 
0029 BOOST_AUTO_TEST_CASE(CurvatureCorrectionModes) {
0030   const Vector3 direction = Vector3::UnitZ();
0031   const Vector3 normal = Vector3(1., 0., 1.).normalized();
0032   const double s = 100.;
0033   const Vector3 seed = s * direction;
0034   for (double k : {0., 0.0005, -0.0005}) {
0035     const Vector3 curvature(k, 0., 0.);
0036     for (double scale : {1., 100.}) {
0037       const auto first = detail::correctCurvatureIntersection<false>(
0038           s, seed, direction, curvature, scale * normal.dot(direction),
0039           scale * normal.dot(curvature), curvature.norm(), scale);
0040       const auto quadratic = detail::correctCurvatureIntersection<true>(
0041           s, seed, direction, curvature, scale * normal.dot(direction),
0042           scale * normal.dot(curvature), curvature.norm(), scale);
0043       BOOST_REQUIRE(first);
0044       BOOST_REQUIRE(quadratic);
0045       CHECK_CLOSE_ABS(normal.dot(first->position - seed), 0., 1e-12);
0046       CHECK_CLOSE_ABS(normal.dot(quadratic->position - seed), 0., 1e-12);
0047       const double path = quadratic->pathLength;
0048       CHECK_CLOSE_ABS(quadratic->position,
0049                       (path * direction + 0.5 * path * path * curvature).eval(),
0050                       1e-12);
0051       const auto selected = detail::correctCurvatureIntersection(
0052           s, seed, direction, curvature, scale * normal.dot(direction),
0053           scale * normal.dot(curvature), curvature.norm(), scale);
0054       BOOST_REQUIRE(selected);
0055       const auto& expected =
0056           detail::s_useQuadraticCurvatureCorrection ? quadratic : first;
0057       CHECK_CLOSE_ABS(selected->position, expected->position, 1e-12);
0058       CHECK_CLOSE_ABS(selected->pathLength, expected->pathLength, 1e-12);
0059     }
0060   }
0061   // A local parabola that turns before reaching the plane has no real root.
0062   BOOST_CHECK(!detail::correctCurvatureIntersection<true>(
0063       s, seed, direction, Vector3(-0.001, 0., 0.), 0.1, -0.001, 0.001));
0064 }
0065 
0066 // Compare against an independent constant-field circle, not the truncated
0067 // trajectory. The quadratic solve need not improve the missing helix terms.
0068 BOOST_AUTO_TEST_CASE(CurvatureQuadraticComparison) {
0069   const Vector3 start = Vector3::Zero();
0070   const Vector3 direction = Vector3::UnitZ();
0071   for (double tilt : {0., 0.5, 1.}) {
0072     Transform3 transform = Transform3::Identity();
0073     transform.translation() = Vector3(0., 0., 200.);
0074     transform.linear() = AngleAxis3(tilt, Vector3::UnitY()).toRotationMatrix();
0075     const Vector3 normal = transform.linear().col(2);
0076     const auto plane = Surface::makeShared<PlaneSurface>(
0077         transform, std::make_shared<RectangleBounds>(1000., 1000.));
0078     for (double k : {0., 0.00025, 0.0005, -0.0005}) {
0079       const Vector3 curvature(k, 0., 0.);
0080       const auto first = detail::PlanarHelper::intersect<false>(
0081           transform, start, direction, curvature, s_onSurfaceTolerance);
0082       const auto quadratic = detail::PlanarHelper::intersect<true>(
0083           transform, start, direction, curvature, s_onSurfaceTolerance);
0084       const auto selected = plane
0085                                 ->intersect(gctx, start, direction, curvature,
0086                                             BoundaryTolerance::None())
0087                                 .at(0);
0088       const auto& expected =
0089           detail::s_useQuadraticCurvatureCorrection ? quadratic : first;
0090       CHECK_CLOSE_ABS(selected.position(), expected.position(), 1e-10);
0091       CHECK_CLOSE_ABS(selected.pathLength(), expected.pathLength(), 1e-10);
0092       BOOST_REQUIRE(first.isValid());
0093       BOOST_REQUIRE(quadratic.isValid());
0094       const double path = quadratic.pathLength();
0095       CHECK_CLOSE_ABS(quadratic.position(),
0096                       (path * direction + 0.5 * path * path * curvature).eval(),
0097                       1e-10);
0098       BOOST_CHECK(plane->isOnSurface(gctx, quadratic.position(), direction));
0099       const auto reverse = detail::PlanarHelper::intersect<true>(
0100           transform, start, -direction, curvature, s_onSurfaceTolerance);
0101       CHECK_CLOSE_ABS(reverse.position(), quadratic.position(), 1e-10);
0102       CHECK_CLOSE_ABS(reverse.pathLength(), -path, 1e-10);
0103       const auto exact = [k](double s) -> Vector3 {
0104         if (k == 0.) {
0105           return Vector3(0., 0., s);
0106         }
0107         return Vector3(2. * std::pow(std::sin(k * s / 2.), 2) / k, 0.,
0108                        std::sin(k * s) / k);
0109       };
0110       double lo = 0.;
0111       double hi = 400.;
0112       for (int i = 0; i < 60; ++i) {
0113         const double mid = 0.5 * (lo + hi);
0114         if (normal.dot(exact(mid) - transform.translation()) < 0.) {
0115           lo = mid;
0116         } else {
0117           hi = mid;
0118         }
0119       }
0120       const Vector3 reference = exact(0.5 * (lo + hi));
0121       std::cout << "tilt=" << tilt << " k=" << k
0122                 << " first_error_mm=" << (first.position() - reference).norm()
0123                 << " quadratic_error_mm="
0124                 << (quadratic.position() - reference).norm() << '\n';
0125       if (k == 0.) {
0126         CHECK_CLOSE_ABS(quadratic.position(), first.position(), 1e-10);
0127       }
0128     }
0129   }
0130 }
0131 
0132 BOOST_AUTO_TEST_CASE(CurvaturePlaneBoundsAndDistance) {
0133   const auto bounds = std::make_shared<RectangleBounds>(0.2, 0.2);
0134   const auto sensor = Surface::makeShared<PlaneSurface>(
0135       Transform3(Translation3(1., 0., 20.)), bounds);
0136   const Vector3 start = Vector3::Zero();
0137   const Vector3 direction = Vector3::UnitZ();
0138   const Vector3 curvature(0.005, 0., 0.);
0139   BOOST_CHECK(
0140       !sensor->intersect(gctx, start, direction, BoundaryTolerance::None())
0141            .at(0)
0142            .isValid());
0143   const auto crossing = sensor->intersect(gctx, start, direction, curvature,
0144                                           BoundaryTolerance::None());
0145   BOOST_REQUIRE(crossing.at(0).isValid());
0146   BOOST_CHECK_EQUAL(crossing.size(), 1u);
0147   CHECK_CLOSE_ABS(crossing.at(0).position(), Vector3(1., 0., 20.), 1e-12);
0148   CHECK_CLOSE_ABS(crossing.at(0).pathLength(), 20., 1e-12);
0149   // Analytic circle with initial tangent +z and curvature towards +x.
0150   const double exactX = (1. - std::sqrt(1. - std::pow(20. * 0.005, 2))) / 0.005;
0151   BOOST_CHECK_LT(std::abs(crossing.at(0).position().x() - exactX), 0.003);
0152   const auto reversed = sensor->intersect(gctx, start, -direction, curvature,
0153                                           BoundaryTolerance::None());
0154   CHECK_CLOSE_ABS(reversed.at(0).position(), crossing.at(0).position(), 1e-12);
0155   CHECK_CLOSE_ABS(reversed.at(0).pathLength(), -20., 1e-12);
0156   BOOST_CHECK(!sensor
0157                    ->intersect(gctx, start, direction, -curvature,
0158                                BoundaryTolerance::None())
0159                    .at(0)
0160                    .isValid());
0161 
0162   Transform3 tilted = Transform3::Identity();
0163   tilted.translation() = Vector3(0., 0., 20.);
0164   tilted.linear() =
0165       AngleAxis3(std::atan(0.2), Vector3::UnitY()).toRotationMatrix();
0166   const auto plane = Surface::makeShared<PlaneSurface>(
0167       tilted, std::make_shared<RectangleBounds>(10., 10.));
0168   const auto hit = plane
0169                        ->intersect(gctx, start, direction, curvature,
0170                                    BoundaryTolerance::None())
0171                        .at(0);
0172   BOOST_REQUIRE(hit.isValid());
0173   CHECK_CLOSE_ABS(hit.pathLength(), 19.8, 1e-12);
0174   CHECK_CLOSE_ABS(hit.position(), Vector3(1., 0., 19.8), 1e-12);
0175   BOOST_CHECK(plane->isOnSurface(gctx, hit.position(), direction));
0176 }
0177 
0178 BOOST_AUTO_TEST_CASE(CurvatureCylinderAndFallbacks) {
0179   const auto cylinder =
0180       Surface::makeShared<CylinderSurface>(Transform3::Identity(), 20., 10.);
0181   const Vector3 direction = Vector3::UnitX();
0182   const auto hits =
0183       cylinder->intersect(gctx, Vector3::Zero(), direction,
0184                           Vector3(0., 0.005, 0.), BoundaryTolerance::None());
0185   BOOST_CHECK_EQUAL(hits.size(), 2u);
0186   for (const auto& hit : hits) {
0187     BOOST_REQUIRE(hit.isValid());
0188     CHECK_CLOSE_ABS(hit.position().head<2>().norm(), 20., 1e-12);
0189     BOOST_CHECK_GT(hit.position().y(), 0.);
0190   }
0191   CHECK_CLOSE_ABS(hits.at(0).pathLength(), -20., 1e-12);
0192   CHECK_CLOSE_ABS(hits.at(1).pathLength(), 20., 1e-12);
0193   Transform3 transform = Transform3::Identity();
0194   transform.translation() = Vector3(3., -4., 7.);
0195   transform.linear() =
0196       AngleAxis3(0.7, Vector3(1., 2., 3.).normalized()).toRotationMatrix();
0197   const auto moved = Surface::makeShared<CylinderSurface>(transform, 20., 10.);
0198   const auto movedHits = moved->intersect(
0199       gctx, transform * Vector3::Zero(), transform.linear() * direction,
0200       transform.linear() * Vector3(0., 0.005, 0.), BoundaryTolerance::None());
0201   CHECK_CLOSE_ABS(movedHits.at(1).position(), transform * hits.at(1).position(),
0202                   1e-12);
0203 
0204   const auto plane = Surface::makeShared<PlaneSurface>(
0205       Transform3(Translation3(0., 0., 20.)),
0206       std::make_shared<RectangleBounds>(10., 10.));
0207   for (const Vector3& curvature :
0208        {Vector3::Zero().eval(), Vector3(1., 0., 0.)}) {
0209     const auto straight =
0210         plane->intersect(gctx, Vector3::Zero(), Vector3::UnitZ());
0211     const auto fallback =
0212         plane->intersect(gctx, Vector3::Zero(), Vector3::UnitZ(), curvature,
0213                          BoundaryTolerance::None());
0214     CHECK_CLOSE_ABS(fallback.at(0).position(), straight.at(0).position(),
0215                     1e-12);
0216     BOOST_CHECK_EQUAL(fallback.at(0).pathLength(), straight.at(0).pathLength());
0217   }
0218   // At the plane but outside its finite bounds: prediction must not claim
0219   // arrival.
0220   const auto outside =
0221       plane->intersect(gctx, Vector3(11., 0., 20.), Vector3::UnitZ(),
0222                        Vector3(-0.005, 0., 0.), BoundaryTolerance::None());
0223   BOOST_CHECK(!outside.at(0).isValid());
0224   const auto inside =
0225       plane->intersect(gctx, Vector3(0., 0., 20.), Vector3::UnitZ(),
0226                        Vector3(0.005, 0., 0.), BoundaryTolerance::None());
0227   BOOST_CHECK(inside.at(0).status() == IntersectionStatus::onSurface);
0228 }
0229 BOOST_AUTO_TEST_CASE(CurvatureRequiresRaySeedAndFallsBackAtGrazingIncidence) {
0230   const auto plane = Surface::makeShared<PlaneSurface>(
0231       Transform3(Translation3(0., 0., 1e-8)),
0232       std::make_shared<RectangleBounds>(10., 10.));
0233   const Vector3 start = Vector3::Zero();
0234   const Vector3 curvature(0., 0., 0.001);
0235   const Vector3 grazing = Vector3(1., 0., 1e-7).normalized();
0236   // Use a tighter arrival tolerance so the start is not already on the plane.
0237   const double tolerance = 1e-12;
0238   const auto ray = plane
0239                        ->intersect(gctx, start, grazing,
0240                                    BoundaryTolerance::None(), tolerance)
0241                        .at(0);
0242   const auto fallback = plane
0243                             ->intersect(gctx, start, grazing, curvature,
0244                                         BoundaryTolerance::None(), tolerance)
0245                             .at(0);
0246   BOOST_REQUIRE(ray.isValid());
0247   CHECK_CLOSE_ABS(fallback.position(), ray.position(), 1e-12);
0248   CHECK_CLOSE_ABS(fallback.pathLength(), ray.pathLength(), 1e-12);
0249   const auto parallel =
0250       plane
0251           ->intersect(gctx, start, Vector3::UnitX(), curvature,
0252                       BoundaryTolerance::None(), tolerance)
0253           .at(0);
0254   BOOST_CHECK(!parallel.isValid());
0255 }
0256 
0257 BOOST_AUTO_TEST_CASE(CurvatureSurfaceDispatchAndDiscBounds) {
0258   const auto disc = Surface::makeShared<DiscSurface>(
0259       Transform3(Translation3(0., 0., 20.)),
0260       std::make_shared<RadialBounds>(0.5, 1.5, 0.2));
0261   const Surface& surface = *disc;
0262   const Vector3 origin = Vector3::Zero();
0263   const Vector3 direction = Vector3::UnitZ();
0264   BOOST_CHECK(
0265       !surface.intersect(gctx, origin, direction, BoundaryTolerance::None())
0266            .at(0)
0267            .isValid());
0268   const auto hit =
0269       surface
0270           .intersect(gctx, origin, direction, Vector3(0.005, 0., 0.),
0271                      BoundaryTolerance::None())
0272           .at(0);
0273   BOOST_REQUIRE(hit.isValid());
0274   CHECK_CLOSE_ABS(hit.position(), Vector3(1., 0., 20.), 1e-12);
0275   BOOST_CHECK(!disc->intersect(gctx, origin, direction, Vector3(0., 0.005, 0.),
0276                                BoundaryTolerance::None())
0277                    .at(0)
0278                    .isValid());
0279   BOOST_CHECK(!disc->intersect(gctx, origin, direction, Vector3(0.01, 0., 0.),
0280                                BoundaryTolerance::None())
0281                    .at(0)
0282                    .isValid());
0283   BOOST_CHECK(disc->intersect(gctx, origin, direction, Vector3(0.01, 0., 0.),
0284                               BoundaryTolerance::AbsoluteEuclidean(0.6))
0285                   .at(0)
0286                   .isValid());
0287   BOOST_CHECK(disc->intersect(gctx, origin, direction, Vector3(0., 0.005, 0.),
0288                               BoundaryTolerance::Infinite())
0289                   .at(0)
0290                   .isValid());
0291 
0292   // Unsupported shapes inherit the ray implementation; neither concrete nor
0293   // polymorphic calls require a curvature override.
0294   const auto point = Surface::makeShared<PointSurface>(Vector3(0., 0., 20.));
0295   const Surface& unsupported = *point;
0296   const auto ray = point->intersect(gctx, origin, direction).at(0);
0297   for (const auto& result :
0298        {unsupported.intersect(gctx, origin, direction, Vector3(0.005, 0., 0.)),
0299         point->intersect(gctx, origin, direction, Vector3(0.005, 0., 0.))}) {
0300     CHECK_CLOSE_ABS(result.at(0).position(), ray.position(), 1e-12);
0301     BOOST_CHECK_EQUAL(result.at(0).pathLength(), ray.pathLength());
0302   }
0303 }
0304 
0305 BOOST_AUTO_TEST_CASE(CurvatureCustomSurfaceKeepsRayImplementation) {
0306   const SurfaceStub custom;
0307   const Surface& surface = custom;
0308   const auto ray = surface.intersect(gctx, Vector3::Zero(), Vector3::UnitX());
0309   const auto result = surface.intersect(gctx, Vector3::Zero(), Vector3::UnitX(),
0310                                         Vector3(0., 0.001, 0.));
0311   BOOST_REQUIRE_EQUAL(result.size(), ray.size());
0312   CHECK_CLOSE_ABS(result.at(0).position(), ray.at(0).position(), 1e-12);
0313   BOOST_CHECK_EQUAL(result.at(0).pathLength(), ray.at(0).pathLength());
0314   BOOST_CHECK(result.at(1).status() == ray.at(1).status());
0315 }
0316 
0317 BOOST_AUTO_TEST_CASE(CurvatureCylinderBoundsAndArrival) {
0318   const auto cylinder =
0319       Surface::makeShared<CylinderSurface>(Transform3::Identity(), 20., 0.5);
0320   const Vector3 start = Vector3::Zero();
0321   const Vector3 direction = Vector3::UnitX();
0322   const Vector3 curvature(0., 0., 0.005);
0323   for (const auto& tolerance :
0324        {BoundaryTolerance::None(), BoundaryTolerance::AbsoluteEuclidean(0.6),
0325         BoundaryTolerance::Infinite()}) {
0326     const auto result =
0327         cylinder->intersect(gctx, start, direction, curvature, tolerance);
0328     BOOST_CHECK_EQUAL(result.size(), 2u);
0329     for (const auto& hit : result) {
0330       // Use the surface's existing tolerance semantics, including at the
0331       // azimuthal seam of the negative root.
0332       BOOST_CHECK_EQUAL(
0333           hit.isValid(),
0334           cylinder->isOnSurface(gctx, hit.position(), direction, tolerance));
0335       CHECK_CLOSE_ABS(hit.position().z(), 1., 1e-12);
0336       CHECK_CLOSE_ABS(hit.position().head<2>().norm(), 20., 1e-12);
0337     }
0338     BOOST_CHECK_EQUAL(result.at(1).isValid(), !tolerance.isNone());
0339     CHECK_CLOSE_ABS(result.at(0).pathLength(), -20., 1e-12);
0340     CHECK_CLOSE_ABS(result.at(1).pathLength(), 20., 1e-12);
0341   }
0342   const auto arrival =
0343       cylinder->intersect(gctx, Vector3(20., 0., 0.), direction, curvature,
0344                           BoundaryTolerance::None());
0345   BOOST_CHECK(arrival.at(1).status() == IntersectionStatus::onSurface);
0346   CHECK_CLOSE_ABS(arrival.at(1).position(), Vector3(20., 0., 0.), 1e-12);
0347   // A rejected correction must retain the original expanded ray-length bound.
0348   const Vector3 edge(0., 0., 0.5 + 0.5 * s_onSurfaceTolerance);
0349   const auto fallback = cylinder->intersect(
0350       gctx, edge, direction, Vector3(0., 1., 0.), BoundaryTolerance::None());
0351   BOOST_CHECK(fallback.at(0).isValid());
0352   BOOST_CHECK(fallback.at(1).isValid());
0353   // In contrast, accepted predictions check the actual requested bounds.
0354   const auto corrected = cylinder->intersect(
0355       gctx, edge, direction, Vector3(0., 0.001, 0.), BoundaryTolerance::None());
0356   BOOST_CHECK(!corrected.at(0).isValid());
0357   BOOST_CHECK(!corrected.at(1).isValid());
0358 }
0359 
0360 BOOST_AUTO_TEST_CASE(CurvatureInvalidInputAndTangentBranches) {
0361   const auto cylinder =
0362       Surface::makeShared<CylinderSurface>(Transform3::Identity(), 20., 10.);
0363   const Vector3 start(20., -5., 0.);
0364   const Vector3 direction = Vector3::UnitY();
0365   const auto ray =
0366       cylinder->intersect(gctx, start, direction, BoundaryTolerance::None());
0367   for (const Vector3& curvature :
0368        {Vector3::Zero().eval(), Vector3(0.001, 0., 0.),
0369         Vector3(std::numeric_limits<double>::quiet_NaN(), 0., 0.)}) {
0370     const auto result = cylinder->intersect(gctx, start, direction, curvature,
0371                                             BoundaryTolerance::None());
0372     BOOST_REQUIRE_EQUAL(result.size(), 2u);
0373     for (std::size_t i = 0; i < result.size(); ++i) {
0374       BOOST_CHECK(result.at(i).status() == ray.at(i).status());
0375       CHECK_CLOSE_ABS(result.at(i).position(), ray.at(i).position(), 1e-12);
0376       CHECK_CLOSE_ABS(result.at(i).pathLength(), ray.at(i).pathLength(), 1e-12);
0377     }
0378   }
0379 }
0380 
0381 }  // namespace ActsTests