File indexing completed on 2025-01-18 09:12:45
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010
0011 #include "Acts/Definitions/Units.hpp"
0012 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0013 #include "Acts/Geometry/NavigationPolicyFactory.hpp"
0014 #include "Acts/Geometry/TrackingVolume.hpp"
0015 #include "Acts/Navigation/INavigationPolicy.hpp"
0016 #include "Acts/Navigation/MultiNavigationPolicy.hpp"
0017 #include "Acts/Navigation/NavigationDelegate.hpp"
0018 #include "Acts/Navigation/NavigationStream.hpp"
0019 #include "Acts/Navigation/TryAllNavigationPolicy.hpp"
0020
0021 using namespace Acts;
0022 using namespace Acts::UnitLiterals;
0023
0024 BOOST_AUTO_TEST_SUITE(NavigationPolicyTests)
0025
0026 GeometryContext gctx;
0027 auto logger = getDefaultLogger("NavigationPolicyTests", Logging::VERBOSE);
0028
0029 struct APolicy : public INavigationPolicy {
0030 APolicy(const GeometryContext& , const TrackingVolume& ,
0031 const Logger& ) {}
0032
0033 void initializeCandidates(const NavigationArguments& ,
0034 AppendOnlyNavigationStream& ,
0035 const Logger& ) const {
0036 const_cast<APolicy*>(this)->executed = true;
0037 }
0038
0039 void connect(NavigationDelegate& delegate) const override {
0040 connectDefault<APolicy>(delegate);
0041 }
0042
0043 bool executed = false;
0044 };
0045
0046 struct BPolicy : public INavigationPolicy {
0047 struct Config {
0048 int value;
0049 };
0050
0051 BPolicy(const GeometryContext& , const TrackingVolume& ,
0052 const Logger& , Config config)
0053 : m_config(config) {}
0054
0055 void connect(NavigationDelegate& delegate) const override {
0056 connectDefault<BPolicy>(delegate);
0057 }
0058
0059 void initializeCandidates(const NavigationArguments& ,
0060 AppendOnlyNavigationStream& ,
0061 const Logger& ) const {
0062 const_cast<BPolicy*>(this)->executed = true;
0063 const_cast<BPolicy*>(this)->value = m_config.value;
0064 }
0065
0066 bool executed = false;
0067 int value = 0;
0068
0069 Config m_config;
0070 };
0071
0072 BOOST_AUTO_TEST_CASE(DirectTest) {
0073 TrackingVolume volume{
0074 Transform3::Identity(),
0075 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0076 "PixelLayer3"};
0077
0078 MultiNavigationPolicy policy{APolicy{gctx, volume, *logger},
0079 BPolicy{gctx, volume, *logger, {.value = 4242}}};
0080
0081 NavigationDelegate delegate;
0082 policy.connect(delegate);
0083
0084 NavigationStream main;
0085 AppendOnlyNavigationStream stream{main};
0086 delegate(NavigationArguments{.position = Vector3::Zero(),
0087 .direction = Vector3::Zero()},
0088 stream, *logger);
0089
0090 BOOST_CHECK(std::get<APolicy>(policy.policies()).executed);
0091 BOOST_CHECK(std::get<BPolicy>(policy.policies()).executed);
0092 BOOST_CHECK_EQUAL(std::get<BPolicy>(policy.policies()).value, 4242);
0093 }
0094
0095 BOOST_AUTO_TEST_CASE(FactoryTest) {
0096 TrackingVolume volume{
0097 Transform3::Identity(),
0098 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0099 "PixelLayer3"};
0100
0101 BPolicy::Config config{.value = 42};
0102
0103 std::function<std::unique_ptr<INavigationPolicy>(
0104 const GeometryContext&, const TrackingVolume&, const Logger&)>
0105 factory = NavigationPolicyFactory::make()
0106 .add<APolicy>()
0107 .add<BPolicy>(config);
0108
0109 auto policyBase = factory(gctx, volume, *logger);
0110 auto policyBase2 = factory(gctx, volume, *logger);
0111
0112 auto& policy =
0113 dynamic_cast<MultiNavigationPolicy<APolicy, BPolicy>&>(*policyBase);
0114
0115 NavigationDelegate delegate;
0116 policy.connect(delegate);
0117
0118 NavigationStream main;
0119 AppendOnlyNavigationStream stream{main};
0120 delegate(NavigationArguments{.position = Vector3::Zero(),
0121 .direction = Vector3::Zero()},
0122 stream, *logger);
0123
0124 BOOST_CHECK(std::get<APolicy>(policy.policies()).executed);
0125 BOOST_CHECK(std::get<BPolicy>(policy.policies()).executed);
0126 BOOST_CHECK_EQUAL(std::get<BPolicy>(policy.policies()).value, 42);
0127
0128 auto& policy2 =
0129 dynamic_cast<MultiNavigationPolicy<APolicy, BPolicy>&>(*policyBase2);
0130
0131 NavigationDelegate delegate2;
0132 policyBase2->connect(delegate2);
0133
0134 delegate2(NavigationArguments{.position = Vector3::Zero(),
0135 .direction = Vector3::Zero()},
0136 stream, *logger);
0137
0138 BOOST_CHECK(std::get<APolicy>(policy2.policies()).executed);
0139 BOOST_CHECK(std::get<BPolicy>(policy2.policies()).executed);
0140 BOOST_CHECK_EQUAL(std::get<BPolicy>(policy2.policies()).value, 42);
0141 }
0142
0143 BOOST_AUTO_TEST_CASE(AsUniquePtrTest) {
0144 TrackingVolume volume{
0145 Transform3::Identity(),
0146 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0147 "PixelLayer3"};
0148
0149 std::unique_ptr<NavigationPolicyFactory> factory =
0150 NavigationPolicyFactory::make().add<APolicy>().asUniquePtr();
0151
0152 auto policyBase = factory->build(gctx, volume, *logger);
0153 auto& policy = dynamic_cast<MultiNavigationPolicy<APolicy>&>(*policyBase);
0154
0155 NavigationDelegate delegate;
0156 policyBase->connect(delegate);
0157
0158 NavigationStream main;
0159 AppendOnlyNavigationStream stream{main};
0160 delegate(NavigationArguments{.position = Vector3::Zero(),
0161 .direction = Vector3::Zero()},
0162 stream, *logger);
0163
0164 BOOST_CHECK(std::get<APolicy>(policy.policies()).executed);
0165 }
0166
0167 struct CPolicy : public INavigationPolicy {};
0168
0169 template <typename T>
0170 struct CPolicySpecialized : public CPolicy {
0171 struct Config {
0172 T value;
0173 };
0174
0175 CPolicySpecialized(const TrackingVolume& , Config config)
0176 : m_config(config) {}
0177
0178 void connect(NavigationDelegate& delegate) const override {
0179 connectDefault<CPolicySpecialized<T>>(delegate);
0180 }
0181
0182 void initializeCandidates(const NavigationArguments& ,
0183 AppendOnlyNavigationStream& ,
0184 const Logger& ) const {
0185 auto* self = const_cast<CPolicySpecialized<int>*>(this);
0186 self->executed = true;
0187 self->value = m_config.value;
0188 }
0189
0190 bool executed = false;
0191 int value = 0;
0192
0193 Config m_config;
0194 };
0195
0196 struct IsolatedConfig {
0197 int value;
0198 };
0199
0200 auto makeCPolicy(const GeometryContext& , const TrackingVolume& volume,
0201 const Logger& , IsolatedConfig config) {
0202
0203 CPolicySpecialized<int>::Config config2{.value = config.value};
0204 return CPolicySpecialized<int>(volume, config2);
0205 }
0206
0207 BOOST_AUTO_TEST_CASE(IsolatedFactory) {
0208 TrackingVolume volume{
0209 Transform3::Identity(),
0210 std::make_shared<CylinderVolumeBounds>(250_mm, 400_mm, 310_mm),
0211 "PixelLayer3"};
0212
0213 IsolatedConfig config{.value = 44};
0214 auto factory =
0215 NavigationPolicyFactory::make().add<APolicy>().add(makeCPolicy, config);
0216
0217 auto factory2 =
0218 NavigationPolicyFactory::make().add(makeCPolicy, config).add<APolicy>();
0219
0220 auto policyBase = factory(gctx, volume, *logger);
0221 auto& policy =
0222 dynamic_cast<MultiNavigationPolicy<APolicy, CPolicySpecialized<int>>&>(
0223 *policyBase);
0224
0225 NavigationDelegate delegate;
0226 policyBase->connect(delegate);
0227
0228 NavigationStream main;
0229 AppendOnlyNavigationStream stream{main};
0230 delegate(NavigationArguments{.position = Vector3::Zero(),
0231 .direction = Vector3::Zero()},
0232 stream, *logger);
0233
0234 BOOST_CHECK(std::get<APolicy>(policy.policies()).executed);
0235 BOOST_CHECK(std::get<CPolicySpecialized<int>>(policy.policies()).executed);
0236 BOOST_CHECK_EQUAL(std::get<CPolicySpecialized<int>>(policy.policies()).value,
0237 44);
0238 }
0239
0240 BOOST_AUTO_TEST_SUITE_END()