-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortLoops3.cs
52 lines (47 loc) · 1.36 KB
/
SortLoops3.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 SortLoops3 : IComparer<Loop3>
{
/// <summary>
/// Coordinates referenced by loop indices.
/// </summary>
protected readonly Vec3[] coords;
/// <summary>
/// Constructs a loop sorting comparator from
/// an array to a mesh's coordinates.
/// </summary>
/// <param name="coords">mesh coordinates</param>
public SortLoops3(in Vec3[] 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(Loop3 a, Loop3 b)
{
Vec3 aAvg = Vec3.Zero;
Index3[] aIdcs = a.Indices;
int aLen = aIdcs.Length;
for (int i = 0; i < aLen; ++i)
{
aAvg += this.coords[aIdcs[i].V];
}
aAvg /= aLen;
Vec3 bAvg = Vec3.Zero;
Index3[] 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);
}
}