{
  "version": 1,
  "description": "A square town quarter cut into irregular lots along streets, centred on the origin (Y up, metres). Voronoi blocks shrunk by half a street leave the streets; each block is cut into lots by a relaxed Voronoi of its own (lots shared out by block area), and every lot gets one house that follows its shape: the lot shrunk to a house 9 to 13 m across (smaller lots keep a strip of garden all round), pushed to 2 m behind the lot's street front, so a front garden, side strips and a back garden stay free. Walls carry a window in every facade cell and a door to the street; a straight-skeleton roof on the footprint is hipped with 0.35 m eaves or pitched with vertical gables on its two ends. About a third of the houses get extra_floors on top. The render (node town_view) shows the three outputs over the lots drawn as gardens.",
  "params": {
    "area": { "type": "float", "default": 120, "min": 60, "max": 400, "step": 1, "label": "area", "desc": "side of the square quarter (m)" },
    "lots": { "type": "int", "default": 40, "min": 4, "max": 60, "label": "lots", "desc": "how many lots the quarter is cut into" },
    "street_width": { "type": "float", "default": 6, "min": 4, "max": 12, "step": 0.5, "label": "street width", "desc": "width of the streets between the blocks of lots (m)" },
    "floors": { "type": "int", "default": 2, "min": 1, "max": 5, "label": "floors", "desc": "floors of an ordinary house (3 m each)" },
    "extra_floors": { "type": "int", "default": 2, "min": 0, "max": 3, "label": "extra floors", "desc": "floors that about a third of the houses, picked at random, get on top" },
    "roof_pitch": { "type": "float", "default": 35, "min": 15, "max": 60, "step": 1, "label": "roof pitch", "desc": "slope of every roof (degrees)" },
    "seed": { "type": "int", "default": 1, "min": 0, "max": 9999, "label": "seed", "desc": "changes the plan, the colours and which houses grow taller" }
  },
  "vars": { "area": 120, "lots": 40, "street_width": 6, "floors": 2, "extra_floors": 2, "roof_pitch": 35, "seed": 1 },
  "meta": { "render": { "node": "town_view", "views": ["persp"], "azimuth": 34, "elevation": 34, "size": "1600x900", "shading": "pbr", "grid": false } },
  "nodes": {
    "blocks": { "type": "voronoi2d", "note": "the street plan: about one block per five lots (fewer where the streets would eat a small quarter: no block narrower than about two street widths), each cell shrunk by half a street", "params": { "count": "=max(2, min(round(lots / 5), floor(pow(area / (3 * street_width), 2))))", "size": ["=area", "=area"], "inset": "=street_width / 2", "relax": 3, "seed": "=seed" } },
    "plan": { "type": "wrangle", "in": ["blocks"], "note": "cut every block into lots: the lots are shared out by block area (at least one each), seeded by best-candidate sampling inside the block, relaxed five times (Lloyd) and clipped to the block, so lots tile each block exactly (corners closer than 1 cm merged). Every lot draws its floors, roof style and colours, and gets its street front (the outward direction of its longest edge on the block's edge; for a lot inside a block, of its edge that faces most away from the block's centre), its yard gap (2 m, less on a tiny lot) and how deep its house reaches from the front (8 to 11 m behind the gap)", "params": { "run_over": "detail", "code": "const area2 = (q) => { let s = 0; for (let i = 0; i < q.length; i++) { const a = q[i], b = q[(i + 1) % q.length]; s += a[0] * b[1] - b[0] * a[1]; } return s / 2; };\nconst centroid = (q) => { let x = 0, z = 0, s = 0; for (let i = 0; i < q.length; i++) { const a = q[i], b = q[(i + 1) % q.length], w = a[0] * b[1] - b[0] * a[1]; s += w; x += (a[0] + b[0]) * w; z += (a[1] + b[1]) * w; } return [x / (3 * s), z / (3 * s)]; };\nconst clip = (q, nx, nz, c) => { const out = []; for (let i = 0; i < q.length; i++) { const a = q[i], b = q[(i + 1) % q.length], da = nx * a[0] + nz * a[1] - c, db = nx * b[0] + nz * b[1] - c; if (da <= 0) out.push(a); if ((da < 0 && db > 0) || (da > 0 && db < 0)) { const t = da / (da - db); out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]); } } return out; };\nconst cell = (q, s, i) => { let c = q; for (let j = 0; j < s.length && c.length > 2; j++) if (j !== i) c = clip(c, s[j][0] - s[i][0], s[j][1] - s[i][1], (s[j][0] ** 2 + s[j][1] ** 2 - s[i][0] ** 2 - s[i][1] ** 2) / 2); return c; };\nconst inside = (q, x, z) => q.every((a, i) => { const b = q[(i + 1) % q.length]; return (b[0] - a[0]) * (z - a[1]) - (b[1] - a[1]) * (x - a[0]) >= 0; });\nconst inset = (q, d) => { let c = q; for (let i = 0; i < q.length && c.length > 2; i++) { const a = q[i], b = q[(i + 1) % q.length], l = Math.hypot(b[0] - a[0], b[1] - a[1]); if (l < 1e-9) continue; const nx = (a[1] - b[1]) / l, nz = (b[0] - a[0]) / l; c = clip(c, -nx, -nz, -(nx * a[0] + nz * a[1]) - d); } return c; };\nconst inradius = (q) => { let lo = 0, hi = 1000; for (let i = 0; i < 30; i++) { const m = (lo + hi) / 2, c = inset(q, m); if (c.length > 2 && area2(c) > 1e-6) lo = m; else hi = m; } return lo; };\nconst dedupe = (q) => q.filter((a, i) => { const b = q[(i + 1) % q.length]; return Math.hypot(b[0] - a[0], b[1] - a[1]) >= 0.01; });\nconst onEdge = (q, m) => q.some((a, i) => { const b = q[(i + 1) % q.length], dx = b[0] - a[0], dz = b[1] - a[1], t = Math.max(0, Math.min(1, ((m[0] - a[0]) * dx + (m[1] - a[1]) * dz) / (dx * dx + dz * dz))); return Math.hypot(a[0] + dx * t - m[0], a[1] + dz * t - m[1]) < 1e-5; });\nconst nb = nprims(0), blocks = [];\nfor (let b = 0; b < nb; b++) { const q = primpoints(0, b).map((p) => { const P = point(0, 'P', p); return [P[0], P[2]]; }); if (area2(q) < 0) q.reverse(); blocks.push(q); }\nconst A = blocks.map(area2), sumA = A.reduce((s, a) => s + a, 0), count = () => k.reduce((s, x) => s + x, 0);\nconst k = A.map((a) => Math.max(1, Math.floor(lots * a / sumA)));\nwhile (count() > lots && Math.max(...k) > 1) k[k.indexOf(Math.max(...k))]--;\nconst order = A.map((a, i) => i).sort((i, j) => (lots * A[j] / sumA - k[j]) - (lots * A[i] / sumA - k[i]));\nfor (let n = 0; count() < lots; n++) k[order[n % nb]]++;\nconst walls = ['cream', 'sand', 'rose', 'white', 'sage'], tiles = ['red', 'brown', 'slate'];\nlet id = 0;\nfor (let b = 0; b < nb; b++) {\n  const q = blocks[b], xs = q.map((p) => p[0]), zs = q.map((p) => p[1]);\n  const x0 = Math.min(...xs), x1 = Math.max(...xs), z0 = Math.min(...zs), z1 = Math.max(...zs);\n  let s = [];\n  for (let i = 0; i < k[b]; i++) {\n    let best = null, far = -1;\n    for (let c = 0; c < 24; c++) {\n      const x = x0 + rand(seed, b, i, c, 1) * (x1 - x0), z = z0 + rand(seed, b, i, c, 2) * (z1 - z0);\n      if (!inside(q, x, z)) continue;\n      const d = s.reduce((m, p) => Math.min(m, (p[0] - x) ** 2 + (p[1] - z) ** 2), 1e12);\n      if (d > far) { far = d; best = [x, z]; }\n    }\n    s.push(best ?? centroid(q));\n  }\n  for (let it = 0; it < 5; it++) s = s.map((p, i) => { const c = cell(q, s, i); return c.length > 2 ? centroid(c) : p; });\n  const bc = centroid(q);\n  for (let i = 0; i < s.length; i++) {\n    const c = dedupe(cell(q, s, i)), n = id++, r = inradius(c), gap = Math.min(2, 0.35 * r);\n    let front = null, fl = 0;\n    for (let e = 0; e < c.length; e++) { const a = c[e], b2 = c[(e + 1) % c.length], l = Math.hypot(b2[0] - a[0], b2[1] - a[1]); if (l > fl && onEdge(q, [(a[0] + b2[0]) / 2, (a[1] + b2[1]) / 2])) { fl = l; front = [(b2[1] - a[1]) / l, (a[0] - b2[0]) / l]; } }\n    if (!front) { const cc = centroid(c), dx = cc[0] - bc[0], dz = cc[1] - bc[1], dl = Math.hypot(dx, dz) || 1; let best = -Infinity; for (let e = 0; e < c.length; e++) { const a = c[e], b2 = c[(e + 1) % c.length], l = Math.hypot(b2[0] - a[0], b2[1] - a[1]); if (l < 1e-9) continue; const nx = (b2[1] - a[1]) / l, nz = (a[0] - b2[0]) / l, sc = (nx * dx + nz * dz) / dl + l * 0.01; if (sc > best) { best = sc; front = [nx, nz]; } } }\n    const pr = addprim(0, 'poly', c.slice().reverse().map((p) => addpoint(0, [p[0], 0, p[1]])));\n    const f = floors + (rand(seed, n, 7) < 0.3 ? extra_floors : 0);\n    setprimattrib(0, 'lot', pr, n);\n    setprimattrib(0, 'name', pr, 'lot_' + String(n).padStart(2, '0'));\n    setprimattrib(0, 'floors', pr, f);\n    setprimattrib(0, 'height', pr, f * 3);\n    setprimattrib(0, 'roof', pr, gap < 1 || rand(seed, n, 8) < 0.6 ? 'gable' : 'hip');\n    setprimattrib(0, 'wall', pr, walls[Math.floor(rand(seed, n, 9) * walls.length)]);\n    setprimattrib(0, 'tiles', pr, tiles[Math.floor(rand(seed, n, 10) * tiles.length)]);\n    setprimattrib(0, 'gap', pr, gap);\n    setprimattrib(0, 'depth', pr, gap + 8 + 3 * rand(seed, n, 11));\n    setprimattrib(0, 'fx', pr, front[0]);\n    setprimattrib(0, 'fz', pr, front[1]);\n  }\n}\nfor (let b = 0; b < nb; b++) removeprim(0, b, true);" } },
    "plan_clean": { "type": "attrib_delete", "in": ["plan"], "params": { "names": "class" } },

    "footprints": { "type": "foreach", "in": ["plan_clean"], "note": "every lot shrunk by its yard gap, so a strip of garden stays free along every lot edge; workaround: straight_skeleton offset refuses lots that share an edge, so each lot is offset on its own", "params": { "mode": "prims", "body": { "nodes": {
      "lot": { "type": "input", "params": { "index": 0 } },
      "house": { "type": "straight_skeleton", "in": ["lot"], "params": { "mode": "offset", "distance": "=prim('lot', 0, 'gap')" } }
    }, "output": "house" } } },
    "house_plan": { "type": "wrangle", "in": ["footprints", "plan_clean"], "note": "each house fills its lot's garden-strip outline from the street front back to its depth (and at most 22 m along the front), so it follows the lot's front and sides and leaves a back garden; corners sharper than 80° are cut back (up to 1 m), so eaves and roofs stay inside the yard gap; every outline point carries its house's wall height. Workaround: corners closer than 5 cm and straight corners are dropped (the straight-skeleton roof fails on a near-straight corner)", "params": { "run_over": "detail", "code": "const area2 = (q) => { let s = 0; for (let i = 0; i < q.length; i++) { const a = q[i], b = q[(i + 1) % q.length]; s += a[0] * b[1] - b[0] * a[1]; } return s / 2; };\nconst clip = (q, nx, nz, c) => { const out = []; for (let i = 0; i < q.length; i++) { const a = q[i], b = q[(i + 1) % q.length], da = nx * a[0] + nz * a[1] - c, db = nx * b[0] + nz * b[1] - c; if (da <= 0) out.push(a); if ((da < 0 && db > 0) || (da > 0 && db < 0)) { const t = da / (da - db); out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]); } } return out; };\nconst tidy = (q) => { const c = q.slice(); for (let i = 0; c.length > 3 && i < c.length; ) { const a = c[(i + c.length - 1) % c.length], b = c[i], d = c[(i + 1) % c.length], ux = b[0] - a[0], uz = b[1] - a[1], vx = d[0] - b[0], vz = d[1] - b[1], lu = Math.hypot(ux, uz), lv = Math.hypot(vx, vz); if (lu < 0.05 || ux * vz - uz * vx < 0.01 * lu * lv) { c.splice(i, 1); i = 0; } else i++; } return c; };\nconst chamfer = (q) => { const out = [], n = q.length; for (let i = 0; i < n; i++) { const a = q[(i + n - 1) % n], b = q[i], d = q[(i + 1) % n], ux = a[0] - b[0], uz = a[1] - b[1], vx = d[0] - b[0], vz = d[1] - b[1], lu = Math.hypot(ux, uz), lv = Math.hypot(vx, vz); if ((ux * vx + uz * vz) / (lu * lv) > 0.17) { const t = Math.min(1, 0.4 * Math.min(lu, lv)); out.push([b[0] + ux / lu * t, b[1] + uz / lu * t], [b[0] + vx / lv * t, b[1] + vz / lv * t]); } else out.push(b); } return out; };\nconst lots = new Map();\nfor (let l = 0; l < nprims(1); l++) lots.set(prim(1, 'lot', l), primpoints(1, l).map((p) => { const P = point(1, 'P', p); return [P[0], P[2]]; }));\nconst keys = ['lot', 'floors', 'height', 'roof', 'wall', 'tiles', 'fx', 'fz'], n0 = nprims(0), W = 22;\nfor (let p = 0; p < n0; p++) {\n  let c = primpoints(0, p).map((pt) => { const P = point(0, 'P', pt); return [P[0], P[2]]; });\n  if (c.length < 3) continue;\n  if (area2(c) < 0) c.reverse();\n  const q = lots.get(prim(0, 'lot', p)), fx = prim(0, 'fx', p), fz = prim(0, 'fz', p);\n  const front = Math.max(...q.map((v) => fx * v[0] + fz * v[1])), along = q.map((v) => fx * v[1] - fz * v[0]), mid = (Math.min(...along) + Math.max(...along)) / 2;\n  c = clip(c, -fx, -fz, prim(0, 'depth', p) - front);\n  c = clip(c, -fz, fx, mid + W / 2);\n  c = clip(c, fz, -fx, W / 2 - mid);\n  if (c.length < 3) continue;\n  c = tidy(chamfer(tidy(c)));\n  const pr = addprim(0, 'poly', c.slice().reverse().map((v) => { const pt = addpoint(0, [v[0], 0, v[1]]); setpointattrib(0, 'height', pt, prim(0, 'height', p)); return pt; }));\n  for (const k of keys) setprimattrib(0, k, pr, prim(0, k, p));\n}\nfor (let p = 0; p < n0; p++) removeprim(0, p, true);" } },

    "walls": { "type": "wrangle", "in": ["house_plan"], "note": "one wall quad per outline edge, as high as the house, facing out. Workaround: extrude drops the outline's attributes, so the walls are built here", "params": { "run_over": "detail", "code": "const np = nprims(0), old = npoints(0);\nfor (let p = 0; p < np; p++) {\n  const P = primpoints(0, p).map((q) => point(0, 'P', q)), n = P.length, h = prim(0, 'height', p);\n  let cx = 0, cz = 0; for (const q of P) { cx += q[0] / n; cz += q[2] / n; }\n  for (let i = 0; i < n; i++) {\n    let a = P[i], b = P[(i + 1) % n];\n    if (((a[0] + b[0]) / 2 - cx) * (a[2] - b[2]) + ((a[2] + b[2]) / 2 - cz) * (b[0] - a[0]) < 0) [a, b] = [b, a];\n    const pr = addprim(0, 'poly', [addpoint(0, [a[0], 0, a[2]]), addpoint(0, [b[0], 0, b[2]]), addpoint(0, [b[0], h, b[2]]), addpoint(0, [a[0], h, a[2]])]);\n    setprimattrib(0, 'lot', pr, prim(0, 'lot', p));\n    setprimattrib(0, 'wall', pr, prim(0, 'wall', p));\n  }\n}\nfor (let p = 0; p < np; p++) removeprim(0, p, false);\nfor (let p = 0; p < old; p++) removepoint(0, p);" } },
    "facade": { "type": "grid_split", "in": ["walls"], "params": { "size": [2.8, 3] } },
    "door_sites": { "type": "wrangle", "in": ["house_plan"], "note": "one door per house: the middle facade cell of the wall that faces the lot's street front most (on a cell 1.4 m or wider where there is one). Workaround: removeprim with its points would also delete the new door points (no prim uses them), so prims and old points are removed separately", "params": { "run_over": "detail", "code": "const np = nprims(0);\nfor (let p = 0; p < np; p++) {\n  const P = primpoints(0, p).map((q) => point(0, 'P', q)), n = P.length, fx = prim(0, 'fx', p), fz = prim(0, 'fz', p);\n  let cx = 0, cz = 0; for (const q of P) { cx += q[0] / n; cz += q[2] / n; }\n  let best = -1e9, site = null;\n  for (let i = 0; i < n; i++) {\n    const a = P[i], b = P[(i + 1) % n], dx = b[0] - a[0], dz = b[2] - a[2], l = Math.hypot(dx, dz), k = Math.max(1, Math.round(l / 2.8));\n    let nx = dz / l, nz = -dx / l;\n    if (((a[0] + b[0]) / 2 - cx) * nx + ((a[2] + b[2]) / 2 - cz) * nz < 0) { nx = -nx; nz = -nz; }\n    const score = (l / k >= 1.4 ? 0 : -10) + nx * fx + nz * fz + l * 1e-3;\n    if (score > best) { best = score; const t = (Math.floor(k / 2) + 0.5) / k; site = { P: [a[0] + dx * t, 0, a[2] + dz * t], N: [nx, 0, nz] }; }\n  }\n  if (!site) continue;\n  const pt = addpoint(0, site.P);\n  setpointattrib(0, 'N', pt, site.N);\n  setpointattrib(0, 'lot', pt, prim(0, 'lot', p));\n}\nconst old = npoints(0);\nfor (let p = 0; p < np; p++) removeprim(0, p, false);\nfor (let p = 0; p < old; p++) removepoint(0, p);" } },
    "facade_cells": { "type": "wrangle", "in": ["facade", "door_sites"], "note": "every wall cell 1.8 m or wider gets a window, but the door's", "params": { "run_over": "prims", "code": "s@material = 'plaster_' + @wall;\nif (Math.abs(@N[1]) > 0.5) return;\nconst b = primintrinsic(0, 'bounds', @primnum);\nif (Math.hypot(b[1] - b[0], b[5] - b[4]) < 1.8) return;\nif (b[2] < 0.01 && nearpoints(1, [@P[0], 0, @P[2]], 0.8, 1).length) return;\n@group_win = 1;" } },
    "windows": { "type": "poly_extrude", "in": ["facade_cells"], "params": { "group": "win", "mode": "individual", "inset": 0.55, "distance": 0 } },
    "window_recess": { "type": "poly_extrude", "in": ["windows"], "params": { "group": "front", "mode": "individual", "distance": -0.15 } },
    "window_glass": { "type": "material", "in": ["window_recess"], "params": { "name": "glass", "group": "front", "base_color": "#41596b", "roughness": 0.15, "metallic": 0.2 } },
    "window_reveal": { "type": "material", "in": ["window_glass"], "params": { "name": "trim", "group": "side", "base_color": "#f1ede4", "roughness": 0.6 } },

    "door": { "type": "topotopo/architecture/door_frame", "params": { "width": 1.2, "height": 2.3, "depth": 0.2, "panels": 2 } },
    "doors": { "type": "copy_to_points", "in": ["door", "door_sites"] },
    "doors_lot": { "type": "attrib_promote", "in": ["doors"], "params": { "name": "lot", "from": "point", "to": "prim", "method": "first" } },

    "eaves": { "type": "straight_skeleton", "in": ["house_plan"], "note": "hipped roofs reach 0.35 m past the walls (always inside the yard gap); pitched roofs stop flush, so their gables stand on the walls", "params": { "mode": "offset", "distance": -0.35, "group": "@roof=hip" } },
    "roof_plan": { "type": "wrangle", "in": ["eaves"], "note": "every roof edge slopes at roof_pitch; a pitched roof gets a vertical gable (90°) on the two ends across its longest side, where those ends are 2 m or longer, not next to each other and turned at least 33° from the ridge", "params": { "run_over": "prims", "code": "const pts = primpoints(0, @primnum), P = pts.map((p) => point(0, 'P', p)), n = P.length;\nconst gable = [-1, -1];\nif (s@roof === 'gable') {\n  let L = 0, d = [1, 0], cx = 0, cz = 0;\n  for (let i = 0; i < n; i++) { const a = P[i], b = P[(i + 1) % n], l = Math.hypot(b[0] - a[0], b[2] - a[2]); cx += a[0] / n; cz += a[2] / n; if (l > L) { L = l; d = [(b[0] - a[0]) / l, (b[2] - a[2]) / l]; } }\n  const score = [0.55, 0.55];\n  for (let i = 0; i < n; i++) {\n    const a = P[i], b = P[(i + 1) % n], dx = b[0] - a[0], dz = b[2] - a[2], l = Math.hypot(dx, dz);\n    if (l < 2) continue;\n    const across = Math.abs(dx * d[1] - dz * d[0]) / l, side = ((a[0] + b[0]) / 2 - cx) * d[0] + ((a[2] + b[2]) / 2 - cz) * d[1] > 0 ? 1 : 0;\n    if (across > score[side]) { score[side] = across; gable[side] = i; }\n  }\n  if (gable[0] >= 0 && gable[1] >= 0 && ((gable[0] - gable[1] + n) % n === 1 || (gable[1] - gable[0] + n) % n === 1)) gable[0] = gable[1] = -1;\n}\nfor (let i = 0; i < n; i++) setpointattrib(0, 'pitch', pts[i], gable.includes(i) ? 90 : roof_pitch);" } },
    "roof": { "type": "straight_skeleton", "in": ["roof_plan"], "params": { "mode": "roof", "pitch": "=roof_pitch", "pitch_attrib": "pitch" } },
    "roof_slopes": { "type": "wrangle", "in": ["roof"], "note": "the sloped faces get the roof's thickness; the gables stay single faces flush with the walls below (thickening them too folds the shell at their corners)", "params": { "run_over": "prims", "code": "if (@N[1] > 0.05) @group_slope = 1;" } },
    "roof_shell": { "type": "poly_extrude", "in": ["roof_slopes"], "params": { "distance": 0.2, "mode": "connected", "keep_back": true, "group": "slope" } },
    "roof_height": { "type": "attrib_promote", "in": ["roof_shell"], "params": { "name": "height", "from": "prim", "to": "point", "method": "max", "keep_original": true } },
    "roof_up": { "type": "wrangle", "in": ["roof_height"], "params": { "run_over": "points", "code": "@P.y += @height;" } },
    "roof_surfaces": { "type": "wrangle", "in": ["roof_up"], "note": "the gables are wall, the rest roof tiles", "params": { "run_over": "prims", "code": "const b = primintrinsic(0, 'bounds', @primnum);\ns@material = Math.abs(@N[1]) < 0.05 && b[3] - b[2] > 0.4 ? 'plaster_' + @wall : 'roof_' + @tiles;" } },

    "house_parts": { "type": "merge", "in": ["window_reveal", "roof_surfaces", "doors_lot"] },
    "house_names": { "type": "wrangle", "in": ["house_parts"], "params": { "run_over": "prims", "code": "s@name = 'house_' + String(@lot).padStart(2, '0');" } },
    "house_tidy": { "type": "attrib_delete", "in": ["house_names"], "note": "keep lot, name, floors, height and material: the build attributes, the door copy's normals and uvs and the facade groups go", "params": { "names": "roof wall tiles fx fz cell_u cell_v edge copynum pitch N uv", "groups": "win front side slope" } },
    "house_tidy_points": { "type": "attrib_delete", "in": ["house_tidy"], "params": { "names": "height", "class": "point" } },
    "house_normals": { "type": "normal", "in": ["house_tidy_points"], "note": "hard edges at every roof ridge, hip and window corner", "params": { "class": "vertex", "cusp_angle": 20 } },
    "mat_cream": { "type": "material", "in": ["house_normals"], "params": { "name": "plaster_cream", "group": "@material=plaster_cream", "base_color": "#e6d9bc", "roughness": 0.9 } },
    "mat_sand": { "type": "material", "in": ["mat_cream"], "params": { "name": "plaster_sand", "group": "@material=plaster_sand", "base_color": "#d6b98c", "roughness": 0.9 } },
    "mat_rose": { "type": "material", "in": ["mat_sand"], "params": { "name": "plaster_rose", "group": "@material=plaster_rose", "base_color": "#d49a86", "roughness": 0.9 } },
    "mat_white": { "type": "material", "in": ["mat_rose"], "params": { "name": "plaster_white", "group": "@material=plaster_white", "base_color": "#ece8df", "roughness": 0.9 } },
    "mat_sage": { "type": "material", "in": ["mat_white"], "params": { "name": "plaster_sage", "group": "@material=plaster_sage", "base_color": "#b5c0a0", "roughness": 0.9 } },
    "mat_red": { "type": "material", "in": ["mat_sage"], "params": { "name": "roof_red", "group": "@material=roof_red", "base_color": "#a4472f", "roughness": 0.8 } },
    "mat_brown": { "type": "material", "in": ["mat_red"], "params": { "name": "roof_brown", "group": "@material=roof_brown", "base_color": "#74503b", "roughness": 0.8 } },
    "mat_slate": { "type": "material", "in": ["mat_brown"], "params": { "name": "roof_slate", "group": "@material=roof_slate", "base_color": "#58606c", "roughness": 0.7 } },
    "house_paint": { "type": "wrangle", "in": ["mat_slate"], "note": "Cd follows each prim's material, for viewers and formats that show Cd", "params": { "run_over": "detail", "code": "const table = JSON.parse(detail(0, 'materials') || '{}');\nfor (let p = 0; p < nprims(0); p++) { const m = table[prim(0, 'material', p)]; if (m) setprimattrib(0, 'Cd', p, m.baseColor); }" } },
    "house_survey": { "type": "wrangle", "in": ["house_paint", "plan_clean"], "note": "measures the promises on the finished houses against the lot outlines: per lot the houses whose ground centre lies in it, per house whether every point stays inside that lot, and its lowest point", "params": { "run_over": "detail", "code": "const lotq = [];\nfor (let l = 0; l < nprims(1); l++) lotq.push(primpoints(1, l).map((p) => point(1, 'P', p)));\nconst inside = (q, x, z) => { let pos = 0, neg = 0; for (let i = 0; i < q.length; i++) { const a = q[i], b = q[(i + 1) % q.length], c = (b[0] - a[0]) * (z - a[2]) - (b[2] - a[2]) * (x - a[0]); if (c > 1e-9) pos++; else if (c < -1e-9) neg++; } return !(pos && neg); };\nconst houses = new Map();\nfor (let p = 0; p < nprims(0); p++) { const id = prim(0, 'lot', p); let h = houses.get(id); if (!h) houses.set(id, h = new Set()); for (const pt of primpoints(0, p)) h.add(pt); }\nconst count = lotq.map(() => 0);\nlet across = 0, off = 0;\nfor (const pts of houses.values()) {\n  let x = 0, z = 0, low = Infinity; const P = [...pts].map((pt) => point(0, 'P', pt));\n  for (const q of P) { x += q[0] / P.length; z += q[2] / P.length; low = Math.min(low, q[1]); }\n  if (Math.abs(low) > 1e-4) off++;\n  const l = lotq.findIndex((q) => inside(q, x, z));\n  if (l < 0) { across++; continue; }\n  count[l]++;\n  if (!P.every((q) => inside(lotq[l], q[0], q[2]))) across++;\n}\nsetdetailattrib(0, 'lots_without_one_house', count.filter((c) => c !== 1).length);\nsetdetailattrib(0, 'houses_across_lots', across);\nsetdetailattrib(0, 'houses_off_ground', off);" } },
    "houses_one_each": { "type": "assert", "in": ["house_survey"], "params": { "label": "one_per_lot", "attrib": "lots_without_one_house", "attrib_class": "detail", "attrib_max": 0 } },
    "houses_in_lots": { "type": "assert", "in": ["houses_one_each"], "params": { "label": "one_per_lot", "attrib": "houses_across_lots", "attrib_class": "detail", "attrib_max": 0 } },
    "houses_grounded": { "type": "assert", "in": ["houses_in_lots"], "params": { "label": "grounded", "attrib": "houses_off_ground", "attrib_class": "detail", "attrib_max": 0, "bbox_min": [-100000, 0, -100000], "tolerance": 0.0001 } },
    "houses_clean": { "type": "attrib_delete", "in": ["houses_grounded"], "params": { "names": "lots_without_one_house houses_across_lots houses_off_ground" } },
    "houses": { "type": "assert", "in": ["houses_clean"], "params": { "label": "inside", "bbox_min": ["=-area / 2", -100000, "=-area / 2"], "bbox_max": ["=area / 2", 100000, "=area / 2"], "tolerance": 0.001 } },

    "lot_lines": { "type": "ends", "in": ["plan_clean"], "params": { "mode": "unroll" } },
    "lot_lines_clean": { "type": "attrib_delete", "in": ["lot_lines"], "params": { "names": "floors height roof wall tiles gap depth fx fz" } },
    "lots_out": { "type": "assert", "in": ["lot_lines_clean"], "params": { "label": "inside", "bbox_min": ["=-area / 2", 0, "=-area / 2"], "bbox_max": ["=area / 2", 0, "=area / 2"], "tolerance": 0.001 } },

    "quarter": { "type": "grid", "params": { "size": ["=area", "=area"], "divisions": [1, 1] } },
    "street_faces": { "type": "boolean2d", "in": ["quarter", "blocks"], "note": "the quarter minus the blocks: half a street wide along the quarter's edge, so quarters tile", "params": { "operation": "subtract", "output": "faces" } },
    "street_named": { "type": "attrib_create", "in": ["street_faces"], "params": { "name": "name", "class": "prim", "value": "street" } },
    "street_mat": { "type": "material", "in": ["street_named"], "params": { "name": "asphalt", "base_color": "#7c7e83", "roughness": 0.95 } },
    "street_paint": { "type": "color", "in": ["street_mat"], "params": { "class": "prim", "color": "#7c7e83" } },
    "streets": { "type": "assert", "in": ["street_paint"], "params": { "label": "inside", "bbox_min": ["=-area / 2", 0, "=-area / 2"], "bbox_max": ["=area / 2", 0, "=area / 2"], "tolerance": 0.001 } },

    "yards": { "type": "wrangle", "in": ["plan_clean"], "note": "for the picture only: every lot a garden, in one of three greens so neighbouring lots read apart", "params": { "run_over": "prims", "code": "s@name = 'garden';\ns@material = 'garden_' + 'abc'[Math.floor(rand(seed, @lot, 12) * 3)];" } },
    "yards_a": { "type": "material", "in": ["yards"], "params": { "name": "garden_a", "group": "@material=garden_a", "base_color": "#8e9c6a", "roughness": 0.95 } },
    "yards_b": { "type": "material", "in": ["yards_a"], "params": { "name": "garden_b", "group": "@material=garden_b", "base_color": "#7f9160", "roughness": 0.95 } },
    "yards_c": { "type": "material", "in": ["yards_b"], "params": { "name": "garden_c", "group": "@material=garden_c", "base_color": "#9ba676", "roughness": 0.95 } },
    "town_view": { "type": "merge", "in": ["houses", "streets", "yards_c", "lots_out"], "note": "the picture: the three outputs over the lots shown as gardens" }
  },
  "output": "houses",
  "outputs": {
    "houses": { "node": "houses", "label": "Houses", "kind": "mesh", "tags": ["houses", "town", "buildings", "roofs", "straight-skeleton"], "description": "One house per lot, following the lot's shape inside it with garden all round: walls with windows and a door to the street, and a hipped or pitched straight-skeleton roof on the footprint; prims named house_NN after their lot." },
    "lots": { "node": "lots_out", "label": "Lots", "kind": "curves", "tags": ["lots", "plots", "town", "plan", "outlines"], "description": "The outline of every lot as a closed-loop polyline on the ground, named lot_NN (the house on it is house_NN), for gardens, fences or paths." },
    "streets": { "node": "streets", "label": "Streets", "kind": "mesh", "tags": ["streets", "roads", "town", "plan", "ground"], "description": "The flat street surface between the blocks of lots, half a street wide along the quarter's edge so quarters can tile." }
  }
}
