TriangleGrouperMorton.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. JPH_NAMESPACE_BEGIN
  7. void TriangleGrouperMorton::Group(const VertexList &inVertices, const IndexedTriangleList &inTriangles, int inGroupSize, vector<uint> &outGroupedTriangleIndices)
  8. {
  9. const uint triangle_count = (uint)inTriangles.size();
  10. vector<Vec3> centroids;
  11. centroids.resize(triangle_count);
  12. outGroupedTriangleIndices.resize(triangle_count);
  13. for (uint t = 0; t < triangle_count; ++t)
  14. {
  15. // Store centroid
  16. centroids[t] = inTriangles[t].GetCentroid(inVertices);
  17. // Initialize sort table
  18. outGroupedTriangleIndices[t] = t;
  19. }
  20. // Get bounding box of all centroids
  21. AABox centroid_bounds;
  22. for (uint t = 0; t < triangle_count; ++t)
  23. centroid_bounds.Encapsulate(centroids[t]);
  24. // Make sure box is not degenerate
  25. centroid_bounds.EnsureMinimalEdgeLength(1.0e-5f);
  26. // Calculate morton code for each centroid
  27. vector<uint32> morton_codes;
  28. morton_codes.resize(triangle_count);
  29. for (uint t = 0; t < triangle_count; ++t)
  30. morton_codes[t] = MortonCode::sGetMortonCode(centroids[t], centroid_bounds);
  31. // Sort triangles based on morton code
  32. sort(outGroupedTriangleIndices.begin(), outGroupedTriangleIndices.end(), [&morton_codes](uint inLHS, uint inRHS) { return morton_codes[inLHS] < morton_codes[inRHS]; });
  33. }
  34. JPH_NAMESPACE_END