AABBTreeBuilder.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 : public NonCopyable
  32. {
  33. public:
  34. JPH_OVERRIDE_NEW_DELETE
  35. /// Constructor
  36. Node();
  37. ~Node();
  38. /// Get number of triangles in this node
  39. inline uint GetTriangleCount() const { return uint(mTriangles.size()); }
  40. /// Check if this node has any children
  41. inline bool HasChildren() const { return mChild[0] != nullptr || mChild[1] != nullptr; }
  42. /// Min depth of tree
  43. uint GetMinDepth() const;
  44. /// Max depth of tree
  45. uint GetMaxDepth() const;
  46. /// Number of nodes in tree
  47. uint GetNodeCount() const;
  48. /// Number of leaf nodes in tree
  49. uint GetLeafNodeCount() const;
  50. /// Get triangle count in tree
  51. uint GetTriangleCountInTree() const;
  52. /// Calculate min and max triangles per node
  53. void GetTriangleCountPerNode(float &outAverage, uint &outMin, uint &outMax) const;
  54. /// Calculate the total cost of the tree using the surface area heuristic
  55. float CalculateSAHCost(float inCostTraversal, float inCostLeaf) const;
  56. /// Recursively get children (breadth first) to get in total inN children (or less if there are no more)
  57. void GetNChildren(uint inN, Array<const Node *> &outChildren) const;
  58. /// Bounding box
  59. AABox mBounds;
  60. /// Triangles (if no child nodes)
  61. IndexedTriangleList mTriangles;
  62. /// Child nodes (if no triangles)
  63. Node * mChild[2];
  64. private:
  65. friend class AABBTreeBuilder;
  66. /// Recursive helper function to calculate cost of the tree
  67. float CalculateSAHCostInternal(float inCostTraversalDivSurfaceArea, float inCostLeafDivSurfaceArea) const;
  68. /// Recursive helper function to calculate min and max triangles per node
  69. void GetTriangleCountPerNodeInternal(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. private:
  76. Node * BuildInternal(const TriangleSplitter::Range &inTriangles);
  77. TriangleSplitter & mTriangleSplitter;
  78. const uint mMaxTrianglesPerLeaf;
  79. };
  80. JPH_NAMESPACE_END