Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/AABBTree/AABBTreeBuilder.h
9906 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/TriangleSplitter/TriangleSplitter.h>
8
#include <Jolt/Geometry/AABox.h>
9
#include <Jolt/Core/NonCopyable.h>
10
11
JPH_NAMESPACE_BEGIN
12
13
struct AABBTreeBuilderStats
14
{
15
///@name Splitter stats
16
TriangleSplitter::Stats mSplitterStats; ///< Stats returned by the triangle splitter algorithm
17
18
///@name Tree structure
19
float mSAHCost = 0.0f; ///< Surface Area Heuristic cost of this tree
20
int mMinDepth = 0; ///< Minimal depth of tree (number of nodes)
21
int mMaxDepth = 0; ///< Maximum depth of tree (number of nodes)
22
int mNodeCount = 0; ///< Number of nodes in the tree
23
int mLeafNodeCount = 0; ///< Number of leaf nodes (that contain triangles)
24
25
///@name Configured stats
26
int mMaxTrianglesPerLeaf = 0; ///< Configured max triangles per leaf
27
28
///@name Actual stats
29
int mTreeMinTrianglesPerLeaf = 0; ///< Minimal amount of triangles in a leaf
30
int mTreeMaxTrianglesPerLeaf = 0; ///< Maximal amount of triangles in a leaf
31
float mTreeAvgTrianglesPerLeaf = 0.0f; ///< Average amount of triangles in leaf nodes
32
};
33
34
/// Helper class to build an AABB tree
35
class JPH_EXPORT AABBTreeBuilder
36
{
37
public:
38
/// A node in the tree, contains the AABox for the tree and any child nodes or triangles
39
class Node
40
{
41
public:
42
JPH_OVERRIDE_NEW_DELETE
43
44
/// Indicates that there is no child
45
static constexpr uint cInvalidNodeIndex = ~uint(0);
46
47
/// Get number of triangles in this node
48
inline uint GetTriangleCount() const { return mNumTriangles; }
49
50
/// Check if this node has any children
51
inline bool HasChildren() const { return mChild[0] != cInvalidNodeIndex || mChild[1] != cInvalidNodeIndex; }
52
53
/// Min depth of tree
54
uint GetMinDepth(const Array<Node> &inNodes) const;
55
56
/// Max depth of tree
57
uint GetMaxDepth(const Array<Node> &inNodes) const;
58
59
/// Number of nodes in tree
60
uint GetNodeCount(const Array<Node> &inNodes) const;
61
62
/// Number of leaf nodes in tree
63
uint GetLeafNodeCount(const Array<Node> &inNodes) const;
64
65
/// Get triangle count in tree
66
uint GetTriangleCountInTree(const Array<Node> &inNodes) const;
67
68
/// Calculate min and max triangles per node
69
void GetTriangleCountPerNode(const Array<Node> &inNodes, float &outAverage, uint &outMin, uint &outMax) const;
70
71
/// Calculate the total cost of the tree using the surface area heuristic
72
float CalculateSAHCost(const Array<Node> &inNodes, float inCostTraversal, float inCostLeaf) const;
73
74
/// Recursively get children (breadth first) to get in total inN children (or less if there are no more)
75
void GetNChildren(const Array<Node> &inNodes, uint inN, Array<const Node *> &outChildren) const;
76
77
/// Bounding box
78
AABox mBounds;
79
80
/// Triangles (if no child nodes)
81
uint mTrianglesBegin; // Index into mTriangles
82
uint mNumTriangles = 0;
83
84
/// Child node indices (if no triangles)
85
uint mChild[2] = { cInvalidNodeIndex, cInvalidNodeIndex };
86
87
private:
88
friend class AABBTreeBuilder;
89
90
/// Recursive helper function to calculate cost of the tree
91
float CalculateSAHCostInternal(const Array<Node> &inNodes, float inCostTraversalDivSurfaceArea, float inCostLeafDivSurfaceArea) const;
92
93
/// Recursive helper function to calculate min and max triangles per node
94
void GetTriangleCountPerNodeInternal(const Array<Node> &inNodes, float &outAverage, uint &outAverageDivisor, uint &outMin, uint &outMax) const;
95
};
96
97
/// Constructor
98
AABBTreeBuilder(TriangleSplitter &inSplitter, uint inMaxTrianglesPerLeaf = 16);
99
100
/// Recursively build tree, returns the root node of the tree
101
Node * Build(AABBTreeBuilderStats &outStats);
102
103
/// Get all nodes
104
const Array<Node> & GetNodes() const { return mNodes; }
105
106
/// Get all triangles
107
const Array<IndexedTriangle> &GetTriangles() const { return mTriangles; }
108
109
private:
110
uint BuildInternal(const TriangleSplitter::Range &inTriangles);
111
112
TriangleSplitter & mTriangleSplitter;
113
const uint mMaxTrianglesPerLeaf;
114
Array<Node> mNodes;
115
Array<IndexedTriangle> mTriangles;
116
};
117
118
JPH_NAMESPACE_END
119
120