Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: utils cleanup #55

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 45 additions & 65 deletions Runtime/Triangulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ public void Execute()
{
if (i == i0 || i == i1) continue;
var p = positions[i];
var r = CircumRadius(p0, p1, p);
var r = CircumRadiusSq(p0, p1, p);
if (r < minRadius)
{
i2 = i;
Expand All @@ -991,7 +991,7 @@ public void Execute()
if (minRadius == float.MaxValue)
{
Debug.LogError("[Triangulator]: Provided input is not supported!");
status.Value = Status.ERR;
status.Value |= Status.ERR;
return;
}

Expand Down Expand Up @@ -1193,46 +1193,6 @@ private void Link(int a, int b)
halfedges[a] = b;
if (b != -1) halfedges[b] = a;
}

private static float CircumRadius(float2 a, float2 b, float2 c) => math.distancesq(CircumCenter(a, b, c), a);

private static float2 CircumCenter(float2 a, float2 b, float2 c)
{
var dx = b.x - a.x;
var dy = b.y - a.y;
var ex = c.x - a.x;
var ey = c.y - a.y;

var bl = dx * dx + dy * dy;
var cl = ex * ex + ey * ey;

var d = 0.5f / (dx * ey - dy * ex);

var x = a.x + (ey * bl - dy * cl) * d;
var y = a.y + (dx * cl - ex * bl) * d;

return new(x, y);
}

private static float Orient2dFast(float2 a, float2 b, float2 c) => (a.y - c.y) * (b.x - c.x) - (a.x - c.x) * (b.y - c.y);

private static bool InCircle(float2 a, float2 b, float2 c, float2 p)
{
var dx = a.x - p.x;
var dy = a.y - p.y;
var ex = b.x - p.x;
var ey = b.y - p.y;
var fx = c.x - p.x;
var fy = c.y - p.y;

var ap = dx * dx + dy * dy;
var bp = ex * ex + ey * ey;
var cp = fx * fx + fy * fy;

return dx * (ey * cp - bp * fy) -
dy * (ex * cp - bp * fx) +
ap * (ex * fy - ey * fx) < 0;
}
}

[BurstCompile]
Expand Down Expand Up @@ -1269,7 +1229,7 @@ private void Execute(int t)
{
var (i, j, k) = triangles[t];

circles[t] = CalculateCircumcenter((i, j, k), positions.AsArray());
circles[t] = CalculateCircumCircle((i, j, k), positions.AsArray());
trianglesToEdges[t] = ((i, j), (i, k), (j, k));

RegisterEdgeData((i, j), t);
Expand Down Expand Up @@ -1629,8 +1589,8 @@ private void UnsafeSwapEdge(Edge edge)
edgesToTriangles.Remove(edge);
edgesToTriangles.Add((q0, q1), new() { t0, t1 });

circles[t0] = CalculateCircumcenter(triangles[t0], outputPositions);
circles[t1] = CalculateCircumcenter(triangles[t1], outputPositions);
circles[t0] = CalculateCircumCircle(triangles[t0], outputPositions);
circles[t1] = CalculateCircumCircle(triangles[t1], outputPositions);
}

public void Execute()
Expand Down Expand Up @@ -2283,13 +2243,6 @@ public void Execute()
}
private static readonly DescendingComparer comparer = new();

private static Circle CalculateCircumcenter(Triangle triangle, NativeArray<float2> outputPositions)
{
var (idA, idB, idC) = triangle;
var (pA, pB, pC) = (outputPositions[idA], outputPositions[idB], outputPositions[idC]);
return GetCircumcenter(pA, pB, pC);
}

private static int InsertPoint(float2 p,
FixedList128Bytes<int> initTriangles,
NativeList<Triangle> triangles,
Expand Down Expand Up @@ -2475,7 +2428,7 @@ NativeHashMap<Edge, FixedList32Bytes<int>> edgesToTriangles
var tId = triangles.Length;
triangles.Add(t);
var (idA, idB, idC) = t;
var circle = CalculateCircumcenter(t, outputPositions.AsArray());
var circle = CalculateCircumCircle(t, outputPositions.AsArray());
circles.Add(circle);
trianglesToEdges.Add(((idA, idB), (idA, idC), (idB, idC)));

Expand All @@ -2499,21 +2452,48 @@ void RegisterEdgeData(Edge edge, int triangleId)

private static float Angle(float2 a, float2 b) => math.atan2(Cross(a, b), math.dot(a, b));
private static float Cross(float2 a, float2 b) => a.x * b.y - a.y * b.x;
private static Circle GetCircumcenter(float2 a, float2 b, float2 c)
private static Circle CalculateCircumCircle(Triangle triangle, NativeArray<float2> outputPositions)
{
var (idA, idB, idC) = triangle;
var (pA, pB, pC) = (outputPositions[idA], outputPositions[idB], outputPositions[idC]);
return new(CircumCenter(pA, pB, pC), CircumRadius(pA, pB, pC));
}
private static float CircumRadius(float2 a, float2 b, float2 c) => math.distance(CircumCenter(a, b, c), a);
private static float CircumRadiusSq(float2 a, float2 b, float2 c) => math.distancesq(CircumCenter(a, b, c), a);
private static float2 CircumCenter(float2 a, float2 b, float2 c)
{
var aLenSq = math.lengthsq(a);
var bLenSq = math.lengthsq(b);
var cLenSq = math.lengthsq(c);
var dx = b.x - a.x;
var dy = b.y - a.y;
var ex = c.x - a.x;
var ey = c.y - a.y;

var d = 2 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y));
var p = math.float2
(
x: aLenSq * (b.y - c.y) + bLenSq * (c.y - a.y) + cLenSq * (a.y - b.y),
y: aLenSq * (c.x - b.x) + bLenSq * (a.x - c.x) + cLenSq * (b.x - a.x)
) / d;
var r = math.distance(p, a);
var bl = dx * dx + dy * dy;
var cl = ex * ex + ey * ey;

var d = 0.5f / (dx * ey - dy * ex);

return new Circle(center: p, radius: r);
var x = a.x + (ey * bl - dy * cl) * d;
var y = a.y + (dx * cl - ex * bl) * d;

return new(x, y);
}
private static float Orient2dFast(float2 a, float2 b, float2 c) => (a.y - c.y) * (b.x - c.x) - (a.x - c.x) * (b.y - c.y);
private static bool InCircle(float2 a, float2 b, float2 c, float2 p)
{
var dx = a.x - p.x;
var dy = a.y - p.y;
var ex = b.x - p.x;
var ey = b.y - p.y;
var fx = c.x - p.x;
var fy = c.y - p.y;

var ap = dx * dx + dy * dy;
var bp = ex * ex + ey * ey;
var cp = fx * fx + fy * fy;

return dx * (ey * cp - bp * fy) -
dy * (ex * cp - bp * fx) +
ap * (ex * fy - ey * fx) < 0;
}
private static float3 Barycentric(float2 a, float2 b, float2 c, float2 p)
{
Expand Down