Skip to content

Commit

Permalink
refactor: Indexed surfaces generator (#2234)
Browse files Browse the repository at this point in the history
This PR mainly changes the `IndexedSurfacesGenerator.hpp ` in order to accept other types of Navigation Delegates for the indexed surfaces, by providing a template class to the struct.
  • Loading branch information
dimitra97 authored Jul 20, 2023
1 parent 375f1a9 commit 4e5afb2
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 97 deletions.
63 changes: 3 additions & 60 deletions Core/include/Acts/Detector/detail/IndexedGridFiller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,66 +40,9 @@ namespace detail {
/// @note for closed binning a span over half the bins flips direction
///
/// @return a vector of bins to be filled
static inline std::vector<std::size_t> binSequence(
std::array<std::size_t, 2u> minMaxBins, std::size_t expand,
std::size_t nBins, Acts::detail::AxisBoundaryType type) {
// Return vector for iterations
std::vector<std::size_t> rBins;
/// Helper method to fill a range
///
/// @param lmin the minimum bin
/// @param lmax the maximum bin
auto fill_linear = [&](std::size_t lmin, std::size_t lmax) -> void {
for (std::size_t b = lmin; b <= lmax; ++b) {
rBins.push_back(b);
}
};
std::size_t bmin = minMaxBins[0u];
std::size_t bmax = minMaxBins[1u];

// Open/Bound cases
if (type != Acts::detail::AxisBoundaryType::Closed) {
rBins.reserve(bmax - bmin + 1u + 2 * expand);
// handle bmin:/max expand it down (for bound, don't fill underflow)
if (type == Acts::detail::AxisBoundaryType::Bound) {
bmin = (int(bmin) - int(expand) > 0) ? bmin - expand : 1u;
bmax = (bmax + expand <= nBins) ? bmax + expand : nBins;
} else if (type == Acts::detail::AxisBoundaryType::Open) {
bmin = (int(bmin) - int(expand) >= 0) ? bmin - expand : 0u;
bmax = (bmax + expand <= nBins + 1u) ? bmax + expand : nBins + 1u;
}
fill_linear(bmin, bmax);
} else {
// Close case
std::size_t span = bmax - bmin + 1u + 2 * expand;
// Safe with respect to the closure point, treat as bound
if (2 * span < nBins and (bmax + expand <= nBins) and
(int(bmin) - int(expand) > 0)) {
return binSequence({bmin, bmax}, expand, nBins,
Acts::detail::AxisBoundaryType::Bound);
} else if (2 * span < nBins) {
bmin = int(bmin) - int(expand) > 0 ? bmin - expand : 1u;
bmax = bmax + expand <= nBins ? bmax + expand : nBins;
fill_linear(bmin, bmax);
// deal with expansions over the phi boundary
if (bmax + expand > nBins) {
std::size_t overstep = (bmax + expand - nBins);
fill_linear(1u, overstep);
}
if (int(bmin) - int(expand) < 1) {
std::size_t understep = abs(int(bmin) - int(expand));
fill_linear(nBins - understep, nBins);
}
std::sort(rBins.begin(), rBins.end());
} else {
// Jump over the phi boundary
fill_linear(bmax - expand, nBins);
fill_linear(1, bmin + expand);
std::sort(rBins.begin(), rBins.end());
}
}
return rBins;
}
std::vector<std::size_t> binSequence(std::array<std::size_t, 2u> minMaxBins,
std::size_t expand, std::size_t nBins,
Acts::detail::AxisBoundaryType type);

/// @brief Helper method to fill local bins given a set of query points
/// bin in between the extra points are filled, and a possible expansion
Expand Down
13 changes: 8 additions & 5 deletions Core/include/Acts/Detector/detail/IndexedSurfacesGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace detail {
/// - a chosen expansion to fill indices in neighborhood bins
///
/// @tparam objects_container the objects container
template <typename surface_container>
template <typename surface_container, template <typename> class indexed_updator>
struct IndexedSurfacesGenerator {
/// The surfaces to be indexed
/// (including surfaces that are assigned to all bins)
Expand Down Expand Up @@ -76,8 +76,10 @@ struct IndexedSurfacesGenerator {
}

// The indexed surfaces delegate
IndexedSurfacesImpl<GridType> indexedSurfaces(std::move(grid), bvArray,
transform);
// IndexedSurfacesImpl<GridType> indexedSurfaces(std::move(grid), bvArray,
// transform);
indexed_updator<GridType> indexedSurfaces(std::move(grid), bvArray,
transform);

// Fill the bin indices
IndexedGridFiller filler{binExpansion};
Expand All @@ -88,9 +90,10 @@ struct IndexedSurfacesGenerator {
AllPortalsImpl allPortals;

// The chained delegate: indexed surfaces and all portals
using DelegateType = IndexedSurfacesAllPortalsImpl<decltype(grid)>;
using DelegateType =
IndexedSurfacesAllPortalsImpl<decltype(grid), indexed_updator>;
auto indesSurfacesAllPortals = std::make_unique<const DelegateType>(
std::tie(indexedSurfaces, allPortals));
std::tie(allPortals, indexedSurfaces));

// Create the delegate and connect it
SurfaceCandidatesUpdator nStateUpdator;
Expand Down
6 changes: 4 additions & 2 deletions Core/include/Acts/Navigation/SurfaceCandidatesUpdators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ using IndexedSurfacesImpl =
IndexedUpdatorImpl<grid_type, IndexedSurfacesExtractor, SurfacesFiller>;

/// @brief An indexed surface implementation with portal access
template <typename grid_type>
///
///@tparam inexed_updator is the updator for the indexed surfaces
template <typename grid_type, template <typename> class indexed_updator>
using IndexedSurfacesAllPortalsImpl =
ChainedUpdatorImpl<IndexedSurfacesImpl<grid_type>, AllPortalsImpl>;
ChainedUpdatorImpl<AllPortalsImpl, indexed_updator<grid_type>>;

} // namespace Experimental
} // namespace Acts
5 changes: 5 additions & 0 deletions Core/include/Acts/Utilities/detail/Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ class Grid final {
return grid_helper::getUpperRightBinEdge(localBins, m_axes);
}

/// @brief get bin width along each specific axis
///
/// @return array giving the bin width alonf all axes
point_t binWidth() const { return grid_helper::getWidth(m_axes); }

/// @brief get number of bins along each specific axis
///
/// @return array giving the number of bins along all axes
Expand Down
1 change: 1 addition & 0 deletions Core/src/Detector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ target_sources(
detail/CylindricalDetectorHelper.cpp
detail/PortalHelper.cpp
detail/SupportHelper.cpp
detail/IndexedGridFiller.cpp
CylindricalContainerBuilder.cpp
Detector.cpp
DetectorBuilder.cpp
Expand Down
16 changes: 9 additions & 7 deletions Core/src/Detector/LayerStructureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ Acts::Experimental::SurfaceCandidatesUpdator createUpdator(
Acts::Experimental::SurfaceCandidatesUpdator sfCandidates;
Acts::Experimental::detail::PolyhedronReferenceGenerator rGenerator;
// Indexed Surface generator for this case
Acts::Experimental::detail::IndexedSurfacesGenerator<decltype(lSurfaces)> isg{
lSurfaces, assignToAll, {binning.binValue}, {binning.expansion}};
Acts::Experimental::detail::IndexedSurfacesGenerator<
decltype(lSurfaces), Acts::Experimental::IndexedSurfacesImpl>
isg{lSurfaces, assignToAll, {binning.binValue}, {binning.expansion}};
if (binning.axisType == Acts::detail::AxisType::Equidistant) {
// Equidistant
Acts::Experimental::detail::GridAxisGenerators::Eq<aType> aGenerator{
Expand Down Expand Up @@ -98,11 +99,12 @@ Acts::Experimental::SurfaceCandidatesUpdator createUpdator(
Acts::Experimental::SurfaceCandidatesUpdator sfCandidates;
Acts::Experimental::detail::PolyhedronReferenceGenerator rGenerator;
// Indexed Surface generator for this case
Acts::Experimental::detail::IndexedSurfacesGenerator<decltype(lSurfaces)> isg{
lSurfaces,
assignToAll,
{aBinning.binValue, bBinning.binValue},
{aBinning.expansion, bBinning.expansion}};
Acts::Experimental::detail::IndexedSurfacesGenerator<
decltype(lSurfaces), Acts::Experimental::IndexedSurfacesImpl>
isg{lSurfaces,
assignToAll,
{aBinning.binValue, bBinning.binValue},
{aBinning.expansion, bBinning.expansion}};
// Run through the cases
if (aBinning.axisType == Acts::detail::AxisType::Equidistant and
bBinning.axisType == Acts::detail::AxisType::Equidistant) {
Expand Down
87 changes: 87 additions & 0 deletions Core/src/Detector/detail/IndexedGridFiller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.

#include "Acts/Detector/detail/IndexedGridFiller.hpp"

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Definitions/Common.hpp"
#include "Acts/Detector/detail/ReferenceGenerators.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/Geometry/Polyhedron.hpp"
#include "Acts/Navigation/SurfaceCandidatesUpdators.hpp"
#include "Acts/Utilities/Delegate.hpp"
#include "Acts/Utilities/Enumerate.hpp"
#include "Acts/Utilities/IAxis.hpp"
#include "Acts/Utilities/Logger.hpp"

#include <algorithm>
#include <array>
#include <set>
#include <string>
#include <vector>

std::vector<std::size_t> Acts::Experimental::detail::binSequence(
std::array<std::size_t, 2u> minMaxBins, std::size_t expand,
std::size_t nBins, Acts::detail::AxisBoundaryType type) {
// Return vector for iterations
std::vector<std::size_t> rBins;
/// Helper method to fill a range
///
/// @param lmin the minimum bin
/// @param lmax the maximum bin
auto fill_linear = [&](std::size_t lmin, std::size_t lmax) -> void {
for (std::size_t b = lmin; b <= lmax; ++b) {
rBins.push_back(b);
}
};
std::size_t bmin = minMaxBins[0u];
std::size_t bmax = minMaxBins[1u];

// Open/Bound cases
if (type != Acts::detail::AxisBoundaryType::Closed) {
rBins.reserve(bmax - bmin + 1u + 2 * expand);
// handle bmin:/max expand it down (for bound, don't fill underflow)
if (type == Acts::detail::AxisBoundaryType::Bound) {
bmin = (int(bmin) - int(expand) > 0) ? bmin - expand : 1u;
bmax = (bmax + expand <= nBins) ? bmax + expand : nBins;
} else if (type == Acts::detail::AxisBoundaryType::Open) {
bmin = (int(bmin) - int(expand) >= 0) ? bmin - expand : 0u;
bmax = (bmax + expand <= nBins + 1u) ? bmax + expand : nBins + 1u;
}
fill_linear(bmin, bmax);
} else {
// Close case
std::size_t span = bmax - bmin + 1u + 2 * expand;
// Safe with respect to the closure point, treat as bound
if (2 * span < nBins and (bmax + expand <= nBins) and
(int(bmin) - int(expand) > 0)) {
return binSequence({bmin, bmax}, expand, nBins,
Acts::detail::AxisBoundaryType::Bound);
} else if (2 * span < nBins) {
bmin = int(bmin) - int(expand) > 0 ? bmin - expand : 1u;
bmax = bmax + expand <= nBins ? bmax + expand : nBins;
fill_linear(bmin, bmax);
// deal with expansions over the phi boundary
if (bmax + expand > nBins) {
std::size_t overstep = (bmax + expand - nBins);
fill_linear(1u, overstep);
}
if (int(bmin) - int(expand) < 1) {
std::size_t understep = abs(int(bmin) - int(expand));
fill_linear(nBins - understep, nBins);
}
std::sort(rBins.begin(), rBins.end());
} else {
// Jump over the phi boundary
fill_linear(bmax - expand, nBins);
fill_linear(1, bmin + expand);
std::sort(rBins.begin(), rBins.end());
}
}
return rBins;
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ void convert(const GeometryContext& gctx, const surface_container& surfaces,
using GridType =
typename instance_type::template grid_type<std::vector<std::size_t>>;
// Defining a Delegate type
using DelegateType = Experimental::IndexedSurfacesAllPortalsImpl<GridType>;
using DelegateType = Experimental::IndexedSurfacesAllPortalsImpl<
GridType, Experimental::IndexedSurfacesImpl>;
using SubDelegateType = Experimental::IndexedSurfacesImpl<GridType>;

// Get the instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ void convert(nlohmann::json& jIndexedSurfaces,
using GridType =
typename instance_type::template grid_type<std::vector<std::size_t>>;
// Defining a Delegate type
using DelegateType = Experimental::IndexedSurfacesAllPortalsImpl<GridType>;
using DelegateType = Experimental::IndexedSurfacesAllPortalsImpl<
GridType, Experimental::IndexedSurfacesImpl>;
using SubDelegateType = Experimental::IndexedSurfacesImpl<GridType>;

// Get the instance
Expand Down
6 changes: 3 additions & 3 deletions Plugins/Json/src/IndexedSurfacesJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ Acts::Experimental::SurfaceCandidatesUpdator createUpdator(
Acts::Experimental::AllPortalsImpl allPortals;

// The chained delegate: indexed surfaces and all portals
using DelegateType =
Acts::Experimental::IndexedSurfacesAllPortalsImpl<grid_type>;
using DelegateType = Acts::Experimental::IndexedSurfacesAllPortalsImpl<
grid_type, Acts::Experimental::IndexedSurfacesImpl>;
auto indexedSurfacesAllPortals = std::make_unique<const DelegateType>(
std::tie(indexedSurfaces, allPortals));
std::tie(allPortals, indexedSurfaces));

// Create the delegate and connect it
Acts::Experimental::SurfaceCandidatesUpdator nStateUpdator;
Expand Down
Loading

0 comments on commit 4e5afb2

Please sign in to comment.