@chanmeng666/archlang-3d 0.6.0 → 0.8.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,983 @@
1
+ import {
2
+ C16,
3
+ CONTACT_SHADOW,
4
+ S16,
5
+ ease
6
+ } from "./chunk-LNIVDMPG.js";
7
+
8
+ // src/replay/wall-lines.ts
9
+ function takesWallLines(n) {
10
+ return n.kind === "wall" && n.part === void 0 || n.kind === "column";
11
+ }
12
+ var EXACT = 2 ** 26;
13
+ function det(a, b, c, d) {
14
+ if (Math.abs(a) < EXACT && Math.abs(b) < EXACT && Math.abs(c) < EXACT && Math.abs(d) < EXACT) return a * d - b * c;
15
+ const r = BigInt(a) * BigInt(d) - BigInt(b) * BigInt(c);
16
+ return r > 0n ? 1 : r < 0n ? -1 : 0;
17
+ }
18
+ function detKey(a, b, c, d) {
19
+ if (Math.abs(a) < EXACT && Math.abs(b) < EXACT && Math.abs(c) < EXACT && Math.abs(d) < EXACT)
20
+ return String(a * d - b * c);
21
+ return (BigInt(a) * BigInt(d) - BigInt(b) * BigInt(c)).toString();
22
+ }
23
+ function gcd(a, b) {
24
+ let x = Math.abs(a);
25
+ let y = Math.abs(b);
26
+ while (y !== 0) [x, y] = [y, x % y];
27
+ return x;
28
+ }
29
+ function nearlyFlat(p, q, fp, fq) {
30
+ const d = BigInt(p[0]) * BigInt(q[0]) + BigInt(p[1]) * BigInt(q[1]);
31
+ const c = BigInt(p[0]) * BigInt(q[1]) - BigInt(p[1]) * BigInt(q[0]);
32
+ const s = fp * fq > 0 ? d : -d;
33
+ return s > 0n && s * s > 3n * c * c;
34
+ }
35
+ function planLine(x0, z0, x1, z1) {
36
+ let dx = x1 - x0;
37
+ let dz = z1 - z0;
38
+ const g = gcd(dx, dz);
39
+ dx /= g;
40
+ dz /= g;
41
+ if (dx < 0 || dx === 0 && dz < 0) {
42
+ dx = 0 - dx;
43
+ dz = 0 - dz;
44
+ }
45
+ return { dx, dz, key: `${dx},${dz},${detKey(dx, dz, x0, z0)}` };
46
+ }
47
+ var lexLess = (a, b) => {
48
+ for (let i = 0; i < a.length; i++) {
49
+ const d = a[i] - b[i];
50
+ if (d !== 0) return d;
51
+ }
52
+ return 0;
53
+ };
54
+ var sortedKeys = (m) => [...m.keys()].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
55
+ var sortedUnique = (xs) => {
56
+ const s = [...xs].sort((a, b) => a - b);
57
+ let n = 0;
58
+ for (let i = 0; i < s.length; i++) if (i === 0 || s[i] !== s[n - 1]) s[n++] = s[i];
59
+ s.length = n;
60
+ return s;
61
+ };
62
+ function extent(xs) {
63
+ let lo = Infinity;
64
+ let hi = -Infinity;
65
+ for (const x of xs) {
66
+ if (x < lo) lo = x;
67
+ if (x > hi) hi = x;
68
+ }
69
+ return [lo, hi];
70
+ }
71
+ function lastRaised(nodes, rank) {
72
+ let best = -1;
73
+ for (const n of nodes) {
74
+ if (best < 0) best = n;
75
+ else {
76
+ const rn = rank?.[n] ?? 0;
77
+ const rb = rank?.[best] ?? 0;
78
+ if (rn > rb || rn === rb && n < best) best = n;
79
+ }
80
+ }
81
+ return best;
82
+ }
83
+ function byStorey(nodes) {
84
+ const storeys = /* @__PURE__ */ new Map();
85
+ nodes.forEach((n, i) => {
86
+ if (!takesWallLines(n)) return;
87
+ const level = /^L(-?\d+)\//.exec(n.key)?.[1] ?? "";
88
+ const members = storeys.get(level);
89
+ if (members) members.push(i);
90
+ else storeys.set(level, [i]);
91
+ });
92
+ return sortedKeys(storeys).map((k) => storeys.get(k) ?? []);
93
+ }
94
+ function wallOutline(nodes, rank) {
95
+ const faces = wallFaces(nodes);
96
+ const drawn = nodes.map((n, i) => takesWallLines(n) ? { ...n, ...withFaces(n, faces[i]) } : n);
97
+ return { faces, lines: linesOf(drawn, rank) };
98
+ }
99
+ function linesOf(nodes, rank) {
100
+ const out = nodes.map((n) => takesWallLines(n) ? [] : void 0);
101
+ const segs = nodes.map(() => []);
102
+ for (const members of byStorey(nodes)) storeyLines(nodes, members, segs, rank);
103
+ segs.forEach((list, i) => {
104
+ if (!out[i]) return;
105
+ const sorted = list.map((s) => lexLess(s.slice(0, 3), s.slice(3)) <= 0 ? s : [s[3], s[4], s[5], s[0], s[1], s[2]]).sort(lexLess);
106
+ out[i] = sorted.flat();
107
+ });
108
+ return out;
109
+ }
110
+ function sweep(items, visit) {
111
+ const byStart = items.map((it, i) => [it, i]).sort((a, b) => a[0].t0 - b[0].t0 || a[1] - b[1]);
112
+ const ts = sortedUnique(items.flatMap((e) => [e.t0, e.t1]));
113
+ let next = 0;
114
+ let on = [];
115
+ for (let j = 0; j + 1 < ts.length; j++) {
116
+ const t0 = ts[j];
117
+ const t1 = ts[j + 1];
118
+ if (on.some((e) => e.t1 <= t0)) on = on.filter((e) => e.t1 > t0);
119
+ while (next < byStart.length && byStart[next]?.[0].t0 <= t0) on.push(byStart[next++]?.[0]);
120
+ if (on.length > 0) visit(t0, t1, on);
121
+ }
122
+ }
123
+ function storeyLines(nodes, members, segs, rank) {
124
+ const buckets = /* @__PURE__ */ new Map();
125
+ const lines = /* @__PURE__ */ new Map();
126
+ const dirs = /* @__PURE__ */ new Map();
127
+ const pairs = /* @__PURE__ */ new Map();
128
+ const push = (plane, line, e) => {
129
+ const key = `${plane}|${line}`;
130
+ const b = buckets.get(key);
131
+ if (b) b.edges.push(e);
132
+ else buckets.set(key, { line, plane, edges: [e] });
133
+ };
134
+ const pair = (plane, p, q, node) => {
135
+ const fwd = lexLess(p, q) < 0;
136
+ const [a, b] = fwd ? [p, q] : [q, p];
137
+ const key = `${plane}|${a[0]},${a[1]},${a[2]}|${b[0]},${b[1]},${b[2]}`;
138
+ const was = pairs.get(key);
139
+ if (was) {
140
+ was.net += fwd ? 1 : -1;
141
+ was.nodes.push(node);
142
+ } else pairs.set(key, { a, b, net: fwd ? 1 : -1, nodes: [node] });
143
+ };
144
+ const horizontal = (y, pl, p, q) => {
145
+ const key = `h${y}:${pl.key}`;
146
+ let at = lines.get(key);
147
+ if (!at) {
148
+ at = { kind: "horizontal", y, alongX: pl.dx !== 0, other: /* @__PURE__ */ new Map() };
149
+ lines.set(key, at);
150
+ }
151
+ if (at.kind === "horizontal") {
152
+ for (const r of [p, q]) {
153
+ const [x, , z] = r;
154
+ if (at.alongX) at.other.set(x, z);
155
+ else at.other.set(z, x);
156
+ }
157
+ }
158
+ return key;
159
+ };
160
+ const along = (pl, p) => pl.dx !== 0 ? p[0] : p[2];
161
+ const edge = (t0, t1, node, f) => t0 < t1 ? { t0, t1, k: 1, node, f } : { t0: t1, t1: t0, k: -1, node, f };
162
+ for (const ni of members) {
163
+ const n = nodes[ni];
164
+ const P = (v) => [
165
+ Math.round(n.positions[v * 3]),
166
+ Math.round(n.positions[v * 3 + 1]),
167
+ Math.round(n.positions[v * 3 + 2])
168
+ ];
169
+ for (let i = 0; i + 2 < n.indices.length; i += 3) {
170
+ const tri = [P(n.indices[i]), P(n.indices[i + 1]), P(n.indices[i + 2])];
171
+ const [a, b, c] = tri;
172
+ const ax = a[0];
173
+ const ay = a[1];
174
+ const az = a[2];
175
+ const sides = [
176
+ [a, b],
177
+ [b, c],
178
+ [c, a]
179
+ ];
180
+ if (ay === b[1] && ay === c[1]) {
181
+ if (det(b[0] - ax, b[2] - az, c[0] - ax, c[2] - az) === 0)
182
+ continue;
183
+ const plane2 = `H${ay}`;
184
+ for (const [p, q] of sides) {
185
+ if (p[0] === q[0] && p[2] === q[2]) continue;
186
+ const pl2 = planLine(p[0], p[2], q[0], q[2]);
187
+ push(plane2, horizontal(ay, pl2, p, q), edge(along(pl2, p), along(pl2, q), ni, 0));
188
+ }
189
+ continue;
190
+ }
191
+ const flatInPlan = det(b[0] - ax, b[2] - az, c[0] - ax, c[2] - az) === 0;
192
+ if (!flatInPlan) {
193
+ for (const [p, q] of sides) segs[ni].push([...p, ...q]);
194
+ continue;
195
+ }
196
+ const far = a[0] !== b[0] || a[2] !== b[2] ? b : c;
197
+ if (far[0] === ax && far[2] === az) continue;
198
+ const pl = planLine(ax, az, far[0], far[2]);
199
+ const plane = `V${pl.key}`;
200
+ if (!dirs.has(plane)) dirs.set(plane, [pl.dx, pl.dz]);
201
+ const s = (p) => along(pl, p);
202
+ const turn = det(s(b) - s(a), b[1] - ay, s(c) - s(a), c[1] - ay);
203
+ if (turn === 0) continue;
204
+ const f = turn > 0 ? 1 : -1;
205
+ for (const [p, q] of sides) {
206
+ if (p[0] === q[0] && p[2] === q[2]) {
207
+ if (p[1] === q[1]) continue;
208
+ const key = `v${p[0]},${p[2]}`;
209
+ if (!lines.has(key)) lines.set(key, { kind: "vertical", x: p[0], z: p[2] });
210
+ push(plane, key, edge(p[1], q[1], ni, f));
211
+ } else if (p[1] === q[1]) {
212
+ push(plane, horizontal(p[1], pl, p, q), edge(along(pl, p), along(pl, q), ni, f));
213
+ } else pair(plane, p, q, ni);
214
+ }
215
+ }
216
+ }
217
+ const bounds = /* @__PURE__ */ new Map();
218
+ for (const key of sortedKeys(buckets)) {
219
+ const { line, plane, edges } = buckets.get(key);
220
+ const [e0, e1] = edges;
221
+ if (edges.length === 2 && e1 && e0.t0 === e1.t0 && e0.t1 === e1.t1 && e0.k !== e1.k) continue;
222
+ sweep(edges, (t0, t1, on) => {
223
+ let m = 0;
224
+ for (const e of on) m += e.k;
225
+ if (m === 0) return;
226
+ const nets = /* @__PURE__ */ new Map();
227
+ for (const e of on) nets.set(e.node, (nets.get(e.node) ?? 0) + e.k);
228
+ const by = sortedUnique(on.filter((e) => nets.get(e.node) !== 0).map((e) => e.node));
229
+ const fs = sortedUnique(on.filter((e) => e.k === Math.sign(m)).map((e) => e.f));
230
+ const b = { t0, t1, nodes: by, plane, f: fs.length === 1 ? fs[0] : 0 };
231
+ const list = bounds.get(line);
232
+ if (list) list.push(b);
233
+ else bounds.set(line, [b]);
234
+ });
235
+ }
236
+ for (const line of sortedKeys(bounds)) {
237
+ const list = bounds.get(line);
238
+ const at = lines.get(line);
239
+ const runs = [];
240
+ sweep(list, (t0, t1, on) => {
241
+ if (at.kind === "vertical" && on.length === 2) {
242
+ const [p, q] = on;
243
+ const dp = dirs.get(p.plane);
244
+ const dq = dirs.get(q.plane);
245
+ if (dp && dq && p.f !== 0 && q.f !== 0 && nearlyFlat(dp, dq, p.f, q.f)) return;
246
+ }
247
+ const node = lastRaised(
248
+ on.flatMap((b) => b.nodes),
249
+ rank
250
+ );
251
+ const last = runs[runs.length - 1];
252
+ if (last && last.t1 === t0 && last.node === node) last.t1 = t1;
253
+ else runs.push({ t0, t1, node });
254
+ });
255
+ for (const r of runs) {
256
+ const end = (t) => {
257
+ if (at.kind === "vertical") return [at.x, t, at.z];
258
+ const o = at.other.get(t);
259
+ return at.alongX ? [t, at.y, o] : [o, at.y, t];
260
+ };
261
+ segs[r.node].push([...end(r.t0), ...end(r.t1)]);
262
+ }
263
+ }
264
+ for (const key of sortedKeys(pairs)) {
265
+ const { a, b, net, nodes: by } = pairs.get(key);
266
+ if (net === 0) continue;
267
+ segs[lastRaised(by, rank)].push([...a, ...b]);
268
+ }
269
+ }
270
+ function joinedGroups(fixes, standing) {
271
+ const up = /* @__PURE__ */ new Map();
272
+ fixes.forEach((f, i) => {
273
+ if (f && f.group >= 0) up.set(f.group, (up.get(f.group) ?? true) && standing(i));
274
+ });
275
+ const out = /* @__PURE__ */ new Set();
276
+ for (const [g, v] of up) if (v) out.add(g);
277
+ return out;
278
+ }
279
+ function wallFaces(nodes) {
280
+ const out = nodes.map(
281
+ (n) => takesWallLines(n) ? { hide: [], add: [], alone: [], joined: [], group: -1 } : void 0
282
+ );
283
+ const root = nodes.map((_, i) => i);
284
+ const find = (i) => {
285
+ let r = i;
286
+ while (root[r] !== r) r = root[r];
287
+ while (root[i] !== r) [i, root[i]] = [root[i], r];
288
+ return r;
289
+ };
290
+ const join = (a, b) => {
291
+ const ra = find(a);
292
+ const rb = find(b);
293
+ if (ra < rb) root[rb] = ra;
294
+ else if (rb < ra) root[ra] = rb;
295
+ };
296
+ const shared = /* @__PURE__ */ new Set();
297
+ for (const members of byStorey(nodes)) storeyFaces(nodes, members, out, join, shared);
298
+ out.forEach((f, i) => {
299
+ if (!f) return;
300
+ f.hide.sort((a, b) => a - b);
301
+ if (shared.has(i)) f.group = find(i);
302
+ });
303
+ return out;
304
+ }
305
+ function faceParts(n, fix) {
306
+ if (!fix) return { positions: n.positions, base: [...n.indices], alone: [], joined: [] };
307
+ const hidden = new Set(fix.hide);
308
+ const base = [];
309
+ for (let i = 0; i + 2 < n.indices.length; i += 3) {
310
+ if (!hidden.has(i / 3)) base.push(n.indices[i], n.indices[i + 1], n.indices[i + 2]);
311
+ }
312
+ let v = n.positions.length / 3;
313
+ const run = (flat, to = []) => {
314
+ for (let k = 0; k < flat.length / 3; k++) to.push(v++);
315
+ return to;
316
+ };
317
+ run(fix.add, base);
318
+ const alone = run(fix.alone);
319
+ const joined = run(fix.joined);
320
+ return { positions: n.positions.concat(fix.add, fix.alone, fix.joined), base, alone, joined };
321
+ }
322
+ function withFaces(n, fix, mode = "joined") {
323
+ if (!fix) return { positions: n.positions, indices: n.indices };
324
+ const parts = faceParts(n, fix);
325
+ return { positions: parts.positions, indices: parts.base.concat(mode === "joined" ? parts.joined : parts.alone) };
326
+ }
327
+ function contested(tris) {
328
+ const boxes = tris.map((t) => {
329
+ const [s0, s1] = extent(t.s);
330
+ const [y0, y1] = extent(t.p.map((q) => q[1]));
331
+ return { s0, s1, y0, y1, f: t.f };
332
+ });
333
+ boxes.sort((a, b) => a.s0 - b.s0);
334
+ let up = [];
335
+ let down = [];
336
+ for (const b of boxes) {
337
+ if (up.some((o) => o.s1 <= b.s0)) up = up.filter((o) => o.s1 > b.s0);
338
+ if (down.some((o) => o.s1 <= b.s0)) down = down.filter((o) => o.s1 > b.s0);
339
+ const other = b.f > 0 ? down : up;
340
+ if (other.some((o) => Math.min(o.y1, b.y1) > Math.max(o.y0, b.y0))) return true;
341
+ (b.f > 0 ? up : down).push(b);
342
+ }
343
+ return false;
344
+ }
345
+ function storeyFaces(nodes, members, out, join, shared) {
346
+ const planes = /* @__PURE__ */ new Map();
347
+ for (const ni of members) {
348
+ const n = nodes[ni];
349
+ const P = (v) => [
350
+ Math.round(n.positions[v * 3]),
351
+ Math.round(n.positions[v * 3 + 1]),
352
+ Math.round(n.positions[v * 3 + 2])
353
+ ];
354
+ for (let i = 0; i + 2 < n.indices.length; i += 3) {
355
+ const a = P(n.indices[i]);
356
+ const b = P(n.indices[i + 1]);
357
+ const c = P(n.indices[i + 2]);
358
+ const ax = a[0];
359
+ const ay = a[1];
360
+ const az = a[2];
361
+ if (ay === b[1] && ay === c[1]) continue;
362
+ if (det(b[0] - ax, b[2] - az, c[0] - ax, c[2] - az) !== 0)
363
+ continue;
364
+ const far = a[0] !== b[0] || a[2] !== b[2] ? b : c;
365
+ if (far[0] === ax && far[2] === az) continue;
366
+ const pl = planLine(ax, az, far[0], far[2]);
367
+ const along = (p) => pl.dx !== 0 ? p[0] : p[2];
368
+ const s = [along(a), along(b), along(c)];
369
+ const turn = det(
370
+ s[1] - s[0],
371
+ b[1] - ay,
372
+ s[2] - s[0],
373
+ c[1] - ay
374
+ );
375
+ if (turn === 0) continue;
376
+ const key = `V${pl.key}`;
377
+ const t = { node: ni, tri: i / 3, p: [a, b, c], f: turn > 0 ? 1 : -1, s };
378
+ const plane = planes.get(key);
379
+ if (plane) plane.tris.push(t);
380
+ else planes.set(key, { pl, tris: [t] });
381
+ }
382
+ }
383
+ for (const key of sortedKeys(planes)) {
384
+ const { tris } = planes.get(key);
385
+ if (!contested(tris)) continue;
386
+ const S = sortedUnique(tris.flatMap((t) => t.s));
387
+ const Y = sortedUnique(tris.flatMap((t) => t.p.map((q) => q[1])));
388
+ const where = /* @__PURE__ */ new Map();
389
+ for (const t of tris) for (let k = 0; k < 3; k++) where.set(t.s[k], t.p[k]);
390
+ const sAt = new Map(S.map((s, i) => [s, i]));
391
+ const yAt = new Map(Y.map((y, j) => [y, j]));
392
+ const ny = Y.length - 1;
393
+ const steps = S.map(() => []);
394
+ for (const t of tris) {
395
+ for (let k = 0; k < 3; k++) {
396
+ const p = t.p[k];
397
+ const q = t.p[(k + 1) % 3];
398
+ if (p[0] !== q[0] || p[2] !== q[2] || p[1] === q[1]) continue;
399
+ const i = sAt.get(t.s[k]);
400
+ const lo = Math.min(p[1], q[1]);
401
+ const hi = Math.max(p[1], q[1]);
402
+ steps[i].push([
403
+ t.node,
404
+ yAt.get(lo),
405
+ yAt.get(hi),
406
+ q[1] > p[1] ? 1 : -1
407
+ ]);
408
+ }
409
+ }
410
+ const wind = /* @__PURE__ */ new Map();
411
+ const nonzero = /* @__PURE__ */ new Map();
412
+ let live = [];
413
+ const total = new Int32Array(ny);
414
+ const cell = (to, s0, s1, j, facing) => {
415
+ const y0 = Y[j];
416
+ const y1 = Y[j + 1];
417
+ const at = (s, y) => {
418
+ const q = where.get(s);
419
+ return [q[0], y, q[2]];
420
+ };
421
+ const [p00, p10, p11, p01] = [at(s0, y0), at(s1, y0), at(s1, y1), at(s0, y1)];
422
+ if (facing > 0) to.push(...p00, ...p10, ...p11, ...p00, ...p11, ...p01);
423
+ else to.push(...p00, ...p11, ...p10, ...p00, ...p01, ...p11);
424
+ };
425
+ for (let i = 0; i + 1 < S.length; i++) {
426
+ const here = steps[i];
427
+ for (const [o, j0, j1, d] of here) {
428
+ let w = wind.get(o);
429
+ if (!w) {
430
+ w = new Int32Array(ny);
431
+ wind.set(o, w);
432
+ }
433
+ let nz = nonzero.get(o) ?? 0;
434
+ for (let j = j0; j < j1; j++) {
435
+ const was = w[j];
436
+ const now = was - d;
437
+ w[j] = now;
438
+ total[j] = total[j] - d;
439
+ if (was === 0) nz++;
440
+ else if (now === 0) nz--;
441
+ }
442
+ nonzero.set(o, nz);
443
+ }
444
+ if (here.length > 0) {
445
+ for (const o of [...wind.keys()]) {
446
+ if (nonzero.get(o) === 0) {
447
+ wind.delete(o);
448
+ nonzero.delete(o);
449
+ }
450
+ }
451
+ live = [...wind.keys()].sort((a, b) => a - b);
452
+ }
453
+ const s0 = S[i];
454
+ const s1 = S[i + 1];
455
+ for (let j = 0; j < ny; j++) {
456
+ const over = live.filter((o) => wind.get(o)[j] !== 0);
457
+ if (over.length === 0) continue;
458
+ if (over.length === 1) {
459
+ const o = over[0];
460
+ cell(out[o].add, s0, s1, j, wind.get(o)[j]);
461
+ continue;
462
+ }
463
+ for (const o of over) {
464
+ cell(out[o].alone, s0, s1, j, wind.get(o)[j]);
465
+ shared.add(o);
466
+ join(o, over[0]);
467
+ }
468
+ const sum = total[j];
469
+ if (sum === 0) continue;
470
+ const owner = over.find((o) => Math.sign(wind.get(o)[j]) === Math.sign(sum));
471
+ cell(out[owner].joined, s0, s1, j, sum);
472
+ }
473
+ }
474
+ for (const t of tris) out[t.node].hide.push(t.tri);
475
+ }
476
+ }
477
+
478
+ // src/replay/shadows.ts
479
+ var cross = (ux, uz, vx, vz) => det(ux, uz, vx, vz);
480
+ var levelOf = (key) => Number(/^L(-?\d+)\//.exec(key)?.[1] ?? 0);
481
+ var whole = (x) => Math.round(x) + 0;
482
+ var lengthOf = (dx, dz) => Math.sqrt(Number(BigInt(dx) * BigInt(dx) + BigInt(dz) * BigInt(dz)));
483
+ function contactFade(ev, u) {
484
+ if (!ev || ev.hidden) return 0;
485
+ const span = ev.riseTo - ev.riseFrom;
486
+ if (span <= 0) return u >= ev.riseTo ? 1 : 0;
487
+ const q = span / 4;
488
+ return ease((u - (ev.riseTo - q)) / q);
489
+ }
490
+ function contains(f, q) {
491
+ const s1 = cross(f.b[0] - f.a[0], f.b[1] - f.a[1], q[0] - f.a[0], q[1] - f.a[1]);
492
+ const s2 = cross(f.c[0] - f.b[0], f.c[1] - f.b[1], q[0] - f.b[0], q[1] - f.b[1]);
493
+ const s3 = cross(f.a[0] - f.c[0], f.a[1] - f.c[1], q[0] - f.c[0], q[1] - f.c[1]);
494
+ return s1 >= 0 && s2 >= 0 && s3 >= 0 || s1 <= 0 && s2 <= 0 && s3 <= 0;
495
+ }
496
+ var GRID_MAX = 64;
497
+ var TriGrid = class {
498
+ x0;
499
+ z0;
500
+ w;
501
+ h;
502
+ k;
503
+ cells;
504
+ constructor(tris) {
505
+ let [x0, z0, x1, z1] = [Infinity, Infinity, -Infinity, -Infinity];
506
+ for (const t of tris)
507
+ for (const p of [t.a, t.b, t.c]) {
508
+ if (p[0] < x0) x0 = p[0];
509
+ if (p[0] > x1) x1 = p[0];
510
+ if (p[1] < z0) z0 = p[1];
511
+ if (p[1] > z1) z1 = p[1];
512
+ }
513
+ this.k = tris.length === 0 ? 0 : Math.max(1, Math.min(GRID_MAX, Math.ceil(Math.sqrt(tris.length))));
514
+ this.x0 = x0;
515
+ this.z0 = z0;
516
+ this.w = this.k === 0 ? 1 : Math.max(1, Math.ceil((x1 - x0 + 1) / this.k));
517
+ this.h = this.k === 0 ? 1 : Math.max(1, Math.ceil((z1 - z0 + 1) / this.k));
518
+ this.cells = Array.from({ length: this.k * this.k }, () => []);
519
+ for (const t of tris) {
520
+ const xs = [t.a[0], t.b[0], t.c[0]];
521
+ const zs = [t.a[1], t.b[1], t.c[1]];
522
+ const [i0, i1] = [this.col(Math.min(...xs)), this.col(Math.max(...xs))];
523
+ const [j0, j1] = [this.row(Math.min(...zs)), this.row(Math.max(...zs))];
524
+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) this.cells[j * this.k + i]?.push(t);
525
+ }
526
+ }
527
+ col(x) {
528
+ return Math.floor((x - this.x0) / this.w);
529
+ }
530
+ row(z) {
531
+ return Math.floor((z - this.z0) / this.h);
532
+ }
533
+ at(q) {
534
+ if (this.k === 0) return [];
535
+ const i = this.col(q[0]);
536
+ const j = this.row(q[1]);
537
+ if (i < 0 || j < 0 || i >= this.k || j >= this.k) return [];
538
+ return this.cells[j * this.k + i] ?? [];
539
+ }
540
+ /** The triangles in the cells a plan box covers, each once (callers sort what they derive). */
541
+ near(x0, z0, x1, z1) {
542
+ if (this.k === 0) return [];
543
+ const clamp = (v) => Math.max(0, Math.min(this.k - 1, v));
544
+ const seen = /* @__PURE__ */ new Set();
545
+ for (let j = clamp(this.row(z0)); j <= clamp(this.row(z1)); j++)
546
+ for (let i = clamp(this.col(x0)); i <= clamp(this.col(x1)); i++)
547
+ for (const t of this.cells[j * this.k + i] ?? []) seen.add(t);
548
+ return [...seen];
549
+ }
550
+ };
551
+ function flatFaces(n, up, y, out) {
552
+ const p = n.positions;
553
+ for (let i = 0; i < n.indices.length; i += 3) {
554
+ const [ia, ib, ic] = [n.indices[i], n.indices[i + 1], n.indices[i + 2]];
555
+ const ya = p[ia * 3 + 1];
556
+ if (p[ib * 3 + 1] !== ya || p[ic * 3 + 1] !== ya || y !== void 0 && ya !== y) continue;
557
+ const a = [p[ia * 3], p[ia * 3 + 2]];
558
+ const b = [p[ib * 3], p[ib * 3 + 2]];
559
+ const c = [p[ic * 3], p[ic * 3 + 2]];
560
+ const turn = cross(b[0] - a[0], b[1] - a[1], c[0] - a[0], c[1] - a[1]);
561
+ if (up ? turn < 0 : turn > 0) out.push({ a, b, c, y: ya });
562
+ }
563
+ }
564
+ function outline(tris) {
565
+ const lines = /* @__PURE__ */ new Map();
566
+ for (const { node, p } of tris) {
567
+ for (let j = 0; j < 3; j++) {
568
+ const a = p[j];
569
+ const b = p[(j + 1) % 3];
570
+ if (a[0] === b[0] && a[1] === b[1]) continue;
571
+ const l = planLine(a[0], a[1], b[0], b[1]);
572
+ const alongX = l.dx !== 0;
573
+ const ta = alongX ? a[0] : a[1];
574
+ const tb = alongX ? b[0] : b[1];
575
+ let line = lines.get(l.key);
576
+ if (!line) {
577
+ line = { edges: [], at: /* @__PURE__ */ new Map() };
578
+ lines.set(l.key, line);
579
+ }
580
+ line.at.set(ta, a);
581
+ line.at.set(tb, b);
582
+ line.edges.push(ta < tb ? { t0: ta, t1: tb, k: 1, node } : { t0: tb, t1: ta, k: -1, node });
583
+ }
584
+ }
585
+ const out = [];
586
+ for (const key of [...lines.keys()].sort()) {
587
+ const line = lines.get(key);
588
+ if (!line) continue;
589
+ const pieces = [];
590
+ sweep(line.edges, (t0, t1, on) => {
591
+ let s = 0;
592
+ let node = Infinity;
593
+ for (const e of on) {
594
+ s += e.k;
595
+ if (e.node < node) node = e.node;
596
+ }
597
+ if (s === 0) return;
598
+ const sign = s > 0 ? 1 : -1;
599
+ const last = pieces[pieces.length - 1];
600
+ if (last && last.t1 === t0 && last.s === sign && last.node === node) last.t1 = t1;
601
+ else pieces.push({ t0, t1, s: sign, node });
602
+ });
603
+ for (const pc of pieces) {
604
+ const a = line.at.get(pc.t0);
605
+ const b = line.at.get(pc.t1);
606
+ out.push(pc.s > 0 ? { a, b, node: pc.node } : { a: b, b: a, node: pc.node });
607
+ }
608
+ }
609
+ return out;
610
+ }
611
+ function downFaces(n, node, y, out) {
612
+ const p = n.positions;
613
+ for (let i = 0; i < n.indices.length; i += 3) {
614
+ const [ia, ib, ic] = [n.indices[i], n.indices[i + 1], n.indices[i + 2]];
615
+ if (y !== void 0 && (p[ia * 3 + 1] !== y || p[ib * 3 + 1] !== y || p[ic * 3 + 1] !== y)) continue;
616
+ const a = [p[ia * 3], p[ia * 3 + 2]];
617
+ const b = [p[ib * 3], p[ib * 3 + 2]];
618
+ const c = [p[ic * 3], p[ic * 3 + 2]];
619
+ if (cross(b[0] - a[0], b[1] - a[1], c[0] - a[0], c[1] - a[1]) > 0) out.push({ node, p: [a, b, c] });
620
+ }
621
+ }
622
+ var yRange = (n) => {
623
+ let lo = Infinity;
624
+ let hi = -Infinity;
625
+ for (let i = 1; i < n.positions.length; i += 3) {
626
+ const y = n.positions[i];
627
+ if (y < lo) lo = y;
628
+ if (y > hi) hi = y;
629
+ }
630
+ return [lo, hi];
631
+ };
632
+ var Buf = class {
633
+ positions = [];
634
+ weight = [];
635
+ caster = [];
636
+ indices = [];
637
+ vertex(x, y, z, w, caster) {
638
+ this.positions.push(x, y, z);
639
+ this.weight.push(w);
640
+ this.caster.push(caster);
641
+ return this.weight.length - 1;
642
+ }
643
+ tri(a, b, c) {
644
+ this.indices.push(a, b, c);
645
+ }
646
+ /** A quad `a b c d` as two triangles. */
647
+ quad(a, b, c, d) {
648
+ this.indices.push(a, b, c, a, c, d);
649
+ }
650
+ };
651
+ function frame(s) {
652
+ const dx = s.b[0] - s.a[0];
653
+ const dz = s.b[1] - s.a[1];
654
+ const len = lengthOf(dx, dz);
655
+ return { d: [dx, dz], len, u: [dx / len, dz / len], n: [dz / len, -dx / len] };
656
+ }
657
+ var scaled = (v, mm) => [
658
+ whole(v[0] * mm * 1e3),
659
+ whole(v[1] * mm * 1e3)
660
+ ];
661
+ var plus = (p, o) => [p[0] + o[0], p[1] + o[1]];
662
+ var MAX_FAN = 8;
663
+ function contactShadows(nodes, ground, lines) {
664
+ const counts = {
665
+ edges: 0,
666
+ convex: 0,
667
+ concave: 0,
668
+ flatJoints: 0,
669
+ dropped: 0,
670
+ furniture: 0,
671
+ fenceSides: 0,
672
+ stairEdges: 0,
673
+ triangles: 0,
674
+ probes: 0,
675
+ tests: 0
676
+ };
677
+ const casters = [];
678
+ const layers = [];
679
+ const levels = [...new Set(nodes.map((n) => levelOf(n.key)))].sort((a, b) => a - b);
680
+ const lowest = levels[0];
681
+ const lift = CONTACT_SHADOW.liftMm * 1e3;
682
+ for (const level of levels) {
683
+ const here = nodes.map((n, i) => [n, i]).filter(([n]) => levelOf(n.key) === level);
684
+ const faces = [];
685
+ for (const [n] of here)
686
+ if (n.kind === "floor" || n.kind === "slab" || n.kind === "outdoor" || n.flat === true)
687
+ flatFaces(n, true, void 0, faces);
688
+ const receivers = new TriGrid(faces);
689
+ const groundY = level === lowest ? ground?.y : void 0;
690
+ const over = (qs) => {
691
+ let best;
692
+ for (const q of qs) {
693
+ counts.probes++;
694
+ let here2 = groundY;
695
+ for (const f of receivers.at(q)) {
696
+ counts.tests++;
697
+ if (here2 !== void 0 && f.y <= here2) continue;
698
+ if (contains(f, q)) here2 = f.y;
699
+ }
700
+ if (here2 !== void 0 && (best === void 0 || here2 > best)) best = here2;
701
+ }
702
+ return best === void 0 ? void 0 : best + lift;
703
+ };
704
+ const foot = new Buf();
705
+ const corner = new Buf();
706
+ const halo = new Buf();
707
+ const band = (s, oa, ob, y, c) => {
708
+ const a = foot.vertex(s.a[0], y, s.a[1], 1, c);
709
+ const b = foot.vertex(s.b[0], y, s.b[1], 1, c);
710
+ const d = foot.vertex(s.b[0] + ob[0], y, s.b[1] + ob[1], 0, c);
711
+ const e = foot.vertex(s.a[0] + oa[0], y, s.a[1] + oa[1], 0, c);
712
+ foot.quad(a, b, d, e);
713
+ };
714
+ const probesOf = (s, oa, ob) => {
715
+ const f = frame(s);
716
+ const mid = [whole((s.a[0] + s.b[0]) / 2), whole((s.a[1] + s.b[1]) / 2)];
717
+ const half = (o) => [whole(o[0] / 2), whole(o[1] / 2)];
718
+ const om = [whole((oa[0] + ob[0]) / 4), whole((oa[1] + ob[1]) / 4)];
719
+ const out = [plus(mid, om)];
720
+ if (f.len > 2e3) {
721
+ out.push(plus(plus(s.a, scaled(f.u, 1)), half(oa)), plus(plus(s.b, scaled(f.u, -1)), half(ob)));
722
+ }
723
+ return out;
724
+ };
725
+ const walls = here.filter(([n]) => n.band !== void 0 && (n.kind === "wall" || n.kind === "column"));
726
+ if (walls.length > 0) {
727
+ const caster = casters.push(walls.map(([n]) => n.key)) - 1;
728
+ let e = Infinity;
729
+ for (const [n] of walls) e = Math.min(e, yRange(n)[0]);
730
+ const tris = [];
731
+ for (const [n, i] of walls) downFaces(n, i, e, tris);
732
+ const solid = new TriGrid(tris.map((t) => ({ a: t.p[0], b: t.p[1], c: t.p[2], y: e, node: t.node })));
733
+ const inSolid = (q) => {
734
+ counts.probes++;
735
+ for (const f of solid.at(q)) {
736
+ counts.tests++;
737
+ if (contains(f, q)) return true;
738
+ }
739
+ return false;
740
+ };
741
+ const segs = outline(tris).flatMap((s) => {
742
+ const f = frame(s);
743
+ const ts = [0, 1];
744
+ const [bx0, bx1] = [Math.min(s.a[0], s.b[0]), Math.max(s.a[0], s.b[0])];
745
+ const [bz0, bz1] = [Math.min(s.a[1], s.b[1]), Math.max(s.a[1], s.b[1])];
746
+ for (const t of solid.near(bx0, bz0, bx1, bz1)) {
747
+ if (t.node === s.node) continue;
748
+ counts.tests++;
749
+ for (const [p, q] of [
750
+ [t.a, t.b],
751
+ [t.b, t.c],
752
+ [t.c, t.a]
753
+ ]) {
754
+ const den = f.d[0] * (q[1] - p[1]) - f.d[1] * (q[0] - p[0]);
755
+ if (den === 0) continue;
756
+ const along = ((p[0] - s.a[0]) * (q[1] - p[1]) - (p[1] - s.a[1]) * (q[0] - p[0])) / den;
757
+ const on = ((p[0] - s.a[0]) * f.d[1] - (p[1] - s.a[1]) * f.d[0]) / den;
758
+ if (along > 0 && along < 1 && on >= 0 && on <= 1) ts.push(along);
759
+ }
760
+ }
761
+ ts.sort((x, y) => x - y);
762
+ const at = (t) => t === 0 ? s.a : t === 1 ? s.b : [whole(s.a[0] + f.d[0] * t), whole(s.a[1] + f.d[1] * t)];
763
+ const kept = [];
764
+ let from;
765
+ for (let k = 0; k + 1 < ts.length; k++) {
766
+ const t0 = ts[k];
767
+ const t1 = ts[k + 1];
768
+ if (t1 - t0 <= 0) continue;
769
+ const m = at((t0 + t1) / 2);
770
+ const outside = !inSolid(plus(m, scaled(f.n, 1)));
771
+ if (outside && from === void 0) from = t0;
772
+ if (!outside) {
773
+ counts.dropped++;
774
+ if (from !== void 0) kept.push({ a: at(from), b: at(t0), node: s.node });
775
+ from = void 0;
776
+ }
777
+ }
778
+ if (from !== void 0) kept.push({ a: at(from), b: s.b, node: s.node });
779
+ return kept.filter((k) => k.a[0] !== k.b[0] || k.a[1] !== k.b[1]);
780
+ });
781
+ const bandOf = (s) => nodes[s.node]?.band ?? 0;
782
+ const pk = (p) => `${p[0]},${p[1]}`;
783
+ const ending = /* @__PURE__ */ new Map();
784
+ const starting = /* @__PURE__ */ new Map();
785
+ for (const s of segs) {
786
+ ending.set(pk(s.b), [...ending.get(pk(s.b)) ?? [], s]);
787
+ starting.set(pk(s.a), [...starting.get(pk(s.a)) ?? [], s]);
788
+ }
789
+ const outerA = /* @__PURE__ */ new Map();
790
+ const outerB = /* @__PURE__ */ new Map();
791
+ for (const s of segs) {
792
+ const o = scaled(frame(s).n, bandOf(s));
793
+ outerA.set(s, o);
794
+ outerB.set(s, o);
795
+ }
796
+ const joints = [];
797
+ for (const key of [...ending.keys()].sort()) {
798
+ const ins = ending.get(key) ?? [];
799
+ const outs = starting.get(key) ?? [];
800
+ if (ins.length !== 1 || outs.length !== 1) continue;
801
+ const si = ins[0];
802
+ const so = outs[0];
803
+ const fi = frame(si);
804
+ const fo = frame(so);
805
+ const turn = cross(fi.d[0], fi.d[1], fo.d[0], fo.d[1]);
806
+ if (nearlyFlat(fi.d, fo.d, 1, 1)) {
807
+ const m = [fi.n[0] + fo.n[0], fi.n[1] + fo.n[1]];
808
+ const k = 1 + fi.n[0] * fo.n[0] + fi.n[1] * fo.n[1];
809
+ outerB.set(si, scaled([m[0] / k, m[1] / k], bandOf(si)));
810
+ outerA.set(so, scaled([m[0] / k, m[1] / k], bandOf(so)));
811
+ if (turn !== 0) counts.flatJoints++;
812
+ continue;
813
+ }
814
+ if (turn !== 0) joints.push({ v: si.b, si, so, turn });
815
+ }
816
+ for (const s of segs) {
817
+ const oa = outerA.get(s);
818
+ const ob = outerB.get(s);
819
+ const y = over(probesOf(s, oa, ob));
820
+ if (y === void 0) continue;
821
+ band(s, oa, ob, y, caster);
822
+ counts.edges++;
823
+ }
824
+ const jambs = [];
825
+ for (const [, i] of walls) {
826
+ const l = lines?.[i] ?? [];
827
+ for (let j = 0; j + 5 < l.length; j += 6)
828
+ if (l[j] === l[j + 3] && l[j + 2] === l[j + 5] && l[j + 1] !== l[j + 4])
829
+ jambs.push([l[j], l[j + 2]]);
830
+ }
831
+ for (const { v, si, so, turn } of joints) {
832
+ const fi = frame(si);
833
+ const fo = frame(so);
834
+ const bi = bandOf(si);
835
+ const bo = bandOf(so);
836
+ if (turn > 0) {
837
+ const dirs = [];
838
+ for (let k = 0; k < 16; k++) {
839
+ const d = [C16[k], S16[k]];
840
+ if (cross(fi.n[0], fi.n[1], d[0], d[1]) > 0 && cross(d[0], d[1], fo.n[0], fo.n[1]) > 0) dirs.push(d);
841
+ }
842
+ dirs.sort((a, b) => -Math.sign(cross(a[0], a[1], b[0], b[1])));
843
+ const ring = [fi.n, ...dirs.slice(0, MAX_FAN - 1), fo.n];
844
+ const rims = ring.map((d, j) => scaled(d, bi + (bo - bi) * j / (ring.length - 1)));
845
+ const y = over(rims.map((o) => plus(v, [whole(o[0] / 2), whole(o[1] / 2)])));
846
+ if (y === void 0) continue;
847
+ const c = foot.vertex(v[0], y, v[1], 1, caster);
848
+ const rim = rims.map((o) => foot.vertex(v[0] + o[0], y, v[1] + o[1], 0, caster));
849
+ for (let j = 0; j + 1 < rim.length; j++) foot.tri(c, rim[j], rim[j + 1]);
850
+ counts.convex++;
851
+ } else {
852
+ const r = Math.max(bi, bo);
853
+ const y0 = over([plus(v, scaled([fi.n[0] + fo.n[0], fi.n[1] + fo.n[1]], r / 2))]);
854
+ if (y0 === void 0) continue;
855
+ const top = Math.min(yRange(nodes[si.node])[1], yRange(nodes[so.node])[1]);
856
+ if (top <= y0) continue;
857
+ const strip = (f, mm, back) => {
858
+ const dir = back ? [-f.d[0], -f.d[1]] : f.d;
859
+ let reach = Math.min(mm * 1e3, f.len);
860
+ for (const p of jambs) {
861
+ const wx = p[0] - v[0];
862
+ const wz = p[1] - v[1];
863
+ if (cross(dir[0], dir[1], wx, wz) !== 0) continue;
864
+ const along = (dir[0] * wx + dir[1] * wz) / f.len;
865
+ if (along > 0 && along < reach) reach = along;
866
+ }
867
+ if (reach <= 0) return;
868
+ const off = scaled(f.n, CONTACT_SHADOW.liftMm);
869
+ const step = scaled(back ? [-f.u[0], -f.u[1]] : f.u, reach / 1e3);
870
+ const x = v[0] + off[0];
871
+ const z = v[1] + off[1];
872
+ const a = corner.vertex(x, y0, z, 1, caster);
873
+ const b = corner.vertex(x, top, z, 1, caster);
874
+ const c = corner.vertex(x + step[0], top, z + step[1], 0, caster);
875
+ const d = corner.vertex(x + step[0], y0, z + step[1], 0, caster);
876
+ corner.quad(a, b, c, d);
877
+ };
878
+ strip(fi, bi, true);
879
+ strip(fo, bo, false);
880
+ counts.concave++;
881
+ }
882
+ }
883
+ }
884
+ for (const [n, i] of here) {
885
+ if (n.kind !== "fence" || n.band === void 0) continue;
886
+ const caster = casters.push([n.key]) - 1;
887
+ const ys = [...new Set(n.positions.filter((_, j) => j % 3 === 1))].sort((a, b) => a - b);
888
+ const seen = /* @__PURE__ */ new Set();
889
+ for (const y of ys) {
890
+ const tris = [];
891
+ downFaces(n, i, y, tris);
892
+ for (const s of outline(tris)) {
893
+ if (frame(s).len <= 2 * n.band * 1e3) continue;
894
+ const key = `${s.a[0]},${s.a[1]},${s.b[0]},${s.b[1]}`;
895
+ if (seen.has(key)) continue;
896
+ seen.add(key);
897
+ const o = scaled(frame(s).n, n.band);
898
+ const yy = over(probesOf(s, o, o));
899
+ if (yy === void 0) continue;
900
+ band(s, o, o, yy, caster);
901
+ counts.fenceSides++;
902
+ }
903
+ }
904
+ }
905
+ for (const [n, i] of here) {
906
+ if (n.kind !== "stair" || n.band === void 0) continue;
907
+ const caster = casters.push([n.key]) - 1;
908
+ const tris = [];
909
+ downFaces(n, i, void 0, tris);
910
+ for (const s of outline(tris)) {
911
+ const o = scaled(frame(s).n, n.band);
912
+ const y = over(probesOf(s, o, o));
913
+ if (y === void 0) continue;
914
+ band(s, o, o, y, caster);
915
+ counts.stairEdges++;
916
+ }
917
+ }
918
+ for (const [n] of here) {
919
+ if (n.kind !== "furniture" || n.band === void 0 || n.positions.length === 0) continue;
920
+ let x0 = Infinity;
921
+ let x1 = -Infinity;
922
+ let z0 = Infinity;
923
+ let z1 = -Infinity;
924
+ for (let j = 0; j < n.positions.length; j += 3) {
925
+ const x = n.positions[j];
926
+ const z = n.positions[j + 2];
927
+ if (x < x0) x0 = x;
928
+ if (x > x1) x1 = x;
929
+ if (z < z0) z0 = z;
930
+ if (z > z1) z1 = z;
931
+ }
932
+ const h = n.band * 1e3;
933
+ const cx = whole((x0 + x1) / 2);
934
+ const cz = whole((z0 + z1) / 2);
935
+ const y = over([
936
+ [cx, cz],
937
+ [cx, z0 - h / 2],
938
+ [x1 + h / 2, cz],
939
+ [cx, z1 + h / 2],
940
+ [x0 - h / 2, cz]
941
+ ]);
942
+ if (y === void 0) continue;
943
+ const caster = casters.push([n.key]) - 1;
944
+ const inner = [
945
+ [x0, z0],
946
+ [x1, z0],
947
+ [x1, z1],
948
+ [x0, z1]
949
+ ];
950
+ const outer = [
951
+ [x0 - h, z0 - h],
952
+ [x1 + h, z0 - h],
953
+ [x1 + h, z1 + h],
954
+ [x0 - h, z1 + h]
955
+ ];
956
+ const iv = inner.map((p) => halo.vertex(p[0], y, p[1], 1, caster));
957
+ const ov = outer.map((p) => halo.vertex(p[0], y, p[1], 0, caster));
958
+ for (let j = 0; j < 4; j++) {
959
+ const k = (j + 1) % 4;
960
+ halo.quad(iv[j], iv[k], ov[k], ov[j]);
961
+ }
962
+ counts.furniture++;
963
+ }
964
+ for (const [cls, buf] of [
965
+ ["foot", foot],
966
+ ["corner", corner],
967
+ ["halo", halo]
968
+ ]) {
969
+ if (buf.indices.length === 0) continue;
970
+ counts.triangles += buf.indices.length / 3;
971
+ layers.push({ level, cls, ...buf });
972
+ }
973
+ }
974
+ return { layers, casters, counts };
975
+ }
976
+
977
+ export {
978
+ wallOutline,
979
+ joinedGroups,
980
+ faceParts,
981
+ contactFade,
982
+ contactShadows
983
+ };