Repository navigation
SDF ray marching: rays inside leave at the first exit - #144
Conversation
`_raymarch_inside` stepped a fixed 1 m along the ray and then marched back from outside. It thus found the last exit within that metre, not the first. Rays from a surface into an object (as after refraction) compared with a reference from the sign of the SDF, 200 per shape for 58 shapes: 216 of 11600 left at a wrong point, by up to 33 mm, e.g. in meniscus lenses, plano- and bi-concave lenses, concave and acylindrical surfaces, rings, mirrors with holes and differences of SDFs; some found no exit at all. In addition, `intersect3d` took a ray that starts below the surface for a ray outside if the SDF increases along it, e.g. from the middle of the wall of a ring towards its inner face. The ray is now marched inside by the distance `-sdf`, which does not pass the exit for an SDF that is exact or a lower bound inside: - a minimum step leaves the starting surface and the internal faces of composite SDFs, where the SDF is about 0 within the object; a point with sdf about 0 is an exit only if the ray is outside just behind it - an over-relaxed step (Keinert et al., 2014) is kept only if the balls around both ends cover it - a step that ends outside brackets the exit, which is found by the Illinois method on the sign of the SDF; the returned point lies outside, at most `eps` behind the last point inside, such that the next ray from it does not start inside (Issue#11 would fail otherwise) - the normal is evaluated `eps` outside of the exit, as for hits from outside: inside a composite SDF the gradient may stem from another part, and exactly on the surface some SDFs have no gradient; a normal against the leaving ray (a part that touches the exit point from outside, Issue#82) is evaluated `h` outside instead - a ray below the surface counts as inside in any direction The same 58 shapes with 500 rays each: 0 of 29000 wrong in exit point, normal or side of the surface. New tests: a ring wall and a lens in `TestAbstractSDF.jl`, all shapes of `TestSDFBoundingSpheres.jl` against the sign reference. The preference `sdf_inside_step` is no longer used. Cost: an aspheric lens takes ~55 % longer (2000 rays 17.8 -> 27.8 ms, before the speed-up of the aspheric SDF in #137), spherical lenses ~7 %: leaving the starting surface needs more steps than the jump did. Fixes #142
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #144 +/- ##
==========================================
+ Coverage 89.22% 89.31% +0.09%
==========================================
Files 81 81
Lines 4500 4539 +39
==========================================
+ Hits 4015 4054 +39
Misses 485 485 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
`_bracketed_exit` returned the outer end of the final bracket, which moves in steps of the bracket width `eps` when a parameter of the setup changes. Finite differences saw this as noise: the derivative of a spot position w.r.t. a lens shift differed from ForwardDiff by 7e-6 relative (1e-11 on master), which matters for optimizers and tolerancing based on finite differences. The exit is now the secant root of the final bracket with the unscaled SDF values. It changes continuously, and lies within `eps` of the surface, far below the threshold `h` at which the next ray from it would count as starting inside. Finite differences and ForwardDiff agree to ~1e-11 again. Part of #142
|
This issue is fundamentally rooted in the original assumption that all SDFs implemented in BMO are sufficiently convex. This has been the case for most practical purposes since I introduced SDFs and my "interpretation" of ray-marchign, however, the assumption does not hold for more and more shapes as we push the capabilities of BMO. This PR is a stopgap measure that fixes the shortcomings of a narrowly designed system. I am in principle fine with this. However, we should consider if the entire ray-marching procedure should be overhauled in light of the major breaking 0.14 release. I think now would be a good time to fix this once and for all with a much more sophisticated, robust algoritm. |
|
Agreed on the diagnosis. The root problem is broader than convexity: sphere tracing is only safe for exact (or Lipschitz ≤ 1) distance fields, and many of ours are not. Union and difference via min/max are not distances inside the shape, and the conics and aspheres are only bounds. The inside march takes blind fixed steps of My proposal for a proper fix:
On timing: I'd merge this PR now. Its tests and the analytic references from #156 make a good regression base for the rewrite. The geometry backend is mostly internal, so the rewrite is only breaking if the interface for custom SDFs changes. For 0.14 I'd only reserve that hook, e.g. an optional |
Bugfixes
Performance
Deprecations
sdf_inside_step(get_sdf_inside_step) is no longer usedAgent skill
Closes #142