File indexing completed on 2026-11-30 09:27:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Definitions/Tolerance.hpp"
0014 #include "Acts/Definitions/Units.hpp"
0015 #include "Acts/Geometry/Blueprint.hpp"
0016 #include "Acts/Geometry/BlueprintNode.hpp"
0017 #include "Acts/Geometry/ContainerBlueprintNode.hpp"
0018 #include "Acts/Geometry/CylinderPortalShell.hpp"
0019 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0020 #include "Acts/Geometry/GeometryContext.hpp"
0021 #include "Acts/Geometry/NavigationPolicyFactory.hpp"
0022 #include "Acts/Geometry/StaticBlueprintNode.hpp"
0023 #include "Acts/Geometry/TrackingVolume.hpp"
0024 #include "Acts/Navigation/CylinderNavigationPolicy.hpp"
0025 #include "Acts/Navigation/FrustumNavigationPolicy.hpp"
0026 #include "Acts/Navigation/INavigationPolicy.hpp"
0027 #include "Acts/Navigation/MultiNavigationPolicy.hpp"
0028 #include "Acts/Navigation/NavigationDelegate.hpp"
0029 #include "Acts/Navigation/NavigationStream.hpp"
0030 #include "Acts/Navigation/SurfaceArrayNavigationPolicy.hpp"
0031 #include "Acts/Navigation/TryAllNavigationPolicy.hpp"
0032 #include "Acts/Surfaces/PlaneSurface.hpp"
0033 #include "Acts/Surfaces/RectangleBounds.hpp"
0034 #include "Acts/Surfaces/SurfaceArray.hpp"
0035 #include "Acts/Utilities/Logger.hpp"
0036
0037 #include <array>
0038 #include <cmath>
0039 #include <numbers>
0040
0041 #include <boost/algorithm/string/join.hpp>
0042
0043 using namespace Acts;
0044 using namespace Acts::UnitLiterals;
0045 namespace bdata = boost::unit_test::data;
0046
0047 namespace ActsTests {
0048
0049 BOOST_AUTO_TEST_SUITE(NavigationSuite)
0050
0051 auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0052 auto logger = getDefaultLogger("NavigationPolicyTests", Logging::VERBOSE);
0053
0054 struct APolicy : public INavigationPolicy {
0055 APolicy(const GeometryContext& , const TrackingVolume& ,
0056 const Logger& ) {}
0057
0058 void initializeCandidates(const GeometryContext& ,
0059 const NavigationArguments& ,
0060 NavigationPolicyState& ,
0061 AppendOnlyNavigationStream& ,
0062 const Logger& ) const {
0063 const_cast<APolicy*>(this)->executed = true;
0064 }
0065
0066 void connect(NavigationDelegate& delegate) const override {
0067 connectDefault<APolicy>(delegate);
0068 }
0069
0070 bool executed = false;
0071 };
0072
0073 struct BPolicy : public INavigationPolicy {
0074 struct Config {
0075 int value;
0076 };
0077
0078 BPolicy(const GeometryContext& , const TrackingVolume& ,
0079 const Logger& , Config config)
0080 : m_config(config) {}
0081
0082 void connect(NavigationDelegate& delegate) const override {
0083 connectDefault<BPolicy>(delegate);
0084 }
0085
0086 void initializeCandidates(const GeometryContext& ,
0087 const NavigationArguments& ,
0088 NavigationPolicyState& ,
0089 AppendOnlyNavigationStream& ,
0090 const Logger& ) const {
0091 const_cast<BPolicy*>(this)->executed = true;
0092 const_cast<BPolicy*>(this)->value = m_config.value;
0093 }
0094
0095 bool executed = false;
0096 int value = 0;
0097
0098 Config m_config;
0099 };
0100
0101 BOOST_AUTO_TEST_CASE(DirectTest) {
0102 TrackingVolume volume{
0103 Transform3::Identity(),
0104 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0105 "PixelLayer3"};
0106
0107 MultiNavigationPolicy policy{
0108 std::make_unique<APolicy>(gctx, volume, *logger),
0109 std::make_unique<BPolicy>(gctx, volume, *logger,
0110 BPolicy::Config{.value = 4242})};
0111
0112 NavigationDelegate delegate;
0113 policy.connect(delegate);
0114
0115 NavigationStream main;
0116 AppendOnlyNavigationStream stream{main};
0117 NavigationArguments args{.position = Vector3::Zero(),
0118 .direction = Vector3::Zero()};
0119 NavigationPolicyStateManager stateManager;
0120 stateManager.pushState<MultiNavigationPolicy::State>();
0121 policy.createState(gctx, args, stateManager, *logger);
0122 auto policyState = stateManager.currentState();
0123 delegate(gctx, args, policyState, stream, *logger);
0124
0125 BOOST_REQUIRE_EQUAL(policy.policies().size(), 2);
0126 const auto& policyA = dynamic_cast<const APolicy&>(*policy.policies()[0]);
0127 const auto& policyB = dynamic_cast<const BPolicy&>(*policy.policies()[1]);
0128
0129 BOOST_CHECK(policyA.executed);
0130 BOOST_CHECK(policyB.executed);
0131 BOOST_CHECK_EQUAL(policyB.value, 4242);
0132 }
0133
0134 BOOST_AUTO_TEST_CASE(FactoryTest) {
0135 TrackingVolume volume{
0136 Transform3::Identity(),
0137 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0138 "PixelLayer3"};
0139
0140 BPolicy::Config config{.value = 42};
0141
0142 std::function<std::unique_ptr<INavigationPolicy>(
0143 const GeometryContext&, const TrackingVolume&, const Logger&)>
0144 factory = NavigationPolicyFactory{}
0145 .add<APolicy>()
0146 .add<BPolicy>(config);
0147
0148 auto policyBase = factory(gctx, volume, *logger);
0149 auto policyBase2 = factory(gctx, volume, *logger);
0150
0151 auto& policy = dynamic_cast<MultiNavigationPolicy&>(*policyBase);
0152
0153 NavigationDelegate delegate;
0154 policy.connect(delegate);
0155
0156 NavigationStream main;
0157 AppendOnlyNavigationStream stream{main};
0158 NavigationArguments args{.position = Vector3::Zero(),
0159 .direction = Vector3::Zero()};
0160 NavigationPolicyStateManager stateManager;
0161 stateManager.pushState<MultiNavigationPolicy::State>();
0162 policy.createState(gctx, args, stateManager, *logger);
0163 auto policyState = stateManager.currentState();
0164 delegate(gctx, args, policyState, stream, *logger);
0165
0166 BOOST_REQUIRE_EQUAL(policy.policies().size(), 2);
0167 const auto& policyA = dynamic_cast<const APolicy&>(*policy.policies()[0]);
0168 const auto& policyB = dynamic_cast<const BPolicy&>(*policy.policies()[1]);
0169
0170 BOOST_CHECK(policyA.executed);
0171 BOOST_CHECK(policyB.executed);
0172 BOOST_CHECK_EQUAL(policyB.value, 42);
0173
0174 auto& policy2 = dynamic_cast<MultiNavigationPolicy&>(*policyBase2);
0175
0176 NavigationDelegate delegate2;
0177 policyBase2->connect(delegate2);
0178
0179 NavigationPolicyStateManager stateManager2;
0180 stateManager2.pushState<MultiNavigationPolicy::State>();
0181 policy2.createState(gctx, args, stateManager2, *logger);
0182 auto policyState2 = stateManager2.currentState();
0183 delegate2(gctx, args, policyState2, stream, *logger);
0184
0185 BOOST_REQUIRE_EQUAL(policy2.policies().size(), 2);
0186 const auto& policy2A = dynamic_cast<const APolicy&>(*policy2.policies()[0]);
0187 const auto& policy2B = dynamic_cast<const BPolicy&>(*policy2.policies()[1]);
0188
0189 BOOST_CHECK(policy2A.executed);
0190 BOOST_CHECK(policy2B.executed);
0191 BOOST_CHECK_EQUAL(policy2B.value, 42);
0192 }
0193
0194 BOOST_AUTO_TEST_CASE(AsUniquePtrTest) {
0195 TrackingVolume volume{
0196 Transform3::Identity(),
0197 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0198 "PixelLayer3"};
0199
0200 std::unique_ptr<NavigationPolicyFactory> factory =
0201 NavigationPolicyFactory{}.add<APolicy>().asUniquePtr();
0202
0203 auto policyBase = factory->build(gctx, volume, *logger);
0204 auto& policy = dynamic_cast<MultiNavigationPolicy&>(*policyBase);
0205
0206 NavigationDelegate delegate;
0207 policyBase->connect(delegate);
0208
0209 NavigationStream main;
0210 AppendOnlyNavigationStream stream{main};
0211 NavigationArguments args{.position = Vector3::Zero(),
0212 .direction = Vector3::Zero()};
0213 NavigationPolicyStateManager stateManager;
0214 stateManager.pushState<MultiNavigationPolicy::State>();
0215 policy.createState(gctx, args, stateManager, *logger);
0216 auto policyState = stateManager.currentState();
0217 delegate(gctx, args, policyState, stream, *logger);
0218
0219 BOOST_REQUIRE_EQUAL(policy.policies().size(), 1);
0220 BOOST_CHECK(dynamic_cast<const APolicy&>(*policy.policies()[0]).executed);
0221 }
0222
0223 struct CPolicy : public INavigationPolicy {};
0224
0225 template <typename T>
0226 struct CPolicySpecialized : public CPolicy {
0227 struct Config {
0228 T value;
0229 };
0230
0231 CPolicySpecialized(const TrackingVolume& , Config config)
0232 : m_config(config) {}
0233
0234 void connect(NavigationDelegate& delegate) const override {
0235 connectDefault<CPolicySpecialized<T>>(delegate);
0236 }
0237
0238 void initializeCandidates(const GeometryContext& ,
0239 const NavigationArguments& ,
0240 NavigationPolicyState& ,
0241 AppendOnlyNavigationStream& ,
0242 const Logger& ) const {
0243 auto* self = const_cast<CPolicySpecialized<int>*>(this);
0244 self->executed = true;
0245 self->value = m_config.value;
0246 }
0247
0248 bool executed = false;
0249 int value = 0;
0250
0251 Config m_config;
0252 };
0253
0254 struct IsolatedConfig {
0255 int value;
0256 };
0257
0258 std::unique_ptr<INavigationPolicy> makeCPolicy(const GeometryContext& ,
0259 const TrackingVolume& volume,
0260 const Logger& ,
0261 IsolatedConfig config) {
0262
0263
0264 CPolicySpecialized<int>::Config config2{.value = config.value};
0265 return std::make_unique<CPolicySpecialized<int>>(volume, config2);
0266 }
0267
0268 BOOST_AUTO_TEST_CASE(IsolatedFactory) {
0269 TrackingVolume volume{
0270 Transform3::Identity(),
0271 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0272 "PixelLayer3"};
0273
0274 IsolatedConfig config{.value = 44};
0275 auto factory =
0276 NavigationPolicyFactory{}.add<APolicy>().add(makeCPolicy, config);
0277
0278 auto factory2 =
0279 NavigationPolicyFactory{}.add(makeCPolicy, config).add<APolicy>();
0280
0281 auto policyBase = factory(gctx, volume, *logger);
0282 auto& policy = dynamic_cast<MultiNavigationPolicy&>(*policyBase);
0283
0284 NavigationDelegate delegate;
0285 policyBase->connect(delegate);
0286
0287 NavigationStream main;
0288 AppendOnlyNavigationStream stream{main};
0289 NavigationArguments args{.position = Vector3::Zero(),
0290 .direction = Vector3::Zero()};
0291 NavigationPolicyStateManager stateManager;
0292 stateManager.pushState<MultiNavigationPolicy::State>();
0293 policy.createState(gctx, args, stateManager, *logger);
0294 auto policyState = stateManager.currentState();
0295 delegate(gctx, args, policyState, stream, *logger);
0296
0297 BOOST_REQUIRE_EQUAL(policy.policies().size(), 2);
0298
0299 const auto& policyA = dynamic_cast<const APolicy&>(*policy.policies()[0]);
0300 const auto& cPolicy =
0301 dynamic_cast<const CPolicySpecialized<int>&>(*policy.policies()[1]);
0302
0303 BOOST_CHECK(policyA.executed);
0304 BOOST_CHECK(cPolicy.executed);
0305 BOOST_CHECK_EQUAL(cPolicy.value, 44);
0306 }
0307
0308 namespace {
0309
0310 std::vector<const Portal*> getTruth(const Vector3& position,
0311 const Vector3& direction,
0312 const Transform3& transform,
0313 const TrackingVolume& cylVolume,
0314 SingleCylinderPortalShell& shell,
0315 const Logger& logger, bool posOnly = true) {
0316 Vector3 gpos = transform * position;
0317 Vector3 gdir = transform.linear() * direction;
0318 TryAllNavigationPolicy tryAll(gctx, cylVolume, logger);
0319 NavigationArguments args{.position = gpos, .direction = gdir};
0320 NavigationStream main;
0321 AppendOnlyNavigationStream stream{main};
0322 auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0323 NavigationPolicyStateManager stateManager;
0324 tryAll.createState(gctx, args, stateManager, logger);
0325 auto policyState = stateManager.currentState();
0326 tryAll.initializeCandidates(gctx, args, policyState, stream, logger);
0327 main.initialize(gctx, {gpos, gdir}, logger());
0328 std::vector<const Portal*> portals;
0329 for (auto& candidate : main.candidates()) {
0330 if (!candidate.intersection().isValid()) {
0331 continue;
0332 }
0333
0334 if (main.candidates().size() > 1 && posOnly &&
0335 !detail::checkPathLength(candidate.intersection().pathLength(),
0336 s_onSurfaceTolerance,
0337 std::numeric_limits<double>::max(), logger)) {
0338 continue;
0339 }
0340
0341 portals.push_back(&candidate.portal());
0342 }
0343
0344
0345 const Portal* outerCylinder = nullptr;
0346 const Portal* innerCylinder = nullptr;
0347 const Portal* positiveDisc = nullptr;
0348 const Portal* negativeDisc = nullptr;
0349
0350 for (const Portal* portal : portals) {
0351 if (portal ==
0352 shell.portal(CylinderVolumeBounds::Face::OuterCylinder).get()) {
0353 outerCylinder = portal;
0354 } else if (portal ==
0355 shell.portal(CylinderVolumeBounds::Face::InnerCylinder).get()) {
0356 innerCylinder = portal;
0357 } else if (portal ==
0358 shell.portal(CylinderVolumeBounds::Face::PositiveDisc).get()) {
0359 positiveDisc = portal;
0360 } else if (portal ==
0361 shell.portal(CylinderVolumeBounds::Face::NegativeDisc).get()) {
0362 negativeDisc = portal;
0363 }
0364 }
0365
0366
0367 std::vector<const Portal*> filteredPortals;
0368
0369
0370
0371 if ((innerCylinder != nullptr) && (outerCylinder != nullptr)) {
0372
0373 filteredPortals.push_back(innerCylinder);
0374 } else {
0375
0376 if (innerCylinder != nullptr) {
0377 filteredPortals.push_back(innerCylinder);
0378 }
0379 if (outerCylinder != nullptr) {
0380 filteredPortals.push_back(outerCylinder);
0381 }
0382 }
0383
0384
0385
0386 if (innerCylinder == nullptr) {
0387
0388 if (positiveDisc != nullptr) {
0389 filteredPortals.push_back(positiveDisc);
0390 }
0391 if (negativeDisc != nullptr) {
0392 filteredPortals.push_back(negativeDisc);
0393 }
0394 }
0395
0396
0397 return filteredPortals;
0398 }
0399
0400 std::vector<const Portal*> getSmart(const Vector3& position,
0401 const Vector3& direction,
0402 const Transform3& transform,
0403 CylinderNavigationPolicy& policy) {
0404 Vector3 gpos = transform * position;
0405 Vector3 gdir = transform.linear() * direction;
0406 NavigationArguments args{.position = gpos, .direction = gdir};
0407 NavigationStream main;
0408 auto gctx = GeometryContext::dangerouslyDefaultConstruct();
0409 AppendOnlyNavigationStream stream{main};
0410 NavigationPolicyStateManager stateManager;
0411 policy.createState(gctx, args, stateManager, *logger);
0412 auto policyState = stateManager.currentState();
0413 policy.initializeCandidates(gctx, args, policyState, stream, *logger);
0414
0415 std::vector<const Portal*> portals;
0416
0417
0418 for (auto& candidate : main.candidates()) {
0419 portals.push_back(&candidate.portal());
0420 }
0421 return portals;
0422 }
0423
0424 void checkEqual(const std::vector<const Portal*>& exp,
0425 const std::vector<const Portal*>& act,
0426 SingleCylinderPortalShell& shell) {
0427 auto which = [&](const Portal* p) -> std::string {
0428 if (p == shell.portal(CylinderVolumeBounds::Face::InnerCylinder).get()) {
0429 return "InnerCylinder";
0430 }
0431 if (p == shell.portal(CylinderVolumeBounds::Face::OuterCylinder).get()) {
0432 return "OuterCylinder";
0433 }
0434 if (p == shell.portal(CylinderVolumeBounds::Face::PositiveDisc).get()) {
0435 return "PositiveDisc";
0436 }
0437 if (p == shell.portal(CylinderVolumeBounds::Face::NegativeDisc).get()) {
0438 return "NegativeDisc";
0439 }
0440 BOOST_FAIL("Unknown portal");
0441 return "";
0442 };
0443
0444 std::set<const Portal*> expSet;
0445 std::set<const Portal*> actSet;
0446
0447 std::ranges::copy(exp, std::inserter(expSet, expSet.begin()));
0448 std::ranges::copy(act, std::inserter(actSet, actSet.begin()));
0449
0450 if (expSet != actSet) {
0451 BOOST_ERROR([&]() -> std::string {
0452 std::vector<std::string> exps;
0453 for (auto& p : exp) {
0454 exps.push_back(which(p));
0455 }
0456 std::vector<std::string> acts;
0457 for (auto& p : act) {
0458 acts.push_back(which(p));
0459 }
0460 return "[" + boost::algorithm::join(exps, ", ") + "] != [" +
0461 boost::algorithm::join(acts, ", ") + "]";
0462 }());
0463 }
0464 }
0465
0466 }
0467
0468 BOOST_DATA_TEST_CASE(
0469 CylinderPolicyTest,
0470 (bdata::xrange(-135, 180, 45) *
0471 bdata::make(Vector3{0_mm, 0_mm, 0_mm}, Vector3{20_mm, 0_mm, 0_mm},
0472 Vector3{0_mm, 20_mm, 0_mm}, Vector3{20_mm, 20_mm, 0_mm},
0473 Vector3{0_mm, 0_mm, 20_mm})),
0474 angle, offset) {
0475 using enum CylinderVolumeBounds::Face;
0476
0477 Transform3 transform = Transform3::Identity();
0478 transform *= AngleAxis3{angle * 1_degree, Vector3::UnitX()};
0479 transform *= Translation3{offset};
0480 auto cylBounds =
0481 std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 300_mm);
0482 auto cylVolume =
0483 std::make_shared<TrackingVolume>(transform, cylBounds, "CylinderVolume");
0484 SingleCylinderPortalShell shell{gctx, *cylVolume};
0485 shell.applyToVolume();
0486
0487 {
0488 Vector3 position = Vector3::UnitX() * 150_mm;
0489 Vector3 direction = Vector3::UnitZ();
0490
0491 auto exp =
0492 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0493
0494 BOOST_CHECK(exp.size() == 1);
0495 BOOST_CHECK(exp.at(0) == shell.portal(PositiveDisc).get());
0496
0497 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0498 auto act = getSmart(position, direction, transform, policy);
0499 checkEqual(exp, act, shell);
0500 }
0501
0502 {
0503 Vector3 position = Vector3::UnitX() * 150_mm;
0504 Vector3 direction = Vector3{1, 1, 0}.normalized();
0505
0506 auto exp =
0507 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0508
0509 BOOST_CHECK(exp.size() == 1);
0510 BOOST_CHECK(exp.at(0) == shell.portal(OuterCylinder).get());
0511
0512 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0513 auto act = getSmart(position, direction, transform, policy);
0514 checkEqual(exp, act, shell);
0515 }
0516
0517 {
0518 Vector3 position = Vector3::UnitX() * 150_mm;
0519 Vector3 direction = Vector3{-1, 0, 0}.normalized();
0520
0521 auto exp =
0522 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0523
0524 BOOST_CHECK(exp.size() == 1);
0525 BOOST_CHECK(exp.at(0) == shell.portal(InnerCylinder).get());
0526
0527 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0528 auto act = getSmart(position, direction, transform, policy);
0529 checkEqual(exp, act, shell);
0530 }
0531
0532 {
0533 Vector3 position = Vector3::UnitX() * 150_mm;
0534 Vector3 direction = -Vector3::UnitZ();
0535
0536 auto exp =
0537 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0538
0539 BOOST_CHECK(exp.size() == 1);
0540 BOOST_CHECK(exp.at(0) == shell.portal(NegativeDisc).get());
0541
0542 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0543 auto act = getSmart(position, direction, transform, policy);
0544 checkEqual(exp, act, shell);
0545 }
0546
0547 {
0548 Vector3 position{50, -200, 0};
0549 Vector3 direction = Vector3{0, 1.5, 1}.normalized();
0550
0551 auto exp =
0552 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0553
0554 BOOST_CHECK(exp.size() == 1);
0555 BOOST_CHECK(exp.at(0) == shell.portal(InnerCylinder).get());
0556
0557 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0558 auto act = getSmart(position, direction, transform, policy);
0559 checkEqual(exp, act, shell);
0560 }
0561
0562 {
0563 Vector3 position{50, -200, 0};
0564 Vector3 direction = Vector3{0, 1.2, 1}.normalized();
0565
0566 auto exp =
0567 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0568
0569 BOOST_CHECK(exp.size() == 1);
0570 BOOST_CHECK(exp.at(0) == shell.portal(InnerCylinder).get());
0571
0572 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0573 auto act = getSmart(position, direction, transform, policy);
0574 checkEqual(exp, act, shell);
0575 }
0576
0577 {
0578 Vector3 position{50, -200, 0};
0579 Vector3 direction = Vector3{0, 0.9, 1}.normalized();
0580
0581 auto exp =
0582 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0583
0584 BOOST_CHECK(exp.size() == 1);
0585 BOOST_CHECK(exp.at(0) == shell.portal(InnerCylinder).get());
0586
0587 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0588 auto act = getSmart(position, direction, transform, policy);
0589 checkEqual(exp, act, shell);
0590 }
0591
0592 {
0593 Vector3 position{20, -200, 0};
0594 Vector3 direction = Vector3{0.45, 0.9, 1}.normalized();
0595
0596 auto exp =
0597 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0598
0599 BOOST_CHECK(exp.size() == 1);
0600 BOOST_CHECK(exp.at(0) == shell.portal(PositiveDisc).get());
0601
0602 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0603 auto act = getSmart(position, direction, transform, policy);
0604 checkEqual(exp, act, shell);
0605 }
0606
0607 {
0608 Vector3 position{20, -200, 0};
0609 Vector3 direction = Vector3{0.45, 0.9, -1}.normalized();
0610
0611 auto exp =
0612 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0613
0614 BOOST_CHECK(exp.size() == 1);
0615 BOOST_CHECK(exp.at(0) == shell.portal(NegativeDisc).get());
0616
0617 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0618 auto act = getSmart(position, direction, transform, policy);
0619 checkEqual(exp, act, shell);
0620 }
0621
0622 {
0623 Vector3 position{400 * std::cos(std::numbers::pi / 4),
0624 400 * std::sin(std::numbers::pi / 4), 0};
0625 Vector3 direction = Vector3{0.45, -0.9, -0.1}.normalized();
0626
0627
0628
0629 auto exp = getTruth(position, direction, transform, *cylVolume, shell,
0630 *logger, false);
0631
0632 BOOST_CHECK(exp.size() == 1);
0633 BOOST_CHECK(exp.at(0) == shell.portal(OuterCylinder).get());
0634
0635 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0636 auto act = getSmart(position, direction, transform, policy);
0637 checkEqual(exp, act, shell);
0638 }
0639
0640 {
0641 Vector3 position{400 * std::cos(std::numbers::pi / 4),
0642 400 * std::sin(std::numbers::pi / 4), 0};
0643 Vector3 direction = Vector3{-0.3, -0.9, -0.1}.normalized();
0644
0645
0646
0647 auto exp = getTruth(position, direction, transform, *cylVolume, shell,
0648 *logger, false);
0649
0650 BOOST_CHECK(exp.size() == 1);
0651 BOOST_CHECK(exp.at(0) == shell.portal(OuterCylinder).get());
0652
0653 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0654 auto act = getSmart(position, direction, transform, policy);
0655 checkEqual(exp, act, shell);
0656 }
0657
0658 {
0659 Vector3 position{100 * std::cos(std::numbers::pi / 4),
0660 100 * std::sin(std::numbers::pi / 4), 0};
0661 double dangle = 0.1;
0662 Vector3 direction = Vector3{std::cos(dangle), std::sin(dangle), 0.01};
0663
0664
0665 auto exp =
0666 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0667
0668 BOOST_CHECK(exp.size() == 1);
0669 BOOST_CHECK(exp.at(0) == shell.portal(OuterCylinder).get());
0670
0671 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0672 auto act = getSmart(position, direction, transform, policy);
0673 checkEqual(exp, act, shell);
0674 }
0675
0676 {
0677 Vector3 position{200 * std::cos(std::numbers::pi / 4),
0678 200 * std::sin(std::numbers::pi / 4), 0};
0679 Vector3 target{150 * std::cos(std::numbers::pi * 5 / 4),
0680 150 * std::sin(std::numbers::pi * 5 / 4), 300};
0681 Vector3 direction = (target - position).normalized();
0682
0683 auto exp =
0684 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0685
0686 BOOST_CHECK_EQUAL(exp.size(), 1);
0687 BOOST_CHECK_EQUAL(exp.at(0), shell.portal(InnerCylinder).get());
0688
0689 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0690 auto act = getSmart(position, direction, transform, policy);
0691 checkEqual(exp, act, shell);
0692 }
0693 }
0694
0695 BOOST_AUTO_TEST_CASE(CylinderPolicyClosestApproachTest) {
0696 using enum CylinderVolumeBounds::Face;
0697
0698 struct TestCase {
0699 const char* name;
0700 Vector3 position;
0701 Vector3 direction;
0702 CylinderVolumeBounds::Face expected;
0703 };
0704 const std::array cases{
0705 TestCase{"radially inward", {150, 0, 0}, {-1, 0, 0}, InnerCylinder},
0706 TestCase{"radially outward", {150, 0, 0}, {1, 0, 0}, OuterCylinder},
0707 TestCase{"opposite side inward", {-150, 0, 0}, {1, 0, 0}, InnerCylinder},
0708 TestCase{
0709 "opposite side outward", {-150, 0, 0}, {-1, 0, 0}, OuterCylinder},
0710 TestCase{"zero radial projection", {150, 0, 0}, {0, 1, 0}, OuterCylinder},
0711 TestCase{"positive axial", {150, 0, 0}, {0, 0, 1}, PositiveDisc},
0712 TestCase{"negative axial", {150, 0, 0}, {0, 0, -1}, NegativeDisc},
0713 TestCase{"nearly axial", {150, 0, 0}, {-1e-12, 0, 1}, PositiveDisc},
0714 TestCase{"disc before closest approach",
0715 {150, 0, 290},
0716 {-1, 0, 1},
0717 PositiveDisc},
0718 TestCase{"negative disc before closest approach",
0719 {150, 0, -290},
0720 {-1, 0, -1},
0721 NegativeDisc},
0722 TestCase{"disc after closest approach",
0723 {150, 0, 0},
0724 {-1, 0, 1},
0725 InnerCylinder},
0726 TestCase{"negative disc after closest approach",
0727 {150, 0, 0},
0728 {-1, 0, -1},
0729 InnerCylinder},
0730 TestCase{
0731 "disc plane inside hole", {150, 0, 200}, {-1, 0, 1}, InnerCylinder},
0732 TestCase{"disc plane beyond outer radius",
0733 {150, 0, 0},
0734 {-2, 0, 1},
0735 InnerCylinder},
0736 TestCase{"just inside tangency",
0737 {100 - 1e-6, -200, 0},
0738 {0, 1, 0},
0739 InnerCylinder},
0740 TestCase{"just outside tangency",
0741 {100 + 1e-6, -200, 0},
0742 {0, 1, 0},
0743 OuterCylinder},
0744 TestCase{"from inner boundary", {100, 0, 0}, {1, 0, 0}, OuterCylinder},
0745 TestCase{"from outer boundary", {400, 0, 0}, {-1, 0, 0}, InnerCylinder},
0746 TestCase{"from disc boundary", {150, 0, 300}, {0, 0, -1}, NegativeDisc},
0747 };
0748
0749 const std::array<Transform3, 2> transforms{
0750 Transform3::Identity(),
0751 Transform3{Translation3{20, -30, 40} *
0752 AngleAxis3{0.7, Vector3{1, 2, 3}.normalized()}}};
0753 for (const auto& transform : transforms) {
0754 auto cylVolume = std::make_shared<TrackingVolume>(
0755 transform,
0756 std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 300_mm),
0757 "CylinderVolume");
0758 SingleCylinderPortalShell shell{gctx, *cylVolume};
0759 shell.applyToVolume();
0760 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0761
0762 for (const auto& test : cases) {
0763 BOOST_TEST_CONTEXT(test.name << " transform=" << transform.matrix()) {
0764 const auto actual = getSmart(test.position, test.direction.normalized(),
0765 transform, policy);
0766 checkEqual({shell.portal(test.expected).get()}, actual, shell);
0767 }
0768 }
0769 }
0770 }
0771
0772 BOOST_AUTO_TEST_CASE(CylinderPolicyExactTangencyTest) {
0773 auto cylVolume = std::make_shared<TrackingVolume>(
0774 Transform3::Identity(),
0775 std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 300_mm),
0776 "CylinderVolume");
0777 SingleCylinderPortalShell shell{gctx, *cylVolume};
0778 shell.applyToVolume();
0779 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0780
0781
0782
0783 const auto actual =
0784 getSmart({100, -200, 0}, {0, 1, 0}, Transform3::Identity(), policy);
0785 checkEqual({shell.portal(CylinderVolumeBounds::Face::OuterCylinder).get()},
0786 actual, shell);
0787 }
0788
0789 namespace {
0790
0791 std::mt19937 engine;
0792
0793 unsigned long seed() {
0794 static unsigned long s = 42;
0795 return s++;
0796 }
0797
0798 std::uniform_real_distribution<double> rDistOffBoundary{
0799 100 + 2 * s_onSurfaceTolerance, 400 - 2 * s_onSurfaceTolerance};
0800 std::uniform_real_distribution<double> zDistOffBoundary{
0801 -300_mm + 2 * s_onSurfaceTolerance, 300_mm - 2 * s_onSurfaceTolerance};
0802 std::uniform_real_distribution<double> phiDist{-std::numbers::pi,
0803 std::numbers::pi};
0804 std::uniform_real_distribution<double> thetaDist{0, std::numbers::pi};
0805
0806 }
0807
0808 BOOST_DATA_TEST_CASE(
0809 CylinderPolicyTestOffBoundary,
0810 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0811 bdata::distribution = rDistOffBoundary)) ^
0812 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0813 bdata::distribution = zDistOffBoundary)) ^
0814 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0815 bdata::distribution = phiDist)) ^
0816 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0817 bdata::distribution = phiDist)) ^
0818 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0819 bdata::distribution = thetaDist)) ^
0820 bdata::xrange(100),
0821 r, z, phiPos, phiDir, theta, index) {
0822 static_cast<void>(index);
0823
0824 Transform3 transform = Transform3::Identity();
0825 auto cylBounds =
0826 std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 300_mm);
0827 auto cylVolume =
0828 std::make_shared<TrackingVolume>(transform, cylBounds, "CylinderVolume");
0829 SingleCylinderPortalShell shell{gctx, *cylVolume};
0830 shell.applyToVolume();
0831
0832 Vector3 position{r * std::cos(phiPos), r * std::sin(phiPos), z};
0833 Vector3 direction{std::sin(theta) * std::cos(phiDir),
0834 std::sin(theta) * std::sin(phiDir), std::cos(theta)};
0835
0836 BOOST_CHECK(cylBounds->inside(position));
0837
0838 auto exp =
0839 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0840
0841 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0842 auto act = getSmart(position, direction, transform, policy);
0843 checkEqual(exp, act, shell);
0844 }
0845
0846 BOOST_DATA_TEST_CASE(
0847 CylinderPolicyTestOnRBoundary,
0848 bdata::make(100, 400) *
0849 (bdata::random((bdata::engine = engine, bdata::seed = seed(),
0850 bdata::distribution = zDistOffBoundary)) ^
0851 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0852 bdata::distribution = phiDist)) ^
0853 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0854 bdata::distribution = phiDist)) ^
0855 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0856 bdata::distribution = zDistOffBoundary)) ^
0857 bdata::xrange(100)),
0858 r, z, phiPos, phiTarget, zTarget, index) {
0859 static_cast<void>(index);
0860
0861 Transform3 transform = Transform3::Identity();
0862 auto cylBounds =
0863 std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 300_mm);
0864 auto cylVolume =
0865 std::make_shared<TrackingVolume>(transform, cylBounds, "CylinderVolume");
0866 SingleCylinderPortalShell shell{gctx, *cylVolume};
0867 shell.applyToVolume();
0868
0869 Vector3 position{r * std::cos(phiPos), r * std::sin(phiPos), z};
0870 Vector3 target{r * std::cos(phiTarget), r * std::sin(phiTarget), zTarget};
0871 Vector3 direction = (target - position).normalized();
0872
0873
0874
0875
0876 if (r == cylBounds->get(CylinderVolumeBounds::eMinR)) {
0877 Vector3 radial{std::cos(phiPos), std::sin(phiPos), 0.};
0878 direction -= 2. * direction.dot(radial) * radial;
0879 }
0880
0881 auto exp =
0882 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0883
0884 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0885 auto act = getSmart(position, direction, transform, policy);
0886 checkEqual(exp, act, shell);
0887 }
0888
0889 BOOST_DATA_TEST_CASE(
0890 CylinderPolicyTestOnZBoundary,
0891 bdata::make(-300, 300) *
0892 (bdata::random((bdata::engine = engine, bdata::seed = seed(),
0893 bdata::distribution = rDistOffBoundary)) ^
0894 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0895 bdata::distribution = phiDist)) ^
0896 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0897 bdata::distribution = phiDist)) ^
0898 bdata::random((bdata::engine = engine, bdata::seed = seed(),
0899 bdata::distribution = zDistOffBoundary)) ^
0900 bdata::xrange(100)),
0901 z, r, phiPos, phiTarget, zTarget, index) {
0902 static_cast<void>(index);
0903 Transform3 transform = Transform3::Identity();
0904 auto cylBounds =
0905 std::make_shared<CylinderVolumeBounds>(100_mm, 400_mm, 300_mm);
0906 auto cylVolume =
0907 std::make_shared<TrackingVolume>(transform, cylBounds, "CylinderVolume");
0908 SingleCylinderPortalShell shell{gctx, *cylVolume};
0909 shell.applyToVolume();
0910
0911 Vector3 position{r * std::cos(phiPos), r * std::sin(phiPos),
0912 static_cast<double>(z)};
0913 Vector3 target{r * std::cos(phiTarget), r * std::sin(phiTarget), zTarget};
0914 Vector3 direction = (target - position).normalized();
0915
0916 BOOST_CHECK(cylBounds->inside(position));
0917
0918 auto exp =
0919 getTruth(position, direction, transform, *cylVolume, shell, *logger);
0920
0921 CylinderNavigationPolicy policy(gctx, *cylVolume, *logger);
0922 auto act = getSmart(position, direction, transform, policy);
0923 checkEqual(exp, act, shell);
0924 }
0925
0926 BOOST_AUTO_TEST_CASE(CylinderPolicyZeroInnerRadiusTest) {
0927
0928 Transform3 transform = Transform3::Identity();
0929
0930
0931 auto cylBounds = std::make_shared<CylinderVolumeBounds>(0_mm, 400_mm, 300_mm);
0932 auto cylVolume = std::make_shared<TrackingVolume>(transform, cylBounds,
0933 "ZeroInnerRadiusVolume");
0934
0935
0936
0937 {
0938 Acts::Logging::ScopedFailureThreshold log{Logging::FATAL};
0939 BOOST_CHECK_THROW(CylinderNavigationPolicy(gctx, *cylVolume, *logger),
0940 std::invalid_argument);
0941 }
0942 }
0943
0944 BOOST_AUTO_TEST_CASE(FrustumNavigationPolicyTest) {
0945
0946 Blueprint::Config cfg;
0947 cfg.envelope[AxisDirection::AxisZ] = {50_mm, 50_mm};
0948 cfg.envelope[AxisDirection::AxisY] = {50_mm, 50_mm};
0949 cfg.envelope[AxisDirection::AxisX] = {50_mm, 50_mm};
0950 auto root = std::make_unique<Blueprint>(cfg);
0951 auto& cub = root->addCuboidContainer("Container", AxisDirection::AxisZ);
0952 auto cubBounds = std::make_shared<CuboidVolumeBounds>(20_mm, 20_mm, 20_mm);
0953 auto childCub = std::make_unique<TrackingVolume>(Transform3::Identity(),
0954 cubBounds, "child");
0955 Acts::Experimental::FrustumNavigationPolicy::Config frustumConfig{2};
0956 Acts::GeometryIdentifier id = Acts::GeometryIdentifier().withVolume(1);
0957 childCub->assignGeometryId(id);
0958 auto cubNode =
0959 std::make_shared<Acts::StaticBlueprintNode>(std::move(childCub));
0960 int chamberId = 1;
0961 auto childBounds = std::make_shared<CuboidVolumeBounds>(5_mm, 5_mm, 5_mm);
0962 std::vector<std::shared_ptr<Acts::StaticBlueprintNode>> nodes;
0963 for (int x = -3; x < 4; x += 2) {
0964 for (int y = -3; y < 4; y += 2) {
0965 for (int z = -3; z < 4; z += 2) {
0966 auto childVol = std::make_unique<TrackingVolume>(
0967 Transform3::Identity() *
0968 Translation3{Vector3{x * 5_mm, y * 5_mm, z * 5_mm}},
0969 childBounds,
0970 "child_" + std::to_string(x) + "_" + std::to_string(y) + "_" +
0971 std::to_string(z));
0972 Acts::GeometryIdentifier chId = id.withLayer(chamberId++);
0973 childVol->assignGeometryId(chId);
0974 auto staticNode =
0975 std::make_shared<Acts::StaticBlueprintNode>(std::move(childVol));
0976 nodes.push_back(std::move(staticNode));
0977 cubNode->addChild(nodes.back());
0978 }
0979 }
0980 }
0981 std::shared_ptr<Acts::NavigationPolicyFactory> factory =
0982 std::make_shared<Acts::NavigationPolicyFactory>(
0983 Acts::NavigationPolicyFactory{}
0984 .add<Acts::Experimental::FrustumNavigationPolicy>(frustumConfig));
0985 cubNode->setNavigationPolicyFactory(factory);
0986 cub.addChild(cubNode);
0987 auto tGeometry = root->construct({}, gctx, *logger);
0988 const TrackingVolume* vol = tGeometry->findVolume(id);
0989 NavigationStream main;
0990 AppendOnlyNavigationStream stream{main};
0991 NavigationArguments args{.position = Vector3{1, 1, 1},
0992 .direction = Vector3{1, 1, 1}};
0993 NavigationPolicyStateManager stateManager;
0994 std::unique_ptr<Acts::Experimental::FrustumNavigationPolicy> frustumPolicy =
0995 std::make_unique<Acts::Experimental::FrustumNavigationPolicy>(
0996 gctx, *vol, *logger, frustumConfig);
0997 frustumPolicy->createState(gctx, args, stateManager, *logger);
0998 auto policyState = stateManager.currentState();
0999 frustumPolicy->initializeCandidates(gctx, args, policyState, stream, *logger);
1000 main.initialize(gctx, {Vector3{1, 1, 1}, Vector3{1, 1, 1}}, *logger);
1001 BOOST_CHECK_EQUAL(main.candidates().size(), 10);
1002 }
1003
1004 BOOST_AUTO_TEST_CASE(SurfaceArrayPolicyNeighborWindowTest) {
1005
1006 auto cylBounds = std::make_shared<CylinderVolumeBounds>(20_mm, 40_mm, 100_mm);
1007 auto cylVolume = std::make_shared<TrackingVolume>(
1008 Transform3::Identity(), cylBounds, "SurfaceArrayVolume");
1009
1010 auto moduleBounds = std::make_shared<RectangleBounds>(8_mm, 30_mm);
1011 for (int iphi = 0; iphi < 12; ++iphi) {
1012 const double phi = (iphi + 0.5) * 2 * std::numbers::pi / 12;
1013
1014 RotationMatrix3 rotation;
1015 rotation.col(0) = Vector3{-std::sin(phi), std::cos(phi), 0};
1016 rotation.col(1) = Vector3::UnitZ();
1017 rotation.col(2) = Vector3{std::cos(phi), std::sin(phi), 0};
1018 for (int iz = -1; iz <= 1; ++iz) {
1019 Transform3 transform = Transform3::Identity();
1020 transform.linear() = rotation;
1021 transform.translation() =
1022 Vector3{30_mm * std::cos(phi), 30_mm * std::sin(phi), iz * 60_mm};
1023 auto surface = Surface::makeShared<PlaneSurface>(transform, moduleBounds);
1024 surface->assignIsSensitive(true);
1025 cylVolume->addSurface(surface);
1026 }
1027 }
1028
1029 using enum SurfaceArrayNavigationPolicy::LayerType;
1030
1031
1032 SurfaceArrayNavigationPolicy defaultPolicy(
1033 gctx, *cylVolume, *logger,
1034 SurfaceArrayNavigationPolicy::Config{.layerType = Cylinder,
1035 .bins = {0, 0}});
1036 const auto defaultWindow = defaultPolicy.surfaceArray().neighborWindow();
1037 BOOST_CHECK_EQUAL(defaultWindow.max[0], 1);
1038 BOOST_CHECK_EQUAL(defaultWindow.max[1], 2);
1039
1040
1041 SurfaceArrayNavigationPolicy windowPolicy(
1042 gctx, *cylVolume, *logger,
1043 SurfaceArrayNavigationPolicy::Config{
1044 .layerType = Cylinder,
1045 .bins = {0, 0},
1046 .numberOfBinsFactor = 5.0,
1047 .neighborWindow = SurfaceArray::NeighborWindow{{0, 1}, {1, 8}},
1048 .overfill = 1});
1049 const auto window = windowPolicy.surfaceArray().neighborWindow();
1050 BOOST_CHECK_EQUAL(window.min[0], 0);
1051 BOOST_CHECK_EQUAL(window.min[1], 1);
1052 BOOST_CHECK_EQUAL(window.max[0], 1);
1053 BOOST_CHECK_EQUAL(window.max[1], 8);
1054 }
1055
1056 BOOST_AUTO_TEST_SUITE_END()
1057
1058 }