AABBTreeBuilder.h 3.4 KB

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