Skip to content

Commit

Permalink
Merge branch 'main' into chore/pr-template-again
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Nov 13, 2024
2 parents 5b09c40 + 3e71b0c commit 4e6f9fe
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
7 changes: 4 additions & 3 deletions Core/include/Acts/Navigation/TryAllNavigationPolicy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ class TryAllNavigationPolicy final : public INavigationPolicy {
};

/// Constructor from a volume
/// @param config The configuration for the policy
/// @param gctx is the geometry context
/// @param volume is the volume to navigate
/// @param logger is the logger
TryAllNavigationPolicy(const Config& config, const GeometryContext& gctx,
const TrackingVolume& volume, const Logger& logger);
/// @param config The configuration for the policy
TryAllNavigationPolicy(const GeometryContext& gctx,
const TrackingVolume& volume, const Logger& logger,
const Config& config);

/// Constructor from a volume
/// @param gctx is the geometry context
Expand Down
22 changes: 22 additions & 0 deletions Core/include/Acts/Utilities/detail/ContextType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ class ContextType {
return std::any_cast<const std::decay_t<T>&>(m_data);
}

/// Retrieve a pointer to the contained type
///
/// @note Returns `nullptr` if @p is not the contained type.
///
/// @tparam T The type to attempt to retrieve the value as
/// @return Pointer to the contained value, may be null
template <typename T>
std::decay_t<T>* maybeGet() {
return std::any_cast<std::decay_t<T>>(&m_data);
}

/// Retrieve a pointer to the contained type
///
/// @note Returns `nullptr` if @p is not the contained type.
///
/// @tparam T The type to attempt to retrieve the value as
/// @return Pointer to the contained value, may be null
template <typename T>
const std::decay_t<T>* maybeGet() const {
return std::any_cast<const std::decay_t<T>>(&m_data);
}

/// Check if the contained type is initialized.
/// @return Boolean indicating whether a type is present
bool hasValue() const { return m_data.has_value(); }
Expand Down
8 changes: 4 additions & 4 deletions Core/src/Navigation/TryAllNavigationPolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Acts {

TryAllNavigationPolicy::TryAllNavigationPolicy(const Config& config,
const GeometryContext& /*gctx*/,
TryAllNavigationPolicy::TryAllNavigationPolicy(const GeometryContext& /*gctx*/,
const TrackingVolume& volume,
const Logger& logger)
const Logger& logger,
const Config& config)
: m_cfg{config}, m_volume(&volume) {
assert(m_volume != nullptr);
ACTS_VERBOSE("TryAllNavigationPolicy created for volume "
Expand All @@ -26,7 +26,7 @@ TryAllNavigationPolicy::TryAllNavigationPolicy(const Config& config,
TryAllNavigationPolicy::TryAllNavigationPolicy(const GeometryContext& gctx,
const TrackingVolume& volume,
const Logger& logger)
: TryAllNavigationPolicy({}, gctx, volume, logger) {}
: TryAllNavigationPolicy(gctx, volume, logger, {}) {}

void TryAllNavigationPolicy::initializeCandidates(
const NavigationArguments& args, AppendOnlyNavigationStream& stream,
Expand Down
28 changes: 27 additions & 1 deletion Examples/Python/src/Navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ struct AnyNavigationPolicyFactory : public Acts::NavigationPolicyFactory {
virtual std::unique_ptr<AnyNavigationPolicyFactory> add(
TypeTag<SurfaceArrayNavigationPolicy> /*type*/,
SurfaceArrayNavigationPolicy::Config config) = 0;

virtual std::unique_ptr<AnyNavigationPolicyFactory> add(
TypeTag<TryAllNavigationPolicy> /*type*/,
TryAllNavigationPolicy::Config config) = 0;
};

template <typename Factory = detail::NavigationPolicyFactoryImpl<>,
Expand All @@ -61,6 +65,12 @@ struct NavigationPolicyFactoryT : public AnyNavigationPolicyFactory {
return add<SurfaceArrayNavigationPolicy>(std::move(config));
}

std::unique_ptr<AnyNavigationPolicyFactory> add(
TypeTag<TryAllNavigationPolicy> /*type*/,
TryAllNavigationPolicy::Config config) override {
return add<TryAllNavigationPolicy>(config);
}

std::unique_ptr<INavigationPolicy> build(
const GeometryContext& gctx, const TrackingVolume& volume,
const Logger& logger) const override {
Expand Down Expand Up @@ -108,6 +118,12 @@ class NavigationPolicyFactory : public Acts::NavigationPolicyFactory {
return *this;
}

NavigationPolicyFactory& addTryAll(
const py::object& /*cls*/, const TryAllNavigationPolicy::Config& config) {
m_impl = m_impl->add(Type<TryAllNavigationPolicy>, config);
return *this;
}

std::unique_ptr<INavigationPolicy> build(
const GeometryContext& gctx, const TrackingVolume& volume,
const Logger& logger) const override {
Expand Down Expand Up @@ -153,7 +169,16 @@ void addNavigation(Context& ctx) {
std::shared_ptr<Acts::NavigationPolicyFactory>>(
m, "_NavigationPolicyFactory");

py::class_<TryAllNavigationPolicy>(m, "TryAllNavigationPolicy");
{
auto tryAll =
py::class_<TryAllNavigationPolicy>(m, "TryAllNavigationPolicy");
using Config = TryAllNavigationPolicy::Config;
auto c = py::class_<Config>(tryAll, "Config").def(py::init<>());
ACTS_PYTHON_STRUCT_BEGIN(c, Config);
ACTS_PYTHON_MEMBER(portals);
ACTS_PYTHON_MEMBER(sensitives);
ACTS_PYTHON_STRUCT_END();
}

py::class_<NavigationPolicyFactory, Acts::NavigationPolicyFactory,
std::shared_ptr<NavigationPolicyFactory>>(
Expand All @@ -162,6 +187,7 @@ void addNavigation(Context& ctx) {
.def_static("make", []() { return NavigationPolicyFactory{}; })
.def("add", &NavigationPolicyFactory::addNoArguments)
.def("add", &NavigationPolicyFactory::addSurfaceArray)
.def("add", &NavigationPolicyFactory::addTryAll)
.def("_buildTest", [](NavigationPolicyFactory& self) {
auto vol1 = std::make_shared<TrackingVolume>(
Transform3::Identity(),
Expand Down
6 changes: 6 additions & 0 deletions Examples/Python/tests/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ def test_navigation_policy_factory_add_multiple():
.add(acts.TryAllNavigationPolicy)
.add(acts.TryAllNavigationPolicy)
)


def test_try_all_arguments():
acts.NavigationPolicyFactory.make().add(
acts.TryAllNavigationPolicy, acts.TryAllNavigationPolicy.Config(sensitives=True)
)
1 change: 1 addition & 0 deletions Tests/UnitTests/Core/Utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ add_unittest(VectorHelpers VectorHelpersTests.cpp)

add_unittest(TrackHelpers TrackHelpersTests.cpp)
add_unittest(GraphViz GraphVizTests.cpp)
add_unittest(ContextType ContextTypeTests.cpp)
38 changes: 38 additions & 0 deletions Tests/UnitTests/Core/Utilities/ContextTypeTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include <boost/test/tools/old/interface.hpp>
#include <boost/test/unit_test.hpp>

#include "Acts/Utilities/detail/ContextType.hpp"

using namespace Acts;

BOOST_AUTO_TEST_SUITE(ContextTypeTests)

BOOST_AUTO_TEST_CASE(PackUnpack) {
ContextType ctx;

int v = 42;
ctx = v;

BOOST_CHECK_EQUAL(ctx.get<int>(), 42);
BOOST_CHECK_THROW(ctx.get<double>(), std::bad_any_cast);
}

BOOST_AUTO_TEST_CASE(MaybeUnpack) {
ContextType ctx;

int v = 42;
ctx = v;

BOOST_CHECK_EQUAL(*ctx.maybeGet<int>(), 42);
BOOST_CHECK_EQUAL(ctx.maybeGet<double>(), nullptr);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 4e6f9fe

Please sign in to comment.