AABBTreeBuilder.h 3.4 KB

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