TriangleGrouperMorton.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/TriangleGrouper/TriangleGrouperMorton.h>
  6. #include <Jolt/Geometry/MortonCode.h>
  7. #include <Jolt/Core/QuickSort.h>
  8. JPH_NAMESPACE_BEGIN
  9. void TriangleGrouperMorton::Group(const VertexList &inVertices, const IndexedTriangleList &inTriangles, int inGroupSize, Array<uint> &outGroupedTriangleIndices)
  10. {
  11. const uint triangle_count = (uint)inTriangles.size();
  12. Array<Vec3> centroids;
  13. centroids.resize(triangle_count);
  14. outGroupedTriangleIndices.resize(triangle_count);
  15. for (uint t = 0; t < triangle_count; ++t)
  16. {
  17. // Store centroid
  18. centroids[t] = inTriangles[t].GetCentroid(inVertices);
  19. // Initialize sort table
  20. outGroupedTriangleIndices[t] = t;
  21. }
  22. // Get bounding box of all centroids
  23. AABox centroid_bounds;
  24. for (uint t = 0; t < triangle_count; ++t)
  25. centroid_bounds.Encapsulate(centroids[t]);
  26. // Make sure box is not degenerate
  27. centroid_bounds.EnsureMinimalEdgeLength(1.0e-5f);
  28. // Calculate morton code for each centroid
  29. Array<uint32> morton_codes;
  30. morton_codes.resize(triangle_count);
  31. for (uint t = 0; t < triangle_count; ++t)
  32. morton_codes[t] = MortonCode::sGetMortonCode(centroids[t], centroid_bounds);
  33. // Sort triangles based on morton code
  34. QuickSort(outGroupedTriangleIndices.begin(), outGroupedTriangleIndices.end(), [&morton_codes](uint inLHS, uint inRHS) { return morton_codes[inLHS] < morton_codes[inRHS]; });
  35. }
  36. JPH_NAMESPACE_END