Skip to content

Ray crossing: each edge test of a triangle widened by its own edge's length - #371

Open
rmrsk wants to merge 3 commits into
mainfrom
thin-triangle-crossing
Open

rmrsk wants to merge 3 commits into
mainfrom
thin-triangle-crossing

Conversation

@rmrsk

@rmrsk rmrsk commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Background

TriangleRay::crossing() widens its three barycentric tests so that a ray through an edge two triangles share cannot slip between them. It used one slack for all three tests, bounding every edge's length by |e1| + |e2|. Across the short edge of a long, thin triangle, the widening then grows with the aspect ratio. On Crytek Sponza (float), rays 0.08–0.09 units outside a sliver 251 long and 6 wide reported hits in front of the true one (#368).

Closes #368

Solution

Each test now carries the L1 length of the edge it measures from: e2 for the u test, e1 for the v test, and e2 - e1 for the 1 - u - v test. The polygon test in FaceT::rayCrossing() already works this way. The widening stays a few ulps of the coordinates, whatever the triangle's shape. A shared edge is still widened by at least that rounding error from each side, so no ray slips through. It is still division- and square-root-free, at a few adds per test.

A new test in TestRayMesh.cpp uses a sliver like Sponza's, at coordinates near 1000. A second face beside it widens the BVH leaf's box, as neighboring faces do in a real mesh; without it, the box test rejects the rays before the triangle test runs. Rays 20 times the rounding error outside the short edge must miss, and 4 times it outside a long edge. Without the fix, the short-edge checks fail in both precisions.

Side-effects

A ray passing just outside a thin triangle's short edge no longer counts as crossing it. That is the documented behavior (RayTracing.rst: "within rounding error"). The hit for these rays can change, now correctly, in TriMeshSDF ray queries and in tapes holding one.

Alternative solutions

  • An L2 edge length, or an exact 2 * area: tighter by at most a factor of 3, but needs square roots.
  • Keeping one slack but bounding it by the shortest edge: too narrow across the long edges, so rays could slip through shared edges.

Performance

Receipt: Performance/receipts/thin-triangle-crossing-187f93ec.md (814aa6d against 0a4fbcc). Both sections are yellow. The maintainer accepted them: "Accept as cost of correctness fix".

  • Host: no regression in 120 cases (1 faster). Four float frame checksums changed (MeshCSG/union-tape/frame, MeshTape/{mesh,tape,view}/frame, Render/mesh/frame). That is expected: rays just outside a thin triangle's short edge now miss it, so a few pixels hit something else. Double checksums are unchanged.
  • CUDA (RTX A4000): 26 minor regressions in 212 cases, none MAJOR.
    • 24 are double-precision mesh-ray cases, 4–6% slower (Ray/mesh, RayCrossing, MeshTape, MeshCSG, Render/mesh, SurfaceParts). Each triangle test now does a few more double operations, and the A4000 runs double at 1/32 of its float rate.
    • 2 are float, the larger Ray/mesh/intersects-random/float at +4.1%.
    • One device frame checksum changed (MeshCSG/union-tape/frame/float), for the same reason as on the host.
  • Kernels: the float mesh frame kernels spill 4 more bytes, and two small float ray kernels gained 1–2 registers. The plain float tape kernels are unchanged (tape-change-check).

Where to look

  • Source/EBGeometry_TriangleRay.hpp, the slack computation. This kernel serves every TriMeshSDF ray query, and its SIMD-pack form serves the TriangleSoA groups.
  • Verified:
    • Unit tests: debug, debug-san and release-test pass in both precisions (-DEBGEOMETRY_TEST_BOTH_PRECISIONS=ON), and the new test fails without the fix.
    • Device tests: the CUDA [gpu] tests of TestRayMeshDevice and TestTriangleSoA pass on an RTX A4000 (sm_86).
    • tape-change-check: the float tape kernels (signedDistance() once and 16 times, firstHit()) and a TriMeshSDF firstHit() kernel have the same registers, stack and spills as the base. The host Tape and ClosestPrimitive probes' out-of-line tape functions are unchanged.
  • Not verified: no HIP run; Sponza itself was not rerun (the test reproduces its geometry).

Reviewer checklist (to be completed by a human)

  • The test suite compiles and runs to completion without warnings or errors.
  • All relevant new features are documented in the user documentation (Sphinx).
  • This contribution does not break existing sections in the user documentation.
  • All relevant APIs are documented in the doxygen documentation.
  • Appropriate labels have been assigned to this PR.
  • New or revised proper licensing and copyright information is in place.
  • A PR review has been run using @claude review.
  • The continuous integration and testing hooks at GitHub run to completion.
  • The performance receipt is current (CI's Performance-receipt passes), and every regression in it is explained.

🤖 Generated with Claude Code

https://claude.ai/code/session_012mR37bzMSJWYPpFgLhy9sk

…length

TriangleRay::crossing() widened its three barycentric tests by one slack that bounded every edge by
|e1| + |e2|. Across a short edge that widening grows with the triangle's aspect ratio: on a sliver 251 long
and 6 wide at coordinates near 1000, rays 0.08 units outside the short edge crossed it (float). Each test
now carries the L1 length of the edge it measures from (u: e2, v: e1, 1 - u - v: e2 - e1), as the polygon
test in FaceT::rayCrossing() already does, so the widening stays a few ulps of the coordinates whatever the
triangle's shape. A shared edge is still widened by at least that rounding error from each side.

The new test fails without the change (both precisions). The float tape kernels (signedDistance once and
16 times, firstHit) and a TriMeshSDF firstHit kernel compile to the same registers, stack and spills as the
base, and the host Tape and ClosestPrimitive probes' out-of-line tape functions are unchanged.

Closes #368

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mR37bzMSJWYPpFgLhy9sk
@rmrsk rmrsk added bug Something isn't working triangles Affects triangle functionality ray tracing Affects ray tracing functionality labels Nov 22, 2026
@rmrsk

rmrsk commented Nov 22, 2026

Copy link
Copy Markdown
Owner Author

@claude review

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Claude finished @rmrsk's task in 28s —— View job


Review of #371: per-edge slack in TriangleRay::crossing()

I read the diff only. I did not build or run anything.

Verdict: no blocking findings. The fix is correct and the test is well targeted. There are two small things to settle before promotion.

  • Read the diff against origin/main
  • Checked the edge-to-barycentric mapping
  • Checked the test's margins
  • Checked the PR against the CLAUDE.md rules

Correctness

  • Edge mapping: with P = v0 + u·e1 + v·e2, the three tests map to these edges:

    • u = 0 is the edge along e2, so slackU uses |e2|₁.
    • v = 0 is the edge along e1, so slackV uses |e1|₁.
    • 1-u-v = 0 is the edge v1→v2, which is e2 - e1, so slackW uses |e3|₁.

    This matches the polygon test in FaceT::rayCrossing().

  • Shared edges: both triangles that share an edge widen it by at least the rounding error of that edge's own length, so no ray slips through. The widening is never narrower than the old per-triangle bound for the edge it measures.

  • Zero det: det == 0 still yields NaN or infinite u, v and t, and the abs(det) > 0 term still rejects it.

  • Device and SIMD safety: the change uses only abs, - and + on V. It adds no std:: calls, so the device-math contract holds. Vec3V<V> subtraction is already used for e1 and e2.

  • Cost: it adds one vector subtraction, two extra abs-sums and two adds per test. That is small, and the PR reports the tape kernels unchanged.

Test (Tests/TestRayMesh.cpp)

  • Margins: the test has comfortable margins.
    • Outside the short edge, the widened tolerance is about δ, and the test uses 20δ. Without the fix it is about δ·(251+6)/6 ≈ 43δ, so the old code is wrong at 20δ. That agrees with the PR's claim that the test fails without the fix.
    • Outside the long edge, the tolerance is about δ, and the test uses 4δ.
  • Second face: the second face that widens the leaf box is a good touch, and the comment explains why it is there.
  • Style: it uses TEMPLATE_TEST_CASE with EBGEOMETRY_TEST_PRECISIONS, and the reference is an independent geometric argument.

Nits

  1. Possible clang-format failure. The declaration block in EBGeometry_TriangleRay.hpp (about lines 100-110) looks misaligned:

    • const V delta = has stray padding.
    • const Vec3V<V> e3 = is not aligned with base/slackU.
    • const auto inside = keeps a double space that may no longer line up.

    Please run clang-format -i or the pre-commit hook. The default-stage formatting hook would otherwise fail CI.

  2. Comment wording. "at most a factor 3 together" is a bit ambiguous. Something like "each bound overestimates its edge's length by at most a factor of 3 (L1 vs. L2 norm), so the widening stays within a small factor of the rounding error" would be clearer. This is minor.

Process, per CLAUDE.md

  • Performance section. The PR body's Performance section is empty. The receipt rules ask for the full comparison and receipt, recorded after the @claude review rounds converge and before promotion. This changes Source/, so a receipt is needed. The TriMeshSDF ray probes are the ones it can affect.
  • Docs. RayTracing.rst already says "within rounding error", so I see no docs change needed. I did not run the docs-audit rebuild.
  • Labels. bug, triangles and ray tracing fit. I would not add gpu, since only the device tests were run.

Overall this is a sound, minimal fix. Once the formatting is checked and the receipt recorded, it looks ready to hand over.
· branch thin-triangle-crossing

@rmrsk

rmrsk commented Nov 22, 2026

Copy link
Copy Markdown
Owner Author

@claude review

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Claude finished @rmrsk's task in 26s —— View job


Review of #371: per-edge slack in TriangleRay::crossing() (head 814aa6d)

I read the diff against origin/main and ran clang-format on the header. I did not build or run the tests.

Verdict: no blocking findings.

Correctness

  • Edge mapping: with P = v0 + u·e1 + v·e2, u = 0 is the edge along e2, v = 0 the edge along e1, and 1 - u - v = 0 the edge along e2 - e1. slackU, slackV and slackW use those lengths, so each test is widened by its own edge's rounding error.
  • Shared edges: each triangle widens a shared edge by at least the rounding error of that edge's length, so a ray cannot slip between the two.
  • Degenerate case: det == 0 still gives NaN or infinite u, v and t, and the abs(det) > 0 term still rejects it.
  • Device and SIMD: the change adds only -, abs and + on V and calls no std:: function, so the device-math contract holds.
  • Comment bound: the new text says the L1 length is at most √3 times the L2 length, and that the largest component of e1 × e2 is at least 1/√3 of its length. That makes the factor 3 at most, which is correct. The earlier wording nit is resolved by 814aa6d.

Test (Tests/TestRayMesh.cpp)

  • It uses TEMPLATE_TEST_CASE with EBGEOMETRY_TEST_PRECISIONS. The reference is a geometric argument, not the code run a second way.
  • The margins separate old and new behavior. The old widening across the short edge is about δ·(251+6)/6 ≈ 43δ, so rays at 20δ hit before the fix and miss after it. The long-edge check at 4δ still guards against under-widening.
  • The second face is needed so the leaf box does not reject the rays first, and the comment says so.

Retracting my earlier nit

My previous review suspected a clang-format failure on the declaration block (around lines 100-110). clang-format on the current header produces no diff, so formatting is fine.

Process, per CLAUDE.md

  • Receipt: the Performance section is still empty and there is no Performance/receipts/ file. This PR changes Source/, so the full comparison (Scripts/run-performance.sh compare origin/main --record) must be recorded before promotion. The TriMeshSDF and TriangleSoA ray probes are the ones it can affect. It needs a quiet machine and the maintainer's go. Pushing the receipt also needs the maintainer's go-ahead.
  • Docs: RayTracing.rst already says "within rounding error", so I found no docs change needed. I did not run the docs-audit Sphinx rebuild.
  • Labels: bug, triangles and ray tracing fit.

Once the receipt is recorded, this looks ready for the hand-over steps.
· branch thin-triangle-crossing

…e correctness fix)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mR37bzMSJWYPpFgLhy9sk
@rmrsk
rmrsk marked this pull request as ready for review November 22, 2026 21:52

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ray tracing Affects ray tracing functionality triangles Affects triangle functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ray crossing: the widened triangle test reaches far outside long, thin triangles

1 participant