Fix mc interpolate canonical#94
Open
snowbldr wants to merge 2 commits into
Open
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
p1andp2reversed.Algebraically the two interpolations are equal:
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 indifferent 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.0for a 6×2×2 box). The two cubes emit triangles whose sharededge 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 atc=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 thetop of
mcInterpolate: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.gopins a sweep across both renderers:c ∈ {50, 75, 100, 125, 150, 200}c ∈ {50, 100, 125, 150}on
Box3D({6, 2, 2}, round=0.3)— the configuration that triggered the4320-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.