Path: blob/master/thirdparty/jolt_physics/Jolt/AABBTree/AABBTreeBuilder.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/TriangleSplitter/TriangleSplitter.h>7#include <Jolt/Geometry/AABox.h>8#include <Jolt/Core/NonCopyable.h>910JPH_NAMESPACE_BEGIN1112struct AABBTreeBuilderStats13{14///@name Splitter stats15TriangleSplitter::Stats mSplitterStats; ///< Stats returned by the triangle splitter algorithm1617///@name Tree structure18float mSAHCost = 0.0f; ///< Surface Area Heuristic cost of this tree19int mMinDepth = 0; ///< Minimal depth of tree (number of nodes)20int mMaxDepth = 0; ///< Maximum depth of tree (number of nodes)21int mNodeCount = 0; ///< Number of nodes in the tree22int mLeafNodeCount = 0; ///< Number of leaf nodes (that contain triangles)2324///@name Configured stats25int mMaxTrianglesPerLeaf = 0; ///< Configured max triangles per leaf2627///@name Actual stats28int mTreeMinTrianglesPerLeaf = 0; ///< Minimal amount of triangles in a leaf29int mTreeMaxTrianglesPerLeaf = 0; ///< Maximal amount of triangles in a leaf30float mTreeAvgTrianglesPerLeaf = 0.0f; ///< Average amount of triangles in leaf nodes31};3233/// Helper class to build an AABB tree34class JPH_EXPORT AABBTreeBuilder35{36public:37/// A node in the tree, contains the AABox for the tree and any child nodes or triangles38class Node39{40public:41JPH_OVERRIDE_NEW_DELETE4243/// Indicates that there is no child44static constexpr uint cInvalidNodeIndex = ~uint(0);4546/// Get number of triangles in this node47inline uint GetTriangleCount() const { return mNumTriangles; }4849/// Check if this node has any children50inline bool HasChildren() const { return mChild[0] != cInvalidNodeIndex || mChild[1] != cInvalidNodeIndex; }5152/// Min depth of tree53uint GetMinDepth(const Array<Node> &inNodes) const;5455/// Max depth of tree56uint GetMaxDepth(const Array<Node> &inNodes) const;5758/// Number of nodes in tree59uint GetNodeCount(const Array<Node> &inNodes) const;6061/// Number of leaf nodes in tree62uint GetLeafNodeCount(const Array<Node> &inNodes) const;6364/// Get triangle count in tree65uint GetTriangleCountInTree(const Array<Node> &inNodes) const;6667/// Calculate min and max triangles per node68void GetTriangleCountPerNode(const Array<Node> &inNodes, float &outAverage, uint &outMin, uint &outMax) const;6970/// Calculate the total cost of the tree using the surface area heuristic71float CalculateSAHCost(const Array<Node> &inNodes, float inCostTraversal, float inCostLeaf) const;7273/// Recursively get children (breadth first) to get in total inN children (or less if there are no more)74void GetNChildren(const Array<Node> &inNodes, uint inN, Array<const Node *> &outChildren) const;7576/// Bounding box77AABox mBounds;7879/// Triangles (if no child nodes)80uint mTrianglesBegin; // Index into mTriangles81uint mNumTriangles = 0;8283/// Child node indices (if no triangles)84uint mChild[2] = { cInvalidNodeIndex, cInvalidNodeIndex };8586private:87friend class AABBTreeBuilder;8889/// Recursive helper function to calculate cost of the tree90float CalculateSAHCostInternal(const Array<Node> &inNodes, float inCostTraversalDivSurfaceArea, float inCostLeafDivSurfaceArea) const;9192/// Recursive helper function to calculate min and max triangles per node93void GetTriangleCountPerNodeInternal(const Array<Node> &inNodes, float &outAverage, uint &outAverageDivisor, uint &outMin, uint &outMax) const;94};9596/// Constructor97AABBTreeBuilder(TriangleSplitter &inSplitter, uint inMaxTrianglesPerLeaf = 16);9899/// Recursively build tree, returns the root node of the tree100Node * Build(AABBTreeBuilderStats &outStats);101102/// Get all nodes103const Array<Node> & GetNodes() const { return mNodes; }104105/// Get all triangles106const Array<IndexedTriangle> &GetTriangles() const { return mTriangles; }107108private:109uint BuildInternal(const TriangleSplitter::Range &inTriangles);110111TriangleSplitter & mTriangleSplitter;112const uint mMaxTrianglesPerLeaf;113Array<Node> mNodes;114Array<IndexedTriangle> mTriangles;115};116117JPH_NAMESPACE_END118119120