@chanmeng666/archlang-3d 0.8.1 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,23 @@
1
1
  import {
2
2
  C16,
3
3
  S16
4
- } from "./chunk-LNIVDMPG.js";
4
+ } from "./chunk-ISFIFLC7.js";
5
+ import {
6
+ version
7
+ } from "./chunk-YUI76V23.js";
8
+ import {
9
+ CONTACT_SHADOW,
10
+ FENCE_PALETTE,
11
+ GROUND_PLANE,
12
+ GROUND_SPLITS,
13
+ GROUND_TINT,
14
+ GROUND_WASH,
15
+ STAIR_MATERIAL,
16
+ STAIR_RAIL_MATERIAL,
17
+ TRIM_MATERIAL,
18
+ floorFinish,
19
+ wordFinish
20
+ } from "./chunk-ONUOWO37.js";
5
21
 
6
22
  // src/mesh/extrude.ts
7
23
  import earcut from "earcut";
@@ -157,9 +173,9 @@ function slab2d(a, b, depth) {
157
173
  outer: [p(a[0] + nx, a[1] + ny), p(b[0] + nx, b[1] + ny), p(b[0] - nx, b[1] - ny), p(a[0] - nx, a[1] - ny)]
158
174
  };
159
175
  }
160
- function clipToConvex(ring3, convex) {
176
+ function clipToConvex(ring2, convex) {
161
177
  const c = area2(convex) > 0 ? [...convex] : [...convex].reverse();
162
- let out = ring3.map((p) => [p[0], p[1]]);
178
+ let out = ring2.map((p) => [p[0], p[1]]);
163
179
  for (let k = 0; k < c.length && out.length > 0; k++) {
164
180
  const a = c[k];
165
181
  const b = c[(k + 1) % c.length];
@@ -181,19 +197,19 @@ function clipToConvex(ring3, convex) {
181
197
  const rounded = clean(out.map((p) => [Math.round(p[0]), Math.round(p[1])]));
182
198
  return rounded ? { outer: rounded } : null;
183
199
  }
184
- function insideCut(poly, ring3) {
185
- if (poly.outer.every((p) => where(p, ring3) !== false)) return poly;
186
- return clipToConvex(ring3, poly.outer);
200
+ function insideCut(poly, ring2) {
201
+ if (poly.outer.every((p) => where(p, ring2) !== false)) return poly;
202
+ return clipToConvex(ring2, poly.outer);
187
203
  }
188
204
  var TAU = 2 * Math.PI;
189
205
  var mod2pi = (x) => (x % TAU + TAU) % TAU;
190
206
  function cutSector(cut, jambs, centre) {
191
- const ring3 = clean(cut);
192
- if (!ring3 || ring3.length <= 4) return null;
193
- const n = ring3.length;
194
- const turn = ring3.map((p, i) => {
195
- const a = ring3[(i + n - 1) % n];
196
- const b = ring3[(i + 1) % n];
207
+ const ring2 = clean(cut);
208
+ if (!ring2 || ring2.length <= 4) return null;
209
+ const n = ring2.length;
210
+ const turn = ring2.map((p, i) => {
211
+ const a = ring2[(i + n - 1) % n];
212
+ const b = ring2[(i + 1) % n];
197
213
  const u2 = [p[0] - a[0], p[1] - a[1]];
198
214
  const v2 = [b[0] - p[0], b[1] - p[1]];
199
215
  return Math.abs(
@@ -206,7 +222,7 @@ function cutSector(cut, jambs, centre) {
206
222
  const corners = turn.map((t, i) => [t, i]).sort((a, b) => b[0] - a[0] || a[1] - b[1]).slice(0, 4).map(([, i]) => i).sort((a, b) => a - b);
207
223
  const paths = corners.map((ci, k) => {
208
224
  const cj = corners[(k + 1) % 4];
209
- return { a: ring3[ci], b: ring3[cj], edges: (cj - ci + n) % n };
225
+ return { a: ring2[ci], b: ring2[cj], edges: (cj - ci + n) % n };
210
226
  });
211
227
  const mid = (p) => [(p.a[0] + p.b[0]) / 2, (p.a[1] + p.b[1]) / 2];
212
228
  const d = (p, q) => Math.hypot(p[0] - q[0], p[1] - q[1]);
@@ -245,13 +261,13 @@ function cutSector(cut, jambs, centre) {
245
261
  }
246
262
  function sectorBand(sector, d0, d1, f0 = 0, f1 = 1) {
247
263
  const k = Math.max(1, Math.ceil(sector.steps * (f1 - f0)));
248
- const at = (rr, f) => {
264
+ const at2 = (rr, f) => {
249
265
  const t = sector.t0 + sector.sweep * f;
250
266
  return [Math.round(sector.c[0] + rr * Math.cos(t)), Math.round(sector.c[1] + rr * Math.sin(t))];
251
267
  };
252
268
  const outer = [];
253
- for (let i = 0; i <= k; i++) outer.push(at(sector.r + d1, f0 + (f1 - f0) * i / k));
254
- for (let i = k; i >= 0; i--) outer.push(at(sector.r + d0, f0 + (f1 - f0) * i / k));
269
+ for (let i = 0; i <= k; i++) outer.push(at2(sector.r + d1, f0 + (f1 - f0) * i / k));
270
+ for (let i = k; i >= 0; i--) outer.push(at2(sector.r + d0, f0 + (f1 - f0) * i / k));
255
271
  return { outer };
256
272
  }
257
273
 
@@ -321,9 +337,9 @@ var SIDE_CHAIR = [
321
337
  [6 / 64, 1],
322
338
  [0, 1]
323
339
  ];
324
- function sideways(mat, box3, y0, y1, back, profile) {
340
+ function sideways(mat, box2, y0, y1, back, profile) {
325
341
  const flip = back === "+z" || back === "+x";
326
- return P(mat, box3.x0, box3.x1, box3.z0, box3.z1, y0, y1, {
342
+ return P(mat, box2.x0, box2.x1, box2.z0, box2.z1, y0, y1, {
327
343
  axis: back === "-z" || back === "+z" ? "x" : "z",
328
344
  profile: flip ? profile.map(([u, v]) => [1 - u, v]) : profile
329
345
  });
@@ -777,9 +793,9 @@ var T0_LIVING = {
777
793
  const L = hd - z0;
778
794
  const kb = 0.13;
779
795
  const kz1 = z0 + L * kb;
780
- const at = (u, v) => [-hw + u * W, z0 + v * L];
796
+ const at2 = (u, v) => [-hw + u * W, z0 + v * L];
781
797
  const leg = (u, v) => {
782
- const [x, z] = at(u, v);
798
+ const [x, z] = at2(u, v);
783
799
  return P("wood_dark", x - 32, x + 32, z - 32, z + 32, 0, 620, { round: true });
784
800
  };
785
801
  const cheek = W * 0.06;
@@ -1193,8 +1209,8 @@ var T0_LIVING = {
1193
1209
  const n = 2 * clamp(Math.round(W / (2.2 * D)), 1, 4);
1194
1210
  return span(n, W / 2, 0).flatMap(([a, b], k) => {
1195
1211
  const zig = k % 2 === 0 ? SCREEN_PANEL : SCREEN_PANEL_BACK;
1196
- const at = (mat, y0, y1) => P(mat, a, b, -D / 2, D / 2, y0, y1, { profile: zig });
1197
- return [at("wood", 0, 120), at("fabric", 120, 1600), at("wood", 1600, 1700)];
1212
+ const at2 = (mat, y0, y1) => P(mat, a, b, -D / 2, D / 2, y0, y1, { profile: zig });
1213
+ return [at2("wood", 0, 120), at2("fabric", 120, 1600), at2("wood", 1600, 1700)];
1198
1214
  });
1199
1215
  }
1200
1216
  },
@@ -1571,8 +1587,8 @@ var T0_OFFICE = {
1571
1587
  const depth = (horizontal ? D : W) - 2 * band;
1572
1588
  const n = clamp(Math.round(run2 / (2.2 * (s * 0.26))), 1, 4);
1573
1589
  const scr = Math.max(depth * 0.025, 8);
1574
- const box3 = (u0, u1, v0, v1) => horizontal ? { x0: u0, x1: u1, z0: v0, z1: v1 } : { x0: v0, x1: v1, z0: u0, z1: u1 };
1575
- const at = (mat, b, y0, y1, more = {}) => P(mat, b.x0, b.x1, b.z0, b.z1, y0, y1, more);
1590
+ const box2 = (u0, u1, v0, v1) => horizontal ? { x0: u0, x1: u1, z0: v0, z1: v1 } : { x0: v0, x1: v1, z0: u0, z1: u1 };
1591
+ const at2 = (mat, b, y0, y1, more = {}) => P(mat, b.x0, b.x1, b.z0, b.z1, y0, y1, more);
1576
1592
  const off = scr + depth * 0.06;
1577
1593
  const mh = Math.max(depth * 0.04, 12);
1578
1594
  const mw = run2 / n * 0.38;
@@ -1581,22 +1597,22 @@ var T0_OFFICE = {
1581
1597
  (u) => [-1, 1].flatMap((sgn) => {
1582
1598
  const v = sgn * (off + mh / 2);
1583
1599
  const side = horizontal ? sgn < 0 ? "-z" : "+z" : sgn < 0 ? "-x" : "+x";
1584
- const chair = sgn < 0 ? box3(u - cw / 2, u + cw / 2, -depth / 2 - band, -depth / 2) : box3(u - cw / 2, u + cw / 2, depth / 2, depth / 2 + band);
1600
+ const chair = sgn < 0 ? box2(u - cw / 2, u + cw / 2, -depth / 2 - band, -depth / 2) : box2(u - cw / 2, u + cw / 2, depth / 2, depth / 2 + band);
1585
1601
  return [
1586
- at("screen", box3(u - mw / 2, u + mw / 2, v - mh / 2, v + mh / 2), 745, 1080, { radius: 4 }),
1602
+ at2("screen", box2(u - mw / 2, u + mw / 2, v - mh / 2, v + mh / 2), 745, 1080, { radius: 4 }),
1587
1603
  sideways("fabric", chair, 0, 820, side, SIDE_CHAIR)
1588
1604
  ];
1589
1605
  })
1590
1606
  );
1591
- const leg = box3(-run2 / 2 + 10, -run2 / 2 + 40, -depth / 2 + 20, depth / 2 - 20);
1592
- const legs = horizontal ? pair(at("wood", leg, 0, 720)) : [
1593
- at("wood", leg, 0, 720),
1594
- at("wood", box3(run2 / 2 - 40, run2 / 2 - 10, -depth / 2 + 20, depth / 2 - 20), 0, 720)
1607
+ const leg = box2(-run2 / 2 + 10, -run2 / 2 + 40, -depth / 2 + 20, depth / 2 - 20);
1608
+ const legs = horizontal ? pair(at2("wood", leg, 0, 720)) : [
1609
+ at2("wood", leg, 0, 720),
1610
+ at2("wood", box2(run2 / 2 - 40, run2 / 2 - 10, -depth / 2 + 20, depth / 2 - 20), 0, 720)
1595
1611
  ];
1596
1612
  return [
1597
- at("wood", box3(-run2 / 2, run2 / 2, -depth / 2, depth / 2), 720, 745, { radius: 6 }),
1613
+ at2("wood", box2(-run2 / 2, run2 / 2, -depth / 2, depth / 2), 720, 745, { radius: 6 }),
1598
1614
  ...legs,
1599
- at("fabric", box3(-run2 * 0.49, run2 * 0.49, -scr, scr), 745, 1200, { radius: scr }),
1615
+ at2("fabric", box2(-run2 * 0.49, run2 * 0.49, -scr, scr), 745, 1200, { radius: scr }),
1600
1616
  ...desks
1601
1617
  ];
1602
1618
  }
@@ -2202,12 +2218,12 @@ var T0_OUTDOOR = {
2202
2218
  const [rx, rz] = [W / 2, D / 2];
2203
2219
  const s = Math.min(W, D);
2204
2220
  const lg = clamp(s * 0.012, 12, 30);
2205
- const at = (cx, cz) => P("metal", cx - lg, cx + lg, cz - lg, cz + lg, 0, 840, { round: true });
2221
+ const at2 = (cx, cz) => P("metal", cx - lg, cx + lg, cz - lg, cz + lg, 0, 840, { round: true });
2206
2222
  const [lx, lz] = [rx * 0.6, rz * 0.6];
2207
2223
  const inner = (f) => ({ x0: -rx * f, x1: rx * f, z0: -rz * f, z1: rz * f, round: true });
2208
2224
  return [
2209
- ...pair(at(-lx, -lz)),
2210
- ...pair(at(-lx, lz)),
2225
+ ...pair(at2(-lx, -lz)),
2226
+ ...pair(at2(-lx, lz)),
2211
2227
  P("metal", -rx * 0.66, rx * 0.66, -rz * 0.66, rz * 0.66, 0, 30, { round: true, holes: [inner(0.58)] }),
2212
2228
  P("metal", -rx * 0.98, rx * 0.98, -rz * 0.98, rz * 0.98, 820, 850, { round: true, holes: [inner(0.86)] }),
2213
2229
  P("rubber", -rx * 0.82, rx * 0.82, -rz * 0.82, rz * 0.82, 840, 856, { round: true }),
@@ -2291,7 +2307,7 @@ var T0_OUTDOOR = {
2291
2307
  const h = s * 0.03;
2292
2308
  const nx = clamp(Math.round(W / (s * 0.25)) - 1, 1, 9);
2293
2309
  const nz = clamp(Math.round(D / (s * 0.25)) - 1, 1, 9);
2294
- const at = (n, len) => Array.from({ length: n }, (_, k) => len * (2 * (k + 1) - n - 1) / (2 * (n + 1)));
2310
+ const at2 = (n, len) => Array.from({ length: n }, (_, k) => len * (2 * (k + 1) - n - 1) / (2 * (n + 1)));
2295
2311
  const rw = clamp(s * 0.02, 12, 45);
2296
2312
  const posts = [-pz, pz].flatMap((z) => [
2297
2313
  ...pair(P("stone", -px - h * 1.6, -px + h * 1.6, z - h * 1.6, z + h * 1.6, 0, 120, { radius: 4 })),
@@ -2300,8 +2316,8 @@ var T0_OUTDOOR = {
2300
2316
  return [
2301
2317
  ...posts,
2302
2318
  ...[-pz, pz].map((z) => P("wood", -hw, hw, z - h, z + h, 2300, 2460, { radius: 3 })),
2303
- ...at(nx, W).map((x) => P("wood", x - rw / 2, x + rw / 2, -hd, hd, 2460, 2560, { radius: 2 })),
2304
- ...at(nz, D).map((z) => P("wood", -hw, hw, z - rw * 0.4, z + rw * 0.4, 2560, 2600, { radius: 2 }))
2319
+ ...at2(nx, W).map((x) => P("wood", x - rw / 2, x + rw / 2, -hd, hd, 2460, 2560, { radius: 2 })),
2320
+ ...at2(nz, D).map((z) => P("wood", -hw, hw, z - rw * 0.4, z + rw * 0.4, 2560, 2600, { radius: 2 }))
2305
2321
  ];
2306
2322
  }
2307
2323
  },
@@ -2345,12 +2361,12 @@ var T0_OUTDOOR = {
2345
2361
  gen(W, D) {
2346
2362
  const [rx, rz] = [W / 2, D / 2];
2347
2363
  const bowl = { x0: -rx * 0.68, x1: rx * 0.68, z0: -rz * 0.68, z1: rz * 0.68, round: true };
2348
- const ring3 = (f2) => ({ x0: -rx * f2, x1: rx * f2, z0: -rz * f2, z1: rz * f2, round: true });
2364
+ const ring2 = (f2) => ({ x0: -rx * f2, x1: rx * f2, z0: -rz * f2, z1: rz * f2, round: true });
2349
2365
  const lg = Math.min(rx, rz) * 0.08;
2350
2366
  const [n, f] = [0.12, 0.58];
2351
2367
  return [
2352
2368
  P("stone", -rx * 0.97, rx * 0.97, -rz * 0.97, rz * 0.97, 0, 360, { round: true, holes: [bowl] }),
2353
- P("stone", -rx, rx, -rz, rz, 360, 420, { round: true, holes: [ring3(0.64)] }),
2369
+ P("stone", -rx, rx, -rz, rz, 360, 420, { round: true, holes: [ring2(0.64)] }),
2354
2370
  P("metal", bowl.x0 - 10, bowl.x1 + 10, bowl.z0 - 10, bowl.z1 + 10, 0, 220, { round: true }),
2355
2371
  ...pair(P("wood_dark", -rx * f, -rx * n, -lg, lg, 220, 220 + 2 * lg, { axis: "x", round: true })),
2356
2372
  P("wood_dark", -lg, lg, -rz * f, -rz * n, 220 + 2 * lg, 220 + 4 * lg, { axis: "z", round: true }),
@@ -2369,7 +2385,7 @@ var T0_OUTDOOR = {
2369
2385
  const [wx, wz] = [hw - ov, hd - ov];
2370
2386
  const eaves = 1900;
2371
2387
  const dw = Math.max(0, Math.min(W * 0.36, (D - 2 * ov) * 1.2)) / 2;
2372
- const along = W >= D;
2388
+ const along2 = W >= D;
2373
2389
  const gable = [
2374
2390
  [0, 0],
2375
2391
  [1, 0],
@@ -2379,7 +2395,7 @@ var T0_OUTDOOR = {
2379
2395
  return [
2380
2396
  P("wood", -wx, wx, -wz, wz, 0, eaves),
2381
2397
  P("wood_dark", -hw, hw, -hd, hd, eaves - 20, eaves + 20),
2382
- P("wood_dark", -hw, hw, -hd, hd, eaves + 20, 2300, { axis: along ? "x" : "z", profile: gable }),
2398
+ P("wood_dark", -hw, hw, -hd, hd, eaves + 20, 2300, { axis: along2 ? "x" : "z", profile: gable }),
2383
2399
  ...span(2, dw, 6).map(([a, b]) => P("wood_dark", a, b, wz, Math.min(wz + 20, hd), 30, 1780)),
2384
2400
  ...pair(P("metal", -26, -12, Math.min(wz + 20, hd) - 2, Math.min(wz + 40, hd), 950, 1100, { radius: 4 })),
2385
2401
  ...pair(P("glass", -wx - 10, -wx, -win, win, 1150, 1550))
@@ -2520,8 +2536,8 @@ var T0_OUTDOOR = {
2520
2536
  materials: ["metal", "rubber"],
2521
2537
  gen(W, D) {
2522
2538
  const [rx, rz] = [W / 2, D / 2];
2523
- const at = (f, y0, y1, mat = "metal") => P(mat, -rx * f, rx * f, -rz * f, rz * f, y0, y1, { round: true });
2524
- return [at(0.92, 0, 760), at(1, 600, 660, "rubber"), at(0.8, 760, 840), at(0.64, 840, 880), at(0.3, 880, 900)];
2539
+ const at2 = (f, y0, y1, mat = "metal") => P(mat, -rx * f, rx * f, -rz * f, rz * f, y0, y1, { round: true });
2540
+ return [at2(0.92, 0, 760), at2(1, 600, 660, "rubber"), at2(0.8, 760, 840), at2(0.64, 840, 880), at2(0.3, 880, 900)];
2525
2541
  }
2526
2542
  },
2527
2543
  bike_rack: {
@@ -3499,8 +3515,8 @@ function sectionRing(u0, u1, v0, v1, shape = {}) {
3499
3515
  if (shape.profile) {
3500
3516
  if (shape.profile.some(([fu, fv]) => !(fu >= 0 && fu <= 1 && fv >= 0 && fv <= 1)))
3501
3517
  throw new RangeError("a T0 profile point lies outside [0, 1]\xB2");
3502
- const at = (f, a, b, c) => f === 0 ? a : f === 1 ? b : c + (f - 0.5) * (b - a);
3503
- pts = shape.profile.map(([fu, fv]) => [at(fu, u0, u1, cu), at(fv, v0, v1, cv)]);
3518
+ const at2 = (f, a, b, c) => f === 0 ? a : f === 1 ? b : c + (f - 0.5) * (b - a);
3519
+ pts = shape.profile.map(([fu, fv]) => [at2(fu, u0, u1, cu), at2(fv, v0, v1, cv)]);
3504
3520
  } else if (shape.round) {
3505
3521
  pts = C16.map((c, k) => [cu + ru * c, cv + rv * S16[k]]);
3506
3522
  } else if (shape.radius && shape.radius > 0) {
@@ -3525,14 +3541,14 @@ function sectionRing(u0, u1, v0, v1, shape = {}) {
3525
3541
  [u0, v1]
3526
3542
  ];
3527
3543
  }
3528
- const ring3 = [];
3544
+ const ring2 = [];
3529
3545
  for (const [u, v] of pts) {
3530
3546
  const p = [um(u), um(v)];
3531
- const q = ring3[ring3.length - 1];
3532
- if (!q || q[0] !== p[0] || q[1] !== p[1]) ring3.push(p);
3547
+ const q = ring2[ring2.length - 1];
3548
+ if (!q || q[0] !== p[0] || q[1] !== p[1]) ring2.push(p);
3533
3549
  }
3534
- while (ring3.length > 1 && ring3[0]?.[0] === ring3.at(-1)?.[0] && ring3[0]?.[1] === ring3.at(-1)?.[1]) ring3.pop();
3535
- return ring3;
3550
+ while (ring2.length > 1 && ring2[0]?.[0] === ring2.at(-1)?.[0] && ring2[0]?.[1] === ring2.at(-1)?.[1]) ring2.pop();
3551
+ return ring2;
3536
3552
  }
3537
3553
  function partShell(p, dropped) {
3538
3554
  if (p.axis && p.holes?.length) throw new Error("a sideways T0 part takes no holes");
@@ -3554,8 +3570,8 @@ function partShell(p, dropped) {
3554
3570
  }
3555
3571
  } else {
3556
3572
  const x = p.axis === "x";
3557
- const ring3 = x ? sectionRing(p.z0, p.z1, p.y0, p.y1, p) : sectionRing(p.x0, p.x1, p.y0, p.y1, p);
3558
- const up = x ? prism({ outer: ring3 }, um(p.x0), um(p.x1)) : prism({ outer: ring3 }, um(p.z0), um(p.z1));
3573
+ const ring2 = x ? sectionRing(p.z0, p.z1, p.y0, p.y1, p) : sectionRing(p.x0, p.x1, p.y0, p.y1, p);
3574
+ const up = x ? prism({ outer: ring2 }, um(p.x0), um(p.x1)) : prism({ outer: ring2 }, um(p.z0), um(p.z1));
3559
3575
  if (up) {
3560
3576
  const positions = [];
3561
3577
  for (let i = 0; i < up.positions.length; i += 3) {
@@ -3599,13 +3615,13 @@ function loftShell(p, table) {
3599
3615
  });
3600
3616
  const [ax, az] = [um(cx), um(cz)];
3601
3617
  const indices = [];
3602
- const at = (i) => [
3618
+ const at2 = (i) => [
3603
3619
  positions[3 * i],
3604
3620
  positions[3 * i + 1],
3605
3621
  positions[3 * i + 2]
3606
3622
  ];
3607
3623
  const face = (a, b, c, want) => {
3608
- const [A, B, C] = [at(a), at(b), at(c)];
3624
+ const [A, B, C] = [at2(a), at2(b), at2(c)];
3609
3625
  const u = [B[0] - A[0], B[1] - A[1], B[2] - A[2]];
3610
3626
  const w = [C[0] - A[0], C[1] - A[1], C[2] - A[2]];
3611
3627
  const nx = u[1] * w[2] - u[2] * w[1];
@@ -3623,7 +3639,7 @@ function loftShell(p, table) {
3623
3639
  for (let i = 0; i < n; i++) {
3624
3640
  const j = (i + 1) % n;
3625
3641
  const [a, b, c, d] = [k * n + i, k * n + j, (k + 1) * n + j, (k + 1) * n + i];
3626
- const m = [a, b, c, d].map(at);
3642
+ const m = [a, b, c, d].map(at2);
3627
3643
  const mx = m.reduce((s, q) => s + q[0], 0) / 4 - ax;
3628
3644
  const mz = m.reduce((s, q) => s + q[2], 0) / 4 - az;
3629
3645
  face(a, b, c, [mx, 0, mz]);
@@ -3750,11 +3766,11 @@ function wallOwnership(walls, tessellate = loopsToPolygons) {
3750
3766
  };
3751
3767
  const rings = byFace(joined?.result.split?.fills ?? []);
3752
3768
  const bodies = byFace(withCuts?.result.split?.fills ?? []);
3753
- const owned = walls.map((wall2, i) => {
3769
+ const owned = walls.map((wall, i) => {
3754
3770
  const pieces = piecesOf(rings.get(i) ?? []);
3755
3771
  return {
3756
3772
  index: i,
3757
- wall: wall2,
3773
+ wall,
3758
3774
  pieces,
3759
3775
  body: piecesOf(bodies.get(i) ?? []),
3760
3776
  area: pieces.reduce((s, p) => s + pieceArea(p), 0)
@@ -3823,10 +3839,10 @@ function triangles(p) {
3823
3839
  for (const q of r) flat.push(q[0], q[1]);
3824
3840
  });
3825
3841
  const ix = earcut2(flat, holes, 2);
3826
- const pt2 = (i) => [flat[2 * i], flat[2 * i + 1]];
3842
+ const pt = (i) => [flat[2 * i], flat[2 * i + 1]];
3827
3843
  const out = [];
3828
3844
  for (let t = 0; t < ix.length; t += 3)
3829
- out.push([pt2(ix[t]), pt2(ix[t + 1]), pt2(ix[t + 2])]);
3845
+ out.push([pt(ix[t]), pt(ix[t + 1]), pt(ix[t + 2])]);
3830
3846
  return out;
3831
3847
  }
3832
3848
  var cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
@@ -3901,12 +3917,9 @@ function floorLifts(floors, overlaps) {
3901
3917
  const below = overlaps.filter((o) => o.rooms[1] === f.id).map((o) => steps.get(o.rooms[0]) ?? 0);
3902
3918
  steps.set(f.id, below.length > 0 ? 1 + Math.max(...below) : 0);
3903
3919
  }
3904
- return new Map([...steps].filter(([, s]) => s > 0).map(([id2, s]) => [id2, s * FLOOR_LIFT_UM]));
3920
+ return new Map([...steps].filter(([, s]) => s > 0).map(([id, s]) => [id, s * FLOOR_LIFT_UM]));
3905
3921
  }
3906
3922
 
3907
- // package.json
3908
- var version = "0.8.1";
3909
-
3910
3923
  // src/scene3d/build.ts
3911
3924
  import * as core2 from "@chanmeng666/archlang";
3912
3925
  import {
@@ -3947,11 +3960,11 @@ function circleRing(c, r, tol = DEFAULT_CURVE_TOL_MM) {
3947
3960
  var ARCHLANG_3D_VERSION = version;
3948
3961
  var ring = (r) => r.map(umPoint);
3949
3962
  var source = (e) => e.span ? { kind: e.kind, id: e.id, span: [e.span.start, e.span.end] } : { kind: e.kind, id: e.id };
3950
- var rectRing = (at, size) => [
3951
- { x: at.x, y: at.y },
3952
- { x: at.x + size.w, y: at.y },
3953
- { x: at.x + size.w, y: at.y + size.h },
3954
- { x: at.x, y: at.y + size.h }
3963
+ var rectRing = (at2, size) => [
3964
+ { x: at2.x, y: at2.y },
3965
+ { x: at2.x + size.w, y: at2.y },
3966
+ { x: at2.x + size.w, y: at2.y + size.h },
3967
+ { x: at2.x, y: at2.y + size.h }
3955
3968
  ];
3956
3969
  var polygon = (outer, holes = []) => holes.length > 0 ? { outer: ring(outer), holes: holes.map(ring) } : { outer: ring(outer) };
3957
3970
  var box = (e) => ({
@@ -3961,7 +3974,8 @@ var box = (e) => ({
3961
3974
  size: { w: um2(e.size.w), h: um2(e.size.h) }
3962
3975
  });
3963
3976
  function floorOf(r, tol) {
3964
- const outer = r.circle ? circleRing(r.circle.c, r.circle.r, tol) : r.poly ?? rectRing(r.at, r.size);
3977
+ const edges = r.edges;
3978
+ const outer = r.circle ? circleRing(r.circle.c, r.circle.r, tol) : edges ? tessellateLoops([edges], tol)[0] : r.poly ?? rectRing(r.at, r.size);
3965
3979
  return {
3966
3980
  id: r.id,
3967
3981
  source: source(r),
@@ -4018,13 +4032,13 @@ var fenceOf = (f) => ({
4018
4032
  });
4019
4033
  var OPPOSITE = { top: "bottom", bottom: "top", left: "right", right: "left" };
4020
4034
  function stairPieces(s, ledger) {
4021
- const treadCount3 = core2.treadCount;
4022
- if (typeof treadCount3 !== "function") return void 0;
4023
- const flight = (at, size, tail2) => ({
4024
- at: umPoint(at),
4035
+ const treadCount2 = core2.treadCount;
4036
+ if (typeof treadCount2 !== "function") return void 0;
4037
+ const flight = (at2, size, tail2) => ({
4038
+ at: umPoint(at2),
4025
4039
  size: { w: um2(size.w), h: um2(size.h) },
4026
4040
  foot: s.dir === "up" ? tail2 : OPPOSITE[tail2],
4027
- treads: treadCount3(tail2 === "top" || tail2 === "bottom" ? size.h : size.w)
4041
+ treads: treadCount2(tail2 === "top" || tail2 === "bottom" ? size.h : size.w)
4028
4042
  });
4029
4043
  if (s.flights && s.landings) {
4030
4044
  const flights = s.flights;
@@ -4053,14 +4067,14 @@ var rectOf = (p) => ({
4053
4067
  w: p.size.w,
4054
4068
  h: p.size.h
4055
4069
  });
4056
- function winderRings(id2, i, l, before, after, ledger) {
4070
+ function winderRings(id, i, l, before, after, ledger) {
4057
4071
  if (l.winders === void 0) return void 0;
4058
4072
  const lines = core2.winderTreads;
4059
4073
  const rings = typeof lines === "function" && before && after ? treadRings(l, before, lines(l, rectOf(before), rectOf(after))) : void 0;
4060
4074
  if (!rings)
4061
4075
  ledger.push({
4062
4076
  kind: "stair",
4063
- id: id2,
4077
+ id,
4064
4078
  field: `winders(landing ${i + 1})`,
4065
4079
  value: "level",
4066
4080
  provenance: "default",
@@ -4123,9 +4137,9 @@ function wallsAndOpenings(ir, byId, tol) {
4123
4137
  for (const cut of own.cuts) {
4124
4138
  const host = ir.walls[cut.wall];
4125
4139
  const op = cut.opening;
4126
- const id2 = op.ownerId;
4127
- if (!host || !id2) continue;
4128
- const el = byId.get(id2);
4140
+ const id = op.ownerId;
4141
+ if (!host || !id) continue;
4142
+ const el = byId.get(id);
4129
4143
  const kind = op.kind ?? (el?.kind === "door" || el?.kind === "window" || el?.kind === "opening" ? el.kind : "opening");
4130
4144
  const outer = tessellateLoops([cut.loop], tol)[0] ?? [];
4131
4145
  const sill = op.sill;
@@ -4136,8 +4150,8 @@ function wallsAndOpenings(ir, byId, tol) {
4136
4150
  const jambs = jambsOf(op.at, op.width, hosted?.host ?? null);
4137
4151
  const leaves = door ? doorLeaves(door).map((l) => [umPoint(l.hinge), umPoint(l.farJamb), umPoint(l.leafEnd)]) : [];
4138
4152
  openings.push({
4139
- id: id2,
4140
- source: el ? source(el) : { kind, id: id2 },
4153
+ id,
4154
+ source: el ? source(el) : { kind, id },
4141
4155
  kind,
4142
4156
  ...door?.doorKind ? { doorKind: door.doorKind } : door ? { doorKind: "hinged" } : {},
4143
4157
  host: host.id,
@@ -4151,7 +4165,7 @@ function wallsAndOpenings(ir, byId, tol) {
4151
4165
  ...door ? { hinge: door.hinge, swing: door.swing } : {},
4152
4166
  ...door?.slide ? { slide: door.slide } : {}
4153
4167
  });
4154
- cutsOf.set(cut.wall, [...cutsOf.get(cut.wall) ?? [], id2]);
4168
+ cutsOf.set(cut.wall, [...cutsOf.get(cut.wall) ?? [], id]);
4155
4169
  }
4156
4170
  const walls = own.owned.map((o) => ({
4157
4171
  id: o.wall.id,
@@ -4202,9 +4216,9 @@ function demolishedOf(ir, standing, ledger, tol) {
4202
4216
  ];
4203
4217
  });
4204
4218
  }
4205
- function jambsOf(at, width, host) {
4206
- const s = host ? doorSwing({ at, width, hinge: "left", swing: "in", host }) : null;
4207
- return s ? [umPoint(s.hinge), umPoint(s.farJamb)] : [umPoint(at), umPoint(at)];
4219
+ function jambsOf(at2, width, host) {
4220
+ const s = host ? doorSwing({ at: at2, width, hinge: "left", swing: "in", host }) : null;
4221
+ return s ? [umPoint(s.hinge), umPoint(s.farJamb)] : [umPoint(at2), umPoint(at2)];
4208
4222
  }
4209
4223
  function levelOf(s, ledger, tol, mirrored) {
4210
4224
  const { ir } = s;
@@ -4321,232 +4335,2212 @@ function buildScene3D(storeys, opts) {
4321
4335
  };
4322
4336
  }
4323
4337
 
4324
- // src/scene3d/validate.ts
4325
- var isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
4326
- var fail = (out, path, message) => {
4327
- out.push({ path, message });
4328
- };
4329
- var integer = (v, p, out) => {
4330
- if (!Number.isInteger(v)) fail(out, p, "must be an integer (micrometres)");
4331
- };
4332
- var number = (v, p, out) => {
4333
- if (typeof v !== "number" || !Number.isFinite(v)) fail(out, p, "must be a finite number");
4334
- };
4335
- var string = (v, p, out) => {
4336
- if (typeof v !== "string") fail(out, p, "must be a string");
4338
+ // src/adapter/adapt.ts
4339
+ import * as core3 from "@chanmeng666/archlang";
4340
+ var MIN_CORE = "1.61.0";
4341
+ var hasHanded = (storeys) => storeys.some((s) => s.ir.elements.some((e) => e.kind === "furniture" && isHandedAsset(assetFor(e.category))));
4342
+ function mirroredIds(source2, world, n) {
4343
+ const unknown = Array.from({ length: n }, () => null);
4344
+ const planToJson2 = core3.planToJson;
4345
+ if (typeof planToJson2 !== "function") return unknown;
4346
+ const r = planToJson2(source2, world ? { world } : {});
4347
+ const pages = r.pages ? r.pages.map((p) => p.json) : r.json ? [r.json] : [];
4348
+ if (pages.length !== n) return unknown;
4349
+ return pages.map((j) => new Set(j.furniture.flatMap((f) => f.mirror === true && f.id !== void 0 ? [f.id] : [])));
4350
+ }
4351
+ function adaptPlan(source2, opts = {}) {
4352
+ const resolvePlan2 = core3.resolvePlan;
4353
+ const hatches = core3.hatchKey;
4354
+ if (typeof resolvePlan2 !== "function" || typeof hatches !== "function") {
4355
+ return {
4356
+ ok: false,
4357
+ refusal: {
4358
+ code: "E3D_CORE_TOO_OLD",
4359
+ message: `@chanmeng666/archlang is too old (no resolvePlan or hatchKey); archlang-3d needs ${MIN_CORE} or later.`
4360
+ }
4361
+ };
4362
+ }
4363
+ const res = resolvePlan2(source2, opts.world ? { world: opts.world } : {});
4364
+ const errors = res.diagnostics.filter((d) => d.severity === "error");
4365
+ if (!res.ir || errors.length > 0) {
4366
+ const codes = [...new Set(errors.map((d) => d.code))].join(", ");
4367
+ return {
4368
+ ok: false,
4369
+ refusal: {
4370
+ code: "E3D_PLAN_ERRORS",
4371
+ message: `The plan does not resolve (${codes || "no IR"}); fix it with \`arch validate\` first.`,
4372
+ diagnostics: errors
4373
+ }
4374
+ };
4375
+ }
4376
+ const storeys = res.levels.length > 0 ? res.levels.map((l) => ({ level: l.level, ...l.name ? { name: l.name } : {}, ir: l.ir })) : [{ level: 1, ir: res.ir }];
4377
+ if (storeys.every((s) => s.ir.walls.length === 0)) {
4378
+ return {
4379
+ ok: false,
4380
+ refusal: { code: "E3D_NO_WALLS", message: "The plan has no walls, so there is nothing to raise." }
4381
+ };
4382
+ }
4383
+ const scene = buildScene3D(storeys, {
4384
+ coreVersion: opts.coreVersion ?? "unknown",
4385
+ ...opts.curveTolMm !== void 0 ? { curveTolMm: opts.curveTolMm } : {},
4386
+ ...hasHanded(storeys) ? { mirrored: mirroredIds(source2, opts.world, storeys.length) } : {}
4387
+ });
4388
+ return {
4389
+ ok: true,
4390
+ scene,
4391
+ storeys,
4392
+ diagnostics: res.diagnostics,
4393
+ warnings: [
4394
+ ...storeys.flatMap((s) => mixedHeightJoints(wallOwnership(s.ir.walls)).map((w) => ({ ...w, level: s.level }))),
4395
+ ...scene.levels.flatMap((lv) => floorOverlaps(lv.floors).map((w) => ({ ...w, level: lv.level })))
4396
+ ]
4397
+ };
4398
+ }
4399
+
4400
+ // src/mesh/slab.ts
4401
+ var rectPolygon = (r) => ({
4402
+ outer: [
4403
+ [r[0], r[1]],
4404
+ [r[2], r[1]],
4405
+ [r[2], r[3]],
4406
+ [r[0], r[3]]
4407
+ ]
4408
+ });
4409
+ function rectMinus(a, b) {
4410
+ const [x0, y0, x1, y1] = [Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.min(a[2], b[2]), Math.min(a[3], b[3])];
4411
+ if (!(x0 < x1 && y0 < y1)) return [a];
4412
+ const out = [];
4413
+ if (a[1] < y0) out.push([a[0], a[1], a[2], y0]);
4414
+ if (y1 < a[3]) out.push([a[0], y1, a[2], a[3]]);
4415
+ if (a[0] < x0) out.push([a[0], y0, x0, y1]);
4416
+ if (x1 < a[2]) out.push([x1, y0, a[2], y1]);
4417
+ return out;
4418
+ }
4419
+ var rectilinear = (r) => r.every((p, i) => {
4420
+ const q = r[(i + 1) % r.length];
4421
+ return p[0] === q[0] || p[1] === q[1];
4422
+ });
4423
+ function rectsOf(r) {
4424
+ const ys = [...new Set(r.map((p) => p[1]))].sort((a, b) => a - b);
4425
+ const out = [];
4426
+ for (let k = 0; k + 1 < ys.length; k++) {
4427
+ const [y0, y1] = [ys[k], ys[k + 1]];
4428
+ const xs = [];
4429
+ for (let i = 0; i < r.length; i++) {
4430
+ const a = r[i];
4431
+ const b = r[(i + 1) % r.length];
4432
+ if (a[0] === b[0] && Math.min(a[1], b[1]) <= y0 && Math.max(a[1], b[1]) >= y1) xs.push(a[0]);
4433
+ }
4434
+ xs.sort((a, b) => a - b);
4435
+ for (let i = 0; i + 1 < xs.length; i += 2)
4436
+ if (xs[i] < xs[i + 1]) out.push([xs[i], y0, xs[i + 1], y1]);
4437
+ }
4438
+ return out;
4439
+ }
4440
+ function covered(room, rings) {
4441
+ const probes = room.flatMap((p, i) => {
4442
+ const q = room[(i + 1) % room.length];
4443
+ return [p, [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2]];
4444
+ });
4445
+ return rings.some((r) => probes.every((p) => where(p, r) !== false) && !r.some((p) => where(p, room) === true));
4446
+ }
4447
+ function apart2(room, rings) {
4448
+ return rings.every((r) => room.every((p) => where(p, r) !== true) && r.every((p) => where(p, room) !== true));
4449
+ }
4450
+ function slabRegion(footprint, floors, ledger) {
4451
+ const out = footprint.map((r) => ({ outer: [...r] }));
4452
+ const exact = footprint.every(rectilinear);
4453
+ let taken = exact ? footprint.flatMap(rectsOf) : [];
4454
+ const added = [];
4455
+ for (const f of floors) {
4456
+ const room = f.polygon.outer;
4457
+ const unsplit = () => {
4458
+ ledger.push({
4459
+ kind: "slab",
4460
+ id: f.id,
4461
+ field: "union",
4462
+ value: "room overlaps the outline or an earlier room: added whole",
4463
+ provenance: "default",
4464
+ rule: "slab-room-unsplit"
4465
+ });
4466
+ };
4467
+ if (exact && rectilinear(room)) {
4468
+ let pieces = rectsOf(room);
4469
+ for (const t of taken) pieces = pieces.flatMap((p) => rectMinus(p, t));
4470
+ taken = [...taken, ...pieces];
4471
+ for (const p of pieces) out.push(rectPolygon(p));
4472
+ if (!pieces.every((p) => apart2(rectPolygon(p).outer, added))) unsplit();
4473
+ continue;
4474
+ }
4475
+ const others = [...footprint, ...added, ...taken.map((t) => rectPolygon(t).outer)];
4476
+ if (covered(room, others)) continue;
4477
+ if (!apart2(room, others)) unsplit();
4478
+ out.push({ outer: [...room] });
4479
+ added.push(room);
4480
+ }
4481
+ return out;
4482
+ }
4483
+ var PROBE_MIN = 2;
4484
+ var PROBE_OFF = 1;
4485
+ function meets(a, b, c, d) {
4486
+ const rx = b[0] - a[0];
4487
+ const ry = b[1] - a[1];
4488
+ const sx = d[0] - c[0];
4489
+ const sy = d[1] - c[1];
4490
+ const qx = c[0] - a[0];
4491
+ const qy = c[1] - a[1];
4492
+ const den = rx * sy - ry * sx;
4493
+ if (den !== 0) {
4494
+ const t = (qx * sy - qy * sx) / den;
4495
+ const u = (qx * ry - qy * rx) / den;
4496
+ return t >= 0 && t <= 1 && u >= 0 && u <= 1 ? [t] : [];
4497
+ }
4498
+ if (qx * ry - qy * rx !== 0) return [];
4499
+ const rr = rx * rx + ry * ry;
4500
+ return [qx * rx + qy * ry, (d[0] - a[0]) * rx + (d[1] - a[1]) * ry].map((x) => x / rr).filter((t) => t > 0 && t < 1);
4501
+ }
4502
+ function stretches(a, b, cutters) {
4503
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
4504
+ const ts = [0, 1, ...cutters.flatMap(([c, d]) => meets(a, b, c, d))].sort((x, y) => x - y);
4505
+ const out = [];
4506
+ for (let i = 1; i < ts.length; i++) {
4507
+ const t0 = ts[i - 1];
4508
+ const t1 = ts[i];
4509
+ if ((t1 - t0) * len < PROBE_MIN) continue;
4510
+ const t = (t0 + t1) / 2;
4511
+ out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]);
4512
+ }
4513
+ return out;
4514
+ }
4515
+ var edgesOf = (r) => r.map((p, i) => [p, r[(i + 1) % r.length]]);
4516
+ function coveredBy(ring2, region) {
4517
+ if (region.length === 0 || ring2.length < 3) return false;
4518
+ const inRegion = (p) => region.some((s) => where(p, s.outer) !== false && !(s.holes ?? []).some((h) => where(p, h) === true));
4519
+ const ringEdges = edgesOf(ring2);
4520
+ const regionEdges = region.flatMap((s) => [s.outer, ...s.holes ?? []].flatMap(edgesOf));
4521
+ for (const [a, b] of ringEdges) {
4522
+ if (!inRegion(a)) return false;
4523
+ if (!stretches(a, b, regionEdges).every(inRegion)) return false;
4524
+ }
4525
+ for (const [a, b] of regionEdges) {
4526
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
4527
+ if (len === 0) continue;
4528
+ const nx = -(b[1] - a[1]) / len * PROBE_OFF;
4529
+ const ny = (b[0] - a[0]) / len * PROBE_OFF;
4530
+ for (const m of stretches(a, b, ringEdges)) {
4531
+ if (where(m, ring2) !== true) continue;
4532
+ if (!inRegion([m[0] + nx, m[1] + ny]) || !inRegion([m[0] - nx, m[1] - ny])) return false;
4533
+ }
4534
+ }
4535
+ return true;
4536
+ }
4537
+
4538
+ // src/mesh/openings.ts
4539
+ var MM = 1e3;
4540
+ var clamp2 = (x, lo, hi) => Math.max(lo, Math.min(hi, x));
4541
+ var share = (k, t, lo, hi) => clamp2(Math.round(k * t), lo, hi);
4542
+ var OPENING_DETAIL = {
4543
+ /** The slot a door's lining leaves for its leaf: the leaf's own thickness (`LEAF_MM`). */
4544
+ slot: 40,
4545
+ lining: (t) => share(0.1, t, 12, 25),
4546
+ casingWidth: (t) => share(0.35, t, 50, 90),
4547
+ casingProud: (t) => share(0.1, t, 10, 20),
4548
+ sillThickness: (t) => share(0.15, t, 20, 40),
4549
+ /** The core's `SILL_PROJECTION` and `SILL_HORN` (both 0.2 of the host's thickness). */
4550
+ sillProjection: (t) => Math.round(0.2 * t),
4551
+ sillHorn: (t) => Math.round(0.2 * t),
4552
+ boardThickness: (t) => share(0.1, t, 18, 30),
4553
+ boardOverhang: (t) => share(0.1, t, 15, 30),
4554
+ reveal: (t) => share(0.075, t, 10, 20),
4555
+ /** The window frame's half depth (`FRAME_DEPTH_MM` / 2): where the reveal and the sills start. */
4556
+ frameHalfDepth: 30
4337
4557
  };
4338
- var id = (v, p, out) => {
4339
- if (typeof v !== "string" || v.length === 0) fail(out, p, "must be a non-empty string");
4558
+ function jambFrame(o) {
4559
+ const [j0, j1] = o.jambs;
4560
+ const dx = j1[0] - j0[0];
4561
+ const dy = j1[1] - j0[1];
4562
+ const w = Math.hypot(dx, dy);
4563
+ if (!(w > 0)) return null;
4564
+ const [ux, uy] = [dx / w, dy / w];
4565
+ const at2 = (u, v) => [Math.round(j0[0] + u * ux - v * uy), Math.round(j0[1] + u * uy + v * ux)];
4566
+ return {
4567
+ w,
4568
+ at: at2,
4569
+ quad: (u0, u1, v0, v1) => ({ outer: [at2(u0, v0), at2(u1, v0), at2(u1, v1), at2(u0, v1)] })
4570
+ };
4571
+ }
4572
+ function toFrame(o, p) {
4573
+ const [j0, j1] = o.jambs;
4574
+ const w = Math.hypot(j1[0] - j0[0], j1[1] - j0[1]) || 1;
4575
+ const [ux, uy] = [(j1[0] - j0[0]) / w, (j1[1] - j0[1]) / w];
4576
+ const [dx, dy] = [p[0] - j0[0], p[1] - j0[1]];
4577
+ return [dx * ux + dy * uy, -dx * uy + dy * ux];
4578
+ }
4579
+ function cutEnds(o, w) {
4580
+ if (o.cut.length !== 4) return null;
4581
+ const pts = o.cut.map((p) => toFrame(o, p)).sort((a, b) => a[0] - b[0]);
4582
+ const ends = [pts.slice(0, 2), pts.slice(2)];
4583
+ const mitred = ends.map((e) => Math.abs(e[0][0] - e[1][0]) > MM);
4584
+ return {
4585
+ mitred,
4586
+ u: (end, v) => {
4587
+ if (!mitred[end]) return end === 0 ? 0 : w;
4588
+ const [a, b] = ends[end];
4589
+ return a[1] === b[1] ? a[0] : a[0] + (b[0] - a[0]) * (v - a[1]) / (b[1] - a[1]);
4590
+ }
4591
+ };
4592
+ }
4593
+ function floorAt(level, p) {
4594
+ return level.floors.some((f) => where(p, f.polygon.outer) === true) || level.voids.some((v) => p[0] > v.at[0] && p[0] < v.at[0] + v.size.w && p[1] > v.at[1] && p[1] < v.at[1] + v.size.h);
4595
+ }
4596
+ function insideOf(o, t, level) {
4597
+ const f = jambFrame(o);
4598
+ if (!f) return null;
4599
+ const mid = f.w / 2;
4600
+ const d = Math.max(t, 1);
4601
+ const plus = floorAt(level, f.at(mid, d));
4602
+ const minus = floorAt(level, f.at(mid, -d));
4603
+ return plus && minus ? "both" : plus ? 1 : minus ? -1 : null;
4604
+ }
4605
+ var CLASH_UM2 = 1e6;
4606
+ var boxPoly = (x0, y0, x1, y1) => ({
4607
+ outer: [
4608
+ [x0, y0],
4609
+ [x1, y0],
4610
+ [x1, y1],
4611
+ [x0, y1]
4612
+ ]
4613
+ });
4614
+ function boxOf(poly) {
4615
+ const xs = poly.outer.map((p) => p[0]);
4616
+ const ys = poly.outer.map((p) => p[1]);
4617
+ return [Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys)];
4618
+ }
4619
+ var Obstacles = class {
4620
+ list = [];
4621
+ add(cls, name, piece, owner) {
4622
+ this.list.push({ ...piece, cls, name, ...owner !== void 0 ? { owner } : {}, box: boxOf(piece.poly) });
4623
+ }
4624
+ /**
4625
+ * What, of `classes`, a piece of `owner`'s trim would stand in, or `null`: of all it meets, the
4626
+ * name that sorts first, so the answer never depends on the order the plan writes its openings in
4627
+ * (review of PR #732, B1).
4628
+ */
4629
+ clash(owner, piece, classes) {
4630
+ const [x0, y0, x1, y1] = boxOf(piece.poly);
4631
+ let first = null;
4632
+ for (const o of this.list) {
4633
+ if (o.owner === owner || !classes.includes(o.cls)) continue;
4634
+ if (o.z1 <= piece.z0 || o.z0 >= piece.z1) continue;
4635
+ if (o.box[2] <= x0 || o.box[0] >= x1 || o.box[3] <= y0 || o.box[1] >= y1) continue;
4636
+ if (first !== null && o.name >= first) continue;
4637
+ if (overlapArea(o.poly, piece.poly) > CLASH_UM2) first = o.name;
4638
+ }
4639
+ return first;
4640
+ }
4340
4641
  };
4341
- var boolean = (v, p, out) => {
4342
- if (typeof v !== "boolean") fail(out, p, "must be a boolean");
4642
+ var FLAT_MM = 50;
4643
+ var STATIC = ["wall", "column", "furniture", "opening"];
4644
+ var TRIM = ["trim"];
4645
+ function fixedObstacles(level, furnitureZ) {
4646
+ const site = new Obstacles();
4647
+ const UP = Number.POSITIVE_INFINITY;
4648
+ for (const w of level.walls) for (const b of w.body) site.add("wall", "wall", { poly: b, z0: 0, z1: UP });
4649
+ for (const c of level.columns)
4650
+ site.add("column", "column", {
4651
+ poly: boxPoly(c.at[0], c.at[1], c.at[0] + c.size.w, c.at[1] + c.size.h),
4652
+ z0: 0,
4653
+ z1: UP
4654
+ });
4655
+ for (const f of level.furniture) {
4656
+ const [z0, z1] = furnitureZ(f);
4657
+ if (z1 - z0 < FLAT_MM * MM) continue;
4658
+ site.add("furniture", `furniture ${f.id}`, {
4659
+ poly: boxPoly(
4660
+ f.centre[0] - f.size.w / 2,
4661
+ f.centre[1] - f.size.d / 2,
4662
+ f.centre[0] + f.size.w / 2,
4663
+ f.centre[1] + f.size.d / 2
4664
+ ),
4665
+ z0,
4666
+ z1
4667
+ });
4668
+ }
4669
+ const thick = new Map(level.walls.map((w) => [w.id, w.thickness]));
4670
+ const reach = OPENING_DETAIL.casingProud(Number.POSITIVE_INFINITY) * MM;
4671
+ for (const b of level.openings) {
4672
+ const fb = jambFrame(b);
4673
+ const hb = (thick.get(b.host) ?? 0) / 2 + reach;
4674
+ if (!fb) continue;
4675
+ const band = fb.quad(0, fb.w, -hb, hb);
4676
+ site.add("opening", `opening ${b.id}`, { poly: band, z0: b.sill, z1: b.head }, b.id);
4677
+ site.add("doorway", `opening ${b.id}`, { poly: band, z0: 0, z1: UP }, b.id);
4678
+ site.add("jamb", `opening ${b.id}`, { poly: fb.quad(-MM, fb.w + MM, -hb, hb), z0: 0, z1: UP }, b.id);
4679
+ }
4680
+ return site;
4681
+ }
4682
+ function cutOverlap(o, level) {
4683
+ const own = { outer: o.cut };
4684
+ const hits = level.openings.filter((b) => b.id !== o.id && overlapArea({ outer: b.cut }, own) > CLASH_UM2).map((b) => b.id).sort();
4685
+ return hits[0] ?? null;
4686
+ }
4687
+ function curvedLining(sector, l, bands, z0, head, withHead, prism2) {
4688
+ const f = Math.min(0.5, l / (sector.r * Math.abs(sector.sweep)));
4689
+ return bands.flatMap(([v0, v1]) => [
4690
+ prism2(sectorBand(sector, v0, v1, 0, f), z0, head),
4691
+ prism2(sectorBand(sector, v0, v1, 1 - f, 1), z0, head),
4692
+ ...withHead && f < 0.5 ? [prism2(sectorBand(sector, v0, v1, f, 1 - f), head - l, head)] : []
4693
+ ]);
4694
+ }
4695
+ var END = ["start", "end"];
4696
+ function storeyTrim(level, inputs, e, prism2, furnitureZ) {
4697
+ const site = fixedObstacles(level, furnitureZ);
4698
+ const out = /* @__PURE__ */ new Map();
4699
+ const doors = [];
4700
+ const windows = [];
4701
+ for (const input of inputs) {
4702
+ const plan = input.o.kind === "window" ? planWindow(input, level, e, prism2) : planDoor(input, level, e, prism2);
4703
+ out.set(input.o.id, plan.result);
4704
+ if (plan.kind === "door") doors.push(plan);
4705
+ else if (plan.kind === "window") windows.push(plan);
4706
+ }
4707
+ const plannedLegs = new Obstacles();
4708
+ for (const d of doors)
4709
+ for (const face of d.faces)
4710
+ for (const leg of face.legs) plannedLegs.add("trim", `casing ${d.input.o.id}`, leg, d.input.o.id);
4711
+ for (const d of doors) {
4712
+ const id = d.input.o.id;
4713
+ for (const face of d.faces)
4714
+ face.legs.forEach((leg, k) => {
4715
+ const what = site.clash(id, leg, STATIC) ?? plannedLegs.clash(id, leg, TRIM);
4716
+ if (what) {
4717
+ face.keep[k] = false;
4718
+ d.omitted.push(`${face.name}:${END[k]}:${what}`);
4719
+ }
4720
+ });
4721
+ }
4722
+ for (const d of doors) {
4723
+ const id = d.input.o.id;
4724
+ for (const face of d.faces) {
4725
+ if (!face.ends || !face.mid) continue;
4726
+ const heads = [];
4727
+ face.ends.forEach((end, k) => {
4728
+ if (!face.keep[k]) {
4729
+ face.endKeep[k] = false;
4730
+ return;
4731
+ }
4732
+ const what2 = site.clash(id, end, STATIC);
4733
+ if (what2) {
4734
+ face.endKeep[k] = false;
4735
+ heads.push(`${face.name}:head-${END[k]}:${what2}`);
4736
+ }
4737
+ });
4738
+ const what = site.clash(id, face.mid, STATIC);
4739
+ if (what) {
4740
+ face.midKeep = false;
4741
+ d.omitted.push(`${face.name}:head:${what}`);
4742
+ } else d.omitted.push(...heads);
4743
+ }
4744
+ }
4745
+ const casings = new Obstacles();
4746
+ for (const d of doors) finishDoor(d, casings, e, prism2);
4747
+ const plannedSlabs = new Obstacles();
4748
+ const plannedCores = new Obstacles();
4749
+ for (const w of windows)
4750
+ for (const sl of w.slabs) {
4751
+ plannedCores.add("trim", `${sl.part} ${w.input.o.id}`, sl.core, w.input.o.id);
4752
+ plannedSlabs.add("trim", `${sl.part} ${w.input.o.id}`, sl.core, w.input.o.id);
4753
+ for (const h of sl.horns) if (h) plannedSlabs.add("trim", `${sl.part} ${w.input.o.id}`, h, w.input.o.id);
4754
+ }
4755
+ const NEAR = ["jamb"];
4756
+ for (const w of windows) {
4757
+ const id = w.input.o.id;
4758
+ for (const sl of w.slabs) {
4759
+ sl.horns.forEach((h, k) => {
4760
+ if (!h) return;
4761
+ const what2 = site.clash(id, h, NEAR) ?? casings.clash(id, h, TRIM) ?? plannedSlabs.clash(id, h, TRIM);
4762
+ if (what2) {
4763
+ sl.hornKeep[k] = false;
4764
+ sl.hornWhy[k] = what2;
4765
+ }
4766
+ });
4767
+ const what = site.clash(id, sl.core, ["doorway"]) ?? casings.clash(id, sl.core, TRIM) ?? plannedCores.clash(id, sl.core, TRIM);
4768
+ if (what) {
4769
+ sl.coreKeep = false;
4770
+ sl.coreWhy = what;
4771
+ }
4772
+ }
4773
+ }
4774
+ for (const w of windows) w.finish();
4775
+ return out;
4776
+ }
4777
+ function planDoor(input, level, e, prism2) {
4778
+ const { o, t, head, top } = input;
4779
+ const result = { parts: [], ledger: [] };
4780
+ const { parts, ledger } = result;
4781
+ const f = jambFrame(o);
4782
+ if (!f) return { kind: "none", result };
4783
+ const tm = t / MM;
4784
+ const kind = o.kind;
4785
+ const door = kind === "door";
4786
+ const sector = cutSector(o.cut, o.jambs, o.centre);
4787
+ const blocked = cutOverlap(o, level);
4788
+ if (blocked || door && sector) {
4789
+ ledger.push({
4790
+ kind,
4791
+ id: o.id,
4792
+ field: "trim",
4793
+ value: blocked ? `cut overlaps ${blocked}` : "curved host",
4794
+ provenance: "default",
4795
+ rule: "door-trim-omitted"
4796
+ });
4797
+ return { kind: "none", result };
4798
+ }
4799
+ const l = OPENING_DETAIL.lining(tm) * MM;
4800
+ const slot = OPENING_DETAIL.slot * MM / 2;
4801
+ const half = t / 2;
4802
+ const z0 = e + o.sill;
4803
+ const zh = e + head;
4804
+ const withHead = head < top;
4805
+ const bands = door ? half - slot >= MM ? [
4806
+ [-half, -slot],
4807
+ [slot, half]
4808
+ ] : [] : [[-half, half]];
4809
+ const lining = sector ? curvedLining(sector, l, bands, z0, zh, withHead, prism2) : bands.flatMap(([v0, v1]) => [
4810
+ prism2(f.quad(0, l, v0, v1), z0, zh),
4811
+ prism2(f.quad(f.w - l, f.w, v0, v1), z0, zh),
4812
+ ...withHead && f.w > 2 * l ? [prism2(f.quad(l, f.w - l, v0, v1), zh - l, zh)] : []
4813
+ ]);
4814
+ if (lining.length) parts.push({ part: "lining", shells: lining });
4815
+ ledger.push({
4816
+ kind,
4817
+ id: o.id,
4818
+ field: "lining_mm",
4819
+ value: bands.length ? { thickness: l / MM, ...door ? { slot: OPENING_DETAIL.slot } : {}, head: withHead } : "omitted",
4820
+ provenance: "default",
4821
+ rule: bands.length ? "door-lining-default" : "door-lining-omitted"
4822
+ });
4823
+ const why = o.doorKind === "garage" ? "garage" : sector ? "curved" : null;
4824
+ if (why) {
4825
+ ledger.push({ kind, id: o.id, field: "casing", value: why, provenance: "default", rule: "door-casing-omitted" });
4826
+ return { kind: "none", result };
4827
+ }
4828
+ const c = OPENING_DETAIL.casingWidth(tm) * MM;
4829
+ const p = OPENING_DETAIL.casingProud(tm) * MM;
4830
+ const panel = o.doorKind === "barn" ? o.swing === "out" ? -1 : 1 : 0;
4831
+ const omitted = [];
4832
+ const barTop = Math.min(head + c, top);
4833
+ const faces = [];
4834
+ for (const s of [1, -1]) {
4835
+ const name = s > 0 ? "+face" : "-face";
4836
+ if (s === panel) {
4837
+ omitted.push(`${name}:barn`);
4838
+ continue;
4839
+ }
4840
+ const [v0, v1] = s > 0 ? [half, half + p] : [-half - p, -half];
4841
+ const bar2 = barTop > head;
4842
+ faces.push({
4843
+ s,
4844
+ name,
4845
+ v0,
4846
+ v1,
4847
+ legs: [
4848
+ { poly: f.quad(-c, 0, v0, v1), z0: 0, z1: head },
4849
+ { poly: f.quad(f.w, f.w + c, v0, v1), z0: 0, z1: head }
4850
+ ],
4851
+ keep: [true, true],
4852
+ ends: bar2 ? [
4853
+ { poly: f.quad(-c, 0, v0, v1), z0: head, z1: barTop },
4854
+ { poly: f.quad(f.w, f.w + c, v0, v1), z0: head, z1: barTop }
4855
+ ] : null,
4856
+ mid: bar2 ? { poly: f.quad(0, f.w, v0, v1), z0: head, z1: barTop } : null,
4857
+ endKeep: [true, true],
4858
+ midKeep: true
4859
+ });
4860
+ }
4861
+ return { kind: "door", input, f, result, faces, omitted, c, p, barTop };
4862
+ }
4863
+ function finishDoor(d, casings, e, prism2) {
4864
+ const { o, head } = d.input;
4865
+ const { f, c, p, barTop } = d;
4866
+ const shells = [];
4867
+ const id = o.id;
4868
+ for (const face of d.faces) {
4869
+ face.legs.forEach((leg, k) => {
4870
+ if (!face.keep[k]) return;
4871
+ shells.push(prism2(leg.poly, e, e + head));
4872
+ casings.add("trim", `casing ${id}`, leg, id);
4873
+ });
4874
+ if (face.ends && face.midKeep) {
4875
+ const u0 = face.endKeep[0] ? -c : 0;
4876
+ const u1 = face.endKeep[1] ? f.w + c : f.w;
4877
+ const bar2 = { poly: f.quad(u0, u1, face.v0, face.v1), z0: head, z1: barTop };
4878
+ shells.push(prism2(bar2.poly, e + head, e + barTop));
4879
+ casings.add("trim", `casing ${id}`, bar2, id);
4880
+ }
4881
+ }
4882
+ const { parts, ledger } = d.result;
4883
+ if (shells.length) {
4884
+ parts.push({ part: "casing", shells });
4885
+ ledger.push({
4886
+ kind: o.kind,
4887
+ id,
4888
+ field: "casing_mm",
4889
+ value: { width: c / MM, proud: p / MM, head: barTop > head },
4890
+ provenance: "default",
4891
+ rule: "door-casing-default"
4892
+ });
4893
+ }
4894
+ if (d.omitted.length)
4895
+ ledger.push({
4896
+ kind: o.kind,
4897
+ id,
4898
+ field: "casing",
4899
+ value: d.omitted,
4900
+ provenance: "default",
4901
+ rule: "door-casing-omitted"
4902
+ });
4903
+ }
4904
+ function planWindow(input, level, e, prism2) {
4905
+ const { o, t, head, top } = input;
4906
+ const result = { parts: [], ledger: [] };
4907
+ const { parts, ledger } = result;
4908
+ const f = jambFrame(o);
4909
+ if (!f) return { kind: "none", result };
4910
+ const tm = t / MM;
4911
+ const half = t / 2;
4912
+ const ff = Math.min(OPENING_DETAIL.frameHalfDepth * MM, half);
4913
+ const inside = insideOf(o, t, level);
4914
+ ledger.push({
4915
+ kind: "window",
4916
+ id: o.id,
4917
+ field: "inside",
4918
+ value: inside === null ? "neither" : inside === "both" ? "both" : inside > 0 ? "+normal" : "-normal",
4919
+ provenance: "derived",
4920
+ rule: "window-inside-probe"
4921
+ });
4922
+ if (inside === null) return { kind: "none", result };
4923
+ const blocked = cutOverlap(o, level);
4924
+ if (blocked) {
4925
+ ledger.push({
4926
+ kind: "window",
4927
+ id: o.id,
4928
+ field: "trim",
4929
+ value: `cut overlaps ${blocked}`,
4930
+ provenance: "default",
4931
+ rule: "window-trim-omitted"
4932
+ });
4933
+ return { kind: "none", result };
4934
+ }
4935
+ const sector = cutSector(o.cut, o.jambs, o.centre);
4936
+ const ends = sector ? null : cutEnds(o, f.w);
4937
+ const hasSill = o.sill > 0;
4938
+ const zs = e + o.sill;
4939
+ const zh = e + head;
4940
+ const withHead = head < top;
4941
+ const bt = OPENING_DETAIL.boardThickness(tm) * MM;
4942
+ const st = OPENING_DETAIL.sillThickness(tm) * MM;
4943
+ const rl = OPENING_DETAIL.reveal(tm) * MM;
4944
+ const sides = inside === "both" ? [1, -1] : [inside];
4945
+ const reveal = (boardBuilt) => {
4946
+ const shells = [];
4947
+ if (half - ff >= MM) {
4948
+ for (const s of sides) {
4949
+ const [v0, v1] = s > 0 ? [ff, half] : [-half, -ff];
4950
+ const base = boardBuilt ? zs + bt : zs;
4951
+ if (sector) {
4952
+ const k = sectorSign(o, sector);
4953
+ const [a2, b2] = s > 0 ? [ff + MM, half] : [-half, -ff - MM];
4954
+ shells.push(
4955
+ ...curvedLining(
4956
+ sector,
4957
+ rl,
4958
+ [[a2 * k, b2 * k].sort((x, y) => x - y)],
4959
+ base,
4960
+ zh,
4961
+ withHead,
4962
+ prism2
4963
+ )
4964
+ );
4965
+ continue;
4966
+ }
4967
+ const m0 = ends?.mitred[0] ?? false;
4968
+ const m1 = ends?.mitred[1] ?? false;
4969
+ if (!m0) shells.push(prism2(f.quad(0, rl, v0, v1), base, zh));
4970
+ if (!m1) shells.push(prism2(f.quad(f.w - rl, f.w, v0, v1), base, zh));
4971
+ const [a, b] = [m0 ? 0 : rl, m1 ? f.w : f.w - rl];
4972
+ if (withHead && b > a) shells.push(prism2(f.quad(a, b, v0, v1), zh - rl, zh));
4973
+ }
4974
+ }
4975
+ if (shells.length) parts.unshift({ part: "reveal", shells });
4976
+ ledger.splice(1, 0, {
4977
+ kind: "window",
4978
+ id: o.id,
4979
+ field: "reveal_mm",
4980
+ value: shells.length ? { thickness: rl / MM, head: withHead } : "omitted",
4981
+ provenance: "default",
4982
+ rule: "window-reveal-lining"
4983
+ });
4984
+ };
4985
+ if (inside === "both" || !hasSill) {
4986
+ reveal(false);
4987
+ ledger.push({
4988
+ kind: "window",
4989
+ id: o.id,
4990
+ field: "sill",
4991
+ value: inside === "both" ? "interior window" : "sill 0",
4992
+ provenance: "derived",
4993
+ rule: "window-sill-omitted"
4994
+ });
4995
+ return { kind: "none", result };
4996
+ }
4997
+ const sIn = inside;
4998
+ const sOut = -inside;
4999
+ if (sector) {
5000
+ const k = sectorSign(o, sector);
5001
+ const band = (s) => (s > 0 ? [ff * k, half * k] : [-half * k, -ff * k]).sort((a, b) => a - b);
5002
+ const [o0, o1] = band(sOut);
5003
+ const [i0, i1] = band(sIn);
5004
+ reveal(true);
5005
+ parts.push({ part: "sill", shells: [prism2(sectorBand(sector, o0, o1), zs, zs + st)] });
5006
+ parts.push({ part: "board", shells: [prism2(sectorBand(sector, i0, i1), zs, zs + bt)] });
5007
+ ledger.push({
5008
+ kind: "window",
5009
+ id: o.id,
5010
+ field: "sill_mm",
5011
+ value: { thickness: st / MM, board: bt / MM },
5012
+ provenance: "default",
5013
+ rule: "window-sill-curved-inside-cut"
5014
+ });
5015
+ return { kind: "none", result };
5016
+ }
5017
+ const proj = OPENING_DETAIL.sillProjection(tm) * MM;
5018
+ const horn = OPENING_DETAIL.sillHorn(tm) * MM;
5019
+ const over = OPENING_DETAIL.boardOverhang(tm) * MM;
5020
+ const quadAt = (s, reach, u0, u1) => {
5021
+ const [va, vb] = s > 0 ? [ff, half + reach] : [-half - reach, -ff];
5022
+ return { outer: [f.at(u0(va), va), f.at(u1(va), va), f.at(u1(vb), vb), f.at(u0(vb), vb)] };
5023
+ };
5024
+ const jamb0 = (v) => ends?.mitred[0] ? ends.u(0, v) : 0;
5025
+ const jamb1 = (v) => ends?.mitred[1] ? ends.u(1, v) : f.w;
5026
+ const slabOf = (part, s, reach, past, thickness) => {
5027
+ const z = { z0: o.sill, z1: o.sill + thickness };
5028
+ return {
5029
+ part,
5030
+ core: { poly: quadAt(s, reach, jamb0, jamb1), ...z },
5031
+ // No horn at a mitred end (a window run to a corner stops on the mitre).
5032
+ horns: [
5033
+ ends?.mitred[0] ? null : {
5034
+ poly: quadAt(
5035
+ s,
5036
+ reach,
5037
+ () => -past,
5038
+ () => 0
5039
+ ),
5040
+ ...z
5041
+ },
5042
+ ends?.mitred[1] ? null : {
5043
+ poly: quadAt(
5044
+ s,
5045
+ reach,
5046
+ () => f.w,
5047
+ () => f.w + past
5048
+ ),
5049
+ ...z
5050
+ }
5051
+ ],
5052
+ hornKeep: [true, true],
5053
+ hornWhy: ["", ""],
5054
+ coreKeep: true,
5055
+ coreWhy: "",
5056
+ built: null,
5057
+ thickness
5058
+ };
5059
+ };
5060
+ const slabs = [slabOf("sill", sOut, proj, horn, st), slabOf("board", sIn, over, over, bt)];
5061
+ const finish = (boardBuilt) => {
5062
+ reveal(boardBuilt);
5063
+ for (const sl of slabs) {
5064
+ if (!sl.built) continue;
5065
+ parts.push({ part: sl.part, shells: [prism2(sl.built, zs, zs + sl.thickness)] });
5066
+ }
5067
+ const [sill, board] = slabs;
5068
+ if (sill.built)
5069
+ ledger.push({
5070
+ kind: "window",
5071
+ id: o.id,
5072
+ field: "sill_mm",
5073
+ value: { thickness: st / MM, projection: proj / MM, horn: horn / MM, mitred: ends?.mitred ?? [false, false] },
5074
+ provenance: "default",
5075
+ rule: "window-sill-exterior"
5076
+ });
5077
+ if (board.built)
5078
+ ledger.push({
5079
+ kind: "window",
5080
+ id: o.id,
5081
+ field: "board_mm",
5082
+ value: {
5083
+ thickness: bt / MM,
5084
+ overhang: over / MM,
5085
+ // A mitred end (a window run to a corner) stops on the mitre, the board's as the sill's.
5086
+ ...ends?.mitred.some(Boolean) ? { mitred: ends.mitred } : {}
5087
+ },
5088
+ provenance: "default",
5089
+ rule: "window-board-interior"
5090
+ });
5091
+ };
5092
+ return {
5093
+ kind: "window",
5094
+ input,
5095
+ f,
5096
+ result,
5097
+ slabs,
5098
+ finish: () => {
5099
+ for (const sl of slabs) {
5100
+ if (!sl.coreKeep) {
5101
+ ledger.push({
5102
+ kind: "window",
5103
+ id: o.id,
5104
+ field: sl.part,
5105
+ value: `clash ${sl.coreWhy}`,
5106
+ provenance: "derived",
5107
+ rule: sl.part === "sill" ? "window-sill-omitted" : "window-board-omitted"
5108
+ });
5109
+ continue;
5110
+ }
5111
+ const cut = END.flatMap((name, k) => sl.horns[k] && !sl.hornKeep[k] ? [`${name}:${sl.hornWhy[k]}`] : []);
5112
+ if (cut.length)
5113
+ ledger.push({
5114
+ kind: "window",
5115
+ id: o.id,
5116
+ field: `${sl.part}_horn`,
5117
+ value: cut,
5118
+ provenance: "derived",
5119
+ rule: "window-sill-horn-omitted"
5120
+ });
5121
+ const s = sl.part === "sill" ? sOut : sIn;
5122
+ const reach = sl.part === "sill" ? proj : over;
5123
+ const past = sl.part === "sill" ? horn : over;
5124
+ sl.built = quadAt(
5125
+ s,
5126
+ reach,
5127
+ sl.horns[0] && sl.hornKeep[0] ? () => -past : jamb0,
5128
+ sl.horns[1] && sl.hornKeep[1] ? () => f.w + past : jamb1
5129
+ );
5130
+ }
5131
+ finish(slabs[1].built !== null);
5132
+ }
5133
+ };
5134
+ }
5135
+ function sectorSign(o, sector) {
5136
+ const [j0, j1] = o.jambs;
5137
+ const [dx, dy] = [j1[0] - j0[0], j1[1] - j0[1]];
5138
+ const [nx, ny] = [-dy, dx];
5139
+ const [rx, ry] = [o.centre[0] - sector.c[0], o.centre[1] - sector.c[1]];
5140
+ return nx * rx + ny * ry >= 0 ? 1 : -1;
5141
+ }
5142
+
5143
+ // src/mesh/stair.ts
5144
+ function stairRisers(flights, landings = []) {
5145
+ return flights.reduce((n, f) => n + f.treads + 1, 0) + landings.slice(0, Math.max(0, flights.length - 1)).reduce((n, l) => n + Math.max(0, (l.winders?.length ?? 1) - 1), 0);
5146
+ }
5147
+ function landingSteps(l) {
5148
+ return l.winders?.length ? l.winders : [rect(l.at[0], l.at[1], l.at[0] + l.size.w, l.at[1] + l.size.h).outer];
5149
+ }
5150
+ function runCoordinate(f, d) {
5151
+ const [x, y] = f.at;
5152
+ switch (f.foot) {
5153
+ case "bottom":
5154
+ return y + f.size.h - d;
5155
+ case "top":
5156
+ return y + d;
5157
+ case "right":
5158
+ return x + f.size.w - d;
5159
+ case "left":
5160
+ return x + d;
5161
+ }
5162
+ }
5163
+ var alongY = (foot) => foot === "top" || foot === "bottom";
5164
+ function sideways2(section, c0, c1, runAlongY) {
5165
+ const up = prism({ outer: section }, c0, c1);
5166
+ if (!up) return null;
5167
+ const positions = [];
5168
+ for (let i = 0; i < up.positions.length; i += 3) {
5169
+ const [u, h, v] = [up.positions[i], up.positions[i + 1], up.positions[i + 2]];
5170
+ if (runAlongY) positions.push(h, v, u);
5171
+ else positions.push(u, v, h);
5172
+ }
5173
+ const indices = [...up.indices];
5174
+ if (!runAlongY) {
5175
+ for (let t = 0; t < indices.length; t += 3) {
5176
+ const b = indices[t + 1];
5177
+ indices[t + 1] = indices[t + 2];
5178
+ indices[t + 2] = b;
5179
+ }
5180
+ }
5181
+ return { positions, indices };
5182
+ }
5183
+ function stairSolid(stair, z0, rise) {
5184
+ const flights = stair.flights;
5185
+ if (!flights?.length || !(rise > 0)) return null;
5186
+ const R = stairRisers(flights, stair.landings);
5187
+ const riserTop = (k) => z0 + Math.round(rise * k / R);
5188
+ const shells = [];
5189
+ const landingTops = [];
5190
+ let dropped = 0;
5191
+ let climbed = 0;
5192
+ flights.forEach((f, i) => {
5193
+ const n = f.treads;
5194
+ const yRun = alongY(f.foot);
5195
+ const run2 = yRun ? f.size.h : f.size.w;
5196
+ const [lo, extent] = yRun ? [f.at[0], f.size.w] : [f.at[1], f.size.h];
5197
+ const width = Math.min(stair.width, extent);
5198
+ const c0 = lo + Math.round((extent - width) / 2);
5199
+ const c1 = c0 + width;
5200
+ const at2 = (t) => runCoordinate(f, Math.round(run2 * t / n));
5201
+ const section = [[at2(0), z0]];
5202
+ for (let t = 0; t < n; t++) {
5203
+ const h = riserTop(climbed + t + 1);
5204
+ section.push([at2(t), h], [at2(t + 1), h]);
5205
+ }
5206
+ section.push([at2(n), z0]);
5207
+ const s = sideways2(section, c0, c1, yRun);
5208
+ if (s) shells.push(s);
5209
+ else dropped++;
5210
+ climbed += n + 1;
5211
+ const landing = stair.landings?.[i];
5212
+ if (landing && i < flights.length - 1) {
5213
+ landingTops.push(riserTop(climbed));
5214
+ const steps = landingSteps(landing);
5215
+ steps.forEach((ring2, k) => {
5216
+ const box2 = prism({ outer: ring2 }, z0, riserTop(climbed + k));
5217
+ if (box2) shells.push(box2);
5218
+ else dropped++;
5219
+ });
5220
+ climbed += steps.length - 1;
5221
+ }
5222
+ });
5223
+ return { shells, risers: R, landingTops, top: riserTop(R - 1), dropped };
5224
+ }
5225
+ var clampMm = (x, lo, hi) => Math.max(lo, Math.min(hi, x));
5226
+ var STAIR_DETAIL = {
5227
+ /** A tread board, 0.25 r in [30, 50] thick (never more than half a riser). */
5228
+ tread: (r) => Math.min(clampMm(Math.round(0.25 * r), 30, 50), Math.max(1, Math.floor(r / 2))),
5229
+ /** Its nosing past the front edge (not on a flight's first tread), 0.1 g in [15, 30]. */
5230
+ nosing: (g) => clampMm(Math.round(0.1 * g), 15, 30),
5231
+ /** A stringer, 0.05 W in [40, 60] wide (at most a quarter of the band). */
5232
+ stringer: (W) => Math.min(clampMm(Math.round(0.05 * W), 40, 60), Math.max(1, Math.floor(W / 4))),
5233
+ /** The stringer's band: 0.3 r in [40, 60] over the nosing line, 1.2 r in [150, 250] under it. */
5234
+ above: (r) => clampMm(Math.round(0.3 * r), 40, 60),
5235
+ below: (r) => clampMm(Math.round(1.2 * r), 150, 250),
5236
+ /** The handrail's top over the nosing line (measured upright), and its 50 × 50 section. */
5237
+ railHeight: 900,
5238
+ rail: 50,
5239
+ /** A baluster a tread, 0.03 W in [20, 30] square. */
5240
+ baluster: (W) => clampMm(Math.round(0.03 * W), 20, 30),
5241
+ /** A newel at each end of a flight, 0.06 W in [60, 90] square, this far over the handrail. */
5242
+ newel: (W) => clampMm(Math.round(0.06 * W), 60, 90),
5243
+ newelAbove: 100,
5244
+ /** A landing's open edge: its handrail's top over the landing; balusters a going apart. */
5245
+ landingRailHeight: 1e3,
5246
+ /** How far past an edge the wall probe looks. */
5247
+ probe: 100
4343
5248
  };
4344
- var any = () => {
5249
+ function freeRuns(s0, s1, taken, min = 0) {
5250
+ let runs = [[s0, s1]];
5251
+ for (const [a, b] of taken)
5252
+ runs = runs.flatMap(
5253
+ ([p, q]) => b <= p || a >= q ? [[p, q]] : [
5254
+ [p, Math.min(a, q)],
5255
+ [Math.max(b, p), q]
5256
+ ].filter(([u, v]) => v > u)
5257
+ );
5258
+ return runs.filter(([u, v]) => v - u >= min);
5259
+ }
5260
+ function stairDetail(stair, z0, rise, solidAt) {
5261
+ const flights = stair.flights;
5262
+ if (!flights?.length || !(rise > 0)) return null;
5263
+ const MM4 = 1e3;
5264
+ const D = STAIR_DETAIL;
5265
+ const R = stairRisers(flights, stair.landings);
5266
+ const riserTop = (k) => z0 + Math.round(rise * k / R);
5267
+ const r = rise / R / MM4;
5268
+ const W = stair.width / MM4;
5269
+ const [T, above, below] = [D.tread(r) * MM4, D.above(r) * MM4, D.below(r) * MM4];
5270
+ const [sw, bb, np] = [D.stringer(W) * MM4, D.baluster(W) * MM4, D.newel(W) * MM4];
5271
+ const [railH, railW] = [D.railHeight * MM4, D.rail * MM4];
5272
+ const out = {
5273
+ treads: [],
5274
+ stringers: [],
5275
+ rails: [],
5276
+ sides: [],
5277
+ dropped: 0
5278
+ };
5279
+ const keep = (into, s) => {
5280
+ if (s) into.push(s);
5281
+ else out.dropped++;
5282
+ };
5283
+ const nosings = [];
5284
+ const planAt = (f, d, a) => alongY(f.foot) ? [a, runCoordinate(f, d)] : [runCoordinate(f, d), a];
5285
+ const planBox = (f, d0, d1, a0, a1) => {
5286
+ const [p, q] = [planAt(f, d0, a0), planAt(f, d1, a1)];
5287
+ return [Math.min(p[0], q[0]), Math.min(p[1], q[1]), Math.max(p[0], q[0]), Math.max(p[1], q[1])];
5288
+ };
5289
+ const solid = (b, za, zb) => prism(rect(b[0], b[1], b[2], b[3]), za, zb);
5290
+ const section = (f, pts, a0, a1) => sideways2(
5291
+ pts.map(([d, z]) => [runCoordinate(f, d), z]),
5292
+ a0,
5293
+ a1,
5294
+ alongY(f.foot)
5295
+ );
5296
+ const flightBoxes = flights.map((f) => [f.at[0], f.at[1], f.at[0] + f.size.w, f.at[1] + f.size.h]);
5297
+ const landingBoxes = (stair.landings ?? []).slice(0, flights.length - 1).map((l) => [l.at[0], l.at[1], l.at[0] + l.size.w, l.at[1] + l.size.h]);
5298
+ const newels = [];
5299
+ const landings = [];
5300
+ let climbed = 0;
5301
+ let base = z0;
5302
+ flights.forEach((f, i) => {
5303
+ const n = f.treads;
5304
+ const yRun = alongY(f.foot);
5305
+ const run2 = yRun ? f.size.h : f.size.w;
5306
+ const [lo, extent] = yRun ? [f.at[0], f.size.w] : [f.at[1], f.size.h];
5307
+ const width = Math.min(stair.width, extent);
5308
+ const c0 = lo + Math.round((extent - width) / 2);
5309
+ const c1 = c0 + width;
5310
+ const g = run2 / n / MM4;
5311
+ const nos = D.nosing(g) * MM4;
5312
+ nosings.push(nos / MM4);
5313
+ const line = (t) => Math.round(run2 * t / n);
5314
+ const first = riserTop(climbed + 1);
5315
+ const head = riserTop(climbed + n + 1);
5316
+ const slope = (head - first) / run2;
5317
+ const h = (d) => first + slope * d;
5318
+ for (let t = 0; t < n; t++) {
5319
+ const top = riserTop(climbed + t + 1);
5320
+ keep(out.treads, solid(planBox(f, t === 0 ? 0 : line(t) - nos, line(t + 1), c0 + sw, c1 - sw), top - T, top));
5321
+ }
5322
+ const band = clipToConvex(
5323
+ [
5324
+ [0, Math.min(Math.round(h(0) - below), base)],
5325
+ [run2, Math.round(h(run2) - below)],
5326
+ [run2, Math.round(h(run2) + above)],
5327
+ [0, Math.round(h(0) + above)]
5328
+ ],
5329
+ rect(0, z0, run2, head).outer
5330
+ );
5331
+ keep(out.stringers, band ? section(f, band.outer, c0, c0 + sw) : null);
5332
+ keep(out.stringers, band ? section(f, band.outer, c1 - sw, c1) : null);
5333
+ const edges = [
5334
+ { side: yRun ? "left" : "top", strip: [c0 - D.probe * MM4, c0], past: c0 - D.probe * MM4, cc: c0 + sw / 2 },
5335
+ { side: yRun ? "right" : "bottom", strip: [c1, c1 + D.probe * MM4], past: c1 + D.probe * MM4, cc: c1 - sw / 2 }
5336
+ ];
5337
+ const r0 = runCoordinate(f, 0);
5338
+ const dir = runCoordinate(f, 1) - r0;
5339
+ const dOf = (r2) => dir * (r2 - r0);
5340
+ for (const { side, strip, past, cc } of edges) {
5341
+ const touching = (boxes) => boxes.filter(([a, b, c, d]) => {
5342
+ const [x0, x1] = yRun ? [a, c] : [b, d];
5343
+ const [y0, y1] = yRun ? [b, d] : [a, c];
5344
+ return x0 < strip[1] && x1 > strip[0] && y1 > Math.min(r0, r0 + dir * run2) && y0 < Math.max(r0, r0 + dir * run2);
5345
+ });
5346
+ const byFlight = touching(flightBoxes.filter((_, j) => j !== i)).length > 0;
5347
+ const covered2 = [...flightBoxes.filter((_, j) => j !== i), ...landingBoxes].filter(([a, b, c, d]) => {
5348
+ const [x0, x1] = yRun ? [a, c] : [b, d];
5349
+ const [y0, y1] = yRun ? [b, d] : [a, c];
5350
+ return x0 < strip[1] && x1 > strip[0] && y1 > Math.min(r0, r0 + dir * run2) && y0 < Math.max(r0, r0 + dir * run2);
5351
+ }).map(([a, b, c, d]) => {
5352
+ const [y0, y1] = yRun ? [b, d] : [a, c];
5353
+ const [da, db] = [dOf(y0), dOf(y1)].sort((x, y) => x - y);
5354
+ return [Math.max(0, da), Math.min(run2, db)];
5355
+ });
5356
+ const free = freeRuns(0, run2, covered2, np);
5357
+ const open = free.filter(([da, db]) => !solidAt(planAt(f, Math.round((da + db) / 2), past)));
5358
+ const against = free.length === 0 ? byFlight ? "flight" : "landing" : open.length === 0 ? "wall" : "open";
5359
+ out.sides.push({ of: `flight ${i + 1}`, side, against });
5360
+ const ra = Math.round(cc - railW / 2);
5361
+ const lean = Math.ceil(slope * bb / 2) + MM4;
5362
+ const ba = Math.round(cc - bb / 2);
5363
+ const na = Math.round(cc - np / 2);
5364
+ for (const [da, db] of open) {
5365
+ keep(
5366
+ out.rails,
5367
+ section(
5368
+ f,
5369
+ [
5370
+ [da, Math.round(h(da) + railH - railW)],
5371
+ [db, Math.round(h(db) + railH - railW)],
5372
+ [db, Math.round(h(db) + railH)],
5373
+ [da, Math.round(h(da) + railH)]
5374
+ ],
5375
+ ra,
5376
+ ra + railW
5377
+ )
5378
+ );
5379
+ for (let t = 0; t < n; t++) {
5380
+ const dm = Math.round((line(t) + line(t + 1)) / 2);
5381
+ if (dm - bb / 2 < da || dm + bb / 2 > db) continue;
5382
+ keep(
5383
+ out.rails,
5384
+ solid(
5385
+ planBox(f, dm - bb / 2, dm + bb / 2, ba, ba + bb),
5386
+ Math.round(h(dm) + above) - lean,
5387
+ Math.round(h(dm) + railH - railW) + lean
5388
+ )
5389
+ );
5390
+ }
5391
+ for (const [d, za] of [
5392
+ [0, base],
5393
+ [run2, riserTop(climbed + n)]
5394
+ ]) {
5395
+ if (d !== da && d !== db) continue;
5396
+ const nb = planBox(f, d - np / 2, d + np / 2, na, na + np);
5397
+ keep(out.rails, solid(nb, za, Math.round(h(d) + railH + D.newelAbove * MM4)));
5398
+ newels.push(nb);
5399
+ }
5400
+ }
5401
+ }
5402
+ climbed += n + 1;
5403
+ const landing = stair.landings?.[i];
5404
+ if (landing && i < flights.length - 1) {
5405
+ const top = riserTop(climbed);
5406
+ base = top;
5407
+ const box2 = [landing.at[0], landing.at[1], landing.at[0] + landing.size.w, landing.at[1] + landing.size.h];
5408
+ if (landing.winders?.length) {
5409
+ const steps = landing.winders.map((ring2, j) => ({ ring: ring2, top: riserTop(climbed + j) }));
5410
+ for (const st of steps) keep(out.treads, prism({ outer: st.ring }, z0, st.top));
5411
+ climbed += steps.length - 1;
5412
+ base = riserTop(climbed);
5413
+ landings.push({ box: box2, top, going: Math.round(g * MM4), k: i + 1, steps });
5414
+ } else {
5415
+ keep(out.treads, solid(box2, z0, top));
5416
+ landings.push({ box: box2, top, going: Math.round(g * MM4), k: i + 1 });
5417
+ }
5418
+ }
5419
+ });
5420
+ const taken = [...newels];
5421
+ for (const { box: box2, top, going, k, steps } of landings) {
5422
+ const [x0, y0, x1, y1] = box2;
5423
+ const p = D.probe * MM4;
5424
+ const edges = [
5425
+ { side: "top", horizontal: true, at: y0, out: -1 },
5426
+ { side: "right", horizontal: false, at: x1, out: 1 },
5427
+ { side: "bottom", horizontal: true, at: y1, out: 1 },
5428
+ { side: "left", horizontal: false, at: x0, out: -1 }
5429
+ ];
5430
+ for (const { side, horizontal, at: at2, out: dir } of edges) {
5431
+ const piecesAlong = (q0, q1) => {
5432
+ if (!steps) return [[q0, q1, top]];
5433
+ const pieces = [];
5434
+ for (const st of steps) {
5435
+ const on = st.ring.filter((p2) => Math.abs((horizontal ? p2[1] : p2[0]) - at2) <= 1).map((p2) => horizontal ? p2[0] : p2[1]);
5436
+ if (on.length < 2) continue;
5437
+ const [p0, p1] = [Math.max(q0, Math.min(...on)), Math.min(q1, Math.max(...on))];
5438
+ if (p1 > p0) pieces.push([p0, p1, st.top]);
5439
+ }
5440
+ return pieces.sort((a, b) => a[0] - b[0]);
5441
+ };
5442
+ const [a0, a1] = horizontal ? [x0, x1] : [y0, y1];
5443
+ const [s0, s1] = dir > 0 ? [at2, at2 + p] : [at2 - p, at2];
5444
+ const flightRuns = flightBoxes.filter(
5445
+ ([fa, fb, fc, fd]) => horizontal ? fb < s1 && fd > s0 && fa < a1 && fc > a0 : fa < s1 && fc > s0 && fb < a1 && fd > a0
5446
+ ).map(([fa, fb, fc, fd]) => horizontal ? [fa, fc] : [fb, fd]);
5447
+ const free = freeRuns(a0, a1, flightRuns, np);
5448
+ const open = free.filter(([r0, r1]) => {
5449
+ const m = Math.round((r0 + r1) / 2);
5450
+ return !solidAt(horizontal ? [m, at2 + dir * p] : [at2 + dir * p, m]);
5451
+ });
5452
+ const sideAt = out.sides.length;
5453
+ out.sides.push({
5454
+ of: `landing ${k}`,
5455
+ side,
5456
+ against: free.length === 0 ? "flight" : open.length === 0 ? "wall" : "open"
5457
+ });
5458
+ let railed = false;
5459
+ const across = side === "top" ? [y0, y0 + railW] : side === "bottom" ? [y1 - railW, y1] : side === "left" ? [x0, x0 + railW] : [x1 - railW, x1];
5460
+ for (const [r0, r1] of open) {
5461
+ const held = taken.filter((b) => horizontal ? b[1] < across[1] && b[3] > across[0] : b[0] < across[1] && b[2] > across[0]).map((b) => horizontal ? [b[0], b[2]] : [b[1], b[3]]);
5462
+ for (const [f0, f1] of freeRuns(r0, r1, held, np))
5463
+ for (const [q0, q1, under] of piecesAlong(f0, f1)) {
5464
+ const railTop = under + D.landingRailHeight * MM4;
5465
+ const b = horizontal ? [q0, across[0], q1, across[1]] : [across[0], q0, across[1], q1];
5466
+ keep(out.rails, solid(b, railTop - railW, railTop));
5467
+ taken.push(b);
5468
+ railed = true;
5469
+ const len = q1 - q0;
5470
+ const count = Math.max(1, Math.min(64, Math.floor(len / going)));
5471
+ const mid = Math.round((across[0] + across[1] - bb) / 2);
5472
+ for (let j = 0; j < count; j++) {
5473
+ const c = Math.round(q0 + len * (2 * j + 1) / (2 * count) - bb / 2);
5474
+ const bal = horizontal ? [c, mid, c + bb, mid + bb] : [mid, c, mid + bb, c + bb];
5475
+ keep(out.rails, solid(bal, under, railTop - railW + MM4));
5476
+ }
5477
+ }
5478
+ }
5479
+ if (open.length > 0 && !railed) out.sides[sideAt].against = "newel";
5480
+ }
5481
+ }
5482
+ return {
5483
+ treads: out.treads,
5484
+ stringers: out.stringers,
5485
+ rails: out.rails,
5486
+ risers: R,
5487
+ dropped: out.dropped,
5488
+ sides: out.sides,
5489
+ dims: {
5490
+ tread: T / MM4,
5491
+ nosing: nosings,
5492
+ stringer: { width: sw / MM4, above: above / MM4, below: below / MM4 },
5493
+ balustrade: {
5494
+ height: D.railHeight,
5495
+ rail: [D.rail, D.rail],
5496
+ baluster: bb / MM4,
5497
+ newel: np / MM4,
5498
+ newel_above: D.newelAbove,
5499
+ landing_height: D.landingRailHeight
5500
+ }
5501
+ }
5502
+ };
5503
+ }
5504
+
5505
+ // src/mesh/outdoor.ts
5506
+ import { DEFAULT_THEME } from "@chanmeng666/archlang";
5507
+ var OUTDOOR_MM = 50;
5508
+ var FENCE_HEIGHT_MM = { picket: 1e3, panel: 1800, post: 1100 };
5509
+ var CORE_POST_PITCH_MM = { picket: 600, panel: 1800, post: 1800 };
5510
+ var CORE_POSTS_PER_RUN = [1, 60];
5511
+ var FENCE_PROFILES = {
5512
+ picket: {
5513
+ post: 80,
5514
+ rails: [
5515
+ [20, 150, 220],
5516
+ [20, 700, 770]
5517
+ ],
5518
+ pickets: [70, 20, 140]
5519
+ },
5520
+ panel: { post: 100, rails: [[40, 0, 1800]] },
5521
+ post: { post: 100, rails: [[60, 1040, 1100]] }
4345
5522
  };
4346
- var record = (v, p, out) => {
4347
- if (!isObj(v)) fail(out, p, "must be an object");
5523
+ var MAX_FENCE_REPEAT = 400;
5524
+ var MM2 = 1e3;
5525
+ var tintOf = (surface) => GROUND_TINT[surface];
5526
+ function groundColour(tint, theme = DEFAULT_THEME) {
5527
+ const ch = (hex, i) => Number.parseInt(hex.slice(i, i + 2), 16);
5528
+ const bg = theme.bg;
5529
+ const wash = theme[tint];
5530
+ const out = [1, 3, 5].map((i) => {
5531
+ const v = ch(bg, i) - GROUND_WASH * (ch(bg, i) - ch(wash, i));
5532
+ return Math.max(0, Math.min(255, v)).toString(16).padStart(2, "0");
5533
+ });
5534
+ return `#${out.join("")}`;
5535
+ }
5536
+ function outdoorColour(surface) {
5537
+ const split = GROUND_SPLITS[surface];
5538
+ if (split) return { srgb: split.srgb, rule: "outdoor-colour-3d" };
5539
+ return { srgb: groundColour(tintOf(surface) ?? "paving"), rule: "outdoor-colour-theme" };
5540
+ }
5541
+ var fenceColour = (style) => (FENCE_PALETTE[style] ?? FENCE_PALETTE.picket).srgb;
5542
+ var at = (a, b, f) => [
5543
+ Math.round(a[0] + (b[0] - a[0]) * f),
5544
+ Math.round(a[1] + (b[1] - a[1]) * f)
5545
+ ];
5546
+ function corePostCount(lenUm, style) {
5547
+ const pitch = CORE_POST_PITCH_MM[style] ?? CORE_POST_PITCH_MM.picket;
5548
+ const [lo, hi] = CORE_POSTS_PER_RUN;
5549
+ return Math.max(lo, Math.min(hi, Math.round(lenUm / MM2 / pitch)));
5550
+ }
5551
+ function fenceRuns(f) {
5552
+ const pts = f.points;
5553
+ const all = [];
5554
+ for (let i = 0; i + 1 < pts.length; i++) all.push([pts[i], pts[i + 1]]);
5555
+ if (f.closed && pts.length > 2) all.push([pts[pts.length - 1], pts[0]]);
5556
+ const runs = all.filter(([a, b]) => Math.hypot(b[0] - a[0], b[1] - a[1]) > 0);
5557
+ return { runs, ring: f.closed && pts.length > 2 && runs.length > 1 };
5558
+ }
5559
+ function fencePosts(f) {
5560
+ const { runs, ring: ring2 } = fenceRuns(f);
5561
+ const out = [];
5562
+ runs.forEach(([a, b], i) => {
5563
+ const n = corePostCount(Math.hypot(b[0] - a[0], b[1] - a[1]), f.style);
5564
+ for (let k = 0; k < n; k++) out.push({ at: at(a, b, k / n), run: [a, b] });
5565
+ if (i === runs.length - 1 && !ring2) out.push({ at: b, run: [a, b] });
5566
+ });
5567
+ return out;
5568
+ }
5569
+ function fencePieces(f, profile, height) {
5570
+ const out = [];
5571
+ const box2 = (c, [a, b], w, d) => {
5572
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
5573
+ const ux = (b[0] - a[0]) / len;
5574
+ const uy = (b[1] - a[1]) / len;
5575
+ const p = (s) => [Math.round(c[0] + ux * s), Math.round(c[1] + uy * s)];
5576
+ return slab2d(p(-w / 2), p(w / 2), d);
5577
+ };
5578
+ for (const p of fencePosts(f))
5579
+ out.push({ poly: box2(p.at, p.run, profile.post * MM2, profile.post * MM2), z: [0, height] });
5580
+ for (const [a, b] of fenceRuns(f).runs) {
5581
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
5582
+ for (const [depth, z0, z1] of profile.rails)
5583
+ out.push({ poly: slab2d(a, b, depth * MM2), z: [Math.min(z0, height), Math.min(z1, height)] });
5584
+ if (profile.pickets) {
5585
+ const [w, d, pitch] = profile.pickets;
5586
+ const m = Math.max(1, Math.min(MAX_FENCE_REPEAT, Math.ceil(len / (pitch * MM2))));
5587
+ const pw = Math.min(w * MM2, len / m);
5588
+ for (let j = 0; j < m; j++) out.push({ poly: box2(at(a, b, (j + 0.5) / m), [a, b], pw, d * MM2), z: [0, height] });
5589
+ }
5590
+ }
5591
+ return out;
5592
+ }
5593
+
5594
+ // src/mesh/build.ts
5595
+ var MM3 = 1e3;
5596
+ var shadowRule = (r, size) => ({
5597
+ of: r.of,
5598
+ size,
5599
+ min: r.minMm,
5600
+ max: r.maxMm
5601
+ });
5602
+ var SLAB_MM = 200;
5603
+ var FLOOR_FINISH_MM = 5;
5604
+ var ROOF_MM = 150;
5605
+ var LEAF_MM = 40;
5606
+ var GLASS_MM = 16;
5607
+ var FRAME_WIDTH_MM = 40;
5608
+ var FRAME_DEPTH_MM = 60;
5609
+ var FURNITURE_DEFAULT_MM = 750;
5610
+ var FURNITURE_HEIGHT_MM = {
5611
+ bed: 500,
5612
+ double_bed: 500,
5613
+ bunk_bed: 1600,
5614
+ sofa: 800,
5615
+ armchair: 800,
5616
+ chair: 850,
5617
+ table: 750,
5618
+ dining_table: 750,
5619
+ desk: 750,
5620
+ wardrobe: 2e3,
5621
+ bookshelf: 1800,
5622
+ fridge: 1800,
5623
+ stove: 900,
5624
+ kitchen_sink: 900,
5625
+ counter: 900,
5626
+ dishwasher: 850,
5627
+ washing_machine: 850,
5628
+ wc: 750,
5629
+ basin: 850,
5630
+ bath: 550,
5631
+ shower: 50,
5632
+ tv: 600,
5633
+ rug: 10
4348
5634
  };
4349
- var oneOf = (...values) => (v, p, out) => {
4350
- if (!values.includes(v)) fail(out, p, `must be one of ${values.map((x) => JSON.stringify(x)).join(", ")}`);
5635
+ var STAGE = {
5636
+ slab: "brief",
5637
+ floor: "rooms",
5638
+ column: "walls",
5639
+ wall: "walls",
5640
+ roof: "walls",
5641
+ door: "openings",
5642
+ window: "openings",
5643
+ opening: "openings",
5644
+ furniture: "fixtures",
5645
+ stair: "walls",
5646
+ // The site (card RM8.3): its surfaces are laid out with the slab, before the rooms; a fence rises
5647
+ // with the storey's walls.
5648
+ outdoor: "brief",
5649
+ fence: "walls"
4351
5650
  };
4352
- var either = (...checks) => (v, p, out) => {
4353
- const ok = checks.some((c) => {
4354
- const tmp = [];
4355
- c(v, p, tmp);
4356
- return tmp.length === 0;
4357
- });
4358
- if (!ok) fail(out, p, "matches none of the allowed forms");
5651
+ var byText = (a, b) => a < b ? -1 : a > b ? 1 : 0;
5652
+ function along(a, b, d) {
5653
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;
5654
+ return [Math.round(a[0] + (b[0] - a[0]) / len * d), Math.round(a[1] + (b[1] - a[1]) / len * d)];
5655
+ }
5656
+ var inRing2 = (p, r) => {
5657
+ let c = false;
5658
+ for (let i = 0, j = r.length - 1; i < r.length; j = i++) {
5659
+ const a = r[i];
5660
+ const b = r[j];
5661
+ if (a[1] > p[1] !== b[1] > p[1] && p[0] < (b[0] - a[0]) * (p[1] - a[1]) / (b[1] - a[1]) + a[0]) c = !c;
5662
+ }
5663
+ return c;
4359
5664
  };
4360
- var array = (item, min = 0, max = Number.POSITIVE_INFINITY) => (v, p, out) => {
4361
- if (!Array.isArray(v)) return fail(out, p, "must be an array");
4362
- if (v.length < min) fail(out, p, `must have at least ${min} items`);
4363
- if (v.length > max) fail(out, p, `must have at most ${max} items`);
4364
- v.forEach((x, i) => {
4365
- item(x, `${p}/${i}`, out);
5665
+ function asRect(ring2) {
5666
+ if (ring2.length !== 4) return null;
5667
+ const xs = [...new Set(ring2.map((p) => p[0]))];
5668
+ const ys = [...new Set(ring2.map((p) => p[1]))];
5669
+ if (xs.length !== 2 || ys.length !== 2) return null;
5670
+ const axis = ring2.every((p, i) => {
5671
+ const q = ring2[(i + 1) % 4];
5672
+ return p[0] === q[0] || p[1] === q[1];
4366
5673
  });
4367
- };
4368
- var object = (required, optional = {}) => (v, p, out) => {
4369
- if (!isObj(v)) return fail(out, p, "must be an object");
4370
- for (const [k, c] of Object.entries(required)) {
4371
- if (!(k in v)) fail(out, `${p}/${k}`, "is required");
4372
- else c(v[k], `${p}/${k}`, out);
5674
+ return axis ? [Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys)] : null;
5675
+ }
5676
+ function rectTouches(r, ring2) {
5677
+ const corners = [
5678
+ [r[0], r[1]],
5679
+ [r[2], r[1]],
5680
+ [r[2], r[3]],
5681
+ [r[0], r[3]]
5682
+ ];
5683
+ if (corners.some((c) => inRing2(c, ring2))) return true;
5684
+ return ring2.some((p) => p[0] >= r[0] && p[0] <= r[2] && p[1] >= r[1] && p[1] <= r[3]);
5685
+ }
5686
+ function lessVoids(outer, voids, where2, ledger) {
5687
+ const rects = voids.map((v) => ({ id: v.id, r: [v.at[0], v.at[1], v.at[0] + v.size.w, v.at[1] + v.size.h] }));
5688
+ const box2 = asRect(outer);
5689
+ if (box2) {
5690
+ let pieces = [box2];
5691
+ for (const v of rects) pieces = pieces.flatMap((p) => rectMinus(p, v.r));
5692
+ return pieces.map((p) => rect(p[0], p[1], p[2], p[3]));
4373
5693
  }
4374
- for (const [k, c] of Object.entries(optional)) if (k in v) c(v[k], `${p}/${k}`, out);
4375
- for (const k of Object.keys(v)) {
4376
- if (!(k in required) && !(k in optional)) fail(out, `${p}/${k}`, "is not a Scene3D field");
5694
+ const holes = [];
5695
+ const taken = [];
5696
+ for (const v of rects) {
5697
+ if (!rectTouches(v.r, outer)) continue;
5698
+ const corners = rect(v.r[0], v.r[1], v.r[2], v.r[3]).outer;
5699
+ const strict = corners.every((c) => inRing2(c, outer)) && !outer.some((p) => p[0] >= v.r[0] && p[0] <= v.r[2] && p[1] >= v.r[1] && p[1] <= v.r[3]) && taken.every((t) => t[2] < v.r[0] || v.r[2] < t[0] || t[3] < v.r[1] || v.r[3] < t[1]);
5700
+ if (strict) {
5701
+ holes.push(corners);
5702
+ taken.push(v.r);
5703
+ } else {
5704
+ ledger.push({
5705
+ kind: "void",
5706
+ id: v.id,
5707
+ field: "cut",
5708
+ value: where2,
5709
+ provenance: "default",
5710
+ rule: "void-not-cut-non-rect"
5711
+ });
5712
+ }
4377
5713
  }
4378
- };
4379
- var treadCount2 = (v, p, out) => {
4380
- if (!Number.isInteger(v) || v < 1) fail(out, p, "must be an integer of at least 1");
4381
- };
4382
- var pt = array(integer, 2, 2);
4383
- var ring2 = array(pt, 3);
4384
- var polygon2 = object({ outer: ring2 }, { holes: array(ring2) });
4385
- var provenance = oneOf("authored", "derived", "default", "user-param");
4386
- var source2 = object({ kind: string, id: string }, { span: array(integer, 2, 2) });
4387
- var sized = (a, b) => object({ [a]: integer, [b]: integer });
4388
- var box2 = object({ id, source: source2, at: pt, size: sized("w", "h") });
4389
- var wall = object(
4390
- {
4391
- id,
4392
- source: source2,
4393
- footprint: array(polygon2),
4394
- body: array(polygon2),
4395
- height: integer,
4396
- thickness: integer,
4397
- cuts: array(string)
4398
- },
4399
- { category: string, material: string }
4400
- );
4401
- var LEVEL_COLLECTIONS = {
4402
- floors: array(object({ id, source: source2, polygon: polygon2 }, { uses: array(string), pattern: string, lift: integer })),
4403
- walls: array(wall),
4404
- openings: array(
4405
- object(
4406
- {
5714
+ return [{ outer: [...outer], ...holes.length ? { holes } : {} }];
5715
+ }
5716
+ var DOOR_POSES = ["closed", "plan"];
5717
+ function openLeaf(h, f, end, t) {
5718
+ const r = Math.hypot(end[0] - h[0], end[1] - h[1]);
5719
+ const c = Math.hypot(f[0] - h[0], f[1] - h[1]);
5720
+ if (r === 0 || c === 0) return { outer: [h, h, h] };
5721
+ const tt = Math.min(t, r / 2);
5722
+ const ox = (end[0] - h[0]) / r;
5723
+ const oy = (end[1] - h[1]) / r;
5724
+ const ax = (f[0] - h[0]) / c;
5725
+ const ay = (f[1] - h[1]) / c;
5726
+ const reach = Math.sqrt(r * r - tt * tt) - 1;
5727
+ const p = (x, y) => [Math.round(x), Math.round(y)];
5728
+ return {
5729
+ outer: [h, end, p(h[0] + ox * reach + ax * tt, h[1] + oy * reach + ay * tt), p(h[0] + ax * tt, h[1] + ay * tt)]
5730
+ };
5731
+ }
5732
+ function leafThroughWall(leaf, walls) {
5733
+ const [h, end, , inner] = leaf.outer;
5734
+ if (!h || !end || !inner) return false;
5735
+ const ox = (inner[0] - h[0]) / 2;
5736
+ const oy = (inner[1] - h[1]) / 2;
5737
+ for (let k = 1; k <= 19; k++) {
5738
+ const s = k / 20;
5739
+ const p = [h[0] + (end[0] - h[0]) * s + ox, h[1] + (end[1] - h[1]) * s + oy];
5740
+ for (const w of walls)
5741
+ for (const piece of w.body)
5742
+ if (inRing2(p, piece.outer) && !(piece.holes ?? []).some((r) => inRing2(p, r))) return true;
5743
+ }
5744
+ return false;
5745
+ }
5746
+ function levelNodes(lv, ledger, defaulted, isTop, doors, below, above) {
5747
+ const e = lv.elevation;
5748
+ const out = [];
5749
+ let holesLeftOut = 0;
5750
+ const prism2 = (poly, z0, z1, splits2) => {
5751
+ const s = prism(poly, z0, z1, splits2);
5752
+ if (s) holesLeftOut += degenerateHoles(poly);
5753
+ return s;
5754
+ };
5755
+ const node = (kind, id, order, shells, extras = {}, part) => {
5756
+ const ok = shells.filter((s) => s !== null);
5757
+ if (ok.length < shells.length) {
5758
+ ledger.push({
5759
+ kind,
5760
+ id,
5761
+ field: "shells_dropped",
5762
+ value: shells.length - ok.length,
5763
+ provenance: "derived",
5764
+ rule: "shell-degenerate-or-touching-hole"
5765
+ });
5766
+ }
5767
+ if (holesLeftOut > 0) {
5768
+ ledger.push({
5769
+ kind,
4407
5770
  id,
4408
- source: source2,
4409
- kind: oneOf("door", "window", "opening"),
4410
- host: string,
4411
- cut: ring2,
4412
- centre: pt,
4413
- jambs: array(pt, 2, 2),
4414
- width: integer,
4415
- sill: integer,
4416
- head: integer
5771
+ field: "holes_dropped",
5772
+ value: holesLeftOut,
5773
+ provenance: "derived",
5774
+ rule: "hole-degenerate"
5775
+ });
5776
+ holesLeftOut = 0;
5777
+ }
5778
+ if (ok.length === 0) return;
5779
+ out.push({
5780
+ key: `L${lv.level}/${kind}:${id}${part ? `:${part}` : ""}`,
5781
+ level: lv.level,
5782
+ kind,
5783
+ id,
5784
+ ...part ? { part } : {},
5785
+ stage: STAGE[kind],
5786
+ order,
5787
+ ...kind === "roof" ? { hidden: true } : {},
5788
+ extras,
5789
+ shells: ok
5790
+ });
5791
+ };
5792
+ node(
5793
+ "slab",
5794
+ "slab",
5795
+ 0,
5796
+ slabRegion(lv.footprint, lv.floors, ledger).flatMap((r) => lessVoids(r.outer, lv.voids, "slab", ledger)).map((p) => prism2(p, e - SLAB_MM * MM3, e)),
5797
+ { thickness_mm: SLAB_MM }
5798
+ );
5799
+ lv.floors.forEach((f, i) => {
5800
+ const pieces = f.polygon.holes?.length ? [f.polygon] : lessVoids(f.polygon.outer, lv.voids, `floor:${f.id}`, ledger);
5801
+ const z0 = e + (f.lift ?? 0);
5802
+ const authored = wordFinish(f.pattern);
5803
+ const finish = authored ?? floorFinish(f.uses);
5804
+ if (authored === void 0)
5805
+ ledger.push({
5806
+ kind: "floor",
5807
+ id: f.id,
5808
+ field: "finish",
5809
+ value: finish,
5810
+ provenance: "default",
5811
+ rule: "floor-finish-default"
5812
+ });
5813
+ node(
5814
+ "floor",
5815
+ f.id,
5816
+ i,
5817
+ pieces.map((p) => prism2(p, z0, z0 + FLOOR_FINISH_MM * MM3)),
5818
+ {
5819
+ ...f.uses?.length ? { uses: f.uses.join(" ") } : {},
5820
+ ...f.lift ? { lift_mm: f.lift / MM3 } : {},
5821
+ finish
5822
+ }
5823
+ );
5824
+ });
5825
+ const ceiling = above ? above.elevation - SLAB_MM * MM3 - e : 0;
5826
+ const slabAbove = above && ceiling > 0 ? slabRegion(above.footprint, above.floors, []).flatMap((r) => lessVoids(r.outer, above.voids, "slab", [])) : [];
5827
+ const stopped = (height, rings) => height > ceiling && ceiling > 0 && rings.every((r) => coveredBy(r, slabAbove));
5828
+ const stopLedger = (kind, id) => {
5829
+ ledger.push({
5830
+ kind,
5831
+ id,
5832
+ field: `top_mm(L${lv.level})`,
5833
+ value: ceiling / MM3,
5834
+ provenance: "derived",
5835
+ rule: "stops-under-slab-above"
5836
+ });
5837
+ };
5838
+ lv.columns.forEach((c, i) => {
5839
+ const box2 = rect(c.at[0], c.at[1], c.at[0] + c.size.w, c.at[1] + c.size.h);
5840
+ const stop = stopped(lv.storeyHeight, [box2.outer]);
5841
+ if (stop) stopLedger("column", c.id);
5842
+ node("column", c.id, i, [prism2(box2, e, e + (stop ? ceiling : lv.storeyHeight))]);
5843
+ });
5844
+ const climbs = [];
5845
+ lv.stairs.forEach((s, i) => {
5846
+ if (s.dir === "up") {
5847
+ climbs.push({ s, order: i, rise: above ? above.elevation - e : lv.storeyHeight, from: "own" });
5848
+ if (!above) {
5849
+ ledger.push({
5850
+ kind: "stair",
5851
+ id: s.id,
5852
+ field: "rise_mm",
5853
+ value: lv.storeyHeight / MM3,
5854
+ provenance: "default",
5855
+ rule: "stair-rise-storey-height"
5856
+ });
5857
+ }
5858
+ } else if (!below) {
5859
+ ledger.push({
5860
+ kind: "stair",
5861
+ id: s.id,
5862
+ field: "geometry",
5863
+ value: "omitted",
5864
+ provenance: "default",
5865
+ rule: "stair-down-no-storey-below"
5866
+ });
5867
+ }
5868
+ });
5869
+ (above?.stairs ?? []).forEach((s, i) => {
5870
+ const partnered = lv.stairs.some((t) => t.id === s.id && t.dir === "up");
5871
+ if (s.dir === "down" && !partnered && above)
5872
+ climbs.push({ s, order: lv.stairs.length + i, rise: above.elevation - e, from: "above" });
5873
+ });
5874
+ const stairFloor = e + FLOOR_FINISH_MM * MM3;
5875
+ const solidAt = (p) => lv.walls.some((w) => w.body.some((b) => inRing2(p, b.outer) && !(b.holes ?? []).some((r) => inRing2(p, r)))) || lv.columns.some((c) => p[0] > c.at[0] && p[0] < c.at[0] + c.size.w && p[1] > c.at[1] && p[1] < c.at[1] + c.size.h);
5876
+ for (const { s, order, rise, from } of climbs) {
5877
+ const solid = stairDetail(s, stairFloor, rise, solidAt);
5878
+ if (!solid) {
5879
+ ledger.push({
5880
+ kind: "stair",
5881
+ id: s.id,
5882
+ field: "geometry",
5883
+ value: "omitted",
5884
+ provenance: "default",
5885
+ rule: s.flights?.length ? "stair-rise-not-positive" : "stair-no-flights"
5886
+ });
5887
+ continue;
5888
+ }
5889
+ if (!defaulted.has("stair:defaults")) {
5890
+ defaulted.add("stair:defaults");
5891
+ ledger.push(
5892
+ {
5893
+ kind: "stair",
5894
+ field: "material",
5895
+ value: STAIR_MATERIAL,
5896
+ provenance: "default",
5897
+ rule: "stair-material-default"
5898
+ },
5899
+ // Card RM8.6: the handrails, balusters and newels in the walnut of the palette.
5900
+ {
5901
+ kind: "stair",
5902
+ field: "material(rail)",
5903
+ value: STAIR_RAIL_MATERIAL,
5904
+ provenance: "default",
5905
+ rule: "stair-rail-material-default"
5906
+ }
5907
+ );
5908
+ }
5909
+ ledger.push({
5910
+ kind: "stair",
5911
+ id: s.id,
5912
+ field: "risers",
5913
+ value: solid.risers,
5914
+ provenance: "derived",
5915
+ rule: "stair-risers-from-treads"
5916
+ });
5917
+ (s.landings ?? []).forEach((l, k) => {
5918
+ if (l.winders?.length)
5919
+ ledger.push(
5920
+ {
5921
+ kind: "stair",
5922
+ id: s.id,
5923
+ field: `winders(landing ${k + 1})`,
5924
+ value: l.winders.length,
5925
+ provenance: "derived",
5926
+ rule: "stair-winders-from-drawing"
5927
+ },
5928
+ {
5929
+ kind: "stair",
5930
+ id: s.id,
5931
+ field: `risers_form(landing ${k + 1})`,
5932
+ value: "solid",
5933
+ provenance: "default",
5934
+ rule: "stair-winders-solid"
5935
+ }
5936
+ );
5937
+ });
5938
+ const d = solid.dims;
5939
+ ledger.push(
5940
+ {
5941
+ kind: "stair",
5942
+ id: s.id,
5943
+ field: "tread_mm",
5944
+ value: { thickness: d.tread, nosing: d.nosing },
5945
+ provenance: "default",
5946
+ rule: "stair-tread-default"
4417
5947
  },
5948
+ { kind: "stair", id: s.id, field: "risers_form", value: "open", provenance: "default", rule: "stair-open-riser" },
4418
5949
  {
4419
- doorKind: oneOf("hinged", "sliding", "barn", "bifold", "pocket", "garage", "double", "glazed_sliding"),
4420
- leaves: array(array(pt, 2, 3)),
4421
- hinge: oneOf("left", "right"),
4422
- swing: oneOf("in", "out"),
4423
- slide: oneOf("left", "right")
5950
+ kind: "stair",
5951
+ id: s.id,
5952
+ field: "stringer_mm",
5953
+ value: d.stringer,
5954
+ provenance: "default",
5955
+ rule: "stair-stringer-default"
5956
+ },
5957
+ ...s.landings?.length ? [
5958
+ {
5959
+ kind: "stair",
5960
+ id: s.id,
5961
+ field: "landing",
5962
+ value: "solid",
5963
+ provenance: "default",
5964
+ rule: "stair-landing-solid"
5965
+ }
5966
+ ] : [],
5967
+ {
5968
+ kind: "stair",
5969
+ id: s.id,
5970
+ field: "balustrade_mm",
5971
+ value: d.balustrade,
5972
+ provenance: "default",
5973
+ rule: "stair-balustrade-default"
5974
+ },
5975
+ ...solid.sides.map((x) => ({
5976
+ kind: "stair",
5977
+ id: s.id,
5978
+ field: `side(${x.of}, ${x.side})`,
5979
+ value: x.against,
5980
+ provenance: "derived",
5981
+ rule: "stair-balustrade-side"
5982
+ })),
5983
+ ...above ? [
5984
+ {
5985
+ kind: "stair",
5986
+ id: s.id,
5987
+ field: "void_guard",
5988
+ value: "omitted",
5989
+ provenance: "default",
5990
+ rule: "stair-void-guard-omitted"
5991
+ }
5992
+ ] : []
5993
+ );
5994
+ node("stair", s.id, order, [...solid.treads, ...Array.from({ length: solid.dropped }, () => null)], {
5995
+ dir: s.dir,
5996
+ width_mm: s.width / MM3,
5997
+ flights: s.flights?.length ?? 0,
5998
+ risers: solid.risers,
5999
+ rise_mm: rise / MM3,
6000
+ ...from === "above" ? { from_storey_above: true } : {}
6001
+ });
6002
+ node("stair", s.id, order, solid.stringers, {}, "stringer");
6003
+ if (solid.rails.length) node("stair", s.id, order, solid.rails, {}, "rail");
6004
+ }
6005
+ const openings = new Map(lv.openings.map((o) => [o.id, o]));
6006
+ const wallHeight = new Map(
6007
+ lv.walls.map((w) => {
6008
+ const cut = w.cuts.flatMap((id) => {
6009
+ const o = openings.get(id);
6010
+ return o ? [o] : [];
6011
+ });
6012
+ const stop = stopped(w.height, [...w.body.map((p) => p.outer), ...cut.map((o) => o.cut)]);
6013
+ if (!stop) return [w.id, w.height];
6014
+ stopLedger("wall", w.id);
6015
+ for (const o of cut.filter((x) => x.head > ceiling)) {
6016
+ ledger.push({
6017
+ kind: o.kind,
6018
+ id: o.id,
6019
+ field: `head_mm(L${lv.level})`,
6020
+ value: ceiling / MM3,
6021
+ provenance: "derived",
6022
+ rule: "head-at-slab-above"
6023
+ });
4424
6024
  }
4425
- )
4426
- ),
4427
- furniture: array(
4428
- object(
4429
- { id, source: source2, category: string, centre: pt, size: sized("w", "d"), yawDeg: integer, asset: string },
4430
- { room: string, mirror: oneOf(true) }
4431
- )
4432
- ),
4433
- stairs: array(
4434
- object(
4435
- { id, source: source2, box: box2, dir: oneOf("up", "down"), width: integer },
6025
+ return [w.id, ceiling];
6026
+ })
6027
+ );
6028
+ const hostHeight = wallHeight;
6029
+ const splitAt = /* @__PURE__ */ new Map();
6030
+ for (const o of lv.openings) {
6031
+ const h = hostHeight.get(o.host);
6032
+ if (h === void 0) continue;
6033
+ const hs = [...o.sill > 0 ? [Math.min(o.sill, h)] : [], ...o.head < h ? [o.head] : []];
6034
+ for (const p of o.cut) {
6035
+ const k = `${p[0]},${p[1]}`;
6036
+ splitAt.set(k, [...splitAt.get(k) ?? [], ...hs]);
6037
+ }
6038
+ }
6039
+ const splits = (p) => (splitAt.get(`${p[0]},${p[1]}`) ?? []).map((h) => e + h);
6040
+ lv.walls.forEach((w, i) => {
6041
+ const height = wallHeight.get(w.id) ?? w.height;
6042
+ const top = e + height;
6043
+ const shells = w.body.map((p) => prism2(p, e, top, splits));
6044
+ for (const id of w.cuts) {
6045
+ const o = openings.get(id);
6046
+ if (!o) continue;
6047
+ if (o.sill > 0) shells.push(prism2({ outer: o.cut }, e, e + Math.min(o.sill, height)));
6048
+ if (o.head < height) shells.push(prism2({ outer: o.cut }, e + o.head, top));
6049
+ }
6050
+ node("wall", w.id, i, shells, {
6051
+ ...w.category ? { category: w.category } : {},
6052
+ height_mm: w.height / MM3,
6053
+ thickness_mm: w.thickness / MM3
6054
+ });
6055
+ });
6056
+ (lv.demolished ?? []).forEach((w, i) => {
6057
+ node(
6058
+ "wall",
6059
+ w.id,
6060
+ lv.walls.length + i,
6061
+ w.body.map((p) => prism2(p, e, e + w.height)),
4436
6062
  {
4437
- flights: array(
4438
- object({
4439
- at: pt,
4440
- size: sized("w", "h"),
4441
- foot: oneOf("top", "bottom", "left", "right"),
4442
- treads: treadCount2
4443
- }),
4444
- 1
4445
- ),
4446
- landings: array(object({ at: pt, size: sized("w", "h") }, { winders: array(ring2, 2) }))
6063
+ ...w.category ? { category: w.category } : {},
6064
+ height_mm: w.height / MM3,
6065
+ status: "demolish",
6066
+ thickness_mm: w.thickness / MM3
6067
+ },
6068
+ "demolish"
6069
+ );
6070
+ });
6071
+ const hostOf = new Map(lv.walls.map((w) => [w.id, w]));
6072
+ const nameless = (o) => /[:/\s]/.test(o.id);
6073
+ const trimInputs = lv.openings.flatMap((o) => {
6074
+ const host = hostOf.get(o.host);
6075
+ if (!host || nameless(o)) return [];
6076
+ const head = Math.min(o.head, hostHeight.get(o.host) ?? o.head);
6077
+ return [{ o, t: host.thickness, head, top: hostHeight.get(o.host) ?? host.height }];
6078
+ });
6079
+ const trims = storeyTrim(
6080
+ lv,
6081
+ trimInputs,
6082
+ e,
6083
+ (poly, a, b) => prism2(poly, a, b),
6084
+ (f) => {
6085
+ const floor = FLOOR_FINISH_MM * MM3;
6086
+ const family = f.asset.startsWith("t0:") ? f.asset.slice(3) : void 0;
6087
+ const turn = (-f.yawDeg % 360 + 360) % 360 % 90 === 0;
6088
+ if (!family || !turn) return [floor, floor + (FURNITURE_HEIGHT_MM[f.category] ?? FURNITURE_DEFAULT_MM) * MM3];
6089
+ const h = (T0_HEIGHT_MM[family] ?? 0) * MM3;
6090
+ if (T0_HUNG_FROM_TOP.has(family)) {
6091
+ const ys = (t0Shells(f, floor)?.shells ?? []).flatMap(
6092
+ (sh) => sh ? sh.positions.filter((_, k) => k % 3 === 1) : []
6093
+ );
6094
+ return [ys.length ? Math.min(...ys) : floor, floor + h];
4447
6095
  }
4448
- )
4449
- ),
4450
- voids: array(box2),
4451
- columns: array(box2),
4452
- outdoor: array(object({ id, source: source2, surface: string, polygon: polygon2 }, { rail: array(string) })),
4453
- fences: array(object({ id, source: source2, style: string, points: array(pt, 2), closed: boolean })),
4454
- roofs: array(object({ id, source: source2, form: oneOf("flat"), ring: ring2, provenance }))
4455
- };
4456
- var level = object(
4457
- { level: integer, elevation: integer, storeyHeight: integer, footprint: array(ring2), ...LEVEL_COLLECTIONS },
4458
- { name: string, demolished: array(wall) }
4459
- );
4460
- var scene = object({
4461
- version: oneOf(0),
4462
- kind: oneOf("scene3d"),
4463
- meta: object(
4464
- {
4465
- name: string,
4466
- units: oneOf("mm"),
4467
- tool: object({ archlang3d: string, archlang: string }),
4468
- assumptions: array(object({ kind: string, field: string, value: any, provenance, rule: string }, { id: string }))
4469
- },
4470
- { north: either(oneOf("up", "down", "left", "right"), number), siteBoundary: ring2, params: record }
4471
- ),
4472
- levels: array(level, 1)
4473
- });
4474
- function references(v, out) {
4475
- const levels = Array.isArray(v.levels) ? v.levels : [];
4476
- levels.forEach((lv, li) => {
4477
- if (!isObj(lv)) return;
4478
- const ids = (k) => (Array.isArray(lv[k]) ? lv[k] : []).map((e) => isObj(e) ? String(e.id) : "");
4479
- for (const k of [...Object.keys(LEVEL_COLLECTIONS), "demolished"]) {
4480
- const seen = /* @__PURE__ */ new Set();
4481
- ids(k).forEach((x, i) => {
4482
- if (seen.has(x)) fail(out, `/levels/${li}/${k}/${i}/id`, `duplicates id "${x}" in ${k}`);
4483
- seen.add(x);
6096
+ return [floor + (T0_MOUNT_MM[family] ?? 0) * MM3, floor + h];
6097
+ }
6098
+ );
6099
+ lv.openings.forEach((o, i) => {
6100
+ const [j0, j1] = o.jambs;
6101
+ const head = Math.min(o.head, hostHeight.get(o.host) ?? o.head);
6102
+ const extras = { host: o.host, width_mm: o.width / MM3, sill_mm: o.sill / MM3, head_mm: o.head / MM3 };
6103
+ const trim = trims.get(o.id);
6104
+ if (hostOf.has(o.host) && nameless(o)) {
6105
+ ledger.push({
6106
+ kind: o.kind,
6107
+ id: o.id,
6108
+ field: "trim",
6109
+ value: "the id names no part node",
6110
+ provenance: "default",
6111
+ rule: "opening-trim-omitted"
6112
+ });
6113
+ } else if (trim) {
6114
+ ledger.push(...trim.ledger);
6115
+ for (const t of trim.parts) {
6116
+ const slot = `${o.kind}:${t.part}`;
6117
+ if (!defaulted.has(`trim:${slot}`)) {
6118
+ defaulted.add(`trim:${slot}`);
6119
+ ledger.push({
6120
+ kind: o.kind,
6121
+ field: `material(${t.part})`,
6122
+ value: TRIM_MATERIAL[slot] ?? o.kind,
6123
+ provenance: "default",
6124
+ rule: "trim-material-default"
6125
+ });
6126
+ }
6127
+ node(o.kind, o.id, i, t.shells, { host: o.host }, t.part);
6128
+ }
6129
+ }
6130
+ if (o.kind === "door") {
6131
+ const z0 = e + o.sill;
6132
+ const swung = o.leaves?.every((l) => l.length === 3) ? o.leaves : void 0;
6133
+ if (doors === "plan" && swung) {
6134
+ const open = swung.map(([h, f, end]) => openLeaf(h, f, end, LEAF_MM * MM3));
6135
+ const through = open.some((leaf) => leafThroughWall(leaf, lv.walls));
6136
+ if (through) {
6137
+ ledger.push({
6138
+ kind: "door",
6139
+ id: o.id,
6140
+ field: "leaf",
6141
+ value: "through-wall",
6142
+ provenance: "derived",
6143
+ rule: "door-leaf-through-wall"
6144
+ });
6145
+ }
6146
+ node(
6147
+ "door",
6148
+ o.id,
6149
+ i,
6150
+ open.map((leaf) => prism2(leaf, z0, e + head)),
6151
+ // `through_wall` rides on the node, so a GLB reader (whose file carries no ledger) sees it too.
6152
+ { ...extras, door_kind: o.doorKind ?? "hinged", pose: "plan", ...through ? { through_wall: true } : {} }
6153
+ );
6154
+ return;
6155
+ }
6156
+ if (doors === "plan") {
6157
+ ledger.push({
6158
+ kind: "door",
6159
+ id: o.id,
6160
+ field: "pose",
6161
+ value: "closed",
6162
+ provenance: "default",
6163
+ rule: "door-pose-plan-no-swing"
6164
+ });
6165
+ }
6166
+ const leaves = o.leaves ?? [[j0, j1]];
6167
+ node(
6168
+ "door",
6169
+ o.id,
6170
+ i,
6171
+ leaves.map(([h, f]) => prism2(slab2d(h, f, LEAF_MM * MM3), z0, e + head)),
6172
+ { ...extras, door_kind: o.doorKind ?? "hinged", pose: "closed" }
6173
+ );
6174
+ } else if (o.kind === "window") {
6175
+ const z0 = e + o.sill;
6176
+ const z1 = e + head;
6177
+ const fw = FRAME_WIDTH_MM * MM3;
6178
+ const fd = FRAME_DEPTH_MM * MM3;
6179
+ const sector = cutSector(o.cut, o.jambs, o.centre);
6180
+ if (sector) {
6181
+ const band = (depth, f0, f1) => sectorBand(sector, -depth / 2, depth / 2, f0, f1);
6182
+ const f = Math.min(0.5, fw / (sector.r * Math.abs(sector.sweep)));
6183
+ node("window", o.id, i, [prism2(band(GLASS_MM * MM3), z0, z1)], extras, "glass");
6184
+ node(
6185
+ "window",
6186
+ o.id,
6187
+ i,
6188
+ [
6189
+ prism2(band(fd, 0, f), z0, z1),
6190
+ prism2(band(fd, 1 - f, 1), z0, z1),
6191
+ prism2(band(fd), z0, z0 + fw),
6192
+ prism2(band(fd), z1 - fw, z1)
6193
+ ],
6194
+ extras,
6195
+ "frame"
6196
+ );
6197
+ return;
6198
+ }
6199
+ const fit = (p, a, b) => {
6200
+ const q = insideCut(p, o.cut);
6201
+ return q ? prism2(q, a, b) : null;
6202
+ };
6203
+ node("window", o.id, i, [fit(slab2d(j0, j1, GLASS_MM * MM3), z0, z1)], extras, "glass");
6204
+ node(
6205
+ "window",
6206
+ o.id,
6207
+ i,
6208
+ [
6209
+ fit(slab2d(j0, along(j0, j1, fw), fd), z0, z1),
6210
+ fit(slab2d(along(j1, j0, fw), j1, fd), z0, z1),
6211
+ fit(slab2d(j0, j1, fd), z0, z0 + fw),
6212
+ fit(slab2d(j0, j1, fd), z1 - fw, z1)
6213
+ ],
6214
+ extras,
6215
+ "frame"
6216
+ );
6217
+ }
6218
+ });
6219
+ const floorTop = e + FLOOR_FINISH_MM * MM3;
6220
+ const tallest = lv.walls.reduce((m, w) => Math.max(m, w.height), 0) || lv.storeyHeight;
6221
+ const tallestBuilt = lv.walls.reduce((m, w) => Math.max(m, wallHeight.get(w.id) ?? w.height), 0) || lv.storeyHeight;
6222
+ lv.furniture.forEach((f, i) => {
6223
+ const parts = t0Shells(f, floorTop);
6224
+ const family = parts ? f.asset.slice("t0:".length) : void 0;
6225
+ const h = family ? T0_HEIGHT_MM[family] ?? 0 : FURNITURE_HEIGHT_MM[f.category] ?? FURNITURE_DEFAULT_MM;
6226
+ if (!defaulted.has(f.category)) {
6227
+ defaulted.add(f.category);
6228
+ ledger.push({
6229
+ kind: "furniture",
6230
+ field: `height_mm(${f.category})`,
6231
+ value: h,
6232
+ provenance: "default",
6233
+ rule: family ? "furniture-height-t0" : f.category in FURNITURE_HEIGHT_MM ? "furniture-height-family" : "furniture-height-greybox"
4484
6234
  });
6235
+ const mount = family ? T0_MOUNT_MM[family] : void 0;
6236
+ if (mount !== void 0) {
6237
+ ledger.push({
6238
+ kind: "furniture",
6239
+ field: `mount_mm(${f.category})`,
6240
+ value: mount,
6241
+ provenance: "default",
6242
+ rule: "furniture-mount-default"
6243
+ });
6244
+ }
6245
+ for (const d of family ? T0_DEFAULTS[family] ?? [] : []) {
6246
+ ledger.push({
6247
+ kind: "furniture",
6248
+ field: `${d.field}(${f.category})`,
6249
+ value: d.value,
6250
+ provenance: "default",
6251
+ rule: d.rule
6252
+ });
6253
+ }
6254
+ const slots = family ? T0_MATERIALS[family] : void 0;
6255
+ if (slots?.length) {
6256
+ ledger.push({
6257
+ kind: "furniture",
6258
+ field: `material(${f.category})`,
6259
+ value: [...slots],
6260
+ provenance: "default",
6261
+ rule: "furniture-material-default"
6262
+ });
6263
+ }
4485
6264
  }
4486
- const walls = new Set(ids("walls"));
4487
- for (const [i, x] of ids("demolished").entries()) {
4488
- if (walls.has(x)) fail(out, `/levels/${li}/demolished/${i}/id`, `names a standing wall too ("${x}")`);
6265
+ const asset = parts ? f.asset : "greybox";
6266
+ if (f.asset !== asset) {
6267
+ ledger.push({
6268
+ kind: "furniture",
6269
+ id: f.id,
6270
+ field: "asset",
6271
+ value: "greybox",
6272
+ provenance: "default",
6273
+ rule: "furniture-t0-not-quarter-turn"
6274
+ });
4489
6275
  }
4490
- const openings = new Set(ids("openings"));
4491
- (Array.isArray(lv.openings) ? lv.openings : []).forEach((o, i) => {
4492
- if (isObj(o) && !walls.has(String(o.host))) {
4493
- fail(out, `/levels/${li}/openings/${i}/host`, `names no wall of this level ("${String(o.host)}")`);
6276
+ if (parts && family && isHung(family)) {
6277
+ if (T0_HUNG_FROM_TOP.has(family)) {
6278
+ const low = Math.min(...parts.shells.flatMap((s) => s ? s.positions.filter((_, k) => k % 3 === 1) : []));
6279
+ ledger.push({
6280
+ kind: "furniture",
6281
+ id: f.id,
6282
+ field: "mount_mm",
6283
+ value: (low - floorTop) / MM3,
6284
+ provenance: "default",
6285
+ rule: "furniture-mount-from-top"
6286
+ });
6287
+ }
6288
+ if (floorTop + h * MM3 > e + tallestBuilt) {
6289
+ ledger.push({
6290
+ kind: "furniture",
6291
+ id: f.id,
6292
+ field: "top_mm",
6293
+ value: h,
6294
+ provenance: "default",
6295
+ rule: "furniture-hung-above-walls"
6296
+ });
4494
6297
  }
6298
+ } else if (parts && family && floorTop + h * MM3 > e + tallestBuilt) {
6299
+ ledger.push({
6300
+ kind: "furniture",
6301
+ id: f.id,
6302
+ field: "top_mm",
6303
+ value: h,
6304
+ provenance: "default",
6305
+ rule: "furniture-above-walls"
6306
+ });
6307
+ }
6308
+ const [cx, cy] = f.centre;
6309
+ const box2 = rect(cx - f.size.w / 2, cy - f.size.d / 2, cx + f.size.w / 2, cy + f.size.d / 2);
6310
+ if (parts) holesLeftOut += parts.holesDropped;
6311
+ const shells = parts ? parts.shells : [prism2(box2, floorTop, floorTop + h * MM3)];
6312
+ node("furniture", f.id, i, shells, {
6313
+ category: f.category,
6314
+ yaw_deg: f.yawDeg,
6315
+ asset,
6316
+ ...f.room ? { room: f.room } : {},
6317
+ ...parts && f.mirror ? { mirror: true } : {},
6318
+ // Hung on the wall, not standing (`isHung`): a fact of the piece as built, so a reader (the
6319
+ // replay's contact shadows, card RM8.8) needs neither the T0 tables nor the ledger, whose
6320
+ // per-piece rows name no storey. Absent on a standing piece.
6321
+ ...parts && family && isHung(family) ? { hung: true } : {}
4495
6322
  });
4496
- (Array.isArray(lv.demolished) ? lv.demolished : []).forEach((w, i) => {
4497
- if (isObj(w) && Array.isArray(w.cuts) && w.cuts.length > 0)
4498
- fail(out, `/levels/${li}/demolished/${i}/cuts`, "a demolished wall hosts nothing");
6323
+ });
6324
+ const roofZ = [e + tallest, e + tallest + ROOF_MM * MM3];
6325
+ lv.roofs.forEach((r, i) => {
6326
+ node("roof", r.id, i, [prism2({ outer: r.ring }, ...roofZ)], { form: r.form });
6327
+ });
6328
+ if (isTop && lv.roofs.length === 0 && lv.footprint.length > 0) {
6329
+ ledger.push({
6330
+ kind: "roof",
6331
+ id: `L${lv.level}`,
6332
+ field: "ring",
6333
+ value: "wall footprint",
6334
+ provenance: "default",
6335
+ rule: "roof-default-footprint"
4499
6336
  });
4500
- (Array.isArray(lv.walls) ? lv.walls : []).forEach((w, i) => {
4501
- if (!isObj(w) || !Array.isArray(w.cuts)) return;
4502
- w.cuts.forEach((c, j) => {
4503
- if (!openings.has(String(c)))
4504
- fail(out, `/levels/${li}/walls/${i}/cuts/${j}`, `names no opening ("${String(c)}")`);
6337
+ node(
6338
+ "roof",
6339
+ "roof",
6340
+ 0,
6341
+ lv.footprint.map((ring2) => prism2({ outer: ring2 }, ...roofZ)),
6342
+ { form: "flat" }
6343
+ );
6344
+ }
6345
+ const slabHere = slabRegion(lv.footprint, lv.floors, []).flatMap((r) => lessVoids(r.outer, lv.voids, "slab", []));
6346
+ const over = (p, q) => overlapArea(p, q) > FLOOR_OVERLAP_MIN_UM2;
6347
+ const steps = /* @__PURE__ */ new Map();
6348
+ lv.outdoor.forEach((o, i) => {
6349
+ const earlier = lv.outdoor.slice(0, i).filter((p) => over(p.polygon, o.polygon));
6350
+ const onSlab = slabHere.some((s) => over(s, o.polygon));
6351
+ const floorsUnder = lv.floors.filter((f) => over(f.polygon, o.polygon));
6352
+ const taken = /* @__PURE__ */ new Set([
6353
+ ...onSlab ? [0] : [],
6354
+ ...floorsUnder.flatMap((f) => [f.lift ?? 0, (f.lift ?? 0) + FLOOR_FINISH_MM * MM3]),
6355
+ ...earlier.map((p) => (steps.get(p.id) ?? 0) * FLOOR_LIFT_UM)
6356
+ ]);
6357
+ let step = Math.max(onSlab ? 1 : 0, ...earlier.map((p) => 1 + (steps.get(p.id) ?? 0)));
6358
+ while (taken.has(step * FLOOR_LIFT_UM)) step++;
6359
+ steps.set(o.id, step);
6360
+ const lift = step * FLOOR_LIFT_UM;
6361
+ if (lift > 0) {
6362
+ ledger.push({
6363
+ kind: "outdoor",
6364
+ id: o.id,
6365
+ field: "lift_mm",
6366
+ value: lift / MM3,
6367
+ provenance: "derived",
6368
+ rule: "outdoor-overlap-lift"
6369
+ });
6370
+ }
6371
+ if (!defaulted.has("outdoor:thickness")) {
6372
+ defaulted.add("outdoor:thickness");
6373
+ ledger.push({
6374
+ kind: "outdoor",
6375
+ field: "thickness_mm",
6376
+ value: OUTDOOR_MM,
6377
+ provenance: "default",
6378
+ rule: "outdoor-thickness-default"
4505
6379
  });
6380
+ ledger.push({
6381
+ kind: "outdoor",
6382
+ field: "wash",
6383
+ value: GROUND_WASH,
6384
+ provenance: "default",
6385
+ rule: "outdoor-colour-wash"
6386
+ });
6387
+ }
6388
+ const colour = outdoorColour(o.surface);
6389
+ if (!defaulted.has(`outdoor:${o.surface}`)) {
6390
+ defaulted.add(`outdoor:${o.surface}`);
6391
+ ledger.push({
6392
+ kind: "outdoor",
6393
+ field: `colour(${o.surface})`,
6394
+ value: colour.srgb,
6395
+ provenance: "default",
6396
+ rule: tintOf(o.surface) ? colour.rule : "outdoor-surface-unknown"
6397
+ });
6398
+ }
6399
+ if (o.rail?.length) {
6400
+ ledger.push({
6401
+ kind: "outdoor",
6402
+ id: o.id,
6403
+ field: "rail",
6404
+ value: "omitted",
6405
+ provenance: "default",
6406
+ rule: "outdoor-rail-omitted"
6407
+ });
6408
+ }
6409
+ node("outdoor", o.id, i, [prism2(o.polygon, e - OUTDOOR_MM * MM3 + lift, e + lift)], {
6410
+ surface: o.surface,
6411
+ colour: colour.srgb,
6412
+ thickness_mm: OUTDOOR_MM,
6413
+ ...lift > 0 ? { lift_mm: lift / MM3 } : {}
4506
6414
  });
4507
6415
  });
4508
- }
4509
- function validateScene3D(v) {
4510
- const out = [];
4511
- scene(v, "", out);
4512
- if (isObj(v)) references(v, out);
6416
+ lv.fences.forEach((f, i) => {
6417
+ const known = f.style in FENCE_PROFILES;
6418
+ const style = known ? f.style : "picket";
6419
+ const profile = FENCE_PROFILES[style];
6420
+ const height = FENCE_HEIGHT_MM[style];
6421
+ const pieces = fencePieces({ ...f, style }, profile, height);
6422
+ if (pieces.length === 0) {
6423
+ ledger.push({
6424
+ kind: "fence",
6425
+ id: f.id,
6426
+ field: "geometry",
6427
+ value: "omitted",
6428
+ provenance: "derived",
6429
+ rule: "fence-no-run"
6430
+ });
6431
+ return;
6432
+ }
6433
+ if (!defaulted.has(`fence:${f.style}`)) {
6434
+ defaulted.add(`fence:${f.style}`);
6435
+ ledger.push(
6436
+ {
6437
+ kind: "fence",
6438
+ field: `height_mm(${f.style})`,
6439
+ value: height,
6440
+ provenance: "default",
6441
+ rule: known ? "fence-height-default" : "fence-style-unknown"
6442
+ },
6443
+ {
6444
+ kind: "fence",
6445
+ field: `profile(${f.style})`,
6446
+ value: {
6447
+ post_mm: profile.post,
6448
+ // The core's own spacing: a post on every tick it draws (`fencePosts`).
6449
+ post_pitch_mm: CORE_POST_PITCH_MM[style],
6450
+ rails_mm: profile.rails.map((r) => [...r]),
6451
+ ...profile.pickets ? { pickets_mm: [...profile.pickets] } : {}
6452
+ },
6453
+ provenance: "default",
6454
+ rule: "fence-profile-default"
6455
+ },
6456
+ {
6457
+ kind: "fence",
6458
+ field: `colour(${f.style})`,
6459
+ value: fenceColour(f.style),
6460
+ provenance: "default",
6461
+ rule: "fence-colour-default"
6462
+ }
6463
+ );
6464
+ }
6465
+ node(
6466
+ "fence",
6467
+ f.id,
6468
+ i,
6469
+ pieces.map((p) => prism2(p.poly, e + p.z[0] * MM3, e + p.z[1] * MM3)),
6470
+ { style: f.style, colour: fenceColour(f.style), height_mm: height, closed: f.closed }
6471
+ );
6472
+ });
4513
6473
  return out;
4514
6474
  }
4515
-
4516
- // src/replay/facts.ts
4517
- import { describe, lint } from "@chanmeng666/archlang";
4518
- var round2 = (x) => Math.round(x * 100) / 100;
4519
- function replayFacts(source3, opts) {
4520
- const view = opts.view ?? "all";
4521
- const shown = view === "all" ? [...opts.storeys] : opts.storeys.slice(view - 1, view);
4522
- if (shown.length === 0) return void 0;
4523
- const d = describe(source3, opts.world ? { world: opts.world } : {});
4524
- if (!d.ok) return void 0;
4525
- const byLevel = d.levels ? d.levels.map((l) => [l.level, l.totals]) : shown.map((n) => [n, d.totals]);
4526
- const pick = shown.map((n) => byLevel.find(([l]) => l === n)?.[1]);
4527
- if (pick.some((t) => t === void 0)) return void 0;
4528
- const totals = pick;
4529
- const sum = (f) => totals.reduce((a, t) => a + f(t), 0);
4530
- const nets = totals.map((t) => t.net_floor_area_m2);
4531
- const warnings = lint(source3, opts.world ? { world: opts.world } : {}).filter(
4532
- (x) => x.severity === "warning" && (x.level === void 0 || shown.includes(x.level))
4533
- ).length;
4534
- return {
4535
- ...view === "all" && shown.length > 1 ? { storeys: shown.length } : {},
4536
- rooms: sum((t) => t.rooms),
4537
- doors: sum((t) => t.doors),
4538
- windows: sum((t) => t.windows),
4539
- areaCentre: round2(sum((t) => t.floor_area_m2)),
4540
- ...nets.every((n) => n !== void 0) ? { areaNet: round2(nets.reduce((a, n) => a + n, 0)) } : {},
4541
- warnings
6475
+ function buildMesh(scene, opts = {}) {
6476
+ const doors = opts.doors ?? "closed";
6477
+ const asked = {
6478
+ kind: "door",
6479
+ field: "pose",
6480
+ value: "plan",
6481
+ provenance: "user-param",
6482
+ rule: "door-pose-plan"
4542
6483
  };
6484
+ const ledger = [
6485
+ ...doors === "closed" ? scene.meta.assumptions : scene.meta.assumptions.map((a) => a.rule === "door-pose-closed" ? asked : a),
6486
+ { kind: "slab", field: "thickness_mm", value: SLAB_MM, provenance: "default", rule: "slab-default-200" },
6487
+ { kind: "floor", field: "finish_mm", value: FLOOR_FINISH_MM, provenance: "default", rule: "floor-finish-5" },
6488
+ {
6489
+ kind: "window",
6490
+ field: "frame_mm",
6491
+ value: [FRAME_WIDTH_MM, FRAME_DEPTH_MM],
6492
+ provenance: "default",
6493
+ rule: "window-frame-40x60"
6494
+ },
6495
+ { kind: "door", field: "leaf_mm", value: LEAF_MM, provenance: "default", rule: "door-leaf-40" },
6496
+ { kind: "roof", field: "thickness_mm", value: ROOF_MM, provenance: "default", rule: "roof-flat-150" },
6497
+ // Card RM8.3 builds outdoor surfaces and fences (it replaced the two `m1-omits-outdoor-fences`
6498
+ // rows here; their defaults are ledgered per kind and style where they are built). The ground
6499
+ // plane the viewer draws round the building is in no node, so it is said here, in every Mesh3D.
6500
+ {
6501
+ kind: "ground",
6502
+ field: "plane",
6503
+ value: {
6504
+ drawn: "viewer only",
6505
+ level: scene.levels[0]?.level ?? 0,
6506
+ margin_mm: GROUND_PLANE.marginMm,
6507
+ colour: GROUND_PLANE.light,
6508
+ colour_dark: GROUND_PLANE.dark
6509
+ },
6510
+ provenance: "default",
6511
+ rule: "ground-plane-viewer-only"
6512
+ },
6513
+ // Card RM8.8: the soft contact shadows the viewer draws are in no node and not in the GLB, so,
6514
+ // as the ground plane, they are said here, in every Mesh3D: each band's width rule and alphas.
6515
+ {
6516
+ kind: "shadow",
6517
+ field: "contact",
6518
+ value: {
6519
+ drawn: "viewer only",
6520
+ wall_band_mm: shadowRule(CONTACT_SHADOW.wall, "thickness"),
6521
+ furniture_band_mm: shadowRule(CONTACT_SHADOW.furniture, "smaller footprint side"),
6522
+ fence_band_mm: shadowRule(CONTACT_SHADOW.fence, "post"),
6523
+ stair_thickness_mm: CONTACT_SHADOW.stairThicknessMm,
6524
+ alpha: { ...CONTACT_SHADOW.alpha },
6525
+ alpha_dark: { ...CONTACT_SHADOW.alphaDark }
6526
+ },
6527
+ provenance: "default",
6528
+ rule: "contact-shadows-viewer-only"
6529
+ }
6530
+ ];
6531
+ const defaulted = /* @__PURE__ */ new Set();
6532
+ const top = scene.levels.length - 1;
6533
+ const nodes = scene.levels.flatMap(
6534
+ (lv, i) => levelNodes(lv, ledger, defaulted, i === top, doors, scene.levels[i - 1], scene.levels[i + 1])
6535
+ );
6536
+ nodes.sort(
6537
+ (a, b) => a.level - b.level || byText(a.kind, b.kind) || byText(a.id, b.id) || byText(a.part ?? "", b.part ?? "")
6538
+ );
6539
+ return { version: 0, kind: "mesh3d", name: scene.meta.name, assumptions: ledger, nodes };
4543
6540
  }
4544
6541
 
4545
6542
  export {
4546
- degenerateHoles,
4547
- where,
4548
6543
  prism,
4549
- rect,
4550
6544
  slab2d,
4551
6545
  clipToConvex,
4552
6546
  insideCut,
@@ -4557,11 +6551,8 @@ export {
4557
6551
  T0_HEIGHT_MM,
4558
6552
  T0_MOUNT_MM,
4559
6553
  T0_HUNG_FROM_TOP,
4560
- T0_DEFAULTS,
4561
- isHung,
4562
6554
  T0_HANDED,
4563
6555
  handedAt,
4564
- isHandedAsset,
4565
6556
  T0_MATERIALS,
4566
6557
  sectionRing,
4567
6558
  partShell,
@@ -4580,6 +6571,33 @@ export {
4580
6571
  floorLifts,
4581
6572
  ARCHLANG_3D_VERSION,
4582
6573
  buildScene3D,
4583
- validateScene3D,
4584
- replayFacts
6574
+ MIN_CORE,
6575
+ adaptPlan,
6576
+ rectsOf,
6577
+ slabRegion,
6578
+ OPENING_DETAIL,
6579
+ insideOf,
6580
+ FLAT_MM,
6581
+ storeyTrim,
6582
+ stairRisers,
6583
+ stairSolid,
6584
+ STAIR_DETAIL,
6585
+ stairDetail,
6586
+ OUTDOOR_MM,
6587
+ FENCE_HEIGHT_MM,
6588
+ CORE_POST_PITCH_MM,
6589
+ CORE_POSTS_PER_RUN,
6590
+ FENCE_PROFILES,
6591
+ MAX_FENCE_REPEAT,
6592
+ groundColour,
6593
+ outdoorColour,
6594
+ fenceColour,
6595
+ corePostCount,
6596
+ fenceRuns,
6597
+ fencePosts,
6598
+ fencePieces,
6599
+ FURNITURE_HEIGHT_MM,
6600
+ DOOR_POSES,
6601
+ openLeaf,
6602
+ buildMesh
4585
6603
  };