VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
KdTree.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 KDTREE_H
35#define KDTREE_H
36
40
45
46#include <cmath>
47#include <string>
48
49namespace kd_tree
50{
51template <typename T>
52class Tree
53{
54public:
55 /* ======================================================================================= */
56 Tree(GbTriFaceMesh3D &mesh, const SplitAlgorithm<T> &splitAlg) : rootNode(NULL) { this->buildTree(mesh, splitAlg); }
57 /* ======================================================================================= */
59 {
60 if (rootNode) {
61 delete rootNode;
62 rootNode = NULL;
63 }
64 }
65 /* ======================================================================================= */
66 // the IntersectionHandler specifies how to handle the intersection
69 {
70 return rootNode->intersectLine(n1, n2, iHandler);
71 }
72 /* ======================================================================================= */
73 // the IntersectionHandler specifies how to handle the intersection
75 {
76 std::set<ub_keys::Key3<int>> mailbox;
77 return rootNode->intersectRay(ray, iHandler, mailbox);
78 }
79 /* ======================================================================================= */
81 {
82 if (rootNode)
83 return rootNode->getNumOfNodes();
84 return 0;
85 }
86 /* ======================================================================================= */
88 {
89 if (rootNode)
90 return rootNode->getNumOfTriFaces();
91 return 0;
92 }
93 /* ======================================================================================= */
94 std::string toString()
95 {
96 return ""; // Tree:: num of nodes: " + rootNode.getNumOfNodes() + ", primitives:" +
97 // rootNode.getNumOfPrimitives() + ", root_primitives:" + getNumOfPrimitives() + ", max_level:" +
98 // max_level;
99 }
100 /* ======================================================================================= */
102 {
103 if (rootNode)
104 delete rootNode;
105
106 // create a copy of triangles
108 new std::vector<GbTriFaceMesh3D::TriFace>(*mesh.getTriangles()));
109
110 const int maxLevel =
111 static_cast<int>(std::lround(8.0 + 1.3 * std::log((double)triFaces->size()))); // TODO: remove magic numbers
112
113 rootNode =
114 new Node<T>(T(mesh.getX1Minimum()), T(mesh.getX2Minimum()), T(mesh.getX3Minimum()), T(mesh.getX1Maximum()),
115 T(mesh.getX2Maximum()), T(mesh.getX3Maximum()), triFaces, mesh.getNodes());
116
117 rootNode->buildTree(0, maxLevel, splitAlg);
118 }
119 void writeTree(const std::string &filename, WbWriter *writer = WbWriterVtkXmlBinary::getInstance())
120 {
121 if (rootNode) {
122 std::vector<UbTupleFloat3> nodes;
123 std::vector<UbTupleInt8> cubes;
124 std::vector<std::string> datanames;
125 std::vector<std::vector<double>> cubesdata;
126 rootNode->addCubeInfo(nodes, cubes, datanames, cubesdata);
127 writer->writeOctsWithCellData(filename, nodes, cubes, datanames, cubesdata);
128 }
129 }
130
131private:
132 Node<T> *rootNode;
133};
134} // namespace kd_tree
135
136#endif // KDTREE_H
137
double getX1Maximum() override
double getX1Minimum() override
double getX2Maximum() override
double getX3Minimum() override
double getX2Minimum() override
double getX3Maximum() override
static WbWriterVtkXmlBinary * getInstance()
bool intersectLine(const UbTuple< T, T, T > &n1, const UbTuple< T, T, T > &n2, const LineIntersectionHandler< T > &iHandler)
Definition KdTree.h:67
int getNumOfTriFaces()
Definition KdTree.h:87
std::string toString()
Definition KdTree.h:94
int intersectRay(const Ray< T > &ray, const RayIntersectionHandler< T > &iHandler)
Definition KdTree.h:74
Tree(GbTriFaceMesh3D &mesh, const SplitAlgorithm< T > &splitAlg)
Definition KdTree.h:56
int getNumOfNodes()
Definition KdTree.h:80
void buildTree(GbTriFaceMesh3D &mesh, const SplitAlgorithm< T > &splitAlg)
Definition KdTree.h:101
void writeTree(const std::string &filename, WbWriter *writer=WbWriterVtkXmlBinary::getInstance())
Definition KdTree.h:119
std::shared_ptr< T > SPtr
std::vector< Vertex > * getNodes()
std::vector< TriFace > * getTriangles()