VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
GbSpatialData3D.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef GBPOINTCLOUD_H
35#define GBPOINTCLOUD_H
36
37#include "DataTypes.h"
38#include "UbMath.h"
39#include "logger/Logger.h"
41
42#include <GbObject3D.h>
43#include <UbTuple.h>
44
45#include <PointerDefinitions.h>
46#include <cmath>
48#include <cstdint>
49#include <functional>
50#include <memory>
51#include <stdexcept>
52
59template <typename T>
61{
62public:
63 virtual T interpolateToPoint(real3 point) const = 0;
64 virtual ~GbSpatialData3D() {};
65};
66
71template <typename T>
73{
74public:
76 GbStructuredMesh3D(const real3& spacing, const real3& origin, const std::array<uint, 3>& nPoints,
77 const std::vector<T>& data, InterpolationStrategy interpolationStrategy)
78 : spacing(spacing), origin(origin), nPoints(nPoints), data(data), interpolationStrategy(interpolationStrategy)
79 {
80 if (nPoints[0] * nPoints[1] * nPoints[2] != data.size())
81 throw std::runtime_error("number of values does not match number of points.");
82 }
84 {
85 return applyTrilinearInterpolation(point);
86 };
87 ~GbStructuredMesh3D() override {};
88
89private:
90 T applyTrilinearInterpolation(const real3& point) const
91 {
92 if (isOutOfBounds(point))
93 throw std::runtime_error("Point is out of bounds!");
94
95 const auto [ix, iy, iz] = getIndexMMM(point);
96 const real3 distances = (point - getCoordinates(ix, iy, iz)) / spacing;
97
98 const bool endOfX = ix == nPoints[0] - 1;
99 const bool endOfY = iy == nPoints[1] - 1;
100 const bool endOfZ = iz == nPoints[2] - 1;
101
102 if(endOfX && endOfY && endOfZ)
103 return data.back();
104 if (endOfX && endOfY)
105 return ub_math::lerp(data[getIndex(ix, iy, iz)], data[getIndex(ix, iy, iz + 1)], distances.z);
106
107 if (endOfX && endOfZ)
108 return ub_math::lerp(data[getIndex(ix, iy, iz)], data[getIndex(ix, iy + 1, iz)], distances.y);
109
110 if (endOfY && endOfZ)
111 return ub_math::lerp(data[getIndex(ix, iy, iz)], data[getIndex(ix + 1, iy, iz)], distances.x);
112
113 if (endOfX)
114 return ub_math::lerp2(data[getIndex(ix, iy, iz)], data[getIndex(ix, iy + 1, iz)], data[getIndex(ix, iy, iz + 1)],
115 data[getIndex(ix, iy + 1, iz + 1)], distances.y, distances.z);
116 if (endOfY)
117 return ub_math::lerp2(data[getIndex(ix, iy, iz)], data[getIndex(ix + 1, iy, iz)], data[getIndex(ix, iy, iz + 1)],
118 data[getIndex(ix + 1, iy, iz + 1)], distances.x, distances.z);
119 if (endOfZ)
120 return ub_math::lerp2(data[getIndex(ix, iy, iz)], data[getIndex(ix + 1, iy + 1, iz)],
121 data[getIndex(ix, iy + 1, iz)], data[getIndex(ix + 1, iy + 1, iz)], distances.x,
122 distances.y);
123
124 return ub_math::lerp3(data[getIndex(ix, iy, iz)], data[getIndex(ix + 1, iy, iz)], data[getIndex(ix, iy + 1, iz)],
125 data[getIndex(ix + 1, iy + 1, iz)], data[getIndex(ix, iy, iz + 1)],
126 data[getIndex(ix + 1, iy, iz + 1)], data[getIndex(ix, iy + 1, iz + 1)],
127 data[getIndex(ix + 1, iy + 1, iz + 1)], distances.x, distances.y, distances.z);
128 }
129 bool isBelowLowerX(real x) const
130 {
131 if (spacing.x == vf::basics::constant::c0o1 || nPoints[0] == 1)
132 return false;
133 return x < origin.x;
134 }
135 bool isBelowLowerY(real y) const
136 {
137 if (spacing.y == vf::basics::constant::c0o1 || nPoints[1] == 1)
138 return false;
139 return y < origin.y;
140 }
141 bool isBelowLowerZ(real z) const
142 {
143 if (spacing.z == vf::basics::constant::c0o1 || nPoints[2] == 1)
144 return false;
145 return z < origin.z;
146 }
147
148 bool isAboveUpperX(real x) const
149 {
150 if (spacing.x == vf::basics::constant::c0o1 || nPoints[0] == 1)
151 return false;
152 return x > origin.x + spacing.x * nPoints[0];
153 }
154 bool isAboveUpperY(real y) const
155 {
156 if (spacing.y == vf::basics::constant::c0o1 || nPoints[1] == 1)
157 return false;
158 return y > origin.y + spacing.y * nPoints[1];
159 }
160 bool isAboveUpperZ(real z) const
161 {
162 if (spacing.z == vf::basics::constant::c0o1 || nPoints[2] == 1)
163 return false;
164 return z > origin.z + spacing.z * nPoints[2];
165 }
166
167 bool isOutOfBounds(const real3& point) const
168 {
169 return isBelowLowerX(point.x) || isBelowLowerY(point.y) || isBelowLowerZ(point.z) || isAboveUpperX(point.x) ||
170 isAboveUpperY(point.y) || isAboveUpperZ(point.z);
171 }
172 uint getNextLowerPoint(const real coord, const real origin, const real spacing, const uint numberOfPoints) const
173 {
174 if (spacing == vf::basics::constant::c0o1 || numberOfPoints == 1)
175 return 0;
176 if (coord < origin)
177 return UINT32_MAX;
178 return uint((coord - origin) / spacing);
179 }
180 uint getNextHigherPoint(const real coord, const real origin, const real spacing, const uint numberOfPoints) const
181 {
182 if (spacing == vf::basics::constant::c0o1 || numberOfPoints == 1)
183 return 0;
184 const uint index = (coord - origin) / spacing + 1;
185 if (index > numberOfPoints)
186 return UINT32_MAX;
187 return index;
188 }
189 uint getNextLowerPointX(const real3& point) const
190 {
191 return getNextLowerPoint(point.x, origin.x, spacing.x, nPoints[0]);
192 }
193 uint getNextLowerPointY(const real3& point) const
194 {
195 return getNextLowerPoint(point.y, origin.y, spacing.y, nPoints[1]);
196 }
197 uint getNextLowerPointZ(const real3& point) const
198 {
199 return getNextLowerPoint(point.z, origin.z, spacing.z, nPoints[2]);
200 }
201 uint getNextHigherPointX(const real3& point) const
202 {
203 return getNextHigherPoint(point.x, origin.x, spacing.x, nPoints[0]);
204 }
205 uint getNextHigherPointY(const real3& point) const
206 {
207 return getNextHigherPoint(point.y, origin.y, spacing.y, nPoints[1]);
208 }
209 uint getNextHigherPointZ(const real3& point) const
210 {
211 return getNextHigherPoint(point.z, origin.z, spacing.z, nPoints[2]);
212 }
213 uint getIndex(const uint indexX, const uint indexY, const uint indexZ) const
214 {
215 return indexX + nPoints[0] * (indexY + nPoints[1] * indexZ);
216 }
217 std::array<uint, 3> getIndexMMM(const real3& point) const
218 {
219 return { getNextLowerPointX(point), getNextLowerPointY(point), getNextLowerPointZ(point) };
220 }
221
222 real getCoordinateX(const uint index) const
223 {
224 return origin.x + index * spacing.x;
225 }
226 real getCoordinateY(const uint index) const
227 {
228 return origin.y + index * spacing.y;
229 }
230 real getCoordinateZ(const uint index) const
231 {
232 return origin.z + index * spacing.z;
233 }
234 real3 getCoordinates(const uint indexX, const uint indexY, const uint indexZ) const
235 {
236 return { getCoordinateX(indexX), getCoordinateY(indexY), getCoordinateZ(indexZ) };
237 }
238 const real3 spacing, origin;
239 const std::array<uint, 3> nPoints;
240 std::vector<T> data;
241 const InterpolationStrategy interpolationStrategy;
242};
243
244struct Node
245{
247 std::unique_ptr<Node> left, right;
248 Node(uint index, std::unique_ptr<Node> left, std::unique_ptr<Node> right)
249 : index(index), left(std::move(left)), right(std::move(right))
250 {
251 }
252};
253
254std::unique_ptr<Node> insertNodes(const std::vector<real3>& coordinates);
255std::vector<uint> findNearestPoints(const Node* root, real3 point, uint nPoints, const std::vector<real3>& coordinates);
256void applyToTree(Node* root, std::function<void(uint index, uint depth)> func);
257
260template <typename T>
262{
263public:
266 class NearestNeighbor;
267 GbPointCloud3D(std::vector<real3> points, std::vector<T> data,
268 std::unique_ptr<InterpolationStrategy> interpolationStrategy, bool printTree = false)
269 : coordinates(std::move(points)), values(std::move(data)), interpolationStrategy(std::move(interpolationStrategy))
270 {
271 if (points.size() != data.size())
272 throw std::runtime_error("Size of points and data does not match!");
273 fillTree();
274 this->interpolationStrategy->setData(this);
275 if (printTree)
276 this->printTree();
277 };
278 ~GbPointCloud3D() override {};
279
281 {
282 return this->interpolationStrategy->interpolateToPoint(point);
283 };
285 {
286 return this->coordinates[index];
287 }
288 T getValue(uint index) const
289 {
290 return this->values[index];
291 }
292
293 std::vector<uint> findNearestPoints(real3 point, uint nPoints) const
294 {
295 return ::findNearestPoints(root.get(), point, nPoints, coordinates);
296 };
298 {
299 return findNearestPoints(point, 1)[0];
300 };
302 {
303 const real3 dist = coordinates[index] - point;
304 return std::hypot(dist.x, dist.y, dist.z);
305 };
307 {
308 applyToTree(root.get(), [&](uint index, uint depth) {
309 VF_LOG_INFO("index {} coords {},{},{} depth {}", index, coordinates[index].x, coordinates[index].y,
310 coordinates[index].z, depth);
311 });
312 }
313
314private:
315 void fillTree()
316 {
317 this->root = insertNodes(coordinates);
318 }
319 std::unique_ptr<Node> root;
320 const std::vector<real3> coordinates;
321 const std::vector<T> values;
322 void nearestNeighborHelper(const Node* node, const real3& point, uint depth, uint& best, real& bestDist) const;
323 const std::unique_ptr<InterpolationStrategy> interpolationStrategy;
324};
325
326template <typename T>
328{
329public:
330 virtual T interpolateToPoint(real3 point) const = 0;
332 {
333 this->data = data;
334 };
337};
338
339template <typename T>
340class GbPointCloud3D<T>::InverseDistanceWeighing : public GbPointCloud3D<T>::InterpolationStrategy
341{
342 using InterpolationStrategy = typename GbPointCloud3D<T>::InterpolationStrategy;
343
344public:
345 static std::unique_ptr<InterpolationStrategy> make(uint nPoints = 10)
346 {
347 return std::unique_ptr<InterpolationStrategy>(new InverseDistanceWeighing(nPoints));
348 }
349 InverseDistanceWeighing(uint nPoints = 10) : nPoints(nPoints) {};
351 {
352 const auto nearestNeighbors = this->data->findNearestPoints(point, nPoints);
353 real sumWeights {};
354 T sumValues {};
355 for (auto index : nearestNeighbors) {
356 const real distance = this->data->distanceToPoint(index, point);
357 if(distance < vf::basics::constant::cSmallSingle)
358 return this->data->getValue(index);
359 const real weight = std::pow(distance, -2);
360 sumValues += weight * this->data->getValue(index);
361 sumWeights += weight;
362 }
363 return sumValues / sumWeights;
364 };
365
366private:
367 const uint nPoints;
368};
369
370template <typename T>
371class GbPointCloud3D<T>::NearestNeighbor : public GbPointCloud3D<T>::InterpolationStrategy
372{
373 using InterpolationStrategy = typename GbPointCloud3D<T>::InterpolationStrategy;
374
375public:
376 static std::unique_ptr<InterpolationStrategy> make()
377 {
378 return std::unique_ptr<InterpolationStrategy>(new NearestNeighbor());
379 }
380 NearestNeighbor() = default;
382 {
383 return this->data->getValue(this->data->findNearestPoint(point));
384 };
385};
386
387#endif
This class provides an object to interpolate spatially distributed data to new points.
This class provides an object to interpolate data on a regular structured mesh to new points.
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
unsigned int uint
Definition DataTypes.h:47
T interpolateToPoint(real3 point) const override
uint findNearestPoint(real3 point) const
virtual ~GbSpatialData3D()
~GbPointCloud3D() override
GbPointCloud3D(std::vector< real3 > points, std::vector< T > data, std::unique_ptr< InterpolationStrategy > interpolationStrategy, bool printTree=false)
T interpolateToPoint(real3 point) const override
virtual T interpolateToPoint(real3 point) const =0
static std::unique_ptr< InterpolationStrategy > make(uint nPoints=10)
T interpolateToPoint(real3 point) const override
GbStructuredMesh3D(const real3 &spacing, const real3 &origin, const std::array< uint, 3 > &nPoints, const std::vector< T > &data, InterpolationStrategy interpolationStrategy)
std::unique_ptr< Node > right
T getValue(uint index) const
void setData(GbPointCloud3D< T > *data)
std::vector< uint > findNearestPoints(real3 point, uint nPoints) const
Node(uint index, std::unique_ptr< Node > left, std::unique_ptr< Node > right)
static std::unique_ptr< InterpolationStrategy > make()
real distanceToPoint(uint index, real3 point) const
void applyToTree(Node *root, std::function< void(uint index, uint depth)> func)
std::unique_ptr< Node > insertNodes(const std::vector< real3 > &coordinates)
real3 getCoordinate(uint index) const
std::vector< uint > findNearestPoints(const Node *root, real3 point, uint nPoints, const std::vector< real3 > &coordinates)
~GbStructuredMesh3D() override
std::unique_ptr< Node > left
virtual T interpolateToPoint(real3 point) const =0
T interpolateToPoint(real3 point) const override
@ z
Definition Axis.h:44
@ x
Definition Axis.h:42
@ y
Definition Axis.h:43
constexpr T lerp2(const T &a, const T &b, const T &c, const T &d, const U &t, const U &u)
Definition UbMath.h:418
constexpr T lerp(const T &a, const T &b, const U &t)
Definition UbMath.h:412
constexpr T lerp3(const T &a, const T &b, const T &c, const T &d, const T &e, const T &f, const T &g, const T &h, const U &t, const U &u, const U &v)
Definition UbMath.h:424
real x
Definition DataTypes.h:52
real y
Definition DataTypes.h:52
real z
Definition DataTypes.h:52