## 2D vision geometry. Pure, stdlib only. Ported from prototype-server-vision/src/geometry.nim. ## Known limitations (see prototype-server-vision/HANDOFF.md, docs/DESIGN.md 5.4 / 12): ## - token exactly on a wall cannot move off it (inclusive segsCross); pinned by a KNOWN LIMITATION test. ## - closing a door on a token: policy open, DESIGN Q14 (5.4 vs H2 conflict); behaviour pinned in tests. ## - boundary epsilon ambiguity: RayEps 1e-4 rad, inclusive tests; token on a vision boundary is ambiguous ## (leak checker uses a 0.5 px neighbourhood). ## - move collision = centre path a->b + destination circle only, NOT the swept circle (corner clipping possible). ## - vision is O(rays x walls) per token, bbox cull only. ## - cellsTouched costs ~12x a polygon (results/bench.log in the prototype). ## - circle range limit is an inscribed 64-gon: error <= ~0.12% of R. import std/[math, algorithm] type Vec* = tuple[x, y: float] Wall* = object a*, b*: Vec door*, open*: bool const 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 rayHit(o, d: Vec, a, b: Vec): float = ## smallest t > Eps where o + t*d hits segment ab (end-inclusive), else Inf let e = b - a let den = cross(d, e) if abs(den) < 1e-12: return Inf # parallel/collinear: grazing, other rays cover it 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 visibilityPolygon*(o: Vec, walls: openArray[Wall], radius: float, circleSegs = 64): seq[Vec] = ## CCW polygon of what `o` sees, bounded by an inscribed circleSegs-gon of `radius`. 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) # same (-pi,pi] range as the wall angles, else the sort scrambles the fan for w in walls: if not w.blocks: continue # shortcut: walls beyond the circle are only culled by ray clipping, O(rays*walls); upgrade: spatial grid 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 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 # token on endpoint let ang = arctan2(p.y - o.y, p.x - o.x) angles.add ang angles.add ang - RayEps angles.add ang + RayEps angles.sort 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])) if best < Inf: result.add((o.x + d.x * best, o.y + d.y * best)) 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) if v > Eps: 1 elif v < -Eps: -1 else: 0 proc onSeg(a, b, c: Vec): bool = # c collinear with ab: within bbox? c.x >= min(a.x, b.x) - Eps and c.x <= max(a.x, b.x) + Eps and c.y >= min(a.y, b.y) - Eps and c.y <= max(a.y, b.y) + Eps let o1 = orient(p, q, a); let o2 = orient(p, q, b) let o3 = orient(a, b, p); let o4 = orient(a, b, q) if o1 != o2 and o3 != o4: return true (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 = 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 for i in 0 ..< poly.len: if (poly[i].y > p.y) != (poly[j].y > p.y) and p.x < (poly[j].x - poly[i].x) * (p.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x: result = not result 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. 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)