-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortLoops2.cs
52 lines (47 loc) · 1.36 KB
/
SortLoops2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections.Generic;
/// <summary>
/// Compares two face loops by averaging the vectors
/// that they reference, then comparing the averages.
/// </summary>
public class SortLoops2 : IComparer<Loop2>
{
/// <summary>
/// Coordinates referenced by loop indices.
/// </summary>
protected readonly Vec2[] coords;
/// <summary>
/// Constructs a loop sorting comparator from
/// an array to a mesh's coordinates.
/// </summary>
/// <param name="coords">mesh coordinates</param>
public SortLoops2(in Vec2[] coords)
{
this.coords = coords;
}
/// <summary>
/// Compares two loops in compliance with the IComparer interface.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public int Compare(Loop2 a, Loop2 b)
{
Vec2 aAvg = Vec2.Zero;
Index2[] aIdcs = a.Indices;
int aLen = aIdcs.Length;
for (int i = 0; i < aLen; ++i)
{
aAvg += this.coords[aIdcs[i].V];
}
aAvg /= aLen;
Vec2 bAvg = Vec2.Zero;
Index2[] bIdcs = b.Indices;
int bLen = bIdcs.Length;
for (int i = 0; i < bLen; ++i)
{
bAvg += this.coords[bIdcs[i].V];
}
bAvg /= bLen;
return aAvg.CompareTo(bAvg);
}
}