@nebula-spatial/cad-loader 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.
Files changed (59) hide show
  1. package/README.md +46 -0
  2. package/dist/core/cad-types.d.ts +26 -0
  3. package/dist/core/errors.d.ts +5 -0
  4. package/dist/core/index.d.ts +24 -0
  5. package/dist/core/index.js +2262 -0
  6. package/dist/core/parser/bin-group-reader.d.ts +9 -0
  7. package/dist/core/parser/bin-parser.d.ts +65 -0
  8. package/dist/core/parser/dxbs-sidecar.d.ts +24 -0
  9. package/dist/core/parser/geometry-segments.d.ts +20 -0
  10. package/dist/core/parser/text-roundtrip.d.ts +4 -0
  11. package/dist/core/parser/types.d.ts +80 -0
  12. package/dist/core/runtime-text-decoder.d.ts +8 -0
  13. package/dist/core/types.d.ts +147 -0
  14. package/dist/core/utils/insert-bbox.d.ts +10 -0
  15. package/dist/core/utils/viewport-rect.d.ts +21 -0
  16. package/dist/index.d.ts +26 -0
  17. package/dist/index.js +4435 -0
  18. package/dist/loader/geo-loader.d.ts +56 -0
  19. package/dist/loader/internal/external-variant-sidecar-fetch.d.ts +13 -0
  20. package/dist/loader/internal/flow-control.d.ts +29 -0
  21. package/dist/loader/internal/frame-budget-scheduler.d.ts +21 -0
  22. package/dist/loader/internal/geo-compact-executor.d.ts +19 -0
  23. package/dist/loader/internal/geo-loader-local-adapter.d.ts +2 -0
  24. package/dist/loader/internal/geo-loader-pipeline.d.ts +81 -0
  25. package/dist/loader/internal/geo-loader-worker-adapter.d.ts +7 -0
  26. package/dist/loader/internal/geo-node-builder.d.ts +29 -0
  27. package/dist/loader/internal/geo-node-load-sink.d.ts +28 -0
  28. package/dist/loader/internal/geo-node.d.ts +284 -0
  29. package/dist/loader/internal/geo-prepare-worker-types.d.ts +87 -0
  30. package/dist/loader/internal/geo-prepare.d.ts +38 -0
  31. package/dist/loader/internal/geo-prepare.worker.d.ts +1 -0
  32. package/dist/loader/internal/insert-model-builder.d.ts +10 -0
  33. package/dist/loader/internal/insert-runtime-geometry.d.ts +15 -0
  34. package/dist/loader/internal/line-canonical.d.ts +37 -0
  35. package/dist/loader/internal/line-quality.d.ts +9 -0
  36. package/dist/loader/internal/prepare-credit-gate.d.ts +20 -0
  37. package/dist/loader/internal/transport.d.ts +2 -0
  38. package/dist/scene/geo-cad-line-geometry.d.ts +28 -0
  39. package/dist/scene/geo-cad-line-material.d.ts +18 -0
  40. package/dist/scene/instancing/block-geometry-registry.d.ts +35 -0
  41. package/dist/scene/instancing/insert-instance-table.d.ts +23 -0
  42. package/dist/scene/instancing/viewport-instance-presenter.d.ts +66 -0
  43. package/dist/selection/cad-insert-selection.d.ts +35 -0
  44. package/dist/session/load-session.d.ts +90 -0
  45. package/dist/shared/abort.d.ts +3 -0
  46. package/dist/shared/cad-display-color.d.ts +55 -0
  47. package/dist/shared/cad-load-source.d.ts +8 -0
  48. package/dist/shared/color-buffer.d.ts +6 -0
  49. package/dist/shared/constants.d.ts +11 -0
  50. package/dist/shared/display-color-buffer.d.ts +27 -0
  51. package/dist/shared/errors.d.ts +2 -0
  52. package/dist/shared/text-font.d.ts +12 -0
  53. package/dist/shared/types.d.ts +57 -0
  54. package/dist/shared/utils/insert-bbox.d.ts +1 -0
  55. package/dist/shared/utils/timing.d.ts +2 -0
  56. package/dist/shared/utils/viewport-rect.d.ts +1 -0
  57. package/dist/shared/viewer-theme.d.ts +2 -0
  58. package/dist/workers/geo-prepare.worker.js +2166 -0
  59. package/package.json +61 -0
@@ -0,0 +1,2262 @@
1
+ var ae = Object.defineProperty;
2
+ var ie = (t, n, e) => n in t ? ae(t, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[n] = e;
3
+ var mn = (t, n, e) => ie(t, typeof n != "symbol" ? n + "" : n, e);
4
+ class l extends Error {
5
+ constructor(e, o) {
6
+ super(o);
7
+ mn(this, "code");
8
+ this.name = "GeoLoaderError", this.code = e;
9
+ }
10
+ }
11
+ const Lt = 4096;
12
+ function To(t, n, e, o, a = "u8") {
13
+ return {
14
+ count: o,
15
+ raw: new Uint8Array(t, n, e - n).slice(),
16
+ groupLayerEncoding: a
17
+ };
18
+ }
19
+ function Wt(t) {
20
+ return t >= 6 ? "u16_lw" : t >= 5 ? "u8_lw" : t >= 4 ? "u16" : "u8";
21
+ }
22
+ function In(t) {
23
+ return t === "u16_lw" ? 13 : t === "u8_lw" ? 12 : t === "u16" ? 9 : 8;
24
+ }
25
+ function Gn(t, n, e) {
26
+ return e === "u16" || e === "u16_lw" ? t.getUint16(n + 7, !0) : t.getUint8(n + 7);
27
+ }
28
+ function se(t) {
29
+ return t === "u16" || t === "u16_lw" ? 2 : 1;
30
+ }
31
+ function Vn(t, n, e) {
32
+ if (!(e !== "u8_lw" && e !== "u16_lw"))
33
+ return t.getFloat32(n + 7 + se(e), !0);
34
+ }
35
+ function lt(t, n, e, o, a = t.byteLength, r = "u8") {
36
+ if (e > a || a > t.byteLength)
37
+ throw new l("E_GROUP_RANGE", "Group byte range exceeds payload length");
38
+ let i = e;
39
+ const c = o * 2, s = c * 4;
40
+ if (i + s > a)
41
+ throw new l("E_GROUP_RANGE", "Group position bytes exceed payload length");
42
+ let u;
43
+ if (c > 0) {
44
+ const w = new Uint8Array(t, i, s), y = new Uint8Array(s), m = c;
45
+ for (let x = 0; x < c; x++)
46
+ y[x * 4] = w[x], y[x * 4 + 1] = w[m + x], y[x * 4 + 2] = w[m * 2 + x], y[x * 4 + 3] = w[m * 3 + x];
47
+ u = new Float32Array(y.buffer);
48
+ } else
49
+ u = new Float32Array(0);
50
+ if (i += s, i + 4 > a)
51
+ throw new l("E_GROUP_RANGE", "Missing seg_count field for group");
52
+ const h = n.getUint32(i, !0);
53
+ i += 4;
54
+ const d = [];
55
+ let _ = 0;
56
+ const f = In(r);
57
+ for (let w = 0; w < h; w++) {
58
+ if (i + f > a)
59
+ throw new l("E_GROUP_RANGE", "Segment record exceeds payload length");
60
+ const y = n.getUint32(i, !0), m = n.getUint8(i + 4), x = n.getUint8(i + 5), G = n.getUint8(i + 6), N = Gn(n, i, r), R = Vn(n, i, r);
61
+ if (_ + y > o)
62
+ throw new l("E_SEGMENT_SUM", "Segment vertex_count sum exceeds group count");
63
+ d.push({
64
+ startVertex: _,
65
+ vertexCount: y,
66
+ color: [m, x, G],
67
+ layerIndex: N,
68
+ ...R === void 0 ? {} : { lineweightPx: R }
69
+ }), i += f;
70
+ const P = _ * 2;
71
+ if (y > 1) {
72
+ let B = u[P], D = u[P + 1];
73
+ for (let C = 1; C < y; C++) {
74
+ const $ = P + C * 2;
75
+ B += u[$], D += u[$ + 1], u[$] = B, u[$ + 1] = D;
76
+ }
77
+ }
78
+ _ += y;
79
+ }
80
+ if (_ !== o)
81
+ throw new l("E_SEGMENT_SUM", `Segment vertex_count sum ${_} does not match group count ${o}`);
82
+ return {
83
+ chunk: { pos: u, count: o, segments: d },
84
+ offset: i
85
+ };
86
+ }
87
+ function ce(t, n, e, o, a = "u8") {
88
+ const r = o * 2, i = r * 4;
89
+ if (e + i > t.byteLength)
90
+ throw new l("E_GROUP_RANGE", "Group position bytes exceed payload length");
91
+ const c = new Uint8Array(t, e, i);
92
+ let s = e + i;
93
+ if (s + 4 > t.byteLength)
94
+ throw new l("E_GROUP_RANGE", "Missing seg_count field for group");
95
+ const u = n.getUint32(s, !0);
96
+ s += 4;
97
+ const h = [];
98
+ let d = 0;
99
+ const _ = In(a);
100
+ for (let f = 0; f < u; f++) {
101
+ if (s + _ > t.byteLength)
102
+ throw new l("E_GROUP_RANGE", "Segment record exceeds payload length");
103
+ const w = n.getUint32(s, !0), y = n.getUint8(s + 4), m = n.getUint8(s + 5), x = n.getUint8(s + 6), G = Gn(n, s, a), N = Vn(n, s, a);
104
+ if (s += _, d + w > o)
105
+ throw new l("E_SEGMENT_SUM", "Segment vertex_count sum exceeds group count");
106
+ h.push({
107
+ startVertex: d,
108
+ vertexCount: w,
109
+ color: [y, m, x],
110
+ layerIndex: G,
111
+ ...N === void 0 ? {} : { lineweightPx: N }
112
+ }), d += w;
113
+ }
114
+ if (d !== o)
115
+ throw new l("E_SEGMENT_SUM", `Segment vertex_count sum ${d} does not match group count ${o}`);
116
+ return {
117
+ shuffled: c,
118
+ plane: r,
119
+ segments: h,
120
+ offset: s
121
+ };
122
+ }
123
+ function le(t, n, e, o) {
124
+ if (o === 0)
125
+ return new Float32Array(0);
126
+ const a = new Uint8Array(o * 4);
127
+ for (let r = 0; r < o; r++)
128
+ a[r * 4] = t[e + r], a[r * 4 + 1] = t[n + e + r], a[r * 4 + 2] = t[n * 2 + e + r], a[r * 4 + 3] = t[n * 3 + e + r];
129
+ return new Float32Array(a.buffer);
130
+ }
131
+ function ue(t, n, e, o, a) {
132
+ const r = le(
133
+ t.shuffled,
134
+ t.plane,
135
+ (n.startVertex + e) * 2,
136
+ o * 2
137
+ );
138
+ let i = (a == null ? void 0 : a[0]) ?? 0, c = (a == null ? void 0 : a[1]) ?? 0;
139
+ if (o > 0) {
140
+ e === 0 ? (i = r[0], c = r[1]) : (i += r[0], c += r[1], r[0] = i, r[1] = c);
141
+ for (let u = 1; u < o; u++) {
142
+ const h = u * 2;
143
+ i += r[h], c += r[h + 1], r[h] = i, r[h + 1] = c;
144
+ }
145
+ }
146
+ const s = o > 0 ? [{
147
+ startVertex: 0,
148
+ vertexCount: o,
149
+ color: [n.color[0], n.color[1], n.color[2]],
150
+ layerIndex: n.layerIndex,
151
+ ...n.lineweightPx === void 0 ? {} : { lineweightPx: n.lineweightPx }
152
+ }] : [];
153
+ return {
154
+ chunk: { pos: r, count: o, segments: s },
155
+ lastPoint: o > 0 ? [i, c] : a
156
+ };
157
+ }
158
+ function* de(t, n, e, o, a = "u8") {
159
+ const r = ce(t, n, e, o, a);
160
+ if (o === 0)
161
+ return yield { pos: new Float32Array(0), count: 0, segments: [] }, r.offset;
162
+ for (const i of r.segments) {
163
+ let c = null;
164
+ for (let s = 0; s < i.vertexCount; s += Lt) {
165
+ const u = Math.min(Lt, i.vertexCount - s), h = ue(r, i, s, u, c);
166
+ c = h.lastPoint, yield h.chunk;
167
+ }
168
+ }
169
+ return r.offset;
170
+ }
171
+ function Tn() {
172
+ const t = globalThis.TextDecoder;
173
+ if (!t)
174
+ throw new l("E_HEADER_JSON", "TextDecoder is required to parse CAD pack metadata");
175
+ return new t();
176
+ }
177
+ function Yt(t) {
178
+ return t ? {
179
+ minX: t.minX,
180
+ minY: t.minY,
181
+ maxX: t.maxX,
182
+ maxY: t.maxY
183
+ } : null;
184
+ }
185
+ function Ft(t) {
186
+ return !!t && Number.isFinite(t.minX) && Number.isFinite(t.minY) && Number.isFinite(t.maxX) && Number.isFinite(t.maxY);
187
+ }
188
+ function fe(t) {
189
+ return Ft(t) ? Yt(t) : null;
190
+ }
191
+ function me(t, n) {
192
+ const e = Ft(t) ? t : null, o = Ft(n) ? n : null;
193
+ return e ? o ? {
194
+ minX: Math.min(e.minX, o.minX),
195
+ minY: Math.min(e.minY, o.minY),
196
+ maxX: Math.max(e.maxX, o.maxX),
197
+ maxY: Math.max(e.maxY, o.maxY)
198
+ } : Yt(e) : Yt(o);
199
+ }
200
+ function bn(t, n) {
201
+ return !(n.maxX < t.minX || n.minX > t.maxX || n.maxY < t.minY || n.minY > t.maxY);
202
+ }
203
+ function bo(t, n) {
204
+ return t.minX <= n.minX && t.minY <= n.minY && t.maxX >= n.maxX && t.maxY >= n.maxY;
205
+ }
206
+ function Bo(t, n) {
207
+ return t === n ? !0 : !t || !n ? !1 : t.minX === n.minX && t.minY === n.minY && t.maxX === n.maxX && t.maxY === n.maxY;
208
+ }
209
+ function _e(t, n) {
210
+ return bn(t, n) ? {
211
+ minX: Math.max(t.minX, n.minX),
212
+ minY: Math.max(t.minY, n.minY),
213
+ maxX: Math.min(t.maxX, n.maxX),
214
+ maxY: Math.min(t.maxY, n.maxY)
215
+ } : null;
216
+ }
217
+ function he(t, n) {
218
+ const e = Number.isFinite(n) ? Math.max(0, n) : 0, o = Math.max(0, t.maxX - t.minX), a = Math.max(0, t.maxY - t.minY), r = o * e, i = a * e;
219
+ return {
220
+ minX: t.minX - r,
221
+ minY: t.minY - i,
222
+ maxX: t.maxX + r,
223
+ maxY: t.maxY + i
224
+ };
225
+ }
226
+ function Ee(t, n, e, o = 0, a = 1) {
227
+ if (n <= 0 || e <= 0) return null;
228
+ let r = Number.POSITIVE_INFINITY, i = Number.POSITIVE_INFINITY, c = Number.NEGATIVE_INFINITY, s = Number.NEGATIVE_INFINITY, u = !1;
229
+ for (let h = 0; h < n; h += 1) {
230
+ const d = h * e, _ = t[d + o], f = t[d + a];
231
+ !Number.isFinite(_) || !Number.isFinite(f) || (_ < r && (r = _), f < i && (i = f), _ > c && (c = _), f > s && (s = f), u = !0);
232
+ }
233
+ return u ? { minX: r, minY: i, maxX: c, maxY: s } : null;
234
+ }
235
+ function Bn(t, n) {
236
+ const e = t.minX, o = t.minY, a = t.maxX, r = t.maxY, { matA: i, matB: c, matC: s, matD: u, tx: h, ty: d } = n;
237
+ let _ = i * e + s * o + h, f = c * e + u * o + d, w = _, y = f, m = _, x = f;
238
+ return _ = i * e + s * r + h, f = c * e + u * r + d, _ < w && (w = _), f < y && (y = f), _ > m && (m = _), f > x && (x = f), _ = i * a + s * o + h, f = c * a + u * o + d, _ < w && (w = _), f < y && (y = f), _ > m && (m = _), f > x && (x = f), _ = i * a + s * r + h, f = c * a + u * r + d, _ < w && (w = _), f < y && (y = f), _ > m && (m = _), f > x && (x = f), { minX: w, minY: y, maxX: m, maxY: x };
239
+ }
240
+ function xe(t) {
241
+ return Math.hypot(t.maxX - t.minX, t.maxY - t.minY);
242
+ }
243
+ function ge(t, n) {
244
+ const e = (t.minX + t.maxX) / 2, o = (t.minY + t.maxY) / 2, a = (n.minX + n.maxX) / 2, r = (n.minY + n.maxY) / 2;
245
+ return Math.hypot(e - a, o - r);
246
+ }
247
+ const ye = {};
248
+ function _n(t) {
249
+ return typeof t == "object" && t !== null;
250
+ }
251
+ function gt(t, n) {
252
+ return Object.prototype.hasOwnProperty.call(t, n);
253
+ }
254
+ function J(t) {
255
+ if (t.length === 0) return "$";
256
+ let n = "";
257
+ for (const e of t) {
258
+ if (typeof e == "number") {
259
+ n += `[${e}]`;
260
+ continue;
261
+ }
262
+ n += n.length === 0 ? e : `.${e}`;
263
+ }
264
+ return n;
265
+ }
266
+ function hn(t, n, e, o) {
267
+ return new l("E_TEXT_ROUNDTRIP", `Text roundtrip mismatch at stage=${t} index=${n} path=${e}: ${o}`);
268
+ }
269
+ function kt(t, n, e) {
270
+ if (Object.is(t, n)) return null;
271
+ if (Array.isArray(t)) {
272
+ if (!Array.isArray(n))
273
+ return { path: J(e), reason: `expected array but found ${typeof n}` };
274
+ if (t.length !== n.length)
275
+ return { path: J([...e, "length"]), reason: `array length ${t.length} !== ${n.length}` };
276
+ for (let o = 0; o < t.length; o += 1) {
277
+ const a = kt(t[o], n[o], [...e, o]);
278
+ if (a) return a;
279
+ }
280
+ return null;
281
+ }
282
+ if (_n(t)) {
283
+ if (!_n(n) || Array.isArray(n))
284
+ return { path: J(e), reason: `expected object but found ${Array.isArray(n) ? "array" : typeof n}` };
285
+ const o = Object.keys(t), a = Object.keys(n);
286
+ if (o.length !== a.length) {
287
+ const r = o.find((c) => !gt(n, c));
288
+ if (r !== void 0)
289
+ return {
290
+ path: J([...e, r]),
291
+ reason: "missing key on actual"
292
+ };
293
+ const i = a.find((c) => !gt(t, c));
294
+ if (i !== void 0)
295
+ return {
296
+ path: J([...e, i]),
297
+ reason: "extra key on actual"
298
+ };
299
+ }
300
+ for (const r of o) {
301
+ if (!gt(n, r))
302
+ return { path: J([...e, r]), reason: "missing key on actual" };
303
+ const i = kt(t[r], n[r], [...e, r]);
304
+ if (i) return i;
305
+ }
306
+ for (const r of a)
307
+ if (!gt(t, r))
308
+ return { path: J([...e, r]), reason: "extra key on actual" };
309
+ return null;
310
+ }
311
+ return { path: J(e), reason: `expected ${String(t)} but found ${String(n)}` };
312
+ }
313
+ function we(t, n) {
314
+ return kt(t, n, []);
315
+ }
316
+ function En(t) {
317
+ if (t === !0 || t === "1" || t === "true") return !0;
318
+ if (t === !1 || t === "0" || t === "false") return !1;
319
+ }
320
+ function Ae() {
321
+ var a, r;
322
+ const t = globalThis.process, n = En(
323
+ ((a = t == null ? void 0 : t.env) == null ? void 0 : a.DXF_STRICT_TEXT_ROUNDTRIP) ?? ((r = t == null ? void 0 : t.env) == null ? void 0 : r.VITE_DXF_STRICT_TEXT_ROUNDTRIP)
324
+ );
325
+ if (n !== void 0) return n;
326
+ const e = ye, o = En(e == null ? void 0 : e.VITE_DXF_STRICT_TEXT_ROUNDTRIP);
327
+ return o !== void 0 ? o : !1;
328
+ }
329
+ function Uo(t) {
330
+ return t;
331
+ }
332
+ function Ne(t, n, e) {
333
+ if (n.length !== e.length)
334
+ throw hn(t, Math.min(n.length, e.length), "$length", `text count ${n.length} !== ${e.length}`);
335
+ for (let o = 0; o < n.length; o += 1) {
336
+ const a = we(n[o], e[o]);
337
+ if (a)
338
+ throw hn(t, o, a.path, a.reason);
339
+ }
340
+ }
341
+ const Re = Tn(), pe = ["left", "center", "right"], Ie = ["top", "middle", "bottom", "alphabetic"];
342
+ function xn(t, n) {
343
+ if (t > BigInt(Number.MAX_SAFE_INTEGER))
344
+ throw new l("E_GROUP_RANGE", `${n} exceeds safe integer range`);
345
+ return Number(t);
346
+ }
347
+ function zt(t, n, e, o) {
348
+ if (!Number.isInteger(t) || !Number.isInteger(n) || t < 0 || n < 0 || t + n > e)
349
+ throw new l("E_GROUP_RANGE", `${o} exceeds payload length`);
350
+ }
351
+ function Ge(t) {
352
+ if (t.byteLength < 4) return !1;
353
+ const n = new Uint8Array(t, 0, 4);
354
+ return n[0] === 68 && n[1] === 88 && n[2] === 66 && n[3] === 83;
355
+ }
356
+ function Jt(t) {
357
+ if (t.byteLength < 20)
358
+ throw new l("E_HEADER", "DXBS payload too small: missing container header");
359
+ if (!Ge(t))
360
+ throw new l("E_HEADER", "DXBS payload missing magic");
361
+ const n = new DataView(t), e = n.getUint16(4, !0), o = n.getUint16(6, !0), a = n.getUint16(8, !0), r = n.getUint16(10, !0), i = n.getUint16(12, !0), c = n.getUint16(14, !0), s = n.getUint32(16, !0);
362
+ if (e !== 3 || o < 3 || o > 6)
363
+ throw new l("E_GROUP_RANGE", `Unsupported DXBS version: ${e}.${o}`);
364
+ if (a !== 0)
365
+ throw new l("E_GROUP_RANGE", `Unsupported DXBS sidecar kind: ${a}`);
366
+ if (c !== 0)
367
+ throw new l("E_GROUP_RANGE", `Invalid DXBS reserved field: ${c}`);
368
+ if (r < 24)
369
+ throw new l("E_GROUP_RANGE", `Invalid DXBS directory entry size: ${r}`);
370
+ if (s !== r * i)
371
+ throw new l("E_GROUP_RANGE", "DXBS directory byte size mismatch");
372
+ const u = 20;
373
+ zt(u, s, t.byteLength, "DXBS section directory");
374
+ const h = /* @__PURE__ */ new Map();
375
+ for (let d = 0; d < i; d += 1) {
376
+ const _ = u + d * r, f = n.getUint16(_, !0), w = n.getUint16(_ + 2, !0), y = n.getUint32(_ + 4, !0), m = xn(n.getBigUint64(_ + 8, !0), "DXBS section offset"), x = xn(n.getBigUint64(_ + 16, !0), "DXBS section length");
377
+ if (w !== 0)
378
+ throw new l("E_GROUP_RANGE", `Unsupported DXBS section flags for type ${f}: ${w}`);
379
+ if (y !== 0)
380
+ throw new l("E_GROUP_RANGE", `Invalid DXBS section reserved field for type ${f}: ${y}`);
381
+ zt(m, x, t.byteLength, `DXBS section ${f}`), h.set(f, { type: f, flags: w, offset: m, length: x });
382
+ }
383
+ return { versionMajor: e, versionMinor: o, sections: h };
384
+ }
385
+ function et(t, n, e) {
386
+ const o = t.sections.get(n);
387
+ if (!o)
388
+ throw new l("E_GROUP_RANGE", `Missing DXBS section ${e}`);
389
+ return o;
390
+ }
391
+ function Ve(t, n, e, o) {
392
+ return zt(n, e, t.byteLength, o), Re.decode(t.subarray(n, n + e));
393
+ }
394
+ function Ut(t, n, e, o, a, r) {
395
+ if (e + 4 > o)
396
+ throw new l("E_GROUP_RANGE", `Missing ${a} vertex_count`);
397
+ const i = n.getUint32(e, !0), c = lt(t, n, e + 4, i, o, r);
398
+ if (c.offset > o)
399
+ throw new l("E_GROUP_RANGE", `${a} exceeds section bounds`);
400
+ return c;
401
+ }
402
+ function Un(t, n, e, o) {
403
+ const a = new DataView(t), r = e.offset + e.length, i = Wt(n.versionMinor), c = Ut(t, a, e.offset, r, `${o} line`, i), s = Ut(t, a, c.offset, r, `${o} fill`, i), u = Ut(t, a, s.offset, r, `${o} dot`, i);
404
+ if (u.offset !== r)
405
+ throw new l("E_GROUP_RANGE", `${o} has trailing bytes`);
406
+ return { line: c.chunk, fill: s.chunk, dot: u.chunk };
407
+ }
408
+ function Te(t, n, e) {
409
+ const o = t.getUint32(n, !0), a = t.getUint32(n + 4, !0), r = t.getUint8(n + 42), i = t.getUint8(n + 43), c = t.getFloat32(n + 24, !0), s = t.getFloat32(n + 28, !0);
410
+ return {
411
+ t: Ve(e, o, a, "DXBS text value"),
412
+ x: t.getFloat32(n + 8, !0),
413
+ y: t.getFloat32(n + 12, !0),
414
+ h: t.getFloat32(n + 16, !0),
415
+ r: t.getFloat32(n + 20, !0),
416
+ c: [t.getUint8(n + 36), t.getUint8(n + 37), t.getUint8(n + 38)],
417
+ li: t.getUint16(n + 40, !0),
418
+ ha: pe[r & 3] ?? "left",
419
+ va: Ie[r >> 2 & 3] ?? "alphabetic",
420
+ lh: t.getFloat32(n + 32, !0),
421
+ ...c ? { w: c } : {},
422
+ ...s !== 1 ? { wf: s } : {},
423
+ ...i & 1 ? { mx: !0 } : {},
424
+ ...i & 2 ? { my: !0 } : {}
425
+ };
426
+ }
427
+ function Sn(t, n, e, o) {
428
+ if (n.length % 44 !== 0)
429
+ throw new l("E_GROUP_RANGE", `${o} length must be a multiple of 44`);
430
+ const a = new Uint8Array(t, e.offset, e.length), r = [], i = new DataView(t);
431
+ for (let c = n.offset; c < n.offset + n.length; c += 44)
432
+ r.push(Te(i, c, a));
433
+ return r;
434
+ }
435
+ function So(t) {
436
+ const n = Jt(t), e = et(n, 513, "SCENE_ANNOTATION_GEOMETRY"), o = et(n, 514, "SCENE_ANNOTATION_TEXT_TABLE"), a = et(n, 515, "SCENE_ANNOTATION_STRING_TABLE");
437
+ return {
438
+ ...Un(t, n, e, "SCENE_ANNOTATION_GEOMETRY"),
439
+ texts: Sn(t, o, a, "SCENE_ANNOTATION_TEXT_TABLE")
440
+ };
441
+ }
442
+ function be(t) {
443
+ const n = Jt(t), e = et(n, 258, "BLOCK_ANNOTATION_GEOMETRY"), o = et(n, 259, "BLOCK_ANNOTATION_TEXT_TABLE"), a = et(n, 260, "BLOCK_ANNOTATION_STRING_TABLE");
444
+ return {
445
+ ...Un(t, n, e, "BLOCK_ANNOTATION_GEOMETRY"),
446
+ texts: Sn(t, o, a, "BLOCK_ANNOTATION_TEXT_TABLE")
447
+ };
448
+ }
449
+ function It() {
450
+ return {
451
+ pos: new Float32Array(0),
452
+ count: 0,
453
+ segments: []
454
+ };
455
+ }
456
+ function ot(t) {
457
+ if (t.segments && t.segments.length > 0)
458
+ return Se(t.segments, t.count), t.segments;
459
+ if (t.count <= 0) return [];
460
+ if (!t.col || !t.lay)
461
+ throw new l("E_GROUP_RANGE", "Geometry chunk is missing segment or expanded color/layer metadata");
462
+ return Ue(t.col, t.lay, t.count);
463
+ }
464
+ function Be(t) {
465
+ if (t.col && t.lay)
466
+ return { col: t.col, lay: t.lay };
467
+ const n = ot(t), e = new Uint8Array(t.count * 3);
468
+ let o = 0;
469
+ for (const r of n)
470
+ r.layerIndex > o && (o = r.layerIndex);
471
+ const a = o > 255 ? new Uint16Array(t.count) : new Uint8Array(t.count);
472
+ for (const r of n) {
473
+ const i = r.startVertex + r.vertexCount;
474
+ for (let c = r.startVertex; c < i; c += 1) {
475
+ const s = c * 3;
476
+ e[s] = r.color[0], e[s + 1] = r.color[1], e[s + 2] = r.color[2], a[c] = r.layerIndex;
477
+ }
478
+ }
479
+ return { col: e, lay: a };
480
+ }
481
+ function Ht(t, n) {
482
+ return t.map((e) => ({
483
+ startVertex: e.startVertex + n,
484
+ vertexCount: e.vertexCount,
485
+ color: Gt(e),
486
+ layerIndex: e.layerIndex,
487
+ ...e.lineweightPx === void 0 ? {} : { lineweightPx: e.lineweightPx }
488
+ }));
489
+ }
490
+ function Cn(t, n) {
491
+ return t.map((e) => ({
492
+ startVertex: e.startVertex,
493
+ vertexCount: e.vertexCount,
494
+ color: Gt(e),
495
+ layerIndex: n(e.layerIndex),
496
+ ...e.lineweightPx === void 0 ? {} : { lineweightPx: e.lineweightPx }
497
+ }));
498
+ }
499
+ function Co(t, n, e) {
500
+ const o = n + e, a = [];
501
+ for (const r of t) {
502
+ const i = r.startVertex, c = r.startVertex + r.vertexCount, s = Math.max(n, i), u = Math.min(o, c);
503
+ s >= u || a.push({
504
+ startVertex: s - n,
505
+ vertexCount: u - s,
506
+ color: Gt(r),
507
+ layerIndex: r.layerIndex,
508
+ ...r.lineweightPx === void 0 ? {} : { lineweightPx: r.lineweightPx }
509
+ });
510
+ }
511
+ return dt(a);
512
+ }
513
+ function dt(t) {
514
+ const n = [];
515
+ for (const e of t) {
516
+ const o = n[n.length - 1];
517
+ if (o && o.startVertex + o.vertexCount === e.startVertex && o.layerIndex === e.layerIndex && o.color[0] === e.color[0] && o.color[1] === e.color[1] && o.color[2] === e.color[2] && o.lineweightPx === e.lineweightPx) {
518
+ n[n.length - 1] = {
519
+ ...o,
520
+ vertexCount: o.vertexCount + e.vertexCount
521
+ };
522
+ continue;
523
+ }
524
+ n.push({
525
+ startVertex: e.startVertex,
526
+ vertexCount: e.vertexCount,
527
+ color: Gt(e),
528
+ layerIndex: e.layerIndex,
529
+ ...e.lineweightPx === void 0 ? {} : { lineweightPx: e.lineweightPx }
530
+ });
531
+ }
532
+ return n;
533
+ }
534
+ function Ue(t, n, e) {
535
+ if (t.length < e * 3 || n.length < e)
536
+ throw new l("E_GROUP_RANGE", "Expanded color/layer metadata is shorter than geometry count");
537
+ const o = [];
538
+ let a = 0;
539
+ for (; a < e; ) {
540
+ const r = a * 3, i = t[r] ?? 0, c = t[r + 1] ?? 0, s = t[r + 2] ?? 0, u = n[a] ?? 0;
541
+ let h = a + 1;
542
+ for (; h < e; ) {
543
+ const d = h * 3;
544
+ if (n[h] !== u || t[d] !== i || t[d + 1] !== c || t[d + 2] !== s)
545
+ break;
546
+ h += 1;
547
+ }
548
+ o.push({
549
+ startVertex: a,
550
+ vertexCount: h - a,
551
+ color: [i, c, s],
552
+ layerIndex: u
553
+ }), a = h;
554
+ }
555
+ return o;
556
+ }
557
+ function Se(t, n) {
558
+ let e = 0;
559
+ for (const o of t) {
560
+ if (o.vertexCount < 0 || o.startVertex !== e)
561
+ throw new l("E_SEGMENT_SUM", "Geometry segments must be contiguous and non-negative");
562
+ e += o.vertexCount;
563
+ }
564
+ if (e !== n)
565
+ throw new l("E_SEGMENT_SUM", `Geometry segment vertex sum ${e} does not match count ${n}`);
566
+ }
567
+ function Gt(t) {
568
+ return [t.color[0], t.color[1], t.color[2]];
569
+ }
570
+ const Ce = 8e6, Oe = 0.25, $e = 5, Me = 4, Pe = 2048, Zt = Tn(), De = ["left", "center", "right"], ve = ["top", "middle", "bottom", "alphabetic"];
571
+ function At(t, n, e) {
572
+ for (const o of t)
573
+ e(o);
574
+ for (const o of n)
575
+ e(o);
576
+ }
577
+ const gn = /* @__PURE__ */ new WeakMap();
578
+ function ct(t) {
579
+ return typeof t == "object" && t !== null;
580
+ }
581
+ function Xe(t) {
582
+ return Array.isArray(t) ? "array" : t === null ? "null" : typeof t;
583
+ }
584
+ function Le(t) {
585
+ return !(!ct(t) || Array.isArray(t) || typeof t.t != "string" || typeof t.x != "number" || typeof t.y != "number" || typeof t.h != "number" || typeof t.r != "number" || !Array.isArray(t.c) || t.c.length !== 3 || !t.c.every((n) => typeof n == "number") || typeof t.li != "number" || t.ha !== "left" && t.ha !== "center" && t.ha !== "right" || t.va !== "top" && t.va !== "middle" && t.va !== "bottom" && t.va !== "alphabetic" || typeof t.lh != "number" || t.w !== void 0 && typeof t.w != "number" || t.wf !== void 0 && typeof t.wf != "number" || t.iv !== void 0 && typeof t.iv != "number" || t.io !== void 0 && typeof t.io != "number" || t.mx !== void 0 && t.mx !== !0 || t.my !== void 0 && t.my !== !0);
586
+ }
587
+ function Ye(t) {
588
+ return t.map((n, e) => ({
589
+ index: e,
590
+ name: n.name,
591
+ color: [...n.color],
592
+ lw_px: n.lw_px,
593
+ visible: n.visible ?? !0
594
+ }));
595
+ }
596
+ function Qt(t) {
597
+ const n = t.texts, e = Array.isArray(n) ? n : [], o = e.filter(Le), a = Ae();
598
+ if (a && n !== void 0 && !Array.isArray(n))
599
+ throw new l(
600
+ "E_TEXT_ROUNDTRIP",
601
+ `Text roundtrip mismatch at stage=Boundary-2:header->stream index=0 path=$: expected array but found ${Xe(n)}`
602
+ );
603
+ return a && Ne("Boundary-2:header->stream", e, o), {
604
+ bounds: [...t.bounds],
605
+ entityCount: Number(t.entity_count || 0),
606
+ lineCount: Number(t.line_count || 0),
607
+ fillCount: Number(t.fill_count || 0),
608
+ dotCount: Number(t.dot_count || 0),
609
+ textCount: o.length,
610
+ layers: Ye(t.layers ?? []),
611
+ texts: o
612
+ };
613
+ }
614
+ function On(t) {
615
+ const n = t == null ? void 0 : t.maxExpandedInstanceVertices, e = typeof n == "number" && Number.isFinite(n) && n >= 0 ? Math.floor(n) : Ce;
616
+ return {
617
+ hiddenInstanceIds: t != null && t.hiddenInstanceIds ? new Set(t.hiddenInstanceIds) : /* @__PURE__ */ new Set(),
618
+ keepTextsForHiddenInstances: (t == null ? void 0 : t.keepTextsForHiddenInstances) === !0,
619
+ loadBlocks: (t == null ? void 0 : t.loadBlocks) !== !1,
620
+ suppressExpandedInsertGeometry: (t == null ? void 0 : t.suppressExpandedInsertGeometry) === !0,
621
+ maxExpandedInstanceVertices: e,
622
+ expandedInstanceVertices: 0,
623
+ externalVariantBuffers: t == null ? void 0 : t.externalVariantBuffers,
624
+ requiredExternalVariantIds: t == null ? void 0 : t.requiredExternalVariantIds,
625
+ externalVariantDeltaOnly: (t == null ? void 0 : t.externalVariantDeltaOnly) === !0,
626
+ deferExternalVariantGeometry: (t == null ? void 0 : t.deferExternalVariantGeometry) === !0
627
+ };
628
+ }
629
+ function Fe(t) {
630
+ return t.loadBlocks && !t.suppressExpandedInsertGeometry;
631
+ }
632
+ function Nt(t, n) {
633
+ return Fe(n) && !n.hiddenInstanceIds.has(t);
634
+ }
635
+ function $n(t, n, e) {
636
+ if (e <= 0)
637
+ return;
638
+ const o = t.expandedInstanceVertices + e;
639
+ if (o > t.maxExpandedInstanceVertices)
640
+ throw new l(
641
+ "E_EXPANSION_BUDGET",
642
+ `CPU-expanded ${n} instance vertices exceed budget: ${o}/${t.maxExpandedInstanceVertices}. Disable block loading or raise maxExpandedInstanceVertices.`
643
+ );
644
+ t.expandedInstanceVertices = o;
645
+ }
646
+ function ke(t, n, e) {
647
+ return e.loadBlocks ? e.hiddenInstanceIds.size === 0 || e.keepTextsForHiddenInstances ? t : t.filter((o) => {
648
+ if (!Number.isInteger(o.iv) || !Number.isInteger(o.io))
649
+ return !0;
650
+ const a = n.get(`${o.iv}:${o.io}`);
651
+ return a === void 0 || !e.hiddenInstanceIds.has(a);
652
+ }) : t.filter((o) => !Number.isInteger(o.iv) || !Number.isInteger(o.io));
653
+ }
654
+ function ze(t, n) {
655
+ const e = /* @__PURE__ */ new Map();
656
+ return t.forEach((o, a) => {
657
+ if (!Number.isInteger(o.iv) || !Number.isInteger(o.io))
658
+ return;
659
+ const r = n.get(`${o.iv}:${o.io}`);
660
+ if (r === void 0)
661
+ return;
662
+ const i = e.get(r);
663
+ i ? i.push(a) : e.set(r, [a]);
664
+ }), e;
665
+ }
666
+ function He(t, n, e, o, a, r, i) {
667
+ const c = n.map((s, u) => ({
668
+ id: s.id,
669
+ blockName: Mn(s, u),
670
+ line: e[u].line,
671
+ fill: e[u].fill,
672
+ dot: e[u].dot,
673
+ texts: o.get(s.id) ?? []
674
+ }));
675
+ return {
676
+ layer0Index: t,
677
+ variants: c,
678
+ instances: a,
679
+ textIndexesByInstanceId: ze(r, i),
680
+ ownerInstanceIdByVariantOrdinal: i
681
+ };
682
+ }
683
+ function je(t) {
684
+ if (t.byteLength < 4)
685
+ throw new l("E_HEADER", "Payload too small: missing header length");
686
+ const n = new DataView(t), e = n.getUint32(0, !0), o = 4, a = o + e;
687
+ if (a > t.byteLength)
688
+ throw new l("E_HEADER", "Header length exceeds payload length");
689
+ let r;
690
+ try {
691
+ r = JSON.parse(Zt.decode(new Uint8Array(t, o, e)));
692
+ } catch (i) {
693
+ throw new l("E_HEADER_JSON", `Invalid JSON header: ${i.message}`);
694
+ }
695
+ return {
696
+ meta: r,
697
+ header: Qt(r),
698
+ dataOffset: a,
699
+ view: n
700
+ };
701
+ }
702
+ function Mn(t, n) {
703
+ var r, i, c;
704
+ const e = (r = t.name) == null ? void 0 : r.trim();
705
+ if (e)
706
+ return e;
707
+ const o = (i = t.block_name) == null ? void 0 : i.trim();
708
+ if (o)
709
+ return o;
710
+ const a = (c = t.blockName) == null ? void 0 : c.trim();
711
+ return a || `block_${n}`;
712
+ }
713
+ function qe(t) {
714
+ const n = Number(t.version ?? 1), e = String(t.encoding ?? "").toLowerCase();
715
+ if (n === 2) {
716
+ const o = e ? ` ${e}` : "";
717
+ throw new l(
718
+ "E_UNSUPPORTED_PACK_VERSION",
719
+ `当前文件是 V2${o} pack,前端不再兼容解析;请升级到最新版本后再打开。`
720
+ );
721
+ }
722
+ }
723
+ function tn(t) {
724
+ const n = je(t);
725
+ return qe(n.meta), n;
726
+ }
727
+ function rt(t) {
728
+ if (t.byteLength < 4)
729
+ return !1;
730
+ const n = new Uint8Array(t, 0, 4);
731
+ return n[0] === 68 && n[1] === 88 && n[2] === 66 && n[3] === 51;
732
+ }
733
+ function yn(t, n) {
734
+ if (t > BigInt(Number.MAX_SAFE_INTEGER))
735
+ throw new l("E_GROUP_RANGE", `${n} exceeds safe integer range`);
736
+ return Number(t);
737
+ }
738
+ function ft(t, n, e, o) {
739
+ if (!Number.isInteger(t) || !Number.isInteger(n) || t < 0 || n < 0 || t + n > e)
740
+ throw new l("E_GROUP_RANGE", `${o} exceeds payload length`);
741
+ }
742
+ function mt(t) {
743
+ if (t.byteLength < 20)
744
+ throw new l("E_HEADER", "V3 payload too small: missing container header");
745
+ const n = new DataView(t), e = n.getUint16(4, !0), o = n.getUint16(6, !0), a = n.getUint32(8, !0), r = n.getUint16(12, !0), i = n.getUint16(14, !0), c = n.getUint32(16, !0);
746
+ if (e !== 3 || o > 6)
747
+ throw new l("E_GROUP_RANGE", `Unsupported V3 version: ${e}.${o}`);
748
+ if (a !== 0)
749
+ throw new l("E_GROUP_RANGE", `Unsupported V3 file flags: ${a}`);
750
+ if (r < 24)
751
+ throw new l("E_GROUP_RANGE", `Invalid V3 directory entry size: ${r}`);
752
+ if (c !== r * i)
753
+ throw new l("E_GROUP_RANGE", "V3 directory byte size mismatch");
754
+ const s = 20;
755
+ ft(s, c, t.byteLength, "V3 section directory");
756
+ const u = /* @__PURE__ */ new Map();
757
+ for (let h = 0; h < i; h += 1) {
758
+ const d = s + h * r, _ = n.getUint16(d, !0), f = n.getUint16(d + 2, !0), w = n.getUint32(d + 4, !0), y = yn(n.getBigUint64(d + 8, !0), "V3 section offset"), m = yn(n.getBigUint64(d + 16, !0), "V3 section length");
759
+ if (f !== 0)
760
+ throw new l("E_GROUP_RANGE", `Unsupported V3 section flags for type ${_}: ${f}`);
761
+ if (w !== 0)
762
+ throw new l("E_GROUP_RANGE", `Invalid V3 section reserved field for type ${_}: ${w}`);
763
+ ft(y, m, t.byteLength, `V3 section ${_}`), u.set(_, { type: _, flags: f, offset: y, length: m });
764
+ }
765
+ return { versionMinor: o, sections: u };
766
+ }
767
+ function X(t, n, e) {
768
+ const o = t.get(n);
769
+ if (!o)
770
+ throw new l("E_GROUP_RANGE", `Missing V3 section ${e}`);
771
+ return o;
772
+ }
773
+ function ut(t, n) {
774
+ return t.get(n) ?? { type: n, flags: 0, offset: 0, length: 0 };
775
+ }
776
+ function _t(t, n, e) {
777
+ try {
778
+ return JSON.parse(Zt.decode(new Uint8Array(t, n.offset, n.length)));
779
+ } catch (o) {
780
+ throw new l("E_HEADER_JSON", `Invalid V3 ${e} JSON: ${o.message}`);
781
+ }
782
+ }
783
+ function nn(t) {
784
+ const n = String(t.encoding ?? "").toLowerCase();
785
+ return n === "nested_instanced_external" || n === "nested_instanced_external_annotations";
786
+ }
787
+ function en(t) {
788
+ return String(t.encoding ?? "").toLowerCase() === "nested_instanced_external_annotations";
789
+ }
790
+ function Ke(t, n, e) {
791
+ const o = t.features, a = o && typeof o == "object" && !Array.isArray(o) ? { ...o } : {};
792
+ return n && (a.has_external_variant_geometry = !0), e && (a.has_external_annotations = !0), Object.keys(a).length ? a : void 0;
793
+ }
794
+ function q(t, n) {
795
+ if (!Number.isInteger(t) || Number(t) < 0)
796
+ throw new l("E_HEADER", `${n} must be a non-negative integer`);
797
+ return Number(t);
798
+ }
799
+ function Pn(t, n) {
800
+ if (typeof t != "string" || !t.trim())
801
+ throw new l("E_HEADER", `${n} must be a non-empty string`);
802
+ return t;
803
+ }
804
+ function wn(t, n) {
805
+ if (t == null)
806
+ return null;
807
+ if (!Array.isArray(t) || t.length !== 4 || t.some((e) => typeof e != "number" || !Number.isFinite(e)))
808
+ throw new l("E_HEADER", `${n} must be null or [minX, minY, maxX, maxY]`);
809
+ return [t[0], t[1], t[2], t[3]];
810
+ }
811
+ function jt(t, n) {
812
+ if (!ct(t) || Array.isArray(t))
813
+ throw new l("E_HEADER", `${n} must be an object`);
814
+ return {
815
+ line_count: q(t.line_count, `${n}.line_count`),
816
+ fill_count: q(t.fill_count, `${n}.fill_count`),
817
+ dot_count: q(t.dot_count, `${n}.dot_count`),
818
+ bbox: wn(t.bbox, `${n}.bbox`),
819
+ own_bbox: wn(t.own_bbox, `${n}.own_bbox`)
820
+ };
821
+ }
822
+ function We(t) {
823
+ return t ? { minX: t[0], minY: t[1], maxX: t[2], maxY: t[3] } : null;
824
+ }
825
+ function Vt(t, n) {
826
+ const e = t.external_variants;
827
+ if (!Array.isArray(e))
828
+ throw new l("E_HEADER", "V3 external SCENE_META.external_variants must be an array");
829
+ if (e.length !== n)
830
+ throw new l(
831
+ "E_GROUP_RANGE",
832
+ `V3 external variant manifest count mismatch: manifest=${e.length}, table=${n}`
833
+ );
834
+ return e.map((o, a) => {
835
+ if (!ct(o) || Array.isArray(o))
836
+ throw new l("E_HEADER", `V3 external_variants[${a}] must be an object`);
837
+ const r = q(o.id, `V3 external_variants[${a}].id`);
838
+ if (r !== a)
839
+ throw new l("E_GROUP_RANGE", `V3 external_variants[${a}].id must equal ${a}, found ${r}`);
840
+ const i = Pn(o.href, `V3 external_variants[${a}].href`), c = q(o.raw_size, `V3 external_variants[${a}].raw_size`);
841
+ if ("normal" in o || "annotation" in o) {
842
+ const s = jt(o.annotation, `V3 external_variants[${a}].annotation`);
843
+ return {
844
+ id: r,
845
+ href: i,
846
+ raw_size: c,
847
+ normal: jt(o.normal, `V3 external_variants[${a}].normal`),
848
+ annotation: {
849
+ ...s,
850
+ text_count: q(
851
+ ct(o.annotation) && !Array.isArray(o.annotation) ? o.annotation.text_count : void 0,
852
+ `V3 external_variants[${a}].annotation.text_count`
853
+ )
854
+ }
855
+ };
856
+ }
857
+ return {
858
+ id: r,
859
+ href: i,
860
+ raw_size: c,
861
+ line_count: q(o.line_count, `V3 external_variants[${a}].line_count`),
862
+ fill_count: q(o.fill_count, `V3 external_variants[${a}].fill_count`),
863
+ dot_count: q(o.dot_count, `V3 external_variants[${a}].dot_count`)
864
+ };
865
+ });
866
+ }
867
+ function Dn(t) {
868
+ const n = t.external_annotations;
869
+ if (n == null) return null;
870
+ if (!ct(n) || Array.isArray(n))
871
+ throw new l("E_HEADER", "V3 external_annotations must be an object");
872
+ const e = n.scene;
873
+ if (e == null) return {};
874
+ if (!ct(e) || Array.isArray(e))
875
+ throw new l("E_HEADER", "V3 external_annotations.scene must be an object");
876
+ const o = jt(e, "V3 external_annotations.scene");
877
+ return {
878
+ scene: {
879
+ href: Pn(e.href, "V3 external_annotations.scene.href"),
880
+ raw_size: q(e.raw_size, "V3 external_annotations.scene.raw_size"),
881
+ ...o,
882
+ text_count: q(e.text_count, "V3 external_annotations.scene.text_count")
883
+ }
884
+ };
885
+ }
886
+ function vn(t) {
887
+ const { sections: n } = mt(t), e = X(n, 1, "SCENE_META");
888
+ return _t(t, e, "SCENE_META");
889
+ }
890
+ function Oo(t) {
891
+ if (!rt(t))
892
+ return [];
893
+ const { sections: n } = mt(t), e = X(n, 1, "SCENE_META"), o = X(n, 5, "VARIANT_TABLE"), a = _t(t, e, "SCENE_META");
894
+ if (!nn(a))
895
+ return [];
896
+ if (o.length % 60 !== 0)
897
+ throw new l("E_GROUP_RANGE", "V3 VARIANT_TABLE length must be a multiple of 60");
898
+ return Vt(a, o.length / 60);
899
+ }
900
+ function An() {
901
+ return {
902
+ manifest: [],
903
+ requiredVariantIds: [],
904
+ requiredNormalBytes: 0,
905
+ totalManifestBytes: 0,
906
+ skippedVariantIds: []
907
+ };
908
+ }
909
+ function Je(t, n) {
910
+ return he(t, n);
911
+ }
912
+ function qt(t, n) {
913
+ return bn(t, n);
914
+ }
915
+ function Ze(t, n) {
916
+ return _e(t, n);
917
+ }
918
+ function Qe(t, n, e, o) {
919
+ if (!n || e === void 0 || !Number.isFinite(e) || e <= 0 || !Number.isFinite(o) || o <= 0) return !1;
920
+ const a = Ze(n, t);
921
+ if (!a) return !0;
922
+ const r = Math.max(0, a.maxX - a.minX) / e, i = Math.max(0, a.maxY - a.minY) / e;
923
+ return r < o && i < o;
924
+ }
925
+ function Xn(t) {
926
+ const n = Math.min(t.minX, t.maxX), e = Math.max(t.minX, t.maxX), o = Math.min(t.minY, t.maxY), a = Math.max(t.minY, t.maxY);
927
+ return [n, e, o, a].every(Number.isFinite) ? { minX: n, minY: o, maxX: e, maxY: a } : null;
928
+ }
929
+ function to(t) {
930
+ return Math.max(1, Math.abs(t.maxX - t.minX), Math.abs(t.maxY - t.minY));
931
+ }
932
+ function Ln(t, n) {
933
+ return `${t}:${n}`;
934
+ }
935
+ function no(t) {
936
+ const n = t.map((o) => o.worldBBox && !o.unknownBBox ? to(o.worldBBox) : 0).filter((o) => Number.isFinite(o) && o > 0).sort((o, a) => o - a), e = n.length > 0 ? n[Math.floor(n.length / 2)] : 1;
937
+ return Math.max(1, e * Me);
938
+ }
939
+ function eo(t, n) {
940
+ const e = t.map((i, c) => {
941
+ const s = n(i), u = s.worldBBox ? Xn(s.worldBBox) : null;
942
+ return {
943
+ sceneInstanceId: c,
944
+ sceneInstance: i,
945
+ worldBBox: u,
946
+ unknownBBox: s.unknownBBox || !u
947
+ };
948
+ }), o = no(e), a = /* @__PURE__ */ new Map(), r = [];
949
+ for (const i of e) {
950
+ if (i.unknownBBox || !i.worldBBox) {
951
+ r.push(i);
952
+ continue;
953
+ }
954
+ const c = Math.floor(i.worldBBox.minX / o), s = Math.floor(i.worldBBox.maxX / o), u = Math.floor(i.worldBBox.minY / o), h = Math.floor(i.worldBBox.maxY / o), d = (s - c + 1) * (h - u + 1);
955
+ if (!Number.isFinite(d) || d > Pe) {
956
+ r.push(i);
957
+ continue;
958
+ }
959
+ for (let _ = c; _ <= s; _ += 1)
960
+ for (let f = u; f <= h; f += 1) {
961
+ const w = Ln(_, f), y = a.get(w);
962
+ y ? y.push(i) : a.set(w, [i]);
963
+ }
964
+ }
965
+ return { cellSize: o, cells: a, overflow: r, records: e };
966
+ }
967
+ function oo(t, n, e) {
968
+ const o = gn.get(t);
969
+ if (o && o.records.length === n.length)
970
+ return o;
971
+ const a = eo(n, e);
972
+ return gn.set(t, a), a;
973
+ }
974
+ function ro(t, n) {
975
+ const e = Xn(n);
976
+ if (!e)
977
+ return t.records;
978
+ const o = /* @__PURE__ */ new Set(), a = Math.floor(e.minX / t.cellSize), r = Math.floor(e.maxX / t.cellSize), i = Math.floor(e.minY / t.cellSize), c = Math.floor(e.maxY / t.cellSize);
979
+ for (let s = a; s <= r; s += 1)
980
+ for (let u = i; u <= c; u += 1) {
981
+ const h = t.cells.get(Ln(s, u));
982
+ if (h)
983
+ for (const d of h)
984
+ d.worldBBox && qt(e, d.worldBBox) && o.add(d);
985
+ }
986
+ for (const s of t.overflow)
987
+ (!s.worldBBox || qt(e, s.worldBBox)) && o.add(s);
988
+ return Array.from(o).sort((s, u) => s.sceneInstanceId - u.sceneInstanceId);
989
+ }
990
+ function $o(t, n = {}) {
991
+ if (!rt(t))
992
+ return An();
993
+ const e = new DataView(t), { sections: o } = mt(t), a = X(o, 1, "SCENE_META"), r = _t(t, a, "SCENE_META");
994
+ if (!nn(r))
995
+ return An();
996
+ const i = X(o, 5, "VARIANT_TABLE");
997
+ if (i.length % 60 !== 0)
998
+ throw new l("E_GROUP_RANGE", "V3 VARIANT_TABLE length must be a multiple of 60");
999
+ const c = i.length / 60, s = Vt(r, c), u = s.reduce((A, E) => A + E.raw_size, 0), h = n.viewport === void 0 ? void 0 : n.viewport, d = (A, E) => {
1000
+ const p = Array.from(A).sort((U, V) => U - V), b = s.map((U) => U.id).filter((U) => !A.has(U)).sort((U, V) => U - V), S = p.reduce((U, V) => {
1001
+ var k;
1002
+ return U + (((k = s[V]) == null ? void 0 : k.raw_size) ?? 0);
1003
+ }, 0), M = {
1004
+ manifest: s,
1005
+ requiredVariantIds: p,
1006
+ requiredNormalBytes: S,
1007
+ totalManifestBytes: u,
1008
+ skippedVariantIds: b,
1009
+ includedSceneInstanceIds: [...E]
1010
+ };
1011
+ return h === void 0 ? M : { ...M, viewport: h };
1012
+ };
1013
+ if (n.loadBlocks === !1)
1014
+ return d(/* @__PURE__ */ new Set(), []);
1015
+ const _ = X(o, 7, "SCENE_INSTANCE_TABLE"), f = ut(o, 8), w = Z(t, _, "V3 SCENE_INSTANCE_TABLE"), y = Z(t, f, "V3 NESTED_INSTANCE_TABLE");
1016
+ if (Number(r.variant_count ?? c) !== c)
1017
+ throw new l("E_GROUP_RANGE", "V3 variant count mismatch");
1018
+ if (Number(r.scene_instance_count ?? w.length) !== w.length)
1019
+ throw new l("E_GROUP_RANGE", "V3 scene instance count mismatch");
1020
+ if (Number(r.nested_instance_count ?? y.length) !== y.length)
1021
+ throw new l("E_GROUP_RANGE", "V3 nested instance count mismatch");
1022
+ const m = /* @__PURE__ */ new Map(), x = [];
1023
+ for (let A = 0; A < c; A += 1) {
1024
+ const E = i.offset + A * 60, p = e.getUint32(E + 32, !0), b = e.getUint32(E + 36, !0);
1025
+ if (p + b > y.length)
1026
+ throw new l("E_GROUP_RANGE", `V3 variant ${A} nested range exceeds table`);
1027
+ const S = s[A], M = rn(S);
1028
+ x[A] = {
1029
+ id: A,
1030
+ nestedStart: p,
1031
+ nestedCount: b,
1032
+ precisionOriginX: e.getFloat32(E + 48, !0),
1033
+ precisionOriginY: e.getFloat32(E + 52, !0),
1034
+ bbox: M,
1035
+ ownBBox: M
1036
+ };
1037
+ const U = [];
1038
+ for (let V = 0; V < b; V += 1)
1039
+ U.push(y[p + V].variantId);
1040
+ m.set(A, U);
1041
+ }
1042
+ At(w, y, (A) => {
1043
+ if (!s[A.variantId])
1044
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${A.variantId}`);
1045
+ });
1046
+ const G = /* @__PURE__ */ new Set(), N = /* @__PURE__ */ new Set(), R = (A, E, p) => {
1047
+ if (E > 64)
1048
+ throw new l("E_GROUP_RANGE", "V3 nested instance depth exceeds limit");
1049
+ if (p.has(A))
1050
+ throw new l("E_GROUP_RANGE", `V3 nested variant cycle detected at ${A}`);
1051
+ if (!s[A])
1052
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${A}`);
1053
+ if (!N.has(A)) {
1054
+ G.add(A), p.add(A);
1055
+ for (const b of m.get(A) ?? [])
1056
+ R(b, E + 1, p);
1057
+ p.delete(A), N.add(A);
1058
+ }
1059
+ }, P = n.hiddenInstanceIds ?? /* @__PURE__ */ new Set(), B = n.viewport == null ? null : Je(
1060
+ n.viewport,
1061
+ n.viewportPaddingRatio ?? Oe
1062
+ ), D = n.minViewportPixelSize ?? $e, C = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new Set(), O = (A, E, p) => {
1063
+ if (E > 64)
1064
+ throw new l("E_GROUP_RANGE", "V3 nested instance depth exceeds limit");
1065
+ if (p.has(A))
1066
+ throw new l("E_GROUP_RANGE", `V3 nested variant cycle detected at ${A}`);
1067
+ if (C.has(A))
1068
+ return C.get(A) ?? null;
1069
+ const b = x[A];
1070
+ if (!b)
1071
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${A}`);
1072
+ p.add(A);
1073
+ let S = b.ownBBox, M = !1;
1074
+ for (let U = 0; U < b.nestedCount; U += 1) {
1075
+ const V = y[b.nestedStart + U], k = x[V.variantId];
1076
+ if (!k)
1077
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${V.variantId}`);
1078
+ const z = O(V.variantId, E + 1, p);
1079
+ if ((!z || $.has(V.variantId)) && (M = !0), !z)
1080
+ continue;
1081
+ const H = an(V, b, k);
1082
+ S = Rt(S, pt(z, H));
1083
+ }
1084
+ return p.delete(A), C.set(A, S), (!S || M) && $.add(A), S;
1085
+ }, v = [], K = (A, E, p) => {
1086
+ P.has(E) || B && p && (!qt(B, p) || Qe(
1087
+ p,
1088
+ n.viewport ?? null,
1089
+ n.worldUnitsPerPixel,
1090
+ D
1091
+ )) || (v.push(E), R(A.variantId, 0, /* @__PURE__ */ new Set()));
1092
+ };
1093
+ if (B) {
1094
+ const A = oo(t, w, (E) => {
1095
+ const p = O(E.variantId, 0, /* @__PURE__ */ new Set()), b = !p || $.has(E.variantId);
1096
+ return {
1097
+ worldBBox: p ? pt(p, E) : null,
1098
+ unknownBBox: b
1099
+ };
1100
+ });
1101
+ for (const E of ro(A, B))
1102
+ K(E.sceneInstance, E.sceneInstanceId, E.unknownBBox ? null : E.worldBBox);
1103
+ } else
1104
+ w.forEach((A, E) => {
1105
+ K(A, E, null);
1106
+ });
1107
+ return d(G, v);
1108
+ }
1109
+ function Mo(t) {
1110
+ if (!rt(t))
1111
+ return null;
1112
+ const n = vn(t);
1113
+ return en(n) ? Dn(n) : null;
1114
+ }
1115
+ function ao(t, n) {
1116
+ if (!Array.isArray(t) || t.length !== 4)
1117
+ throw new l("E_HEADER", `${n} must be a four-number array`);
1118
+ const e = t.map(Number);
1119
+ if (!e.every(Number.isFinite))
1120
+ throw new l("E_HEADER", `${n} must contain finite numbers`);
1121
+ return [e[0], e[1], e[2], e[3]];
1122
+ }
1123
+ function Tt(t, n, e, o) {
1124
+ return ft(n, e, t.byteLength, o), Zt.decode(t.subarray(n, n + e));
1125
+ }
1126
+ function Yn(t, n, e) {
1127
+ if (n.length % 20 !== 0)
1128
+ throw new l("E_GROUP_RANGE", "V3 LAYER_TABLE length must be a multiple of 20");
1129
+ const o = new DataView(t), a = [];
1130
+ for (let r = n.offset; r < n.offset + n.length; r += 20) {
1131
+ const i = o.getUint32(r, !0), c = o.getUint32(r + 4, !0);
1132
+ a.push({
1133
+ name: Tt(e, i, c, "V3 layer name"),
1134
+ color: [o.getUint8(r + 8), o.getUint8(r + 9), o.getUint8(r + 10)],
1135
+ visible: o.getUint8(r + 11) !== 0,
1136
+ lw_px: o.getFloat32(r + 12, !0)
1137
+ });
1138
+ }
1139
+ return a;
1140
+ }
1141
+ function yt(t, n, e, o, a, r) {
1142
+ if (e + 4 > o)
1143
+ throw new l("E_GROUP_RANGE", `Missing ${a} vertex_count`);
1144
+ const i = n.getUint32(e, !0), c = e + 4, s = lt(t, n, c, i, o, r);
1145
+ if (s.offset > o)
1146
+ throw new l("E_GROUP_RANGE", `${a} exceeds section bounds`);
1147
+ return {
1148
+ chunk: s.chunk,
1149
+ offset: s.offset
1150
+ };
1151
+ }
1152
+ function Fn(t, n, e, o, a, r) {
1153
+ const i = yt(t, n, e, o, a, r), c = e + 4;
1154
+ return {
1155
+ chunk: i.chunk,
1156
+ snapshot: {
1157
+ count: i.chunk.count,
1158
+ raw: new Uint8Array(t, c, i.offset - c).slice(),
1159
+ groupLayerEncoding: r
1160
+ },
1161
+ offset: i.offset
1162
+ };
1163
+ }
1164
+ function wt(t = "u8") {
1165
+ return {
1166
+ chunk: { pos: new Float32Array(0), col: new Uint8Array(0), lay: new Uint8Array(0), count: 0 },
1167
+ snapshot: { count: 0, raw: new Uint8Array(0), groupLayerEncoding: t }
1168
+ };
1169
+ }
1170
+ function io(t = "u8") {
1171
+ return {
1172
+ line: wt(t),
1173
+ fill: wt(t),
1174
+ dot: wt(t)
1175
+ };
1176
+ }
1177
+ function so(t, n, e) {
1178
+ var o;
1179
+ return !!(n && e && t.deferExternalVariantGeometry && !((o = t.externalVariantBuffers) != null && o.has(n.id)));
1180
+ }
1181
+ function St(t, n, e, o, a, r, i) {
1182
+ if (a === 0)
1183
+ return wt(i);
1184
+ ft(o, a, e.length, r);
1185
+ const c = e.offset + o, s = c + a, u = Fn(t, n, c, s, r, i);
1186
+ if (u.offset !== s)
1187
+ throw new l("E_GROUP_RANGE", `${r} has trailing bytes`);
1188
+ return { chunk: u.chunk, snapshot: u.snapshot };
1189
+ }
1190
+ function kn(t, n, e) {
1191
+ if ([
1192
+ t.getUint32(n + 8, !0),
1193
+ t.getUint32(n + 12, !0),
1194
+ t.getUint32(n + 16, !0),
1195
+ t.getUint32(n + 20, !0),
1196
+ t.getUint32(n + 24, !0),
1197
+ t.getUint32(n + 28, !0)
1198
+ ].some((a) => a !== 0))
1199
+ throw new l("E_GROUP_RANGE", `V3 external variant ${e} must use zero inline geometry offsets`);
1200
+ }
1201
+ function st(t, n, e, o, a, r) {
1202
+ const i = Fn(t, n, e, t.byteLength, a, r);
1203
+ if (i.chunk.count !== o)
1204
+ throw new l(
1205
+ "E_GROUP_RANGE",
1206
+ `${a} vertex_count mismatch: manifest=${o}, sidecar=${i.chunk.count}`
1207
+ );
1208
+ return i;
1209
+ }
1210
+ function on(t) {
1211
+ return "normal" in t ? t.normal : t;
1212
+ }
1213
+ function co(t, n, e) {
1214
+ const o = on(t), a = new DataView(n);
1215
+ let r = 0;
1216
+ const i = st(n, a, r, o.line_count, `V3 external variant ${t.id} line blob`, e);
1217
+ r = i.offset;
1218
+ const c = st(n, a, r, o.fill_count, `V3 external variant ${t.id} fill blob`, e);
1219
+ r = c.offset;
1220
+ const s = st(n, a, r, o.dot_count, `V3 external variant ${t.id} dot blob`, e);
1221
+ if (r = s.offset, r !== n.byteLength)
1222
+ throw new l("E_GROUP_RANGE", `V3 external variant ${t.id} sidecar has trailing bytes`);
1223
+ return {
1224
+ line: { chunk: i.chunk, snapshot: i.snapshot },
1225
+ fill: { chunk: c.chunk, snapshot: c.snapshot },
1226
+ dot: { chunk: s.chunk, snapshot: s.snapshot }
1227
+ };
1228
+ }
1229
+ function lo(t, n) {
1230
+ const e = Jt(n), o = et(e, 257, "BLOCK_NORMAL_GEOMETRY"), a = new DataView(n);
1231
+ let r = o.offset;
1232
+ const i = o.offset + o.length, c = Wt(e.versionMinor), s = st(n, a, r, t.normal.line_count, `V3.3 external variant ${t.id} normal line blob`, c);
1233
+ r = s.offset;
1234
+ const u = st(n, a, r, t.normal.fill_count, `V3.3 external variant ${t.id} normal fill blob`, c);
1235
+ r = u.offset;
1236
+ const h = st(n, a, r, t.normal.dot_count, `V3.3 external variant ${t.id} normal dot blob`, c);
1237
+ if (r = h.offset, r !== i)
1238
+ throw new l("E_GROUP_RANGE", `V3.3 external variant ${t.id} normal geometry section has trailing bytes`);
1239
+ return {
1240
+ line: { chunk: s.chunk, snapshot: s.snapshot },
1241
+ fill: { chunk: u.chunk, snapshot: u.snapshot },
1242
+ dot: { chunk: h.chunk, snapshot: h.snapshot }
1243
+ };
1244
+ }
1245
+ function uo(t, n, e = "u8") {
1246
+ if (n.byteLength !== t.raw_size)
1247
+ throw new l(
1248
+ "E_GROUP_RANGE",
1249
+ `V3 external variant ${t.id} raw_size mismatch: manifest=${t.raw_size}, sidecar=${n.byteLength}`
1250
+ );
1251
+ return "normal" in t ? lo(t, n) : co(t, n, e);
1252
+ }
1253
+ function fo(t, n, e) {
1254
+ const o = n == null ? void 0 : n.get(t.id);
1255
+ if (!o)
1256
+ throw new l("E_GROUP_RANGE", `Missing V3 external sidecar buffer for variant ${t.id}`);
1257
+ return uo(t, o, e);
1258
+ }
1259
+ function Ct(t, n, e, o, a) {
1260
+ if (o === 0)
1261
+ return 0;
1262
+ if (ft(e, o, n.length, a), o < 4)
1263
+ throw new l("E_GROUP_RANGE", `Missing ${a} vertex_count`);
1264
+ return t.getUint32(n.offset + e, !0);
1265
+ }
1266
+ function mo(t, n, e, o) {
1267
+ const a = e.offset + e.length;
1268
+ let r = e.offset;
1269
+ const i = yt(t, n, r, a, "V3 BASE_GEOMETRY line", o);
1270
+ r = i.offset;
1271
+ const c = yt(t, n, r, a, "V3 BASE_GEOMETRY fill", o);
1272
+ r = c.offset;
1273
+ const s = yt(t, n, r, a, "V3 BASE_GEOMETRY dot", o);
1274
+ if (r = s.offset, r !== a)
1275
+ throw new l("E_GROUP_RANGE", "V3 BASE_GEOMETRY has trailing bytes");
1276
+ return {
1277
+ line: i.chunk,
1278
+ fill: c.chunk,
1279
+ dot: s.chunk
1280
+ };
1281
+ }
1282
+ function Ot(t) {
1283
+ return Ee(t.pos, t.count, 2);
1284
+ }
1285
+ function _o(t, n, e) {
1286
+ return Rt(Rt(Ot(t), Ot(n)), Ot(e));
1287
+ }
1288
+ function rn(t) {
1289
+ return "normal" in t ? We(t.normal.bbox ?? t.normal.own_bbox ?? null) : null;
1290
+ }
1291
+ function ho(t, n, e, o, a, r, i, c) {
1292
+ if (e.length % 60 !== 0)
1293
+ throw new l("E_GROUP_RANGE", "V3 VARIANT_TABLE length must be a multiple of 60");
1294
+ const s = [], u = /* @__PURE__ */ new Map(), h = [], d = e.length / 60;
1295
+ for (let _ = 0; _ < d; _ += 1) {
1296
+ const f = e.offset + _ * 60, w = n.getUint32(f, !0), y = n.getUint32(f + 4, !0), m = (r == null ? void 0 : r[_]) ?? null, x = !m || !c.requiredExternalVariantIds || c.requiredExternalVariantIds.has(_), G = so(c, m, x), N = m ? (kn(n, f, _), !x || G ? io(i) : fo(m, c.externalVariantBuffers, i)) : {
1297
+ line: St(
1298
+ t,
1299
+ n,
1300
+ o,
1301
+ n.getUint32(f + 8, !0),
1302
+ n.getUint32(f + 12, !0),
1303
+ `V3 variant ${_} line blob`,
1304
+ i
1305
+ ),
1306
+ fill: St(
1307
+ t,
1308
+ n,
1309
+ o,
1310
+ n.getUint32(f + 16, !0),
1311
+ n.getUint32(f + 20, !0),
1312
+ `V3 variant ${_} fill blob`,
1313
+ i
1314
+ ),
1315
+ dot: St(
1316
+ t,
1317
+ n,
1318
+ o,
1319
+ n.getUint32(f + 24, !0),
1320
+ n.getUint32(f + 28, !0),
1321
+ `V3 variant ${_} dot blob`,
1322
+ i
1323
+ )
1324
+ }, R = m ? on(m) : null, P = (R == null ? void 0 : R.line_count) ?? N.line.chunk.count, B = (R == null ? void 0 : R.fill_count) ?? N.fill.chunk.count, D = (R == null ? void 0 : R.dot_count) ?? N.dot.chunk.count, C = m && (!x || G) ? rn(m) : _o(N.line.chunk, N.fill.chunk, N.dot.chunk), $ = {
1325
+ id: _,
1326
+ name: Tt(a, w, y, `V3 variant ${_} name`),
1327
+ line_count: G ? P : N.line.chunk.count,
1328
+ fill_count: G ? B : N.fill.chunk.count,
1329
+ dot_count: G ? D : N.dot.chunk.count,
1330
+ sourceLineCount: P,
1331
+ sourceFillCount: B,
1332
+ sourceDotCount: D,
1333
+ nestedStart: n.getUint32(f + 32, !0),
1334
+ nestedCount: n.getUint32(f + 36, !0),
1335
+ textStart: n.getUint32(f + 40, !0),
1336
+ textCount: n.getUint32(f + 44, !0),
1337
+ precisionOriginX: n.getFloat32(f + 48, !0),
1338
+ precisionOriginY: n.getFloat32(f + 52, !0),
1339
+ lineSnapshot: N.line.snapshot,
1340
+ fillSnapshot: N.fill.snapshot,
1341
+ dotSnapshot: N.dot.snapshot,
1342
+ bbox: C
1343
+ };
1344
+ s.push($), u.set(_, { line: N.line.chunk, fill: N.fill.chunk, dot: N.dot.chunk }), h.push({ line: N.line.snapshot, fill: N.fill.snapshot, dot: N.dot.snapshot });
1345
+ }
1346
+ return { variantsById: s, variants: u, snapshots: h };
1347
+ }
1348
+ function zn(t, n, e, o, a) {
1349
+ if (n.length % 60 !== 0)
1350
+ throw new l("E_GROUP_RANGE", "V3 VARIANT_TABLE length must be a multiple of 60");
1351
+ const r = [], i = n.length / 60;
1352
+ for (let c = 0; c < i; c += 1) {
1353
+ const s = n.offset + c * 60, u = t.getUint32(s, !0), h = t.getUint32(s + 4, !0), d = (a == null ? void 0 : a[c]) ?? null;
1354
+ d && kn(t, s, c);
1355
+ const _ = d ? on(d) : null, f = t.getFloat32(s + 48, !0), w = t.getFloat32(s + 52, !0);
1356
+ r.push({
1357
+ id: c,
1358
+ name: Tt(o, u, h, `V3 variant ${c} name`),
1359
+ line_count: d ? _.line_count : Ct(
1360
+ t,
1361
+ e,
1362
+ t.getUint32(s + 8, !0),
1363
+ t.getUint32(s + 12, !0),
1364
+ `V3 variant ${c} line blob`
1365
+ ),
1366
+ fill_count: d ? _.fill_count : Ct(
1367
+ t,
1368
+ e,
1369
+ t.getUint32(s + 16, !0),
1370
+ t.getUint32(s + 20, !0),
1371
+ `V3 variant ${c} fill blob`
1372
+ ),
1373
+ dot_count: d ? _.dot_count : Ct(
1374
+ t,
1375
+ e,
1376
+ t.getUint32(s + 24, !0),
1377
+ t.getUint32(s + 28, !0),
1378
+ `V3 variant ${c} dot blob`
1379
+ ),
1380
+ nestedStart: t.getUint32(s + 32, !0),
1381
+ nestedCount: t.getUint32(s + 36, !0),
1382
+ textStart: t.getUint32(s + 40, !0),
1383
+ textCount: t.getUint32(s + 44, !0),
1384
+ precisionOriginX: f,
1385
+ precisionOriginY: w,
1386
+ bbox: d ? rn(d) : null
1387
+ });
1388
+ }
1389
+ return r;
1390
+ }
1391
+ function Z(t, n, e) {
1392
+ if (n.length % 32 !== 0)
1393
+ throw new l("E_GROUP_RANGE", `${e} length must be a multiple of 32`);
1394
+ const o = new DataView(t), a = [];
1395
+ for (let r = n.offset; r < n.offset + n.length; r += 32) {
1396
+ const i = o.getUint16(r + 30, !0);
1397
+ Ao(i), a.push({
1398
+ variantId: o.getUint32(r, !0),
1399
+ matA: o.getFloat32(r + 4, !0),
1400
+ matB: o.getFloat32(r + 8, !0),
1401
+ matC: o.getFloat32(r + 12, !0),
1402
+ matD: o.getFloat32(r + 16, !0),
1403
+ tx: o.getFloat32(r + 20, !0),
1404
+ ty: o.getFloat32(r + 24, !0),
1405
+ insLayerIdx: o.getUint16(r + 28, !0),
1406
+ flags: i
1407
+ });
1408
+ }
1409
+ return a;
1410
+ }
1411
+ function Eo(t, n, e) {
1412
+ const o = t.getUint32(n, !0), a = t.getUint32(n + 4, !0), r = t.getUint8(n + 42), i = t.getUint8(n + 43), c = t.getFloat32(n + 24, !0), s = t.getFloat32(n + 28, !0);
1413
+ return {
1414
+ t: Tt(e, o, a, "V3 text value"),
1415
+ x: t.getFloat32(n + 8, !0),
1416
+ y: t.getFloat32(n + 12, !0),
1417
+ h: t.getFloat32(n + 16, !0),
1418
+ r: t.getFloat32(n + 20, !0),
1419
+ c: [t.getUint8(n + 36), t.getUint8(n + 37), t.getUint8(n + 38)],
1420
+ li: t.getUint16(n + 40, !0),
1421
+ ha: De[r & 3] ?? "left",
1422
+ va: ve[r >> 2 & 3] ?? "alphabetic",
1423
+ lh: t.getFloat32(n + 32, !0),
1424
+ ...c ? { w: c } : {},
1425
+ ...s !== 1 ? { wf: s } : {},
1426
+ ...i & 1 ? { mx: !0 } : {},
1427
+ ...i & 2 ? { my: !0 } : {}
1428
+ };
1429
+ }
1430
+ function $t(t, n, e, o) {
1431
+ if (n.length % 44 !== 0)
1432
+ throw new l("E_GROUP_RANGE", `${o} length must be a multiple of 44`);
1433
+ const a = [], r = new DataView(t);
1434
+ for (let i = n.offset; i < n.offset + n.length; i += 44)
1435
+ a.push(Eo(r, i, e));
1436
+ return a;
1437
+ }
1438
+ function xo(t, n) {
1439
+ if (t.length % 44 !== 0)
1440
+ throw new l("E_GROUP_RANGE", `${n} length must be a multiple of 44`);
1441
+ return t.length / 44;
1442
+ }
1443
+ function Hn(t, n, e, o) {
1444
+ return {
1445
+ instanceId: e,
1446
+ variantId: n.variantId,
1447
+ matA: t.matA * n.matA + t.matC * n.matB,
1448
+ matB: t.matB * n.matA + t.matD * n.matB,
1449
+ matC: t.matA * n.matC + t.matC * n.matD,
1450
+ matD: t.matB * n.matC + t.matD * n.matD,
1451
+ tx: t.matA * n.tx + t.matC * n.ty + t.tx,
1452
+ ty: t.matB * n.tx + t.matD * n.ty + t.ty,
1453
+ insLayerIdx: o
1454
+ };
1455
+ }
1456
+ function jn(t, n, e, o) {
1457
+ var y;
1458
+ const a = ((y = o[n.insLayerIdx]) == null ? void 0 : y.visible) === !1, r = n.matA * t.x + n.matC * t.y + n.tx, i = n.matB * t.x + n.matD * t.y + n.ty, c = Math.hypot(n.matA, n.matB), s = Math.hypot(n.matC, n.matD), u = s > 1e-12 ? s : c > 1e-12 ? c : 1, h = c > 1e-12 && u > 1e-12 ? c / u : 1, d = go(t, n), { mx: _, my: f, ...w } = t;
1459
+ return {
1460
+ ...w,
1461
+ x: r,
1462
+ y: i,
1463
+ h: t.h * u,
1464
+ w: t.w === void 0 ? void 0 : t.w * u,
1465
+ wf: (t.wf ?? 1) * h,
1466
+ r: d.r,
1467
+ ...d.mx ? { mx: !0 } : {},
1468
+ ...d.my ? { my: !0 } : {},
1469
+ li: a || t.li === e ? n.insLayerIdx : t.li
1470
+ };
1471
+ }
1472
+ function go(t, n) {
1473
+ const e = t.r * Math.PI / 180, o = Math.cos(e), a = Math.sin(e), r = t.mx ? -1 : 1, i = t.my ? -1 : 1, c = r * (n.matA * o + n.matC * a), s = r * (n.matB * o + n.matD * a), u = i * (n.matC * o - n.matA * a), h = i * (n.matD * o - n.matB * a), d = c * h - s * u, _ = Math.atan2(s, c) * 180 / Math.PI;
1474
+ return d < 0 ? { r: Math.atan2(-s, -c) * 180 / Math.PI, mx: !0 } : { r: _ };
1475
+ }
1476
+ function Mt(t, n, e, o) {
1477
+ var c;
1478
+ if (t.count === 0)
1479
+ return It();
1480
+ const a = new Float32Array(t.count * 2);
1481
+ for (let s = 0; s < t.count; s += 1) {
1482
+ const u = s * 2, h = t.pos[u], d = t.pos[u + 1];
1483
+ a[u] = n.matA * h + n.matC * d + n.tx, a[u + 1] = n.matB * h + n.matD * d + n.ty;
1484
+ }
1485
+ const r = ((c = o[n.insLayerIdx]) == null ? void 0 : c.visible) === !1, i = Cn(
1486
+ ot(t),
1487
+ (s) => r || s === e ? n.insLayerIdx : s
1488
+ );
1489
+ return { pos: a, count: t.count, segments: i };
1490
+ }
1491
+ function Pt(t) {
1492
+ const n = t.reduce((r, i) => r + i.count, 0);
1493
+ if (n === 0)
1494
+ return It();
1495
+ const e = new Float32Array(n * 2), o = [];
1496
+ let a = 0;
1497
+ for (const r of t)
1498
+ e.set(r.pos, a * 2), o.push(...Ht(ot(r), a)), a += r.count;
1499
+ return { pos: e, count: n, segments: dt(o) };
1500
+ }
1501
+ function qn(t, n) {
1502
+ return t === n ? n : t;
1503
+ }
1504
+ function Kn(t, n, e) {
1505
+ return t === e ? n : t;
1506
+ }
1507
+ function Wn(t, n) {
1508
+ const e = new DataView(t), { versionMinor: o, sections: a } = mt(t), r = Wt(o), i = X(a, 1, "SCENE_META"), c = X(a, 2, "LAYER_TABLE"), s = X(a, 3, "STRING_TABLE"), u = X(a, 4, "BASE_GEOMETRY"), h = X(a, 5, "VARIANT_TABLE"), d = X(a, 6, "VARIANT_GEOMETRY"), _ = X(a, 7, "SCENE_INSTANCE_TABLE"), f = ut(a, 8), w = ut(a, 9), y = ut(a, 10), m = _t(t, i, "SCENE_META"), x = nn(m) ? Vt(m, h.length / 60) : null, G = en(m), N = G ? Dn(m) : null, R = Ke(m, x, G), P = G ? "nested_instanced_external_annotations" : x ? "nested_instanced_external" : "nested_instanced", B = new Uint8Array(t, s.offset, s.length), D = Yn(t, c, B), C = Number(m.layer0_index ?? 0), $ = Qt({
1509
+ bounds: m.bounds ?? [0, 0, 0, 0],
1510
+ entity_count: Number(m.entity_count ?? 0),
1511
+ line_count: Number(m.base_line_count ?? 0),
1512
+ fill_count: Number(m.base_fill_count ?? 0),
1513
+ dot_count: Number(m.base_dot_count ?? 0),
1514
+ layers: D,
1515
+ texts: []
1516
+ });
1517
+ Zn(C, $.layers);
1518
+ const O = mo(t, e, u, r);
1519
+ if (!n.loadBlocks) {
1520
+ const g = zn(e, h, d, B, x), I = Z(t, _, "V3 SCENE_INSTANCE_TABLE"), F = Z(t, f, "V3 NESTED_INSTANCE_TABLE"), L = $t(t, w, B, "V3 SCENE_TEXT_TABLE"), Y = xo(y, "V3 VARIANT_TEXT_TABLE");
1521
+ if (Number(m.variant_count ?? g.length) !== g.length)
1522
+ throw new l("E_GROUP_RANGE", "V3 variant count mismatch");
1523
+ if (Number(m.scene_instance_count ?? I.length) !== I.length)
1524
+ throw new l("E_GROUP_RANGE", "V3 scene instance count mismatch");
1525
+ if (Number(m.nested_instance_count ?? F.length) !== F.length)
1526
+ throw new l("E_GROUP_RANGE", "V3 nested instance count mismatch");
1527
+ if (Number(m.scene_text_count ?? L.length) !== L.length)
1528
+ throw new l("E_GROUP_RANGE", "V3 scene text count mismatch");
1529
+ if (Number(m.variant_text_count ?? Y) !== Y)
1530
+ throw new l("E_GROUP_RANGE", "V3 variant text count mismatch");
1531
+ for (const T of g) {
1532
+ if (T.nestedStart + T.nestedCount > F.length)
1533
+ throw new l("E_GROUP_RANGE", `V3 variant ${T.id} nested range exceeds table`);
1534
+ if (T.textStart + T.textCount > Y)
1535
+ throw new l("E_GROUP_RANGE", `V3 variant ${T.id} text range exceeds table`);
1536
+ }
1537
+ return At(I, F, (T) => {
1538
+ if (!g[T.variantId])
1539
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${T.variantId}`);
1540
+ Kt(T.insLayerIdx, D.length);
1541
+ }), {
1542
+ meta: {
1543
+ bounds: m.bounds ?? [0, 0, 0, 0],
1544
+ entity_count: Number(m.entity_count ?? 0),
1545
+ line_count: O.line.count,
1546
+ fill_count: O.fill.count,
1547
+ dot_count: O.dot.count,
1548
+ version: 3,
1549
+ version_minor: o,
1550
+ encoding: P,
1551
+ ...R ? { features: R } : {},
1552
+ ...x ? { external_variants: [...x] } : {},
1553
+ ...N ? { external_annotations: N } : {},
1554
+ layer0_index: C,
1555
+ block_variants: g.map((T) => ({
1556
+ id: T.id,
1557
+ name: T.name,
1558
+ line_count: T.line_count,
1559
+ fill_count: T.fill_count,
1560
+ dot_count: T.dot_count
1561
+ })),
1562
+ instance_count: I.length,
1563
+ layers: D,
1564
+ texts: L
1565
+ },
1566
+ header: {
1567
+ ...$,
1568
+ lineCount: O.line.count,
1569
+ fillCount: O.fill.count,
1570
+ dotCount: O.dot.count,
1571
+ textCount: L.length,
1572
+ texts: L
1573
+ },
1574
+ decoded: {
1575
+ baseLine: O.line,
1576
+ baseFill: O.fill,
1577
+ baseDot: O.dot,
1578
+ variants: /* @__PURE__ */ new Map(),
1579
+ instances: [],
1580
+ instanceIdByVariantOrdinal: /* @__PURE__ */ new Map()
1581
+ }
1582
+ };
1583
+ }
1584
+ const { variantsById: v, variants: K, snapshots: A } = ho(
1585
+ t,
1586
+ e,
1587
+ h,
1588
+ d,
1589
+ B,
1590
+ x,
1591
+ r,
1592
+ n
1593
+ ), E = Z(t, _, "V3 SCENE_INSTANCE_TABLE"), p = Z(t, f, "V3 NESTED_INSTANCE_TABLE"), b = $t(t, w, B, "V3 SCENE_TEXT_TABLE"), S = $t(t, y, B, "V3 VARIANT_TEXT_TABLE");
1594
+ if (Number(m.variant_count ?? v.length) !== v.length)
1595
+ throw new l("E_GROUP_RANGE", "V3 variant count mismatch");
1596
+ if (Number(m.scene_instance_count ?? E.length) !== E.length)
1597
+ throw new l("E_GROUP_RANGE", "V3 scene instance count mismatch");
1598
+ if (Number(m.nested_instance_count ?? p.length) !== p.length)
1599
+ throw new l("E_GROUP_RANGE", "V3 nested instance count mismatch");
1600
+ if (Number(m.scene_text_count ?? b.length) !== b.length)
1601
+ throw new l("E_GROUP_RANGE", "V3 scene text count mismatch");
1602
+ if (Number(m.variant_text_count ?? S.length) !== S.length)
1603
+ throw new l("E_GROUP_RANGE", "V3 variant text count mismatch");
1604
+ for (const g of v) {
1605
+ if (g.nestedStart + g.nestedCount > p.length)
1606
+ throw new l("E_GROUP_RANGE", `V3 variant ${g.id} nested range exceeds table`);
1607
+ if (g.textStart + g.textCount > S.length)
1608
+ throw new l("E_GROUP_RANGE", `V3 variant ${g.id} text range exceeds table`);
1609
+ }
1610
+ At(E, p, (g) => {
1611
+ if (!v[g.variantId])
1612
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${g.variantId}`);
1613
+ Kt(g.insLayerIdx, D.length);
1614
+ });
1615
+ const M = /* @__PURE__ */ new Map();
1616
+ for (const g of v)
1617
+ M.set(
1618
+ g.id,
1619
+ S.slice(g.textStart, g.textStart + g.textCount)
1620
+ );
1621
+ const U = [], V = n.externalVariantDeltaOnly, k = V ? [] : [...b], z = /* @__PURE__ */ new Map(), H = /* @__PURE__ */ new Map(), ht = /* @__PURE__ */ new Map(), bt = (g, I) => {
1622
+ z.set(g, Rt(z.get(g) ?? null, I));
1623
+ }, Et = (g, I, F, L, Y, Q) => {
1624
+ var fn;
1625
+ if (Y > 64)
1626
+ throw new l("E_GROUP_RANGE", "V3 nested instance depth exceeds limit");
1627
+ if (Q.has(g))
1628
+ throw new l("E_GROUP_RANGE", `V3 nested variant cycle detected at ${g}`);
1629
+ const T = v[g];
1630
+ if (!T)
1631
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${g}`);
1632
+ Q.add(g);
1633
+ const tt = !n.requiredExternalVariantIds || n.requiredExternalVariantIds.has(g), W = V ? ((fn = n.requiredExternalVariantIds) == null ? void 0 : fn.has(g)) === !0 : tt;
1634
+ let j = !1;
1635
+ const dn = !!(T.line_count || T.fill_count || T.dot_count), ne = T.textCount > 0;
1636
+ if (W && (dn || ne) && (U.push({
1637
+ instanceId: I.instanceId,
1638
+ variantId: g,
1639
+ matA: I.matA,
1640
+ matB: I.matB,
1641
+ matC: I.matC,
1642
+ matD: I.matD,
1643
+ tx: I.tx,
1644
+ ty: I.ty,
1645
+ insLayerIdx: I.insLayerIdx
1646
+ }), dn && bt(I.instanceId, T.bbox ? Jn(T.bbox, I) : null), j = !0), W) {
1647
+ T.textCount > 0 && (j = !0);
1648
+ for (let nt = 0; nt < T.textCount; nt += 1) {
1649
+ const at = jn(S[T.textStart + nt], I, C, $.layers);
1650
+ k.push({
1651
+ ...at,
1652
+ iv: F,
1653
+ io: L
1654
+ });
1655
+ }
1656
+ }
1657
+ for (let nt = 0; nt < T.nestedCount; nt += 1) {
1658
+ const at = p[T.nestedStart + nt], ee = Kn(at.insLayerIdx, I.insLayerIdx, C), oe = an(at, T, v[at.variantId]), re = Hn(I, oe, I.instanceId, ee);
1659
+ j = Et(at.variantId, re, F, L, Y + 1, Q) || j;
1660
+ }
1661
+ return Q.delete(g), j;
1662
+ }, Bt = /* @__PURE__ */ new Map();
1663
+ E.forEach((g, I) => {
1664
+ if (!v[g.variantId])
1665
+ throw new l("E_GROUP_RANGE", `Unknown V3 scene variantId ${g.variantId}`);
1666
+ const L = ht.get(g.variantId) ?? 0;
1667
+ ht.set(g.variantId, L + 1), H.set(`${g.variantId}:${L}`, I);
1668
+ const Y = qn(g.insLayerIdx, C), Q = {
1669
+ instanceId: I,
1670
+ variantId: g.variantId,
1671
+ matA: g.matA,
1672
+ matB: g.matB,
1673
+ matC: g.matC,
1674
+ matD: g.matD,
1675
+ tx: g.tx,
1676
+ ty: g.ty,
1677
+ insLayerIdx: Y
1678
+ }, T = Et(g.variantId, Q, g.variantId, L, 0, /* @__PURE__ */ new Set());
1679
+ if (V && !T)
1680
+ return;
1681
+ let tt = Bt.get(Y);
1682
+ tt || (tt = /* @__PURE__ */ new Map(), Bt.set(Y, tt));
1683
+ let W = tt.get(g.variantId);
1684
+ W || (W = { count: 0, bboxes: [], inserts: [] }, tt.set(g.variantId, W)), W.count += 1;
1685
+ const j = z.get(I) ?? null;
1686
+ j && W.bboxes.push(j), W.inserts.push({
1687
+ instanceId: I,
1688
+ variantId: g.variantId,
1689
+ label: `#${I + 1} (${g.tx.toFixed(1)}, ${g.ty.toFixed(1)})`,
1690
+ tx: g.tx,
1691
+ ty: g.ty,
1692
+ canHighlight: j !== null,
1693
+ ...j ? { bbox: j } : {}
1694
+ });
1695
+ });
1696
+ const sn = /* @__PURE__ */ new Map();
1697
+ for (const [g, I] of Bt) {
1698
+ const F = [];
1699
+ for (const [L, Y] of I)
1700
+ F.push({
1701
+ variantId: L,
1702
+ blockName: Mn(v[L], L),
1703
+ instanceCount: Y.count,
1704
+ bboxes: Y.bboxes,
1705
+ inserts: Y.inserts
1706
+ });
1707
+ F.sort((L, Y) => Y.instanceCount - L.instanceCount), sn.set(g, F);
1708
+ }
1709
+ let cn = V ? 0 : O.line.count, ln = V ? 0 : O.fill.count, un = V ? 0 : O.dot.count;
1710
+ for (const g of U) {
1711
+ const I = v[g.variantId];
1712
+ !(n.deferExternalVariantGeometry && I.lineSnapshot.count === 0 && I.fillSnapshot.count === 0 && I.dotSnapshot.count === 0) && !Nt(g.instanceId, n) || (cn += I.line_count, ln += I.fill_count, un += I.dot_count);
1713
+ }
1714
+ const xt = ke(k, H, n), Qn = {
1715
+ bounds: m.bounds ?? [0, 0, 0, 0],
1716
+ entity_count: V ? 0 : Number(m.entity_count ?? 0),
1717
+ line_count: O.line.count,
1718
+ fill_count: O.fill.count,
1719
+ dot_count: O.dot.count,
1720
+ version: 3,
1721
+ version_minor: o,
1722
+ encoding: P,
1723
+ ...R ? { features: R } : {},
1724
+ ...x ? { external_variants: [...x] } : {},
1725
+ ...N ? { external_annotations: N } : {},
1726
+ layer0_index: C,
1727
+ block_variants: v.map((g) => ({
1728
+ id: g.id,
1729
+ name: g.name,
1730
+ line_count: g.sourceLineCount,
1731
+ fill_count: g.sourceFillCount,
1732
+ dot_count: g.sourceDotCount
1733
+ })),
1734
+ instance_count: E.length,
1735
+ layers: D,
1736
+ texts: xt
1737
+ }, te = {
1738
+ ...$,
1739
+ entityCount: V ? 0 : $.entityCount,
1740
+ lineCount: cn,
1741
+ fillCount: ln,
1742
+ dotCount: un,
1743
+ textCount: xt.length,
1744
+ texts: xt,
1745
+ ...n.loadBlocks ? {
1746
+ layerBlockUsages: sn,
1747
+ insertRuntime: He(
1748
+ C,
1749
+ v,
1750
+ A,
1751
+ M,
1752
+ U,
1753
+ xt,
1754
+ H
1755
+ )
1756
+ } : {}
1757
+ };
1758
+ return {
1759
+ meta: Qn,
1760
+ header: te,
1761
+ decoded: {
1762
+ baseLine: V ? Xt() : O.line,
1763
+ baseFill: V ? Xt() : O.fill,
1764
+ baseDot: V ? Xt() : O.dot,
1765
+ variants: K,
1766
+ instances: U,
1767
+ instanceIdByVariantOrdinal: H
1768
+ }
1769
+ };
1770
+ }
1771
+ function Nn(t) {
1772
+ return "annotation" in t && (t.annotation.line_count > 0 || t.annotation.fill_count > 0 || t.annotation.dot_count > 0 || t.annotation.text_count > 0);
1773
+ }
1774
+ function yo(t, n) {
1775
+ if (n.line.count !== t.annotation.line_count || n.fill.count !== t.annotation.fill_count || n.dot.count !== t.annotation.dot_count || n.texts.length !== t.annotation.text_count)
1776
+ throw new l(
1777
+ "E_GROUP_RANGE",
1778
+ `V3.3 external variant ${t.id} annotation count mismatch`
1779
+ );
1780
+ }
1781
+ function wo(t, n, e, o, a, r) {
1782
+ return {
1783
+ line: Mt(t.line, n, e, o),
1784
+ fill: Mt(t.fill, n, e, o),
1785
+ dot: Mt(t.dot, n, e, o),
1786
+ texts: t.texts.map((i) => ({
1787
+ ...jn(i, n, e, o),
1788
+ iv: a,
1789
+ io: r
1790
+ }))
1791
+ };
1792
+ }
1793
+ function Po(t, n) {
1794
+ if (!rt(t))
1795
+ return null;
1796
+ const e = new DataView(t), { sections: o } = mt(t), a = X(o, 1, "SCENE_META"), r = X(o, 2, "LAYER_TABLE"), i = X(o, 3, "STRING_TABLE"), c = X(o, 5, "VARIANT_TABLE"), s = X(o, 6, "VARIANT_GEOMETRY"), u = X(o, 7, "SCENE_INSTANCE_TABLE"), h = ut(o, 8), d = _t(t, a, "SCENE_META");
1797
+ if (!en(d))
1798
+ return null;
1799
+ const _ = Vt(d, c.length / 60);
1800
+ if (!_.some(Nn))
1801
+ return null;
1802
+ const f = new Uint8Array(t, i.offset, i.length), w = Yn(t, r, f), y = Number(d.layer0_index ?? 0), m = Qt({
1803
+ bounds: d.bounds ?? [0, 0, 0, 0],
1804
+ entity_count: Number(d.entity_count ?? 0),
1805
+ line_count: Number(d.base_line_count ?? 0),
1806
+ fill_count: Number(d.base_fill_count ?? 0),
1807
+ dot_count: Number(d.base_dot_count ?? 0),
1808
+ layers: w,
1809
+ texts: []
1810
+ });
1811
+ Zn(y, m.layers);
1812
+ const x = zn(e, c, s, f, _), G = Z(t, u, "V3 SCENE_INSTANCE_TABLE"), N = Z(t, h, "V3 NESTED_INSTANCE_TABLE");
1813
+ if (Number(d.variant_count ?? x.length) !== x.length)
1814
+ throw new l("E_GROUP_RANGE", "V3 variant count mismatch");
1815
+ if (Number(d.scene_instance_count ?? G.length) !== G.length)
1816
+ throw new l("E_GROUP_RANGE", "V3 scene instance count mismatch");
1817
+ if (Number(d.nested_instance_count ?? N.length) !== N.length)
1818
+ throw new l("E_GROUP_RANGE", "V3 nested instance count mismatch");
1819
+ for (const E of x)
1820
+ if (E.nestedStart + E.nestedCount > N.length)
1821
+ throw new l("E_GROUP_RANGE", `V3 variant ${E.id} nested range exceeds table`);
1822
+ At(G, N, (E) => {
1823
+ if (!x[E.variantId])
1824
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${E.variantId}`);
1825
+ Kt(E.insLayerIdx, w.length);
1826
+ });
1827
+ const R = /* @__PURE__ */ new Map(), P = (E) => {
1828
+ var U;
1829
+ const p = _[E];
1830
+ if (!p || !Nn(p))
1831
+ return null;
1832
+ const b = R.get(E);
1833
+ if (b)
1834
+ return b;
1835
+ const S = (U = n == null ? void 0 : n.externalVariantBuffers) == null ? void 0 : U.get(p.id);
1836
+ if (!S)
1837
+ return null;
1838
+ if (S.byteLength !== p.raw_size)
1839
+ throw new l(
1840
+ "E_GROUP_RANGE",
1841
+ `V3.3 annotation sidecar ${p.id} raw_size mismatch: manifest=${p.raw_size}, sidecar=${S.byteLength}`
1842
+ );
1843
+ const M = be(S);
1844
+ return yo(p, M), R.set(E, M), M;
1845
+ }, B = [], D = [], C = [], $ = [], O = (n == null ? void 0 : n.hiddenInstanceIds) ?? /* @__PURE__ */ new Set(), v = (E, p, b, S) => {
1846
+ const M = wo(
1847
+ E,
1848
+ p,
1849
+ y,
1850
+ m.layers,
1851
+ b,
1852
+ S
1853
+ );
1854
+ B.push(M.line), D.push(M.fill), C.push(M.dot), $.push(...M.texts);
1855
+ }, K = (E, p, b, S, M, U) => {
1856
+ if (M > 64)
1857
+ throw new l("E_GROUP_RANGE", "V3 nested instance depth exceeds limit");
1858
+ if (U.has(E))
1859
+ throw new l("E_GROUP_RANGE", `V3 nested variant cycle detected at ${E}`);
1860
+ const V = x[E];
1861
+ if (!V)
1862
+ throw new l("E_GROUP_RANGE", `Unknown V3 variantId ${E}`);
1863
+ U.add(E);
1864
+ const k = P(E);
1865
+ k && v(k, p, b, S);
1866
+ for (let z = 0; z < V.nestedCount; z += 1) {
1867
+ const H = N[V.nestedStart + z], ht = Kn(H.insLayerIdx, p.insLayerIdx, y), bt = an(H, V, x[H.variantId]), Et = Hn(p, bt, p.instanceId, ht);
1868
+ K(H.variantId, Et, b, S, M + 1, U);
1869
+ }
1870
+ U.delete(E);
1871
+ }, A = /* @__PURE__ */ new Map();
1872
+ return G.forEach((E, p) => {
1873
+ const b = A.get(E.variantId) ?? 0;
1874
+ if (A.set(E.variantId, b + 1), O.has(p))
1875
+ return;
1876
+ const S = qn(E.insLayerIdx, y), M = {
1877
+ instanceId: p,
1878
+ variantId: E.variantId,
1879
+ matA: E.matA,
1880
+ matB: E.matB,
1881
+ matC: E.matC,
1882
+ matD: E.matD,
1883
+ tx: E.tx,
1884
+ ty: E.ty,
1885
+ insLayerIdx: S
1886
+ };
1887
+ K(E.variantId, M, E.variantId, b, 0, /* @__PURE__ */ new Set());
1888
+ }), B.every((E) => E.count === 0) && D.every((E) => E.count === 0) && C.every((E) => E.count === 0) && $.length === 0 ? null : {
1889
+ line: Pt(B),
1890
+ fill: Pt(D),
1891
+ dot: Pt(C),
1892
+ texts: $
1893
+ };
1894
+ }
1895
+ function Rt(t, n) {
1896
+ return me(t, n);
1897
+ }
1898
+ function Jn(t, n) {
1899
+ return Bn(t, n);
1900
+ }
1901
+ function Rn(t) {
1902
+ return xe(t);
1903
+ }
1904
+ function pn(t, n) {
1905
+ return ge(t, n);
1906
+ }
1907
+ function pt(t, n) {
1908
+ return Jn(t, {
1909
+ variantId: n.variantId,
1910
+ matA: n.matA,
1911
+ matB: n.matB,
1912
+ matC: n.matC,
1913
+ matD: n.matD,
1914
+ tx: n.tx,
1915
+ ty: n.ty,
1916
+ insLayerIdx: n.insLayerIdx
1917
+ });
1918
+ }
1919
+ function an(t, n, e) {
1920
+ if (!(e != null && e.bbox) || !n.bbox)
1921
+ return t;
1922
+ const o = n.precisionOriginX, a = n.precisionOriginY;
1923
+ if (!Number.isFinite(o) || !Number.isFinite(a) || o === 0 && a === 0)
1924
+ return t;
1925
+ const r = {
1926
+ ...t,
1927
+ tx: t.tx - o,
1928
+ ty: t.ty - a
1929
+ }, i = pt(e.bbox, t), c = pt(e.bbox, r), s = Math.max(Rn(n.bbox), Rn(e.bbox), 1), u = Math.hypot(o, a), h = Math.hypot(t.tx, t.ty), d = Math.hypot(r.tx, r.ty);
1930
+ if (u <= s * 16 || h <= s * 16)
1931
+ return t;
1932
+ const _ = pn(n.bbox, i), f = pn(n.bbox, c);
1933
+ return _ > s * 16 && f * 4 < _ && d * 4 < h ? r : t;
1934
+ }
1935
+ function Kt(t, n) {
1936
+ if (t >= n)
1937
+ throw new l("E_GROUP_RANGE", `Instance layer index ${t} exceeds layer count ${n}`);
1938
+ }
1939
+ function Zn(t, n) {
1940
+ if (!Number.isInteger(t) || t < 0 || t >= n.length)
1941
+ throw new l("E_GROUP_RANGE", `layer0_index out of range: ${t}`);
1942
+ if (n[t].name !== "0")
1943
+ throw new l("E_GROUP_RANGE", `layer0_index must reference layer "0": ${t}`);
1944
+ }
1945
+ function Ao(t) {
1946
+ if (t !== 0)
1947
+ throw new l("E_GROUP_RANGE", `Instance flags must be zero for Stage 1, found ${t}`);
1948
+ }
1949
+ function* No(t, n, e, o, a) {
1950
+ let r = e;
1951
+ const i = [
1952
+ { kind: "line", count: o.lineCount },
1953
+ { kind: "fill", count: o.fillCount },
1954
+ { kind: "dot", count: o.dotCount }
1955
+ ];
1956
+ for (const c of i) {
1957
+ const s = de(t, n, r, c.count);
1958
+ for (; ; ) {
1959
+ const u = s.next();
1960
+ if (u.done) {
1961
+ r = u.value;
1962
+ break;
1963
+ }
1964
+ yield { kind: c.kind, payload: u.value };
1965
+ }
1966
+ }
1967
+ if (r !== t.byteLength)
1968
+ throw new l("E_GROUP_RANGE", `Trailing bytes detected: offset=${r}, size=${t.byteLength}`);
1969
+ for (const c of o.texts)
1970
+ yield { kind: "text", payload: c };
1971
+ }
1972
+ function* Ro(t, n, e, o) {
1973
+ yield* No(t, n, e, o);
1974
+ }
1975
+ function Dt(t, n, e, o, a, r, i) {
1976
+ let c = t.count, s = 0;
1977
+ for (const f of e) {
1978
+ if (!Nt(f.instanceId, i))
1979
+ continue;
1980
+ const w = n.get(f.variantId);
1981
+ if (!w)
1982
+ throw new l("E_GROUP_RANGE", `Unknown variantId ${f.variantId}`);
1983
+ const y = w[o].count;
1984
+ s += y, c += y;
1985
+ }
1986
+ if ($n(i, o, s), c === 0)
1987
+ return It();
1988
+ const u = new Float32Array(c * 2), h = [];
1989
+ let d = 0;
1990
+ ((f) => {
1991
+ u.set(f.pos, d * 2), h.push(...Ht(ot(f), d)), d += f.count;
1992
+ })(t);
1993
+ for (const f of e) {
1994
+ if (!Nt(f.instanceId, i))
1995
+ continue;
1996
+ const w = n.get(f.variantId);
1997
+ if (!w)
1998
+ throw new l("E_GROUP_RANGE", `Unknown variantId ${f.variantId}`);
1999
+ const y = w[o], m = r[f.insLayerIdx].visible === !1;
2000
+ for (let x = 0; x < y.count; x += 1) {
2001
+ const G = x * 2, R = (d + x) * 2, P = y.pos[G], B = y.pos[G + 1];
2002
+ u[R] = f.matA * P + f.matC * B + f.tx, u[R + 1] = f.matB * P + f.matD * B + f.ty;
2003
+ }
2004
+ h.push(...Ht(
2005
+ Cn(
2006
+ ot(y),
2007
+ (x) => m || x === a ? f.insLayerIdx : x
2008
+ ),
2009
+ d
2010
+ )), d += y.count;
2011
+ }
2012
+ return { pos: u, count: c, segments: dt(h) };
2013
+ }
2014
+ function* vt(t, n, e, o, a, r, i, c) {
2015
+ if ($n(c, t, Math.max(0, n - e.count)), n === 0) {
2016
+ yield { kind: t, payload: It() };
2017
+ return;
2018
+ }
2019
+ const s = Lt;
2020
+ let u = new Float32Array(s * 2), h = [], d = 0, _ = 0;
2021
+ const f = function* () {
2022
+ if (d <= 0)
2023
+ return;
2024
+ const m = d === s ? { pos: u, count: d, segments: dt(h) } : { pos: u.slice(0, d * 2), count: d, segments: dt(h) };
2025
+ yield { kind: t, payload: m }, _ += d, d = 0, u = new Float32Array(s * 2), h = [];
2026
+ }, w = (m, x, G, N) => {
2027
+ if (m <= 0)
2028
+ return;
2029
+ const R = h[h.length - 1];
2030
+ if (R && R.startVertex + R.vertexCount === d && R.layerIndex === G && R.color[0] === x[0] && R.color[1] === x[1] && R.color[2] === x[2] && R.lineweightPx === N) {
2031
+ h[h.length - 1] = {
2032
+ ...R,
2033
+ vertexCount: R.vertexCount + m
2034
+ };
2035
+ return;
2036
+ }
2037
+ h.push({
2038
+ startVertex: d,
2039
+ vertexCount: m,
2040
+ color: [x[0], x[1], x[2]],
2041
+ layerIndex: G,
2042
+ ...N === void 0 ? {} : { lineweightPx: N }
2043
+ });
2044
+ };
2045
+ yield* function* () {
2046
+ for (const m of ot(e)) {
2047
+ let x = m.startVertex;
2048
+ const G = m.startVertex + m.vertexCount;
2049
+ for (; x < G; ) {
2050
+ const N = Math.min(s - d, G - x), R = x * 2, P = d * 2;
2051
+ u.set(e.pos.subarray(R, R + N * 2), P), w(N, m.color, m.layerIndex, m.lineweightPx), x += N, d += N, d === s && (yield* f());
2052
+ }
2053
+ }
2054
+ }();
2055
+ for (const m of a) {
2056
+ if (!Nt(m.instanceId, c))
2057
+ continue;
2058
+ const x = o.get(m.variantId);
2059
+ if (!x)
2060
+ throw new l("E_GROUP_RANGE", `Unknown variantId ${m.variantId}`);
2061
+ const G = x[t], N = i[m.insLayerIdx].visible === !1;
2062
+ for (const R of ot(G)) {
2063
+ const P = N || R.layerIndex === r ? m.insLayerIdx : R.layerIndex;
2064
+ let B = R.startVertex;
2065
+ const D = R.startVertex + R.vertexCount;
2066
+ for (; B < D; ) {
2067
+ const C = Math.min(s - d, D - B);
2068
+ for (let $ = 0; $ < C; $ += 1) {
2069
+ const O = (B + $) * 2, v = (d + $) * 2, K = G.pos[O], A = G.pos[O + 1];
2070
+ u[v] = m.matA * K + m.matC * A + m.tx, u[v + 1] = m.matB * K + m.matD * A + m.ty;
2071
+ }
2072
+ w(C, R.color, P, R.lineweightPx), B += C, d += C, d === s && (yield* f());
2073
+ }
2074
+ }
2075
+ }
2076
+ if (yield* f(), _ !== n)
2077
+ throw new l("E_GROUP_RANGE", `Expanded ${t} count mismatch: header=${n}, emitted=${_}`);
2078
+ }
2079
+ function* po(t, n, e, o) {
2080
+ yield* vt(
2081
+ "line",
2082
+ n.lineCount,
2083
+ t.baseLine,
2084
+ t.variants,
2085
+ t.instances,
2086
+ e.layer0_index,
2087
+ n.layers,
2088
+ o
2089
+ ), yield* vt(
2090
+ "fill",
2091
+ n.fillCount,
2092
+ t.baseFill,
2093
+ t.variants,
2094
+ t.instances,
2095
+ e.layer0_index,
2096
+ n.layers,
2097
+ o
2098
+ ), yield* vt(
2099
+ "dot",
2100
+ n.dotCount,
2101
+ t.baseDot,
2102
+ t.variants,
2103
+ t.instances,
2104
+ e.layer0_index,
2105
+ n.layers,
2106
+ o
2107
+ );
2108
+ for (const a of n.texts)
2109
+ yield { kind: "text", payload: a };
2110
+ }
2111
+ function Io(t, n) {
2112
+ const e = On(n), o = Wn(t, e);
2113
+ return {
2114
+ header: o.header,
2115
+ chunks: po(
2116
+ o.decoded,
2117
+ o.header,
2118
+ { layer0_index: o.meta.layer0_index ?? 0 },
2119
+ e
2120
+ )
2121
+ };
2122
+ }
2123
+ function Go(t, n) {
2124
+ if (rt(t))
2125
+ return Io(t, n);
2126
+ const e = tn(t);
2127
+ return {
2128
+ header: e.header,
2129
+ chunks: Ro(t, e.view, e.dataOffset, e.header)
2130
+ };
2131
+ }
2132
+ function Do(t) {
2133
+ if (rt(t)) {
2134
+ const n = vn(t);
2135
+ return ao(n.bounds, "V3 SCENE_META bounds");
2136
+ }
2137
+ return tn(t).header.bounds;
2138
+ }
2139
+ function* vo(t, n) {
2140
+ const e = Go(t, n);
2141
+ for (const o of e.chunks)
2142
+ yield o;
2143
+ return e.header;
2144
+ }
2145
+ function Xt() {
2146
+ return {
2147
+ pos: new Float32Array(0),
2148
+ col: new Uint8Array(0),
2149
+ lay: new Uint8Array(0),
2150
+ count: 0
2151
+ };
2152
+ }
2153
+ function it(t) {
2154
+ const n = Be(t);
2155
+ return {
2156
+ pos: t.pos,
2157
+ col: n.col,
2158
+ lay: n.lay,
2159
+ count: t.count
2160
+ };
2161
+ }
2162
+ function Xo(t, n) {
2163
+ if (rt(t)) {
2164
+ const c = On(n), s = Wn(t, c), u = Dt(
2165
+ s.decoded.baseLine,
2166
+ s.decoded.variants,
2167
+ s.decoded.instances,
2168
+ "line",
2169
+ s.meta.layer0_index ?? 0,
2170
+ s.header.layers,
2171
+ c
2172
+ ), h = Dt(
2173
+ s.decoded.baseFill,
2174
+ s.decoded.variants,
2175
+ s.decoded.instances,
2176
+ "fill",
2177
+ s.meta.layer0_index ?? 0,
2178
+ s.header.layers,
2179
+ c
2180
+ ), d = Dt(
2181
+ s.decoded.baseDot,
2182
+ s.decoded.variants,
2183
+ s.decoded.instances,
2184
+ "dot",
2185
+ s.meta.layer0_index ?? 0,
2186
+ s.header.layers,
2187
+ c
2188
+ );
2189
+ return {
2190
+ meta: s.meta,
2191
+ line: it(u),
2192
+ fill: it(h),
2193
+ dot: it(d),
2194
+ rawBytes: t.byteLength
2195
+ };
2196
+ }
2197
+ const e = tn(t);
2198
+ let o = e.dataOffset;
2199
+ const a = lt(t, e.view, o, e.header.lineCount);
2200
+ o = a.offset;
2201
+ const r = lt(t, e.view, o, e.header.fillCount);
2202
+ o = r.offset;
2203
+ const i = lt(t, e.view, o, e.header.dotCount);
2204
+ if (o = i.offset, o !== t.byteLength)
2205
+ throw new l("E_GROUP_RANGE", `Trailing bytes detected: offset=${o}, size=${t.byteLength}`);
2206
+ return {
2207
+ meta: e.meta,
2208
+ line: it(a.chunk),
2209
+ fill: it(r.chunk),
2210
+ dot: it(i.chunk),
2211
+ rawBytes: t.byteLength
2212
+ };
2213
+ }
2214
+ function Lo(t, n) {
2215
+ return t ? fe(Bn(t, n)) : null;
2216
+ }
2217
+ export {
2218
+ l as CadPackError,
2219
+ l as GeoLoaderError,
2220
+ Lt as STREAM_VERTEX_WINDOW,
2221
+ Ne as assertStrictTextRoundtrip,
2222
+ To as captureGroupSnapshot,
2223
+ Yt as cloneViewportRect,
2224
+ Ee as computeViewportRectFromPositions,
2225
+ It as emptyGeometryChunk,
2226
+ Be as expandGeometryChunkMetadata,
2227
+ he as expandViewportRect,
2228
+ et as getDxbsSection,
2229
+ Wt as groupLayerEncodingForV3Minor,
2230
+ _e as intersectViewportRects,
2231
+ Ge as isDxbsPayload,
2232
+ Ae as isStrictTextRoundtripEnabled,
2233
+ dt as mergeAdjacentSegments,
2234
+ me as mergeViewportRects,
2235
+ ot as normalizeGeometrySegments,
2236
+ fe as normalizeViewportRect,
2237
+ Ht as offsetGeometrySegments,
2238
+ Do as parseBinBounds,
2239
+ Xo as parseBinPayload,
2240
+ Go as parseBinStreaming,
2241
+ vo as parseBinToChunks,
2242
+ be as readBlockAnnotationSidecar,
2243
+ Jt as readDxbsContainer,
2244
+ Mo as readExternalAnnotationManifest,
2245
+ Po as readExternalBlockAnnotations,
2246
+ $o as readExternalVariantDependencyPlan,
2247
+ uo as readExternalVariantGeometrySidecar,
2248
+ Oo as readExternalVariantManifest,
2249
+ lt as readGroup,
2250
+ So as readSceneAnnotationSidecar,
2251
+ Cn as remapGeometrySegmentLayers,
2252
+ Uo as shouldAssertStrictTextRoundtrip,
2253
+ Co as sliceGeometrySegments,
2254
+ de as streamGroupChunks,
2255
+ Lo as transformBBoxByInstance,
2256
+ Bn as transformViewportRect,
2257
+ ge as viewportRectCenterDistance,
2258
+ bo as viewportRectContains,
2259
+ xe as viewportRectDiagonal,
2260
+ Bo as viewportRectEquals,
2261
+ bn as viewportRectIntersects
2262
+ };