Isoline extraction fix batch: pin seam, endpoint attributes, ray depth gate; delete the dead edge swap

The smoke gains a shared-endpoint s-continuity check (every jump has a seam
side), per-endpoint depth and slope against the radial profiles, a cell box
check, a ray depth-window check, and s on the real zone with RhoMax sized
from the zone radius. The Crossing swap never ran (kEdge already runs low
index to high): deleted with gIdx, and the closure comment says why it holds.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-29 20:32:11 +01:00
commit cda612dca8
2 changed files with 133 additions and 9 deletions

View file

@ -48,6 +48,18 @@ namespace Aetheria.EditorTools
// Ray endpoint's angle off its level, rad: shipped worst 0.00187; least mutant (levels half a step off) 0.263, separation 140.
const float RayAngleBound = 0.0222f;
// Endpoint depth |depth / d(r) - 1| on the radial wells: shipped worst 0.00064; least mutant (depth ends swapped, offset well)
// 0.0373, separation 58. Slope |slope / |d'(r)| - 1|: shipped worst 0.0031; least mutant (slope not interpolated, offset well)
// 0.0580, separation 18.9 (slope ends swapped reads 0.074).
const float DepthBound = 0.0049f;
const float SlopeBound = 0.0133f;
// Share of the real zone's rays or contours carrying the rho-clamp bit at RhoMax = the zone's radius: shipped worst 3.3%;
// the same zone at RhoMax 500 (a fixture-sized value) reads 49.6%, separation 15.
const float ClampedBound = 0.13f;
// Share of unclamped real-zone rays whose s rises with depth: shipped 5.4%; the mutant (s negated) 94.6%, separation 17.5.
const float WrongWayBound = 0.226f;
static float _zoneRhoMax;
static bool _ok = true;
static string _out;
@ -145,6 +157,7 @@ namespace Aetheria.EditorTools
Guarded("layout", Layout);
Guarded("saddle", Saddle);
Guarded("along-line", AlongLine);
Guarded("ray window", RayWindow);
Guarded("overflow", Overflow);
EditorSceneManager.OpenScene("Assets/Scenes/ARPG.unity", OpenSceneMode.Single);
@ -267,6 +280,7 @@ namespace Aetheria.EditorTools
{
public string Name;
public Func<double, double> D; // the profile, d(r)
public Func<double, double> Slope; // |d'(r)|
}
// Radial wells on a 128 x 128 field at 2 u per texel: d = K / (r + r0) (a reference offset) and
@ -294,8 +308,8 @@ namespace Aetheria.EditorTools
static readonly Radial[] Profiles =
{
new Radial { Name = "offset", D = r => 2000.0 / (r + 40.0) },
new Radial { Name = "soft-core", D = r => 2000.0 / Math.Sqrt(r * r + 15.0 * 15.0) }
new Radial { Name = "offset", D = r => 2000.0 / (r + 40.0), Slope = r => 2000.0 / ((r + 40.0) * (r + 40.0)) },
new Radial { Name = "soft-core", D = r => 2000.0 / Math.Sqrt(r * r + 15.0 * 15.0), Slope = r => 2000.0 * r / Math.Pow(r * r + 15.0 * 15.0, 1.5) }
};
// The wells' radii the checks read: far enough from the centre to be off the core and from the field's edge to be off the
@ -364,6 +378,8 @@ namespace Aetheria.EditorTools
var holding = (uint)((int)texel.y * 128 + (int)texel.x);
if (rays.Any(s => s.cell == holding)) Fail("rays", $"{profile.Name}: a ray segment lies in the cell holding the well centre");
EndpointAttributes(profile, result.Segments, centre, origin, perTexel, 128);
Continuity("radial " + profile.Name, contours, perTexel);
Info($"ray segments per level, {profile.Name}: {string.Join(" ", Enumerable.Range(0, 12).Select(k => rays.Count(r => LevelOf(r.key) == k)))}");
Info($"along-line {profile.Name}: {contours.Length} contour / {rays.Length} ray segments; arc ratio error worst over {arcLevels} rings {arcWorst:G4} (mutants: raw theta {arcMutant:G4}, rho = d/|g| {arcMutantRho:G4}); ray rho error worst {rhoWorst:G4} at r={worstAt:F1} (mutant d/|g| worst {rhoMutant:G4}, {rayCount} endpoints); ray angle error worst {angleWorst:G4} rad (mutant half-step levels {angleMutant:G4})");
{
@ -376,6 +392,110 @@ namespace Aetheria.EditorTools
}
}
// ---- endpoint attributes, continuity, cell, ray window -----------------------------------------------------------------
// Slope and depth at each endpoint, against the profile's own d(r) and |d'(r)|. A ray's depth runs along its edge, so an
// end swap or a missing interpolation moves it by far more than the field's sampling error does. Bounds by the margin
// rule, readings from the shader mutants M21 (depth ends swapped), M22 (slope ends swapped), M15 (slope not interpolated).
static void EndpointAttributes(Radial profile, IsolineExtractor.IsoSegment[] segments, Vector2 centre, Vector2 origin, Vector2 perTexel, int width)
{
double depthWorst = 0, slopeWorst = 0;
foreach (var s in segments)
foreach (var (p, slope, depth) in new[] { (s.a, s.slopeA, s.depthA), (s.b, s.slopeB, s.depthB) })
{
if (!InBand(p - centre)) continue;
var r = (p - centre).magnitude;
depthWorst = Math.Max(depthWorst, Math.Abs(depth / profile.D(r) - 1.0));
slopeWorst = Math.Max(slopeWorst, Math.Abs(slope / profile.Slope(r) - 1.0));
}
Info($"endpoint attributes {profile.Name}: depth error worst {depthWorst:G4}, slope error worst {slopeWorst:G4}");
if (depthWorst > DepthBound) Fail("endpoint attributes", $"{profile.Name}: depth off its profile by {depthWorst:G4} > {DepthBound:G4}");
if (slopeWorst > SlopeBound) Fail("endpoint attributes", $"{profile.Name}: slope off its profile by {slopeWorst:G4} > {SlopeBound:G4}");
CellBoxes(profile.Name, segments, origin, perTexel, width);
}
// Each segment's cell holds both its endpoints: cell is y * width + x of the cell's first corner. A transposed index
// passes on a fixture whose cell sits on the diagonal, so the boxes are read on whole fields.
static void CellBoxes(string what, IsolineExtractor.IsoSegment[] segments, Vector2 origin, Vector2 perTexel, int width)
{
var outside = 0;
foreach (var s in segments)
{
var cx = (int)(s.cell % (uint)width);
var cy = (int)(s.cell / (uint)width);
foreach (var p in new[] { s.a, s.b })
{
var tx = (p.x - origin.x) / perTexel.x;
var ty = (p.y - origin.y) / perTexel.y;
if (tx < cx - 1e-3 || tx > cx + 1 + 1e-3 || ty < cy - 1e-3 || ty > cy + 1 + 1e-3) { outside++; break; }
}
}
if (outside != 0) Fail("cell", $"{what}: {outside} segments have an endpoint outside their cell");
}
// The seam bit says s jumps by a full turn between a straddling cell and its neighbour. At every pair of bitwise-equal
// contour endpoints where s jumps, one side must carry the bit; and the field must have jumps (else the check is vacuous).
static void Continuity(string what, IsolineExtractor.IsoSegment[] contours, Vector2 perTexel)
{
var jump = .5f * Math.Max(Math.Abs(perTexel.x), Math.Abs(perTexel.y));
var seen = new Dictionary<(int, int, int), (float s, bool seam)>();
int jumps = 0, unflagged = 0;
foreach (var seg in contours)
foreach (var (p, sv) in new[] { (seg.a, seg.sA), (seg.b, seg.sB) })
{
var key = (LevelOf(seg.key), BitConverter.SingleToInt32Bits(p.x), BitConverter.SingleToInt32Bits(p.y));
if (seen.TryGetValue(key, out var other))
{
if (Math.Abs(other.s - sv) > jump)
{
jumps++;
if (!other.seam && !Seam(seg.key)) unflagged++;
}
seen.Remove(key);
}
else seen[key] = (sv, Seam(seg.key));
}
Info($"continuity {what}: {jumps} jumps in s between shared endpoints, {unflagged} without a seam bit");
if (jumps == 0) Fail("seam", $"{what}: no jump in s anywhere; the check is vacuous");
if (unflagged != 0) Fail("seam", $"{what}: {unflagged} jumps in s have no seam-flagged side");
}
// s on the real zone: how much of it the rho clamp spoils, and whether it runs the way the flowing dashes need (a
// ray's s falls as it descends toward a well). The mutant (s negated) reads the complement.
static void RealZoneS(IsolineExtractor.IsoSegment[] segments)
{
var rays = segments.Where(s => IsRay(s.key)).ToArray();
var contours = segments.Where(s => !IsRay(s.key)).ToArray();
double rayClamped = rays.Count(s => Clamped(s.key)) / (double)rays.Length;
double contourClamped = contours.Count(s => Clamped(s.key)) / (double)contours.Length;
var live = rays.Where(s => !Clamped(s.key) && Math.Abs(s.depthB - s.depthA) > 1e-4f).ToArray();
var wrong = live.Count(s => (s.sB - s.sA) * (s.depthB - s.depthA) > 0) / (double)live.Length;
Info($"real zone s: rho clamped on {rayClamped:P1} of rays, {contourClamped:P1} of contours (RhoMax {_zoneRhoMax:G5}); {live.Length} unclamped rays, s rises with depth on {wrong:P1} (mutant s negated {1 - wrong:P1})");
if (rayClamped > ClampedBound || contourClamped > ClampedBound) Fail("real zone s", $"rho is clamped on {rayClamped:P1} of rays, {contourClamped:P1} of contours > {ClampedBound:P0}");
if (wrong > WrongWayBound) Fail("real zone s", $"s rises with depth on {wrong:P1} of unclamped rays > {WrongWayBound:P0}");
}
// Rays exist only where the cell reaches the contour window [Start, Start + Range] (IsolinesUI draws none outside it).
// Window r 50..90 of the offset well: no ray beyond a cell's reach of it, and rays in the middle of it.
static void RayWindow()
{
var profile = Profiles[0];
var (field, origin, perTexel, centre, _, _) = RadialField(profile);
try
{
var start = (float)profile.D(90);
var range = (float)(profile.D(50) - profile.D(90));
var result = Extract(field, origin, perTexel, new IsolineExtractor.IsolineLevels(start, range, RhoMax, SlopeEpsilon), 1 << 18);
var rays = result.Segments.Where(s => IsRay(s.key)).ToArray();
var outside = rays.Count(s => new[] { s.a, s.b }.Any(p => (p - centre).magnitude < 44f || (p - centre).magnitude > 96f));
var inside = rays.Count(s => (((s.a + s.b) * .5f) - centre).magnitude is > 60f and < 80f);
Info($"ray window: {rays.Length} rays, {outside} outside r 44..96, {inside} inside r 60..80");
if (outside != 0) Fail("ray depth gate", $"{outside} ray segments lie outside the contour window");
if (inside < 20) Fail("ray depth gate", $"only {inside} ray segments inside the contour window");
}
finally { field.Release(); UnityEngine.Object.DestroyImmediate(field); }
}
// ---- overflow ----------------------------------------------------------------------------------------------------
static void Overflow()
@ -480,7 +600,9 @@ namespace Aetheria.EditorTools
{
field = RenderField(camera, 256, 256, out var origin, out var perTexel);
var range = Math.Max(1f, orbits.Max(o => -zone.GetHeight(new float2(o.Position.x, o.Position.y))));
var levels = new IsolineExtractor.IsolineLevels(0f, range, RhoMax, SlopeEpsilon);
// RhoMax from the zone: its radius, the largest curvature radius a contour can have inside it.
_zoneRhoMax = zone.Pack.Radius;
var levels = new IsolineExtractor.IsolineLevels(0f, range, _zoneRhoMax, SlopeEpsilon);
var result = Extract(field, origin, perTexel, levels, 1 << 20);
var spacing = range / (Contours - 1);
var contours = result.Segments.Where(s => !IsRay(s.key)).ToArray();
@ -531,6 +653,9 @@ namespace Aetheria.EditorTools
if (!Border(p) && seen[(LevelOf(s.key), BitConverter.SingleToInt32Bits(p.x), BitConverter.SingleToInt32Bits(p.y))] != 2)
open++;
Info($"real zone: {open} contour endpoints off the border without exactly one partner");
Continuity("real zone", contours, perTexel);
CellBoxes("real zone", result.Segments, origin, perTexel, 256);
RealZoneS(result.Segments);
if (open != 0) Fail("contours close", $"{open} endpoints are not shared with exactly one other endpoint");
}
finally
@ -572,7 +697,7 @@ namespace Aetheria.EditorTools
File.WriteAllBytes(Path.Combine(_out, "before.png"), before.EncodeToPNG());
// segments.png: the extracted segments over the same frame, contours cyan and rays magenta.
var levels = new IsolineExtractor.IsolineLevels(0f, range, RhoMax, SlopeEpsilon);
var levels = new IsolineExtractor.IsolineLevels(0f, range, zone.Pack.Radius, SlopeEpsilon);
var result = Extract(field, origin, perTexel, levels, 1 << 18);
var raster = new Texture2D(CaptureSize, CaptureSize, TextureFormat.RGBA32, false);
raster.SetPixels32(Enumerable.Repeat(new Color32(0, 0, 0, 255), CaptureSize * CaptureSize).ToArray());

View file

@ -76,7 +76,6 @@ void Curve(uint3 id : SV_DispatchThreadID)
}
// One cell's corners, in the order (x, y), (x+1, y), (x+1, y+1), (x, y+1); edges 0..3 join corners 0-1, 1-2, 3-2, 0-3.
static uint gIdx[4];
static float2 gPos[4];
static float gVal[4]; // the scalar being contoured: d for contours, the cell-unwrapped theta for rays
static float gS[4];
@ -87,13 +86,14 @@ static const uint2 kEdge[4] = { uint2(0, 1), uint2(1, 2), uint2(3, 2), uint2(0,
// Edge pair (e0 | e1 << 4) of the one segment of each non-saddle case; 0xFF none or saddle.
static const uint kSegment[16] = { 0xFF, 0x03, 0x10, 0x13, 0x21, 0xFF, 0x20, 0x32, 0x32, 0x20, 0xFF, 0x21, 0x13, 0x10, 0x03, 0xFF };
// One crossing of `level` on an edge, a function of (edge, level) alone: the corners go in canonical order (lower linear
// texel index first), so both cells sharing the edge compute the same floats bit for bit.
// One crossing of `level` on an edge, a function of (edge, level) alone. kEdge runs every edge from its lower linear texel
// index to its higher (edges 0 and 2 left to right, 1 and 3 bottom to top), so both cells sharing an edge read the corners
// in the same order and compute the same floats bit for bit. Reordering kEdge breaks that; the closure check in
// IsolineExtractSmoke is the guard.
void Crossing(uint edge, float level, out float2 p, out float s, out float slope, out float depth)
{
uint a = kEdge[edge].x;
uint b = kEdge[edge].y;
if (gIdx[a] > gIdx[b]) { uint w = a; a = b; b = w; }
precise float t = saturate((level - gVal[a]) / (gVal[b] - gVal[a]));
precise float2 q = gPos[a] + (gPos[b] - gPos[a]) * t;
p = q;
@ -148,7 +148,6 @@ void March(uint3 id : SV_DispatchThreadID)
[unroll] for (uint i = 0; i < 4; i++)
{
int2 q = p0 + int2((i == 1 || i == 2) ? 1 : 0, i >= 2 ? 1 : 0);
gIdx[i] = (uint)(q.y * _Size.x + q.x);
gPos[i] = _Origin + float2(q) * _PerTexel;
float4 g = _GradIn.Load(int3(q, 0));
float2 c = _CurveIn.Load(int3(q, 0));