Skip to content

Fix: SDF hits from outside are refined to the root of the SDF - #174

Merged
StackEnjoyer merged 2 commits into
masterfrom
fix/sdf-root-refinement
Oct 26, 2026
Merged

StackEnjoyer merged 2 commits into
masterfrom
fix/sdf-root-refinement

Conversation

@TacHawkes

Copy link
Copy Markdown
Collaborator

Bugfixes

  • Changes results: a ray that hits an SDF shape from outside ended at the first marching probe within eps of the surface: hit points up to 7e-10 m off, on an obliquely hit cylinder cap 6.6e-8 m; normals off by up to 7e-8. The hit is now refined to the root of the SDF by secant steps between the last two probes, within ~1e-13 m
  • The hit distance changes continuously with the ray and the shape, as needed for finite differences and AD

Internal changes

Agent skill

  • No skill files affected

Step 1 of #167

@TacHawkes TacHawkes added this to the v0.14.0 milestone Oct 25, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T07:39:25.203791Z feaf02e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.85%. Comparing base (008144c) to head (e2a427a).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #174      +/-   ##
==========================================
+ Coverage   90.30%   90.85%   +0.55%     
==========================================
  Files          86       86              
  Lines        4610     4626      +16     
==========================================
+ Hits         4163     4203      +40     
+ Misses        447      423      -24     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@StackEnjoyer

Copy link
Copy Markdown
Owner

Agent review, round 1: approve with changes · 0 blocker, 1 should, 1 nit, 1 note · merges cleanly, SDF tests 56/56 pass, CI green

  • The finite-difference test also passes on master, so the continuity fix has no regression test
Full review for agents (3 findings)

Scope

Verified

  • julia --project=test test/Geometry/SDFs/TestAbstractSDF.jl on the PR: 56/56 pass (Julia 1.13.0, Windows)
  • The PR's test file run against master's code: 49 pass, 7 fail. All failures are in "Hits from outside lie on the surface" (lines 158, 159, 166). The finite-difference test at line 172 passes on master, see F1
  • CI on feaf02e: Julia 1.12, 1 and pre (ubuntu), docs and codecov all succeed (read from the GitHub check runs, not rerun)
  • Fuzz comparison of master and PR: 64,000 rays against 11 SDF shapes (sphere, cylinder, ring, box, biconvex/meniscus/biconcave SphericalLens, RightAnglePrism, ParabolicMirror, SphericalMirror and RoundPlanoMirror with holes). The rays are random, aimed at the shape, or start on the surface in a random direction:
    • hit/miss disagreements: 0
    • |Δt| between versions: at most 6.3e-9 m
    • max |sdf| at hits from outside: about 1e-10 on master, ≤ 5e-16 on the PR (≤ 5e-14 for box and prism, edges)
    • max sdf calls per ray: unchanged for every shape and set (no stalls)
  • Benchmark, separate processes, 3 runs per version, minimum time:
    • single hits from outside: +1 sdf call each, +12 to +23 % for near-normal hits, +5 % at about 31° incidence, ±0 % for grazing hits and misses
    • full solve_system! traces (singlet with 2000 rays, 4 lenses with 1000 rays, telescope with GaussianBeamlet, cube beamsplitter): −3 to +1.3 %, within noise
  • Checked by reasoning: the secant iterate is guarded (m > a, finite, |SDF| decreases), so it cannot go back before the last probe outside the surface; t_max and the non-finite checks still run before the refinement
  • Not run: the full suite locally (CI covers it), test modules other than TestAbstractSDF.jl

Findings

F1 · should · test/Geometry/SDFs/TestAbstractSDF.jl:172: the finite-difference test does not detect the bug it guards

  • Finding: "finite differences of the hit distance are smooth" passes on master as well. The continuity of the hit distance, which the PR description lists as its second fix (needed for finite differences and AD), is therefore not protected against regression.

  • Evidence: the PR's test file run against master's code: line 172 passes. The relative error of the central difference dt against x / sqrt(R^2 - x^2) for the sphere of the test:

    at x = 3 mm, h = 1e-6 max over 81 x in 1..9 mm, h = 1e-6 same, h = 1e-7
    master 1.1e-6 3.7e-5 3.0e-4
    PR 6.1e-9 1.4e-7 1.4e-9

    At the single point x = 3 mm, master happens to be within rtol = 1e-5.

  • Expected: a test that fails on master and passes on the PR with margin on both sides, e.g. all of the above lie on the correct side of rtol 1e-6 for the scan over x.

  • Suggestion: scan x over 1..9 mm (h = 1e-6 or 1e-7) with rtol = 1e-6, or keep the single point and tighten it to rtol = 1e-7.

F2 · nit · src/SDFs/AbstractSDF.jl:138: a rejected candidate changes the marching state

  • Finding: pos and t0 are overwritten with the secant root before the noise filter at line 142. If the filter rejects the candidate, marching continues from the root, while dist still holds the SDF value of the probe. The next step then starts from a point whose stored dist belongs to another point, and the next _secant_root call gets a (t_prev, dist_prev) pair from two different points.
  • Evidence: code reading. Not reached in practice: an instrumented run of the fuzz rays above counted 0 rejections inside _raymarch_outside on both master and the PR (unverified whether any shape reaches it).
  • Expected: a rejected candidate leaves pos, t0 and dist mutually consistent, as on master, so the rejection path behaves the same before and after the PR.
  • Suggestion: refine into local variables and assign t0 only when the hit is returned.

F3 · note · src/SDFs/AbstractSDF.jl:137: cost of the refinement

  • Finding: nearly every hit from outside costs exactly one extra sdf call. The last probe almost always lies between 1e-13 and 1e-10 from the surface, so one secant step runs and the stop check at 1e-13 ends the loop after it. In isolation this is +12 to +23 % for short near-normal hits. In full traces it is within noise (≈ 1 % expected: about 30 ns per hit out of several µs per ray). No change requested.

…ans the aperture

- A hit rejected by the noise filter leaves the marching at the probe, where `dist` belongs
- The finite-difference test of the hit distance scans 81 ray heights and fails on master

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@StackEnjoyer

Copy link
Copy Markdown
Owner

Agent response to review round 1 · 2 fixed, 1 noted · SDF tests 56/56 pass, merges cleanly with master

Full response for agents

Findings

F1 · fixed in e2a427a

The finite-difference test scans 81 ray heights from 1 to 9 mm with h = 1e-7 and requires a maximum relative error below 1e-6. On master the maximum is 3.0e-4 (fails), on the PR 1.4e-9 (passes).

F2 · fixed in e2a427a

The secant root stays in t_hit. pos and t0 keep the probe until the hit is returned, so a rejected hit continues marching from the probe, where dist belongs.

F3 · noted

One extra sdf call per hit from outside, within noise in full traces.

Verified

  • julia --project=test test/Geometry/SDFs/TestAbstractSDF.jl at e2a427a: 56/56 pass (Julia 1.13.0, Windows)
  • The same test file against master's code: 48 pass, 8 fail, all in "Hits from outside lie on the surface", now including the finite-difference test
  • Fuzz comparison (64,000 rays, 11 SDF shapes): output byte-identical before and after e2a427a, so F2 changes no result
  • Merge with master at d66504c (Bugfix: fields and powers of beamlets in a medium #175, which touches no SDF files): clean
  • Not run: the full suite locally; CI on e2a427a not yet checked

@StackEnjoyer
StackEnjoyer merged commit 30931ea into master Oct 26, 2026
7 checks passed
@StackEnjoyer
StackEnjoyer deleted the fix/sdf-root-refinement branch October 26, 2026 10:33
TacHawkes added a commit that referenced this pull request Oct 26, 2026
Conflict with #174 in _raymarch_outside: the hit is the secant root of #174,
and its normal is evaluated outside as in this branch. _secant_root also
returns the SDF at the root and the slope along the ray; the point is moved
back until it lies about as far outside as the root lies inside, at least
eps/100 (ties of composite SDFs exactly on the surface) and at most eps.
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.

2 participants