AABBTreeBuilder.h 4.1 KB

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