@chanmeng666/archlang-3d 0.8.1 → 0.9.0

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