Skip to content

Fix mc interpolate canonical#94

Open
snowbldr wants to merge 2 commits into
deadsy:masterfrom
snowbldr:fix_mc_interpolate_canonical
Open

Fix mc interpolate canonical#94
snowbldr wants to merge 2 commits into
deadsy:masterfrom
snowbldr:fix_mc_interpolate_canonical

Conversation

@snowbldr
Copy link
Copy Markdown
Contributor

Fix mcInterpolate: canonicalize argument order to avoid quantization-induced boundary edges

Fixes one of the bugs in #85.

The bug

Two adjacent marching-cubes cells share a cube edge, but they number that
edge with swapped endpoints — e.g. cube A's MC edge 1 is cube B's MC
edge 3 on their shared +X/−X face, with p1 and p2 reversed.

Algebraically the two interpolations are equal:

p1 + t·(p2 − p1)   ==   p2 + t'·(p1 − p2)     where t' = 1 − t

but in floating-point they can differ by ~1 ULP — the multiplications and
additions don't commute bit-exactly. The downstream vertex quantization
in mesh.go (int32(c · 1e4)) then lands the two near-equal results in
different buckets whenever the crossing sits near a 1e-4-aligned
coordinate. That's the typical case on axis-aligned box faces (e.g.
Y = 1.0 for a 6×2×2 box). The two cubes emit triangles whose shared
edge is recorded as two distinct quantized edges, and the watertightness
check counts both as boundary edges instead of cancelling.

For Box3D({6, 2, 2}, round=0.3) rendered via the octree at c=100,
this produced 4320 boundary edges on a shape that should be perfectly
watertight.

The fix

Sort (p1, p2) and (v1, v2) into a fixed lexicographic order at the
top of mcInterpolate:

if p2.X < p1.X ||
   (p2.X == p1.X && p2.Y < p1.Y) ||
   (p2.X == p1.X && p2.Y == p1.Y && p2.Z < p1.Z) {
    p1, p2 = p2, p1
    v1, v2 = v2, v1
}

Both cubes now evaluate the identical floating-point expression on the
shared edge, so the quantizer buckets them together and the boundary
edge cancels. After: 0 boundary edges at every tested resolution.

Tests

render/mc_interpolate_test.go pins a sweep across both renderers:

  • Octree at c ∈ {50, 75, 100, 125, 150, 200}
  • Uniform at c ∈ {50, 100, 125, 150}

on Box3D({6, 2, 2}, round=0.3) — the configuration that triggered the
4320-boundary-edge case before the fix. All zero on macOS.

Architecture-specific note

Same family as the earlier #84 issue: a borderline floating-point
ordering that flips on FMA / FTZ rounding differences. The bug was
visible on x86_64 and likely worse there than Apple Silicon because
FMA fusion changes which of the two cube interpolations rounds where.
The canonicalization removes the ordering ambiguity entirely, so both
architectures are equivalent after the fix.

snowbldr added 2 commits May 8, 2026 20:15
…induced boundary edges

Two adjacent cubes sharing the same cube edge number its endpoints in
swapped order (e.g. cube A's edge 1 == cube B's edge 3 on their shared
+X/-X face, with p1/p2 reversed). Algebraically p1 + t·(p2 − p1) and
p2 + t'·(p1 − p2) are equal, but in floating-point they can differ by
~1 ULP. The downstream `int32(c · 1e4)` vertex quantization in mesh.go
lands the two near-equal results in different buckets whenever the
crossing sits near a 1e-4-aligned coordinate (typical on axis-aligned
box faces), producing phantom boundary edges in the watertightness
check.

Sorting the (p1, p2)/(v1, v2) pair into a fixed lexicographic order
before interpolating makes both cubes evaluate the identical
floating-point expression on the shared edge.

Before: Box3D({6, 2, 2}, round=0.3) rendered via octree at c=100 had
4320 boundary edges. After: 0 at every resolution tested (c ∈ {50, 75,
100, 125, 150, 200} octree, c ∈ {50, 100, 125, 150} uniform).

render/mc_interpolate_test.go pins the sweep so a regression surfaces
immediately.
11 axis-aligned box configurations (varied size, aspect, and
rounding) that place MC surface vertices at integer or half-integer
coordinates — the trap for the swap-ordered MC edge case. Each runs
through both renderers (octree and uniform) at 6 octree resolutions
and 4 uniform resolutions.

Plus regression coverage:
  - Non-axis-aligned shapes (sphere, cylinder, capsule) — ensure the
    canonicalization doesn't break the case the bug never affected.
  - Translated boxes — surface vertices land off the bbox-symmetric
    grid, exercising shared-edge swapping under translation.

All assert zero boundary edges.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant