Delaunay: exact orientation and in-circle predicates, loud degenerate input, Galaxy zone-linked guard

Double-precision filter with BigInteger fallback replaces the circumcentre test; the super-triangle is far enough out
that no float input reaches it. Collinear points link along the line, duplicate or non-finite points throw, and
Galaxy refuses a zone no link reaches. The exact oracle replaces ScenarioTests' double brute force.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Nibu 2026-11-13 15:36:20 +00:00 • committed by soul
commit f428ab1d88
4 changed files with 319 additions and 48 deletions

View file

@ -1,17 +1,34 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using CultMath;
// The Delaunay triangulation of a galaxy's zone positions, as the edges Galaxy links zones along. Bowyer-Watson
// (Bowyer 1981, Watson 1981) in double precision: each point is inserted by removing every triangle whose
// circumcircle holds it and fanning the cavity's boundary to it. Every point lands in the triangulation, so for two or
// more distinct points the edge graph is connected.
// (Bowyer 1981, Watson 1981): each point is inserted by removing every triangle whose circumcircle strictly holds it and
// fanning the cavity's boundary to it. The orientation and in-circle predicates are exact: a double-precision
// determinant answers when it clears its rounding-error bound (Shewchuk 1997), otherwise the determinant is evaluated
// in BigInteger, since every double is a dyadic rational. For two or more distinct points the edge graph is connected:
// collinear points link each to its neighbour along the line; duplicate or non-finite points throw.
public static class Delaunay
{
// Each edge once, as (lower index, higher index), in ascending order.
public static List<(int a, int b)> Edges(IReadOnlyList<float2> points)
{
var n = points.Count;
var order = new int[n];
for (var i = 0; i < n; i++)
{
if (!float.IsFinite(points[i].x) || !float.IsFinite(points[i].y))
throw new ArgumentException($"Delaunay point {i} is not finite: ({points[i].x}, {points[i].y})");
order[i] = i;
}
// Lexicographic order runs along any line, and puts equal points side by side.
Array.Sort(order, (i, j) => points[i].x != points[j].x ? points[i].x.CompareTo(points[j].x) : points[i].y.CompareTo(points[j].y));
for (var i = 1; i < n; i++)
if (points[order[i - 1]].x == points[order[i]].x && points[order[i - 1]].y == points[order[i]].y)
throw new ArgumentException($"Delaunay points {Math.Min(order[i - 1], order[i])} and {Math.Max(order[i - 1], order[i])} are the same point ({points[order[i]].x}, {points[order[i]].y})");
if (n < 2) return new List<(int a, int b)>();
var x = new double[n + 3];
var y = new double[n + 3];
double minX = double.MaxValue, minY = double.MaxValue, maxX = double.MinValue, maxY = double.MinValue;
@ -25,9 +42,22 @@ public static class Delaunay
maxY = Math.Max(maxY, y[i]);
}
// A super-triangle far outside the points, so its vertices bend no edge between them; its triangles are
// dropped at the end.
var size = Math.Max(Math.Max(maxX - minX, maxY - minY), 1e-6) * 1000;
var collinear = true;
for (var i = 2; i < n && collinear; i++)
collinear = Orientation(x, y, order[0], order[1], order[i]) == 0;
if (collinear)
{
var line = new List<(int a, int b)>();
for (var i = 1; i < n; i++) line.Add(Ordered(order[i - 1], order[i]));
line.Sort();
return line;
}
// A super-triangle so far outside the points that its vertices bend no edge between them: a float's coordinates
// are multiples of 2^-149, so a triple of distinct non-collinear points has circumradius under extent^3 * 2^300,
// which 2^700 * extent exceeds for any extent a float holds. Its triangles are dropped at the end. Its
// coordinates overflow the double determinants, which then fail every comparison and fall to the exact path.
var size = Math.Max(maxX - minX, maxY - minY) * Math.Pow(2, 700);
double midX = (minX + maxX) / 2, midY = (minY + maxY) / 2;
x[n] = midX - size; y[n] = midY - size;
x[n + 1] = midX + size; y[n + 1] = midY - size;
@ -41,9 +71,7 @@ public static class Delaunay
for (var t = triangles.Count - 1; t >= 0; t--)
{
var triangle = triangles[t];
var dx = x[p] - triangle.CenterX;
var dy = y[p] - triangle.CenterY;
if (dx * dx + dy * dy >= triangle.RadiusSquared) continue;
if (InCircle(x, y, triangle.A, triangle.B, triangle.C, p) * triangle.Orientation <= 0) continue;
Count(cavity, triangle.A, triangle.B);
Count(cavity, triangle.B, triangle.C);
Count(cavity, triangle.C, triangle.A);
@ -79,19 +107,90 @@ public static class Delaunay
private readonly struct Triangle
{
public readonly int A, B, C;
public readonly double CenterX, CenterY, RadiusSquared;
// +1 if A, B, C run counter-clockwise, -1 if clockwise; a triangle never has zero area.
public readonly int Orientation;
public Triangle(int a, int b, int c, double[] x, double[] y)
{
A = a;
B = b;
C = c;
double ax = x[a], ay = y[a], bx = x[b], by = y[b], cx = x[c], cy = y[c];
var d = 2 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
double a2 = ax * ax + ay * ay, b2 = bx * bx + by * by, c2 = cx * cx + cy * cy;
CenterX = (a2 * (by - cy) + b2 * (cy - ay) + c2 * (ay - by)) / d;
CenterY = (a2 * (cx - bx) + b2 * (ax - cx) + c2 * (bx - ax)) / d;
RadiusSquared = (ax - CenterX) * (ax - CenterX) + (ay - CenterY) * (ay - CenterY);
Orientation = Delaunay.Orientation(x, y, a, b, c);
if (Orientation == 0) throw new InvalidOperationException($"Delaunay built a zero-area triangle on points {a}, {b}, {c}");
}
}
// Shewchuk's error bounds for the double-precision determinants: (3 + 16e)e and (10 + 96e)e, e = 2^-53.
private const double OrientationBound = 3.3306690738754716e-16;
private const double InCircleBound = 1.1102230246251577e-15;
// The sign of the area a, b, c span: +1 counter-clockwise, -1 clockwise, 0 collinear.
private static int Orientation(double[] x, double[] y, int a, int b, int c)
{
var left = (x[a] - x[c]) * (y[b] - y[c]);
var right = (y[a] - y[c]) * (x[b] - x[c]);
var det = left - right;
var bound = OrientationBound * (Math.Abs(left) + Math.Abs(right));
if (det > bound) return 1;
if (-det > bound) return -1;
var exact = Exact.Of(x, y, a, b, c);
return (exact[0] * exact[3] - exact[1] * exact[2]).Sign;
}
// The sign of the in-circle determinant: +1 when p is strictly inside the circle through a, b, c taken
// counter-clockwise, -1 strictly outside, 0 on it.
private static int InCircle(double[] x, double[] y, int a, int b, int c, int p)
{
double adx = x[a] - x[p], ady = y[a] - y[p], bdx = x[b] - x[p], bdy = y[b] - y[p], cdx = x[c] - x[p], cdy = y[c] - y[p];
double abL = adx * bdy, abR = bdx * ady, bcL = bdx * cdy, bcR = cdx * bdy, caL = cdx * ady, caR = adx * cdy;
double alift = adx * adx + ady * ady, blift = bdx * bdx + bdy * bdy, clift = cdx * cdx + cdy * cdy;
var det = alift * (bcL - bcR) + blift * (caL - caR) + clift * (abL - abR);
var permanent = (Math.Abs(bcL) + Math.Abs(bcR)) * alift + (Math.Abs(caL) + Math.Abs(caR)) * blift + (Math.Abs(abL) + Math.Abs(abR)) * clift;
var bound = InCircleBound * permanent;
if (det > bound) return 1;
if (-det > bound) return -1;
var e = Exact.Of(x, y, a, b, c, p);
BigInteger ax = e[0], ay = e[1], bx = e[2], by = e[3], cx = e[4], cy = e[5];
return (e[6] * (bx * cy - cx * by) + e[7] * (cx * ay - ax * cy) + e[8] * (ax * by - bx * ay)).Sign;
}
// Coordinates exactly, as integers in units of the smallest binary exponent among them.
private static class Exact
{
// Orientation: [a.x - c.x, a.y - c.y, b.x - c.x, b.y - c.y] with the cross product the caller takes.
public static BigInteger[] Of(double[] x, double[] y, int a, int b, int c)
{
var v = Scaled(new[] { x[a], y[a], x[b], y[b], x[c], y[c] });
return new[] { v[0] - v[4], v[1] - v[5], v[2] - v[4], v[3] - v[5] };
}
// In-circle: [a - p (x, y), b - p, c - p, then each one's squared length].
public static BigInteger[] Of(double[] x, double[] y, int a, int b, int c, int p)
{
var v = Scaled(new[] { x[a], y[a], x[b], y[b], x[c], y[c], x[p], y[p] });
BigInteger adx = v[0] - v[6], ady = v[1] - v[7], bdx = v[2] - v[6], bdy = v[3] - v[7], cdx = v[4] - v[6], cdy = v[5] - v[7];
return new[] { adx, ady, bdx, bdy, cdx, cdy, adx * adx + ady * ady, bdx * bdx + bdy * bdy, cdx * cdx + cdy * cdy };
}
private static BigInteger[] Scaled(double[] values)
{
var mantissa = new long[values.Length];
var exponent = new int[values.Length];
var lowest = int.MaxValue;
for (var i = 0; i < values.Length; i++)
{
var bits = BitConverter.DoubleToInt64Bits(values[i]);
var biased = (int) ((bits >> 52) & 0x7FF);
var fraction = bits & 0xFFFFFFFFFFFFFL;
mantissa[i] = biased == 0 ? fraction : fraction | (1L << 52);
exponent[i] = biased == 0 ? -1074 : biased - 1075;
if (bits < 0) mantissa[i] = -mantissa[i];
if (mantissa[i] != 0) lowest = Math.Min(lowest, exponent[i]);
}
var scaled = new BigInteger[values.Length];
for (var i = 0; i < values.Length; i++)
scaled[i] = mantissa[i] == 0 ? BigInteger.Zero : new BigInteger(mantissa[i]) << (exponent[i] - lowest);
return scaled;
}
}
}

View file

@ -451,6 +451,21 @@ public class Galaxy
}
}
// A zone no link reaches would throw far from here, placing homes or the entrance; refuse it where it is made.
public static void RequireEveryZoneLinked(GalaxyZone[] zones, IEnumerable<(GalaxyZone, GalaxyZone)> links)
{
if (zones.Length < 2) return;
var linked = new HashSet<GalaxyZone>();
foreach (var link in links)
{
linked.Add(link.Item1);
linked.Add(link.Item2);
}
var stranded = zones.Where(zone => !linked.Contains(zone)).ToList();
if (stranded.Count > 0)
throw new InvalidOperationException($"Triangulation left {stranded.Count} of {zones.Length} zones unlinked, first at {stranded[0].Position}");
}
private void GenerateLinks(float linkDensity, Action<string> progressCallback = null)
{
progressCallback?.Invoke("Triangulating Zone Positions");
@ -460,6 +475,7 @@ public class Galaxy
var links = Delaunay.Edges(Zones.Select(z => z.Position).ToArray())
.Select(edge => (Zones[edge.a], Zones[edge.b]))
.ToList();
RequireEveryZoneLinked(Zones, links);
progressCallback?.Invoke("Eliminating Zone Links");
if (progressCallback != null) Thread.Sleep(500); // Inserting Delay to make it seem like it's doing more work lmao