#!/usr/bin/env python3 """Turn segmentation masks into a 2.5D town block model (a heightfield with roofs, trees, water and zones). Footprints are straightened, slivers opened away, merged row-house blocks split into house-sized pieces (separate houses must be separate in pixels, or the re-surfacing model re-slices them into a "caterpillar"), and every footprint gets a roof: a gable over its fitted rectangle, or a capped hip from the distance transform. Heights are NOT on the map. They come from wealth zones in the config (`zones`): each zone gives an eaves range and a roof pitch. Zones are assigned by polygon (in map px) when given, else by roof density (dense core = zone 2, sparse edge = zone 1). Landmarks you model by hand are cut out (`cutouts`, polygons in map px). usage: massing.py seg.npz --config citykit.json --out town.npz [--window x0 y0 x1 y1] Writes HR (roof height m), SDF (m, <0 inside a footprint), EAV (eaves m), CLS, ZONE, TINT, WD (water depth), TREE, DECK (bridges), plus the window and cell size. render_plate.py reads it. """ import argparse import cv2 import numpy as np from citykit import ell, h01, load_config RISE_MAX = 6.5 # m, tallest roof rise above the eaves def roof_heights(roof, zone_of, lab, n, st, zones, mpp): H = np.zeros(roof.shape, np.float32) EAV = np.zeros(roof.shape, np.float32) for i in range(1, n): x, y, w, h, area = st[i] sub = lab[y:y + h, x:x + w] == i z = zones[str(zone_of[i])] emin, emax, pitch = z["eaves_min"], z["eaves_max"], z["pitch"] cx, cy = x + w / 2, y + h / 2 eav = emin + (emax - emin) * h01("eav", i, int(cx), int(cy)) ys, xs = np.nonzero(sub) rect = cv2.minAreaRect(np.stack([xs, ys], 1).astype(np.float32)) (rcx, rcy), (rw, rh), ang = rect fill = area / (rw * rh + 1e-6) blk = H[y:y + h, x:x + w] eb = EAV[y:y + h, x:x + w] if fill > 0.70 and min(rw, rh) > 5 and max(rw, rh) * mpp < 75: box = cv2.boxPoints(rect) if np.linalg.norm(box[1] - box[0]) < np.linalg.norm(box[2] - box[1]): box = np.roll(box, -1, axis=0) p0, p1, p2, p3 = box d = (p1 - p0) / (np.linalg.norm(p1 - p0) + 1e-6) nrm = np.array([-d[1], d[0]]) a = (p0 + p3) / 2 span = abs(np.dot(p3 - p0, nrm)) / 2 m = np.zeros((h, w), np.uint8) cv2.fillPoly(m, [np.round(box).astype(np.int32)], 1) m &= cv2.dilate(sub.astype(np.uint8), ell(3)) yy, xx = np.nonzero(m) dist = np.abs((xx - a[0]) * nrm[0] + (yy - a[1]) * nrm[1]) rise = np.clip(span - dist, 0, None) * mpp * pitch hv = eav + np.minimum(rise, RISE_MAX) blk[yy, xx] = np.maximum(blk[yy, xx], hv) eb[yy, xx] = np.maximum(eb[yy, xx], eav) else: comp = sub.astype(np.uint8) dt = cv2.distanceTransform(np.pad(comp, 1), cv2.DIST_L2, 5)[1:-1, 1:-1] * mpp hv = np.where(comp > 0, eav + np.minimum(dt * pitch, RISE_MAX), 0) np.maximum(blk, hv, out=blk) eb[comp > 0] = np.maximum(eb[comp > 0], eav) return H, EAV def split_blocks(roof, mpp, min_area_m2=180.0, house_min=8.0, house_max=15.0, depth_split=17.0): """Merged row-house blocks -> house-sized pieces with a 1-cell gap between them.""" n, lab, st, _ = cv2.connectedComponentsWithStats(roof, 8) cut = np.zeros_like(roof) for i in range(1, n): x, y, w, h, a = st[i] if a * mpp ** 2 < min_area_m2: continue sub = lab[y:y + h, x:x + w] == i ys, xs = np.nonzero(sub) cx, cy = xs.mean(), ys.mean() cov = np.cov(np.stack([xs - cx, ys - cy])) ev, evec = np.linalg.eigh(cov) ux, uy = evec[0, 1], evec[1, 1] yy, xx = np.mgrid[0:h, 0:w] u = (xx - cx) * ux + (yy - cy) * uy v = -(xx - cx) * uy + (yy - cy) * ux pv = -(xs - cx) * uy + (ys - cy) * ux edges = [u.min()] k = 0 while edges[-1] < u.max(): edges.append(edges[-1] + (house_min + (house_max - house_min) * h01("w", i, k)) / mpp) k += 1 e = np.array(edges) boundary = np.zeros((h, w), bool) if len(e) > 2: dist_edge = np.min(np.abs(u[..., None] - e[None, None, 1:-1]), axis=-1) boundary |= dist_edge < 0.6 if (pv.max() - pv.min()) * mpp > depth_split: boundary |= np.abs(v) < 0.6 cut[y:y + h, x:x + w] |= (boundary & sub).astype(np.uint8) out = roof.copy() out[cut > 0] = 0 return out def build(seg, cfg, window=None): mpp = float(seg["mpp"]) roof, tree, water = seg["roof"].copy(), seg["tree"].copy(), seg["water"].copy() if window is not None: x0, y0, x1, y1 = window else: x0, y0, x1, y1 = 0, 0, roof.shape[1], roof.shape[0] roof, tree, water = roof[y0:y1, x0:x1], tree[y0:y1, x0:x1], water[y0:y1, x0:x1] gh, gw = roof.shape # hand-modeled landmarks are cut out (polygons in map px) excl = np.zeros_like(roof) for poly in cfg.get("cutouts", []): P = np.array(poly, float) - [x0, y0] cv2.fillPoly(excl, [P.astype(np.int32)], 1) if excl.any(): excl = cv2.dilate(excl, ell(6 / mpp)) roof[excl > 0] = 0 tree[excl > 0] = 0 # bridges/decks: roof components lying mostly over (dilated) water wd = cv2.dilate(water, ell(15)) n, lab, st, _ = cv2.connectedComponentsWithStats(roof, 8) deck = np.zeros_like(roof) for i in range(1, n): x, y, w, h, a = st[i] comp = lab[y:y + h, x:x + w] == i if wd[y:y + h, x:x + w][comp].mean() > 0.5: deck[y:y + h, x:x + w][comp] = 1 roof[deck > 0] = 0 # straighten + de-sliver cnts, hier = cv2.findContours(roof, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) clean = np.zeros_like(roof) if hier is not None: for i, c in enumerate(cnts): if hier[0][i][3] < 0: cv2.fillPoly(clean, [cv2.approxPolyDP(c, 1.0, True)], 1) for i, c in enumerate(cnts): if hier[0][i][3] >= 0 and cv2.contourArea(c) > 60: cv2.fillPoly(clean, [cv2.approxPolyDP(c, 1.0, True)], 0) roof = cv2.morphologyEx(clean, cv2.MORPH_OPEN, ell(2.0 / mpp)) n, lab, st, cen = cv2.connectedComponentsWithStats(roof, 8) keep = np.zeros(n, bool) keep[1:] = st[1:, 4] * mpp ** 2 >= cfg.get("min_house_m2", 40) roof = keep[lab].astype(np.uint8) roof = split_blocks(roof, mpp) n, lab, st, cen = cv2.connectedComponentsWithStats(roof, 4) # zones zones = cfg["zones"] zone_of = np.ones(n, np.uint8) polys = [(int(z.get("id", k)), np.array(z["polygon_px"], float) - [x0, y0]) for k, z in zones.items() if "polygon_px" in z] dens = cv2.blur(roof.astype(np.float32), (int(80 / mpp) | 1,) * 2) default_core = int(cfg.get("default_core_zone", 2)) default_edge = int(cfg.get("default_edge_zone", 1)) for i in range(1, n): cx, cy = cen[i] zid = None for pid, P in polys: if cv2.pointPolygonTest(P.astype(np.float32), (float(cx), float(cy)), False) >= 0: zid = pid break if zid is None: zid = default_core if dens[int(cy), int(cx)] >= cfg.get("core_density", 0.16) else default_edge zone_of[i] = zid HR, EAV = roof_heights(roof, zone_of, lab, n, st, zones, mpp) ZONE = zone_of[lab] * (roof > 0) TINT = (np.array([0] + [int(255 * h01("t", i)) for i in range(1, n)], np.uint8))[lab] # trees -> lumpy crowns tt = cv2.morphologyEx(tree, cv2.MORPH_OPEN, ell(3)) & (roof == 0) dtt = cv2.distanceTransform(tt, cv2.DIST_L2, 3) * mpp rng = np.random.default_rng(3) lump = cv2.GaussianBlur(rng.normal(0, 1, tt.shape).astype(np.float32), (0, 0), 2.0) TREE = np.where(tt > 0, 5.5 + 1.6 * np.minimum(dtt, 4.5) + 0.9 * lump, 0).astype(np.float32) TREE = cv2.GaussianBlur(TREE, (0, 0), 1.0) * (tt > 0) din = cv2.distanceTransform(roof, cv2.DIST_L2, 5) dout = cv2.distanceTransform((1 - roof).astype(np.uint8), cv2.DIST_L2, 5) SDF = np.clip(((dout - din) * mpp).astype(np.float32), -20, 20) HRx = cv2.dilate(np.where(roof > 0, HR, 0).astype(np.float32), np.ones((3, 3), np.uint8)) HR = np.where(roof > 0, HR, HRx).astype(np.float32) EAVx = cv2.dilate(EAV, np.ones((3, 3), np.uint8)) EAV = np.where(roof > 0, EAV, EAVx) wsoft = cv2.GaussianBlur(water.astype(np.float32), (0, 0), 1.2) WD = float(cfg.get("water_depth_m", 3.0)) * np.clip(wsoft * 1.6 - 0.3, 0, 1) # classes: 0 street 1 roof 3 tree 4 water 6 deck 8 field CLS = np.zeros((gh, gw), np.uint8) open_ = ((roof == 0) & (water == 0) & (tt == 0)).astype(np.uint8) dens2 = cv2.blur((roof > 0).astype(np.float32), (int(160 / mpp) | 1,) * 2) CLS[(open_ > 0) & (dens2 < 0.05)] = 8 CLS[roof > 0] = 1 CLS[tt > 0] = 3 CLS[water > 0] = 4 CLS[deck > 0] = 6 print("town grid", gh, gw, "houses", n - 1, "roof frac %.3f" % (roof > 0).mean(), "zones", dict(zip(*np.unique(zone_of[1:], return_counts=True)))) return dict(HR=HR.astype(np.float32), SDF=SDF.astype(np.float16), EAV=EAV.astype(np.float16), CLS=CLS, TINT=TINT, ZONE=ZONE.astype(np.uint8), WD=WD.astype(np.float16), TREE=TREE.astype(np.float16), DECK=deck.astype(np.uint8), win=np.array([x0, y0, x1, y1]), cell=np.float32(mpp)) if __name__ == "__main__": ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("seg") ap.add_argument("--config", required=True) ap.add_argument("--out", default="town.npz") ap.add_argument("--window", type=int, nargs=4, default=None, help="x0 y0 x1 y1 in map px") a = ap.parse_args() cfg = load_config(a.config) out = build(np.load(a.seg), cfg["massing"], a.window) np.savez(a.out, **out) print("wrote", a.out)