diff --git a/prototype-wall-sight/.gitignore b/prototype-wall-sight/.gitignore new file mode 100644 index 0000000..d77f55a --- /dev/null +++ b/prototype-wall-sight/.gitignore @@ -0,0 +1,2 @@ +pictures +bench_thick diff --git a/prototype-wall-sight/README.md b/prototype-wall-sight/README.md new file mode 100644 index 0000000..fc88dc1 --- /dev/null +++ b/prototype-wall-sight/README.md @@ -0,0 +1,41 @@ +# PROTOTYPE #43 — sight with thick and low walls + +**Throwaway.** Branch `prototype/wall-sight` only; never pushed, never merged, `server/` not modified. +Open `index.html` (double-click) — it links every picture, the cost table and the open questions. + +Question it answers: *what does sight actually look like with thick and low walls in our polygon code?* +(issue [#43](https://git.fossellini.top/SirStone/FFF_vtt/issues/43), map #38; constraints from #42 / DESIGN 5.8 +and #41 / DESIGN 5.7.) + +## What is here + +| file | what | +|---|---| +| `sightmodel.nim` | the model the tickets argue about: 5.8 switch filter (no kind logic), thickness → 3-face or 4-face expansion, per-target height predicate. Wraps the real `server/geometry.nim` unchanged. | +| `pictures.nim` | the six scenarios, rendered as SVG into `img/`. | +| `img/*.svg` | the pictures (open in any browser; regenerated by `pictures.nim`). | +| `bench_thick.nim` | cost: thin baseline vs 3-face and 4-face thickness, per-query and hoisted, on the prototype scene and the 5k sparse/dense scenes from `tools/bench.nim`. | +| `cost.txt` | raw output of the last bench run. | +| `index.html` | the artifact: pictures + captions + cost table + surprises / open questions with suggested answers. | + +## Run + +```sh +nim c -r prototype-wall-sight/pictures.nim # rewrites img/*.svg +nim c -d:release -r prototype-wall-sight/bench_thick.nim # rewrites cost.txt +``` + +## Model in one paragraph + +A wall is `{blocks_sight, blocks_movement, door, open, sight_dir, thickness, height}`. Before any geometry the +sight input is filtered: absent when `blocks_sight` is off (that is `terrain`, and it costs nothing), absent when it +is an open door, absent for the unblocked side of a one-way wall. What survives contributes either one zero-width +segment (today) or 3–4 rectangle faces (thick). `height` never touches the polygon: it is one comparison per wall +crossing on the line to the target's top, because a nearest-hit ray fan can shorten a ray but never resume it +(research #39 §3.3). The explored/fog set in the pictures comes from the strict polygon, and the dashed polygon in +scenario 4 is the permissive alternative, so the pictures show what each choice reveals. + +## Verdict / where it feeds back + +Captured on #43 for the owner; the lists in `index.html` are written to feed #44 (low walls: sight and movement) and +#45 (thick walls). Main branch keeps only whatever the owner decides there. diff --git a/prototype-wall-sight/bench_thick.nim b/prototype-wall-sight/bench_thick.nim new file mode 100644 index 0000000..a65d59c --- /dev/null +++ b/prototype-wall-sight/bench_thick.nim @@ -0,0 +1,113 @@ +## PROTOTYPE (#43) - THROWAWAY. Cost of thick walls in the sight polygon, against today's thin wall. +## Harness shape copied from tools/bench.nim (50 warm-up runs, avg + worst, O(1) sink, -d:release): +## nim c -d:release -r prototype-wall-sight/bench_thick.nim +## Scenes: tests/fixtures/scene.json (60 walls) and genScene() 5k sparse / 5k dense (tools/bench.nim). +## +## Two different costs are separated on purpose, because #45 has to pay both: +## (a) the O(walls in the scene) filter + thickness expansion, which runs per query unless hoisted; +## (b) the ray fan itself, O(rays x segments in the box). +## A one-way wall forces (a) per query (the filter is per observer); a quad does not. +import std/[json, monotimes, times, strformat, os, math, strutils] +import sightmodel, geometry, ../tools/scenegen, ../tests/testutil + +let root = currentSourcePath().parentDir.parentDir +const + thickPx = 10.0 + cell = 32.0 + radius = 400.0 + iters = 2000 + +var sink = 0 + +proc bench(n: int, f: proc(i: int) {.closure.}): (float, float) = + for i in 0 ..< 50: f(i) + var sum, worst = 0.0 + for i in 0 ..< n: + let t0 = getMonoTime(); f(i) + let ms = (getMonoTime() - t0).inNanoseconds.float / 1e6 + sum += ms; worst = max(worst, ms) + result = (sum / n.float, worst) + +proc loadProto(): seq[SightWall] = + let j = parseFile(root / "tests" / "fixtures" / "scene.json") + for w in j["walls"]: + result.add SightWall(a: (w["a"][0].getFloat, w["a"][1].getFloat), + b: (w["b"][0].getFloat, w["b"][1].getFloat), + blocksSight: not (w["door"].getBool and w["open"].getBool), + blocksMovement: true, door: w["door"].getBool, open: w["open"].getBool, + height: FullHeight, label: "wall") + +proc loadBig(world: float): seq[SightWall] = + for w in genScene(world, 5000, 1): + result.add SightWall(a: w.a, b: w.b, blocksSight: true, blocksMovement: true, + door: w.door, open: w.open, height: FullHeight, label: "wall") + +proc thickened(walls: seq[SightWall]): seq[SightWall] = + for w in walls: result.add thick(w, thickPx) + +proc wallsInBox(walls: seq[SightWall], o: Vec): int = + for w in walls: + if not (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): inc result + +proc expand(walls: seq[SightWall], o: Vec, mode: ThickMode): seq[Wall] = + toWalls(sightSegments(walls, o, mode)) + +type Row = object + scene, arm: string + ms: float + verts, cells: int + +var rows: seq[Row] + +proc runScene(label: string, thin, thickWalls: seq[SightWall], lo, hi: float) = + echo "== ", label, " ==" + var r = Rng(s: 99) + var orig: seq[Vec] + for i in 0 ..< 200: orig.add (r.uni(lo, hi), r.uni(lo, hi)) + var boxN = 0 + for o in orig: boxN += wallsInBox(thin, o) + echo &" avg walls in the 800x800 vision box: {boxN.float / 200.0:.1f}" + + # today, exactly: the blocking walls as one pre-built segment list, ray fan only + let baseSegs = expand(thin, orig[0], tmThin) + let quadSegs = expand(thickWalls, orig[0], tmQuad) # viewer-independent, so hoistable + + var msBase = 0.0 + proc measure(name: string, f: proc(o: Vec): seq[Vec]) = + var vsum = 0 + let (ms, worst) = bench(iters, proc(i: int) = + let k = f(orig[i mod 200]).len + vsum += k) + var csum = 0 + for i in 0 ..< 60: csum += cellsTouched(f(orig[i]), cell).len + if name == "thin (today)": msBase = ms + echo &" {name:<28} avg={ms:.3f} ms worst={worst:.3f} verts={vsum div iters} cells={csum div 60} " & + (if msBase > 0: &"{ms / msBase:.1f}x thin" else: "") + rows.add Row(scene: label, arm: name, ms: ms, verts: vsum div iters, cells: csum div 60) + + measure("thin (today)", proc(o: Vec): seq[Vec] = visibilityPolygon(o, baseSegs, radius)) + measure("thin + per-query filter", proc(o: Vec): seq[Vec] = + visibilityPolygon(o, expand(thin, o, tmThin), radius)) + measure("thick 3 faces (per query)", proc(o: Vec): seq[Vec] = + visibilityPolygon(o, expand(thickWalls, o, tmSilhouette), radius)) + measure("thick 4 faces (per query)", proc(o: Vec): seq[Vec] = + visibilityPolygon(o, expand(thickWalls, o, tmQuad), radius)) + measure("thick 4 faces (hoisted)", proc(o: Vec): seq[Vec] = visibilityPolygon(o, quadSegs, radius)) + +let proto = loadProto() +runScene("prototype scene (60 walls, 4096 px, radius 400)", proto, thickened(proto), 200.0, 3896.0) +let sparse = loadBig(16384) +runScene("5k sparse (16384 px)", sparse, thickened(sparse), 400.0, 15984.0) +let dense = loadBig(4096) +runScene("5k dense (4096 px)", dense, thickened(dense), 400.0, 3696.0) + +echo "" +echo "| scene | arm | poly avg ms | verts | cells | vs thin |" +echo "|---|---|---|---|---|---|" +for r in rows: + var base = 1.0 + for b in rows: + if b.scene == r.scene and b.arm == "thin (today)": base = b.ms + echo &"| {r.scene} | {r.arm} | {r.ms:.3f} | {r.verts} | {r.cells} | {r.ms / base:.2f}x |" +echo "(sink ", sink, ")" diff --git a/prototype-wall-sight/cost.txt b/prototype-wall-sight/cost.txt new file mode 100644 index 0000000..abeacb9 --- /dev/null +++ b/prototype-wall-sight/cost.txt @@ -0,0 +1,40 @@ +== prototype scene (60 walls, 4096 px, radius 400) == + avg walls in the 800x800 vision box: 3.9 + thin (today) avg=0.031 ms worst=0.094 verts=88 cells=412 1.0x thin + thin + per-query filter avg=0.033 ms worst=0.069 verts=88 cells=412 1.1x thin + thick 3 faces (per query) avg=0.051 ms worst=0.133 verts=119 cells=396 1.7x thin + thick 4 faces (per query) avg=0.064 ms worst=0.183 verts=142 cells=394 2.1x thin + thick 4 faces (hoisted) avg=0.057 ms worst=0.475 verts=142 cells=394 1.8x thin +== 5k sparse (16384 px) == + avg walls in the 800x800 vision box: 15.8 + thin (today) avg=0.083 ms worst=0.150 verts=162 cells=241 1.0x thin + thin + per-query filter avg=0.237 ms worst=0.337 verts=162 cells=241 2.8x thin + thick 3 faces (per query) avg=0.699 ms worst=2.362 verts=314 cells=219 8.4x thin + thick 4 faces (per query) avg=0.943 ms worst=2.641 verts=410 cells=217 11.3x thin + thick 4 faces (hoisted) avg=0.263 ms worst=0.599 verts=410 cells=217 3.2x thin +== 5k dense (4096 px) == + avg walls in the 800x800 vision box: 206.0 + thin (today) avg=1.254 ms worst=3.096 verts=1332 cells=54 1.0x thin + thin + per-query filter avg=1.399 ms worst=3.333 verts=1332 cells=54 1.1x thin + thick 3 faces (per query) avg=7.851 ms worst=9.884 verts=3724 cells=26 6.3x thin + thick 4 faces (per query) avg=13.229 ms worst=17.079 verts=4976 cells=21 10.6x thin + thick 4 faces (hoisted) avg=12.721 ms worst=15.625 verts=4976 cells=21 10.1x thin + +| scene | arm | poly avg ms | verts | cells | vs thin | +|---|---|---|---|---|---| +| prototype scene (60 walls, 4096 px, radius 400) | thin (today) | 0.031 | 88 | 412 | 1.00x | +| prototype scene (60 walls, 4096 px, radius 400) | thin + per-query filter | 0.033 | 88 | 412 | 1.06x | +| prototype scene (60 walls, 4096 px, radius 400) | thick 3 faces (per query) | 0.051 | 119 | 396 | 1.65x | +| prototype scene (60 walls, 4096 px, radius 400) | thick 4 faces (per query) | 0.064 | 142 | 394 | 2.06x | +| prototype scene (60 walls, 4096 px, radius 400) | thick 4 faces (hoisted) | 0.057 | 142 | 394 | 1.84x | +| 5k sparse (16384 px) | thin (today) | 0.083 | 162 | 241 | 1.00x | +| 5k sparse (16384 px) | thin + per-query filter | 0.237 | 162 | 241 | 2.84x | +| 5k sparse (16384 px) | thick 3 faces (per query) | 0.699 | 314 | 219 | 8.37x | +| 5k sparse (16384 px) | thick 4 faces (per query) | 0.943 | 410 | 217 | 11.30x | +| 5k sparse (16384 px) | thick 4 faces (hoisted) | 0.263 | 410 | 217 | 3.15x | +| 5k dense (4096 px) | thin (today) | 1.254 | 1332 | 54 | 1.00x | +| 5k dense (4096 px) | thin + per-query filter | 1.399 | 1332 | 54 | 1.12x | +| 5k dense (4096 px) | thick 3 faces (per query) | 7.851 | 3724 | 26 | 6.26x | +| 5k dense (4096 px) | thick 4 faces (per query) | 13.229 | 4976 | 21 | 10.55x | +| 5k dense (4096 px) | thick 4 faces (hoisted) | 12.721 | 4976 | 21 | 10.15x | +(sink 0) diff --git a/prototype-wall-sight/img/s1-thin-vs-thick.svg b/prototype-wall-sight/img/s1-thin-vs-thick.svg new file mode 100644 index 0000000..697806c --- /dev/null +++ b/prototype-wall-sight/img/s1-thin-vs-thick.svg @@ -0,0 +1,659 @@ + + + + +1a thin wall (t = 0 px, today) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall + +1.8 +T1 + +1.8 +T2 + +1.8 +T3 + +eye 1.7 m + +polygon reaches the line; explored cells hug it + + +1b thick wall (t = 14 px, 3 faces) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall t=14.0 + +1.8 +T1 + +1.8 +T2 + +1.8 +T3 + +eye 1.7 m + +body hidden, shadow shifted out by t/2; same tokens, fewer cells + + + +floor the eye sees + +fog would reveal it, the eye does not see it + +explored cell (memory, no current sight) +solid blue = today's sight polygon +dashed blue = low walls ignored (family A), scenario 4 only + + diff --git a/prototype-wall-sight/img/s2-corner.svg b/prototype-wall-sight/img/s2-corner.svg new file mode 100644 index 0000000..1a8897e --- /dev/null +++ b/prototype-wall-sight/img/s2-corner.svg @@ -0,0 +1,684 @@ + + + + +2a inside corner, thin walls + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall + + +wall + +1.8 +T1 + +1.8 +T2 + +1.8 +T3 + +eye 1.7 m + +sight reaches the shared vertex exactly + + +2b same corner, thick walls (t = 14) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall t=14.0 + + +wall t=14.0 + +1.8 +T1 + +1.8 +T2 + +1.8 +T3 + +eye 1.7 m + +bodies overlap at the joint; corner cell lost, no crack either way + + + +floor the eye sees + +fog would reveal it, the eye does not see it + +explored cell (memory, no current sight) +solid blue = today's sight polygon +dashed blue = low walls ignored (family A), scenario 4 only + + diff --git a/prototype-wall-sight/img/s3-door-in-thick-wall.svg b/prototype-wall-sight/img/s3-door-in-thick-wall.svg new file mode 100644 index 0000000..d08a893 --- /dev/null +++ b/prototype-wall-sight/img/s3-door-in-thick-wall.svg @@ -0,0 +1,1072 @@ + + + + +3a thick wall, door CLOSED (viewer at 30 deg) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall t=14.0 + + +wall t=14.0 + + +door t=14.0 + +1.8 +A + +1.8 +B + +1.8 +C + +eye 1.7 m + +door body + walls; opening is a slot of depth t + + +3b same wall, door OPEN + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall t=14.0 + + +wall t=14.0 + + +door t=14.0 (open) + +1.8 +A + +1.8 +B + +1.8 +C + +eye 1.7 m + +door absent from geometry; the deep slot still clips an oblique view + + + +floor the eye sees + +fog would reveal it, the eye does not see it + +explored cell (memory, no current sight) +solid blue = today's sight polygon +dashed blue = low walls ignored (family A), scenario 4 only + + diff --git a/prototype-wall-sight/img/s4-low-wall.svg b/prototype-wall-sight/img/s4-low-wall.svg new file mode 100644 index 0000000..e832e7e --- /dev/null +++ b/prototype-wall-sight/img/s4-low-wall.svg @@ -0,0 +1,810 @@ + + + + +4a low wall 1.0 m, tall viewer 1.8 m (eye 1.67) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall t=12.0 1.0 m + +1.8 +1.8 near + +0.3 +0.3 near + +0.1 +0.1 near + +1.0 +1.0 mid + +0.3 +0.3 far + +1.8 +1.8 far + +eye 1.7 m + +paper = eye sees floor; salmon = fog reveals it but the eye does not + + +4b same wall, short viewer 1.0 m (eye 0.93) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +wall t=12.0 1.0 m + +1.8 +1.8 near + +0.3 +0.3 near + +0.1 +0.1 near + +1.0 +1.0 mid + +0.3 +0.3 far + +1.8 +1.8 far + +eye 0.9 m + +eye 0.93 m < wall 1.0 m: no floor see-over anywhere; only tall targets' tops + + + +floor the eye sees + +fog would reveal it, the eye does not see it + +explored cell (memory, no current sight) +solid blue = today's sight polygon +dashed blue = low walls ignored (family A), scenario 4 only + + diff --git a/prototype-wall-sight/img/s5-terrain.svg b/prototype-wall-sight/img/s5-terrain.svg new file mode 100644 index 0000000..439c07c --- /dev/null +++ b/prototype-wall-sight/img/s5-terrain.svg @@ -0,0 +1,831 @@ + + + + +5a no wall + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1.8 +T1 + +1.8 +T2 + +eye 1.7 m + +reference polygon and explored set + + +5b terrain wall (blocks_movement only, t = 14) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +terrain t=14.0 + +1.8 +T1 + +1.8 +T2 + +eye 1.7 m + +identical picture: blocks_sight off = zero polygon change, thickness included + + + +floor the eye sees + +fog would reveal it, the eye does not see it + +explored cell (memory, no current sight) +solid blue = today's sight polygon +dashed blue = low walls ignored (family A), scenario 4 only + + diff --git a/prototype-wall-sight/img/s6-one-way.svg b/prototype-wall-sight/img/s6-one-way.svg new file mode 100644 index 0000000..c15a8e5 --- /dev/null +++ b/prototype-wall-sight/img/s6-one-way.svg @@ -0,0 +1,848 @@ + + + + +6a one-way (sight_dir = left), viewer WEST of a->b + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +one-way t=12.0 + +1.8 +west 1 + +1.8 +west 2 + +1.8 +east 1 + +1.8 +east 2 + +eye 1.7 m + +west is the blocked side: polygon stops at the wall, east tokens stay fogged + + +6b same wall, viewer EAST + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +one-way t=12.0 + +1.8 +west 1 + +1.8 +west 2 + +1.8 +east 1 + +1.8 +east 2 + +eye 1.7 m + +east is the unblocked side: the wall is absent from this observer's geometry + + + +floor the eye sees + +fog would reveal it, the eye does not see it + +explored cell (memory, no current sight) +solid blue = today's sight polygon +dashed blue = low walls ignored (family A), scenario 4 only + + diff --git a/prototype-wall-sight/index.html b/prototype-wall-sight/index.html new file mode 100644 index 0000000..223d3ab --- /dev/null +++ b/prototype-wall-sight/index.html @@ -0,0 +1,266 @@ + + + + +Prototype #43 — sight with thick and low walls + + + + +

Sight with thick and low walls

+

Throwaway prototype for issue #43 (wayfinder map #38). Pictures, explored cells and cost of what our +polygon code does when a wall has a width and a height. Branch prototype/wall-sight, never pushed, +server/ untouched.

+ +
This is a prototype, not a design. It wraps the real +server/geometry.nim (the same ray fan, the same cellsTouched) and adds only the model the +tickets are arguing about: the 5.8 switch filter, a thickness expansion and a per-target height predicate. Nothing +here is production code and nothing was folded back.
+ +

How to read every picture

+
+ +

Every picture is generated from scratch by pictures.nim. The filter is +5.8's and nothing else: a wall enters the fan only if blocks_sight, never when it is an open door, +a one-way wall only for the blocked side — no kind logic anywhere. Height never enters the polygon; it is a +per-target 3D test (eye + t·(target − eye) > wall height), which is what research #39 §3.3 said +has to happen because a nearest-hit fan can shorten a ray but never resume it.

+
+ +

The six pictures

+ +
+ thin wall vs thick wall, same layout +
+

1 — Thin vs thick, same layout (t = 14 px)

+

Left: today's zero-width wall. Right: the same wall as a 3-face rectangle (near face + two end caps). The + wall's own body becomes hidden floor, the shadow edge moves out by t/2 (7 px here — visible only if you compare + the two panels), and the polygon is a strict subset: 228 explored cells thin, 227 thick, 0 gained, 1 lost.

+

Surprise: at 14 px on a 400 px room the pictures are almost identical. Thickness costs cells in + proportion to cell size vs room size, so on a normal map it is a border cell here and there — the dense + 5k layout is where it eats half the view.

+
+
+ +
+ two thick walls meeting at a corner +
+

2 — Corner where two thick walls meet

+

Same L-shaped room, thin then thick. Thin: the polygon reaches the shared vertex exactly. Thick: the two + bodies overlap at the joint, the inner corner loses its cells (242 → 232 explored, 0 gained, 10 lost), and the + eye sees less into the corner because the near faces are 7 px closer.

+

Surprise: no crack at the joint in either panel. The expected zero-width sliver leak at a shared + vertex did not appear at this scale — the ±RayEps fan already covers it — so "thickness is needed to close the + joints" is not what this layout shows; the polygon change comes from the wall bodies alone.

+
+
+ +
+ doorway in a thick wall, closed and open +
+

3 — Doorway in a thick wall, closed vs open, viewer at an angle

+

The wall has a 90 px opening with a 14 px thick leaf. Closed (left): nothing passes. Open (right): the door is + gone from the input, but the two jambs are 14 px deep, so the view through is a narrow diagonal beam — token A + (right behind the opening) is visible, token C (left of it) is behind the wall, and token B is clipped by the + far corner of the right jamb even though it is well past the opening.

+

Surprise: opening a thick door does not reveal the doorway's width, it reveals a keyhole whose + shape depends on where the viewer stands. On a 32 px grid that beam is 1–2 cells wide, so a player standing two + steps to the side unlocks almost nothing through a door they just opened.

+
+
+ +
+ 1 m wall, tall and short viewer, tokens of different heights +
+

4 — A 1 m wall, tall viewer (1.8 m) vs short viewer (1.0 m)

+

Wall 12 px thick, 1.0 m high; the eye is 70 px in front of it. For the 1.8 m viewer (eye 1.67 m) the floor + behind the wall is hidden out to d = s·h_eye/(h_eye−h_wall) = 174 px from the eye — the salmon band — + and visible again beyond it. The dashed polygon is the same fan with the low wall ignored: exactly the extra floor + a "low walls never block" rule would reveal. For the 1.0 m viewer (eye 0.93 m, below the wall top) no floor behind + is ever visible: the whole far side is salmon, but a 1.8 m token's head still shows over the wall.

+

Surprise: the target's height matters as much as the wall's. In 4a a 1.0 m token 75 px behind the + wall is visible while a 0.3 m crate 45 px behind it is not; in 4b (eye below the wall top) two tall tokens are + visible and nothing else is. A wall-only flag ("low wall = see over") gets both of those wrong.

+

Surprise: the fog has to pick an error. Solid polygon = the floor behind a low wall is + never explored from a distance, so a token you can see stands on a black tile. Dashed polygon = the fog + reveals a band nobody can see. There is no polygon that does the right thing; that is #44's decision.

+
+
+ +
+ terrain wall vs no wall +
+

5 — Terrain wall = no wall (the 5.8 payoff)

+

Left: no wall at all. Right: the same layout with a 14 px thick terrain wall + (blocks_sight:false, blocks_movement:true). The two polygons are vertex-identical (64/64, 318 explored + cells both), the explored sets are equal and nothing is drawn differently apart from the wall itself.

+

Confirmed rather than surprising: blocks_sight:false costs literally zero polygon work, + thickness and height included, because the wall never reaches the geometry. Any "cheap terrain" argument in #44/#45 + can rely on that.

+
+
+ +
+ one-way wall seen from both sides +
+

6 — One-way wall, from both sides

+

One 12 px thick wall, sight_dir:"left", a → b pointing down the page, so the left + (west) side is the blocked one. Left: a viewer west of it gets a polygon that stops at the wall, and both east + tokens stay in fog. Right: a viewer east of it has the wall absent from the geometry (one filter decision, + not a special case in the fan) and sees west through it, including the two west tokens.

+

Surprise: this is the only scenario where the explored maps of two players in the same room differ + permanently, and it is the only place where the 5.8 filter must run per observer — i.e. per polygon, not per scene + (see the cost table: that filter is not free).

+
+
+ +

Cost

+

Same harness as tools/bench.nim (2000 iterations, 50 warm-up, -d:release, avg ms per +one visibility polygon, radius 400, cell 32). "Today" = the blocking walls pre-expanded into the segment list the fan +already takes, which is what production does. Thick = 10 px, 3 faces (near + 2 caps, exact for a viewer on the near +side but observer-dependent) or 4 faces (a quad, exact and hoistable once per wall edit). Cells = explored cells per +polygon.

+ + + + + + + + + + + + + + + + + + + + +
scenearmpoly msvertscellsvs today
prototype (60 walls, 4096 px)thin (today)0.031884121.00×
thin + 5.8 filter per query0.033884121.06×
thick 3 faces, per query0.0511193961.65×
thick 4 faces, per query0.0641423942.06×
thick 4 faces, hoisted0.0571423941.84×
5k sparse (16384 px, 15.8 walls in box)thin (today)0.0831622411.00×
thin + 5.8 filter per query0.2371622412.84×
thick 3 faces, per query0.6993142198.37×
thick 4 faces, per query0.94341021711.30×
thick 4 faces, hoisted0.2634102173.15×
5k dense (4096 px, 206 walls in box)thin (today)1.2541332541.00×
thin + 5.8 filter per query1.3991332541.12×
thick 3 faces, per query7.8513724266.26×
thick 4 faces, per query13.22949762110.55×
thick 4 faces, hoisted12.72149762110.15×
+ +

Read: on the realistic (sparse) density a hoisted quad costs 0.26 ms per polygon — +inside the 1 ms target, and it reproduces the 0.257 ms measured for #39. The dense stress layout is 10× over and stays +over for every exact variant. Two costs hide in the numbers: the 5.8 filter is +0.15 ms per polygon at 5k walls +(because it scans every wall in the scene: 2.8× the sparse baseline, ≈ 0.9 ms for a 6-token party — the whole +per-move budget), and the 3-face silhouette is observer-dependent so it can never be hoisted. Raw output: +cost.txt.

+ +

Surprises and open questions (for #44 and #45)

+ + + +

Not done (do not read the pictures for these)

+ + +

Rebuild

+
+nim c -r prototype-wall-sight/pictures.nim          # rewrites img/*.svg
+nim c -d:release -r prototype-wall-sight/bench_thick.nim   # rewrites the table above (cost.txt)
+
+

Files: sightmodel.nim (filter, thickness expansion, height predicate), +pictures.nim (scenarios + SVG), bench_thick.nim (cost), cost.txt (raw run).

+ + + diff --git a/prototype-wall-sight/pictures.nim b/prototype-wall-sight/pictures.nim new file mode 100644 index 0000000..7abbcd2 --- /dev/null +++ b/prototype-wall-sight/pictures.nim @@ -0,0 +1,340 @@ +## PROTOTYPE (#43) - THROWAWAY. Renders one SVG per scenario of the wall-sight question: +## thick walls, a corner joint, a doorway in a thick wall, a 1 m wall seen by two viewers, +## a terrain wall vs no wall, and a one-way wall from both sides. +## +## Run from the repo root: nim c -r prototype-wall-sight/pictures.nim +## Output: prototype-wall-sight/img/*.svg (open prototype-wall-sight/index.html) +import std/[math, strutils, os, algorithm, monotimes, times] +import sightmodel, geometry + +const + PW = 480.0 # panel width in world px (1 world px = 1 svg unit) + PH = 360.0 + GAP = 40.0 + TOP = 30.0 + +type + Tok = object + pos: Vec + h: float + name: string + Panel = object + title, note: string + walls: seq[SightWall] + eye: Vec + eyeH: float + radius: float + cell: float + mode: ThickMode + toks: seq[Tok] + alt: seq[SightWall] ## non-empty: also draw this wall list's polygon (dashed) + +proc f1(v: float): string = formatFloat(v, ffDecimal, 1) +proc esc(s: string): string = + s.replace("&", "&").replace("<", "<").replace(">", ">") + +proc rectSvg(x, y, w, h: float, fill: string): string = + "\n" + +proc lineSvg(a, b: Vec, col: string, w: float, extra = ""): string = + "\n" + +proc textSvg(p: Vec, size: float, col: string, s: string, anchor = "start"): string = + "" & esc(s) & "\n" + +proc ptsSvg(ps: seq[Vec]): string = + for i, p in ps: + if i > 0: result.add " " + result.add f1(p.x) & "," & f1(p.y) + +# ---------------------------------------------------------------- per-panel drawing +proc wallColour(w: SightWall): (string, string) = + ## (body, label) colour by preset - display only, the rules never read `label` + if w.label.contains("door"): ("#a45f26", "#ffd7a8") + elif w.label.contains("terrain"): ("#4b6b35", "#d8e8c8") + elif w.label.contains("one-way"): ("#4b3f8f", "#d8d2f5") + else: ("#2b303a", "#c9d1d9") + +proc wallSvg(w: SightWall): string = + let (body, lab) = wallColour(w) + let c = rectCorners(w) + if w.door and w.open: + result.add "\n" + result.add lineSvg(w.a, w.b, "#ffffff", 0.7, "stroke-opacity=\"0.35\" stroke-dasharray=\"4 3\"") + let mid: Vec = ((w.a.x + w.b.x) / 2, (w.a.y + w.b.y) / 2) + result.add textSvg((mid.x + 4, mid.y - 6), 9, lab, w.label & " (open)") + return + if c.len == 4: + result.add "\n" + else: + result.add lineSvg(w.a, w.b, body, 3.0, "stroke-linecap=\"round\"") + # the zero-width centre line the same wall is today (comparison aid) + result.add lineSvg(w.a, w.b, "#ffffff", 0.7, "stroke-opacity=\"0.35\" stroke-dasharray=\"4 3\"") + let mid: Vec = ((w.a.x + w.b.x) / 2, (w.a.y + w.b.y) / 2) + result.add textSvg((mid.x + 4, mid.y - 5), 9, lab, w.label) + +proc fieldSvg(p: Panel, poly: seq[Vec], altPoly: seq[Vec]): string = + ## Floor visibility at 5 px: light = visible, salmon = inside the sight polygon but hidden by a + ## low wall's height (the band a 2D polygon cannot express). Runs are merged along x. + const step = 5.0 + let nx = int(PW / step) + let ny = int(PH / step) + for j in 0 ..< ny: + var runStart = -1 + var runCol = "" + for i in 0 .. nx: + var col = "" + if i < nx: + let pt: Vec = (i.float * step + step / 2, j.float * step + step / 2) + if hypot(pt.x - p.eye.x, pt.y - p.eye.y) > p.radius: col = "" + elif visible3D(p.walls, p.eye, p.eyeH, pt, 0.0): col = "#f2ead7" # floor visible to the eye + elif inside(poly, pt) or (altPoly.len > 2 and inside(altPoly, pt)): + col = "#d98e73" # fog reveals, eye cannot see + if col != runCol: + if runCol != "": + result.add rectSvg(runStart.float * step, j.float * step, (i - runStart).float * step + 0.6, step + 0.6, runCol) + runStart = i + runCol = col + discard + +proc kindOf(p: Panel, poly, altPoly: seq[Vec], pos: Vec, h: float): int = + let d = hypot(pos.x - p.eye.x, pos.y - p.eye.y) + if d <= p.radius and visible3D(p.walls, p.eye, p.eyeH, pos, h): return 2 + if inside(poly, pos) or (altPoly.len > 2 and d <= p.radius and inside(altPoly, pos)): return 1 + 0 + +proc tokColour(kind: int): (string, string) = + case kind + of 2: ("#2ea043", "#0b0e12") # visible + of 1: ("#d93025", "#ffffff") # inside fog polygon but hidden by height + else: ("#6e7681", "#0b0e12") # fog + +proc panelSvg(p: Panel, ox, oy: float, id: int): string = + let poly = sightPolygon(p.eye, p.walls, p.radius, p.mode) + let altPoly = if p.alt.len > 0: sightPolygon(p.eye, p.alt, p.radius, p.mode) else: @[] + let cells = cellsTouched(poly, p.cell) + result.add "\n" + result.add textSvg((0.0, -10.0), 12, "#c9d1d9", p.title) + result.add "\n" + result.add "\n" + result.add rectSvg(0, 0, PW, PH, "#10141a") + for (cx, cy) in cells: + result.add rectSvg(cx.float * p.cell, cy.float * p.cell, p.cell, p.cell, "#33404f") + result.add fieldSvg(p, poly, altPoly) + result.add "\n" + if p.alt.len > 0: + result.add "\n" + for w in p.walls: result.add wallSvg(w) + for t in p.toks: + let kind = kindOf(p, poly, altPoly, t.pos, t.h) + let (fill, ink) = tokColour(kind) + result.add "\n" + result.add textSvg((t.pos.x, t.pos.y + 3.2), 8, ink, $t.h, "middle") + result.add textSvg((t.pos.x + 12, t.pos.y + 3.5), 9, "#c9d1d9", t.name) + result.add "\n" + result.add textSvg((p.eye.x + 11, p.eye.y + 3.5), 9, "#58a6ff", "eye " & f1(p.eyeH) & " m") + result.add "\n" + result.add textSvg((0.0, PH + 16.0), 10, "#8b949e", p.note) + result.add "\n" + +proc writeSvg(path: string, panels: seq[Panel]) = + let n = panels.len + let w = GAP + n.float * (PW + GAP) + let h = TOP + PH + GAP + 60 + var s = "\n" + s.add "\n" + s.add rectSvg(0, 0, w, h, "#05070a") + for i, p in panels: + s.add panelSvg(p, GAP + i.float * (PW + GAP), TOP, i) + s.add "\n" + s.add rectSvg(0, -9, 14, 10, "#f2ead7") + s.add textSvg((18.0, 0.0), 10, "#8b949e", "floor the eye sees") + s.add rectSvg(200, -9, 14, 10, "#d98e73") + s.add textSvg((218.0, 0.0), 10, "#8b949e", "fog would reveal it, the eye does not see it") + s.add rectSvg(0, 6, 14, 10, "#33404f") + s.add textSvg((18.0, 15.0), 10, "#8b949e", "explored cell (memory, no current sight)") + s.add textSvg((520.0, 0.0), 10, "#4f9cf9", "solid blue = today's sight polygon") + s.add textSvg((520.0, 15.0), 10, "#58a6ff", "dashed blue = low walls ignored (family A), scenario 4 only") + s.add "\n" + s.add "\n" + writeFile(path, s) + echo "wrote ", path, " (", s.len div 1024, " KiB)" + +# ---------------------------------------------------------------- diagnostics +proc polyDiffCell(a, b: seq[Vec], cell: float): (int, int, int, int) = + ## (cellsA, cellsB, gained, lost) + let ca = cellsTouched(a, cell) + let cb = cellsTouched(b, cell) + func toS(cs: seq[(int,int)]): seq[string] = + result = @[] + for c in cs: result.add $c[0] & "," & $c[1] + let sa = toS(ca); let sb = toS(cb) + var gained, lost = 0 + for c in sa: + if c notin sb: inc lost + for c in sb: + if c notin sa: inc gained + (sa.len, sb.len, gained, lost) + +proc samePoly(a, b: seq[Vec]): bool = + if a.len != b.len: return false + for i in 0 ..< a.len: + if abs(a[i].x - b[i].x) > 1e-9 or abs(a[i].y - b[i].y) > 1e-9: return false + true + +proc tokTable(label: string, p: Panel) = + let poly = sightPolygon(p.eye, p.walls, p.radius, p.mode) + let altPoly = if p.alt.len > 0: sightPolygon(p.eye, p.alt, p.radius, p.mode) else: @[] + for t in p.toks: + let k = kindOf(p, poly, altPoly, t.pos, t.h) + echo " ", label, " | ", t.name, " h=", t.h, " -> ", (if k == 2: "VISIBLE" elif k == 1: "hidden by height" else: "fog") + +# ---------------------------------------------------------------- scenarios +proc s1(outdir: string) = + let base = @[solid(140, 60, 140, 300)] + var a: Panel + a.title = "1a thin wall (t = 0 px, today)" + a.note = "polygon reaches the line; explored cells hug it" + a.walls = base; a.eye = (70, 180); a.eyeH = eyeOf(1.8); a.radius = 300; a.cell = 32; a.mode = tmThin + a.toks = @[Tok(pos: (330, 90), h: 1.8, name: "T1"), Tok(pos: (330, 180), h: 1.8, name: "T2"), + Tok(pos: (330, 270), h: 1.8, name: "T3")] + + var b = a + b.title = "1b thick wall (t = 14 px, 3 faces)" + b.note = "body hidden, shadow shifted out by t/2; same tokens, fewer cells" + b.walls = @[thick(solid(140, 60, 140, 300), 14)] + b.mode = tmSilhouette + writeSvg(outdir / "s1-thin-vs-thick.svg", @[a, b]) + let (n1, n2, gained, lost) = polyDiffCell(sightPolygon(a.eye, a.walls, a.radius, tmThin), + sightPolygon(b.eye, b.walls, b.radius, tmSilhouette), 32) + echo "s1 explored cells: thin=", n1, " thick=", n2, " gained=", gained, " lost=", lost + +proc s2(outdir: string) = + let base = @[solid(160, 80, 160, 260), solid(160, 260, 340, 260)] + var a: Panel + a.title = "2a inside corner, thin walls" + a.note = "sight reaches the shared vertex exactly" + a.walls = base; a.eye = (95, 170); a.eyeH = eyeOf(1.8); a.radius = 300; a.cell = 32; a.mode = tmThin + a.toks = @[Tok(pos: (280, 120), h: 1.8, name: "T1"), Tok(pos: (280, 210), h: 1.8, name: "T2"), + Tok(pos: (120, 310), h: 1.8, name: "T3")] + + var b = a + b.title = "2b same corner, thick walls (t = 14)" + b.note = "bodies overlap at the joint; corner cell lost, no crack either way" + b.walls = @[thick(solid(160, 80, 160, 260), 14), thick(solid(160, 260, 340, 260), 14)] + b.mode = tmSilhouette + writeSvg(outdir / "s2-corner.svg", @[a, b]) + let (n1, n2, gained, lost) = polyDiffCell(sightPolygon(a.eye, a.walls, a.radius, tmThin), + sightPolygon(b.eye, b.walls, b.radius, tmSilhouette), 32) + echo "s2 explored cells: thin=", n1, " thick=", n2, " gained=", gained, " lost=", lost + +proc s3(outdir: string) = + const t = 14.0 + # a doorway = a gap in two solid walls + a door segment spanning it + let shell = @[thick(solid(60, 230, 210, 230), t), thick(solid(300, 230, 430, 230), t)] + var closed: Panel + closed.title = "3a thick wall, door CLOSED (viewer at 30 deg)" + closed.note = "door body + walls; opening is a slot of depth t" + closed.walls = shell & @[thick(doorW(210, 230, 300, 230, open = false), t)] + closed.eye = (130, 340); closed.eyeH = eyeOf(1.8); closed.radius = 400; closed.cell = 32 + closed.mode = tmSilhouette + closed.toks = @[Tok(pos: (350, 80), h: 1.8, name: "A"), Tok(pos: (450, 120), h: 1.8, name: "B"), + Tok(pos: (280, 60), h: 1.8, name: "C")] + + var open = closed + open.title = "3b same wall, door OPEN" + open.note = "door absent from geometry; the deep slot still clips an oblique view" + open.walls = shell & @[thick(doorW(210, 230, 300, 230, open = true), t)] + writeSvg(outdir / "s3-door-in-thick-wall.svg", @[closed, open]) + let pc = sightPolygon(closed.eye, closed.walls, closed.radius, tmSilhouette).len + let po = sightPolygon(open.eye, open.walls, open.radius, tmSilhouette).len + echo "s3 polygon vertices: closed=", pc, " open=", po + tokTable("3a closed", closed) + tokTable("3b open", open) + +proc s4(outdir: string) = + const t = 12.0 + const wallH = 1.0 + let walls = @[low(thick(solid(110, 250, 390, 250), t), wallH)] + let toks = @[Tok(pos: (240, 195), h: 1.8, name: "1.8 near"), + Tok(pos: (255, 205), h: 0.3, name: "0.3 near"), + Tok(pos: (310, 215), h: 0.1, name: "0.1 near"), + Tok(pos: (330, 175), h: 1.0, name: "1.0 mid"), + Tok(pos: (340, 110), h: 0.3, name: "0.3 far"), + Tok(pos: (200, 80), h: 1.8, name: "1.8 far")] + + var a: Panel + a.title = "4a low wall 1.0 m, tall viewer 1.8 m (eye 1.67)" + a.note = "paper = eye sees floor; salmon = fog reveals it but the eye does not" + a.walls = walls; a.eye = (200, 320); a.eyeH = eyeOf(1.8); a.radius = 330; a.cell = 32 + a.mode = tmSilhouette; a.toks = toks + + var perm = walls[0] + perm.blocksSight = false + a.alt = @[perm] + + var b = a + b.title = "4b same wall, short viewer 1.0 m (eye 0.93)" + b.note = "eye 0.93 m < wall 1.0 m: no floor see-over anywhere; only tall targets' tops" + b.eyeH = eyeOf(1.0) + writeSvg(outdir / "s4-low-wall.svg", @[a, b]) + let sDist = 320.0 - 250.0 + for (lab, eh) in [("4a tall", eyeOf(1.8)), ("4b short", eyeOf(1.0))]: + if eh > wallH: + echo " ", lab, " d_floor = s*h_eye/(h_eye-h_wall) = ", formatFloat(sDist * eh / (eh - wallH), ffDecimal, 0), + " px behind the eye (wall is at s = ", formatFloat(sDist, ffDecimal, 0), " px)" + else: + echo " ", lab, ": eye ", formatFloat(eh, ffDecimal, 2), " m <= wall ", wallH, + " m -> floor never visible behind the wall (no d_floor); only target tops taller than the wall can clear it" + tokTable("4a tall", a) + tokTable("4b short", b) + +proc s5(outdir: string) = + let none: seq[SightWall] = @[] + let terr = @[thick(terrain(240, 80, 240, 300), 14)] + var a: Panel + a.title = "5a no wall" + a.note = "reference polygon and explored set" + a.walls = none; a.eye = (130, 190); a.eyeH = eyeOf(1.8); a.radius = 300; a.cell = 32; a.mode = tmSilhouette + a.toks = @[Tok(pos: (350, 140), h: 1.8, name: "T1"), Tok(pos: (350, 250), h: 1.8, name: "T2")] + + var b = a + b.title = "5b terrain wall (blocks_movement only, t = 14)" + b.note = "identical picture: blocks_sight off = zero polygon change, thickness included" + b.walls = terr + writeSvg(outdir / "s5-terrain.svg", @[a, b]) + let pa = sightPolygon(a.eye, a.walls, a.radius, tmSilhouette) + let pb = sightPolygon(b.eye, b.walls, b.radius, tmSilhouette) + let ca = cellsTouched(pa, 32) + let cb = cellsTouched(pb, 32) + echo "s5 terrain vs no wall: polygon identical=", samePoly(pa, pb), " vertices=", pa.len, + " cells=", ca.len, "/", cb.len + +proc s6(outdir: string) = + let w = @[thick(oneWay(240, 80, 240, 300, sdLeft), 12)] + let toks = @[Tok(pos: (150, 140), h: 1.8, name: "west 1"), Tok(pos: (150, 250), h: 1.8, name: "west 2"), + Tok(pos: (350, 140), h: 1.8, name: "east 1"), Tok(pos: (350, 250), h: 1.8, name: "east 2")] + var a: Panel + a.title = "6a one-way (sight_dir = left), viewer WEST of a->b" + a.note = "west is the blocked side: polygon stops at the wall, east tokens stay fogged" + a.walls = w; a.eye = (130, 190); a.eyeH = eyeOf(1.8); a.radius = 320; a.cell = 32 + a.mode = tmSilhouette; a.toks = toks + var b = a + b.title = "6b same wall, viewer EAST" + b.note = "east is the unblocked side: the wall is absent from this observer's geometry" + b.eye = (350, 190) + writeSvg(outdir / "s6-one-way.svg", @[a, b]) + tokTable("6a west", a) + tokTable("6b east", b) + +when isMainModule: + let outdir = currentSourcePath().parentDir / "img" + createDir(outdir) + s1(outdir); s2(outdir); s3(outdir); s4(outdir); s5(outdir); s6(outdir) diff --git a/prototype-wall-sight/sightmodel.nim b/prototype-wall-sight/sightmodel.nim new file mode 100644 index 0000000..5af7fa7 --- /dev/null +++ b/prototype-wall-sight/sightmodel.nim @@ -0,0 +1,174 @@ +## PROTOTYPE (#43) - THROWAWAY, not production code, do not fold back as is. +## +## The sight *input* model for walls with thickness and height, wrapped around server/geometry.nim +## unchanged: geometry never learns a kind, it only ever receives segments. +## +## Rule order is the one DESIGN 5.8 fixes: the switches choose WHICH walls reach the geometry +## (blocks_sight off -> absent; open door -> absent; one-way -> absent for the unblocked side), and +## only after that filter does thickness/hight shape the segment list a wall contributes. +## +## Height is NOT geometry here (research #39 3.3): a low wall's "hidden band, then visible again" +## cannot be a nearest-hit polygon, so height is a per-target predicate (`visible3D`) and the +## polygon only ever carries full-height occlusion. +import std/math +import geometry + +const FullHeight* = 1.0e9 ## "full height": nobody sees over it (DESIGN 5.7) + +type + Side* = enum sdBoth, sdLeft, sdRight + ThickMode* = enum tmThin, tmSilhouette, tmQuad + ## tmThin: today's zero-width segment. tmSilhouette: near face + 2 end edges (3 faces, exact + ## occlusion for a viewer on the near side, but viewer-dependent). tmQuad: 4 faces, exact and + ## viewer-independent (hoistable once per wall edit). + Seg* = tuple[a, b: Vec] + + SightWall* = object + a*, b*: Vec + blocksSight*: bool + blocksMovement*: bool + door*, open*: bool + thickness*: float ## px, half on each side; 0 = zero-width + height*: float ## metres; FullHeight = never see over + sightDir*: Side + label*: string ## display only, never read by the rules + +proc vadd(a, b: Vec): Vec = (a.x + b.x, a.y + b.y) +proc vmul(a: Vec, k: float): Vec = (a.x * k, a.y * k) +proc vsub(a, b: Vec): Vec = (a.x - b.x, a.y - b.y) +proc vlen(a: Vec): float = hypot(a.x, a.y) +proc vnorm(a: Vec): Vec = (a.x / vlen(a), a.y / vlen(a)) + +# ---------------------------------------------------------------- presets (DESIGN 5.8 table) +proc solid*(ax, ay, bx, by: float): SightWall = + SightWall(a: (ax, ay), b: (bx, by), blocksSight: true, blocksMovement: true, + sightDir: sdBoth, height: FullHeight, label: "wall") +proc doorW*(ax, ay, bx, by: float, open = false): SightWall = + SightWall(a: (ax, ay), b: (bx, by), blocksSight: true, blocksMovement: true, + door: true, open: open, sightDir: sdBoth, height: FullHeight, label: "door") +proc terrain*(ax, ay, bx, by: float): SightWall = + SightWall(a: (ax, ay), b: (bx, by), blocksSight: false, blocksMovement: true, + sightDir: sdBoth, height: FullHeight, label: "terrain") +proc oneWay*(ax, ay, bx, by: float, dir: Side): SightWall = + SightWall(a: (ax, ay), b: (bx, by), blocksSight: true, blocksMovement: true, + sightDir: dir, height: FullHeight, label: "one-way") +proc low*(w: SightWall, h: float): SightWall = + result = w + result.height = h + result.label = w.label & " " & $h & " m" +proc thick*(w: SightWall, t: float): SightWall = + result = w + result.thickness = t + result.label = w.label & " t=" & $t + +# ---------------------------------------------------------------- filter (DESIGN 5.8, before geometry) +proc sideOf*(w: SightWall, o: Vec): int = + ## +1 = o left of a->b, -1 = right. (Left/right are the a->b sides, 5.8.) + let s = cross(w.b - w.a, o - w.a) + if s > 1e-9: 1 elif s < -1e-9: -1 else: 0 + +proc sightPasses*(w: SightWall, o: Vec): bool = + ## The whole kind logic of the prototype: switches only, no kind field. A wall that fails this + ## is absent from the vision input entirely, thickness and height included. + if not w.blocksSight: return false # terrain: zero polygon change + if w.door and w.open: return false # open door never blocks + if w.sightDir != sdBoth: + # PROTOTYPE reading of 5.8: `left` names the side whose view across the wall is blocked -> + # the wall is IN the geometry for an observer on that side, absent for the other one. + # (The wording is ambiguous, see index.html surprises.) + let s = sideOf(w, o) + if w.sightDir == sdLeft and s < 0: return false + if w.sightDir == sdRight and s > 0: return false + true + +# ---------------------------------------------------------------- thickness -> segments +proc faces*(w: SightWall, mode: ThickMode, o: Vec): seq[Seg] = + ## The segments a wall contributes to the ray fan. For thickness 0 every mode degenerates to the + ## one zero-width segment, so the thin baseline is exactly today's geometry. + let e = w.b - w.a + if w.thickness <= 0 or vlen(e) < 1e-9: + return @[(w.a, w.b)] + let n = (-e.y / vlen(e), e.x / vlen(e)) # left normal + let h = w.thickness / 2 + let sgn = if sideOf(w, o) >= 0: 1.0 else: -1.0 + let nn = vmul(n, h * sgn) + let p1 = vadd(w.a, nn) # near face: a->b offset toward the viewer + let p2 = vadd(w.b, nn) + let q1 = vsub(w.a, nn) # far face + let q2 = vsub(w.b, nn) + case mode + of tmThin: result = @[(w.a, w.b)] + of tmSilhouette: result = @[(p1, p2), (p1, q1), (p2, q2)] + of tmQuad: result = @[(p1, p2), (p1, q1), (p2, q2), (q1, q2)] + +proc rectCorners*(w: SightWall): seq[Vec] = + ## The wall body as a closed polygon (drawing only; the predicate uses `rectEdges`). + let e = w.b - w.a + if w.thickness <= 0 or vlen(e) < 1e-9: return @[w.a, w.b] + let n = (-e.y / vlen(e), e.x / vlen(e)) + let h = w.thickness / 2 + let p1 = vadd(w.a, vmul(n, h)); let p2 = vadd(w.b, vmul(n, h)) + let q1 = vsub(w.a, vmul(n, h)); let q2 = vsub(w.b, vmul(n, h)) + @[p1, p2, q2, q1] + +proc rectEdges*(w: SightWall): seq[Seg] = + ## The wall body for the per-target predicate: always all 4 edges (a crossing cannot be missed by + ## picking the wrong side). Thickness 0 -> the segment itself. + let e = w.b - w.a + if w.thickness <= 0 or vlen(e) < 1e-9: return @[(w.a, w.b)] + let n = (-e.y / vlen(e), e.x / vlen(e)) + let h = w.thickness / 2 + let p1 = vadd(w.a, vmul(n, h)); let p2 = vadd(w.b, vmul(n, h)) + let q1 = vsub(w.a, vmul(n, h)); let q2 = vsub(w.b, vmul(n, h)) + @[(p1, p2), (p1, q1), (p2, q2), (q1, q2)] + +# ---------------------------------------------------------------- geometry (server/geometry.nim) +proc toWalls*(segs: seq[Seg]): seq[Wall] = + for s in segs: result.add Wall(a: s.a, b: s.b, door: false, open: false) + +proc sightSegments*(walls: openArray[SightWall], o: Vec, mode: ThickMode): seq[Seg] = + for w in walls: + if not sightPasses(w, o): continue + result.add w.faces(mode, o) + +proc sightPolygon*(o: Vec, walls: openArray[SightWall], radius: float, + mode = tmThin, circleSegs = 64): seq[Vec] = + ## Exactly server/geometry.visibilityPolygon over the filtered, expanded segment list. + visibilityPolygon(o, toWalls(sightSegments(walls, o, mode)), radius, circleSegs) + +# ---------------------------------------------------------------- height: per-target predicate +proc rayT(o, p, a, b: Vec): float = + ## parameter t in (0,1) where o->p crosses segment ab, else -1 + let d = vsub(p, o); let e = vsub(b, a) + let den = cross(d, e) + if abs(den) < 1e-12: return -1 + let ao = vsub(a, o) + let t = cross(ao, e) / den + let u = cross(ao, d) / den + if t > 1e-9 and t < 1 - 1e-9 and u >= -1e-9 and u <= 1 + 1e-9: t else: -1 + +proc visible3D*(walls: openArray[SightWall], o: Vec, eyeH: float, p: Vec, targetH: float): bool = + ## Exact 3D line of sight from an eye at `eyeH` to the TOP of a target at `targetH` (metres). + ## A wall blocks when the sight line crosses it at or below the wall's top: + ## height at crossing t = eyeH + t*(targetH - eyeH) <= wall.height -> blocked. + ## targetH = 0 gives the floor-reveal question (the d_floor band of research #39 3.3). + for w in walls: + if not sightPasses(w, o): continue + for s in w.rectEdges(): + let t = rayT(o, p, s.a, s.b) + if t > 0: + let hline = eyeH + t * (targetH - eyeH) + if hline <= w.height: return false + true + +proc howSeen*(walls: openArray[SightWall], poly: openArray[Vec], o: Vec, eyeH: float, + p: Vec, targetH: float, radius = 1e9): int = + ## 0 = fog (outside the sight polygon and not seen either), 1 = inside the fog polygon but the + ## eye cannot see it (hidden by height: fog reveals what you cannot see), 2 = visible to the eye. + ## The 2/1 order matters for low walls: the permissive 3D answer wins, and the difference from the + ## strict polygon is exactly the band research #39 3.3 describes. + if hypot(p.x - o.x, p.y - o.y) <= radius and visible3D(walls, o, eyeH, p, targetH): return 2 + if inside(poly, p): return 1 + 0 + +proc eyeOf*(tokenHeight: float): float = 0.93 * tokenHeight ## DESIGN 5.7 default