perf: test only the in-sector 64-gon chord per vision ray (#64)
Each fan ray now tests the one circle-segment chord of its angular sector instead of all 64. A sector-boundary ray tests both meeting chords, so the rayHit set and the picked minimum are unchanged: the polygon is bit-identical to the all-segments fan. tests/tgeometry.nim keeps the old fan as an oracle and asserts exact float equality over the prototype corpus, the seed scene (all switch shapes), the 5k sparse/dense scenegen layouts, the #26 collinear/radial cases and random walls. tools/mutation.sh gains a2 (a ray tests the next sector's chord), which fails 5 of those tests. Bench on this host, before -> after (3-run median): 1 poly, prototype 0.028 -> 0.008 ms (3.5x) 1 poly, 5k sparse 0.062 -> 0.033 ms (1.9x) 1 poly, 5k dense 1.085 -> 0.708 ms (1.5x) Refs #64.
This commit is contained in:
+26
-4
@@ -15,7 +15,8 @@
|
||||
## - cellsTouched keeps a prototype quirk on purpose: a cell touching the polygon only along its bbox minimum x / y line
|
||||
## is not marked (every other touch is). The regression corpus has such polygons (walls on x = 256).
|
||||
## - vision is O(rays x walls in bbox): bbox cull, no spatial grid (H3: not built, 5k bench met target; results/bench.log).
|
||||
## A dense 5k layout (4096 px world, ~200 walls per vision box) costs ~1.3 ms per polygon.
|
||||
## Each ray tests only its own 64-gon chord (#64), not all 64; the polygon is bit-identical to the
|
||||
## all-segments fan. A dense 5k layout (4096 px world, ~200 walls per vision box) costs ~0.7 ms per polygon.
|
||||
## - circle range limit is an inscribed 64-gon: error <= ~0.12% of R.
|
||||
import std/[math, algorithm]
|
||||
|
||||
@@ -43,8 +44,9 @@ type
|
||||
movementBarrier*: bool = true
|
||||
|
||||
const
|
||||
Eps* = 1e-9 # geometric tolerance (px)
|
||||
RayEps = 1e-4 # angular nudge past endpoints (rad)
|
||||
Eps* = 1e-9 # geometric tolerance (px)
|
||||
RayEps = 1e-4 # angular nudge past endpoints (rad)
|
||||
SectorEps = 1e-9 # sector-fraction tolerance for "the ray is on a 64-gon vertex" (#64)
|
||||
|
||||
proc blocks*(w: Wall): bool =
|
||||
## The movement rule: a wall blocks movement unless it is an open door or its switch is off.
|
||||
@@ -118,10 +120,30 @@ proc visibilityPolygon*(o: Vec, walls: openArray[Wall], radius: float,
|
||||
angles.add ang - RayEps
|
||||
angles.add ang + RayEps
|
||||
angles.sort
|
||||
# Sector-limited fan (#64): a ray crosses only the chord of its own 64-gon sector, so test that
|
||||
# chord instead of all of them. The min is still taken over exactly the segs the old fan could
|
||||
# hit: at a sector boundary both meeting chords are tested (they share the vertex), and wall segs
|
||||
# are tested as before. `d` is built from the stored angle so every `rayHit` value, and therefore
|
||||
# every polygon vertex, is bit-identical to the all-segments fan (including the #26 radial case).
|
||||
let step = 2 * PI / circleSegs.float
|
||||
for ang in angles:
|
||||
let d: Vec = (cos ang, sin ang)
|
||||
var best = Inf
|
||||
for s in segs: best = min(best, rayHit(o, d, s[0], s[1]))
|
||||
var pos = ang
|
||||
while pos < 0: pos += 2 * PI
|
||||
while pos >= 2 * PI: pos -= 2 * PI
|
||||
let rpos = pos / step
|
||||
var sector = int(rpos)
|
||||
if sector >= circleSegs: sector = circleSegs - 1
|
||||
best = min(best, rayHit(o, d, segs[sector][0], segs[sector][1]))
|
||||
let frac = rpos - sector.float
|
||||
if frac < SectorEps:
|
||||
let prev = (sector + circleSegs - 1) mod circleSegs
|
||||
best = min(best, rayHit(o, d, segs[prev][0], segs[prev][1]))
|
||||
elif frac > 1 - SectorEps:
|
||||
let next = (sector + 1) mod circleSegs
|
||||
best = min(best, rayHit(o, d, segs[next][0], segs[next][1]))
|
||||
for j in circleSegs ..< segs.len: best = min(best, rayHit(o, d, segs[j][0], segs[j][1]))
|
||||
if best < Inf: result.add((o.x + d.x * best, o.y + d.y * best))
|
||||
|
||||
proc segsCross*(p, q, a, b: Vec): bool =
|
||||
|
||||
Reference in New Issue
Block a user