Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Created September 19, 2020 21:44
Show Gist options
  • Save andrew-raphael-lukasik/4419c98e4aa572c50e024d8657f6eb3a to your computer and use it in GitHub Desktop.
Save andrew-raphael-lukasik/4419c98e4aa572c50e024d8657f6eb3a to your computer and use it in GitHub Desktop.
creates array of edges from array of mesh triangles
int2[] ToEdges ( int[] triangles )
{
var edges = new Dictionary<ulong,int2>();
for( int i=0 ; i<triangles.Length ; i+=3 )
{
int a = triangles[i];
int b = triangles[i+1];
int c = triangles[i+2];
ulong hash;
hash = (ulong)math.max(a,b)*(ulong)1e6 + (ulong)math.min(a,b);
if( !edges.ContainsKey(hash) )
edges.Add( hash , new int2(a,b) );
hash = (ulong)math.max(b,c)*(ulong)1e6 + (ulong)math.min(b,c);
if( !edges.ContainsKey(hash) )
edges.Add( hash , new int2(b,c) );
hash = (ulong)math.max(c,a)*(ulong)1e6 + (ulong)math.min(c,a);
if( !edges.ContainsKey(hash) )
edges.Add( hash , new int2(c,a) );
}
var results = new int2[ edges.Count ];
edges.Values.CopyTo( results , 0 );
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment