@chanmeng666/archlang-3d 0.5.0 → 0.7.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,950 +0,0 @@
1
- import {
2
- T0_HEIGHT_MM,
3
- T0_HUNG_FROM_TOP,
4
- T0_MATERIALS,
5
- T0_MOUNT_MM,
6
- assetFor,
7
- buildScene3D,
8
- cutSector,
9
- degenerateHoles,
10
- floorOverlaps,
11
- insideCut,
12
- isHandedAsset,
13
- isHung,
14
- mixedHeightJoints,
15
- prism,
16
- rect,
17
- sectorBand,
18
- slab2d,
19
- t0Shells,
20
- wallOwnership,
21
- where
22
- } from "./chunk-WCWLYIVD.js";
23
- import {
24
- STAIR_MATERIAL,
25
- floorFinish
26
- } from "./chunk-EQBMPXB5.js";
27
-
28
- // src/adapter/adapt.ts
29
- import * as core from "@chanmeng666/archlang";
30
- var MIN_CORE = "1.57.0";
31
- var hasHanded = (storeys) => storeys.some((s) => s.ir.elements.some((e) => e.kind === "furniture" && isHandedAsset(assetFor(e.category))));
32
- function mirroredIds(source, world, n) {
33
- const unknown = Array.from({ length: n }, () => null);
34
- const planToJson2 = core.planToJson;
35
- if (typeof planToJson2 !== "function") return unknown;
36
- const r = planToJson2(source, world ? { world } : {});
37
- const pages = r.pages ? r.pages.map((p) => p.json) : r.json ? [r.json] : [];
38
- if (pages.length !== n) return unknown;
39
- return pages.map((j) => new Set(j.furniture.flatMap((f) => f.mirror === true && f.id !== void 0 ? [f.id] : [])));
40
- }
41
- function adaptPlan(source, opts = {}) {
42
- const resolvePlan2 = core.resolvePlan;
43
- const hatches = core.hatchKey;
44
- if (typeof resolvePlan2 !== "function" || typeof hatches !== "function") {
45
- return {
46
- ok: false,
47
- refusal: {
48
- code: "E3D_CORE_TOO_OLD",
49
- message: `@chanmeng666/archlang is too old (no resolvePlan or hatchKey); archlang-3d needs ${MIN_CORE} or later.`
50
- }
51
- };
52
- }
53
- const res = resolvePlan2(source, opts.world ? { world: opts.world } : {});
54
- const errors = res.diagnostics.filter((d) => d.severity === "error");
55
- if (!res.ir || errors.length > 0) {
56
- const codes = [...new Set(errors.map((d) => d.code))].join(", ");
57
- return {
58
- ok: false,
59
- refusal: {
60
- code: "E3D_PLAN_ERRORS",
61
- message: `The plan does not resolve (${codes || "no IR"}); fix it with \`arch validate\` first.`,
62
- diagnostics: errors
63
- }
64
- };
65
- }
66
- 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 }];
67
- if (storeys.every((s) => s.ir.walls.length === 0)) {
68
- return {
69
- ok: false,
70
- refusal: { code: "E3D_NO_WALLS", message: "The plan has no walls, so there is nothing to raise." }
71
- };
72
- }
73
- const scene = buildScene3D(storeys, {
74
- coreVersion: opts.coreVersion ?? "unknown",
75
- ...opts.curveTolMm !== void 0 ? { curveTolMm: opts.curveTolMm } : {},
76
- ...hasHanded(storeys) ? { mirrored: mirroredIds(source, opts.world, storeys.length) } : {}
77
- });
78
- return {
79
- ok: true,
80
- scene,
81
- storeys,
82
- diagnostics: res.diagnostics,
83
- warnings: [
84
- ...storeys.flatMap((s) => mixedHeightJoints(wallOwnership(s.ir.walls)).map((w) => ({ ...w, level: s.level }))),
85
- ...scene.levels.flatMap((lv) => floorOverlaps(lv.floors).map((w) => ({ ...w, level: lv.level })))
86
- ]
87
- };
88
- }
89
-
90
- // src/mesh/slab.ts
91
- var rectPolygon = (r) => ({
92
- outer: [
93
- [r[0], r[1]],
94
- [r[2], r[1]],
95
- [r[2], r[3]],
96
- [r[0], r[3]]
97
- ]
98
- });
99
- function rectMinus(a, b) {
100
- 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])];
101
- if (!(x0 < x1 && y0 < y1)) return [a];
102
- const out = [];
103
- if (a[1] < y0) out.push([a[0], a[1], a[2], y0]);
104
- if (y1 < a[3]) out.push([a[0], y1, a[2], a[3]]);
105
- if (a[0] < x0) out.push([a[0], y0, x0, y1]);
106
- if (x1 < a[2]) out.push([x1, y0, a[2], y1]);
107
- return out;
108
- }
109
- var rectilinear = (r) => r.every((p, i) => {
110
- const q = r[(i + 1) % r.length];
111
- return p[0] === q[0] || p[1] === q[1];
112
- });
113
- function rectsOf(r) {
114
- const ys = [...new Set(r.map((p) => p[1]))].sort((a, b) => a - b);
115
- const out = [];
116
- for (let k = 0; k + 1 < ys.length; k++) {
117
- const [y0, y1] = [ys[k], ys[k + 1]];
118
- const xs = [];
119
- for (let i = 0; i < r.length; i++) {
120
- const a = r[i];
121
- const b = r[(i + 1) % r.length];
122
- if (a[0] === b[0] && Math.min(a[1], b[1]) <= y0 && Math.max(a[1], b[1]) >= y1) xs.push(a[0]);
123
- }
124
- xs.sort((a, b) => a - b);
125
- for (let i = 0; i + 1 < xs.length; i += 2)
126
- if (xs[i] < xs[i + 1]) out.push([xs[i], y0, xs[i + 1], y1]);
127
- }
128
- return out;
129
- }
130
- function covered(room, rings) {
131
- const probes = room.flatMap((p, i) => {
132
- const q = room[(i + 1) % room.length];
133
- return [p, [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2]];
134
- });
135
- return rings.some((r) => probes.every((p) => where(p, r) !== false) && !r.some((p) => where(p, room) === true));
136
- }
137
- function apart(room, rings) {
138
- return rings.every((r) => room.every((p) => where(p, r) !== true) && r.every((p) => where(p, room) !== true));
139
- }
140
- function slabRegion(footprint, floors, ledger) {
141
- const out = footprint.map((r) => ({ outer: [...r] }));
142
- const exact = footprint.every(rectilinear);
143
- let taken = exact ? footprint.flatMap(rectsOf) : [];
144
- const added = [];
145
- for (const f of floors) {
146
- const room = f.polygon.outer;
147
- const unsplit = () => {
148
- ledger.push({
149
- kind: "slab",
150
- id: f.id,
151
- field: "union",
152
- value: "room overlaps the outline or an earlier room: added whole",
153
- provenance: "default",
154
- rule: "slab-room-unsplit"
155
- });
156
- };
157
- if (exact && rectilinear(room)) {
158
- let pieces = rectsOf(room);
159
- for (const t of taken) pieces = pieces.flatMap((p) => rectMinus(p, t));
160
- taken = [...taken, ...pieces];
161
- for (const p of pieces) out.push(rectPolygon(p));
162
- if (!pieces.every((p) => apart(rectPolygon(p).outer, added))) unsplit();
163
- continue;
164
- }
165
- const others = [...footprint, ...added, ...taken.map((t) => rectPolygon(t).outer)];
166
- if (covered(room, others)) continue;
167
- if (!apart(room, others)) unsplit();
168
- out.push({ outer: [...room] });
169
- added.push(room);
170
- }
171
- return out;
172
- }
173
- var PROBE_MIN = 2;
174
- var PROBE_OFF = 1;
175
- function meets(a, b, c, d) {
176
- const rx = b[0] - a[0];
177
- const ry = b[1] - a[1];
178
- const sx = d[0] - c[0];
179
- const sy = d[1] - c[1];
180
- const qx = c[0] - a[0];
181
- const qy = c[1] - a[1];
182
- const den = rx * sy - ry * sx;
183
- if (den !== 0) {
184
- const t = (qx * sy - qy * sx) / den;
185
- const u = (qx * ry - qy * rx) / den;
186
- return t >= 0 && t <= 1 && u >= 0 && u <= 1 ? [t] : [];
187
- }
188
- if (qx * ry - qy * rx !== 0) return [];
189
- const rr = rx * rx + ry * ry;
190
- 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);
191
- }
192
- function stretches(a, b, cutters) {
193
- const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
194
- const ts = [0, 1, ...cutters.flatMap(([c, d]) => meets(a, b, c, d))].sort((x, y) => x - y);
195
- const out = [];
196
- for (let i = 1; i < ts.length; i++) {
197
- const t0 = ts[i - 1];
198
- const t1 = ts[i];
199
- if ((t1 - t0) * len < PROBE_MIN) continue;
200
- const t = (t0 + t1) / 2;
201
- out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]);
202
- }
203
- return out;
204
- }
205
- var edgesOf = (r) => r.map((p, i) => [p, r[(i + 1) % r.length]]);
206
- function coveredBy(ring, region) {
207
- if (region.length === 0 || ring.length < 3) return false;
208
- const inRegion = (p) => region.some((s) => where(p, s.outer) !== false && !(s.holes ?? []).some((h) => where(p, h) === true));
209
- const ringEdges = edgesOf(ring);
210
- const regionEdges = region.flatMap((s) => [s.outer, ...s.holes ?? []].flatMap(edgesOf));
211
- for (const [a, b] of ringEdges) {
212
- if (!inRegion(a)) return false;
213
- if (!stretches(a, b, regionEdges).every(inRegion)) return false;
214
- }
215
- for (const [a, b] of regionEdges) {
216
- const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
217
- if (len === 0) continue;
218
- const nx = -(b[1] - a[1]) / len * PROBE_OFF;
219
- const ny = (b[0] - a[0]) / len * PROBE_OFF;
220
- for (const m of stretches(a, b, ringEdges)) {
221
- if (where(m, ring) !== true) continue;
222
- if (!inRegion([m[0] + nx, m[1] + ny]) || !inRegion([m[0] - nx, m[1] - ny])) return false;
223
- }
224
- }
225
- return true;
226
- }
227
-
228
- // src/mesh/stair.ts
229
- function stairRisers(flights) {
230
- return flights.reduce((n, f) => n + f.treads + 1, 0);
231
- }
232
- function runCoordinate(f, d) {
233
- const [x, y] = f.at;
234
- switch (f.foot) {
235
- case "bottom":
236
- return y + f.size.h - d;
237
- case "top":
238
- return y + d;
239
- case "right":
240
- return x + f.size.w - d;
241
- case "left":
242
- return x + d;
243
- }
244
- }
245
- var alongY = (foot) => foot === "top" || foot === "bottom";
246
- function sideways(section, c0, c1, runAlongY) {
247
- const up = prism({ outer: section }, c0, c1);
248
- if (!up) return null;
249
- const positions = [];
250
- for (let i = 0; i < up.positions.length; i += 3) {
251
- const [u, h, v] = [up.positions[i], up.positions[i + 1], up.positions[i + 2]];
252
- if (runAlongY) positions.push(h, v, u);
253
- else positions.push(u, v, h);
254
- }
255
- const indices = [...up.indices];
256
- if (!runAlongY) {
257
- for (let t = 0; t < indices.length; t += 3) {
258
- const b = indices[t + 1];
259
- indices[t + 1] = indices[t + 2];
260
- indices[t + 2] = b;
261
- }
262
- }
263
- return { positions, indices };
264
- }
265
- function stairSolid(stair, z0, rise) {
266
- const flights = stair.flights;
267
- if (!flights?.length || !(rise > 0)) return null;
268
- const R = stairRisers(flights);
269
- const riserTop = (k) => z0 + Math.round(rise * k / R);
270
- const shells = [];
271
- const landingTops = [];
272
- let dropped = 0;
273
- let climbed = 0;
274
- flights.forEach((f, i) => {
275
- const n = f.treads;
276
- const yRun = alongY(f.foot);
277
- const run = yRun ? f.size.h : f.size.w;
278
- const [lo, extent] = yRun ? [f.at[0], f.size.w] : [f.at[1], f.size.h];
279
- const width = Math.min(stair.width, extent);
280
- const c0 = lo + Math.round((extent - width) / 2);
281
- const c1 = c0 + width;
282
- const at = (t) => runCoordinate(f, Math.round(run * t / n));
283
- const section = [[at(0), z0]];
284
- for (let t = 0; t < n; t++) {
285
- const h = riserTop(climbed + t + 1);
286
- section.push([at(t), h], [at(t + 1), h]);
287
- }
288
- section.push([at(n), z0]);
289
- const s = sideways(section, c0, c1, yRun);
290
- if (s) shells.push(s);
291
- else dropped++;
292
- climbed += n + 1;
293
- const landing = stair.landings?.[i];
294
- if (landing && i < flights.length - 1) {
295
- const top = riserTop(climbed);
296
- landingTops.push(top);
297
- const box = prism(
298
- rect(landing.at[0], landing.at[1], landing.at[0] + landing.size.w, landing.at[1] + landing.size.h),
299
- z0,
300
- top
301
- );
302
- if (box) shells.push(box);
303
- else dropped++;
304
- }
305
- });
306
- return { shells, risers: R, landingTops, top: riserTop(R - 1), dropped };
307
- }
308
-
309
- // src/mesh/build.ts
310
- var MM = 1e3;
311
- var SLAB_MM = 200;
312
- var FLOOR_FINISH_MM = 5;
313
- var ROOF_MM = 150;
314
- var LEAF_MM = 40;
315
- var GLASS_MM = 16;
316
- var FRAME_WIDTH_MM = 40;
317
- var FRAME_DEPTH_MM = 60;
318
- var FURNITURE_DEFAULT_MM = 750;
319
- var FURNITURE_HEIGHT_MM = {
320
- bed: 500,
321
- double_bed: 500,
322
- bunk_bed: 1600,
323
- sofa: 800,
324
- armchair: 800,
325
- chair: 850,
326
- table: 750,
327
- dining_table: 750,
328
- desk: 750,
329
- wardrobe: 2e3,
330
- bookshelf: 1800,
331
- fridge: 1800,
332
- stove: 900,
333
- kitchen_sink: 900,
334
- counter: 900,
335
- dishwasher: 850,
336
- washing_machine: 850,
337
- wc: 750,
338
- basin: 850,
339
- bath: 550,
340
- shower: 50,
341
- tv: 600,
342
- rug: 10
343
- };
344
- var STAGE = {
345
- slab: "brief",
346
- floor: "rooms",
347
- column: "walls",
348
- wall: "walls",
349
- roof: "walls",
350
- door: "openings",
351
- window: "openings",
352
- opening: "openings",
353
- furniture: "fixtures",
354
- stair: "walls"
355
- };
356
- var byText = (a, b) => a < b ? -1 : a > b ? 1 : 0;
357
- function along(a, b, d) {
358
- const len = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;
359
- return [Math.round(a[0] + (b[0] - a[0]) / len * d), Math.round(a[1] + (b[1] - a[1]) / len * d)];
360
- }
361
- var inRing = (p, r) => {
362
- let c = false;
363
- for (let i = 0, j = r.length - 1; i < r.length; j = i++) {
364
- const a = r[i];
365
- const b = r[j];
366
- 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;
367
- }
368
- return c;
369
- };
370
- function asRect(ring) {
371
- if (ring.length !== 4) return null;
372
- const xs = [...new Set(ring.map((p) => p[0]))];
373
- const ys = [...new Set(ring.map((p) => p[1]))];
374
- if (xs.length !== 2 || ys.length !== 2) return null;
375
- const axis = ring.every((p, i) => {
376
- const q = ring[(i + 1) % 4];
377
- return p[0] === q[0] || p[1] === q[1];
378
- });
379
- return axis ? [Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys)] : null;
380
- }
381
- function rectTouches(r, ring) {
382
- const corners = [
383
- [r[0], r[1]],
384
- [r[2], r[1]],
385
- [r[2], r[3]],
386
- [r[0], r[3]]
387
- ];
388
- if (corners.some((c) => inRing(c, ring))) return true;
389
- return ring.some((p) => p[0] >= r[0] && p[0] <= r[2] && p[1] >= r[1] && p[1] <= r[3]);
390
- }
391
- function lessVoids(outer, voids, where2, ledger) {
392
- 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] }));
393
- const box = asRect(outer);
394
- if (box) {
395
- let pieces = [box];
396
- for (const v of rects) pieces = pieces.flatMap((p) => rectMinus(p, v.r));
397
- return pieces.map((p) => rect(p[0], p[1], p[2], p[3]));
398
- }
399
- const holes = [];
400
- const taken = [];
401
- for (const v of rects) {
402
- if (!rectTouches(v.r, outer)) continue;
403
- const corners = rect(v.r[0], v.r[1], v.r[2], v.r[3]).outer;
404
- const strict = corners.every((c) => inRing(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]);
405
- if (strict) {
406
- holes.push(corners);
407
- taken.push(v.r);
408
- } else {
409
- ledger.push({
410
- kind: "void",
411
- id: v.id,
412
- field: "cut",
413
- value: where2,
414
- provenance: "default",
415
- rule: "void-not-cut-non-rect"
416
- });
417
- }
418
- }
419
- return [{ outer: [...outer], ...holes.length ? { holes } : {} }];
420
- }
421
- var DOOR_POSES = ["closed", "plan"];
422
- function openLeaf(h, f, end, t) {
423
- const r = Math.hypot(end[0] - h[0], end[1] - h[1]);
424
- const c = Math.hypot(f[0] - h[0], f[1] - h[1]);
425
- if (r === 0 || c === 0) return { outer: [h, h, h] };
426
- const tt = Math.min(t, r / 2);
427
- const ox = (end[0] - h[0]) / r;
428
- const oy = (end[1] - h[1]) / r;
429
- const ax = (f[0] - h[0]) / c;
430
- const ay = (f[1] - h[1]) / c;
431
- const reach = Math.sqrt(r * r - tt * tt) - 1;
432
- const p = (x, y) => [Math.round(x), Math.round(y)];
433
- return {
434
- 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)]
435
- };
436
- }
437
- function leafThroughWall(leaf, walls) {
438
- const [h, end, , inner] = leaf.outer;
439
- if (!h || !end || !inner) return false;
440
- const ox = (inner[0] - h[0]) / 2;
441
- const oy = (inner[1] - h[1]) / 2;
442
- for (let k = 1; k <= 19; k++) {
443
- const s = k / 20;
444
- const p = [h[0] + (end[0] - h[0]) * s + ox, h[1] + (end[1] - h[1]) * s + oy];
445
- for (const w of walls)
446
- for (const piece of w.body)
447
- if (inRing(p, piece.outer) && !(piece.holes ?? []).some((r) => inRing(p, r))) return true;
448
- }
449
- return false;
450
- }
451
- function levelNodes(lv, ledger, defaulted, isTop, doors, below, above) {
452
- const e = lv.elevation;
453
- const out = [];
454
- let holesLeftOut = 0;
455
- const prism2 = (poly, z0, z1, splits2) => {
456
- const s = prism(poly, z0, z1, splits2);
457
- if (s) holesLeftOut += degenerateHoles(poly);
458
- return s;
459
- };
460
- const node = (kind, id, order, shells, extras = {}, part) => {
461
- const ok = shells.filter((s) => s !== null);
462
- if (ok.length < shells.length) {
463
- ledger.push({
464
- kind,
465
- id,
466
- field: "shells_dropped",
467
- value: shells.length - ok.length,
468
- provenance: "derived",
469
- rule: "shell-degenerate-or-touching-hole"
470
- });
471
- }
472
- if (holesLeftOut > 0) {
473
- ledger.push({
474
- kind,
475
- id,
476
- field: "holes_dropped",
477
- value: holesLeftOut,
478
- provenance: "derived",
479
- rule: "hole-degenerate"
480
- });
481
- holesLeftOut = 0;
482
- }
483
- if (ok.length === 0) return;
484
- out.push({
485
- key: `L${lv.level}/${kind}:${id}${part ? `:${part}` : ""}`,
486
- level: lv.level,
487
- kind,
488
- id,
489
- ...part ? { part } : {},
490
- stage: STAGE[kind],
491
- order,
492
- ...kind === "roof" ? { hidden: true } : {},
493
- extras,
494
- shells: ok
495
- });
496
- };
497
- node(
498
- "slab",
499
- "slab",
500
- 0,
501
- slabRegion(lv.footprint, lv.floors, ledger).flatMap((r) => lessVoids(r.outer, lv.voids, "slab", ledger)).map((p) => prism2(p, e - SLAB_MM * MM, e)),
502
- { thickness_mm: SLAB_MM }
503
- );
504
- lv.floors.forEach((f, i) => {
505
- const pieces = f.polygon.holes?.length ? [f.polygon] : lessVoids(f.polygon.outer, lv.voids, `floor:${f.id}`, ledger);
506
- const z0 = e + (f.lift ?? 0);
507
- const finish = floorFinish(f.uses);
508
- ledger.push({
509
- kind: "floor",
510
- id: f.id,
511
- field: "finish",
512
- value: finish,
513
- provenance: "default",
514
- rule: "floor-finish-default"
515
- });
516
- node(
517
- "floor",
518
- f.id,
519
- i,
520
- pieces.map((p) => prism2(p, z0, z0 + FLOOR_FINISH_MM * MM)),
521
- {
522
- ...f.uses?.length ? { uses: f.uses.join(" ") } : {},
523
- ...f.lift ? { lift_mm: f.lift / MM } : {},
524
- finish
525
- }
526
- );
527
- });
528
- const ceiling = above ? above.elevation - SLAB_MM * MM - e : 0;
529
- const slabAbove = above && ceiling > 0 ? slabRegion(above.footprint, above.floors, []).flatMap((r) => lessVoids(r.outer, above.voids, "slab", [])) : [];
530
- const stopped = (height, rings) => height > ceiling && ceiling > 0 && rings.every((r) => coveredBy(r, slabAbove));
531
- const stopLedger = (kind, id) => {
532
- ledger.push({
533
- kind,
534
- id,
535
- field: `top_mm(L${lv.level})`,
536
- value: ceiling / MM,
537
- provenance: "derived",
538
- rule: "stops-under-slab-above"
539
- });
540
- };
541
- lv.columns.forEach((c, i) => {
542
- const box = rect(c.at[0], c.at[1], c.at[0] + c.size.w, c.at[1] + c.size.h);
543
- const stop = stopped(lv.storeyHeight, [box.outer]);
544
- if (stop) stopLedger("column", c.id);
545
- node("column", c.id, i, [prism2(box, e, e + (stop ? ceiling : lv.storeyHeight))]);
546
- });
547
- const climbs = [];
548
- lv.stairs.forEach((s, i) => {
549
- if (s.dir === "up") {
550
- climbs.push({ s, order: i, rise: above ? above.elevation - e : lv.storeyHeight, from: "own" });
551
- if (!above) {
552
- ledger.push({
553
- kind: "stair",
554
- id: s.id,
555
- field: "rise_mm",
556
- value: lv.storeyHeight / MM,
557
- provenance: "default",
558
- rule: "stair-rise-storey-height"
559
- });
560
- }
561
- } else if (!below) {
562
- ledger.push({
563
- kind: "stair",
564
- id: s.id,
565
- field: "geometry",
566
- value: "omitted",
567
- provenance: "default",
568
- rule: "stair-down-no-storey-below"
569
- });
570
- }
571
- });
572
- (above?.stairs ?? []).forEach((s, i) => {
573
- const partnered = lv.stairs.some((t) => t.id === s.id && t.dir === "up");
574
- if (s.dir === "down" && !partnered && above)
575
- climbs.push({ s, order: lv.stairs.length + i, rise: above.elevation - e, from: "above" });
576
- });
577
- const stairFloor = e + FLOOR_FINISH_MM * MM;
578
- for (const { s, order, rise, from } of climbs) {
579
- const solid = stairSolid(s, stairFloor, rise);
580
- if (!solid) {
581
- ledger.push({
582
- kind: "stair",
583
- id: s.id,
584
- field: "geometry",
585
- value: "omitted",
586
- provenance: "default",
587
- rule: s.flights?.length ? "stair-rise-not-positive" : "stair-no-flights"
588
- });
589
- continue;
590
- }
591
- if (!defaulted.has("stair:defaults")) {
592
- defaulted.add("stair:defaults");
593
- ledger.push(
594
- {
595
- kind: "stair",
596
- field: "material",
597
- value: STAIR_MATERIAL,
598
- provenance: "default",
599
- rule: "stair-material-default"
600
- },
601
- { kind: "stair", field: "form", value: "solid", provenance: "default", rule: "stair-solid-default" }
602
- );
603
- }
604
- ledger.push({
605
- kind: "stair",
606
- id: s.id,
607
- field: "risers",
608
- value: solid.risers,
609
- provenance: "derived",
610
- rule: "stair-risers-from-treads"
611
- });
612
- node("stair", s.id, order, [...solid.shells, ...Array.from({ length: solid.dropped }, () => null)], {
613
- dir: s.dir,
614
- width_mm: s.width / MM,
615
- flights: s.flights?.length ?? 0,
616
- risers: solid.risers,
617
- rise_mm: rise / MM,
618
- ...from === "above" ? { from_storey_above: true } : {}
619
- });
620
- }
621
- const openings = new Map(lv.openings.map((o) => [o.id, o]));
622
- const wallHeight = new Map(
623
- lv.walls.map((w) => {
624
- const cut = w.cuts.flatMap((id) => {
625
- const o = openings.get(id);
626
- return o ? [o] : [];
627
- });
628
- const stop = stopped(w.height, [...w.body.map((p) => p.outer), ...cut.map((o) => o.cut)]);
629
- if (!stop) return [w.id, w.height];
630
- stopLedger("wall", w.id);
631
- for (const o of cut.filter((x) => x.head > ceiling)) {
632
- ledger.push({
633
- kind: o.kind,
634
- id: o.id,
635
- field: `head_mm(L${lv.level})`,
636
- value: ceiling / MM,
637
- provenance: "derived",
638
- rule: "head-at-slab-above"
639
- });
640
- }
641
- return [w.id, ceiling];
642
- })
643
- );
644
- const hostHeight = wallHeight;
645
- const splitAt = /* @__PURE__ */ new Map();
646
- for (const o of lv.openings) {
647
- const h = hostHeight.get(o.host);
648
- if (h === void 0) continue;
649
- const hs = [...o.sill > 0 ? [Math.min(o.sill, h)] : [], ...o.head < h ? [o.head] : []];
650
- for (const p of o.cut) {
651
- const k = `${p[0]},${p[1]}`;
652
- splitAt.set(k, [...splitAt.get(k) ?? [], ...hs]);
653
- }
654
- }
655
- const splits = (p) => (splitAt.get(`${p[0]},${p[1]}`) ?? []).map((h) => e + h);
656
- lv.walls.forEach((w, i) => {
657
- const height = wallHeight.get(w.id) ?? w.height;
658
- const top = e + height;
659
- const shells = w.body.map((p) => prism2(p, e, top, splits));
660
- for (const id of w.cuts) {
661
- const o = openings.get(id);
662
- if (!o) continue;
663
- if (o.sill > 0) shells.push(prism2({ outer: o.cut }, e, e + Math.min(o.sill, height)));
664
- if (o.head < height) shells.push(prism2({ outer: o.cut }, e + o.head, top));
665
- }
666
- node("wall", w.id, i, shells, {
667
- ...w.category ? { category: w.category } : {},
668
- height_mm: w.height / MM,
669
- thickness_mm: w.thickness / MM
670
- });
671
- });
672
- (lv.demolished ?? []).forEach((w, i) => {
673
- node(
674
- "wall",
675
- w.id,
676
- lv.walls.length + i,
677
- w.body.map((p) => prism2(p, e, e + w.height)),
678
- {
679
- ...w.category ? { category: w.category } : {},
680
- height_mm: w.height / MM,
681
- status: "demolish",
682
- thickness_mm: w.thickness / MM
683
- },
684
- "demolish"
685
- );
686
- });
687
- lv.openings.forEach((o, i) => {
688
- const [j0, j1] = o.jambs;
689
- const head = Math.min(o.head, hostHeight.get(o.host) ?? o.head);
690
- const extras = { host: o.host, width_mm: o.width / MM, sill_mm: o.sill / MM, head_mm: o.head / MM };
691
- if (o.kind === "door") {
692
- const z0 = e + o.sill;
693
- const swung = o.leaves?.every((l) => l.length === 3) ? o.leaves : void 0;
694
- if (doors === "plan" && swung) {
695
- const open = swung.map(([h, f, end]) => openLeaf(h, f, end, LEAF_MM * MM));
696
- const through = open.some((leaf) => leafThroughWall(leaf, lv.walls));
697
- if (through) {
698
- ledger.push({
699
- kind: "door",
700
- id: o.id,
701
- field: "leaf",
702
- value: "through-wall",
703
- provenance: "derived",
704
- rule: "door-leaf-through-wall"
705
- });
706
- }
707
- node(
708
- "door",
709
- o.id,
710
- i,
711
- open.map((leaf) => prism2(leaf, z0, e + head)),
712
- // `through_wall` rides on the node, so a GLB reader (whose file carries no ledger) sees it too.
713
- { ...extras, door_kind: o.doorKind ?? "hinged", pose: "plan", ...through ? { through_wall: true } : {} }
714
- );
715
- return;
716
- }
717
- if (doors === "plan") {
718
- ledger.push({
719
- kind: "door",
720
- id: o.id,
721
- field: "pose",
722
- value: "closed",
723
- provenance: "default",
724
- rule: "door-pose-plan-no-swing"
725
- });
726
- }
727
- const leaves = o.leaves ?? [[j0, j1]];
728
- node(
729
- "door",
730
- o.id,
731
- i,
732
- leaves.map(([h, f]) => prism2(slab2d(h, f, LEAF_MM * MM), z0, e + head)),
733
- { ...extras, door_kind: o.doorKind ?? "hinged", pose: "closed" }
734
- );
735
- } else if (o.kind === "window") {
736
- const z0 = e + o.sill;
737
- const z1 = e + head;
738
- const fw = FRAME_WIDTH_MM * MM;
739
- const fd = FRAME_DEPTH_MM * MM;
740
- const sector = cutSector(o.cut, o.jambs, o.centre);
741
- if (sector) {
742
- const band = (depth, f0, f1) => sectorBand(sector, -depth / 2, depth / 2, f0, f1);
743
- const f = Math.min(0.5, fw / (sector.r * Math.abs(sector.sweep)));
744
- node("window", o.id, i, [prism2(band(GLASS_MM * MM), z0, z1)], extras, "glass");
745
- node(
746
- "window",
747
- o.id,
748
- i,
749
- [
750
- prism2(band(fd, 0, f), z0, z1),
751
- prism2(band(fd, 1 - f, 1), z0, z1),
752
- prism2(band(fd), z0, z0 + fw),
753
- prism2(band(fd), z1 - fw, z1)
754
- ],
755
- extras,
756
- "frame"
757
- );
758
- return;
759
- }
760
- const fit = (p, a, b) => {
761
- const q = insideCut(p, o.cut);
762
- return q ? prism2(q, a, b) : null;
763
- };
764
- node("window", o.id, i, [fit(slab2d(j0, j1, GLASS_MM * MM), z0, z1)], extras, "glass");
765
- node(
766
- "window",
767
- o.id,
768
- i,
769
- [
770
- fit(slab2d(j0, along(j0, j1, fw), fd), z0, z1),
771
- fit(slab2d(along(j1, j0, fw), j1, fd), z0, z1),
772
- fit(slab2d(j0, j1, fd), z0, z0 + fw),
773
- fit(slab2d(j0, j1, fd), z1 - fw, z1)
774
- ],
775
- extras,
776
- "frame"
777
- );
778
- }
779
- });
780
- const floorTop = e + FLOOR_FINISH_MM * MM;
781
- const tallest = lv.walls.reduce((m, w) => Math.max(m, w.height), 0) || lv.storeyHeight;
782
- const tallestBuilt = lv.walls.reduce((m, w) => Math.max(m, wallHeight.get(w.id) ?? w.height), 0) || lv.storeyHeight;
783
- lv.furniture.forEach((f, i) => {
784
- const parts = t0Shells(f, floorTop);
785
- const family = parts ? f.asset.slice("t0:".length) : void 0;
786
- const h = family ? T0_HEIGHT_MM[family] ?? 0 : FURNITURE_HEIGHT_MM[f.category] ?? FURNITURE_DEFAULT_MM;
787
- if (!defaulted.has(f.category)) {
788
- defaulted.add(f.category);
789
- ledger.push({
790
- kind: "furniture",
791
- field: `height_mm(${f.category})`,
792
- value: h,
793
- provenance: "default",
794
- rule: family ? "furniture-height-t0" : f.category in FURNITURE_HEIGHT_MM ? "furniture-height-family" : "furniture-height-greybox"
795
- });
796
- const mount = family ? T0_MOUNT_MM[family] : void 0;
797
- if (mount !== void 0) {
798
- ledger.push({
799
- kind: "furniture",
800
- field: `mount_mm(${f.category})`,
801
- value: mount,
802
- provenance: "default",
803
- rule: "furniture-mount-default"
804
- });
805
- }
806
- const slots = family ? T0_MATERIALS[family] : void 0;
807
- if (slots?.length) {
808
- ledger.push({
809
- kind: "furniture",
810
- field: `material(${f.category})`,
811
- value: [...slots],
812
- provenance: "default",
813
- rule: "furniture-material-default"
814
- });
815
- }
816
- }
817
- const asset = parts ? f.asset : "greybox";
818
- if (f.asset !== asset) {
819
- ledger.push({
820
- kind: "furniture",
821
- id: f.id,
822
- field: "asset",
823
- value: "greybox",
824
- provenance: "default",
825
- rule: "furniture-t0-not-quarter-turn"
826
- });
827
- }
828
- if (parts && family && isHung(family)) {
829
- if (T0_HUNG_FROM_TOP.has(family)) {
830
- const low = Math.min(...parts.shells.flatMap((s) => s ? s.positions.filter((_, k) => k % 3 === 1) : []));
831
- ledger.push({
832
- kind: "furniture",
833
- id: f.id,
834
- field: "mount_mm",
835
- value: (low - floorTop) / MM,
836
- provenance: "default",
837
- rule: "furniture-mount-from-top"
838
- });
839
- }
840
- if (floorTop + h * MM > e + tallestBuilt) {
841
- ledger.push({
842
- kind: "furniture",
843
- id: f.id,
844
- field: "top_mm",
845
- value: h,
846
- provenance: "default",
847
- rule: "furniture-hung-above-walls"
848
- });
849
- }
850
- } else if (parts && family && floorTop + h * MM > e + tallestBuilt) {
851
- ledger.push({
852
- kind: "furniture",
853
- id: f.id,
854
- field: "top_mm",
855
- value: h,
856
- provenance: "default",
857
- rule: "furniture-above-walls"
858
- });
859
- }
860
- const [cx, cy] = f.centre;
861
- const box = rect(cx - f.size.w / 2, cy - f.size.d / 2, cx + f.size.w / 2, cy + f.size.d / 2);
862
- if (parts) holesLeftOut += parts.holesDropped;
863
- const shells = parts ? parts.shells : [prism2(box, floorTop, floorTop + h * MM)];
864
- node("furniture", f.id, i, shells, {
865
- category: f.category,
866
- yaw_deg: f.yawDeg,
867
- asset,
868
- ...f.room ? { room: f.room } : {},
869
- ...parts && f.mirror ? { mirror: true } : {}
870
- });
871
- });
872
- const roofZ = [e + tallest, e + tallest + ROOF_MM * MM];
873
- lv.roofs.forEach((r, i) => {
874
- node("roof", r.id, i, [prism2({ outer: r.ring }, ...roofZ)], { form: r.form });
875
- });
876
- if (isTop && lv.roofs.length === 0 && lv.footprint.length > 0) {
877
- ledger.push({
878
- kind: "roof",
879
- id: `L${lv.level}`,
880
- field: "ring",
881
- value: "wall footprint",
882
- provenance: "default",
883
- rule: "roof-default-footprint"
884
- });
885
- node(
886
- "roof",
887
- "roof",
888
- 0,
889
- lv.footprint.map((ring) => prism2({ outer: ring }, ...roofZ)),
890
- { form: "flat" }
891
- );
892
- }
893
- return out;
894
- }
895
- function buildMesh(scene, opts = {}) {
896
- const doors = opts.doors ?? "closed";
897
- const asked = {
898
- kind: "door",
899
- field: "pose",
900
- value: "plan",
901
- provenance: "user-param",
902
- rule: "door-pose-plan"
903
- };
904
- const ledger = [
905
- ...doors === "closed" ? scene.meta.assumptions : scene.meta.assumptions.map((a) => a.rule === "door-pose-closed" ? asked : a),
906
- { kind: "slab", field: "thickness_mm", value: SLAB_MM, provenance: "default", rule: "slab-default-200" },
907
- { kind: "floor", field: "finish_mm", value: FLOOR_FINISH_MM, provenance: "default", rule: "floor-finish-5" },
908
- {
909
- kind: "window",
910
- field: "frame_mm",
911
- value: [FRAME_WIDTH_MM, FRAME_DEPTH_MM],
912
- provenance: "default",
913
- rule: "window-frame-40x60"
914
- },
915
- { kind: "door", field: "leaf_mm", value: LEAF_MM, provenance: "default", rule: "door-leaf-40" },
916
- { kind: "roof", field: "thickness_mm", value: ROOF_MM, provenance: "default", rule: "roof-flat-150" },
917
- // Narrowed by card X3 (was `m1-omits-stairs-outdoor-fences`, kind `stair`): stairs are built.
918
- ...["outdoor", "fence"].map(
919
- (kind) => ({
920
- kind,
921
- field: "geometry",
922
- value: "omitted",
923
- provenance: "default",
924
- rule: "m1-omits-outdoor-fences"
925
- })
926
- )
927
- ];
928
- const defaulted = /* @__PURE__ */ new Set();
929
- const top = scene.levels.length - 1;
930
- const nodes = scene.levels.flatMap(
931
- (lv, i) => levelNodes(lv, ledger, defaulted, i === top, doors, scene.levels[i - 1], scene.levels[i + 1])
932
- );
933
- nodes.sort(
934
- (a, b) => a.level - b.level || byText(a.kind, b.kind) || byText(a.id, b.id) || byText(a.part ?? "", b.part ?? "")
935
- );
936
- return { version: 0, kind: "mesh3d", name: scene.meta.name, assumptions: ledger, nodes };
937
- }
938
-
939
- export {
940
- MIN_CORE,
941
- adaptPlan,
942
- rectsOf,
943
- slabRegion,
944
- stairRisers,
945
- stairSolid,
946
- FURNITURE_HEIGHT_MM,
947
- DOOR_POSES,
948
- openLeaf,
949
- buildMesh
950
- };