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