File size: 5,352 Bytes
4eecd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
"""Roller path quality on synthetic walls with known geometry (plain, windows, door, L-shape, narrow gaps).

Metrics per wall: coverage of the paintable target by the true roller footprint, overlap waste (painted area counted
twice), keep-out intrusions (roller footprint on a window), travel between strokes as a share of path, lifts, and
upward moves (a pass started above the previous one: paint drips onto finished work).
    python eval_paths.py [--json out.json]
"""
from __future__ import annotations

import argparse
import json

import numpy as np

import robot_planner as rp

W, H, ROLLER, OVERLAP, MARGIN = 900, 600, 60, 0.2, 10


def walls():
    base = np.ones((H, W), bool)
    keep = lambda *boxes: np.pad(np.zeros((0, 0), bool), 0) if False else _boxes(boxes)
    out = {"plain": (base, np.zeros_like(base))}
    out["one window"] = (base, _boxes([(300, 150, 560, 350)]))
    out["two windows"] = (base, _boxes([(120, 150, 330, 360), (560, 150, 780, 360)]))
    out["windows+door"] = (base, _boxes([(120, 120, 300, 300), (380, 120, 560, 300), (700, 250, 860, 600)]))
    out["window grid 3x2"] = (base, _boxes([(90 + 270 * i, 90 + 250 * j, 230 + 270 * i, 260 + 250 * j) for i in range(3) for j in range(2)]))
    l = base.copy(); l[:300, 450:] = False
    out["L-shape"] = (l, _boxes([(100, 360, 300, 520)]))
    out["narrow gaps"] = (base, _boxes([(100, 100, 400, 500), (430, 100, 800, 500)]))
    return out


def _boxes(boxes):
    m = np.zeros((H, W), bool)
    for x0, y0, x1, y1 in boxes:
        m[y0:y1, x0:x1] = True
    return m


def true_footprint(segments, shape, width):
    """Rasterise each stroke as a width-wide band (exact for horizontal/vertical strokes), counting passes per pixel."""
    count = np.zeros(shape, np.int16)
    half = width / 2.0
    for seg in segments:
        (x0, y0), (x1, y1) = seg[0], seg[-1]
        ya, yb = int(round(min(y0, y1) - half)), int(round(max(y0, y1) + half))
        xa, xb = int(round(min(x0, x1) - half)), int(round(max(x0, x1) + half))
        count[max(0, ya):min(shape[0], yb), max(0, xa):min(shape[1], xb)] += 1
    return count


def evaluate(paintable, keepout, **kw):
    plan, dbg = rp.build_plan(paintable, keepout, ROLLER, OVERLAP, MARGIN, "horizontal", None, 5.0, 4.0, **kw)
    segs = plan["plan"]["strokes"]
    count = true_footprint(segs, paintable.shape, ROLLER)
    if kw.get("trim_width"):          # the trim tool's own strokes cover what the roller could not reach
        count += true_footprint(plan["plan"]["trim"]["strokes"], paintable.shape, kw["trim_width"])
    target = dbg["target"]
    cov = float((count[target] > 0).mean())
    painted = int((count > 0).sum())
    waste = float((count[count > 1] - 1).sum() / max(painted, 1))
    intrude = float(((count > 0) & keepout).sum() / max(keepout.sum(), 1))
    ups, lifts, travel, painted_len = 0, 0, 0.0, 0.0
    prev = None
    for s in segs:
        a, b = np.array(s[0], float), np.array(s[-1], float)
        painted_len += float(np.linalg.norm(b - a))
        if prev is not None:
            hop = float(np.linalg.norm(a - prev))
            travel += hop
            if hop > ROLLER * 1.5:
                lifts += 1
            if a[1] < prev[1] - ROLLER * 0.5:
                ups += 1
        prev = b
    # wet edge: time between two passes that overlap each other (adjacent rows, overlapping in x), at roller speed
    speed_mm_s = 300.0
    clock, t_start = 0.0, []
    prev_end = None
    for s_ in segs:
        a, b = np.array(s_[0], float), np.array(s_[-1], float)
        if prev_end is not None:
            clock += float(np.linalg.norm(a - prev_end)) * 5 / speed_mm_s
        t_start.append(clock)
        clock += float(np.linalg.norm(b - a)) * 5 / speed_mm_s
        prev_end = b
    gaps = []
    for i, si in enumerate(segs):
        for j in range(i):
            sj = segs[j]
            dy = abs(si[0][1] - sj[0][1])
            if 0 < dy <= ROLLER and min(max(si[0][0], si[-1][0]), max(sj[0][0], sj[-1][0])) > max(min(si[0][0], si[-1][0]), min(sj[0][0], sj[-1][0])):
                gaps.append(t_start[i] - (t_start[j] + abs(sj[-1][0] - sj[0][0]) * 5 / speed_mm_s))
    wet = round(max(gaps), 1) if gaps else 0.0
    return {"wet_edge_max_s": wet, "coverage": round(cov, 4), "overlap_waste": round(waste, 4), "keepout_intrusion": round(intrude, 4),
            "strokes": len(segs), "painted_mm": round(painted_len * 5), "travel_mm": round(travel * 5),
            "travel_share": round(travel / max(travel + painted_len, 1), 4), "lifts": lifts, "upward_moves": ups}


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--json")
    ap.add_argument("--order", default="optimal", choices=["optimal", "cells", "rows"])
    a = ap.parse_args()
    res = {}
    print(f"{'wall':16s} {'cover':>6s} {'waste':>6s} {'intr':>6s} {'strokes':>7s} {'travel%':>7s} {'lifts':>5s} {'up':>3s} {'wet-edge s':>10s}")
    for name, (p, k) in walls().items():
        r = res[name] = evaluate(p, k, order=a.order)
        print(f"{name:16s} {r['coverage']:6.3f} {r['overlap_waste']:6.3f} {r['keepout_intrusion']:6.3f} {r['strokes']:7d} {r['travel_share']*100:7.1f} {r['lifts']:5d} {r['upward_moves']:3d} {r['wet_edge_max_s']:10.1f}")
    if a.json:
        open(a.json, "w").write(json.dumps(res, indent=1))


if __name__ == "__main__":
    main()