File indexing completed on 2026-11-30 09:45:27
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Material/ISurfaceMaterial.hpp"
0013 #include "Acts/Material/MaterialSlab.hpp"
0014 #include "Acts/Utilities/AnyGridView.hpp"
0015 #include "Acts/Utilities/Delegate.hpp"
0016 #include "Acts/Utilities/Grid.hpp"
0017 #include "Acts/Utilities/GridAccessHelpers.hpp"
0018
0019 #include <ostream>
0020 #include <stdexcept>
0021 #include <vector>
0022
0023 namespace Acts {
0024
0025
0026
0027
0028
0029
0030
0031 struct IGridMaterialAccessor {
0032 virtual ~IGridMaterialAccessor() = default;
0033 };
0034
0035
0036
0037 struct GridMaterialAccessor : public IGridMaterialAccessor {
0038
0039 using grid_value_type = MaterialSlab;
0040
0041
0042
0043
0044
0045
0046 template <typename grid_type>
0047 inline const MaterialSlab& slab(
0048 grid_type& grid, const typename grid_type::point_t& point) const {
0049 return grid.atPosition(point);
0050 }
0051
0052
0053
0054
0055
0056
0057
0058 template <typename grid_type>
0059 void scale(grid_type& grid, double scale) {
0060
0061 for (std::size_t ib = 0; ib < grid.size(); ++ib) {
0062 grid.at(ib).scaleThickness(static_cast<float>(scale));
0063 }
0064 }
0065 };
0066
0067
0068
0069 struct IndexedMaterialAccessor : public IGridMaterialAccessor {
0070
0071 using grid_value_type = std::size_t;
0072
0073
0074
0075 explicit IndexedMaterialAccessor(std::vector<MaterialSlab>&& mmaterial)
0076 : IGridMaterialAccessor(), material(std::move(mmaterial)) {}
0077
0078
0079 std::vector<MaterialSlab> material;
0080
0081
0082
0083
0084
0085
0086 template <typename grid_type>
0087 inline const MaterialSlab& slab(
0088 const grid_type& grid, const typename grid_type::point_t& point) const
0089 requires(std::is_same_v<typename grid_type::value_type, grid_value_type>)
0090 {
0091 std::size_t index = grid.atPosition(point);
0092 return material[index];
0093 }
0094
0095
0096
0097
0098 template <typename grid_type>
0099 void scale(grid_type& , double scale) {
0100 for (auto& m : material) {
0101 m.scaleThickness(static_cast<float>(scale));
0102 }
0103 }
0104 };
0105
0106
0107
0108 struct GloballyIndexedMaterialAccessor : public IGridMaterialAccessor {
0109
0110
0111
0112 explicit GloballyIndexedMaterialAccessor(
0113 std::shared_ptr<std::vector<MaterialSlab>> gMaterial, bool shared = false)
0114 : IGridMaterialAccessor(),
0115 globalMaterial(std::move(gMaterial)),
0116 sharedEntries(shared) {}
0117
0118
0119 using grid_value_type = std::size_t;
0120
0121
0122 std::shared_ptr<std::vector<MaterialSlab>> globalMaterial = nullptr;
0123
0124
0125
0126
0127
0128 bool sharedEntries = false;
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 template <typename grid_type>
0139 inline const MaterialSlab& slab(
0140 const grid_type& grid, const typename grid_type::point_t& point) const {
0141 auto index = grid.atPosition(point);
0142 return (*globalMaterial)[index];
0143 }
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 template <typename grid_type>
0155 void scale(grid_type& grid, double scale) {
0156 if (sharedEntries) {
0157 throw std::invalid_argument(
0158 "GloballyIndexedMaterialAccessor: shared entry scaling is not "
0159 "supported.");
0160 }
0161
0162 for (std::size_t ib = 0; ib < grid.size(); ++ib) {
0163 auto index = grid.at(ib);
0164 (*globalMaterial)[index].scaleThickness(static_cast<float>(scale));
0165 }
0166 }
0167 };
0168
0169
0170
0171 class IGridSurfaceMaterialBase : public ISurfaceMaterial {};
0172
0173
0174
0175 template <typename grid_value_t>
0176 class IGridSurfaceMaterial : public IGridSurfaceMaterialBase {
0177 public:
0178
0179
0180 virtual const IGrid& grid() const = 0;
0181
0182
0183
0184 virtual const IGridMaterialAccessor& materialAccessor() const = 0;
0185
0186
0187
0188 virtual const GridAccess::IBoundToGridLocal& boundToGridLocal() const = 0;
0189
0190
0191
0192 virtual const GridAccess::IGlobalToGridLocal& globalToGridLocal() const = 0;
0193
0194
0195
0196 virtual AnyGridView<grid_value_t> gridView() = 0;
0197
0198
0199
0200 virtual AnyGridConstView<grid_value_t> gridConstView() const = 0;
0201 };
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 template <typename grid_t, typename material_accessor_t>
0214 class GridSurfaceMaterialT
0215 : public IGridSurfaceMaterial<
0216 typename material_accessor_t::grid_value_type> {
0217 public:
0218
0219
0220 using BoundToGridLocalDelegate =
0221 OwningDelegate<typename grid_t::point_t(const Vector2&),
0222 GridAccess::IBoundToGridLocal>;
0223
0224
0225
0226 using GlobalToGridLocalDelegate =
0227 OwningDelegate<typename grid_t::point_t(const Vector3&),
0228 GridAccess::IGlobalToGridLocal>;
0229
0230
0231 using grid_type = grid_t;
0232
0233
0234 using material_accessor_type = material_accessor_t;
0235
0236
0237
0238
0239
0240
0241
0242 GridSurfaceMaterialT(grid_type&& grid,
0243 material_accessor_type&& materialAccessor,
0244 BoundToGridLocalDelegate boundToGridLocal,
0245 GlobalToGridLocalDelegate globalToGridLocal)
0246 : m_grid(std::move(grid)),
0247 m_materialAccessor(std::move(materialAccessor)),
0248 m_globalToGridLocal(std::move(globalToGridLocal)),
0249 m_boundToGridLocal(std::move(boundToGridLocal)) {
0250 if (!m_globalToGridLocal.connected()) {
0251 throw std::invalid_argument(
0252 "GridSurfaceMaterialT: GlobalToGridLocalDelegate is not connected.");
0253 }
0254 if (!m_boundToGridLocal.connected()) {
0255 throw std::invalid_argument(
0256 "GridSurfaceMaterialT: BoundToGridLocalDelegate is not connected.");
0257 }
0258 }
0259
0260
0261 const MaterialSlab& materialSlab(const Vector2& lp) const final {
0262 return m_materialAccessor.slab(m_grid, m_boundToGridLocal(lp));
0263 }
0264
0265
0266 std::vector<AxisDirection> localAxisDirections() const final { return {}; }
0267
0268
0269
0270
0271 [[deprecated(
0272 "Use materialSlab(const Vector2& lp) with a prior "
0273 "Surface::globalToLocal() call instead")]] const MaterialSlab&
0274 materialSlab(const Vector3& gp) const final {
0275 return m_materialAccessor.slab(m_grid, m_globalToGridLocal(gp));
0276 }
0277
0278 using ISurfaceMaterial::materialSlab;
0279
0280
0281
0282
0283
0284 ISurfaceMaterial& scale(double factor) final {
0285 m_materialAccessor.scale(m_grid, factor);
0286 return (*this);
0287 }
0288
0289
0290
0291
0292 std::ostream& toStream(std::ostream& sl) const final {
0293 sl << "GridSurfaceMaterial - material access via accessor.";
0294 return sl;
0295 }
0296
0297
0298
0299 const grid_type& grid() const final { return m_grid; }
0300
0301
0302
0303 AnyGridView<typename material_accessor_t::grid_value_type> gridView() final {
0304 return AnyGridView<typename material_accessor_t::grid_value_type>(m_grid);
0305 }
0306
0307
0308
0309 AnyGridConstView<typename material_accessor_t::grid_value_type>
0310 gridConstView() const final {
0311 return AnyGridConstView<typename material_accessor_t::grid_value_type>(
0312 m_grid);
0313 }
0314
0315
0316
0317 const material_accessor_type& materialAccessor() const final {
0318 return m_materialAccessor;
0319 }
0320
0321
0322
0323 const GridAccess::IBoundToGridLocal& boundToGridLocal() const final {
0324 return *(m_boundToGridLocal.instance());
0325 }
0326
0327
0328
0329 const BoundToGridLocalDelegate& boundToGridLocalDelegate() const {
0330 return m_boundToGridLocal;
0331 }
0332
0333
0334
0335 const GridAccess::IGlobalToGridLocal& globalToGridLocal() const final {
0336 return *(m_globalToGridLocal.instance());
0337 }
0338
0339
0340
0341 const GlobalToGridLocalDelegate& globalToGridLocalDelegate() const {
0342 return m_globalToGridLocal;
0343 }
0344
0345 private:
0346
0347 grid_type m_grid;
0348
0349
0350 material_accessor_type m_materialAccessor;
0351
0352
0353 GlobalToGridLocalDelegate m_globalToGridLocal;
0354
0355
0356 BoundToGridLocalDelegate m_boundToGridLocal;
0357 };
0358
0359
0360
0361
0362 template <typename grid_type>
0363 using IndexedSurfaceMaterial =
0364 GridSurfaceMaterialT<grid_type, IndexedMaterialAccessor>;
0365
0366
0367
0368 template <typename grid_type>
0369 using GloballyIndexedSurfaceMaterial =
0370 GridSurfaceMaterialT<grid_type, GloballyIndexedMaterialAccessor>;
0371
0372
0373
0374 template <typename grid_type>
0375 using GridSurfaceMaterial =
0376 GridSurfaceMaterialT<grid_type, GridMaterialAccessor>;
0377
0378
0379
0380 }