FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
UniformGrid3.h
1#pragma once
4
5#include <algorithm>
6#include <cmath>
7#include <cstddef>
8#include <stdexcept>
9#include <vector>
10
11namespace marenv::grid
12{
25template<class T>
27{
28public:
30 UniformGrid3() = default;
31
40 UniformGrid3(const double lower[3], const double spacing[3], const int numCells[3], const T& initial = T {})
41 {
42 std::size_t size = 1;
43 for (int a = 0; a < 3; ++a) {
44 if (!(spacing[a] > 0.0))
45 throw std::invalid_argument("UniformGrid3: the spacing must be positive");
46 if (numCells[a] < 1)
47 throw std::invalid_argument("UniformGrid3: the number of cells must be at least 1");
48 m_lower[a] = lower[a];
49 m_spacing[a] = spacing[a];
50 m_numCells[a] = numCells[a];
51 const std::size_t nodes = static_cast<std::size_t>(numCells[a]) + 1;
52 if (size > std::vector<T>().max_size() / nodes)
53 throw std::invalid_argument("UniformGrid3: too many nodes");
54 size *= nodes;
55 }
56 m_values.assign(size, initial);
57 }
58
60 int NumCells(int axis) const { return m_numCells[axis]; }
61
63 double Spacing(int axis) const { return m_spacing[axis]; }
64
66 double NodeCoordinate(int axis, int index) const { return m_lower[axis] + index * m_spacing[axis]; }
67
69 T& At(int i, int j, int k) { return m_values[Index(i, j, k)]; }
70
72 const T& At(int i, int j, int k) const { return m_values[Index(i, j, k)]; }
73
81 bool Contains(const double pos[3]) const
82 {
83 double scaled[3];
84 return Scale(pos, scaled);
85 }
86
93 bool Interpolate(const double pos[3], T& out) const
94 {
95 double scaled[3];
96 if (!Scale(pos, scaled)) {
97 out = T {};
98 return false;
99 }
100 int cell[3];
101 double fraction[3];
102 for (int a = 0; a < 3; ++a) {
103 const double clamped = std::clamp(scaled[a], 0.0, static_cast<double>(m_numCells[a]));
104 cell[a] = std::min(static_cast<int>(std::floor(clamped)), m_numCells[a] - 1);
105 fraction[a] = clamped - cell[a];
106 }
107 T sum {};
108 for (int corner = 0; corner < 8; ++corner) {
109 double weight = 1.0;
110 int node[3];
111 for (int a = 0; a < 3; ++a) {
112 const bool upper = (corner >> a) & 1;
113 node[a] = cell[a] + (upper ? 1 : 0);
114 weight *= upper ? fraction[a] : 1.0 - fraction[a];
115 }
116 sum = sum + weight * At(node[0], node[1], node[2]);
117 }
118 out = sum;
119 return true;
120 }
121
122private:
123 static constexpr double boundarySlack = 1e-9;
124
126 bool Scale(const double pos[3], double scaled[3]) const
127 {
128 if (m_values.empty())
129 return false;
130 for (int a = 0; a < 3; ++a) {
131 scaled[a] = (pos[a] - m_lower[a]) / m_spacing[a];
132 if (!(scaled[a] >= -boundarySlack && scaled[a] <= m_numCells[a] + boundarySlack))
133 return false;
134 }
135 return true;
136 }
137
138 std::size_t Index(int i, int j, int k) const
139 {
140 const std::size_t numX = static_cast<std::size_t>(m_numCells[0] + 1);
141 const std::size_t numY = static_cast<std::size_t>(m_numCells[1] + 1);
142 return static_cast<std::size_t>(i) + numX * (static_cast<std::size_t>(j) + numY * static_cast<std::size_t>(k));
143 }
144
145 double m_lower[3] = {0.0, 0.0, 0.0};
146 double m_spacing[3] = {1.0, 1.0, 1.0};
147 int m_numCells[3] = {0, 0, 0};
148 std::vector<T> m_values;
149};
150} // namespace marenv::grid
Definition UniformGrid3.h:27
double Spacing(int axis) const
Node spacing along axis.
Definition UniformGrid3.h:63
double NodeCoordinate(int axis, int index) const
Coordinate of node index along axis.
Definition UniformGrid3.h:66
T & At(int i, int j, int k)
Value at node (i, j, k); no bounds check.
Definition UniformGrid3.h:69
UniformGrid3()=default
An empty grid; Contains() is false everywhere.
bool Contains(const double pos[3]) const
Definition UniformGrid3.h:81
int NumCells(int axis) const
Number of cells along axis.
Definition UniformGrid3.h:60
const T & At(int i, int j, int k) const
Value at node (i, j, k); no bounds check.
Definition UniformGrid3.h:72
UniformGrid3(const double lower[3], const double spacing[3], const int numCells[3], const T &initial=T {})
Definition UniformGrid3.h:40
bool Interpolate(const double pos[3], T &out) const
Definition UniformGrid3.h:93