OGS
IndexCalculator.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <spdlog/spdlog.h>
7
8#include <array>
9#include <cstddef>
10#include <limits>
11#include <numeric>
12
13#include "BaseLib/Logging.h"
14
15namespace FileIO
16{
17namespace Gocad
18{
23class IndexCalculator final
24{
25public:
32 IndexCalculator(std::size_t x_dim, std::size_t y_dim, std::size_t z_dim)
33 : _x_dim(x_dim),
34 _y_dim(y_dim),
35 _z_dim(z_dim),
36 _n_nodes(x_dim * y_dim * z_dim),
37 _n_cells((_x_dim - 1) * (_y_dim - 1) * (_z_dim - 1))
38 {
39 }
40
41 IndexCalculator() = default;
42
43 std::size_t operator()(std::array<std::size_t, 3> const& c) const
44 {
45 const std::size_t idx(c[2] * _x_dim * _y_dim + c[1] * _x_dim + c[0]);
46 if (idx >= _n_nodes)
47 {
48 return std::numeric_limits<std::size_t>::max();
49 }
50 return idx;
51 }
52
53 std::size_t getCellIdx(std::size_t u, std::size_t v, std::size_t w) const
54 {
55 // ensure (u,v,w) is a valid cell
56 if (u >= _x_dim - 1 || v >= _y_dim - 1 || w >= _z_dim - 1)
57 {
58 ERR("GocadSGridReader::IndexCalculator::getCellIdx(): At least "
59 "one grid coordinate to big.");
60 ERR("\t Given: ({:d}, {:d}, {:d}), max allowed cell grid coords: "
61 "({:d}, {:d}, {:d}).",
62 u, v, w, _x_dim - 1, _y_dim - 1, _z_dim - 1);
63 return std::numeric_limits<std::size_t>::max();
64 }
65
66 return (_x_dim - 1) * (_y_dim - 1) + v * (_x_dim - 1) + u;
67 }
68
69 std::array<std::size_t, 3> getCoordsForID(std::size_t id) const
70 {
71 std::array<std::size_t, 3> const coords{
72 (id % (_x_dim * _y_dim)) % _x_dim,
73 (id % (_x_dim * _y_dim)) / _x_dim, id / (_x_dim * _y_dim)};
74 return coords;
75 }
76
77 std::size_t _x_dim{0};
78 std::size_t _y_dim{0};
79 std::size_t _z_dim{0};
80 std::size_t _n_nodes{0};
81 std::size_t _n_cells{0};
82};
83
84} // end namespace Gocad
85} // end namespace FileIO
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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)