| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 16 |
TriangleSplitter::Stats mSplitterStats; |
| 17 |
|
| 18 |
|
| 19 |
float mSAHCost = 0.0f; |
| 20 |
int mMinDepth = 0; |
| 21 |
int mMaxDepth = 0; |
| 22 |
int mNodeCount = 0; |
| 23 |
int mLeafNodeCount = 0; |
| 24 |
|
| 25 |
|
| 26 |
int mMaxTrianglesPerLeaf = 0; |
| 27 |
|
| 28 |
|
| 29 |
int mTreeMinTrianglesPerLeaf = 0; |
| 30 |
int mTreeMaxTrianglesPerLeaf = 0; |
| 31 |
float mTreeAvgTrianglesPerLeaf = 0.0f; |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
class JPH_EXPORT AABBTreeBuilder |
| 36 |
{ |
| 37 |
public: |
| 38 |
|
| 39 |
class Node |
| 40 |
{ |
| 41 |
public: |
| 42 |
JPH_OVERRIDE_NEW_DELETE |
| 43 |
|
| 44 |
|
| 45 |
static constexpr uint cInvalidNodeIndex = ~uint(0); |
| 46 |
|
| 47 |
|
| 48 |
inline uint GetTriangleCount() const { return mNumTriangles; } |
| 49 |
|
| 50 |
|
| 51 |
inline bool HasChildren() const { return mChild[0] != cInvalidNodeIndex || mChild[1] != cInvalidNodeIndex; } |
| 52 |
|
| 53 |
|
| 54 |
uint GetMinDepth(const Array<Node> &inNodes) const; |
| 55 |
|
| 56 |
|
| 57 |
uint GetMaxDepth(const Array<Node> &inNodes) const; |
| 58 |
|
| 59 |
|
| 60 |
uint GetNodeCount(const Array<Node> &inNodes) const; |
| 61 |
|
| 62 |
|
| 63 |
uint GetLeafNodeCount(const Array<Node> &inNodes) const; |
| 64 |
|
| 65 |
|
| 66 |
uint GetTriangleCountInTree(const Array<Node> &inNodes) const; |
| 67 |
|
| 68 |
|
| 69 |
void GetTriangleCountPerNode(const Array<Node> &inNodes, float &outAverage, uint &outMin, uint &outMax) const; |
| 70 |
|
| 71 |
|
| 72 |
float CalculateSAHCost(const Array<Node> &inNodes, float inCostTraversal, float inCostLeaf) const; |
| 73 |
|
| 74 |
|
| 75 |
void GetNChildren(const Array<Node> &inNodes, uint inN, Array<const Node *> &outChildren) const; |
| 76 |
|
| 77 |
|
| 78 |
AABox mBounds; |
| 79 |
|
| 80 |
|
| 81 |
uint mTrianglesBegin; |
| 82 |
uint mNumTriangles = 0; |
| 83 |
|
| 84 |
|
| 85 |
uint mChild[2] = { cInvalidNodeIndex, cInvalidNodeIndex }; |
| 86 |
|
| 87 |
private: |
| 88 |
friend class AABBTreeBuilder; |
| 89 |
|
| 90 |
|
| 91 |
float CalculateSAHCostInternal(const Array<Node> &inNodes, float inCostTraversalDivSurfaceArea, float inCostLeafDivSurfaceArea) const; |
| 92 |
|
| 93 |
|
| 94 |
void GetTriangleCountPerNodeInternal(const Array<Node> &inNodes, float &outAverage, uint &outAverageDivisor, uint &outMin, uint &outMax) const; |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
AABBTreeBuilder(TriangleSplitter &inSplitter, uint inMaxTrianglesPerLeaf = 16); |
| 99 |
|
| 100 |
|
| 101 |
Node * Build(AABBTreeBuilderStats &outStats); |
| 102 |
|
| 103 |
|
| 104 |
const Array<Node> & GetNodes() const { return mNodes; } |
| 105 |
|
| 106 |
|
| 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 |