fix: hit a wall lying along a sight ray so it casts its line's shadow (#26)

visibilityPolygon casts one ray per 64-gon vertex and ang, ang+-RayEps per wall
endpoint. A wall radial to the viewer is collinear with the ray that crosses it,
and rayHit rejected the collinear case; its own +-RayEps rays meet the wall's
line at the viewer (t = 0, rejected by t > Eps). So the wall got no ray, no
vertex and no shadow, and a token on the line beyond it stayed inside the
polygon: a player-view fog leak (DESIGN 3.1).

Fix: rayHit returns the nearest point of the overlap when the wall lies on the
ray's line (and Inf only for a parallel wall on another line). {.inline.} keeps
the fan's inner loop at its previous cost (CPU-time cross-check on 20k dense
polygons: 25.8s before, 32.5s without the pragma, 24.9s with it).

Proof: tgeometry visibility tests (horizontal k=0, vertical k=16, both wall
directions, a 32 px grid translation, 10 degrees, plus the non-radial and
0.5 px-off negative controls); the leak world gains a wall on the subject's own
column with a hidden token beyond it, and leakcheck's HIDDEN_TOKEN class now
asserts the token-level over-approximation (token centre + viewer->token
segment, 5.4 tolerance), which the GEOMETRY class structurally cannot see.
This commit is contained in:
2026-11-20 22:17:45 +01:00
parent a11782f00d
commit 414bb0326c
7 changed files with 169 additions and 6 deletions
+49
View File
@@ -188,6 +188,55 @@ suite "visibility":
test "radius clips":
check not sees(@[], (0.0, 0.0), (300.0, 0.0), 200)
test "wall lying along a 64-gon ray: no leak past its far end (#26)":
# A wall radial to the viewer (the line through the viewer) is collinear with the ray that
# crosses it, and `rayHit` used to reject the collinear case, while its own +-RayEps rays meet
# the wall's line at the viewer (t = 0). So no ray hit it, it cast no shadow, and a token on the
# line beyond it stayed `inside` the polygon: a player-view fog leak (DESIGN 3.1, 5.1).
# A zero-width wall hides exactly its line's continuation, so the leak was that line only.
# Both wall directions, and a 1 px-off control that light really does reach around the far end.
for w in [@[W(300, 200, 500, 200)], @[W(500, 200, 300, 200)]]:
let poly = visibilityPolygon((200.0, 200.0), w, 600) # k = 0 ray from the viewer
check poly.hasVertex((300.0, 200.0)) # the wall contributes its near end
check not poly.inside((560.0, 200.0)) # hidden on the line beyond it
check poly.inside((250.0, 200.0)) # in front of the wall: seen
check poly.inside((560.0, 201.0)) # 1 px off the line: seen round the end
test "radial wall: the same column (k = 16) and a grid-snapped translation (#26)":
# the k = 16 ray is straight up; a vertical wall on the viewer's column is the same case
for w in [@[W(200, 300, 200, 500)], @[W(200, 500, 200, 300)]]:
let poly = visibilityPolygon((200.0, 200.0), w, 600)
check poly.hasVertex((200.0, 300.0))
check not poly.inside((200.0, 560.0))
# translated onto 32 px grid lines (the maps snap there; exact collinearity is routine)
let hrow = @[W(448, 256, 608, 256)]
check not visibilityPolygon((320.0, 256.0), hrow, 600).inside((704.0, 256.0))
check visibilityPolygon((320.0, 256.0), hrow, 600).inside((704.0, 257.0))
let vcol = @[W(320, 448, 320, 608)]
check not visibilityPolygon((320.0, 256.0), vcol, 600).inside((320.0, 704.0))
test "radial wall at an arbitrary angle: the 64-gon alignment is not the condition (#26)":
# Reported as a 64-gon ray, but any wall radial to the viewer behaves the same way: its own
# +-RayEps rays land on t = 0 too. Verified at 10 degrees.
let a = 10.0 * PI / 180
let ux = cos a
let uy = sin a
let w = @[Wall(a: (200.0 + 100 * ux, 200.0 + 100 * uy),
b: (200.0 + 300 * ux, 200.0 + 300 * uy))]
let poly = visibilityPolygon((200.0, 200.0), w, 600)
check poly.hasVertex((200.0 + 100 * ux, 200.0 + 100 * uy))
check not poly.inside((200.0 + 360 * ux, 200.0 + 360 * uy))
test "negative controls: a non-radial wall with an endpoint on a ray still blocks (#26)":
# (500,200) is exactly on the k = 0 ray from (200,200) but the wall runs north from it, so the
# per-endpoint fan does cover it: the fix must not trade the leak for an under-approximation.
let w = @[W(500, 200, 500, 400)]
check not sees(w, (200.0, 200.0), (560.0, 300.0))
check sees(w, (200.0, 200.0), (560.0, 150.0))
# a wall 0.5 px off the viewer's row: not radial, so nothing is behind it on the line
let off = @[W(300, 200.5, 500, 200.5)]
check sees(off, (200.0, 200.0), (560.0, 200.0))
suite "cells":
test "square polygon":
let sq = [(10.0, 10.0), (50.0, 10.0), (50.0, 50.0), (10.0, 50.0)]