OGS
IndexCalculator.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <spdlog/spdlog.h>
13
14#include <array>
15#include <cstddef>
16#include <limits>
17#include <numeric>
18
19#include "BaseLib/Logging.h"
20
21namespace FileIO
22{
24{
29class IndexCalculator final
30{
31public:
38 IndexCalculator(std::size_t x_dim, std::size_t y_dim, std::size_t z_dim)
39 : _x_dim(x_dim),
40 _y_dim(y_dim),
41 _z_dim(z_dim),
42 _n_nodes(x_dim * y_dim * z_dim),
43 _n_cells((_x_dim - 1) * (_y_dim - 1) * (_z_dim - 1))
44 {
45 }
46
47 IndexCalculator() = default;
48
49 std::size_t operator()(std::array<std::size_t, 3> const& c) const
50 {
51 const std::size_t idx(c[2] * _x_dim * _y_dim + c[1] * _x_dim + c[0]);
52 if (idx >= _n_nodes)
53 {
54 return std::numeric_limits<std::size_t>::max();
55 }
56 return idx;
57 }
58
59 std::size_t getCellIdx(std::size_t u, std::size_t v, std::size_t w) const
60 {
61 // ensure (u,v,w) is a valid cell
62 if (u >= _x_dim - 1 || v >= _y_dim - 1 || w >= _z_dim - 1)
63 {
65 "one grid coordinate to big.");
66 ERR("\t Given: ({:d}, {:d}, {:d}), max allowed cell grid coords: "
67 "({:d}, {:d}, {:d}).",
68 u, v, w, _x_dim - 1, _y_dim - 1, _z_dim - 1);
69 return std::numeric_limits<std::size_t>::max();
70 }
71
72 return (_x_dim - 1) * (_y_dim - 1) + v * (_x_dim - 1) + u;
73 }
74
75 std::array<std::size_t, 3> getCoordsForID(std::size_t id) const
76 {
77 std::array<std::size_t, 3> const coords{
78 (id % (_x_dim * _y_dim)) % _x_dim,
79 (id % (_x_dim * _y_dim)) / _x_dim, id / (_x_dim * _y_dim)};
80 return coords;
81 }
82
83 std::size_t _x_dim{0};
84 std::size_t _y_dim{0};
85 std::size_t _z_dim{0};
86 std::size_t _n_nodes{0};
87 std::size_t _n_cells{0};
88};
89
90} // end namespace Gocad
91} // end namespace FileIO
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:44
std::size_t getCellIdx(std::size_t u, std::size_t v, std::size_t w) const
std::size_t operator()(std::array< std::size_t, 3 > const &c) const
std::array< std::size_t, 3 > getCoordsForID(std::size_t id) const
IndexCalculator(std::size_t x_dim, std::size_t y_dim, std::size_t z_dim)
constexpr std::array c
static const double u
static const double v