DelaunayTests: pin the orientation predicate, the collinear detector, the super-triangle and whole triangulation of cocircular input

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-22 21:47:30 +01:00
commit c28003d295

View file

@ -16,24 +16,7 @@ public sealed class DelaunayTests
internal static (SortedSet<(int a, int b)> Edges, bool Unique) Oracle(IReadOnlyList<float2> points)
{
var n = points.Count;
var raw = new (BigInteger m, int e)[n, 2];
var lowest = int.MaxValue;
for (var i = 0; i < n; i++)
for (var axis = 0; axis < 2; axis++)
{
var bits = BitConverter.SingleToInt32Bits(axis == 0 ? points[i].x : points[i].y);
var biased = (bits >> 23) & 0xFF;
var m = biased == 0 ? bits & 0x7FFFFF : (bits & 0x7FFFFF) | 0x800000;
raw[i, axis] = (bits < 0 ? -m : m, biased == 0 ? -149 : biased - 150);
if (m != 0) lowest = Math.Min(lowest, raw[i, axis].e);
}
var px = new BigInteger[n];
var py = new BigInteger[n];
for (var i = 0; i < n; i++)
{
px[i] = raw[i, 0].m << Math.Max(0, raw[i, 0].e - lowest);
py[i] = raw[i, 1].m << Math.Max(0, raw[i, 1].e - lowest);
}
var (px, py) = ExactCoordinates(points);
var edges = new SortedSet<(int a, int b)>();
var unique = true;
@ -64,6 +47,78 @@ public sealed class DelaunayTests
return (edges, unique);
}
// Every coordinate as an integer in one shared unit, so differences and products are exact.
private static (BigInteger[] X, BigInteger[] Y) ExactCoordinates(IReadOnlyList<float2> points)
{
var n = points.Count;
var raw = new (BigInteger m, int e)[n, 2];
var lowest = int.MaxValue;
for (var i = 0; i < n; i++)
for (var axis = 0; axis < 2; axis++)
{
var bits = BitConverter.SingleToInt32Bits(axis == 0 ? points[i].x : points[i].y);
var biased = (bits >> 23) & 0xFF;
var m = biased == 0 ? bits & 0x7FFFFF : (bits & 0x7FFFFF) | 0x800000;
raw[i, axis] = (bits < 0 ? -m : m, biased == 0 ? -149 : biased - 150);
if (m != 0) lowest = Math.Min(lowest, raw[i, axis].e);
}
var px = new BigInteger[n];
var py = new BigInteger[n];
for (var i = 0; i < n; i++)
{
px[i] = raw[i, 0].m << Math.Max(0, raw[i, 0].e - lowest);
py[i] = raw[i, 1].m << Math.Max(0, raw[i, 1].e - lowest);
}
return (px, py);
}
private static int Turn(BigInteger[] x, BigInteger[] y, int a, int b, int c) =>
((x[b] - x[a]) * (y[c] - y[a]) - (y[b] - y[a]) * (x[c] - x[a])).Sign;
// The edges are a triangulation of the points' convex hull: no two cross, and their count is 3n - 3 - b, b being the
// points on the hull's boundary (a planar graph with that many edges has no face but triangles and the outer one).
// Cocircular input has several Delaunay triangulations; this pins that the one returned is a whole triangulation.
internal static void AssertTriangulatesTheHull(IReadOnlyList<float2> points, IReadOnlyList<(int a, int b)> edges, string context)
{
var n = points.Count;
var (x, y) = ExactCoordinates(points);
var order = Enumerable.Range(0, n).OrderBy(i => x[i]).ThenBy(i => y[i]).ToArray();
var hull = new List<int>();
foreach (var pass in new[] { order, order.Reverse().ToArray() })
{
var start = hull.Count;
foreach (var i in pass)
{
while (hull.Count >= start + 2 && Turn(x, y, hull[^2], hull[^1], i) <= 0) hull.RemoveAt(hull.Count - 1);
hull.Add(i);
}
hull.RemoveAt(hull.Count - 1);
}
var boundary = 0;
for (var p = 0; p < n; p++)
for (var h = 0; h < hull.Count; h++)
{
int u = hull[h], v = hull[(h + 1) % hull.Count];
if (Turn(x, y, u, v, p) != 0) continue;
if (BigInteger.Min(x[u], x[v]) <= x[p] && x[p] <= BigInteger.Max(x[u], x[v]) && BigInteger.Min(y[u], y[v]) <= y[p] && y[p] <= BigInteger.Max(y[u], y[v]))
{
boundary++;
break;
}
}
if (hull.Count == 2) boundary = n; // collinear input: every point is on the boundary
for (var i = 0; i < edges.Count; i++)
for (var j = i + 1; j < edges.Count; j++)
{
var (a, b) = edges[i];
var (c, d) = edges[j];
if (a == c || a == d || b == c || b == d) continue;
Assert.False(Turn(x, y, a, b, c) * Turn(x, y, a, b, d) < 0 && Turn(x, y, c, d, a) * Turn(x, y, c, d, b) < 0,
$"{context}: edges ({a},{b}) and ({c},{d}) cross");
}
Assert.True(edges.Count == 3 * n - 3 - boundary, $"{context}: {edges.Count} edges, a triangulation of the hull has {3 * n - 3 - boundary}");
}
// The triangulation lies within the oracle's edges, and equals them when the oracle's answer is unique.
internal static void AssertDelaunay(IReadOnlyList<float2> points, string context)
{
@ -72,6 +127,7 @@ public sealed class DelaunayTests
Assert.True(edges.All(oracle.Contains), $"{context}: edge outside the Delaunay set: {string.Join(" ", edges.Where(e => !oracle.Contains(e)))}");
if (unique)
Assert.True(oracle.SetEquals(edges), $"{context}: missed Delaunay edges: {string.Join(" ", oracle.Where(e => !edges.Contains(e)))}");
if (!unique) AssertTriangulatesTheHull(points, edges, context);
Assert.Equal(points.Count, edges.SelectMany(e => new[] { e.a, e.b }).Distinct().Count());
}
@ -163,6 +219,49 @@ public sealed class DelaunayTests
Assert.Equal(19, Delaunay.Edges(slanted).Count);
}
// Kills: the orientation exact path replaced by the plain double sign. Each triple is far from collinear in
// exact arithmetic, yet its double cross product is 0 (the coordinate differences span more than 53 bits and round),
// so the plain sign calls the three points collinear and links only two of the three edges.
[Theory]
[InlineData(66144f, -196608f, 480f, 384f, -70368744177664f, 211106232532992f)]
[InlineData(1.9075232557952404e-05f, 6.62929865148937e+18f, 1.9073442672379315e-05f, 6.62929865148937e+18f, 844424930131968f, 6.629861601442791e+18f)]
[InlineData(-0.00018310546875f, 1759218604441600f, -1610612736f, 1759220215054336f, -2.8823037615171174e+18f, 2.884062980121559e+18f)]
[InlineData(-1536f, -0.0361328125f, 162304f, 81919.9609375f, -1.8014398509481984e+17f, -9.007199254740992e+16f)]
public void WideExponentTrianglesAreNotCalledCollinear(float ax, float ay, float bx, float by, float cx, float cy)
{
var points = new[] { float2(ax, ay), float2(bx, by), float2(cx, cy) };
Assert.Equal(new[] { (0, 1), (0, 2), (1, 2) }, Delaunay.Edges(points));
}
// Kills: the collinear detector skipping the last sorted point. One point off the line sorts last, so a detector
// that never tests it reads the set as a line and drops the hull edges to it.
[Fact]
public void ALoneOffLinePointThatSortsLastStillTriangulates()
{
var points = new[] { float2(4, 1), float2(0, 0), float2(2, 0), float2(1, 0), float2(3, 0) };
// The apex fans to every point on the base: the only triangulation of this hull.
Assert.Equal(new[] { (0, 1), (0, 2), (0, 3), (0, 4), (1, 3), (2, 4), (3, 2) }.Select(e => (Math.Min(e.Item1, e.Item2), Math.Max(e.Item1, e.Item2))).OrderBy(e => e).ToArray(),
Delaunay.Edges(points));
AssertDelaunay(points, "off-line point last");
}
// Kills: the super-triangle too small to keep its vertices out of a thin triangle's circumcircle. A triangle of
// height h on a base of 1 has a circumradius near 1/(8h); the super vertices must stay outside that circle, or
// they cut the triangle's hull edges.
[Theory]
[InlineData(-10)]
[InlineData(-30)]
[InlineData(-60)]
[InlineData(-100)]
[InlineData(-140)]
public void ThinTrianglesKeepTheirHullEdges(int exponent)
{
var h = (float) Math.Pow(2, exponent);
AssertDelaunay(new[] { float2(0, 0), float2(1, 0), float2(0.5f, h) }, $"3 points, h 2^{exponent}");
AssertDelaunay(new[] { float2(0, 0), float2(1, 0), float2(0.25f, h), float2(0.5f, h), float2(0.75f, h) }, $"5 points, h 2^{exponent}");
Assert.Equal(new[] { (0, 1), (0, 2), (1, 2) }, Delaunay.Edges(new[] { float2(0, 0), float2(1, 0), float2(0.5f, h) }));
}
// Kills: the duplicate check.
[Fact]
public void DuplicatePointsAreRefusedByIndex()