VoxelUtility.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace Pathfinding.Voxels {
  4. using Pathfinding.Util;
  5. /// <summary>Various utilities for voxel rasterization.</summary>
  6. public class Utility {
  7. public static float Min (float a, float b, float c) {
  8. a = a < b ? a : b;
  9. return a < c ? a : c;
  10. }
  11. public static float Max (float a, float b, float c) {
  12. a = a > b ? a : b;
  13. return a > c ? a : c;
  14. }
  15. /// <summary>
  16. /// Removes duplicate vertices from the array and updates the triangle array.
  17. /// Returns: The new array of vertices
  18. /// </summary>
  19. public static Int3[] RemoveDuplicateVertices (Int3[] vertices, int[] triangles) {
  20. // Get a dictionary from an object pool to avoid allocating a new one
  21. var firstVerts = ObjectPoolSimple<Dictionary<Int3, int> >.Claim();
  22. firstVerts.Clear();
  23. // Remove duplicate vertices
  24. var compressedPointers = new int[vertices.Length];
  25. int count = 0;
  26. for (int i = 0; i < vertices.Length; i++) {
  27. if (!firstVerts.ContainsKey(vertices[i])) {
  28. firstVerts.Add(vertices[i], count);
  29. compressedPointers[i] = count;
  30. vertices[count] = vertices[i];
  31. count++;
  32. } else {
  33. // There are some cases, rare but still there, that vertices are identical
  34. compressedPointers[i] = firstVerts[vertices[i]];
  35. }
  36. }
  37. firstVerts.Clear();
  38. ObjectPoolSimple<Dictionary<Int3, int> >.Release(ref firstVerts);
  39. for (int i = 0; i < triangles.Length; i++) {
  40. triangles[i] = compressedPointers[triangles[i]];
  41. }
  42. var compressed = new Int3[count];
  43. for (int i = 0; i < count; i++) compressed[i] = vertices[i];
  44. return compressed;
  45. }
  46. }
  47. }