ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Grid.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Grid.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 
13 #include <array>
14 #include <numeric>
15 #include <set>
16 #include <tuple>
17 #include <type_traits>
18 #include <vector>
19 #include "Acts/Utilities/IAxis.hpp"
22 
23 namespace Acts {
24 
25 namespace detail {
26 
37 template <typename T, class... Axes>
38 class Grid final {
39  public:
41  static constexpr size_t DIM = sizeof...(Axes);
42 
44  using value_type = T;
48  using const_reference = const value_type&;
50  using point_t = std::array<double, DIM>;
52  using index_t = std::array<size_t, DIM>;
53 
57  Grid(std::tuple<Axes...> axes) : m_axes(std::move(axes)) {
58  m_values.resize(size());
59  }
60 
74  //
75  template <class Point>
76  reference atPosition(const Point& point) {
77  return m_values.at(globalBinFromPosition(point));
78  }
79 
94  template <class Point>
95  const_reference atPosition(const Point& point) const {
96  return m_values.at(globalBinFromPosition(point));
97  }
98 
104  reference at(size_t bin) { return m_values.at(bin); }
105 
111  const_reference at(size_t bin) const { return m_values.at(bin); }
112 
121  reference atLocalBins(const index_t& localBins) {
122  return m_values.at(globalBinFromLocalBins(localBins));
123  }
124 
133  const_reference atLocalBins(const index_t& localBins) const {
134  return m_values.at(globalBinFromLocalBins(localBins));
135  }
136 
148  template <class Point>
150  const Point& position) const {
152  }
153 
157  static constexpr size_t dimensions() { return DIM; }
158 
166  std::array<double, DIM> binCenter(const index_t& localBins) const {
167  return grid_helper::getBinCenter(localBins, m_axes);
168  }
169 
181  template <class Point>
182  size_t globalBinFromPosition(const Point& point) const {
184  }
185 
193  size_t globalBinFromLocalBins(const index_t& localBins) const {
194  return grid_helper::getGlobalBin(localBins, m_axes);
195  }
196 
209  template <class Point>
210  size_t globalBinFromFromLowerLeftEdge(const Point& point) const {
212  }
213 
226  template <class Point>
227  index_t localBinsFromPosition(const Point& point) const {
229  }
230 
241  }
242 
256  template <class Point>
258  Point shiftedPoint;
260  for (size_t i = 0; i < DIM; i++) {
261  shiftedPoint[i] = point[i] + width[i] / 2;
262  }
263  return grid_helper::getLocalBinIndices(shiftedPoint, m_axes);
264  }
265 
273  point_t lowerLeftBinEdge(const index_t& localBins) const {
274  return grid_helper::getLowerLeftBinEdge(localBins, m_axes);
275  }
276 
284  point_t upperRightBinEdge(const index_t& localBins) const {
285  return grid_helper::getUpperRightBinEdge(localBins, m_axes);
286  }
287 
294 
299 
304 
311  for (size_t index : grid_helper::exteriorBinIndices(m_axes)) {
312  at(index) = value;
313  }
314  }
315 
341  template <
342  class Point, typename U = T,
344  Point, std::array<double, DIM>, std::array<double, DIM>, U>::value>>
345  T interpolate(const Point& point) const {
346  // there are 2^DIM corner points used during the interpolation
347  constexpr size_t nCorners = 1 << DIM;
348 
349  // construct vector of pairs of adjacent bin centers and values
350  std::array<value_type, nCorners> neighbors;
351 
352  // get local indices for current bin
353  // value of bin is interpreted as being the field value at its lower left
354  // corner
355  const auto& llIndices = localBinsFromPosition(point);
356 
357  // get global indices for all surrounding corner points
358  const auto& closestIndices = rawClosestPointsIndices(llIndices);
359 
360  // get values on grid points
361  size_t i = 0;
362  for (size_t index : closestIndices) {
363  neighbors.at(i++) = at(index);
364  }
365 
366  return Acts::interpolate(point, lowerLeftBinEdge(llIndices),
367  upperRightBinEdge(llIndices), neighbors);
368  }
369 
381  template <class Point>
382  bool isInside(const Point& position) const {
383  return grid_helper::isInside(position, m_axes);
384  }
385 
402  const index_t& localBins, size_t size = 1u) const {
403  return grid_helper::neighborHoodIndices(localBins, size, m_axes);
404  }
405 
411  size_t size() const {
412  index_t nBinsArray = numLocalBins();
413  // add under-and overflow bins for each axis and multiply all bins
414  return std::accumulate(
415  nBinsArray.begin(), nBinsArray.end(), 1,
416  [](const size_t& a, const size_t& b) { return a * (b + 2); });
417  }
418 
419  std::array<const IAxis*, DIM> axes() const {
421  }
422 
423  private:
425  std::tuple<Axes...> m_axes;
427  std::vector<T> m_values;
428 
429  // Part of closestPointsIndices that goes after local bins resolution.
430  // Used as an interpolation performance optimization, but not exposed as it
431  // doesn't make that much sense from an API design standpoint.
433  const index_t& localBins) const {
434  return grid_helper::closestPointsIndices(localBins, m_axes);
435  }
436 };
437 } // namespace detail
438 
439 } // namespace Acts