TriangleSplitter.h 2.5 KB

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