File size: 17,755 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Make a to-scale wall elevation from a photo (or the fused mosaic of several photos / a video), as a drawing a person can read and the
matcher can read back.

Pipeline: wall measurement (wall_measure.measure_wall) gives the wall's floor-to-ceiling quad and its width and height in feet WITH the
source of that scale. The class mask is rectified onto that quad at a fixed millimetres-per-pixel, windows and doors are pulled out as
rectangles in millimetres, and the sheet is written as JSON (the format drawing_match.drawing_from_json reads), SVG, DXF and a PNG.

What the drawing says about itself (``provenance``), because a drawing made from a photo is a DRAFT and the numbers are only as good as their anchor:
* ``scale_source`` / ``scale_assumed``: a measured width or height, a depth scale, or an ASSUMED ceiling height. With an assumed height every
  dimension on the sheet scales with that assumption, and the title block says so in capitals.
* ``partial``: the wall runs past the edge of the frame on a side, so the drawing is only the visible part.
* ``fixtures``: switches, AC units and obstacles are listed as marks, not as dimensioned objects.
* Perspective is corrected for a flat wall only. Openings are axis-aligned rectangles (a bay window, an arch or a rotated frame is
  simplified; ``skipped`` lists any shape too far from a rectangle to draw honestly).

Pure numpy + cv2: no model is needed (the labels come from predict.py).
"""
from __future__ import annotations

import json
import math

import cv2
import numpy as np
from scipy import ndimage

from wall_measure import AC, DOOR, MM_PER_FT, OBSTACLE, SWITCH, WINDOW

RECT_MM_PER_PX = 10.0
MIN_OPENING_MM = 250.0
MIN_RECT_FILL = 0.6                  # an opening's mask must fill at least this much of its bounding rectangle
DISCLAIMER = "DRAFT generated from photographs. Verify every dimension on site before ordering, cutting or programming anything."


def rectify(labels, quad_px, width_mm, height_mm, mm_per_px=RECT_MM_PER_PX):
    """The class mask warped from the photo onto the wall plane: a (height/mm_per_px, width/mm_per_px) map, 255 outside the photo."""
    w, h = int(round(width_mm / mm_per_px)), int(round(height_mm / mm_per_px))
    if w < 20 or h < 20:
        raise ValueError(f"the wall is {width_mm:.0f} x {height_mm:.0f} mm: too small to draw")
    src = np.float32(quad_px)                                          # TL, TR, BR, BL in the photo
    dst = np.float32([[0, 0], [w, 0], [w, h], [0, h]])
    m = cv2.getPerspectiveTransform(src, dst)
    return cv2.warpPerspective(np.asarray(labels).astype(np.uint8), m, (w, h), flags=cv2.INTER_NEAREST, borderValue=255)


def _components(rect, cls, mm_per_px, wall_h, min_mm=MIN_OPENING_MM):
    comp, n = ndimage.label(rect == cls)
    boxes, skipped = [], []
    for i in range(1, n + 1):
        ys, xs = np.nonzero(comp == i)
        x0, x1, y0, y1 = xs.min(), xs.max() + 1, ys.min(), ys.max() + 1
        w_mm, h_mm = (x1 - x0) * mm_per_px, (y1 - y0) * mm_per_px
        if w_mm < min_mm or h_mm < min_mm:
            continue
        fill = len(xs) / float((x1 - x0) * (y1 - y0))
        if fill < MIN_RECT_FILL:
            skipped.append({"kind": "window" if cls == WINDOW else "door" if cls == DOOR else "fixture",
                            "x_mm": round(x0 * mm_per_px), "reason": f"shape fills only {fill:.0%} of its bounding rectangle (not a rectangle)"})
            continue
        boxes.append((x0 * mm_per_px, (rect.shape[0] - y1) * mm_per_px, w_mm, h_mm))     # x, y from the BOTTOM-left, as drawing_match reads
    return sorted(boxes), skipped


def make_elevation(labels, face, name="wall", mm_per_px=RECT_MM_PER_PX, source_note=None):
    """Drawing spec for one wall. ``face`` is one entry of wall_measure.measure_wall(...)["faces"]."""
    width_mm, height_mm = face["width_ft"] * MM_PER_FT, face["height_ft"] * MM_PER_FT
    quad = face.get("corners_px")
    if not quad or len(quad) != 4:
        raise ValueError("the wall measurement has no floor-to-ceiling corners, so there is nothing to rectify")
    rect = rectify(labels, quad, width_mm, height_mm, mm_per_px)
    seen = float((rect != 255).mean())
    openings, skipped = [], []
    k = {"window": 0, "door": 0}
    for cls, kind in ((WINDOW, "window"), (DOOR, "door")):
        boxes, sk = _components(rect, cls, mm_per_px, height_mm)
        skipped += sk
        for x, y, w, h in boxes:
            k[kind] += 1
            openings.append({"id": f"{'W' if kind == 'window' else 'D'}{k[kind]}", "kind": kind, "x_mm": round(x), "y_mm": round(y),
                             "w_mm": round(w), "h_mm": round(h)})
    fixtures = []
    for cls, kind in ((SWITCH, "switch/outlet"), (AC, "air conditioner"), (OBSTACLE, "obstacle")):
        boxes, _ = _components(rect, cls, mm_per_px, height_mm, min_mm=60.0)
        fixtures += [{"kind": kind, "x_mm": round(x), "y_mm": round(y), "w_mm": round(w), "h_mm": round(h)} for x, y, w, h in boxes]
    src = face.get("scale_source", "unknown")
    assumed = str(src).startswith("assumed")
    clipped = list(face.get("clipped_sides") or [])
    notes = []
    if assumed:
        notes.append("DIMENSIONS ASSUME " + src.replace("assumed_ceiling_", "a ceiling height of ").replace("ft", " ft")
                     + ": every dimension scales with that assumption. Measure the wall height and re-run.")
    if clipped:
        notes.append("the wall continues past the " + " and ".join(clipped) + " of the photo: this drawing is only the part that was visible")
    if seen < 0.9:
        notes.append(f"only {seen:.0%} of the wall rectangle was inside the photo")
    if source_note:
        notes.append(source_note)
    return {"name": name, "wall": {"width_mm": round(width_mm), "height_mm": round(height_mm)}, "openings": openings, "fixtures": fixtures,
            "provenance": {"generated_from": "photo", "scale_source": src, "scale_assumed": assumed, "partial": bool(clipped),
                           "clipped_sides": clipped, "seen_fraction": round(seen, 3), "rect_mm_per_px": mm_per_px,
                           "skipped": skipped, "notes": notes, "disclaimer": DISCLAIMER}}


def make_elevation_from_twin(twin, name=None, source_note=None):
    """Drawing spec for the wall a DIGITAL TWIN holds: its size is whatever the twin's best source gave (scan, laser meter, fused photos), and its
    openings are read from the twin's fused surface map, so the sheet shows the whole wall rather than the one frame a single photo caught.

    The provenance says which source the dimensions rest on; the DRAFT warning stays (a twin is still camera-derived unless a scan/laser fixed it)."""
    import twin_model as tmod
    acc = twin.accuracy()
    src = acc["dimension_source"]
    W, H = twin.width_mm, twin.height_mm
    openings, fixtures, k = [], [], {"window": 0, "door": 0}
    for o in tmod.openings(twin):
        if o["kind"] in ("window", "door"):
            k[o["kind"]] += 1
            openings.append({"id": f"{'W' if o['kind'] == 'window' else 'D'}{k[o['kind']]}", "kind": o["kind"], "x_mm": o["x_mm"], "y_mm": round(H - o["y_mm"] - o["h_mm"]),
                             "w_mm": o["w_mm"], "h_mm": o["h_mm"]})
        else:
            fixtures.append({"kind": {"switch": "switch/outlet", "unit": "air conditioner"}.get(o["kind"], o["kind"]), "x_mm": o["x_mm"],
                             "y_mm": round(H - o["y_mm"] - o["h_mm"]), "w_mm": o["w_mm"], "h_mm": o["h_mm"]})
    assumed = bool(twin.provenance.get("scale_assumed"))
    complete = twin.provenance.get("wall_complete")
    notes = [f"drawn from the digital twin: dimensions from {src}" + (f", {acc['views_fused']} photo view(s) fused" if acc.get("views_fused", 1) > 1 else "")]
    if assumed:
        notes.append("DIMENSIONS ASSUME " + str(twin.provenance.get("scale_source", "an assumed ceiling height")).replace("assumed_ceiling_", "a ceiling height of ").replace("ft", " ft")
                     + ": every dimension scales with that assumption. Measure the wall height and rebuild the twin.")
    if complete is False:
        notes.append("the wall runs past the edge of what was photographed: this drawing is only the part that was seen")
    known = float((twin.surface != 255).mean())
    if known < 0.9:
        notes.append(f"only {known:.0%} of the wall has been observed")
    if source_note:
        notes.append(source_note)
    return {"name": name or twin.name, "wall": {"width_mm": round(W), "height_mm": round(H)}, "openings": openings, "fixtures": fixtures,
            "provenance": {"generated_from": "twin", "scale_source": {"laser scan": "laser_scan", "laser distance meter": "laser"}.get(src, str(twin.provenance.get("scale_source", src))),
                           "scale_assumed": assumed, "partial": complete is False, "clipped_sides": list(twin.provenance.get("clipped_sides") or []),
                           "seen_fraction": round(known, 3), "rect_mm_per_px": twin.cell_mm, "skipped": [], "notes": notes, "disclaimer": DISCLAIMER}}


def schedule(spec):
    """Opening schedule lines (sill height, head height, distance from the left end) and the net wall area, for the sheet and for anyone reading the JSON."""
    W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"]
    rows = []
    for o in spec["openings"]:
        rows.append({"id": o["id"], "kind": o["kind"], "w_mm": o["w_mm"], "h_mm": o["h_mm"], "sill_mm": o["y_mm"], "head_mm": o["y_mm"] + o["h_mm"], "from_left_mm": o["x_mm"],
                     "area_m2": round(o["w_mm"] * o["h_mm"] / 1e6, 2)})
    gross = W * H / 1e6
    openings_area = sum(r["area_m2"] for r in rows)
    return {"rows": rows, "gross_m2": round(gross, 2), "openings_m2": round(openings_area, 2), "net_m2": round(max(gross - openings_area, 0), 2)}


# ------------------------------------------------------------------------------------------------ outputs

def _fmt(mm):
    return f"{mm / 1000:.2f} m" if mm >= 1000 else f"{mm:.0f} mm"


def to_svg(spec, px_per_mm=0.1):
    """A dimensioned elevation: wall, openings (window cross, door swing), fixtures, overall and opening dimensions, and a title block that
    repeats the scale assumption and the DRAFT warning. y is drawn downward from the top of the wall."""
    W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"]
    import textwrap
    s = px_per_mm
    my = 70
    vw = max(W * s + 180, 760)                                           # a narrow wall still gets a sheet wide enough for its notes
    mx = (vw - W * s) / 2
    p = spec["provenance"]
    src = p["scale_source"]
    src_text = {"measured_height": "the wall height entered", "measured_width": "the wall width entered", "depth_scale": "a saved scale (mm per pixel)",
                "laser_scan": "a laser scan", "laser": "a laser distance meter"}.get(
        src, src.replace("assumed_ceiling_", "an assumed ceiling height of ").replace("ft", " ft"))
    chars = int((vw - 2 * 40) / 5.9)
    sched = schedule(spec)
    sched_lines = ([f"SCHEDULE  gross {sched['gross_m2']:.2f} m2, openings {sched['openings_m2']:.2f} m2, net wall {sched['net_m2']:.2f} m2"]
                   + [f"{r['id']}  {r['kind']}  {_fmt(r['w_mm'])} x {_fmt(r['h_mm'])}  sill {_fmt(r['sill_mm'])}  head {_fmt(r['head_mm'])}  {_fmt(r['from_left_mm'])} from left" for r in sched["rows"][:14]])
    body = [f"Scale from {src_text}" + (" (ASSUMED: check it)" if p["scale_assumed"] else "")] + p["notes"][:3] + [p["disclaimer"]]
    wrapped = [(ln, i) for i, t in enumerate(body) for ln in textwrap.wrap(t, chars)]
    vh = H * s + my + 56 + 30 + 14 * (len(wrapped) + len(sched_lines) + 1) + 20
    out = [f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {vw:.0f} {vh:.0f}" width="{vw:.0f}" height="{vh:.0f}" font-family="Helvetica,Arial,sans-serif">',
           f'<rect width="100%" height="100%" fill="#fff"/>', f'<g transform="translate({mx:.1f},{my})" stroke="#111" fill="none" stroke-width="1.6">',
           f'<rect x="0" y="0" width="{W * s:.1f}" height="{H * s:.1f}" stroke-width="2.4"/>']
    for o in spec["openings"]:
        x, y, w, h = o["x_mm"] * s, (H - o["y_mm"] - o["h_mm"]) * s, o["w_mm"] * s, o["h_mm"] * s
        out.append(f'<rect x="{x:.1f}" y="{y:.1f}" width="{w:.1f}" height="{h:.1f}" fill="#f4f8fc"/>')
        if o["kind"] == "window":
            out.append(f'<path d="M{x:.1f} {y:.1f}L{x + w:.1f} {y + h:.1f}M{x + w:.1f} {y:.1f}L{x:.1f} {y + h:.1f}" stroke="#7a8794" stroke-width=".8"/>')
        else:
            out.append(f'<path d="M{x:.1f} {y + h:.1f}A{w:.1f} {w:.1f} 0 0 1 {x + w:.1f} {y + h - w:.1f}" stroke="#7a8794" stroke-width=".8" stroke-dasharray="4 3"/>')
        out.append(f'<text x="{x + w / 2:.1f}" y="{y + h / 2:.1f}" font-size="11" text-anchor="middle" fill="#111" stroke="none">{o["id"]}</text>')
        out.append(f'<text x="{x + w / 2:.1f}" y="{y - 6:.1f}" font-size="9.5" text-anchor="middle" fill="#445" stroke="none">{_fmt(o["w_mm"])} x {_fmt(o["h_mm"])}</text>')
    for f in spec.get("fixtures", []):
        x, y, w, h = f["x_mm"] * s, (H - f["y_mm"] - f["h_mm"]) * s, max(f["w_mm"] * s, 4), max(f["h_mm"] * s, 4)
        out.append(f'<rect x="{x:.1f}" y="{y:.1f}" width="{w:.1f}" height="{h:.1f}" stroke="#b25e00" stroke-dasharray="3 2"/>')
    by = H * s + 26
    out += [f'<path d="M0 {by:.1f}H{W * s:.1f}M0 {by - 5:.1f}V{by + 5:.1f}M{W * s:.1f} {by - 5:.1f}V{by + 5:.1f}" stroke-width="1"/>',
            f'<text x="{W * s / 2:.1f}" y="{by + 15:.1f}" font-size="12" text-anchor="middle" fill="#111" stroke="none">{_fmt(W)}</text>',
            f'<path d="M-26 0V{H * s:.1f}M-31 0H-21M-31 {H * s:.1f}H-21" stroke-width="1"/>',
            f'<text transform="translate(-34 {H * s / 2:.1f}) rotate(-90)" font-size="12" text-anchor="middle" fill="#111" stroke="none">{_fmt(H)}</text>']
    xs = sorted({0, W} | {o["x_mm"] for o in spec["openings"]} | {o["x_mm"] + o["w_mm"] for o in spec["openings"]})
    ty = -16
    out.append(f'<path d="M0 {ty}H{W * s:.1f}" stroke-width=".8"/>')
    for a, b in zip(xs[:-1], xs[1:]):
        if (b - a) * s > 46:
            out.append(f'<text x="{(a + b) / 2 * s:.1f}" y="{ty - 4}" font-size="9" text-anchor="middle" fill="#445" stroke="none">{_fmt(b - a)}</text>')
        out.append(f'<path d="M{b * s:.1f} {ty - 4}V{ty + 4}" stroke-width=".8"/>')
    out.append("</g>")
    ty0 = my + H * s + 56
    out.append(f'<text x="40" y="{ty0:.0f}" font-size="14" font-weight="700" fill="#111">{spec["name"]} · ELEVATION · DRAFT</text>')
    for k, (ln, i) in enumerate(wrapped):
        warn = (i == 0 and p["scale_assumed"]) or ln.startswith("DIMENSIONS ASSUME") or i == len(body) - 1
        out.append(f'<text x="40" y="{ty0 + 18 + 14 * k:.0f}" font-size="10.5" fill="{"#b00020" if warn else "#445"}">{ln}</text>')
    sy = ty0 + 18 + 14 * len(wrapped) + 8
    for k, ln in enumerate(sched_lines):
        out.append(f'<text x="40" y="{sy + 14 * k:.0f}" font-size="10" font-family="Menlo,Consolas,monospace" fill="#223" font-weight="{700 if k == 0 else 400}">{ln}</text>')
    out.append("</svg>")
    return "\n".join(out)


def to_dxf(spec):
    """A minimal ASCII DXF (R12) in millimetres, y up from the base of the wall: layers WALL, WINDOW, DOOR, FIXTURE, TEXT. Opens in any CAD viewer."""
    ents = []

    def line(layer, x0, y0, x1, y1):
        ents.append(f"0\nLINE\n8\n{layer}\n10\n{x0:.1f}\n20\n{y0:.1f}\n30\n0.0\n11\n{x1:.1f}\n21\n{y1:.1f}\n31\n0.0")

    def rect(layer, x, y, w, h):
        for a, b in (((x, y), (x + w, y)), ((x + w, y), (x + w, y + h)), ((x + w, y + h), (x, y + h)), ((x, y + h), (x, y))):
            line(layer, *a, *b)

    def text(layer, x, y, h, t):
        ents.append(f"0\nTEXT\n8\n{layer}\n10\n{x:.1f}\n20\n{y:.1f}\n30\n0.0\n40\n{h:.1f}\n1\n{t}")

    W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"]
    rect("WALL", 0, 0, W, H)
    for o in spec["openings"]:
        rect("WINDOW" if o["kind"] == "window" else "DOOR", o["x_mm"], o["y_mm"], o["w_mm"], o["h_mm"])
        text("TEXT", o["x_mm"] + 40, o["y_mm"] + o["h_mm"] / 2, 90, f'{o["id"]} {o["w_mm"]}x{o["h_mm"]}')
    for f in spec.get("fixtures", []):
        rect("FIXTURE", f["x_mm"], f["y_mm"], f["w_mm"], f["h_mm"])
    text("TEXT", 0, -260, 110, f'{spec["name"]} - DRAFT from photographs, scale: {spec["provenance"]["scale_source"]}')
    text("TEXT", 0, -420, 90, spec["provenance"]["disclaimer"][:110])
    layers = "".join(f"0\nLAYER\n2\n{n}\n70\n0\n62\n{c}\n6\nCONTINUOUS\n" for n, c in
                     (("WALL", 7), ("WINDOW", 5), ("DOOR", 3), ("FIXTURE", 30), ("TEXT", 8)))
    return ("0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1009\n9\n$INSUNITS\n70\n4\n0\nENDSEC\n"
            f"0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n5\n{layers}0\nENDTAB\n0\nENDSEC\n"
            "0\nSECTION\n2\nENTITIES\n" + "\n".join(ents) + "\n0\nENDSEC\n0\nEOF\n")


def to_png(spec, mm_per_px=15.0, line=3):
    """The sheet as a raster the console and drawing_match.drawing_from_image can read: black lines on white, wall rectangle with
    openings inside it, a door standing on the base line. Returns (BGR image, mm_per_px)."""
    W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"]
    pad = 40
    img = np.full((int(H / mm_per_px) + 2 * pad, int(W / mm_per_px) + 2 * pad, 3), 255, np.uint8)
    x0, y0 = pad, pad
    cv2.rectangle(img, (x0, y0), (x0 + int(W / mm_per_px), y0 + int(H / mm_per_px)), (0, 0, 0), line)
    for o in spec["openings"]:
        a = (x0 + int(o["x_mm"] / mm_per_px), y0 + int((H - o["y_mm"] - o["h_mm"]) / mm_per_px))
        b = (x0 + int((o["x_mm"] + o["w_mm"]) / mm_per_px), y0 + int((H - o["y_mm"]) / mm_per_px))
        cv2.rectangle(img, a, b, (0, 0, 0), line)
    return img, mm_per_px


def dumps(spec):
    return json.dumps(spec, indent=1)