|
|
|||
File indexing completed on 2025-12-11 09:40:11
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 /// @brief Type-safe view into a grid with mutable access 0019 /// 0020 /// @tparam T Type of values stored in the grid 0021 /// 0022 /// This class provides a type-safe interface to access grid objects through the 0023 /// type-erased IGrid interface with mutable access. It ensures that the grid 0024 /// being viewed contains values of the expected type T. 0025 /// 0026 /// Example usage: 0027 /// ``` 0028 /// Grid<double, Axis> grid(...); 0029 /// AnyGridView<double> view(grid); 0030 /// view.atLocalBins({1}) = 42.0; // Modify the grid through the view 0031 /// ``` 0032 template <typename T> 0033 class AnyGridView { 0034 public: 0035 /// Type of pointer to grid, const or non-const depending on isConst 0036 using GridPointerType = IGrid*; 0037 /// Type for indices, imported from IGrid 0038 using AnyIndexType = IGrid::AnyIndexType; 0039 /// Type for points, imported from IGrid 0040 using AnyPointType = IGrid::AnyPointType; 0041 0042 /// @brief Constructor from non-const IGrid reference 0043 /// @param grid The grid to view 0044 explicit AnyGridView(IGrid& grid) : m_grid(&grid) { checkType(); } 0045 0046 /// @brief Constructor from non-const concrete Grid reference 0047 /// @tparam Axes Parameter pack of axis types defining the grid 0048 /// @param grid The concrete grid to view 0049 /// @note This constructor creates a mutable view of the grid 0050 template <typename... Axes> 0051 explicit AnyGridView(Grid<T, Axes...>& grid) : m_grid(&grid) { 0052 checkType(); 0053 } 0054 0055 /// Copy constructor 0056 /// @param other The AnyGridView to copy from 0057 AnyGridView(const AnyGridView& other) = default; 0058 /// Copy assignment operator 0059 /// @param other The AnyGridView to copy from 0060 /// @return Reference to this AnyGridView after copying 0061 AnyGridView& operator=(const AnyGridView& other) = default; 0062 0063 /// Move constructor 0064 AnyGridView(AnyGridView&&) noexcept = default; 0065 /// Move assignment operator 0066 /// @return Reference to this AnyGridView after moving 0067 AnyGridView& operator=(AnyGridView&&) noexcept = default; 0068 0069 /// @brief Access value at given local bin indices with mutable access 0070 /// @param indices The local bin indices 0071 /// @return Reference to the value at the specified bin 0072 /// @note This method provides mutable access to grid values 0073 /// @throws std::invalid_argument if indices size doesn't match grid dimensions 0074 /// @throws std::out_of_range if indices are out of bounds 0075 T& atLocalBins(const AnyIndexType& indices) { 0076 std::any any = m_grid->atLocalBinsAny(indices); 0077 return *std::any_cast<T*>(any); 0078 } 0079 0080 /// @brief Access value at given local bin indices with const access 0081 /// @param indices The local bin indices 0082 /// @return Const reference to the value at the specified bin 0083 /// @throws std::invalid_argument if indices size doesn't match grid dimensions 0084 /// @throws std::out_of_range if indices are out of bounds 0085 const T& atLocalBins(const AnyIndexType& indices) const { 0086 std::any any = m_grid->atLocalBinsAny(indices); 0087 return *std::any_cast<T const*>(any); 0088 } 0089 0090 /// @brief Get the number of dimensions of the grid 0091 /// @return The number of dimensions 0092 std::size_t dimensions() const { return m_grid->dimensions(); } 0093 0094 /// @brief Get the center position of a bin for given indices 0095 /// @param indices The local bin indices 0096 /// @return The center position of the bin 0097 AnyPointType binCenter(const IGrid::AnyIndexType& indices) const { 0098 return m_grid->binCenterAny(indices); 0099 } 0100 0101 /// @brief Get the lower left edge position of a bin for given indices 0102 /// @param indices The local bin indices 0103 /// @return The lower left edge position of the bin 0104 AnyPointType lowerLeftBinEdge(const IGrid::AnyIndexType& indices) const { 0105 return m_grid->lowerLeftBinEdgeAny(indices); 0106 } 0107 0108 /// @brief Get the upper right edge position of a bin for given indices 0109 /// @param indices The local bin indices 0110 /// @return The upper right edge position of the bin 0111 AnyPointType upperRightBinEdge(const IGrid::AnyIndexType& indices) const { 0112 return m_grid->upperRightBinEdgeAny(indices); 0113 } 0114 0115 /// @brief Get the number of bins along each axis 0116 /// @return Vector containing the number of bins for each axis 0117 AnyIndexType numLocalBins() const { return m_grid->numLocalBinsAny(); } 0118 0119 /// @brief Check if the grid's value type matches the template parameter T 0120 /// @throws std::invalid_argument if there's a type mismatch 0121 void checkType() { 0122 if (m_grid->valueType() != typeid(T)) { 0123 throw std::invalid_argument("Type mismatch between grid and view type"); 0124 } 0125 } 0126 0127 /// Type-erased pointer to the underlying grid 0128 GridPointerType m_grid; 0129 }; 0130 0131 /// @brief Deduction guide for AnyGridView from Grid 0132 /// @tparam T Type of values stored in the grid 0133 /// @tparam Axes Parameter pack of axis types defining the grid 0134 /// @param grid The grid to create a view for 0135 template <typename T, typename... Axes> 0136 AnyGridView(Grid<T, Axes...>& grid) -> AnyGridView<T>; 0137 0138 /// @brief Type-safe view into a grid with const access 0139 /// 0140 /// @tparam T Type of values stored in the grid 0141 /// 0142 /// This class provides a type-safe interface to access grid objects through the 0143 /// type-erased IGrid interface with const access only. It ensures that the grid 0144 /// being viewed contains values of the expected type T. 0145 /// 0146 /// Example usage: 0147 /// ``` 0148 /// const Grid<double, Axis> grid(...); 0149 /// AnyGridConstView<double> view(grid); 0150 /// double value = view.atLocalBins({1}); // Read-only access 0151 /// ``` 0152 template <typename T> 0153 class AnyGridConstView { 0154 public: 0155 /// Type of pointer to grid, const or non-const depending on isConst 0156 using GridPointerType = const IGrid*; 0157 /// Type for indices, imported from IGrid 0158 using AnyIndexType = IGrid::AnyIndexType; 0159 /// Type for points, imported from IGrid 0160 using AnyPointType = IGrid::AnyPointType; 0161 0162 /// @brief Constructor from const IGrid reference 0163 /// @param grid The grid to view 0164 /// @note This constructor creates a const view of the grid 0165 explicit AnyGridConstView(const IGrid& grid) : m_grid(&grid) { checkType(); } 0166 0167 /// @brief Constructor from const concrete Grid reference 0168 /// @tparam Axes Parameter pack of axis types defining the grid 0169 /// @param grid The concrete grid to view 0170 /// @note This constructor creates a const view of the grid 0171 template <typename... Axes> 0172 explicit AnyGridConstView(const Grid<T, Axes...>& grid) : m_grid(&grid) { 0173 checkType(); 0174 } 0175 0176 /// @brief Access value at given local bin indices with const access 0177 /// @param indices The local bin indices 0178 /// @return Const reference to the value at the specified bin 0179 /// @throws std::invalid_argument if indices size doesn't match grid dimensions 0180 /// @throws std::out_of_range if indices are out of bounds 0181 const T& atLocalBins(const AnyIndexType& indices) const { 0182 std::any any = m_grid->atLocalBinsAny(indices); 0183 return *std::any_cast<T const*>(any); 0184 } 0185 0186 /// @brief Get the number of dimensions of the grid 0187 /// @return The number of dimensions 0188 std::size_t dimensions() const { return m_grid->dimensions(); } 0189 0190 /// @brief Get the center position of a bin for given indices 0191 /// @param indices The local bin indices 0192 /// @return The center position of the bin 0193 AnyPointType binCenter(const IGrid::AnyIndexType& indices) const { 0194 return m_grid->binCenterAny(indices); 0195 } 0196 0197 /// @brief Get the lower left edge position of a bin for given indices 0198 /// @param indices The local bin indices 0199 /// @return The lower left edge position of the bin 0200 AnyPointType lowerLeftBinEdge(const IGrid::AnyIndexType& indices) const { 0201 return m_grid->lowerLeftBinEdgeAny(indices); 0202 } 0203 0204 /// @brief Get the upper right edge position of a bin for given indices 0205 /// @param indices The local bin indices 0206 /// @return The upper right edge position of the bin 0207 AnyPointType upperRightBinEdge(const IGrid::AnyIndexType& indices) const { 0208 return m_grid->upperRightBinEdgeAny(indices); 0209 } 0210 0211 /// @brief Get the number of bins along each axis 0212 /// @return Vector containing the number of bins for each axis 0213 AnyIndexType numLocalBins() const { return m_grid->numLocalBinsAny(); } 0214 0215 /// @brief Check if the grid's value type matches the template parameter T 0216 /// @throws std::invalid_argument if there's a type mismatch 0217 void checkType() { 0218 if (m_grid->valueType() != typeid(T)) { 0219 throw std::invalid_argument("Type mismatch between grid and view type"); 0220 } 0221 } 0222 0223 /// Type-erased pointer to the underlying const grid 0224 GridPointerType m_grid; 0225 }; 0226 0227 /// @brief Deduction guide for AnyGridConstView from const Grid 0228 /// @tparam T Type of values stored in the grid 0229 /// @tparam Axes Parameter pack of axis types defining the grid 0230 /// @param grid The const grid to create a view for 0231 template <typename T, typename... Axes> 0232 AnyGridConstView(const Grid<T, Axes...>& grid) -> AnyGridConstView<T>; 0233 0234 } // namespace Acts
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|