Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-04 07:57:47

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 #pragma once
0010 
0011 #include "Acts/Utilities/Grid.hpp"
0012 #include "Acts/Utilities/TypeTraits.hpp"
0013 
0014 #include <type_traits>
0015 
0016 namespace Acts {
0017 
0018 namespace detail {
0019 
0020 /// @brief Base class for type-safe views into grid objects
0021 ///
0022 /// @tparam T Type of values stored in the grid
0023 /// @tparam isConst Whether this view provides const access (true) or mutable access (false)
0024 ///
0025 /// This class provides a type-safe interface to access grid objects through the
0026 /// type-erased IGrid interface. It ensures that the grid being viewed contains
0027 /// values of the expected type T.
0028 template <typename T, bool isConst>
0029 class AnyGridViewBase {
0030  public:
0031   /// Type of pointer to grid, const or non-const depending on isConst
0032   using GridPointerType = std::conditional_t<isConst, const IGrid*, IGrid*>;
0033   /// Type for indices, imported from IGrid
0034   using AnyIndexType = IGrid::AnyIndexType;
0035   /// Type for points, imported from IGrid
0036   using AnyPointType = IGrid::AnyPointType;
0037 
0038   /// @brief Constructor from const IGrid reference
0039   /// @param grid The grid to view
0040   /// @note This constructor is only available for const views (isConst=true)
0041   explicit AnyGridViewBase(const IGrid& grid)
0042     requires(isConst)
0043       : m_grid(&grid) {
0044     checkType();
0045   }
0046 
0047   /// @brief Constructor from non-const IGrid reference
0048   /// @param grid The grid to view
0049   explicit AnyGridViewBase(IGrid& grid) : m_grid(&grid) { checkType(); }
0050 
0051   /// @brief Constructor from non-const concrete Grid reference
0052   /// @tparam Axes Parameter pack of axis types defining the grid
0053   /// @param grid The concrete grid to view
0054   /// @note This constructor is only available for non-const views (isConst=false)
0055   template <typename... Axes>
0056   explicit AnyGridViewBase(Grid<T, Axes...>& grid)
0057     requires(!isConst)
0058       : m_grid(&grid) {
0059     checkType();
0060   }
0061 
0062   /// @brief Constructor from const concrete Grid reference
0063   /// @tparam Axes Parameter pack of axis types defining the grid
0064   /// @param grid The concrete grid to view
0065   /// @note This constructor is only available for const views (isConst=true)
0066   template <typename... Axes>
0067   explicit AnyGridViewBase(const Grid<T, Axes...>& grid)
0068     requires(isConst)
0069       : m_grid(&grid) {
0070     checkType();
0071   }
0072 
0073   /// Copy constructor
0074   AnyGridViewBase(const AnyGridViewBase& other) = default;
0075   /// Copy assignment operator
0076   AnyGridViewBase& operator=(const AnyGridViewBase& other) = default;
0077 
0078   /// Move constructor
0079   AnyGridViewBase(AnyGridViewBase&&) noexcept = default;
0080   /// Move assignment operator
0081   AnyGridViewBase& operator=(AnyGridViewBase&&) noexcept = default;
0082 
0083   /// @brief Access value at given local bin indices with mutable access
0084   /// @param indices The local bin indices
0085   /// @return Reference to the value at the specified bin
0086   /// @note This method is only available for non-const views (isConst=false)
0087   /// @throws std::invalid_argument if indices size doesn't match grid dimensions
0088   /// @throws std::out_of_range if indices are out of bounds
0089   T& atLocalBins(const AnyIndexType& indices)
0090     requires(!isConst)
0091   {
0092     std::any any = m_grid->atLocalBinsAny(indices);
0093     return *std::any_cast<T*>(any);
0094   }
0095 
0096   /// @brief Access value at given local bin indices with const access
0097   /// @param indices The local bin indices
0098   /// @return Const reference to the value at the specified bin
0099   /// @throws std::invalid_argument if indices size doesn't match grid dimensions
0100   /// @throws std::out_of_range if indices are out of bounds
0101   const T& atLocalBins(const AnyIndexType& indices) const {
0102     std::any any = m_grid->atLocalBinsAny(indices);
0103     return *std::any_cast<T const*>(any);
0104   }
0105 
0106   /// @brief Get the number of dimensions of the grid
0107   /// @return The number of dimensions
0108   std::size_t dimensions() const { return m_grid->dimensions(); }
0109 
0110   /// @brief Get the center position of a bin for given indices
0111   /// @param indices The local bin indices
0112   /// @return The center position of the bin
0113   AnyPointType binCenter(const IGrid::AnyIndexType& indices) const {
0114     return m_grid->binCenterAny(indices);
0115   }
0116 
0117   /// @brief Get the lower left edge position of a bin for given indices
0118   /// @param indices The local bin indices
0119   /// @return The lower left edge position of the bin
0120   AnyPointType lowerLeftBinEdge(const IGrid::AnyIndexType& indices) const {
0121     return m_grid->lowerLeftBinEdgeAny(indices);
0122   }
0123 
0124   /// @brief Get the upper right edge position of a bin for given indices
0125   /// @param indices The local bin indices
0126   /// @return The upper right edge position of the bin
0127   AnyPointType upperRightBinEdge(const IGrid::AnyIndexType& indices) const {
0128     return m_grid->upperRightBinEdgeAny(indices);
0129   }
0130 
0131   /// @brief Get the number of bins along each axis
0132   /// @return Vector containing the number of bins for each axis
0133   AnyIndexType numLocalBins() const { return m_grid->numLocalBinsAny(); }
0134 
0135  private:
0136   /// @brief Check if the grid's value type matches the template parameter T
0137   /// @throws std::invalid_argument if there's a type mismatch
0138   void checkType() {
0139     if (m_grid->valueType() != typeid(T)) {
0140       throw std::invalid_argument("Type mismatch between grid and view type");
0141     }
0142   }
0143 
0144   /// Pointer to the underlying grid
0145   GridPointerType m_grid;
0146 };
0147 
0148 }  // namespace detail
0149 
0150 /// @brief Type-safe view into a grid with mutable access
0151 ///
0152 /// @tparam T Type of values stored in the grid
0153 ///
0154 /// This class provides a type-safe interface to access grid objects through the
0155 /// type-erased IGrid interface with mutable access. It ensures that the grid
0156 /// being viewed contains values of the expected type T.
0157 ///
0158 /// Example usage:
0159 /// ```
0160 /// Grid<double, Axis> grid(...);
0161 /// AnyGridView<double> view(grid);
0162 /// view.atLocalBins({1}) = 42.0;  // Modify the grid through the view
0163 /// ```
0164 template <typename T>
0165 class AnyGridView : public detail::AnyGridViewBase<T, false> {
0166  public:
0167   using detail::AnyGridViewBase<T, false>::AnyGridViewBase;
0168 };
0169 
0170 /// @brief Deduction guide for AnyGridView from Grid
0171 /// @tparam T Type of values stored in the grid
0172 /// @tparam Axes Parameter pack of axis types defining the grid
0173 template <typename T, typename... Axes>
0174 AnyGridView(Grid<T, Axes...>& grid) -> AnyGridView<T>;
0175 
0176 /// @brief Type-safe view into a grid with const access
0177 ///
0178 /// @tparam T Type of values stored in the grid
0179 ///
0180 /// This class provides a type-safe interface to access grid objects through the
0181 /// type-erased IGrid interface with const access only. It ensures that the grid
0182 /// being viewed contains values of the expected type T.
0183 ///
0184 /// Example usage:
0185 /// ```
0186 /// const Grid<double, Axis> grid(...);
0187 /// AnyGridConstView<double> view(grid);
0188 /// double value = view.atLocalBins({1});  // Read-only access
0189 /// ```
0190 template <typename T>
0191 class AnyGridConstView : public detail::AnyGridViewBase<T, true> {
0192  public:
0193   using detail::AnyGridViewBase<T, true>::AnyGridViewBase;
0194 };
0195 
0196 /// @brief Deduction guide for AnyGridConstView from const Grid
0197 /// @tparam T Type of values stored in the grid
0198 /// @tparam Axes Parameter pack of axis types defining the grid
0199 template <typename T, typename... Axes>
0200 AnyGridConstView(const Grid<T, Axes...>& grid) -> AnyGridConstView<T>;
0201 
0202 }  // namespace Acts