TriangleSplitter.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/Geometry/IndexedTriangle.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// A class that splits a triangle list into two parts for building a tree
  8. class TriangleSplitter
  9. {
  10. public:
  11. /// Constructor
  12. TriangleSplitter(const VertexList &inVertices, const IndexedTriangleList &inTriangles);
  13. /// Virtual destructor
  14. virtual ~TriangleSplitter() = default;
  15. struct Stats
  16. {
  17. const char * mSplitterName = nullptr;
  18. int mLeafSize = 0;
  19. };
  20. /// Get stats of splitter
  21. virtual void GetStats(Stats &outStats) const = 0;
  22. /// Helper struct to indicate triangle range before and after the split
  23. struct Range
  24. {
  25. /// Constructor
  26. Range() = default;
  27. Range(uint inBegin, uint inEnd) : mBegin(inBegin), mEnd(inEnd) { }
  28. /// Get number of triangles in range
  29. uint Count() const
  30. {
  31. return mEnd - mBegin;
  32. }
  33. /// Start and end index (end = 1 beyond end)
  34. uint mBegin;
  35. uint mEnd;
  36. };
  37. /// Range of triangles to start with
  38. Range GetInitialRange() const
  39. {
  40. return Range(0, (uint)mSortedTriangleIdx.size());
  41. }
  42. /// Split triangles into two groups left and right, returns false if no split could be made
  43. /// @param inTriangles The range of triangles (in mSortedTriangleIdx) to process
  44. /// @param outLeft On return this will contain the ranges for the left subpart. mSortedTriangleIdx may have been shuffled.
  45. /// @param outRight On return this will contain the ranges for the right subpart. mSortedTriangleIdx may have been shuffled.
  46. /// @return Returns true when a split was found
  47. virtual bool Split(const Range &inTriangles, Range &outLeft, Range &outRight) = 0;
  48. /// Get the list of vertices
  49. const VertexList & GetVertices() const
  50. {
  51. return mVertices;
  52. }
  53. /// Get triangle by index
  54. const IndexedTriangle & GetTriangle(uint inIdx) const
  55. {
  56. return mTriangles[mSortedTriangleIdx[inIdx]];
  57. }
  58. protected:
  59. /// Helper function to split triangles based on dimension and split value
  60. bool SplitInternal(const Range &inTriangles, uint inDimension, float inSplit, Range &outLeft, Range &outRight);
  61. const VertexList & mVertices; ///< Vertices of the indexed triangles
  62. const IndexedTriangleList & mTriangles; ///< Unsorted triangles
  63. Array<Float3> mCentroids; ///< Unsorted centroids of triangles
  64. Array<uint> mSortedTriangleIdx; ///< Indices to sort triangles
  65. };
  66. JPH_NAMESPACE_END