TriangleSplitter.h 2.4 KB

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