VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
GbSpatialData3D.cpp
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#include "GbSpatialData3D.h"
35#include <algorithm>
36#include <functional>
37#include <iterator>
38#include <memory>
39#include <numeric>
40#include <queue>
41#include <vector>
42
43std::unique_ptr<Node> insertNodes(const std::vector<real3>& coordinates)
44{
45 using iter = std::vector<uint>::iterator;
46 std::function<std::unique_ptr<Node>(iter, iter, const std::vector<real3>&, uint)> recurse =
47 [&](iter startIndices, iter endIndices, const std::vector<real3>& coordinates, uint level) {
49 return std::unique_ptr<Node>();
50
51 auto midPointer = std::next(startIndices, std::distance(startIndices, endIndices) / 2);
52
53 std::nth_element(startIndices, midPointer, endIndices, [&](const auto& a, const auto& b) {
54 if (level % 3 == 0)
55 return coordinates[a].x < coordinates[b].x;
56 if (level % 3 == 1)
57 return coordinates[a].y < coordinates[b].y;
58 return coordinates[a].z < coordinates[b].z;
59 });
60
61 return std::make_unique<Node>(*midPointer, recurse(startIndices, midPointer, coordinates, level + 1),
62 recurse(std::next(midPointer), endIndices, coordinates, level + 1));
63 };
64 std::vector<uint> indices(coordinates.size());
65 std::iota(indices.begin(), indices.end(), 0);
66 return recurse(indices.begin(), indices.end(), coordinates, 0);
67}
68
69std::vector<uint> findNearestPoints(const Node* root, real3 point, uint nPoints, const std::vector<real3>& coordinates)
70{
71 std::vector<uint> result;
72 nPoints = std::min<uint>(nPoints, (uint)coordinates.size());
73 using DistPoint = std::pair<real, uint>;
74 struct Compare
75 {
76 bool operator()(const DistPoint& a, const DistPoint& b) const
77 {
78 return a.first < b.first; // max-heap by distance
79 }
80 };
81
82 std::priority_queue<DistPoint, std::vector<DistPoint>, Compare> pq;
83
84 std::function<void(const Node*, uint)> recurse = [&](const Node* node, uint depth) {
85 if (node == nullptr)
86 return;
87 const real3 coordinate = coordinates[node->index];
88 const real dist = std::sqrt(square(coordinate - point));
89 if (pq.size() < nPoints) {
90 pq.emplace(dist, node->index);
91 } else if (dist < pq.top().first) {
92 pq.pop();
93 pq.emplace(dist, node->index);
94 }
95
96 const uint axis = depth % 3;
97
98 const real diff = (axis == 0 ? point.x - coordinate.x : axis == 1 ? point.y - coordinate.y : point.z - coordinate.z);
99
100 const Node* first = (diff <= 0) ? node->left.get() : node->right.get();
101 const Node* second = (diff <= 0) ? node->right.get() : node->left.get();
102
103 recurse(first, depth + 1);
104
105 const real worstDist = pq.size() < nPoints ? std::numeric_limits<real>::infinity() : pq.top().first;
106 if (std::abs(diff) < worstDist) {
107 recurse(second, depth + 1);
108 }
109 };
110
111 recurse(root, 0);
112
113 while (!pq.empty()) {
114 result.push_back(pq.top().second);
115 pq.pop();
116 }
117 std::reverse(result.begin(), result.end());
118 return result;
119}
120
121void applyToTree(Node* root, std::function<void(uint, uint)> func)
122{
123 std::function<void(Node*, uint)> recurse = [&](Node* node, uint depth) {
124 if (node == nullptr)
125 return;
126 func(node->index, depth);
127 recurse(node->left.get(), depth + 1);
128 recurse(node->right.get(), depth + 1);
129 };
130 recurse(root, 0);
131}
132
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
unsigned int uint
Definition DataTypes.h:47
constexpr real square(const real3 &a)
Definition DataTypes.h:102
void applyToTree(Node *root, std::function< void(uint, uint)> func)
std::unique_ptr< Node > right
std::unique_ptr< Node > insertNodes(const std::vector< real3 > &coordinates)
std::vector< uint > findNearestPoints(const Node *root, real3 point, uint nPoints, const std::vector< real3 > &coordinates)
std::unique_ptr< Node > left
@ x
Definition Axis.h:42
@ y
Definition Axis.h:43
Definition Axis.h:48