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:
+14
-19
@@ -1,22 +1,17 @@
|
||||
/home/davide/Projects/GDR/FFF_vtt-chore/server/worldmap.nim(18, 44) Warning: imported and not used: 'faults' [UnusedImport]
|
||||
/home/davide/Projects/GDR/FFF_vtt-chore/server/vision.nim(31, 43) Warning: imported and not used: 'faults' [UnusedImport]
|
||||
== prototype scene (60 walls, 8 tokens, radius 400, cell 32) ==
|
||||
1 poly (jittered start positions): n=2000 avg=0.030ms worst=0.546ms
|
||||
6 polys (one 6-token party): n=500 avg=0.180ms worst=2.611ms
|
||||
1 poly + cellsTouched (scanline): n=2000 avg=0.039ms worst=0.553ms
|
||||
6 polys + cellsTouched (scanline): n=300 avg=0.214ms worst=0.908ms
|
||||
1 poly + cellsTouchedOld (prototype reference): n=500 avg=0.537ms worst=1.708ms
|
||||
TARGET polygon+cells, prototype scene (prototype baseline 0.451): 0.039 ms vs <= 0.451 ms: PASS
|
||||
1 poly (jittered start positions): n=2000 avg=0.008ms worst=0.042ms
|
||||
6 polys (one 6-token party): n=500 avg=0.049ms worst=0.062ms
|
||||
1 poly + cellsTouched (scanline): n=2000 avg=0.015ms worst=0.036ms
|
||||
6 polys + cellsTouched (scanline): n=300 avg=0.088ms worst=0.118ms
|
||||
1 poly + cellsTouchedOld (prototype reference): n=500 avg=0.517ms worst=1.031ms
|
||||
TARGET polygon+cells, prototype scene (prototype baseline 0.451): 0.015 ms vs <= 0.451 ms: PASS
|
||||
== 5k sparse: genScene(world=16384., n=5000, seed=1), avg walls in vision bbox 15.6 ==
|
||||
1 poly, 5k sparse: n=2000 avg=0.064ms worst=0.524ms
|
||||
1 poly + cellsTouched, 5k sparse: n=2000 avg=0.074ms worst=0.158ms
|
||||
1 move check (pathClear, cap-length step), 5k sparse: n=20000 avg=0.008ms worst=0.578ms
|
||||
TARGET polygon, 5k-wall scene: 0.064 ms vs <= 1.0 ms: PASS
|
||||
1 poly, 5k sparse: n=2000 avg=0.031ms worst=0.059ms
|
||||
1 poly + cellsTouched, 5k sparse: n=2000 avg=0.042ms worst=0.364ms
|
||||
1 move check (pathClear, cap-length step), 5k sparse: n=20000 avg=0.008ms worst=0.162ms
|
||||
TARGET polygon, 5k-wall scene: 0.031 ms vs <= 1.0 ms: PASS
|
||||
== 5k dense: genScene(world=4096., n=5000, seed=1), avg walls in vision bbox 206.9 ==
|
||||
1 poly, 5k dense: n=2000 avg=1.036ms worst=2.647ms
|
||||
1 poly + cellsTouched, 5k dense: n=2000 avg=1.882ms worst=6.806ms
|
||||
1 move check (pathClear, cap-length step), 5k dense: n=20000 avg=0.008ms worst=0.109ms
|
||||
== egress (#28: 6 players x 40 encountered entries, 262144 explored cells) ==
|
||||
GM egress pass (fresh diff base): n=200 avg=0.242ms worst=0.306ms
|
||||
player egress pass (fresh diff base): n=200 avg=0.082ms worst=0.103ms
|
||||
(sink 6038922)
|
||||
1 poly, 5k dense: n=2000 avg=0.681ms worst=2.336ms
|
||||
1 poly + cellsTouched, 5k dense: n=2000 avg=0.710ms worst=2.193ms
|
||||
1 move check (pathClear, cap-length step), 5k dense: n=20000 avg=0.007ms worst=0.127ms
|
||||
(sink 6023672)
|
||||
|
||||
+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 =
|
||||
|
||||
+129
-1
@@ -1,6 +1,7 @@
|
||||
# FFF_MUTATION=1 (set only by tools/mutation.sh) cuts iteration counts ~10x via scaled(); plain `devbox run test` runs full counts.
|
||||
import std/[unittest, json, os, math]
|
||||
import std/[unittest, json, os, math, algorithm]
|
||||
import geometry, refold, testutil
|
||||
import ../tools/scenegen
|
||||
|
||||
proc sees(walls: openArray[Wall], o, p: Vec, r = 500.0): bool =
|
||||
visibilityPolygon(o, walls, r).inside(p)
|
||||
@@ -330,6 +331,133 @@ suite "corpus (prototype leak transcripts)":
|
||||
check n >= scaled(300) # corpus is not silently empty
|
||||
check doorsSeen > 0 # and exercises opened doors
|
||||
|
||||
# ---- #64: the sector-limited fan must be bit-identical to the all-segments fan ----------------------------------
|
||||
# The oracle is the pre-#64 `visibilityPolygon`, copied verbatim (including the #26 collinear-hit branch in
|
||||
# `rayHit`). Equality is exact float equality, not a tolerance: the sector-limited fan only skips segs that the
|
||||
# ray cannot hit, so every surviving `rayHit` value (and the picked min) must be identical bit for bit.
|
||||
const RefRayEps = 1e-4
|
||||
|
||||
proc refRayHit(o, d: Vec, a, b: Vec): float {.inline.} =
|
||||
let e = b - a
|
||||
let den = cross(d, e)
|
||||
if abs(den) < 1e-12:
|
||||
if abs(cross(a - o, d)) > Eps: return Inf
|
||||
let ta = (a.x - o.x) * d.x + (a.y - o.y) * d.y
|
||||
let tb = (b.x - o.x) * d.x + (b.y - o.y) * d.y
|
||||
let t0 = min(ta, tb)
|
||||
return (if t0 > Eps: t0 else: Inf)
|
||||
let ao = a - o
|
||||
let t = cross(ao, e) / den
|
||||
let u = cross(ao, d) / den
|
||||
if t > Eps and u >= -Eps and u <= 1 + Eps: t else: Inf
|
||||
|
||||
proc referencePolygon(o: Vec, walls: openArray[Wall], radius: float, circleSegs = 64): seq[Vec] =
|
||||
## The all-64-circle-segments fan, unchanged. Test-only oracle for #64.
|
||||
var segs: seq[(Vec, Vec)]
|
||||
var angles: seq[float]
|
||||
for i in 0 ..< circleSegs:
|
||||
let a0 = 2 * PI * i.float / circleSegs.float
|
||||
let a1 = 2 * PI * (i + 1).float / circleSegs.float
|
||||
segs.add(((o.x + radius * cos a0, o.y + radius * sin a0),
|
||||
(o.x + radius * cos a1, o.y + radius * sin a1)))
|
||||
angles.add arctan2(sin a0, cos a0)
|
||||
for w in walls:
|
||||
if not w.blocksSight: continue
|
||||
if max(w.a.x, w.b.x) < o.x - radius or min(w.a.x, w.b.x) > o.x + radius or
|
||||
max(w.a.y, w.b.y) < o.y - radius or min(w.a.y, w.b.y) > o.y + radius: continue
|
||||
if w.sightDir != wdBoth and not w.blindTo(o): continue
|
||||
segs.add((w.a, w.b))
|
||||
for p in [w.a, w.b]:
|
||||
if abs(p.x - o.x) < Eps and abs(p.y - o.y) < Eps: continue
|
||||
let ang = arctan2(p.y - o.y, p.x - o.x)
|
||||
angles.add ang
|
||||
angles.add ang - RefRayEps
|
||||
angles.add ang + RefRayEps
|
||||
angles.sort
|
||||
for ang in angles:
|
||||
let d: Vec = (cos ang, sin ang)
|
||||
var best = Inf
|
||||
for s in segs: best = min(best, refRayHit(o, d, s[0], s[1]))
|
||||
if best < Inf: result.add((o.x + d.x * best, o.y + d.y * best))
|
||||
|
||||
proc wallVariants(base: seq[Wall]): seq[seq[Wall]] =
|
||||
## The round-1 switch shapes geometry must see: plain, one-way both directions, window, open doors.
|
||||
result.add base
|
||||
var oneWay = base
|
||||
for i in 0 ..< oneWay.len:
|
||||
oneWay[i].sightDir = [wdLeft, wdRight, wdBoth][i mod 3]
|
||||
result.add oneWay
|
||||
var windows = base
|
||||
for i in 0 ..< windows.len:
|
||||
windows[i].sightBarrier = i mod 5 != 0
|
||||
result.add windows
|
||||
var opened = base
|
||||
for i in 0 ..< opened.len:
|
||||
if opened[i].door: opened[i].open = true
|
||||
result.add opened
|
||||
|
||||
suite "sector-limited fan (#64: same polygon, fewer segment tests)":
|
||||
test "corpus polygons are bit-identical to the all-segments fan":
|
||||
let sc = loadScene()
|
||||
var n = 0
|
||||
var li = 0
|
||||
for line in lines(currentSourcePath().parentDir / "fixtures" / "corpus.jsonl"):
|
||||
inc li
|
||||
if li mod (300 div scaled(300)) != 0: continue
|
||||
let j = parseJson(line)
|
||||
var walls = sc.walls
|
||||
for w in walls.mitems:
|
||||
if w.door: w.open = false
|
||||
for id in j["open"]: walls[id.getInt].open = true
|
||||
let o: Vec = (j["pos"][0].getFloat, j["pos"][1].getFloat)
|
||||
inc n
|
||||
check visibilityPolygon(o, walls, sc.radius) == referencePolygon(o, walls, sc.radius)
|
||||
check n >= 1
|
||||
|
||||
test "seed scene tokens, doors and every switch shape: bit-identical":
|
||||
let sc = loadScene()
|
||||
for walls in wallVariants(sc.walls):
|
||||
for p in sc.tokens:
|
||||
check visibilityPolygon(p, walls, sc.radius) == referencePolygon(p, walls, sc.radius)
|
||||
|
||||
test "scenegen sparse and dense layouts, every switch shape: bit-identical":
|
||||
for (world, seed) in [(16384.0, 1'u64), (4096.0, 1'u64)]:
|
||||
let base = genScene(world, 5000, seed)
|
||||
var r = Rng(s: 0xC64)
|
||||
for walls in wallVariants(base):
|
||||
for _ in 0 ..< scaled(8):
|
||||
let o: Vec = (r.uni(400, world - 400), r.uni(400, world - 400))
|
||||
check visibilityPolygon(o, walls, 400) == referencePolygon(o, walls, 400)
|
||||
|
||||
test "#26 collinear/radial walls: bit-identical (the fix is preserved)":
|
||||
let cases: seq[(Vec, seq[Wall])] = @[
|
||||
((200.0, 200.0), @[W(300, 200, 500, 200)]),
|
||||
((200.0, 200.0), @[W(500, 200, 300, 200)]),
|
||||
((200.0, 200.0), @[W(200, 300, 200, 500)]),
|
||||
((200.0, 200.0), @[W(200, 500, 200, 300)]),
|
||||
((320.0, 256.0), @[W(448, 256, 608, 256)]),
|
||||
((320.0, 256.0), @[W(320, 448, 320, 608)]),
|
||||
((320.0, 256.0), @[W(320, 448, 320, 608, sightDir = wdLeft)]),
|
||||
((320.0, 256.0), @[Wall(a: (320.0, 448.0), b: (320.0, 608.0), sightBarrier: false)]),
|
||||
]
|
||||
for (o, walls) in cases:
|
||||
check visibilityPolygon(o, walls, 600) == referencePolygon(o, walls, 600)
|
||||
|
||||
test "random walls, grid-snapped radial additions and switches: bit-identical":
|
||||
var r = Rng(s: 64)
|
||||
for iter in 0 ..< scaled(120):
|
||||
var walls = randomWalls(r, 5 + iter mod 50, 1200)
|
||||
for i in 0 ..< walls.len:
|
||||
if i mod 3 == 0: walls[i].sightDir = wdLeft
|
||||
if i mod 5 == 0: walls[i].sightBarrier = false
|
||||
let o: Vec = (r.uni(100, 1100), r.uni(100, 1100))
|
||||
if iter mod 3 == 0:
|
||||
# force the #26 case: a wall on the viewer's own row or column
|
||||
walls.add W(o.x + 60, o.y, o.x + 260, o.y)
|
||||
walls.add W(o.x, o.y + 60, o.x, o.y + 260)
|
||||
let radius = r.uni(50, 500)
|
||||
check visibilityPolygon(o, walls, radius) == referencePolygon(o, walls, radius)
|
||||
|
||||
# ---- differential: scanline vs prototype per-cell ---------------------------------------------------------------
|
||||
suite "cellsTouched differential vs prototype":
|
||||
test "random visibility polygons: identical cell lists":
|
||||
|
||||
+3
-2
@@ -2,7 +2,7 @@
|
||||
# usage: devbox run mutation (or tools/mutation.sh inside devbox shell)
|
||||
# Mutation check on a TEMP COPY of server/ + tests/ + tools/ (the tree is never touched). One mutation per run, ALL
|
||||
# tests/t*.nim are run. Control must pass; every mutation must fail >=1 expected test; a sed that changes nothing is an error.
|
||||
# a: no +-RayEps rays | b: upper segment end exclusive | c: lower segment end exclusive (rayHit only)
|
||||
# a: no +-RayEps rays | a2: a ray tests the next sector's chord (#64) | b: upper segment end exclusive | c: lower segment end exclusive (rayHit only)
|
||||
# d1/d2: drop arm 1/2 of the shared-vertex fixture
|
||||
# e: cellsTouched without the fill spans | f: escape rule without the crossing check | g: GM moves checked against walls
|
||||
# h: capsule reduced to centre path + destination circle (prototype H10) | i: no step cap | j: blocked trigger needs centre-on-wall
|
||||
@@ -115,6 +115,7 @@ run() {
|
||||
}
|
||||
run control "unmutated copy (must pass)" server/geometry.nim "" ""
|
||||
run a "remove the +-RayEps rays" server/geometry.nim '/angles.add ang [-+] RayEps/d' "$NEW"
|
||||
run a2 "a ray tests the next sector's chord" server/geometry.nim 's/^ best = min(best, rayHit(o, d, segs\[sector\]\[0\], segs\[sector\]\[1\]))$/ best = min(best, rayHit(o, d, segs[(sector + 1) mod circleSegs][0], segs[(sector + 1) mod circleSegs][1]))/' "bit-identical"
|
||||
run b "upper segment end exclusive" server/geometry.nim 's/u <= 1 + Eps/u < 1 - Eps/' "$NEW"
|
||||
run c "lower segment end exclusive" server/geometry.nim 's/u >= -Eps/u > Eps/' "$NEW"
|
||||
run d1 "shared-vertex fixture without arm 1" tests/tgeometry.nim 's/@\[arm1, arm2\] *# SHARED-VERTEX/@[arm2]/' "$D"
|
||||
@@ -219,7 +220,7 @@ run f3 "the opaque depth cap is not enforced" server/props.nim 's/if depth > Max
|
||||
wait
|
||||
|
||||
rc=0; : > "$LOG"
|
||||
for id in control a b c d1 d2 d3 e e1 e2 e3 e4 e5 f1 f2 f3 f g h i j k l m n o p q r s t u v w x y z z2 \
|
||||
for id in control a a2 b c d1 d2 d3 e e1 e2 e3 e4 e5 f1 f2 f3 f g h i j k l m n o p q r s t u v w x y z z2 \
|
||||
s1 s2 s3 s4 s5 s6 s7 aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au \
|
||||
av aw ax ay az b1 b2 b3 b4 c1 c2 c3 c4; do
|
||||
[ -f "$T/$id/res" ] && cat "$T/$id/res" >> "$LOG"
|
||||
|
||||
Reference in New Issue
Block a user