geometry: scanline cellsTouched, differential + independent oracle tests, prototype corpus

cellsTouched is now a scanline over cell rows (prototype per-cell version kept test-only in
tests/refold.nim). Differential tests (random polygons, concave, grid-aligned) and a brute-force
explored oracle (0.5 px tolerance) cover it; regression corpus = prototype leak-run polygons.
moveBlocked leaves geometry.nim (replaced by server/moverules.nim next); geometry primitives exported.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 12:08:21 +01:00
co-authored by Claude Sonnet 5.5
parent 206f99deda
commit 67fa96e23d
5 changed files with 667 additions and 114 deletions
+41 -28
View File
@@ -17,13 +17,13 @@ type
door*, open*: bool
const
Eps = 1e-9 # geometric tolerance (px)
Eps* = 1e-9 # geometric tolerance (px)
RayEps = 1e-4 # angular nudge past endpoints (rad)
proc blocks*(w: Wall): bool = not (w.door and w.open)
proc cross(a, b: Vec): float = a.x * b.y - a.y * b.x
proc `-`(a, b: Vec): Vec = (a.x - b.x, a.y - b.y)
proc cross*(a, b: Vec): float = a.x * b.y - a.y * b.x
proc `-`*(a, b: Vec): Vec = (a.x - b.x, a.y - b.y)
proc rayHit(o, d: Vec, a, b: Vec): float =
## smallest t > Eps where o + t*d hits segment ab (end-inclusive), else Inf
@@ -65,7 +65,7 @@ proc visibilityPolygon*(o: Vec, walls: openArray[Wall], radius: float,
for s in segs: best = min(best, rayHit(o, d, s[0], s[1]))
if best < Inf: result.add((o.x + d.x * best, o.y + d.y * best))
proc segsCross(p, q, a, b: Vec): bool =
proc segsCross*(p, q, a, b: Vec): bool =
## inclusive segment intersection (touching and collinear overlap count)
proc orient(a, b, c: Vec): int =
let v = cross(b - a, c - a)
@@ -79,20 +79,12 @@ proc segsCross(p, q, a, b: Vec): bool =
(o1 == 0 and onSeg(p, q, a)) or (o2 == 0 and onSeg(p, q, b)) or
(o3 == 0 and onSeg(a, b, p)) or (o4 == 0 and onSeg(a, b, q))
proc segDist(p, a, b: Vec): float =
proc segDist*(p, a, b: Vec): float =
let e = b - a
let l2 = e.x * e.x + e.y * e.y
let t = if l2 < Eps: 0.0 else: clamp(((p.x - a.x) * e.x + (p.y - a.y) * e.y) / l2, 0.0, 1.0)
hypot(p.x - a.x - t * e.x, p.y - a.y - t * e.y)
proc moveBlocked*(walls: openArray[Wall], a, b: Vec, r = 0.0): bool =
## true if travelling a->b touches any blocking wall (touching counts: no leaking through shared vertices)
## or the token circle (radius r) at b overlaps one. Only b is checked for clearance, so a token
## already overlapping (legacy position, door closed on it) can still leave to a clear spot.
## shortcut: a token standing exactly on a wall can't move off it; server must keep tokens off walls.
for w in walls:
if w.blocks and (segsCross(a, b, w.a, w.b) or segDist(b, w.a, w.b) < r): return true
proc inside*(poly: openArray[Vec], p: Vec): bool =
## even-odd point-in-polygon (boundary ambiguous)
var j = poly.len - 1
@@ -103,23 +95,44 @@ proc inside*(poly: openArray[Vec], p: Vec): bool =
j = i
proc cellsTouched*(poly: openArray[Vec], cell: float): seq[(int, int)] =
## grid cells (ix, iy) overlapped by the polygon (area overlap, boundary touch included), sorted.
## grid cells (ix, iy) overlapped by the polygon (area overlap; boundary touch within Eps included, except along the bbox minimum lines, see below), sorted by (iy, ix).
## Scanline over the cell rows, O(edges x rows spanned + cells). Per row band [by0, by1] the part of the polygon in
## the band projects on x as (a) the clipped extent of every edge plus (b) the even-odd fill spans at the band's
## lower line (a vertical line in the band that meets no edge is wholly inside or wholly outside, and inside iff
## inside at by0). Differential-tested against the prototype per-cell version (tests/refold.nim).
if poly.len < 3: return
var x0, y0 = Inf
var x1, y1 = -Inf
for p in poly:
x0 = min(x0, p.x); x1 = max(x1, p.x); y0 = min(y0, p.y); y1 = max(y1, p.y)
for iy in int(floor(y0 / cell)) .. int(floor(y1 / cell)):
for ix in int(floor(x0 / cell)) .. int(floor(x1 / cell)):
let (cx0, cy0, cx1, cy1) = (ix.float * cell, iy.float * cell, (ix + 1).float * cell, (iy + 1).float * cell)
let c = [(cx0, cy0), (cx1, cy0), (cx1, cy1), (cx0, cy1)]
# no edge crosses => cell wholly inside poly (a corner decides) or poly wholly inside cell (a vertex decides) or disjoint
var hit = inside(poly, c[0]) or
(poly[0].x >= cx0 and poly[0].x <= cx1 and poly[0].y >= cy0 and poly[0].y <= cy1)
if not hit:
for i in 0 ..< poly.len:
let p = poly[i]; let q = poly[(i + 1) mod poly.len]
for k in 0 .. 3:
if segsCross(p, q, c[k], c[(k + 1) mod 4]): hit = true; break
if hit: break
if hit: result.add (ix, iy)
# closed touch: a value exactly on (or within Eps of) a cell line belongs to both neighbours
proc lo(v, cell: float): int = int(ceil((v - Eps) / cell)) - 1
proc hi(v, cell: float): int = int(floor((v + Eps) / cell))
# prototype-compatible quirk: the grid starts at floor(bbox min), so a cell touching only the bbox minimum line is
# not marked (the regression corpus has such polygons: walls on x = 256). Every other touch is inclusive.
let (ix0, ix1, iy0, iy1) = (int(floor(x0 / cell)), hi(x1, cell), int(floor(y0 / cell)), hi(y1, cell))
let nx = ix1 - ix0 + 1
var hit = newSeq[bool](nx * (iy1 - iy0 + 1))
var xs = newSeq[seq[float]](iy1 - iy0 + 1) # fill crossings per row at the row's lower line
proc mark(row: int, xa, xb: float) =
for ix in max(lo(xa, cell), ix0) .. min(hi(xb, cell), ix1): hit[row * nx + ix - ix0] = true
for i in 0 ..< poly.len:
let p = poly[i]; let q = poly[(i + 1) mod poly.len]
let (ylo, yhi) = (min(p.y, q.y), max(p.y, q.y))
let (exlo, exhi) = (min(p.x, q.x), max(p.x, q.x))
proc xAt(y: float): float =
if p.y == q.y: exlo else: clamp(p.x + (y - p.y) * (q.x - p.x) / (q.y - p.y), exlo, exhi)
for iy in max(lo(ylo, cell), iy0) .. min(hi(yhi, cell), iy1):
let by0 = iy.float * cell
let row = iy - iy0
if p.y == q.y: mark(row, exlo, exhi)
else:
let (a, b) = (xAt(max(ylo, by0)), xAt(min(yhi, by0 + cell)))
mark(row, min(a, b), max(a, b))
if (p.y > by0) != (q.y > by0): xs[row].add xAt(by0)
for row in 0 ..< xs.len:
xs[row].sort
for k in countup(0, xs[row].len - 2, 2): mark(row, xs[row][k], xs[row][k + 1])
for row in 0 ..< xs.len:
for c in 0 ..< nx:
if hit[row * nx + c]: result.add (ix0 + c, iy0 + row)