TriangleGrouperMorton.cpp 1.4 KB

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