using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Mathematics;
namespace UnityEngine.U2D.Animation
{
[BurstCompile]
internal static class MeshUtilities
{
///
/// Get the outline edges from a set of indices.
/// This method expects the index array to be laid out with one triangle for every 3 indices.
/// E.g. triangle 0: index 0 - 2, triangle 1: index 3 - 5, etc.
///
/// Returns a NativeArray of sorted edges. It is up to the caller to dispose this array.
public static NativeArray GetOutlineEdges(in NativeArray indices)
{
UnsafeHashMap edges = new UnsafeHashMap(indices.Length, Allocator.Persistent);
for (int i = 0; i < indices.Length; i += 3)
{
ushort i0 = indices[i];
ushort i1 = indices[i + 1];
ushort i2 = indices[i + 2];
int2 edge0 = new int2(i0, i1);
int2 edge1 = new int2(i1, i2);
int2 edge2 = new int2(i2, i0);
AddToEdgeMap(edge0, ref edges);
AddToEdgeMap(edge1, ref edges);
AddToEdgeMap(edge2, ref edges);
}
#if COLLECTIONS_2_0_OR_ABOVE
NativeList outlineEdges = new NativeList(edges.Count, Allocator.Temp);
#else
var outlineEdges = new NativeList(edges.Count(), Allocator.Temp);
#endif
foreach (KVPair edgePair in edges)
{
// If an edge is only used in one triangle, it is an outline edge.
if (edgePair.Value.z == 1)
outlineEdges.Add(edgePair.Value.xy);
}
edges.Dispose();
SortEdges(outlineEdges.AsArray(), out NativeArray sortedEdges);
return sortedEdges;
}
[BurstCompile]
static void AddToEdgeMap(in int2 edge, ref UnsafeHashMap edgeMap)
{
int2 tmpEdge = math.min(edge.x, edge.y) == edge.x ? edge.xy : edge.yx;
int hashCode = tmpEdge.GetHashCode();
// We store the hashCode as key, so that we can do less GetHashCode-calls.
// Then we store the count the int3s z-value.
if (!edgeMap.ContainsKey(hashCode))
edgeMap[hashCode] = new int3(edge, 1);
else
{
int3 val = edgeMap[hashCode];
val.z++;
edgeMap[hashCode] = val;
}
}
[BurstCompile]
static void SortEdges(in NativeArray unsortedEdges, out NativeArray sortedEdges)
{
NativeArray tmpEdges = new NativeArray(unsortedEdges.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
NativeList shapeStartingEdge = new NativeList(1, Allocator.Persistent);
UnsafeHashMap edgeMap = new UnsafeHashMap(unsortedEdges.Length, Allocator.Persistent);
NativeArray usedEdges = new NativeArray(unsortedEdges.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
for (int i = 0; i < unsortedEdges.Length; i++)
{
edgeMap[unsortedEdges[i].x] = i;
usedEdges[i] = false;
}
bool findStartingEdge = true;
int edgeIndex = -1;
int startingEdge = 0;
for (int i = 0; i < unsortedEdges.Length; i++)
{
if (findStartingEdge)
{
edgeIndex = GetFirstUnusedIndex(usedEdges);
startingEdge = edgeIndex;
findStartingEdge = false;
shapeStartingEdge.Add(i);
}
usedEdges[edgeIndex] = true;
tmpEdges[i] = unsortedEdges[edgeIndex];
int nextVertex = unsortedEdges[edgeIndex].y;
edgeIndex = edgeMap[nextVertex];
if (edgeIndex == startingEdge)
findStartingEdge = true;
}
int finalEdgeArrLength = unsortedEdges.Length;
sortedEdges = new NativeArray(finalEdgeArrLength, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
int count = 0;
for (int i = 0; i < shapeStartingEdge.Length; ++i)
{
int edgeStart = shapeStartingEdge[i];
int edgeEnd = (i + 1) == shapeStartingEdge.Length ? tmpEdges.Length : shapeStartingEdge[i + 1];
for (int m = edgeStart; m < edgeEnd; ++m)
sortedEdges[count++] = tmpEdges[m];
}
usedEdges.Dispose();
edgeMap.Dispose();
shapeStartingEdge.Dispose();
tmpEdges.Dispose();
}
[BurstCompile]
static int GetFirstUnusedIndex(in NativeArray usedValues)
{
for (int i = 0; i < usedValues.Length; i++)
{
if (!usedValues[i])
return i;
}
return -1;
}
}
}