VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
KdSAHSplit.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 KDSAHSPLIT_H
35#define KDSAHSPLIT_H
36
41
46
47#include <cmath>
48#include <vector>
49
50namespace kd_tree
51{
52template <typename T>
53class SAHSplit : public SplitAlgorithm<T>
54{
55public:
56 /* ======================================================================================= */
57 SplitCandidate<T> findBestSplitCandidate(const int &level, const int &maxLevel, Node<T> &node) const override
58 {
59 if (!node.getTriFaces())
60 throw UbException(UB_EXARGS, "triFace NULL pointer");
61
62 if (node.getTriFaces()->size() <= 1 // max triangles in node
63 || level >= maxLevel) {
64 return SplitCandidate<T>();
65 }
66
68 T minCN = ub_math::inf;
69
70 for (int splitAxis = 0; splitAxis < 3; splitAxis++) {
72 findPossibleSplitCandidates(splitAxis, node, sc);
73
74 // incremental sweep to find best split position
75 for (std::size_t i = 0; i < sc.size(); i++) {
76 if (i == 0) {
77 sc[i].nl = sc.objects_starting_outside_left + sc.objects_fully_outside_node;
78 sc[i].nr = int(node.getTriFaces()->size()) - sc[0].np - sc[0].ending;
79 } else {
80 sc[i].nl = sc[i - 1].nl + sc[i - 1].starting + sc[i - 1].np;
81 sc[i].nr = sc[i - 1].nr - sc[i].ending - sc[i].np;
82 }
83
84 this->calcSAH(sc[i], node);
85
86 if (sc[i].Cn < minCN) {
87 minCN = sc[i].Cn;
89 }
90 }
91 }
92
93 // automatic termination criterion (SAH)
94 if (bestSplitCandidate.isValid && bestSplitCandidate.Cn >= node.getTriFaces()->size() * Ci) {
95 return SplitCandidate<T>();
96 }
97
98 return bestSplitCandidate;
99 }
100 /* ======================================================================================= */
102 std::vector<GbTriFaceMesh3D::TriFace> &triFacesForChild1,
103 std::vector<GbTriFaceMesh3D::TriFace> &triFacesForChild2, Node<T> &node) const override
104 {
105 if (!node.getTriFaces())
106 throw UbException(UB_EXARGS, "null pointer at triface list");
107
108 std::vector<GbTriFaceMesh3D::TriFace> &srcTriFaces = *node.getTriFaces();
109 std::vector<GbTriFaceMesh3D::Vertex> &srcNodes = node.getNodes();
110 std::vector<T> projection;
111
112 for (std::size_t i = 0; i < srcTriFaces.size(); i++) {
115
116 T &min = projection[0];
117 T &max = projection[2];
118 // --------------------------------------------------- //
119 // case 1 : object inside plane
120 if (ub_math::equal(min, max)) {
121 if (ub_math::equal(min, candidate.position)) {
122 if (candidate.np_left) {
123 triFacesForChild1.push_back(triFace);
124 } else if (candidate.np_right) {
125 triFacesForChild2.push_back(triFace);
126 }
127 } else if (ub_math::less(min, candidate.position)) {
128 triFacesForChild1.push_back(triFace);
129 } else // if( UbMath::greater(min, candidate.position)
130 {
131 triFacesForChild2.push_back(triFace);
132 }
133 } //
134 // --------------------------------------------------- //
135 // case 2 : object on left side of plane
136 else if (ub_math::lessEqual(max, candidate.position)) {
137 triFacesForChild1.push_back(triFace);
138 } // --------------------------------------------------- //
139 // case 3 : object on right side of plane
140 else if (ub_math::greaterEqual(min, candidate.position)) {
141 triFacesForChild2.push_back(triFace);
142 } //
143 // --------------------------------------------------- //
144 // case 4 : object in both nodes
145 else {
146 triFacesForChild1.push_back(triFace);
147 triFacesForChild2.push_back(triFace);
148 } //
149 // --------------------------------------------------- //
150 }
151
152 node.deleteTriFaces();
153 }
154
155private:
156 /* ======================================================================================= */
157 // cost function
158 inline T calcCosts(const int &nl, const int &nr, const T &SA_VL, const T &SA_VR, const T &SA_V) const
159 {
160 return Ct + Ci * (nl * SA_VL / SA_V + nr * SA_VR / SA_V);
161 }
162 /* ======================================================================================= */
163 void findPossibleSplitCandidates(const int &splitAxis, Node<T> &node,
165 {
166 T p1_node = (splitAxis == Axis::X ? node.x[0] : splitAxis == Axis::Y ? node.y[0] : node.z[0]);
167 T p2_node = (splitAxis == Axis::X ? node.x[1] : splitAxis == Axis::Y ? node.y[1] : node.z[1]);
168
169 if (!node.getTriFaces())
170 throw UbException(UB_EXARGS, "null pointer");
171
172 std::vector<GbTriFaceMesh3D::TriFace> &srcTriFaces = *node.getTriFaces();
173 std::vector<GbTriFaceMesh3D::Vertex> &srcNodes = node.getNodes();
174 std::vector<T> projection;
175
176 for (std::size_t i = 0; i < srcTriFaces.size(); i++) {
178
179 // project object to axis
181 // left point
182 T &p1 = projection[0];
183 // right point
184 T &p2 = projection[2];
185
186 // --------------------------------------------------- //
187 // --------------------------------------------------- //
188 // case 1 : object is fully inside the current node
190 if (ub_math::equal(p1, p2)) {
191 // object is inside the plane
192 splitCandidateManager.add(p1, splitAxis, 0, 0, 1);
193 } else {
194 splitCandidateManager.add(p1, splitAxis, 1, 0, 0);
195 splitCandidateManager.add(p2, splitAxis, 0, 1, 0);
196 }
197 } //
198 // --------------------------------------------------- //
199 // --------------------------------------------------- //
200 // case 2 : just the right point (p2) is inside the current node
202 splitCandidateManager.add(p2, splitAxis, 0, 1, 0);
203 splitCandidateManager.objects_starting_outside_left++;
204 } //
205 // --------------------------------------------------- //
206 // --------------------------------------------------- //
207 // case 3 : just the left point (p1) is inside the current node
210 splitCandidateManager.add(p1, splitAxis, 1, 0, 0);
211 } //
212 // --------------------------------------------------- //
213 // --------------------------------------------------- //
214 // case 4 : left and right point are outside the current node
215 else if (ub_math::less(p1, p1_node) && ub_math::greater(p2, p2_node)) {
216 splitCandidateManager.objects_fully_outside_node++;
217 } //
218 // --------------------------------------------------- //
219 // --------------------------------------------------- //
220 }
221
222 splitCandidateManager.createSortedArray();
223 }
224
225 /* ======================================================================================= */
226 // calculates the costs for a given splitCandidate based on the Surface Area Heuristic (SAH)
227 void calcSAH(SplitCandidate<T> &candidate, Node<T> &node) const
228 {
229 T p1_node = (candidate.axis == Axis::X ? node.x[0] : candidate.axis == Axis::Y ? node.y[0] : node.z[0]);
230
231 // edges of (root) voxel
232 T dx = std::fabs(node.x[1] - node.x[0]);
233 T dy = std::fabs(node.y[1] - node.y[0]);
234 T dz = std::fabs(node.z[1] - node.z[0]);
235
236 // surface area (root) voxel
237 T SA_V = T((2.0 * dx * dy) + (2.0 * dx * dz) + (2.0 * dy * dz));
238
239 T delta = (candidate.axis == Axis::X ? dx : candidate.axis == Axis::Y ? dy : dz);
240 T deltaL = std::fabs(candidate.position - p1_node);
241 T deltaR = std::fabs(delta - deltaL);
242
243 // edges of sub voxel left
244 T dx_l = (candidate.axis == Axis::X ? deltaL : dx), dy_l = (candidate.axis == Axis::Y ? deltaL : dy),
245 dz_l = (candidate.axis == Axis::Z ? deltaL : dz);
246
247 // surface area sub voxel left
248 T SA_VL = T((2.0 * dx_l * dy_l) + (2.0 * dx_l * dz_l) + (2.0 * dy_l * dz_l));
249
250 // edges of sub voxel right
251 T dx_r = (candidate.axis == Axis::X ? deltaR : dx), dy_r = (candidate.axis == Axis::Y ? deltaR : dy),
252 dz_r = (candidate.axis == Axis::Z ? deltaR : dz);
253
254 // surface area sub voxel right
255 T SA_VR = T((2.0 * dx_r * dy_r) + (2.0 * dx_r * dz_r) + (2.0 * dy_r * dz_r));
256
257 if (candidate.np == 0) {
258 candidate.Cn = calcCosts(candidate.nl, candidate.nr, SA_VL, SA_VR, SA_V);
259 return;
260 }
261
262 // once putting np with nl, and once with nr - and select the one with lowest cost
263 // see: Wald, Havran: "On building fast kd-Trees for Ray Tracing, and doing that in O(N log N)", 2006
264 T CP_L = calcCosts(candidate.nl + candidate.np, candidate.nr, SA_VL, SA_VR, SA_V);
265 T CP_R = calcCosts(candidate.nl, candidate.nr + candidate.np, SA_VL, SA_VR, SA_V);
266
267 if (CP_L < CP_R) {
268 candidate.Cn = CP_L;
269 candidate.np_right = true;
270 } else {
271 candidate.Cn = CP_R;
272 candidate.np_left = true;
273 }
274 }
275 /* ======================================================================================= */
276
277protected:
278 static const T Ct; // = 3.0; traversal cost
279 static const T Ci; // = 4.0; ray-patch-intersection-cost
280};
281
282template <typename T>
283const T SAHSplit<T>::Ct = 3.0; // traversal cost
284template <typename T>
285const T SAHSplit<T>::Ci = 4.0; // ray-patch-intersection-cost
286} // namespace kd_tree
287
288#endif // KDSAHSPLIT_H
289
void deleteTriFaces()
Definition KdNode.h:105
const MbSmartPtr< std::vector< GbTriFaceMesh3D::TriFace > > & getTriFaces()
Definition KdNode.h:107
std::vector< GbTriFaceMesh3D::Vertex > & getNodes()
Definition KdNode.h:109
void distributeTriFaces(const SplitCandidate< T > &candidate, std::vector< GbTriFaceMesh3D::TriFace > &triFacesForChild1, std::vector< GbTriFaceMesh3D::TriFace > &triFacesForChild2, Node< T > &node) const override
Definition KdSAHSplit.h:101
SplitCandidate< T > findBestSplitCandidate(const int &level, const int &maxLevel, Node< T > &node) const override
Definition KdSAHSplit.h:57
static const T Ct
Definition KdSAHSplit.h:278
static const T Ci
Definition KdSAHSplit.h:279
std::shared_ptr< T > SPtr
Axis
Definition Axis.h:41
const real dx
const real dz
const real dy
#define UB_EXARGS
Definition UbException.h:73
Definition Axis.h:48
void project2Axis(GbTriFaceMesh3D::TriFace &triFace, std::vector< GbTriFaceMesh3D::Vertex > &nodes, const int &axis, std::vector< T > &projection)
Definition KdUtilities.h:62
bool greaterEqual(const T1 &value, const T2 &reference)
Definition UbMath.h:223
bool lessEqual(const T1 &value, const T2 &reference)
Definition UbMath.h:209
bool equal(const T1 &value, const T2 &reference)
Definition UbMath.h:189
bool greater(const T1 &value, const T2 &reference)
Definition UbMath.h:216
bool less(const T1 &value, const T2 &reference)
Definition UbMath.h:202
static const int Y
Definition KdUtilities.h:50
static const int X
Definition KdUtilities.h:49