@kalyptos/narcissus 0.1.1 → 0.1.2

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.
@@ -78,24 +78,14 @@ var m = 19, h = 1024, g = class {
78
78
  #_ = [];
79
79
  constructor(e, t) {
80
80
  this.capacity = t, this.#e = e;
81
- let n = this.slots, r = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC, i = (t, n) => e.createBuffer({
82
- label: t,
83
- size: n,
84
- usage: r
85
- });
86
- this.geometry = i("pool geometry", t * 4 * 4), this.colour = i("pool colour", t * 3 * 4), this.pages = i("pool pages", n * 36 * 4), this.clusters = i("pool clusters", n * 12 * 4), this.list = i("pool list", n * 4), this.#a = new Uint8Array(n), this.#o = n, this.#c = Array.from({ length: n }, (e, t) => n - 1 - t), this.#u = new Uint32Array(n * 36), this.#d = new Float32Array(this.#u.buffer), this.#p = new Uint32Array(n * 12), this.#m = new Float32Array(this.#p.buffer), this.#g = new Uint32Array(n);
81
+ let n = this.slots;
82
+ this.#y(), this.#a = new Uint8Array(n), this.#o = n, this.#c = Array.from({ length: n }, (e, t) => n - 1 - t), this.#u = new Uint32Array(n * 36), this.#d = new Float32Array(this.#u.buffer), this.#p = new Uint32Array(n * 12), this.#m = new Float32Array(this.#p.buffer), this.#g = new Uint32Array(n);
87
83
  }
88
84
  get slots() {
89
85
  return this.capacity / 256;
90
86
  }
91
87
  get bytes() {
92
- return [
93
- this.geometry,
94
- this.colour,
95
- this.pages,
96
- this.clusters,
97
- this.list
98
- ].reduce((e, t) => e + t.size, 0);
88
+ return this.#v().reduce((e, t) => e + t.size, 0);
99
89
  }
100
90
  has(e) {
101
91
  let t = this.#t.get(e);
@@ -110,13 +100,13 @@ var m = 19, h = 1024, g = class {
110
100
  add(e, n, r, i) {
111
101
  if (this.#t.has(e)) return !0;
112
102
  let a = c(n.count) / 256;
113
- if (!this.#b(a, i)) return !1;
103
+ if (!this.#S(a, i)) return !1;
114
104
  let o = t.origin, s = {
115
105
  id: e,
116
106
  previous: null,
117
107
  next: null,
118
108
  page: n,
119
- slots: this.#w(a),
109
+ slots: this.#E(a),
120
110
  active: [],
121
111
  mask: 0,
122
112
  table: this.#c.pop(),
@@ -127,14 +117,14 @@ var m = 19, h = 1024, g = class {
127
117
  ],
128
118
  factors: 0
129
119
  };
130
- return this.#t.set(e, s), this.#y(s), this.#i.add(e), !0;
120
+ return this.#t.set(e, s), this.#x(s), this.#i.add(e), !0;
131
121
  }
132
122
  remove(e) {
133
123
  let t = this.#t.get(e);
134
124
  if (t) {
135
- this.#v(t), this.#t.delete(e), this.#i.delete(e);
125
+ this.#b(t), this.#t.delete(e), this.#i.delete(e);
136
126
  for (let e of t.slots) this.#a[e] = 0;
137
- this.#s = Math.min(this.#s, t.slots[0]), this.#o += t.slots.length, this.#c.push(t.table), t.page === null && t.factors !== 0 && this.#C(t, 0);
127
+ this.#s = Math.min(this.#s, t.slots[0]), this.#o += t.slots.length, this.#c.push(t.table), t.page === null && t.factors !== 0 && this.#T(t, 0);
138
128
  }
139
129
  }
140
130
  clear() {
@@ -147,7 +137,7 @@ var m = 19, h = 1024, g = class {
147
137
  let t = 0;
148
138
  for (let n of this.#i) {
149
139
  if (t >= e) break;
150
- t += this.#x(this.#t.get(n)), this.#i.delete(n);
140
+ t += this.#C(this.#t.get(n)), this.#i.delete(n);
151
141
  }
152
142
  return t;
153
143
  }
@@ -157,7 +147,7 @@ var m = 19, h = 1024, g = class {
157
147
  let e = this.#t.get(i);
158
148
  if (!e || e.page !== null) continue;
159
149
  let o = e.active;
160
- e.factors !== a && this.#C(e, a), r ||= o !== e.active, this.#y(e), r ||= this.#_[n] !== e, this.#_[n++] = e, r && this.#g.set(e.active, t), t += e.active.length;
150
+ e.factors !== a && this.#T(e, a), r ||= o !== e.active, this.#x(e), r ||= this.#_[n] !== e, this.#_[n++] = e, r && this.#g.set(e.active, t), t += e.active.length;
161
151
  }
162
152
  r ||= this.#_.length !== n, this.#_.length = n, this.listed = t;
163
153
  let i = this.#e.queue;
@@ -169,26 +159,53 @@ var m = 19, h = 1024, g = class {
169
159
  e[1],
170
160
  e[2]
171
161
  ];
172
- for (let e of this.#t.values()) e.page === null && this.#S(e);
162
+ for (let e of this.#t.values()) e.page === null && this.#w(e);
173
163
  this.#e.queue.writeBuffer(this.pages, 0, this.#u), this.#f.clear();
174
164
  }
165
+ grow(e) {
166
+ if (e <= this.capacity) return;
167
+ let t = this.slots, n = this.#v();
168
+ this.capacity = e, this.#y();
169
+ let r = this.#e.createCommandEncoder({ label: "pool grow" });
170
+ this.#v().forEach((e, t) => r.copyBufferToBuffer(n[t], 0, e, 0, n[t].size)), this.#e.queue.submit([r.finish()]);
171
+ for (let e of n) e.destroy();
172
+ let i = this.slots, a = new Uint8Array(i);
173
+ a.set(this.#a), this.#a = a, this.#o += i - t;
174
+ for (let e = i - 1; e >= t; e--) this.#c.push(e);
175
+ let o = new Uint32Array(i * 36);
176
+ o.set(this.#u), this.#u = o, this.#d = new Float32Array(o.buffer);
177
+ let s = new Uint32Array(i * 12);
178
+ s.set(this.#p), this.#p = s, this.#m = new Float32Array(s.buffer);
179
+ let c = new Uint32Array(i);
180
+ c.set(this.#g), this.#g = c;
181
+ }
175
182
  destroy() {
176
- let e = [
183
+ for (let e of this.#v()) e.destroy();
184
+ }
185
+ #v() {
186
+ return [
177
187
  this.geometry,
178
188
  this.colour,
179
189
  this.pages,
180
190
  this.clusters,
181
191
  this.list
182
192
  ];
183
- for (let t of e) t.destroy();
184
193
  }
185
- #v(e) {
194
+ #y() {
195
+ let { capacity: e, slots: t } = this, n = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC, r = (e, t) => this.#e.createBuffer({
196
+ label: e,
197
+ size: t,
198
+ usage: n
199
+ });
200
+ this.geometry = r("pool geometry", e * 4 * 4), this.colour = r("pool colour", e * 3 * 4), this.pages = r("pool pages", t * 36 * 4), this.clusters = r("pool clusters", t * 12 * 4), this.list = r("pool list", t * 4);
201
+ }
202
+ #b(e) {
186
203
  e.previous ? e.previous.next = e.next : this.#n === e && (this.#n = e.next), e.next ? e.next.previous = e.previous : this.#r === e && (this.#r = e.previous), e.previous = null, e.next = null;
187
204
  }
188
- #y(e) {
189
- this.#r !== e && (this.#v(e), e.previous = this.#r, this.#r ? this.#r.next = e : this.#n = e, this.#r = e);
205
+ #x(e) {
206
+ this.#r !== e && (this.#b(e), e.previous = this.#r, this.#r ? this.#r.next = e : this.#n = e, this.#r = e);
190
207
  }
191
- #b(e, t) {
208
+ #S(e, t) {
192
209
  let n = this.#o;
193
210
  if (n >= e) return !0;
194
211
  let r = [];
@@ -197,7 +214,7 @@ var m = 19, h = 1024, g = class {
197
214
  for (let e of r) this.remove(e);
198
215
  return !0;
199
216
  }
200
- #x(e) {
217
+ #C(e) {
201
218
  let n = e.page, { slots: r } = e, i = this.#e.queue, a = r.length, o = this.#p, s = this.#m;
202
219
  for (let t = 0; t < a; t++) {
203
220
  let i = r[t] * 12;
@@ -211,13 +228,13 @@ var m = 19, h = 1024, g = class {
211
228
  i.writeBuffer(this.geometry, c * 4 * 4, n.geometry, o * 4, s * 4), i.writeBuffer(this.colour, c * 3 * 4, n.colour, o * 3, s * 3), this.#h.mark(r[e] * 12, (r[e] + t) * 12), e += t;
212
229
  }
213
230
  let c = e.table * 36, l = this.#u, u = this.#d, d = n.params, [f, p, m] = n.quadrants;
214
- return l.fill(0, c, c + 36), this.#S(e), l[c + 3] = n.count, u.set(d.subarray(t.step, t.step + 3), c + 4), l[c + 7] = f, u.set(d.subarray(t.scaleMin, t.scaleMin + 3), c + 8), l[c + 11] = p, u.set(d.subarray(t.scaleMax, t.scaleMax + 3), c + 12), l[c + 15] = m, u.set(d.subarray(t.dcMin, t.dcMin + 3), c + 16), u.set(d.subarray(t.dcMax, t.dcMax + 3), c + 20), u.set(d.subarray(t.shScale, t.shScale + 9), c + 24), this.#f.mark(c, c + 36), e.page = null, n.count * 7 * 4 + a * 12 * 4 + 144;
231
+ return l.fill(0, c, c + 36), this.#w(e), l[c + 3] = n.count, u.set(d.subarray(t.step, t.step + 3), c + 4), l[c + 7] = f, u.set(d.subarray(t.scaleMin, t.scaleMin + 3), c + 8), l[c + 11] = p, u.set(d.subarray(t.scaleMax, t.scaleMax + 3), c + 12), l[c + 15] = m, u.set(d.subarray(t.dcMin, t.dcMin + 3), c + 16), u.set(d.subarray(t.dcMax, t.dcMax + 3), c + 20), u.set(d.subarray(t.shScale, t.shScale + 9), c + 24), this.#f.mark(c, c + 36), e.page = null, n.count * 7 * 4 + a * 12 * 4 + 144;
215
232
  }
216
- #S(e) {
233
+ #w(e) {
217
234
  let t = e.table * 36;
218
235
  for (let n = 0; n < 3; n++) this.#d[t + n] = e.base[n] - this.#l[n];
219
236
  }
220
- #C(e, t) {
237
+ #T(e, t) {
221
238
  let n = 0;
222
239
  for (let e = 0; e < 4; e++) t >>> 8 * e & 255 && (n |= 1 << e);
223
240
  if (n !== e.mask) {
@@ -236,7 +253,7 @@ var m = 19, h = 1024, g = class {
236
253
  let r = e.table * 36 + m;
237
254
  this.#u[r] = t, this.#f.mark(r, r + 1);
238
255
  }
239
- #w(e) {
256
+ #E(e) {
240
257
  let t = this.#a, n = -1;
241
258
  for (let r = this.#s; r <= t.length - e;) {
242
259
  let i = r;
@@ -681,7 +698,7 @@ function he(e, t, n, r, i, a, o) {
681
698
  }
682
699
  //#endregion
683
700
  //#region src/engine/gpu/sort.wgsl?raw
684
- var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan, a stable scatter; no spin-waits\n\nconst THREADS: u32 = 128u;\nconst ITEMS: u32 = 8u;\nconst BLOCK: u32 = 1024u;\n// blocks per scan chunk\nconst CHUNK: u32 = 64u;\nconst DIGITS: u32 = 256u;\n\nstruct SortParams {\n shift: u32,\n // word offsets of the keys read and written; their values follow capacity words later\n src: u32,\n dst: u32,\n capacity: u32,\n // word offsets in the work buffer\n projected: u32,\n drawArgs: u32,\n}\n\nstruct SortState {\n count: u32,\n blocks: u32,\n chunks: u32,\n}\n\n@group(0) @binding(0) var<uniform> params: SortParams;\n@group(0) @binding(1) var<storage, read_write> data: array<u32>;\n// block-major: count, then exclusive global offset, of digit d in block b at b DIGITS + d\n@group(0) @binding(2) var<storage, read_write> hist: array<u32>;\n// chunk-major sums of hist, then their exclusive global offsets\n@group(0) @binding(3) var<storage, read_write> sums: array<u32>;\n@group(0) @binding(4) var<storage, read> state: SortState;\n@group(0) @binding(5) var<storage, read_write> stateOut: SortState;\n@group(0) @binding(6) var<storage, read_write> work: array<u32>;\n\nvar<workgroup> bins: array<atomic<u32>, DIGITS>;\nvar<workgroup> totals: array<u32, DIGITS>;\nvar<workgroup> bitValues: atomic<u32>;\nvar<workgroup> activeBits: u32;\nvar<workgroup> sk: array<u32, BLOCK>;\nvar<workgroup> sv: array<u32, BLOCK>;\n// three 10-bit counts; the fourth follows from the number of items\nvar<workgroup> counts: array<u32, THREADS * 2u>;\nvar<workgroup> tail: u32;\nvar<workgroup> starts: array<u32, DIGITS>;\nvar<workgroup> offsets: array<u32, DIGITS>;\n\nfn unpack(packed: u32, count: u32) -> vec4u {\n let a = packed & 1023u;\n let b = (packed >> 10u) & 1023u;\n let c = (packed >> 20u) & 1023u;\n return vec4u(a, b, c, count - a - b - c);\n}\n\nfn digit(key: u32) -> u32 {\n return (key >> params.shift) & 0xffu;\n}\n\n@compute @workgroup_size(1)\nfn prepare() {\n let count = min(work[params.projected], params.capacity);\n let blocks = (count + BLOCK - 1u) / BLOCK;\n work[params.drawArgs] = 4u;\n work[params.drawArgs + 1u] = count;\n work[params.drawArgs + 2u] = 0u;\n work[params.drawArgs + 3u] = 0u;\n stateOut = SortState(count, blocks, (blocks + CHUNK - 1u) / CHUNK);\n}\n\n@compute @workgroup_size(THREADS)\nfn histogram(\n @builtin(workgroup_id) wid: vec3u,\n @builtin(num_workgroups) groups: vec3u,\n @builtin(local_invocation_index) t: u32\n) {\n for (var block = wid.y * groups.x + wid.x; block < state.blocks; block += groups.x * groups.y) {\n let head = block * BLOCK;\n for (var k = 0u; k < ITEMS; k++) {\n let i = head + k * THREADS + t;\n if (i < state.count) {\n atomicAdd(&bins[digit(data[params.src + i])], 1u);\n }\n }\n workgroupBarrier();\n for (var d = t; d < DIGITS; d += THREADS) {\n hist[block * DIGITS + d] = atomicExchange(&bins[d], 0u);\n }\n workgroupBarrier();\n }\n}\n\n@compute @workgroup_size(DIGITS)\nfn reduce(@builtin(workgroup_id) wid: vec3u, @builtin(local_invocation_index) t: u32) {\n let end = min((wid.x + 1u) * CHUNK, state.blocks);\n var sum = 0u;\n for (var b = wid.x * CHUNK; b < end; b++) {\n sum += hist[b * DIGITS + t];\n }\n sums[wid.x * DIGITS + t] = sum;\n}\n\n@compute @workgroup_size(DIGITS)\nfn top(@builtin(local_invocation_index) t: u32) {\n var run = 0u;\n for (var c = 0u; c < state.chunks; c++) {\n let x = sums[c * DIGITS + t];\n sums[c * DIGITS + t] = run;\n run += x;\n }\n totals[t] = run;\n // a bit varies only when both zero and one occur in nonempty digits\n if (run > 0u) {\n atomicOr(&bitValues, t | ((255u ^ t) << 8u));\n }\n workgroupBarrier();\n if (t == 0u) {\n let values = atomicLoad(&bitValues);\n sums[arrayLength(&sums) - 1u] = values & (values >> 8u);\n }\n for (var o = 1u; o < DIGITS; o *= 2u) {\n var x = 0u;\n if (t >= o) {\n x = totals[t - o];\n }\n workgroupBarrier();\n totals[t] += x;\n workgroupBarrier();\n }\n let base = totals[t] - run;\n for (var c = 0u; c < state.chunks; c++) {\n sums[c * DIGITS + t] += base;\n }\n}\n\n@compute @workgroup_size(DIGITS)\nfn down(@builtin(workgroup_id) wid: vec3u, @builtin(local_invocation_index) t: u32) {\n let end = min((wid.x + 1u) * CHUNK, state.blocks);\n var run = sums[wid.x * DIGITS + t];\n for (var b = wid.x * CHUNK; b < end; b++) {\n let x = hist[b * DIGITS + t];\n hist[b * DIGITS + t] = run;\n run += x;\n }\n}\n\n@compute @workgroup_size(THREADS)\nfn scatter(\n @builtin(workgroup_id) wid: vec3u,\n @builtin(num_workgroups) groups: vec3u,\n @builtin(local_invocation_index) t: u32\n) {\n let first = wid.y * groups.x + wid.x;\n if (first >= state.blocks) {\n return;\n }\n if (t == 0u) {\n activeBits = sums[arrayLength(&sums) - 1u];\n }\n let bits = workgroupUniformLoad(&activeBits);\n for (var block = first; block < state.blocks; block += groups.x * groups.y) {\n let head = block * BLOCK;\n let valid = min(BLOCK, state.count - head);\n // padding has digit 255 at every shift, so it sorts behind the valid keys and is never written\n for (var k = 0u; k < ITEMS; k++) {\n let i = k * THREADS + t;\n var key = 0xffffffffu;\n var value = 0u;\n if (i < valid) {\n key = data[params.src + head + i];\n value = data[params.src + params.capacity + head + i];\n }\n sk[i] = key;\n sv[i] = value;\n }\n for (var d = t; d < DIGITS; d += THREADS) {\n offsets[d] = hist[block * DIGITS + d];\n }\n workgroupBarrier();\n\n // stable local sort by the digit, two bits a split; thread t owns positions t ITEMS + k\n for (var bit = 0u; bit < 8u; bit += 2u) {\n if ((bits & (3u << bit)) == 0u) {\n continue;\n }\n var keys: array<u32, ITEMS>;\n var values: array<u32, ITEMS>;\n var before: array<u32, ITEMS>;\n var mine = vec4u(0u);\n for (var k = 0u; k < ITEMS; k++) {\n keys[k] = sk[t * ITEMS + k];\n values[k] = sv[t * ITEMS + k];\n let b = (keys[k] >> (params.shift + bit)) & 3u;\n before[k] = mine[b];\n mine[b] += 1u;\n }\n counts[t] = mine.x | (mine.y << 10u) | (mine.z << 20u);\n if (t == THREADS - 1u) {\n tail = counts[t];\n }\n workgroupBarrier();\n // separate reads and writes need only one barrier per scan step\n var source = 0u;\n for (var o = 1u; o < THREADS; o *= 2u) {\n var x = counts[source + t];\n if (t >= o) {\n x += counts[source + t - o];\n }\n let dest = source ^ THREADS;\n counts[dest + t] = x;\n workgroupBarrier();\n source = dest;\n }\n // prefixes stop at 1016 items, so no count carries into its neighbour\n let total = unpack(counts[source + THREADS - 2u], BLOCK - ITEMS) + unpack(tail, ITEMS);\n var prior = vec4u(0u);\n if (t > 0u) {\n prior = unpack(counts[source + t - 1u], t * ITEMS);\n }\n let base = vec4u(0u, total.x, total.x + total.y, total.x + total.y + total.z);\n for (var k = 0u; k < ITEMS; k++) {\n let b = (keys[k] >> (params.shift + bit)) & 3u;\n let p = base[b] + prior[b] + before[k];\n sk[p] = keys[k];\n sv[p] = values[k];\n }\n workgroupBarrier();\n }\n\n for (var k = 0u; k < ITEMS; k++) {\n let i = t * ITEMS + k;\n let d = digit(sk[i]);\n if (i == 0u || digit(sk[i - 1u]) != d) {\n starts[d] = i;\n }\n }\n workgroupBarrier();\n for (var k = 0u; k < ITEMS; k++) {\n let i = k * THREADS + t;\n if (i < valid) {\n let d = digit(sk[i]);\n let slot = offsets[d] + i - starts[d];\n data[params.dst + slot] = sk[i];\n data[params.dst + params.capacity + slot] = sv[i];\n }\n }\n workgroupBarrier();\n }\n}\n", _e = 1024, ve = 64, ye = 256, P = 3, F = 256, be = 32, xe = class {
701
+ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan, a stable scatter; no spin-waits\n\nconst THREADS: u32 = 128u;\nconst ITEMS: u32 = 8u;\nconst BLOCK: u32 = 1024u;\n// blocks per scan chunk\nconst CHUNK: u32 = 64u;\nconst DIGITS: u32 = 256u;\n\nstruct SortParams {\n shift: u32,\n // word offsets of the keys read and written; their values follow capacity words later\n src: u32,\n dst: u32,\n capacity: u32,\n // word offsets in the work buffer\n projected: u32,\n drawArgs: u32,\n}\n\nstruct SortState {\n count: u32,\n blocks: u32,\n chunks: u32,\n}\n\n@group(0) @binding(0) var<uniform> params: SortParams;\n@group(0) @binding(1) var<storage, read_write> data: array<u32>;\n// block-major: count, then exclusive global offset, of digit d in block b at b DIGITS + d\n@group(0) @binding(2) var<storage, read_write> hist: array<u32>;\n// chunk-major sums of hist, then their exclusive global offsets\n@group(0) @binding(3) var<storage, read_write> sums: array<u32>;\n@group(0) @binding(4) var<storage, read> state: SortState;\n@group(0) @binding(5) var<storage, read_write> stateOut: SortState;\n@group(0) @binding(6) var<storage, read_write> work: array<u32>;\n\nvar<workgroup> bins: array<atomic<u32>, DIGITS>;\nvar<workgroup> totals: array<u32, DIGITS>;\nvar<workgroup> bitValues: atomic<u32>;\nvar<workgroup> activeBits: u32;\nvar<workgroup> sk: array<u32, BLOCK>;\nvar<workgroup> sv: array<u32, BLOCK>;\n// three 10-bit counts; the fourth follows from the number of items\nvar<workgroup> counts: array<u32, THREADS * 2u>;\nvar<workgroup> tail: u32;\nvar<workgroup> starts: array<u32, DIGITS>;\nvar<workgroup> offsets: array<u32, DIGITS>;\n\nfn unpack(packed: u32, count: u32) -> vec4u {\n let a = packed & 1023u;\n let b = (packed >> 10u) & 1023u;\n let c = (packed >> 20u) & 1023u;\n return vec4u(a, b, c, count - a - b - c);\n}\n\nfn digit(key: u32) -> u32 {\n return (key >> params.shift) & 0xffu;\n}\n\n@compute @workgroup_size(1)\nfn prepare() {\n let count = min(work[params.projected], params.capacity);\n let blocks = (count + BLOCK - 1u) / BLOCK;\n work[params.drawArgs] = 4u;\n work[params.drawArgs + 1u] = count;\n work[params.drawArgs + 2u] = 0u;\n work[params.drawArgs + 3u] = 0u;\n stateOut = SortState(count, blocks, (blocks + CHUNK - 1u) / CHUNK);\n}\n\n@compute @workgroup_size(THREADS)\nfn histogram(\n @builtin(workgroup_id) wid: vec3u,\n @builtin(num_workgroups) groups: vec3u,\n @builtin(local_invocation_index) t: u32\n) {\n for (var block = wid.y * groups.x + wid.x; block < state.blocks; block += groups.x * groups.y) {\n let head = block * BLOCK;\n for (var k = 0u; k < ITEMS; k++) {\n let i = head + k * THREADS + t;\n if (i < state.count) {\n atomicAdd(&bins[digit(data[params.src + i])], 1u);\n }\n }\n workgroupBarrier();\n for (var d = t; d < DIGITS; d += THREADS) {\n hist[block * DIGITS + d] = atomicExchange(&bins[d], 0u);\n }\n workgroupBarrier();\n }\n}\n\n@compute @workgroup_size(DIGITS)\nfn reduce(@builtin(workgroup_id) wid: vec3u, @builtin(local_invocation_index) t: u32) {\n let end = min((wid.x + 1u) * CHUNK, state.blocks);\n var sum = 0u;\n for (var b = wid.x * CHUNK; b < end; b++) {\n sum += hist[b * DIGITS + t];\n }\n sums[wid.x * DIGITS + t] = sum;\n}\n\n@compute @workgroup_size(DIGITS)\nfn top(@builtin(local_invocation_index) t: u32) {\n var run = 0u;\n for (var c = 0u; c < state.chunks; c++) {\n let x = sums[c * DIGITS + t];\n sums[c * DIGITS + t] = run;\n run += x;\n }\n totals[t] = run;\n // a bit varies only when both zero and one occur in nonempty digits\n if (run > 0u) {\n atomicOr(&bitValues, t | ((255u ^ t) << 8u));\n }\n workgroupBarrier();\n if (t == 0u) {\n let values = atomicLoad(&bitValues);\n sums[arrayLength(&sums) - 1u] = values & (values >> 8u);\n }\n for (var o = 1u; o < DIGITS; o *= 2u) {\n var x = 0u;\n if (t >= o) {\n x = totals[t - o];\n }\n workgroupBarrier();\n totals[t] += x;\n workgroupBarrier();\n }\n let base = totals[t] - run;\n for (var c = 0u; c < state.chunks; c++) {\n sums[c * DIGITS + t] += base;\n }\n}\n\n@compute @workgroup_size(DIGITS)\nfn down(@builtin(workgroup_id) wid: vec3u, @builtin(local_invocation_index) t: u32) {\n let end = min((wid.x + 1u) * CHUNK, state.blocks);\n var run = sums[wid.x * DIGITS + t];\n for (var b = wid.x * CHUNK; b < end; b++) {\n let x = hist[b * DIGITS + t];\n hist[b * DIGITS + t] = run;\n run += x;\n }\n}\n\n@compute @workgroup_size(THREADS)\nfn scatter(\n @builtin(workgroup_id) wid: vec3u,\n @builtin(num_workgroups) groups: vec3u,\n @builtin(local_invocation_index) t: u32\n) {\n let first = wid.y * groups.x + wid.x;\n if (first >= state.blocks) {\n return;\n }\n if (t == 0u) {\n activeBits = sums[arrayLength(&sums) - 1u];\n }\n let bits = workgroupUniformLoad(&activeBits);\n for (var block = first; block < state.blocks; block += groups.x * groups.y) {\n let head = block * BLOCK;\n let valid = min(BLOCK, state.count - head);\n // padding has digit 255 at every shift, so it sorts behind the valid keys and is never written\n for (var k = 0u; k < ITEMS; k++) {\n let i = k * THREADS + t;\n var key = 0xffffffffu;\n var value = 0u;\n if (i < valid) {\n key = data[params.src + head + i];\n value = data[params.src + params.capacity + head + i];\n }\n sk[i] = key;\n sv[i] = value;\n }\n for (var d = t; d < DIGITS; d += THREADS) {\n offsets[d] = hist[block * DIGITS + d];\n }\n workgroupBarrier();\n\n // stable local sort by the digit, two bits a split; thread t owns positions t ITEMS + k\n for (var bit = 0u; bit < 8u; bit += 2u) {\n if ((bits & (3u << bit)) == 0u) {\n continue;\n }\n var keys: array<u32, ITEMS>;\n var values: array<u32, ITEMS>;\n var before: array<u32, ITEMS>;\n var mine = vec4u(0u);\n for (var k = 0u; k < ITEMS; k++) {\n keys[k] = sk[t * ITEMS + k];\n values[k] = sv[t * ITEMS + k];\n let b = (keys[k] >> (params.shift + bit)) & 3u;\n before[k] = mine[b];\n mine[b] += 1u;\n }\n counts[t] = mine.x | (mine.y << 10u) | (mine.z << 20u);\n if (t == THREADS - 1u) {\n tail = counts[t];\n }\n workgroupBarrier();\n // separate reads and writes need only one barrier per scan step\n var source = 0u;\n for (var o = 1u; o < THREADS; o *= 2u) {\n var x = counts[source + t];\n if (t >= o) {\n x += counts[source + t - o];\n }\n let dest = source ^ THREADS;\n counts[dest + t] = x;\n workgroupBarrier();\n source = dest;\n }\n // prefixes stop at 1016 items, so no count carries into its neighbour\n let total = unpack(counts[source + THREADS - 2u], BLOCK - ITEMS) + unpack(tail, ITEMS);\n var prior = vec4u(0u);\n if (t > 0u) {\n prior = unpack(counts[source + t - 1u], t * ITEMS);\n }\n let base = vec4u(0u, total.x, total.x + total.y, total.x + total.y + total.z);\n for (var k = 0u; k < ITEMS; k++) {\n let b = (keys[k] >> (params.shift + bit)) & 3u;\n let p = base[b] + prior[b] + before[k];\n sk[p] = keys[k];\n sv[p] = values[k];\n }\n workgroupBarrier();\n }\n\n for (var k = 0u; k < ITEMS; k++) {\n let i = t * ITEMS + k;\n let d = digit(sk[i]);\n if (i == 0u || digit(sk[i - 1u]) != d) {\n starts[d] = i;\n }\n }\n workgroupBarrier();\n for (var k = 0u; k < ITEMS; k++) {\n let i = k * THREADS + t;\n if (i < valid) {\n let d = digit(sk[i]);\n let slot = offsets[d] + i - starts[d];\n data[params.dst + slot] = sk[i];\n data[params.dst + params.capacity + slot] = sv[i];\n }\n }\n workgroupBarrier();\n }\n}\n", P = 1024, _e = 64, ve = 256, F = 3, I = 256, ye = 32, be = class {
685
702
  keys;
686
703
  values;
687
704
  bytes;
@@ -694,21 +711,21 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
694
711
  constructor(e, t) {
695
712
  let { capacity: n } = t;
696
713
  this.keys = n * 8, this.values = n * 12;
697
- let r = Math.max(1, Math.ceil(n / _e));
714
+ let r = Math.max(1, Math.ceil(n / P));
698
715
  this.#a = r;
699
- let i = Math.ceil(r / ve), a = (t, n, r) => e.createBuffer({
716
+ let i = Math.ceil(r / _e), a = (t, n, r) => e.createBuffer({
700
717
  label: t,
701
718
  size: n,
702
719
  usage: r
703
- }), o = GPUBufferUsage.STORAGE, s = a("sort params", P * F, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST), c = a("sort histograms", r * ye * 4, o), u = a("sort chunk sums", (i * ye + 1) * 4, o), d = a("sort state", 12, o);
720
+ }), o = GPUBufferUsage.STORAGE, s = a("sort params", F * I, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST), c = a("sort histograms", r * ve * 4, o), u = a("sort chunk sums", (i * ve + 1) * 4, o), d = a("sort state", 12, o);
704
721
  this.#e = [
705
722
  s,
706
723
  c,
707
724
  u,
708
725
  d
709
726
  ], this.bytes = this.#e.reduce((e, t) => e + t.size, 0);
710
- let f = new Uint32Array(P * F / 4);
711
- for (let e = 0; e < P; e++) {
727
+ let f = new Uint32Array(F * I / 4);
728
+ for (let e = 0; e < F; e++) {
712
729
  let [t, r] = e % 2 ? [2 * n, 0] : [0, 2 * n];
713
730
  f.set([
714
731
  8 * e,
@@ -717,7 +734,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
717
734
  n,
718
735
  l.projected,
719
736
  l.drawArgs
720
- ], e * F / 4);
737
+ ], e * I / 4);
721
738
  }
722
739
  e.queue.writeBuffer(s, 0, f);
723
740
  let p = (e, t) => ({
@@ -757,8 +774,8 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
757
774
  ].map((e) => _(e, m));
758
775
  let v = (e) => ({
759
776
  buffer: s,
760
- offset: e * F,
761
- size: be
777
+ offset: e * I,
778
+ size: ye
762
779
  });
763
780
  this.#r = e.createBindGroup({
764
781
  label: "sort prepare",
@@ -777,7 +794,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
777
794
  resource: { buffer: t.work }
778
795
  }
779
796
  ]
780
- }), this.#i = Array.from({ length: P }, (n, r) => e.createBindGroup({
797
+ }), this.#i = Array.from({ length: F }, (n, r) => e.createBindGroup({
781
798
  label: `sort pass ${r}`,
782
799
  layout: m,
783
800
  entries: [
@@ -805,7 +822,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
805
822
  }));
806
823
  }
807
824
  encode(e, t, n, r) {
808
- let [i, a, o, s, c] = this.#n, l = Math.max(1, Math.min(this.#a, Math.ceil(t / _e))), u = Math.max(l / 16, (n || t) / _e), f = d(Math.min(Math.ceil(l / 4), Math.ceil(u))), p = Math.ceil(l / ve), m = e.beginComputePass({
825
+ let [i, a, o, s, c] = this.#n, l = Math.max(1, Math.min(this.#a, Math.ceil(t / P))), u = Math.max(l / 16, (n || t) / P), f = d(Math.min(Math.ceil(l / 4), Math.ceil(u))), p = Math.ceil(l / _e), m = e.beginComputePass({
809
826
  label: "sort",
810
827
  timestampWrites: r
811
828
  });
@@ -816,7 +833,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
816
833
  destroy() {
817
834
  for (let e of this.#e) e.destroy();
818
835
  }
819
- }, Se = class {
836
+ }, xe = class {
820
837
  ms = {};
821
838
  #e;
822
839
  #t;
@@ -867,14 +884,14 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
867
884
  destroy() {
868
885
  this.#t?.destroy(), this.#n?.destroy(), this.#r?.destroy();
869
886
  }
870
- }, Ce = [
887
+ }, Se = [
871
888
  "cull",
872
889
  "project",
873
890
  "sort",
874
891
  "raster",
875
892
  "heights",
876
893
  "post"
877
- ], I = {
894
+ ], L = {
878
895
  view: 0,
879
896
  proj: 16,
880
897
  viewProj: 32,
@@ -887,7 +904,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
887
904
  far: 81,
888
905
  lowPass: 82,
889
906
  clusters: 83
890
- }, we = class {
907
+ }, Ce = class {
891
908
  #e;
892
909
  #t;
893
910
  #n;
@@ -916,7 +933,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
916
933
  #T = 0;
917
934
  constructor(e, t, n, r) {
918
935
  let { device: i } = e;
919
- this.#e = i, this.#t = t, this.#n = e.format, this.#r = n, this.#i = T(i, r, n.slots), this.#a = new j(i, n, this.#i), this.#o = new le(i, n, this.#i), this.#s = new xe(i, this.#i), this.#c = new me(i, this.#i, this.#s), this.#u = new re(i, n), this.#d = new Se(i, e.timestamps, Ce), this.#S = i.createBuffer({
936
+ this.#e = i, this.#t = t, this.#n = e.format, this.#r = n, this.#i = T(i, r, n.slots), this.#a = new j(i, n, this.#i), this.#o = new le(i, n, this.#i), this.#s = new be(i, this.#i), this.#c = new me(i, this.#i, this.#s), this.#u = new re(i, n), this.#d = new xe(i, e.timestamps, Se), this.#S = i.createBuffer({
920
937
  label: "counts readback",
921
938
  size: (l.projected + 1) * 4,
922
939
  usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
@@ -1009,7 +1026,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
1009
1026
  }
1010
1027
  #D(e, t) {
1011
1028
  let n = this.#f, [r, i] = [this.#b, this.#x];
1012
- n.set(e.view, I.view), n.set(e.proj, I.proj), n.set(S(this.#m, e.proj, e.view), I.viewProj), n.set(t, I.planes), n.set(e.camera.subarray(0, 3), I.camera), n[I.time] = e.time, n.set([r, i], I.viewport), n.set([e.proj[0] * r / 2, e.proj[5] * i / 2], I.focal), n[I.near] = e.near, n[I.far] = e.far, n[I.lowPass] = f, this.#p[I.clusters] = this.#r.listed, this.#e.queue.writeBuffer(this.#i.uniforms, 0, n);
1029
+ n.set(e.view, L.view), n.set(e.proj, L.proj), n.set(S(this.#m, e.proj, e.view), L.viewProj), n.set(t, L.planes), n.set(e.camera.subarray(0, 3), L.camera), n[L.time] = e.time, n.set([r, i], L.viewport), n.set([e.proj[0] * r / 2, e.proj[5] * i / 2], L.focal), n[L.near] = e.near, n[L.far] = e.far, n[L.lowPass] = f, this.#p[L.clusters] = this.#r.listed, this.#e.queue.writeBuffer(this.#i.uniforms, 0, n);
1013
1030
  for (let { shade: t, slot: n } of this.#g) t.update(e, this.#h.subarray(n * 4, (n + t.uniforms) * 4));
1014
1031
  this.#g.length && this.#e.queue.writeBuffer(this.#i.plugins, 0, this.#h);
1015
1032
  }
@@ -1042,7 +1059,7 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
1042
1059
  this.#T = t[l.visible], this.#w = Math.min(t[l.projected], this.#i.capacity), e.unmap();
1043
1060
  }).catch(() => {}).finally(() => this.#C = !1);
1044
1061
  }
1045
- }, Te = [
1062
+ }, we = [
1046
1063
  [0, 1],
1047
1064
  [0, -1],
1048
1065
  [1, 1],
@@ -1050,180 +1067,25 @@ var ge = "// lsd radix sort, 8 bits a pass: block digit counts, a two-level scan
1050
1067
  [2, 0],
1051
1068
  [2, -1]
1052
1069
  ];
1053
- function Ee(e, t) {
1070
+ function Te(e, t) {
1054
1071
  let n = (e, n) => t[n * 4 + e];
1055
- return Te.forEach(([t, r], i) => {
1072
+ return we.forEach(([t, r], i) => {
1056
1073
  for (let a = 0; a < 4; a++) e[i * 4 + a] = r ? n(3, a) + r * n(t, a) : n(t, a);
1057
1074
  let a = Math.hypot(e[i * 4], e[i * 4 + 1], e[i * 4 + 2]) || 1;
1058
1075
  for (let t = 0; t < 4; t++) e[i * 4 + t] /= a;
1059
1076
  }), e;
1060
1077
  }
1061
- //#endregion
1062
- //#region src/engine/plan/footprint.ts
1063
- var De = [
1064
- 0,
1065
- 0,
1066
- 0
1067
- ];
1068
- function Oe(e, t, n, r) {
1069
- let i = (e * e + e * t + t * t + n * n + n * r + r * r) / 3;
1070
- return 1 + 15 * Math.max(0, 1 - i) ** 2;
1071
- }
1072
- function ke(e, t, n, r, i, a, o, s = 1, c = {
1073
- shown: !1,
1074
- pixels: 0,
1075
- weight: 1
1076
- }, l = De) {
1077
- let u = (e[0] + e[3]) / 2 - l[0], d = (e[1] + e[4]) / 2 - l[1], f = (e[2] + e[5]) / 2 - l[2], p = (e[3] - e[0]) / 2, m = (e[4] - e[1]) / 2, h = (e[5] - e[2]) / 2, g = t[0] * u + t[4] * d + t[8] * f + t[12], _ = t[1] * u + t[5] * d + t[9] * f + t[13], v = -(t[2] * u + t[6] * d + t[10] * f + t[14]), y = Math.abs(t[0]) * p + Math.abs(t[4]) * m + Math.abs(t[8]) * h, b = Math.abs(t[1]) * p + Math.abs(t[5]) * m + Math.abs(t[9]) * h, x = Math.abs(t[2]) * p + Math.abs(t[6]) * m + Math.abs(t[10]) * h;
1078
- if (c.pixels = 0, c.weight = 1, c.shown = !1, v + x < a || v - x > o) return c;
1079
- let S = Math.max(a, v - x), C = Math.max(a, v + x), w = i * n / r;
1080
- if (Math.abs(g) - y > s * w * C || Math.abs(_) - b > s * i * C) return c;
1081
- let T = Math.min((g - y) / S, (g - y) / C) / w, E = Math.max((g + y) / S, (g + y) / C) / w, D = Math.min((_ - b) / S, (_ - b) / C) / i, O = Math.max((_ + b) / S, (_ + b) / C) / i;
1082
- if (E < -s || T > s || O < -s || D > s) return c;
1083
- let k = Math.max(-s, T), A = Math.min(s, E), j = Math.max(-s, D), M = Math.min(s, O), N = (E - T) * (O - D), ee = (A - k) * (M - j);
1084
- return N <= 0 || ee <= 0 || (c.shown = E > -1 && T < 1 && O > -1 && D < 1, c.pixels = N * n * r / 4, c.weight = Oe(k, A, j, M)), c;
1085
- }
1086
- //#endregion
1087
- //#region src/engine/plan/cut.ts
1088
- var Ae = 1 / .8 ** 2, je = 1.1, Me = class {
1089
- #e = /* @__PURE__ */ new Map();
1090
- #t = /* @__PURE__ */ new Map();
1091
- #n = {
1092
- shown: !1,
1093
- pixels: 0,
1094
- weight: 1
1095
- };
1096
- plan(e, t, n, r) {
1097
- let { budget: i, capacity: a, now: o, predicted: s } = r, l = [], u = [], d = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map(), p = e.wanted(), m = /* @__PURE__ */ new Map(), h = new Pe(), g = 0, _ = 0, v = (e) => {
1098
- let t = e.page, r = t.row.count;
1099
- l.push(t), u.push(+!!e.shown | (e.near ? 2 : 0)), _ += c(r), e.shown && (g += r);
1100
- let i = this.#e.get(t.id), a = i !== void 0, s = e.pixels * e.weight, d = s / Math.max(1, r);
1101
- p.length && e.pixels > 0 && m.set(t.id, d);
1102
- let f = a && !e.shown && o - (this.#t.get(t.id) ?? -Infinity) <= 1500 ? i : null;
1103
- (r < s * (a ? Ae : 1) || f !== null) && Ne(t, n) && h.push({
1104
- page: t,
1105
- shown: e.shown,
1106
- held: f,
1107
- ratio: d
1108
- });
1109
- };
1110
- for (let n of e.roots) v(this.#r(n, t, s));
1111
- let y = !1;
1112
- for (; h.size;) {
1113
- let { page: e, shown: n, held: r } = h.pop(), l = [], u = 0;
1114
- for (let n = 0; n < e.children.length; n++) {
1115
- let i = e.children[n];
1116
- if (!i) continue;
1117
- let a = this.#r(i, t, s);
1118
- a.pixels <= 0 && !((r ?? 0) & 1 << n) || (l.push(a), u |= 1 << n);
1119
- }
1120
- if (!l.length) continue;
1121
- let p = 0, m = n ? -e.row.count : 0;
1122
- for (let e of l) p += c(e.page.row.count), e.shown && (m += e.page.row.count);
1123
- if (_ + p > a) break;
1124
- if (m > 0 && (y || g + m > i)) {
1125
- y = !0;
1126
- continue;
1127
- }
1128
- d.set(e.id, u);
1129
- let b = n ? o : this.#t.get(e.id);
1130
- b !== void 0 && f.set(e.id, b), n && (g -= e.row.count);
1131
- for (let e of l) v(e);
1132
- }
1133
- this.#e = d, this.#t = f;
1134
- let b = [], x = [], S = new Set(d.keys()), C = 0;
1135
- for (let e = 0; e < l.length; e++) {
1136
- let t = l[e];
1137
- d.has(t.id) || (b.push(t), S.add(t.id), n(t.id) || (u[e] & 1 && (C += t.row.count), x.push({
1138
- page: t,
1139
- near: !!(u[e] & 2)
1140
- })));
1141
- }
1142
- let w = [];
1143
- for (let e of p) {
1144
- let t = -1;
1145
- for (let n of e.over) t = Math.max(t, m.get(n.id) ?? -1);
1146
- t >= 0 && w.push({
1147
- wanted: e,
1148
- rank: t
1149
- });
1150
- }
1151
- return w.sort((e, t) => t.rank - e.rank), {
1152
- cut: b,
1153
- load: x.filter(({ near: e }) => e || !s && C === 0).map(({ page: e }) => e),
1154
- idx: w.map((e) => e.wanted),
1155
- keep: S,
1156
- planned: g,
1157
- missing: C
1158
- };
1159
- }
1160
- #r(e, t, n) {
1161
- let r = this.#i(e, t, je), i = r.shown, { pixels: a, weight: o } = r, s = i;
1162
- if (n) {
1163
- let t = this.#i(e, n, je);
1164
- t.pixels > a && ({pixels: a, weight: o} = t), s ||= t.shown;
1165
- }
1166
- return {
1167
- page: e,
1168
- shown: i,
1169
- near: s,
1170
- pixels: a,
1171
- weight: o
1172
- };
1173
- }
1174
- #i(e, t, n) {
1175
- let { width: r, height: i, tan: a, near: o, far: s } = t;
1176
- return ke(e.box, t.view, r, i, a, o, s, n, this.#n, t.origin);
1177
- }
1178
- };
1179
- function Ne(e, t) {
1180
- let { children: n } = e;
1181
- return e.g > 0 && t(e.id) && !n.includes(void 0) && n.some((e) => e !== null);
1182
- }
1183
- var Pe = class {
1184
- #e = [];
1185
- get size() {
1186
- return this.#e.length;
1187
- }
1188
- push(e) {
1189
- let t = this.#e;
1190
- t.push(e);
1191
- let n = t.length - 1;
1192
- for (; n > 0;) {
1193
- let r = n - 1 >> 1;
1194
- if (!L(e, t[r])) break;
1195
- t[n] = t[r], n = r;
1196
- }
1197
- t[n] = e;
1198
- }
1199
- pop() {
1200
- let e = this.#e, t = e[0], n = e.pop();
1201
- if (e.length) {
1202
- let t = 0;
1203
- for (; 2 * t + 1 < e.length;) {
1204
- let r = 2 * t + 1;
1205
- if (r + 1 < e.length && L(e[r + 1], e[r]) && r++, !L(e[r], n)) break;
1206
- e[t] = e[r], t = r;
1207
- }
1208
- e[t] = n;
1209
- }
1210
- return t;
1211
- }
1212
- };
1213
- function L(e, t) {
1214
- return e.shown === t.shown ? e.ratio > t.ratio : e.shown;
1215
- }
1216
- var R = .25, Fe = class {
1078
+ var R = .25, Ee = class {
1217
1079
  #e = /* @__PURE__ */ new Map();
1218
1080
  update(e, t, n, r) {
1219
- let i = this.#e, a = Ie(e, t), o = -1;
1220
- for (let [e, r] of i) t(r.page.id) ? (o = Math.max(o, r.level), !r.held && (r.start !== null && n - r.start >= 250 && (r.start = null), a.has(e) || (r.frozen = ze(r, n), r.start = null, r.held = !0))) : i.delete(e);
1081
+ let i = this.#e, a = De(e, t), o = -1;
1082
+ for (let [e, r] of i) t(r.page.id) ? (o = Math.max(o, r.level), !r.held && (r.start !== null && n - r.start >= 250 && (r.start = null), a.has(e) || (r.frozen = Ae(r, n), r.start = null, r.held = !0))) : i.delete(e);
1221
1083
  let s = /* @__PURE__ */ new Set();
1222
1084
  for (let [e, t] of a) {
1223
1085
  let r = i.get(e);
1224
1086
  if (r) r.held && (r.held = !1, r.frozen < 255 && (r.start = n - r.frozen / 255 * 250, s.add(r)));
1225
1087
  else {
1226
- let r = Le(t, e % 4, n);
1088
+ let r = Oe(t, e % 4, n);
1227
1089
  o = Math.max(o, r.level), i.set(e, r), s.add(r);
1228
1090
  }
1229
1091
  }
@@ -1266,7 +1128,7 @@ var R = .25, Fe = class {
1266
1128
  continue;
1267
1129
  }
1268
1130
  (t.held || t.start !== null) && (y = !0);
1269
- let r = ze(t, n);
1131
+ let r = Ae(t, n);
1270
1132
  if (!r) continue;
1271
1133
  let a = v.get(t.page.id);
1272
1134
  a ? a.factors = (a.factors | r << 8 * t.q) >>> 0 : v.set(t.page.id, {
@@ -1280,7 +1142,7 @@ var R = .25, Fe = class {
1280
1142
  };
1281
1143
  }
1282
1144
  };
1283
- function Ie(e, t) {
1145
+ function De(e, t) {
1284
1146
  let n = new Set(e), r = /* @__PURE__ */ new Set(), i = [];
1285
1147
  for (let t of e) for (let e = t; e && !r.has(e); e = e.parent) r.add(e), e.parent || i.push(e);
1286
1148
  let a = /* @__PURE__ */ new Map(), o = (e) => {
@@ -1292,7 +1154,7 @@ function Ie(e, t) {
1292
1154
  for (let e of i) t(e.id) && o(e);
1293
1155
  return a;
1294
1156
  }
1295
- function Le(t, n, r) {
1157
+ function Oe(t, n, r) {
1296
1158
  let i = e * 2 ** t.g, a = t.g - 1, o = 2 * (Math.round(t.unit.e / i) + t.row.i) + (n & 1), s = 2 * (Math.round(t.unit.n / i) + t.row.j) + (n >> 1);
1297
1159
  return {
1298
1160
  page: t,
@@ -1301,122 +1163,504 @@ function Le(t, n, r) {
1301
1163
  col: o,
1302
1164
  row: s,
1303
1165
  cell: z(a, o, s),
1304
- splats: Re(t.row, n),
1166
+ splats: ke(t.row, n),
1305
1167
  start: r,
1306
1168
  frozen: 0,
1307
1169
  held: !1
1308
1170
  };
1309
1171
  }
1310
1172
  var z = (e, t, n) => ((e + 1) * 2 ** 21 + t) * 2 ** 21 + n;
1311
- function Re({ count: e, quadrants: t }, n) {
1173
+ function ke({ count: e, quadrants: t }, n) {
1312
1174
  return (n < 3 ? t[n] : e) - (n > 0 ? t[n - 1] : 0);
1313
1175
  }
1314
- function ze(e, t) {
1176
+ function Ae(e, t) {
1315
1177
  return e.held ? e.frozen : e.start === null ? 255 : Math.min(255, Math.round((t - e.start) / 250 * 255));
1316
1178
  }
1317
1179
  //#endregion
1318
- //#region src/engine/quality.ts
1319
- var Be = 5e5, Ve = 2073600, He = [1, .85];
1320
- function Ue(e, t, n, r) {
1321
- return Math.min(n, Math.sqrt(Ve / Math.max(1, e * t))) * r;
1180
+ //#region src/engine/plan/footprint.ts
1181
+ var je = [
1182
+ 0,
1183
+ 0,
1184
+ 0
1185
+ ];
1186
+ function Me(e, t, n, r) {
1187
+ let i = (e * e + e * t + t * t + n * n + n * r + r * r) / 3;
1188
+ return 1 + 15 * Math.max(0, 1 - i) ** 2;
1322
1189
  }
1323
- function B(e, t) {
1324
- if (!e.length) return 0;
1325
- let n = [...e].sort((e, t) => e - t);
1326
- return n[Math.ceil(n.length * t) - 1];
1190
+ function Ne(e, t, n, r, i, a, o, s = 1, c = {
1191
+ shown: !1,
1192
+ pixels: 0,
1193
+ weight: 1
1194
+ }, l = je) {
1195
+ let u = (e[0] + e[3]) / 2 - l[0], d = (e[1] + e[4]) / 2 - l[1], f = (e[2] + e[5]) / 2 - l[2], p = (e[3] - e[0]) / 2, m = (e[4] - e[1]) / 2, h = (e[5] - e[2]) / 2, g = t[0] * u + t[4] * d + t[8] * f + t[12], _ = t[1] * u + t[5] * d + t[9] * f + t[13], v = -(t[2] * u + t[6] * d + t[10] * f + t[14]), y = Math.abs(t[0]) * p + Math.abs(t[4]) * m + Math.abs(t[8]) * h, b = Math.abs(t[1]) * p + Math.abs(t[5]) * m + Math.abs(t[9]) * h, x = Math.abs(t[2]) * p + Math.abs(t[6]) * m + Math.abs(t[10]) * h;
1196
+ if (c.pixels = 0, c.weight = 1, c.shown = !1, v + x < a || v - x > o) return c;
1197
+ let S = Math.max(a, v - x), C = Math.max(a, v + x), w = i * n / r;
1198
+ if (Math.abs(g) - y > s * w * C || Math.abs(_) - b > s * i * C) return c;
1199
+ let T = Math.min((g - y) / S, (g - y) / C) / w, E = Math.max((g + y) / S, (g + y) / C) / w, D = Math.min((_ - b) / S, (_ - b) / C) / i, O = Math.max((_ + b) / S, (_ + b) / C) / i;
1200
+ if (E < -s || T > s || O < -s || D > s) return c;
1201
+ let k = Math.max(-s, T), A = Math.min(s, E), j = Math.max(-s, D), M = Math.min(s, O), N = (E - T) * (O - D), ee = (A - k) * (M - j);
1202
+ return N <= 0 || ee <= 0 || (c.shown = E > -1 && T < 1 && O > -1 && D < 1, c.pixels = N * n * r / 4, c.weight = Me(k, A, j, M)), c;
1327
1203
  }
1328
- var We = class {
1329
- #e = 0;
1330
- #t = [];
1331
- #n = -Infinity;
1332
- #r = 0;
1333
- #i = 0;
1334
- get scale() {
1335
- return He[this.#e];
1336
- }
1337
- full() {
1338
- this.#e = 0, this.#a();
1339
- }
1340
- #a() {
1341
- this.#t = [], this.#r = 0, this.#i = 0, this.#n = -Infinity;
1342
- }
1343
- sample(e, t, n) {
1344
- if (!n || t - this.#n > Math.max(250, 2 * e) || !Number.isFinite(e) || e <= 0) return this.#a(), this.#n = t, !1;
1345
- let r = Math.max(0, t - this.#n);
1346
- if (this.#n = t, this.#t.push({
1347
- time: t,
1348
- ms: e
1349
- }), this.#t = this.#t.filter((n) => t - n.time <= Math.max(1e3, 2 * e)), t - this.#t[0].time < 300) return !1;
1350
- let i = B(this.#t.map((e) => e.ms), .9);
1351
- if (this.#r = i > 20 ? this.#r + r : 0, this.#i = i < 14 ? this.#i + r : 0, this.#r >= 2e3) {
1352
- if (this.#e < He.length - 1) this.#e++;
1353
- else return !1;
1354
- } else if (this.#i >= 5e3) {
1355
- if (this.#e > 0) this.#e--;
1356
- else return !1;
1357
- } else return !1;
1358
- return this.#a(), !0;
1359
- }
1360
- }, Ge = class {
1204
+ var B = /* @__PURE__ */ new Float64Array(6), Pe = {
1205
+ shown: !1,
1206
+ pixels: 0,
1207
+ weight: 1
1208
+ };
1209
+ function Fe(e, [t, n, r], i, a, o, s, c, l, u = je) {
1210
+ let d = r / 2, f = Math.max(0, t - e[0], n - e[1], e[3] - t - r, e[4] - n - r);
1211
+ B[2] = e[2], B[5] = e[5];
1212
+ let p = 0;
1213
+ for (let r = 0; r < 4; r++) {
1214
+ let m = t + (r & 1) * d, h = n + (r >> 1) * d;
1215
+ B[0] = Math.max(e[0], m - f), B[1] = Math.max(e[1], h - f), B[3] = Math.min(e[3], m + d + f), B[4] = Math.min(e[4], h + d + f), !(B[0] > B[3] || B[1] > B[4]) && Ne(B, i, a, o, s, c, l, 1, Pe, u).shown && (p |= 1 << r);
1216
+ }
1217
+ return p;
1218
+ }
1219
+ //#endregion
1220
+ //#region src/engine/world/tree.ts
1221
+ var V = (e, t, n) => (e * 256 + t) * 256 + n, Ie = class {
1222
+ roots = [];
1223
+ pages = /* @__PURE__ */ new Map();
1224
+ units = /* @__PURE__ */ new Map();
1361
1225
  #e;
1362
- #t = [];
1363
- #n = /* @__PURE__ */ new Set();
1364
- #r = /* @__PURE__ */ new Set();
1365
- #i = /* @__PURE__ */ new Set();
1366
- #a = /* @__PURE__ */ new Map();
1226
+ #t;
1227
+ #n = 0;
1228
+ #r = /* @__PURE__ */ new Map();
1229
+ #i = /* @__PURE__ */ new Map();
1367
1230
  constructor(e) {
1368
- this.#e = e;
1369
- }
1370
- want(e) {
1371
- this.#t = e, this.#n = new Set(e.map((e) => e.id));
1231
+ this.#e = new Set(e.roots.map(H)), this.#t = Math.max(...e.roots.map((e) => e.level));
1372
1232
  }
1373
- next() {
1374
- let e = [];
1375
- for (let t of this.#r) this.#i.has(t) || t.pages.some((e) => this.#n.has(e.id)) || (this.#i.add(t), e.push(t));
1376
- let t = [];
1377
- if (this.#r.size >= this.#e.concurrency) return {
1378
- start: t,
1379
- abort: e
1233
+ add(e, t) {
1234
+ let n = H(e), r = this.units.get(n);
1235
+ if (r) return r;
1236
+ let o = /* @__PURE__ */ new Map(), s = {
1237
+ level: e.level,
1238
+ e: e.e,
1239
+ n: e.n,
1240
+ idx: e,
1241
+ path: t,
1242
+ parent: null,
1243
+ pages: [],
1244
+ grid: o
1380
1245
  };
1381
- let n = /* @__PURE__ */ new Map();
1382
- for (let e of this.#t) {
1383
- if (this.#a.has(e.id)) continue;
1384
- let t = n.get(e.pack);
1385
- t ? t.push(e) : n.set(e.pack, [e]);
1246
+ for (let t of e.rows) {
1247
+ let n = t.box, r = {
1248
+ id: this.#n++,
1249
+ unit: s,
1250
+ row: t,
1251
+ g: t.g,
1252
+ box: Float64Array.of(e.e + n[0], e.n + n[1], n[2], e.e + n[3], e.n + n[4], n[5]),
1253
+ parent: null,
1254
+ children: [
1255
+ null,
1256
+ null,
1257
+ null,
1258
+ null
1259
+ ]
1260
+ };
1261
+ this.pages.set(r.id, r), s.pages.push(r), o.set(V(t.g, t.i, t.j), r);
1386
1262
  }
1387
- for (let e of n.values()) e.sort((e, t) => e.offset - t.offset);
1388
- for (let e of this.#t) {
1389
- if (this.#r.size >= this.#e.concurrency) break;
1390
- if (this.#a.has(e.id)) continue;
1391
- let r = this.#o(e, n.get(e.pack));
1392
- for (let e of r.pages) this.#a.set(e.id, r);
1393
- this.#r.add(r), t.push(r);
1263
+ this.units.set(n, s), this.#r.delete(n);
1264
+ for (let t = e.level + 1; t <= this.#t; t++) {
1265
+ let n = H(U(e, t));
1266
+ this.#i.set(n, (this.#i.get(n) ?? 0) + 1);
1394
1267
  }
1395
- return {
1396
- start: t,
1397
- abort: e
1398
- };
1399
- }
1400
- done(e) {
1401
- if (this.#r.delete(e)) {
1402
- this.#i.delete(e);
1403
- for (let t of e.pages) this.#a.get(t.id) === e && this.#a.delete(t.id);
1268
+ let c = new Set(e.rows.map((e) => e.g)), l = Math.max(...c), u = e.level === 0 ? 0 : e.level + 1, d = new Set(e.children.map((e) => e.quadrant));
1269
+ for (let e of s.pages) {
1270
+ let { g: t, i: n, j: r } = e.row;
1271
+ c.has(t + 1) && (e.parent = o.get(V(t + 1, n >> 1, r >> 1)) ?? null);
1272
+ for (let i = 0; i < 4; i++) {
1273
+ let a = 2 * n + (i & 1), s = 2 * r + (i >> 1);
1274
+ c.has(t - 1) ? e.children[i] = o.get(V(t - 1, a, s)) ?? null : t === u && t > 0 && (e.children[i] = d.has((a >> 3) + 2 * (s >> 3)) ? void 0 : null);
1275
+ }
1404
1276
  }
1405
- }
1406
- get queued() {
1407
- let e = 0;
1408
- for (let t of this.#n) this.#a.has(t) || e++;
1409
- return e;
1410
- }
1411
- get loading() {
1412
- return this.#a.size;
1413
- }
1414
- #o(e, t) {
1415
- let { gap: n, span: r } = this.#e, i = Math.floor(e.offset / r), a = t.indexOf(e), o = [e], s = e.offset, c = e.offset + e.length;
1416
- for (let e = a + 1; e < t.length; e++) {
1417
- let a = t[e];
1418
- if (this.#a.has(a.id)) continue;
1419
- let l = Math.max(c, a.offset + a.length);
1277
+ if (this.#e.has(n)) for (let e of s.pages) e.g === l && this.roots.push(e);
1278
+ else {
1279
+ let t = this.units.get(H(U(e, e.level + 1)));
1280
+ t && this.#o(t, s);
1281
+ }
1282
+ let f = a(s.idx);
1283
+ for (let { key: t } of f) {
1284
+ let n = t.level === e.level - 1 ? this.units.get(H(t)) : void 0;
1285
+ n && this.#o(s, n);
1286
+ }
1287
+ for (let { key: e, hash: t } of f) {
1288
+ let n = H(e);
1289
+ if (this.units.has(n)) continue;
1290
+ let r = this.#r.get(n);
1291
+ if (r && r.from <= s.level) continue;
1292
+ let a = this.#s(s, e), o = {
1293
+ ...e,
1294
+ path: i(e, t, "idx"),
1295
+ over: a
1296
+ };
1297
+ this.#r.set(n, {
1298
+ wanted: o,
1299
+ from: s.level
1300
+ });
1301
+ }
1302
+ return s;
1303
+ }
1304
+ wanted() {
1305
+ return Array.from(this.#r.values(), (e) => e.wanted);
1306
+ }
1307
+ ground(e, t) {
1308
+ for (let n of this.roots) {
1309
+ let [r, i, a] = W(n);
1310
+ if (e < r || e >= r + a || t < i || t >= i + a) continue;
1311
+ let o = n;
1312
+ for (;;) {
1313
+ let n = +(e >= r + a / 2), s = +(t >= i + a / 2), c = o.children[n + 2 * s];
1314
+ if (!c) return o.box[2];
1315
+ o = c, [r, i, a] = W(o);
1316
+ }
1317
+ }
1318
+ }
1319
+ async load(e, t, n) {
1320
+ for (;;) {
1321
+ let r = this.wanted().filter((n) => Le(n, e, t));
1322
+ if (r.length === 0) return;
1323
+ (await Promise.all(r.map((e) => n(e.path)))).forEach((e, t) => this.add(e, r[t].path));
1324
+ }
1325
+ }
1326
+ prune(e) {
1327
+ let t = [], n = [...this.units.values()].sort((e, t) => e.level - t.level);
1328
+ for (let r of n) {
1329
+ let n = H(r);
1330
+ if (this.#e.has(n) || this.#i.has(n) || r.pages.some((t) => e.has(t.id))) continue;
1331
+ for (let e of r.pages) this.pages.delete(e.id), t.push(e.id);
1332
+ let i = r.parent;
1333
+ if (i) {
1334
+ let e = 250 * 2 ** r.level, t = (r.e - i.e) / e, n = (r.n - i.n) / e, a = i.idx.children.some((e) => e.quadrant === t + 2 * n);
1335
+ for (let e of this.#s(i, r)) e.children.fill(a ? void 0 : null);
1336
+ }
1337
+ this.units.delete(n);
1338
+ for (let e = r.level + 1; e <= this.#t; e++) {
1339
+ let t = H(U(r, e)), n = this.#i.get(t) - 1;
1340
+ n ? this.#i.set(t, n) : this.#i.delete(t);
1341
+ }
1342
+ for (let { key: e } of a(r.idx)) this.#a(e);
1343
+ this.#a(r);
1344
+ }
1345
+ return t;
1346
+ }
1347
+ #a(e) {
1348
+ let t = H(e);
1349
+ this.#r.delete(t);
1350
+ for (let n = e.level + 1; n <= e.level + 3; n++) {
1351
+ let r = this.units.get(H(U(e, n)));
1352
+ if (!r) continue;
1353
+ let o = a(r.idx).find((e) => H(e.key) === t);
1354
+ if (!o) continue;
1355
+ let s = {
1356
+ level: e.level,
1357
+ e: e.e,
1358
+ n: e.n,
1359
+ path: i(o.key, o.hash, "idx"),
1360
+ over: this.#s(r, e)
1361
+ };
1362
+ this.#r.set(t, {
1363
+ wanted: s,
1364
+ from: r.level
1365
+ });
1366
+ return;
1367
+ }
1368
+ }
1369
+ #o(e, t) {
1370
+ t.parent = e;
1371
+ let n = t.level + 1, r = 250 * 2 ** t.level, i = (t.e - e.e) / r, a = (t.n - e.n) / r, o = e.grid;
1372
+ for (let n of this.#s(e, t)) n.children.fill(null);
1373
+ for (let e of t.pages) {
1374
+ if (e.g !== n) continue;
1375
+ let t = 8 * i + e.row.i, r = 8 * a + e.row.j, s = o.get(V(n + 1, t >> 1, r >> 1)) ?? null;
1376
+ e.parent = s, s && (s.children[(t & 1) + 2 * (r & 1)] = e);
1377
+ }
1378
+ }
1379
+ #s(t, n) {
1380
+ let r = t.level + 1, i = e * 2 ** r, a = (n.e - t.e) / i, o = (n.n - t.n) / i, s = 250 * 2 ** n.level / i, c = [];
1381
+ for (let e = o; e < o + s; e++) for (let n = a; n < a + s; n++) {
1382
+ let i = t.grid.get(V(r, n, e));
1383
+ i && c.push(i);
1384
+ }
1385
+ return c;
1386
+ }
1387
+ };
1388
+ function H(e) {
1389
+ return `${e.level}/${e.e}/${e.n}`;
1390
+ }
1391
+ function U(e, t) {
1392
+ let n = 250 * 2 ** t;
1393
+ return {
1394
+ level: t,
1395
+ e: Math.floor(e.e / n) * n,
1396
+ n: Math.floor(e.n / n) * n
1397
+ };
1398
+ }
1399
+ function Le(e, t, n) {
1400
+ let r = 250 * 2 ** e.level;
1401
+ return t >= e.e && t < e.e + r && n >= e.n && n < e.n + r;
1402
+ }
1403
+ function W(t) {
1404
+ let n = e * 2 ** t.g;
1405
+ return [
1406
+ t.unit.e + t.row.i * n,
1407
+ t.unit.n + t.row.j * n,
1408
+ n
1409
+ ];
1410
+ }
1411
+ //#endregion
1412
+ //#region src/engine/plan/cut.ts
1413
+ var Re = 1 / .8 ** 2, ze = 1.1, Be = class {
1414
+ #e = /* @__PURE__ */ new Map();
1415
+ #t = /* @__PURE__ */ new Map();
1416
+ #n = {
1417
+ shown: !1,
1418
+ pixels: 0,
1419
+ weight: 1
1420
+ };
1421
+ plan(e, t, n, r) {
1422
+ let { budget: i, capacity: a, now: o, predicted: s } = r, l = [], u = [], d = [], f = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), m = e.wanted(), h = /* @__PURE__ */ new Map(), g = new Ue(), _ = 0, v = 0, y = (e) => {
1423
+ let t = e.page, r = t.row.count;
1424
+ l.push(t), u.push(e.visible), d.push(e.near), v += c(r), _ += e.visible;
1425
+ let i = this.#e.get(t.id), a = i !== void 0, s = e.pixels * e.weight, f = s / Math.max(1, r);
1426
+ m.length && e.pixels > 0 && h.set(t.id, f);
1427
+ let p = a && !e.shown && o - (this.#t.get(t.id) ?? -Infinity) <= 1500 ? i : null;
1428
+ (r < s * (a ? Re : 1) || p !== null) && He(t, n) && g.push({
1429
+ page: t,
1430
+ shown: e.shown,
1431
+ held: p,
1432
+ visible: e.visible,
1433
+ ratio: f
1434
+ });
1435
+ };
1436
+ for (let n of e.roots) y(this.#r(n, t, s));
1437
+ let b = [];
1438
+ for (; g.size;) {
1439
+ let e = g.pop(), { page: n, shown: r, held: l } = e, u = [], d = 0;
1440
+ for (let e = 0; e < n.children.length; e++) {
1441
+ let r = n.children[e];
1442
+ if (!r) continue;
1443
+ let i = this.#r(r, t, s);
1444
+ i.pixels <= 0 && !((l ?? 0) & 1 << e) || (u.push(i), d |= 1 << e);
1445
+ }
1446
+ if (!u.length) continue;
1447
+ let m = 0, h = -e.visible;
1448
+ for (let e of u) m += c(e.page.row.count), h += e.visible;
1449
+ if (v + m > a) break;
1450
+ if (h > 0 && (b.length || _ + h > i)) {
1451
+ b.push(e);
1452
+ continue;
1453
+ }
1454
+ if (h < 0) for (let e of b.splice(0)) g.push(e);
1455
+ f.set(n.id, d);
1456
+ let x = r ? o : this.#t.get(n.id);
1457
+ x !== void 0 && p.set(n.id, x), _ -= e.visible;
1458
+ for (let e of u) y(e);
1459
+ }
1460
+ this.#e = f, this.#t = p;
1461
+ let x = [], S = [], C = new Set(f.keys()), w = 0;
1462
+ for (let e = 0; e < l.length; e++) {
1463
+ let t = l[e];
1464
+ f.has(t.id) || (x.push(t), C.add(t.id), n(t.id) || (w += u[e], S.push({
1465
+ page: t,
1466
+ near: d[e]
1467
+ })));
1468
+ }
1469
+ let T = [];
1470
+ for (let e of m) {
1471
+ let t = -1;
1472
+ for (let n of e.over) t = Math.max(t, h.get(n.id) ?? -1);
1473
+ t >= 0 && T.push({
1474
+ wanted: e,
1475
+ rank: t
1476
+ });
1477
+ }
1478
+ return T.sort((e, t) => t.rank - e.rank), {
1479
+ cut: x,
1480
+ load: S.filter(({ near: e }) => e || !s && w === 0).map(({ page: e }) => e),
1481
+ idx: T.map((e) => e.wanted),
1482
+ keep: C,
1483
+ planned: _,
1484
+ missing: w
1485
+ };
1486
+ }
1487
+ #r(e, t, n) {
1488
+ let r = this.#i(e, t, ze), i = r.shown, { pixels: a, weight: o } = r, s = i;
1489
+ if (n) {
1490
+ let t = this.#i(e, n, ze);
1491
+ t.pixels > a && ({pixels: a, weight: o} = t), s ||= t.shown;
1492
+ }
1493
+ return {
1494
+ page: e,
1495
+ shown: i,
1496
+ near: s,
1497
+ visible: i ? Ve(e, t) : 0,
1498
+ pixels: a,
1499
+ weight: o
1500
+ };
1501
+ }
1502
+ #i(e, t, n) {
1503
+ let { width: r, height: i, tan: a, near: o, far: s } = t;
1504
+ return Ne(e.box, t.view, r, i, a, o, s, n, this.#n, t.origin);
1505
+ }
1506
+ };
1507
+ function Ve(e, t) {
1508
+ let { width: n, height: r, tan: i, near: a, far: o, origin: s } = t, c = Fe(e.box, W(e), t.view, n, r, i, a, o, s), l = 0;
1509
+ for (let t = 0; t < 4; t++) c & 1 << t && (l += ke(e.row, t));
1510
+ return l;
1511
+ }
1512
+ function He(e, t) {
1513
+ let { children: n } = e;
1514
+ return e.g > 0 && t(e.id) && !n.includes(void 0) && n.some((e) => e !== null);
1515
+ }
1516
+ var Ue = class {
1517
+ #e = [];
1518
+ get size() {
1519
+ return this.#e.length;
1520
+ }
1521
+ push(e) {
1522
+ let t = this.#e;
1523
+ t.push(e);
1524
+ let n = t.length - 1;
1525
+ for (; n > 0;) {
1526
+ let r = n - 1 >> 1;
1527
+ if (!G(e, t[r])) break;
1528
+ t[n] = t[r], n = r;
1529
+ }
1530
+ t[n] = e;
1531
+ }
1532
+ pop() {
1533
+ let e = this.#e, t = e[0], n = e.pop();
1534
+ if (e.length) {
1535
+ let t = 0;
1536
+ for (; 2 * t + 1 < e.length;) {
1537
+ let r = 2 * t + 1;
1538
+ if (r + 1 < e.length && G(e[r + 1], e[r]) && r++, !G(e[r], n)) break;
1539
+ e[t] = e[r], t = r;
1540
+ }
1541
+ e[t] = n;
1542
+ }
1543
+ return t;
1544
+ }
1545
+ };
1546
+ function G(e, t) {
1547
+ return e.shown === t.shown ? e.ratio > t.ratio : e.shown;
1548
+ }
1549
+ //#endregion
1550
+ //#region src/engine/quality.ts
1551
+ var We = 5e5, Ge = 2073600, Ke = [1, .85], qe = 30, Je = 55, Ye = 2e3, Xe = 5e3, Ze = 5e5, Qe = .9;
1552
+ function $e(e, t, n, r) {
1553
+ return Math.min(n, Math.sqrt(Ge / Math.max(1, e * t))) * r;
1554
+ }
1555
+ function et(e, t) {
1556
+ if (!e.length) return 0;
1557
+ let n = [...e].sort((e, t) => e - t);
1558
+ return n[Math.ceil(n.length * t) - 1];
1559
+ }
1560
+ var tt = class {
1561
+ #e = 0;
1562
+ #t;
1563
+ #n;
1564
+ #r;
1565
+ #i = [];
1566
+ #a = -Infinity;
1567
+ #o = 0;
1568
+ #s = 0;
1569
+ constructor(e, [t, n] = [e, e]) {
1570
+ this.#n = t, this.#r = Math.max(t, n), this.#t = Math.min(this.#r, Math.max(this.#n, e));
1571
+ }
1572
+ get scale() {
1573
+ return Ke[this.#e];
1574
+ }
1575
+ get budget() {
1576
+ return this.#t;
1577
+ }
1578
+ full() {
1579
+ this.#e = 0, this.#c();
1580
+ }
1581
+ #c() {
1582
+ this.#i = [], this.#o = 0, this.#s = 0, this.#a = -Infinity;
1583
+ }
1584
+ sample(e, t, n, r) {
1585
+ if (!n || t - this.#a > Math.max(250, 2 * e) || !Number.isFinite(e) || e <= 0) return this.#c(), this.#a = t, !1;
1586
+ let i = Math.max(0, t - this.#a);
1587
+ if (this.#a = t, this.#i.push({
1588
+ time: t,
1589
+ ms: e
1590
+ }), this.#i = this.#i.filter((n) => t - n.time <= Math.max(1e3, 2 * e)), t - this.#i[0].time < 300) return !1;
1591
+ let a = this.#i.reduce((e, t) => e + t.ms, 0) / this.#i.length;
1592
+ if (this.#o = a > 1e3 / qe ? this.#o + i : 0, this.#s = a <= 1e3 / Je ? this.#s + i : 0, this.#o >= Ye) {
1593
+ let e = Math.floor(Math.min(.9, 1e3 / qe / a) * this.#t);
1594
+ if (this.#t > this.#n) this.#t = Math.max(this.#n, e);
1595
+ else if (this.#e < Ke.length - 1) this.#e++;
1596
+ else return !1;
1597
+ } else if (this.#s >= Xe) {
1598
+ if (this.#e > 0) this.#e--;
1599
+ else if (this.#t < this.#r && r >= Qe * this.#t) this.#t = Math.min(this.#r, this.#t + Ze);
1600
+ else return !1;
1601
+ } else return !1;
1602
+ return this.#c(), !0;
1603
+ }
1604
+ }, nt = class {
1605
+ #e;
1606
+ #t = [];
1607
+ #n = /* @__PURE__ */ new Set();
1608
+ #r = /* @__PURE__ */ new Set();
1609
+ #i = /* @__PURE__ */ new Set();
1610
+ #a = /* @__PURE__ */ new Map();
1611
+ constructor(e) {
1612
+ this.#e = e;
1613
+ }
1614
+ want(e) {
1615
+ this.#t = e, this.#n = new Set(e.map((e) => e.id));
1616
+ }
1617
+ next() {
1618
+ let e = [];
1619
+ for (let t of this.#r) this.#i.has(t) || t.pages.some((e) => this.#n.has(e.id)) || (this.#i.add(t), e.push(t));
1620
+ let t = [];
1621
+ if (this.#r.size >= this.#e.concurrency) return {
1622
+ start: t,
1623
+ abort: e
1624
+ };
1625
+ let n = /* @__PURE__ */ new Map();
1626
+ for (let e of this.#t) {
1627
+ if (this.#a.has(e.id)) continue;
1628
+ let t = n.get(e.pack);
1629
+ t ? t.push(e) : n.set(e.pack, [e]);
1630
+ }
1631
+ for (let e of n.values()) e.sort((e, t) => e.offset - t.offset);
1632
+ for (let e of this.#t) {
1633
+ if (this.#r.size >= this.#e.concurrency) break;
1634
+ if (this.#a.has(e.id)) continue;
1635
+ let r = this.#o(e, n.get(e.pack));
1636
+ for (let e of r.pages) this.#a.set(e.id, r);
1637
+ this.#r.add(r), t.push(r);
1638
+ }
1639
+ return {
1640
+ start: t,
1641
+ abort: e
1642
+ };
1643
+ }
1644
+ done(e) {
1645
+ if (this.#r.delete(e)) {
1646
+ this.#i.delete(e);
1647
+ for (let t of e.pages) this.#a.get(t.id) === e && this.#a.delete(t.id);
1648
+ }
1649
+ }
1650
+ get queued() {
1651
+ let e = 0;
1652
+ for (let t of this.#n) this.#a.has(t) || e++;
1653
+ return e;
1654
+ }
1655
+ get loading() {
1656
+ return this.#a.size;
1657
+ }
1658
+ #o(e, t) {
1659
+ let { gap: n, span: r } = this.#e, i = Math.floor(e.offset / r), a = t.indexOf(e), o = [e], s = e.offset, c = e.offset + e.length;
1660
+ for (let e = a + 1; e < t.length; e++) {
1661
+ let a = t[e];
1662
+ if (this.#a.has(a.id)) continue;
1663
+ let l = Math.max(c, a.offset + a.length);
1420
1664
  if (a.offset - c > n || l - s > r || Math.floor(a.offset / r) !== i) break;
1421
1665
  o.push(a), c = l;
1422
1666
  }
@@ -1436,12 +1680,12 @@ var We = class {
1436
1680
  pages: o
1437
1681
  };
1438
1682
  }
1439
- }, Ke = {
1683
+ }, rt = {
1440
1684
  idx: 8,
1441
1685
  concurrency: 8,
1442
1686
  gap: 65536,
1443
1687
  span: 2097152
1444
- }, qe = 1e3, Je = 3e4, Ye = 1e3, Xe = 2e3, Ze = class {
1688
+ }, it = 1e3, at = 3e4, ot = 1e3, st = 2e3, ct = class {
1445
1689
  #e;
1446
1690
  #t;
1447
1691
  #n;
@@ -1471,8 +1715,8 @@ var We = class {
1471
1715
  #y = -Infinity;
1472
1716
  #b = !1;
1473
1717
  #x = !1;
1474
- constructor(e, t, n, r, i, a = Ke) {
1475
- this.#e = e, this.#t = t, this.#n = n, this.#r = r, this.#i = i, this.#a = a, this.#o = new Ge(a);
1718
+ constructor(e, t, n, r, i, a = rt) {
1719
+ this.#e = e, this.#t = t, this.#n = n, this.#r = r, this.#i = i, this.#a = a, this.#o = new nt(a);
1476
1720
  }
1477
1721
  update(e, t, n) {
1478
1722
  if (!this.#x) {
@@ -1539,7 +1783,7 @@ var We = class {
1539
1783
  let n = this.#e.pages.get(e);
1540
1784
  if (!n) return;
1541
1785
  let { keep: r } = this.#s, i = this.#c;
1542
- this.#n.add(e, t, [n.unit.e, n.unit.n], (e) => r.has(e) || i.has(e)) ? (this.#g.add(e), this.#m.delete(e) && this.#j(e)) : (this.#m.set(e, this.#u + Ye), this.#A(e, Ye)), this.#r();
1786
+ this.#n.add(e, t, [n.unit.e, n.unit.n], (e) => r.has(e) || i.has(e)) ? (this.#g.add(e), this.#m.delete(e) && this.#j(e)) : (this.#m.set(e, this.#u + ot), this.#A(e, ot)), this.#r();
1543
1787
  };
1544
1788
  #E(e) {
1545
1789
  this.#x || (this.#f.delete(e), this.#k(e.pack), this.#l = this.#l.filter((e) => this.#N(e.id, e.pack)), this.#o.want(this.#l), this.#o.done(e), this.#w(), this.#r());
@@ -1553,7 +1797,7 @@ var We = class {
1553
1797
  this.#b = !0, this.#i();
1554
1798
  return;
1555
1799
  }
1556
- let n = this.#p.get(e), r = n ? Math.min(2 * n.delay, Je) : qe;
1800
+ let n = this.#p.get(e), r = n ? Math.min(2 * n.delay, at) : it;
1557
1801
  this.#p.set(e, {
1558
1802
  delay: r,
1559
1803
  until: this.#u + r
@@ -1574,11 +1818,11 @@ var We = class {
1574
1818
  }
1575
1819
  #M() {
1576
1820
  let e = this.#u;
1577
- if (e - this.#y < Xe) return;
1821
+ if (e - this.#y < st) return;
1578
1822
  this.#y = e;
1579
1823
  for (let e of this.#g) this.#n.holds(e) || this.#g.delete(e);
1580
1824
  for (let [t, n] of this.#m) n <= e && this.#m.delete(t);
1581
- for (let [t, n] of this.#_) e - n >= Xe && this.#_.delete(t);
1825
+ for (let [t, n] of this.#_) e - n >= st && this.#_.delete(t);
1582
1826
  let { keep: t } = this.#s;
1583
1827
  if (this.#e.pages.size <= 2 * (this.#g.size + t.size)) return;
1584
1828
  let n = /* @__PURE__ */ new Set([
@@ -1603,7 +1847,7 @@ var We = class {
1603
1847
  };
1604
1848
  //#endregion
1605
1849
  //#region src/engine/stream/remote.ts
1606
- function Qe(e, t) {
1850
+ function lt(e, t) {
1607
1851
  let n = /* @__PURE__ */ new Map();
1608
1852
  return (r) => {
1609
1853
  if ("unfetch" in r) return void n.get(r.unfetch)?.abort();
@@ -1626,29 +1870,29 @@ function Qe(e, t) {
1626
1870
  }
1627
1871
  //#endregion
1628
1872
  //#region src/engine/stream/worker.ts?worker&inline
1629
- var $e = "(function(){var e=class extends Error{name=`Missing`};let t=async(t,n,r)=>{let i=await fetch(t,n?{signal:r,headers:{Range:`bytes=${n[0]}-${n[1]-1}`}}:{signal:r});if(i.status===404)throw new e(`http 404 for ${t}`);if(i.status!==200&&i.status!==206)throw Error(`http ${i.status} for ${t}`);let a=new Uint8Array(await i.arrayBuffer());return n&&i.status===200?a.slice(n[0],n[1]):a},n=.28209479177387814,r=15.625,i=`GMIX`,a={position:0,alpha:6,rotationIndex:7,rotation:8,scale:11,dc:14,label:17},o={origin:0,step:3,scaleMin:6,scaleMax:9,dcMin:12,dcMax:15,shScale:18};async function s(e,t){let n=new ReadableStream({start(t){t.enqueue(e),t.close()}}).pipeThrough(t);return new Uint8Array(await new Response(n).arrayBuffer())}function c(e){return s(e,new DecompressionStream(`deflate-raw`))}function l(e){return s(e,new CompressionStream(`deflate-raw`))}async function u(e,t,n){let r=new DataView(e.buffer,e.byteOffset,e.byteLength);if(e.length<8||String.fromCharCode(...e.subarray(0,4))!==t)throw Error(`not a ${n} file`);let i=r.getUint16(4,!0);if(i!==1)throw Error(`${n} version ${i}; narcissus reads 1`);let a;try{a=await c(e.subarray(8))}catch{throw Error(`${n} body is not a valid deflate stream`)}return new DataView(a.buffer,a.byteOffset,a.byteLength)}function d(e,t){let n=``;for(let r=0;r<8;r++){let i=e.getUint8(t+r);n+=i.toString(16).padStart(2,`0`)}return n}function f(e,t){let n=/* @__PURE__ */ new Float32Array(6);for(let r=0;r<6;r++)n[r]=e.getFloat32(t+20+4*r,!0);return{g:e.getUint8(t),i:e.getUint8(t+1),j:e.getUint8(t+2),count:e.getUint32(t+4,!0),quadrants:[e.getUint32(t+8,!0),e.getUint32(t+12,!0),e.getUint32(t+16,!0)],box:n,offset:e.getUint32(t+44,!0),length:e.getUint32(t+48,!0)}}async function p(e){let t=await u(e,i,`.idx`);if(t.byteLength<40)throw Error(`.idx header is cut off`);let n=t.getUint16(32,!0),r=t.getUint8(34),a=t.getUint16(36,!0),o=40+n*52+r*9+a*11;if(t.byteLength!==o)throw Error(`.idx holds ${t.byteLength} bytes, its counts need ${o}`);let s=d(t,24),c={level:t.getUint8(0),e:t.getInt32(4,!0),n:t.getInt32(8,!0),pack:d(t,12),packSize:t.getUint32(20,!0),col:s===`0000000000000000`?null:s,rows:[],children:[],descendants:[]},l=40;for(let e=0;e<n;e++,l+=52)c.rows.push(f(t,l));for(let e=0;e<r;e++,l+=9)c.children.push({quadrant:t.getUint8(l),idx:d(t,l+1)});for(let e=0;e<a;e++,l+=11)c.descendants.push({depth:t.getUint8(l),column:t.getUint8(l+1),row:t.getUint8(l+2),idx:d(t,l+3)});return c}function m(e,t,n){return`u/${e.level}/${e.e}/${e.n}.${t}.${n}`}let h=/(?:^|\\/)u\\/(\\d+)\\/(-?\\d+)\\/(-?\\d+)\\.([0-9a-f]{16})\\.(?:idx|pack|col)$/;function g(e){let t=h.exec(e);return t?{level:Number(t[1]),e:Number(t[2]),n:Number(t[3]),hash:t[4]}:null}async function _(e){let t=await u(e,`GMCL`,`.col`);if(t.byteLength<60)throw Error(`.col header is cut off`);let n=t.getUint16(0,!0),r=60+12*n*n;if(t.byteLength!==r)throw Error(`.col holds ${t.byteLength} bytes, ${n} nodes need ${r}`);let i=new Float32Array(12*n*n),a=60;for(let e=0;e<12;e++){let r=t.getFloat32(12+4*e,!0)/127;for(let o=0;o<n;o++){let s=0;for(let c=0;c<n;c++,a++)s=s+t.getInt8(a)<<24>>24,i[(e*n+o)*n+c]=s*r}}return{n,pitch:t.getFloat32(4,!0),margin:t.getFloat32(8,!0),values:i}}function v(e,t,n,r){let{n:i,pitch:a,margin:o,values:s}=e,c=i-1,l=Math.min(Math.max((t+o)/a-.5,0),c),u=Math.min(Math.max((n+o)/a-.5,0),c),d=Math.min(Math.floor(l),Math.max(c-1,0)),f=Math.min(Math.floor(u),Math.max(c-1,0)),p=Math.min(d+1,c),m=Math.min(f+1,c),h=l-d,g=u-f,_=(1-h)*(1-g),v=h*(1-g),y=(1-h)*g,b=h*g,x=i*i;for(let e=0,t=0;e<12;e++,t+=x)r[e]=s[t+f*i+d]*_+s[t+f*i+p]*v+s[t+m*i+d]*y+s[t+m*i+p]*b;return r}let y=Float64Array.from({length:256},(e,t)=>(t/255-.5)*Math.SQRT2);function b(e,t,n,r,i){let a=y[t],o=y[n],s=y[r];i[e]=Math.sqrt(Math.max(0,1-a*a-o*o-s*s)),i[e>0?0:1]=a,i[e>1?1:2]=o,i[e>2?2:3]=s}function x(e,t,n,r,i){let a=e[0],o=e[1],s=e[2],c=e[3],l=t*t,u=n*n,d=r*r,f=1-2*(s*s+c*c),p=2*(o*s-a*c),m=2*(o*c+a*s),h=2*(o*s+a*c),g=1-2*(o*o+c*c),_=2*(s*c-a*o),v=2*(o*c-a*s),y=2*(s*c+a*o),b=1-2*(o*o+s*s);i[0]=3*Math.sqrt(f*f*l+p*p*u+m*m*d),i[1]=3*Math.sqrt(h*h*l+g*g*u+_*_*d),i[2]=3*Math.sqrt(v*v*l+y*y*u+b*b*d)}function S(e){if(e.length<128)throw Error(`page payload is shorter than its header`);return new DataView(e.buffer,e.byteOffset,e.byteLength)}function C(e){let t=S(e),n=t.getUint32(0,!0),r=t.getUint16(16,!0),i=128+(17+(r>0?2:0))*n,a=i+r*9;if(e.length!==a)throw Error(`page payload holds ${e.length} bytes, its header needs ${a}`);return{view:t,count:n,k:r,book:i}}function w(e){return[e.getUint32(4,!0),e.getUint32(8,!0),e.getUint32(12,!0)]}function T(e,t){let{view:n,count:r,k:i,book:s}=C(e),c=/* @__PURE__ */ new Float32Array(27);for(let e=0;e<27;e++)c[e]=n.getFloat32(20+4*e,!0);i===0&&c.fill(0,o.shScale);let l=new Uint32Array(r*4),u=new Float32Array(Math.ceil(r/256)*6),d=/* @__PURE__ */ new Float64Array(768);for(let e=0;e<3;e++){let t=c[o.scaleMin+e],n=c[o.scaleMax+e]-t;for(let r=0;r<256;r++)d[e*256+r]=Math.exp(t+r/255*n)}let f=c[o.step],p=c[o.step+1],m=c[o.step+2],h=128+a.position*r,g=128+a.alpha*r,_=128+a.rotationIndex*r,v=128+a.rotation*r,y=128+a.scale*r,S=/* @__PURE__ */ new Float64Array(4),T=/* @__PURE__ */ new Float64Array(3),O=0,k=0,A=0;for(let t=0,n=0;n<r;t++){let i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,C=-1/0;for(let t=Math.min(n+256,r);n<t;n++){let t=e[h+n]|e[h+r+n]<<8;O=O+(t>>>1^-(t&1))&65535,t=e[h+2*r+n]|e[h+3*r+n]<<8,k=k+(t>>>1^-(t&1))&65535,t=e[h+4*r+n]|e[h+5*r+n]<<8,A=A+(t>>>1^-(t&1))&65535;let u=e[_+n],w=e[v+n],E=e[v+r+n],D=e[v+2*r+n],j=e[y+n],M=e[y+r+n],N=e[y+2*r+n],P=n*4;l[P]=O|k<<16,l[P+1]=A|e[g+n]<<16|u<<24,l[P+2]=w|E<<8|D<<16|j<<24,l[P+3]=M|N<<8,b(u,w,E,D,S),x(S,d[j],d[256+M],d[512+N],T);let F=O*f,I=k*p,L=A*m;i=Math.min(i,F-T[0]),a=Math.min(a,I-T[1]),o=Math.min(o,L-T[2]),s=Math.max(s,F+T[0]),c=Math.max(c,I+T[1]),C=Math.max(C,L+T[2])}u.set([i,a,o,s,c,C],t*6)}let j=t?D(e,r,i,s,l,c,t):E(e,r,i,s);return{count:r,quadrants:w(n),geometry:l,colour:j,params:c,clusters:u}}function E(e,t,n,r){let i=new Uint32Array(t*3),o=128+a.dc*t,s=128+a.label*t;for(let a=0;a<t;a++){let c=a*3;if(i[c]=e[o+a]|e[o+t+a]<<8|e[o+2*t+a]<<16,n===0)continue;let l=r+(e[s+a]|e[s+t+a]<<8)*9;i[c]|=e[l]<<24,i[c+1]=e[l+1]|e[l+2]<<8|e[l+3]<<16|e[l+4]<<24,i[c+2]=e[l+5]|e[l+6]<<8|e[l+7]<<16|e[l+8]<<24}return i}function D(e,t,r,i,s,c,l){let u=128+a.dc*t,d=128+a.label*t,f=new Int8Array(e.buffer,e.byteOffset+i,r*9),p=new Float32Array(t*3),m=new Float32Array(t*9),h=[1/0,1/0,1/0],g=[-1/0,-1/0,-1/0],_=/* @__PURE__ */ new Float32Array(9),y=/* @__PURE__ */ new Float32Array(12),b=c[o.origin],x=c[o.origin+1],S=c[o.step],C=c[o.step+1],w=/* @__PURE__ */ new Float64Array(768);for(let e=0;e<3;e++){let t=c[o.dcMin+e],n=c[o.dcMax+e]-t;for(let r=0;r<256;r++)w[e*256+r]=t+r/255*n}let T=new Float64Array(Math.max(r,1)*9);for(let e=0;e<r*9;e++)T[e]=f[e]/127*c[o.shScale+e%9];for(let i=0;i<t;i++){let a=s[i*4];v(l,b+(a&65535)*S,x+(a>>>16)*C,y);for(let r=0;r<3;r++){let a=i*3+r;p[a]=w[r*256+e[u+r*t+i]]+y[r]/n,h[r]=Math.min(h[r],p[a]),g[r]=Math.max(g[r],p[a])}let o=r>0?(e[d+i]|e[d+t+i]<<8)*9:0;for(let e=0;e<9;e++){let t=i*9+e;m[t]=T[o+e]+y[3+e],_[e]=Math.max(_[e],Math.abs(m[t]))}}for(let e=0;e<3;e++)c[o.dcMin+e]=t>0?h[e]:0,c[o.dcMax+e]=t>0?g[e]:0;c.set(_,o.shScale);let E=[0,1,2].map(e=>{let t=c[o.dcMax+e]-c[o.dcMin+e];return t>0?255/t:0}),D=Array.from(_,e=>e>0?127/e:0),O=[0,1,2].map(e=>c[o.dcMin+e]),k=new Uint32Array(t*3),A=new Uint8Array(k.buffer);for(let e=0;e<t;e++){let t=e*4*3;for(let n=0;n<3;n++){let r=Math.round((p[e*3+n]-O[n])*E[n]);A[t+n]=Math.min(255,Math.max(0,r))}for(let n=0;n<9;n++){let r=Math.round(m[e*9+n]*D[n]);A[t+3+n]=Math.min(127,Math.max(-127,r))}}return k}async function O(e,t){return p(await A(e,t))}async function*k(e,t,n,r){let i=t.col===null?null:M(e,t.col,n),a=await j(e,t,r),o=await i;for(let[e,n]of t.pages.entries()){let t=await c(a[e]);r?.throwIfAborted(),yield{id:n.id,page:T(t,o)}}}async function A(e,t){let n=await e.cache.get(t);if(n)return n;let r=await e.fetch(e.base+t,null);return e.cache.put(t,r),r}async function j(e,t,n){let r=t.pages.map(e=>`${t.pack}#${e.offset}+${e.length}`),i=await Promise.all(r.map(t=>e.cache.get(t)));if(n?.throwIfAborted(),i.every(e=>e!==void 0))return i;let a=await e.fetch(e.base+t.pack,[t.start,t.end],n);if(a.length!==t.end-t.start)throw Error(`${t.pack}: ${a.length} bytes for ${t.start}-${t.end}`);return t.pages.map((n,o)=>{let s=a.slice(n.offset-t.start,n.offset-t.start+n.length);return i[o]||e.cache.put(r[o],s),s})}function M(e,t,n){let r=n.get(t);if(r)n.delete(t);else{let i=A(e,t).then(_);i.catch(()=>{n.get(t)===i&&n.delete(t)}),r=i}if(n.set(t,r),n.size>64){let[e]=n.keys();n.delete(e)}return r}function N(e){let t=/* @__PURE__ */ new Map,n=0;return{async get(e){let n=t.get(e);return n&&(t.delete(e),t.set(e,n)),n},async put(r,i){let a=t.get(r);if(a&&(t.delete(r),n-=a.length),!(i.length>e)){t.set(r,i),n+=i.length;for(let[r,i]of t){if(n<=e)break;t.delete(r),n-=i.length}}}}}async function P(e,t){let n;try{n=await(await I()).getDirectoryHandle(String(t),{create:!0})}catch{return null}let r=/* @__PURE__ */ new Map,i=0,a=!1,o=(e,t)=>{i+=t-(r.get(e)??0),r.delete(e),r.set(e,t)},s=e=>{i-=r.get(e)??0,r.delete(e)},c=()=>{if(a)for(let[t,a]of r){if(i<=e)break;r.delete(t),i-=a,n.removeEntry(t).catch(()=>void 0)}};return z(n).then(e=>{let t=[...r];r.clear(),i=0;for(let{name:t,size:n}of e.sort((e,t)=>e.time-t.time))o(t,n);for(let[e,n]of t)o(e,n);a=!0,c()}),{async get(e){let t=encodeURIComponent(e);if(a&&!r.has(t))return;let i=await L(n,t);return i?o(t,i.length):s(t),i},async put(t,r){if(r.length===0||r.length>e)return;let i=encodeURIComponent(t);await R(n,i,r)&&(o(i,r.length),c())}}}async function F(e){let t=new Set(Array.from({length:e},(e,t)=>String(t)));try{let e=await I(),n=[];for await(let[r,i]of e.entries())(i.kind!==`directory`||!t.has(r))&&n.push(r);let r={recursive:!0};for(let t of n)await e.removeEntry(t,r).catch(()=>void 0)}catch{}}async function I(){return(await navigator.storage.getDirectory()).getDirectoryHandle(`cache`,{create:!0})}async function L(e,t){try{let n=await(await e.getFileHandle(t)).getFile();return n.size>0?new Uint8Array(await n.arrayBuffer()):void 0}catch{return}}async function R(e,t,n){let r;try{return r=await(await e.getFileHandle(t,{create:!0})).createWritable(),await r.write(n),await r.close(),!0}catch{return r?.abort().catch(()=>void 0),!1}}async function z(e){let t=[];try{for await(let[n,r]of e.entries()){if(r.kind!==`file`)continue;let e=await r.getFile().catch(()=>null);e&&t.push({name:n,size:e.size,time:e.lastModified})}}catch{}return t}function B(t){let n=/* @__PURE__ */ new Map,r=0;return{fetch:(i,a,o)=>new Promise((s,c)=>{if(o?.aborted)return c(o.reason);let l=++r,u=()=>{n.delete(l),t({unfetch:l}),c(o.reason)};n.set(l,t=>{o?.removeEventListener(`abort`,u),`bytes`in t?s(t.bytes):c(t.missing?new e(t.error):Error(t.error))}),o?.addEventListener(`abort`,u,{once:!0}),t({fetch:l,url:i,range:a})}),take(e){let t=n.get(e.fetched);n.delete(e.fetched),t?.(e)}}}let V=(e,t,n)=>Math.min(n,Math.max(t,e));function ee(e,t,n){return n>t?V(Math.round((e-t)/(n-t)*255),0,255):0}function te(e,t){return t>0?V(Math.round(e/t*127),-127,127):0}function H(e){return e&=1023,e=(e|e<<16)&50331903,e=(e|e<<8)&50393103,e=(e|e<<4)&51130563,(e|e<<2)&153391689}function ne(e,t,n){return H(e>>6)|H(t>>6)<<1|H(n>>6)<<2}function re(e,t){let n=/* @__PURE__ */ new Float32Array(9);for(let r=0;r<t;r++)for(let t=0;t<9;t++)n[t]=Math.max(n[t],Math.abs(e[r*9+t]));return n}function ie(e,t,n){let r=new Int8Array(t*9);for(let i=0;i<t;i++)for(let t=0;t<9;t++)r[i*9+t]=te(e[i*9+t],n[t]);let i=e=>r.subarray(e*9,(e+1)*9),a=/* @__PURE__ */ new Map,o=[],s=new Uint16Array(t);for(let e=0;e<t;e++){let t=i(e).join(),n=a.get(t);n===void 0&&(n=o.length,a.set(t,n),o.push(e)),s[e]=n}if(o.length>2048){let e=o.length/256,n=Array.from({length:256},(t,n)=>o[Math.floor(n*e)]);o.splice(0,o.length,...n);for(let e=0;e<t;e++){let t=1/0;for(let n=0;n<256;n++){let i=0;for(let t=0;t<9;t++)i+=(r[e*9+t]-r[o[n]*9+t])**2;i<t&&(t=i,s[e]=n)}}}let c=new Int8Array(o.length*9);return o.forEach((e,t)=>c.set(i(e),t*9)),{book:c,labels:s}}function ae(e,t){let{count:n,position:i,rotation:o,scale:s,alpha:c,dc:l,sh:u}=e,d=[1/0,1/0,1/0],f=[-1/0,-1/0,-1/0],p=[...d],m=[...f],h=[...d],g=[...f];for(let e=0;e<n;e++)for(let t=0;t<3;t++)d[t]=Math.min(d[t],i[e*3+t]),f[t]=Math.max(f[t],i[e*3+t]),p[t]=Math.min(p[t],s[e*3+t]),m[t]=Math.max(m[t],s[e*3+t]),h[t]=Math.min(h[t],l[e*3+t]),g[t]=Math.max(g[t],l[e*3+t]);let _=5e-4*2**t.g,v=Float32Array.from(d),y=Float32Array.from(d,(e,t)=>Math.max(_,(f[t]-e)/65535)),b=new Uint16Array(n*3);for(let e=0;e<n;e++)for(let t=0;t<3;t++){let n=(i[e*3+t]-v[t])/y[t];b[e*3+t]=V(Math.round(n),0,65535)}let S=r*2**t.g,C=(t.i+.5)*S,w=(t.j+.5)*S,T=new Uint8Array(n),E=new Float64Array(n);for(let e=0;e<n;e++){let t=+(i[e*3]>=C),n=+(i[e*3+1]>=w);T[e]=t+2*n,E[e]=T[e]*2**30+ne(b[e*3],b[e*3+1],b[e*3+2])}let D=Uint32Array.from({length:n},(e,t)=>t).sort((e,t)=>E[e]-E[t]),O=[0,0,0];for(let e=1;e<4;e++)O[e-1]=T.filter(t=>t<e).length;let k=u?re(u,n):/* @__PURE__ */ new Float32Array(9),{book:A,labels:j}=u?ie(u,n,k):{book:null,labels:null},M=A?A.length/9:0,N=17+(M>0?2:0),P=new Uint8Array(128+N*n+M*9),F=new DataView(P.buffer);F.setUint32(0,n,!0),O.forEach((e,t)=>F.setUint32(4+4*t,e,!0)),F.setUint16(16,M,!0),[...v,...y,...p,...m,...h,...g,...k].forEach((e,t)=>F.setFloat32(20+4*t,e,!0));let I=e=>128+e*n,L=new Float32Array([...d,...f]),R=/* @__PURE__ */ new Float64Array(4),z=/* @__PURE__ */ new Float64Array(3),B=[0,0,0];for(let e=0;e<n;e++){let t=D[e];for(let n=0;n<3;n++){let r=b[t*3+n]-B[n]<<16>>16,i=r<0?-2*r-1:2*r;B[n]=b[t*3+n],P[I(a.position+2*n)+e]=i&255,P[I(a.position+2*n+1)+e]=i>>8}P[I(a.alpha)+e]=V(Math.round(c[t]*255),0,255);let n=Math.hypot(...o.subarray(t*4,t*4+4));for(let e=0;e<4;e++)R[e]=o[t*4+e]/n;let r=0;for(let e=1;e<4;e++)Math.abs(R[e])>Math.abs(R[r])&&(r=e);let u=R[r]<0?-1:1;P[I(a.rotationIndex)+e]=r;for(let t=0,n=0;t<4;t++){if(t===r)continue;let i=Math.round((u*R[t]/Math.SQRT2+.5)*255);P[I(a.rotation+n++)+e]=V(i,0,255)}for(let n=0;n<3;n++)P[I(a.scale+n)+e]=ee(s[t*3+n],p[n],m[n]),P[I(a.dc+n)+e]=ee(l[t*3+n],h[n],g[n]);j&&(P[I(a.label)+e]=j[t]&255,P[I(a.label+1)+e]=j[t]>>8);let[d,f,_]=[0,1,2].map(e=>Math.exp(s[t*3+e]));x(R,d,f,_,z);for(let e=0;e<3;e++)L[e]=Math.min(L[e],i[t*3+e]-z[e]),L[3+e]=Math.max(L[3+e],i[t*3+e]+z[e])}return A&&P.set(new Uint8Array(A.buffer),I(N)),{payload:P,row:{g:t.g,i:t.i,j:t.j,count:n,quadrants:O,box:L,offset:0,length:0}}}function oe(e,t,n){e.setUint8(t,n.g),e.setUint8(t+1,n.i),e.setUint8(t+2,n.j),e.setUint32(t+4,n.count,!0),n.quadrants.forEach((n,r)=>e.setUint32(t+8+4*r,n,!0)),n.box.forEach((n,r)=>e.setFloat32(t+20+4*r,n,!0)),e.setUint32(t+44,n.offset,!0),e.setUint32(t+48,n.length,!0)}function se(e,t,n){for(let r=0;r<4;r++)e[t+r]=n.charCodeAt(r)}function U(e){return Uint8Array.from({length:8},(t,n)=>parseInt(e.slice(2*n,2*n+2),16))}async function ce(e,t){let n=await l(e),r=new Uint8Array(8+n.length);return se(r,0,t),new DataView(r.buffer).setUint16(4,1,!0),r.set(n,8),r}async function le(e){let{rows:t,children:n,descendants:r}=e,a=new Uint8Array(40+t.length*52+n.length*9+r.length*11),o=new DataView(a.buffer);o.setUint8(0,e.level),o.setInt32(4,e.e,!0),o.setInt32(8,e.n,!0),a.set(U(e.pack),12),o.setUint32(20,e.packSize,!0),e.col&&a.set(U(e.col),24),o.setUint16(32,t.length,!0),o.setUint8(34,n.length),o.setUint16(36,r.length,!0);let s=40;for(let e of t)oe(o,s,e),s+=52;for(let e of n)o.setUint8(s,e.quadrant),a.set(U(e.idx),s+1),s+=9;for(let e of r)o.setUint8(s,e.depth),o.setUint8(s+1,e.column),o.setUint8(s+2,e.row),a.set(U(e.idx),s+3),s+=11;return ce(a,i)}function ue(e){let t=3421674724,n=2216829733;for(let r=0;r<e.length;r++){n=(n^e[r])>>>0;let i=n*435;t=t*435+(n<<8>>>0)+Math.floor(i/4294967296)>>>0,n=i>>>0}return t.toString(16).padStart(8,`0`)+n.toString(16).padStart(8,`0`)}function de(e){return()=>{e=e+1831565813|0;let t=Math.imul(e^e>>>15,1|e);return t=t+Math.imul(t^t>>>7,61|t)^t,((t^t>>>14)>>>0)/2**32}}let W=16384,fe=1408,pe=65535,G=324121,me=.4,he=[[12,1/7e3,0,1/9e3,.7],[9,1/2300,1.1,1/3100,.4],[6,1/800,.3,1/1100,2],[3,1/150,.9,1/210,1.7]],ge=he.reduce((e,[t,n,,r])=>e+t*(n+r),0),_e=Float32Array.from({length:fe*9},(e,t)=>{let n=Math.floor(t/9),r=t%9,i=(n*(37+2*r)+11*r)%255-127;return r===0&&(i=n%255-127),r===1&&(i=20*Math.floor(n/255)-50),i/127*.04}),K=new TextEncoder,ve={version:1,crs:`synthetic`,height:`synthetic`,leaf:250,roots:[512e4,6144e3].map(e=>({level:12,e:0,n:e})).map(e=>({...e,idx:m(e,q(e,`idx`),`idx`)})),extent:{e:[44e4,9e5],n:[605e4,641e4],h:[0,80]}},ye=K.encode(JSON.stringify(ve));function be(){return async(t,n)=>{if(t.endsWith(`world.json`))return Se(ye,n);let r=g(t),i=t.slice(t.lastIndexOf(`.`)+1);if(!r||i!==`idx`&&i!==`pack`||!Ce(r)||r.hash!==q(r,i))throw new e(`no file ${t}`);return i===`idx`?Se(await ke(r),n):Ae(r,n,t)}}function xe(e,t){let n=30;for(let[r,i,a,o,s]of he)n+=r*Math.sin(i*e+a)*Math.sin(o*t+s);return n}function Se(e,t){return t?e.slice(t[0],t[1]):e.slice()}function q(e,t){return ue(K.encode(`${e.level}/${e.e}/${e.n}.${t}`))}function J(e,t,n){let{e:r,n:i}=ve.extent;return e<r[1]&&e+n>r[0]&&t<i[1]&&t+n>i[0]}function Ce({level:e,e:t,n}){let r=250*2**e;return e<=12&&t%r===0&&n%r===0&&J(t,n,r)}function we(e){return e===0?[1,0]:e===12?[16,15,14,13]:[e+1]}function Te(e){let t=0;for(let n=0;n<8;n++)t|=(e>>2*n&1)<<n;return t}function Ee(e){let t=[];for(let n of we(e.level)){let i=r*2**n,a=250*2**e.level/i;for(let r=0;r<a*a;r++){let a=Te(r),o=Te(r>>1);J(e.e+a*i,e.n+o*i,i)&&t.push({g:n,i:a,j:o})}}return t}function De(e,t){let n=e.level-t,r=250*2**n,i=[];for(let a=0;n>=0&&a<2**t;a++)for(let o=0;o<2**t;o++){let t={level:n,e:e.e+o*r,n:e.n+a*r};J(t.e,t.n,r)&&i.push({column:o,row:a,unit:t})}return i}function Oe(e,{g:t,i:n,j:i},a){let o=r*2**t,s=o/128,c=3*1.2*s,l=1.17*s,u=xe(e.e+(n+.5)*o,e.n+(i+.5)*o),d=ge*o/2,f=Float32Array.of(n*o-c,i*o-c,Math.max(0,u-d)-l,(n+1)*o+c,(i+1)*o+c,Math.min(60,u+d)+l),p=W/4;return{g:t,i:n,j:i,count:W,quadrants:[p,2*p,3*p],box:f,offset:a,length:G}}function ke(e){let t=Ee(e),n=[];for(let{column:t,row:r,unit:i}of De(e,1))n.push({quadrant:t+2*r,idx:q(i,`idx`)});let r=[];for(let t of[2,3])for(let{column:n,row:i,unit:a}of De(e,t))r.push({depth:t,column:n,row:i,idx:q(a,`idx`)});return le({level:e.level,e:e.e,n:e.n,pack:q(e,`pack`),packSize:t.length*324173+16,col:null,rows:t.map((t,n)=>Oe(e,t,n*G)),children:n,descendants:r})}function Ae(e,t,n){let r=Ee(e);if(!t||t[0]%G!=0||t[1]%G!=0||t[0]>=t[1]||t[1]>r.length*G)throw Error(`${n}: the synthetic world serves whole pages only`);let i=new Uint8Array(t[1]-t[0]);for(let n=0;n<i.length;n+=G)je(Ne(e,r[(t[0]+n)/G]),i,n);return i}function je(e,t,n){let r=n;for(let n=0;n<e.length;n+=pe){let i=Math.min(pe,e.length-n);t[r]=+(n+i===e.length),t[r+1]=i&255,t[r+2]=i>>8,t[r+3]=~i&255,t[r+4]=~i>>8&255,t.set(e.subarray(n,n+i),r+5),r+=5+i}}let Me=e=>de(parseInt(ue(K.encode(e)).slice(8),16));function Ne(e,{g:t,i,j:a}){let o=W,s=r*2**t/128,c=Me(`${e.level}/${e.e}/${e.n}/${t}/${i}/${a}`),l=new Float64Array(3*o),u=new Float32Array(4*o),d=new Float32Array(3*o),f=new Float32Array(o),p=new Float32Array(3*o),m=new Float32Array(9*o);for(let t=0;t<o;t++){let r=(i*128+t%128+.5+me*(2*c()-1))*s,o=(a*128+Math.floor(t/128)+.5+me*(2*c()-1))*s,h=xe(e.e+r,e.n+o);l[3*t]=r,l[3*t+1]=o,l[3*t+2]=h;let g=Math.PI*c(),_=.1/2*(2*c()-1);u[4*t]=Math.cos(g)*Math.cos(_),u[4*t+1]=Math.cos(g)*Math.sin(_),u[4*t+2]=Math.sin(g)*Math.sin(_),u[4*t+3]=Math.sin(g)*Math.cos(_);for(let e=0;e<2;e++)d[3*t+e]=Math.log(s*(.6+.6*c()));d[3*t+2]=Math.log(s*.15*(.5+.5*c())),f[t]=.35+.65*c();let v=h/60,y=.5+.5*Math.sin((e.e+r)/410)*Math.sin((e.n+o)/330);p[3*t]=(.32+.2*v+.1*y-.5)/n,p[3*t+1]=(.42+.15*y-.1*v-.5)/n,p[3*t+2]=(.25+.1*v+.04*c()-.5)/n;let b=t%fe;m.set(_e.subarray(9*b,9*(b+1)),9*t)}return ae({count:o,position:l,rotation:u,scale:d,alpha:f,dc:p,sh:m},{g:t,i,j:a}).payload}let Y=2**20,X=self,Z,Pe=/* @__PURE__ */ new Map,Q=/* @__PURE__ */ new Map,Fe=B(e=>X.postMessage(e,[]));X.onmessage=({data:e})=>{`fetched`in e?Fe.take(e):e.type===`init`?(Z=Ie(e),Z.catch(()=>void 0)):e.type===`abort`?Q.get(e.id)?.abort():Re(e)};async function Ie(e){return e.source===`synthetic`?{fetch:be(),cache:await $({...e,cache:`none`}),base:e.base}:{fetch:e.source===`page`?Fe.fetch:t,cache:await $(e),base:e.base}}async function $({cache:e,shard:t,shards:n}){return e===`memory`?N(256*Y):e===`none`?{get:async()=>void 0,put:async()=>void 0}:(t===0&&F(n),await P(await Le()/n,t)??N(256*Y))}async function Le(){try{let{quota:e}=await navigator.storage.estimate();return Math.min(4096*Y,e===void 0?1/0:e/2)}catch{return 4096*Y}}async function Re(t){let{id:n}=t,r=new AbortController;Q.set(n,r);try{if(!Z)throw Error(`stream worker used before init`);let e=await Z;if(t.type===`idx`)X.postMessage({id:n,value:await O(e,t.path)},[]);else{for await(let i of k(e,t.batch,Pe,r.signal))X.postMessage({id:n,page:i},ze(i));X.postMessage({id:n,done:!0},[])}}catch(t){let r=t instanceof Error?t.message:String(t);X.postMessage({id:n,error:r,missing:t instanceof e},[])}finally{Q.delete(n)}}function ze({page:e}){let t=/* @__PURE__ */ new Set;for(let n of[e.geometry,e.colour,e.params,e.clusters])t.add(n.buffer);return[...t]}})();", et = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", $e], { type: "text/javascript;charset=utf-8" });
1630
- function tt(e) {
1873
+ var ut = "(function(){var e=class extends Error{name=`Missing`};let t=async(t,n,r)=>{let i=await fetch(t,n?{signal:r,headers:{Range:`bytes=${n[0]}-${n[1]-1}`}}:{signal:r});if(i.status===404)throw new e(`http 404 for ${t}`);if(i.status!==200&&i.status!==206)throw Error(`http ${i.status} for ${t}`);let a=new Uint8Array(await i.arrayBuffer());return n&&i.status===200?a.slice(n[0],n[1]):a},n=.28209479177387814,r=15.625,i=`GMIX`,a={position:0,alpha:6,rotationIndex:7,rotation:8,scale:11,dc:14,label:17},o={origin:0,step:3,scaleMin:6,scaleMax:9,dcMin:12,dcMax:15,shScale:18};async function s(e,t){let n=new ReadableStream({start(t){t.enqueue(e),t.close()}}).pipeThrough(t);return new Uint8Array(await new Response(n).arrayBuffer())}function c(e){return s(e,new DecompressionStream(`deflate-raw`))}function l(e){return s(e,new CompressionStream(`deflate-raw`))}async function u(e,t,n){let r=new DataView(e.buffer,e.byteOffset,e.byteLength);if(e.length<8||String.fromCharCode(...e.subarray(0,4))!==t)throw Error(`not a ${n} file`);let i=r.getUint16(4,!0);if(i!==1)throw Error(`${n} version ${i}; narcissus reads 1`);let a;try{a=await c(e.subarray(8))}catch{throw Error(`${n} body is not a valid deflate stream`)}return new DataView(a.buffer,a.byteOffset,a.byteLength)}function d(e,t){let n=``;for(let r=0;r<8;r++){let i=e.getUint8(t+r);n+=i.toString(16).padStart(2,`0`)}return n}function f(e,t){let n=/* @__PURE__ */ new Float32Array(6);for(let r=0;r<6;r++)n[r]=e.getFloat32(t+20+4*r,!0);return{g:e.getUint8(t),i:e.getUint8(t+1),j:e.getUint8(t+2),count:e.getUint32(t+4,!0),quadrants:[e.getUint32(t+8,!0),e.getUint32(t+12,!0),e.getUint32(t+16,!0)],box:n,offset:e.getUint32(t+44,!0),length:e.getUint32(t+48,!0)}}async function p(e){let t=await u(e,i,`.idx`);if(t.byteLength<40)throw Error(`.idx header is cut off`);let n=t.getUint16(32,!0),r=t.getUint8(34),a=t.getUint16(36,!0),o=40+n*52+r*9+a*11;if(t.byteLength!==o)throw Error(`.idx holds ${t.byteLength} bytes, its counts need ${o}`);let s=d(t,24),c={level:t.getUint8(0),e:t.getInt32(4,!0),n:t.getInt32(8,!0),pack:d(t,12),packSize:t.getUint32(20,!0),col:s===`0000000000000000`?null:s,rows:[],children:[],descendants:[]},l=40;for(let e=0;e<n;e++,l+=52)c.rows.push(f(t,l));for(let e=0;e<r;e++,l+=9)c.children.push({quadrant:t.getUint8(l),idx:d(t,l+1)});for(let e=0;e<a;e++,l+=11)c.descendants.push({depth:t.getUint8(l),column:t.getUint8(l+1),row:t.getUint8(l+2),idx:d(t,l+3)});return c}function m(e,t,n){return`u/${e.level}/${e.e}/${e.n}.${t}.${n}`}let h=/(?:^|\\/)u\\/(\\d+)\\/(-?\\d+)\\/(-?\\d+)\\.([0-9a-f]{16})\\.(?:idx|pack|col)$/;function g(e){let t=h.exec(e);return t?{level:Number(t[1]),e:Number(t[2]),n:Number(t[3]),hash:t[4]}:null}async function _(e){let t=await u(e,`GMCL`,`.col`);if(t.byteLength<60)throw Error(`.col header is cut off`);let n=t.getUint16(0,!0),r=60+12*n*n;if(t.byteLength!==r)throw Error(`.col holds ${t.byteLength} bytes, ${n} nodes need ${r}`);let i=new Float32Array(12*n*n),a=60;for(let e=0;e<12;e++){let r=t.getFloat32(12+4*e,!0)/127;for(let o=0;o<n;o++){let s=0;for(let c=0;c<n;c++,a++)s=s+t.getInt8(a)<<24>>24,i[(e*n+o)*n+c]=s*r}}return{n,pitch:t.getFloat32(4,!0),margin:t.getFloat32(8,!0),values:i}}function v(e,t,n,r){let{n:i,pitch:a,margin:o,values:s}=e,c=i-1,l=Math.min(Math.max((t+o)/a-.5,0),c),u=Math.min(Math.max((n+o)/a-.5,0),c),d=Math.min(Math.floor(l),Math.max(c-1,0)),f=Math.min(Math.floor(u),Math.max(c-1,0)),p=Math.min(d+1,c),m=Math.min(f+1,c),h=l-d,g=u-f,_=(1-h)*(1-g),v=h*(1-g),y=(1-h)*g,b=h*g,x=i*i;for(let e=0,t=0;e<12;e++,t+=x)r[e]=s[t+f*i+d]*_+s[t+f*i+p]*v+s[t+m*i+d]*y+s[t+m*i+p]*b;return r}let y=Float64Array.from({length:256},(e,t)=>(t/255-.5)*Math.SQRT2);function b(e,t,n,r,i){let a=y[t],o=y[n],s=y[r];i[e]=Math.sqrt(Math.max(0,1-a*a-o*o-s*s)),i[e>0?0:1]=a,i[e>1?1:2]=o,i[e>2?2:3]=s}function x(e,t,n,r,i){let a=e[0],o=e[1],s=e[2],c=e[3],l=t*t,u=n*n,d=r*r,f=1-2*(s*s+c*c),p=2*(o*s-a*c),m=2*(o*c+a*s),h=2*(o*s+a*c),g=1-2*(o*o+c*c),_=2*(s*c-a*o),v=2*(o*c-a*s),y=2*(s*c+a*o),b=1-2*(o*o+s*s);i[0]=3*Math.sqrt(f*f*l+p*p*u+m*m*d),i[1]=3*Math.sqrt(h*h*l+g*g*u+_*_*d),i[2]=3*Math.sqrt(v*v*l+y*y*u+b*b*d)}function S(e){if(e.length<128)throw Error(`page payload is shorter than its header`);return new DataView(e.buffer,e.byteOffset,e.byteLength)}function C(e){let t=S(e),n=t.getUint32(0,!0),r=t.getUint16(16,!0),i=128+(17+(r>0?2:0))*n,a=i+r*9;if(e.length!==a)throw Error(`page payload holds ${e.length} bytes, its header needs ${a}`);return{view:t,count:n,k:r,book:i}}function w(e){return[e.getUint32(4,!0),e.getUint32(8,!0),e.getUint32(12,!0)]}function T(e,t){let{view:n,count:r,k:i,book:s}=C(e),c=/* @__PURE__ */ new Float32Array(27);for(let e=0;e<27;e++)c[e]=n.getFloat32(20+4*e,!0);i===0&&c.fill(0,o.shScale);let l=new Uint32Array(r*4),u=new Float32Array(Math.ceil(r/256)*6),d=/* @__PURE__ */ new Float64Array(768);for(let e=0;e<3;e++){let t=c[o.scaleMin+e],n=c[o.scaleMax+e]-t;for(let r=0;r<256;r++)d[e*256+r]=Math.exp(t+r/255*n)}let f=c[o.step],p=c[o.step+1],m=c[o.step+2],h=128+a.position*r,g=128+a.alpha*r,_=128+a.rotationIndex*r,v=128+a.rotation*r,y=128+a.scale*r,S=/* @__PURE__ */ new Float64Array(4),T=/* @__PURE__ */ new Float64Array(3),O=0,k=0,A=0;for(let t=0,n=0;n<r;t++){let i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,C=-1/0;for(let t=Math.min(n+256,r);n<t;n++){let t=e[h+n]|e[h+r+n]<<8;O=O+(t>>>1^-(t&1))&65535,t=e[h+2*r+n]|e[h+3*r+n]<<8,k=k+(t>>>1^-(t&1))&65535,t=e[h+4*r+n]|e[h+5*r+n]<<8,A=A+(t>>>1^-(t&1))&65535;let u=e[_+n],w=e[v+n],E=e[v+r+n],D=e[v+2*r+n],j=e[y+n],M=e[y+r+n],N=e[y+2*r+n],P=n*4;l[P]=O|k<<16,l[P+1]=A|e[g+n]<<16|u<<24,l[P+2]=w|E<<8|D<<16|j<<24,l[P+3]=M|N<<8,b(u,w,E,D,S),x(S,d[j],d[256+M],d[512+N],T);let F=O*f,I=k*p,L=A*m;i=Math.min(i,F-T[0]),a=Math.min(a,I-T[1]),o=Math.min(o,L-T[2]),s=Math.max(s,F+T[0]),c=Math.max(c,I+T[1]),C=Math.max(C,L+T[2])}u.set([i,a,o,s,c,C],t*6)}let j=t?D(e,r,i,s,l,c,t):E(e,r,i,s);return{count:r,quadrants:w(n),geometry:l,colour:j,params:c,clusters:u}}function E(e,t,n,r){let i=new Uint32Array(t*3),o=128+a.dc*t,s=128+a.label*t;for(let a=0;a<t;a++){let c=a*3;if(i[c]=e[o+a]|e[o+t+a]<<8|e[o+2*t+a]<<16,n===0)continue;let l=r+(e[s+a]|e[s+t+a]<<8)*9;i[c]|=e[l]<<24,i[c+1]=e[l+1]|e[l+2]<<8|e[l+3]<<16|e[l+4]<<24,i[c+2]=e[l+5]|e[l+6]<<8|e[l+7]<<16|e[l+8]<<24}return i}function D(e,t,r,i,s,c,l){let u=128+a.dc*t,d=128+a.label*t,f=new Int8Array(e.buffer,e.byteOffset+i,r*9),p=new Float32Array(t*3),m=new Float32Array(t*9),h=[1/0,1/0,1/0],g=[-1/0,-1/0,-1/0],_=/* @__PURE__ */ new Float32Array(9),y=/* @__PURE__ */ new Float32Array(12),b=c[o.origin],x=c[o.origin+1],S=c[o.step],C=c[o.step+1],w=/* @__PURE__ */ new Float64Array(768);for(let e=0;e<3;e++){let t=c[o.dcMin+e],n=c[o.dcMax+e]-t;for(let r=0;r<256;r++)w[e*256+r]=t+r/255*n}let T=new Float64Array(Math.max(r,1)*9);for(let e=0;e<r*9;e++)T[e]=f[e]/127*c[o.shScale+e%9];for(let i=0;i<t;i++){let a=s[i*4];v(l,b+(a&65535)*S,x+(a>>>16)*C,y);for(let r=0;r<3;r++){let a=i*3+r;p[a]=w[r*256+e[u+r*t+i]]+y[r]/n,h[r]=Math.min(h[r],p[a]),g[r]=Math.max(g[r],p[a])}let o=r>0?(e[d+i]|e[d+t+i]<<8)*9:0;for(let e=0;e<9;e++){let t=i*9+e;m[t]=T[o+e]+y[3+e],_[e]=Math.max(_[e],Math.abs(m[t]))}}for(let e=0;e<3;e++)c[o.dcMin+e]=t>0?h[e]:0,c[o.dcMax+e]=t>0?g[e]:0;c.set(_,o.shScale);let E=[0,1,2].map(e=>{let t=c[o.dcMax+e]-c[o.dcMin+e];return t>0?255/t:0}),D=Array.from(_,e=>e>0?127/e:0),O=[0,1,2].map(e=>c[o.dcMin+e]),k=new Uint32Array(t*3),A=new Uint8Array(k.buffer);for(let e=0;e<t;e++){let t=e*4*3;for(let n=0;n<3;n++){let r=Math.round((p[e*3+n]-O[n])*E[n]);A[t+n]=Math.min(255,Math.max(0,r))}for(let n=0;n<9;n++){let r=Math.round(m[e*9+n]*D[n]);A[t+3+n]=Math.min(127,Math.max(-127,r))}}return k}async function O(e,t){return p(await A(e,t))}async function*k(e,t,n,r){let i=t.col===null?null:M(e,t.col,n),a=await j(e,t,r),o=await i;for(let[e,n]of t.pages.entries()){let t=await c(a[e]);r?.throwIfAborted(),yield{id:n.id,page:T(t,o)}}}async function A(e,t){let n=await e.cache.get(t);if(n)return n;let r=await e.fetch(e.base+t,null);return e.cache.put(t,r),r}async function j(e,t,n){let r=t.pages.map(e=>`${t.pack}#${e.offset}+${e.length}`),i=await Promise.all(r.map(t=>e.cache.get(t)));if(n?.throwIfAborted(),i.every(e=>e!==void 0))return i;let a=await e.fetch(e.base+t.pack,[t.start,t.end],n);if(a.length!==t.end-t.start)throw Error(`${t.pack}: ${a.length} bytes for ${t.start}-${t.end}`);return t.pages.map((n,o)=>{let s=a.slice(n.offset-t.start,n.offset-t.start+n.length);return i[o]||e.cache.put(r[o],s),s})}function M(e,t,n){let r=n.get(t);if(r)n.delete(t);else{let i=A(e,t).then(_);i.catch(()=>{n.get(t)===i&&n.delete(t)}),r=i}if(n.set(t,r),n.size>64){let[e]=n.keys();n.delete(e)}return r}function N(e){let t=/* @__PURE__ */ new Map,n=0;return{async get(e){let n=t.get(e);return n&&(t.delete(e),t.set(e,n)),n},async put(r,i){let a=t.get(r);if(a&&(t.delete(r),n-=a.length),!(i.length>e)){t.set(r,i),n+=i.length;for(let[r,i]of t){if(n<=e)break;t.delete(r),n-=i.length}}}}}async function P(e,t){let n;try{n=await(await I()).getDirectoryHandle(String(t),{create:!0})}catch{return null}let r=/* @__PURE__ */ new Map,i=0,a=!1,o=(e,t)=>{i+=t-(r.get(e)??0),r.delete(e),r.set(e,t)},s=e=>{i-=r.get(e)??0,r.delete(e)},c=()=>{if(a)for(let[t,a]of r){if(i<=e)break;r.delete(t),i-=a,n.removeEntry(t).catch(()=>void 0)}};return z(n).then(e=>{let t=[...r];r.clear(),i=0;for(let{name:t,size:n}of e.sort((e,t)=>e.time-t.time))o(t,n);for(let[e,n]of t)o(e,n);a=!0,c()}),{async get(e){let t=encodeURIComponent(e);if(a&&!r.has(t))return;let i=await L(n,t);return i?o(t,i.length):s(t),i},async put(t,r){if(r.length===0||r.length>e)return;let i=encodeURIComponent(t);await R(n,i,r)&&(o(i,r.length),c())}}}async function F(e){let t=new Set(Array.from({length:e},(e,t)=>String(t)));try{let e=await I(),n=[];for await(let[r,i]of e.entries())(i.kind!==`directory`||!t.has(r))&&n.push(r);let r={recursive:!0};for(let t of n)await e.removeEntry(t,r).catch(()=>void 0)}catch{}}async function I(){return(await navigator.storage.getDirectory()).getDirectoryHandle(`cache`,{create:!0})}async function L(e,t){try{let n=await(await e.getFileHandle(t)).getFile();return n.size>0?new Uint8Array(await n.arrayBuffer()):void 0}catch{return}}async function R(e,t,n){let r;try{return r=await(await e.getFileHandle(t,{create:!0})).createWritable(),await r.write(n),await r.close(),!0}catch{return r?.abort().catch(()=>void 0),!1}}async function z(e){let t=[];try{for await(let[n,r]of e.entries()){if(r.kind!==`file`)continue;let e=await r.getFile().catch(()=>null);e&&t.push({name:n,size:e.size,time:e.lastModified})}}catch{}return t}function B(t){let n=/* @__PURE__ */ new Map,r=0;return{fetch:(i,a,o)=>new Promise((s,c)=>{if(o?.aborted)return c(o.reason);let l=++r,u=()=>{n.delete(l),t({unfetch:l}),c(o.reason)};n.set(l,t=>{o?.removeEventListener(`abort`,u),`bytes`in t?s(t.bytes):c(t.missing?new e(t.error):Error(t.error))}),o?.addEventListener(`abort`,u,{once:!0}),t({fetch:l,url:i,range:a})}),take(e){let t=n.get(e.fetched);n.delete(e.fetched),t?.(e)}}}let V=(e,t,n)=>Math.min(n,Math.max(t,e));function ee(e,t,n){return n>t?V(Math.round((e-t)/(n-t)*255),0,255):0}function te(e,t){return t>0?V(Math.round(e/t*127),-127,127):0}function H(e){return e&=1023,e=(e|e<<16)&50331903,e=(e|e<<8)&50393103,e=(e|e<<4)&51130563,(e|e<<2)&153391689}function ne(e,t,n){return H(e>>6)|H(t>>6)<<1|H(n>>6)<<2}function re(e,t){let n=/* @__PURE__ */ new Float32Array(9);for(let r=0;r<t;r++)for(let t=0;t<9;t++)n[t]=Math.max(n[t],Math.abs(e[r*9+t]));return n}function ie(e,t,n){let r=new Int8Array(t*9);for(let i=0;i<t;i++)for(let t=0;t<9;t++)r[i*9+t]=te(e[i*9+t],n[t]);let i=e=>r.subarray(e*9,(e+1)*9),a=/* @__PURE__ */ new Map,o=[],s=new Uint16Array(t);for(let e=0;e<t;e++){let t=i(e).join(),n=a.get(t);n===void 0&&(n=o.length,a.set(t,n),o.push(e)),s[e]=n}if(o.length>2048){let e=o.length/256,n=Array.from({length:256},(t,n)=>o[Math.floor(n*e)]);o.splice(0,o.length,...n);for(let e=0;e<t;e++){let t=1/0;for(let n=0;n<256;n++){let i=0;for(let t=0;t<9;t++)i+=(r[e*9+t]-r[o[n]*9+t])**2;i<t&&(t=i,s[e]=n)}}}let c=new Int8Array(o.length*9);return o.forEach((e,t)=>c.set(i(e),t*9)),{book:c,labels:s}}function ae(e,t){let{count:n,position:i,rotation:o,scale:s,alpha:c,dc:l,sh:u}=e,d=[1/0,1/0,1/0],f=[-1/0,-1/0,-1/0],p=[...d],m=[...f],h=[...d],g=[...f];for(let e=0;e<n;e++)for(let t=0;t<3;t++)d[t]=Math.min(d[t],i[e*3+t]),f[t]=Math.max(f[t],i[e*3+t]),p[t]=Math.min(p[t],s[e*3+t]),m[t]=Math.max(m[t],s[e*3+t]),h[t]=Math.min(h[t],l[e*3+t]),g[t]=Math.max(g[t],l[e*3+t]);let _=5e-4*2**t.g,v=Float32Array.from(d),y=Float32Array.from(d,(e,t)=>Math.max(_,(f[t]-e)/65535)),b=new Uint16Array(n*3);for(let e=0;e<n;e++)for(let t=0;t<3;t++){let n=(i[e*3+t]-v[t])/y[t];b[e*3+t]=V(Math.round(n),0,65535)}let S=r*2**t.g,C=(t.i+.5)*S,w=(t.j+.5)*S,T=new Uint8Array(n),E=new Float64Array(n);for(let e=0;e<n;e++){let t=+(i[e*3]>=C),n=+(i[e*3+1]>=w);T[e]=t+2*n,E[e]=T[e]*2**30+ne(b[e*3],b[e*3+1],b[e*3+2])}let D=Uint32Array.from({length:n},(e,t)=>t).sort((e,t)=>E[e]-E[t]),O=[0,0,0];for(let e=1;e<4;e++)O[e-1]=T.filter(t=>t<e).length;let k=u?re(u,n):/* @__PURE__ */ new Float32Array(9),{book:A,labels:j}=u?ie(u,n,k):{book:null,labels:null},M=A?A.length/9:0,N=17+(M>0?2:0),P=new Uint8Array(128+N*n+M*9),F=new DataView(P.buffer);F.setUint32(0,n,!0),O.forEach((e,t)=>F.setUint32(4+4*t,e,!0)),F.setUint16(16,M,!0),[...v,...y,...p,...m,...h,...g,...k].forEach((e,t)=>F.setFloat32(20+4*t,e,!0));let I=e=>128+e*n,L=new Float32Array([...d,...f]),R=/* @__PURE__ */ new Float64Array(4),z=/* @__PURE__ */ new Float64Array(3),B=[0,0,0];for(let e=0;e<n;e++){let t=D[e];for(let n=0;n<3;n++){let r=b[t*3+n]-B[n]<<16>>16,i=r<0?-2*r-1:2*r;B[n]=b[t*3+n],P[I(a.position+2*n)+e]=i&255,P[I(a.position+2*n+1)+e]=i>>8}P[I(a.alpha)+e]=V(Math.round(c[t]*255),0,255);let n=Math.hypot(...o.subarray(t*4,t*4+4));for(let e=0;e<4;e++)R[e]=o[t*4+e]/n;let r=0;for(let e=1;e<4;e++)Math.abs(R[e])>Math.abs(R[r])&&(r=e);let u=R[r]<0?-1:1;P[I(a.rotationIndex)+e]=r;for(let t=0,n=0;t<4;t++){if(t===r)continue;let i=Math.round((u*R[t]/Math.SQRT2+.5)*255);P[I(a.rotation+n++)+e]=V(i,0,255)}for(let n=0;n<3;n++)P[I(a.scale+n)+e]=ee(s[t*3+n],p[n],m[n]),P[I(a.dc+n)+e]=ee(l[t*3+n],h[n],g[n]);j&&(P[I(a.label)+e]=j[t]&255,P[I(a.label+1)+e]=j[t]>>8);let[d,f,_]=[0,1,2].map(e=>Math.exp(s[t*3+e]));x(R,d,f,_,z);for(let e=0;e<3;e++)L[e]=Math.min(L[e],i[t*3+e]-z[e]),L[3+e]=Math.max(L[3+e],i[t*3+e]+z[e])}return A&&P.set(new Uint8Array(A.buffer),I(N)),{payload:P,row:{g:t.g,i:t.i,j:t.j,count:n,quadrants:O,box:L,offset:0,length:0}}}function oe(e,t,n){e.setUint8(t,n.g),e.setUint8(t+1,n.i),e.setUint8(t+2,n.j),e.setUint32(t+4,n.count,!0),n.quadrants.forEach((n,r)=>e.setUint32(t+8+4*r,n,!0)),n.box.forEach((n,r)=>e.setFloat32(t+20+4*r,n,!0)),e.setUint32(t+44,n.offset,!0),e.setUint32(t+48,n.length,!0)}function se(e,t,n){for(let r=0;r<4;r++)e[t+r]=n.charCodeAt(r)}function U(e){return Uint8Array.from({length:8},(t,n)=>parseInt(e.slice(2*n,2*n+2),16))}async function ce(e,t){let n=await l(e),r=new Uint8Array(8+n.length);return se(r,0,t),new DataView(r.buffer).setUint16(4,1,!0),r.set(n,8),r}async function le(e){let{rows:t,children:n,descendants:r}=e,a=new Uint8Array(40+t.length*52+n.length*9+r.length*11),o=new DataView(a.buffer);o.setUint8(0,e.level),o.setInt32(4,e.e,!0),o.setInt32(8,e.n,!0),a.set(U(e.pack),12),o.setUint32(20,e.packSize,!0),e.col&&a.set(U(e.col),24),o.setUint16(32,t.length,!0),o.setUint8(34,n.length),o.setUint16(36,r.length,!0);let s=40;for(let e of t)oe(o,s,e),s+=52;for(let e of n)o.setUint8(s,e.quadrant),a.set(U(e.idx),s+1),s+=9;for(let e of r)o.setUint8(s,e.depth),o.setUint8(s+1,e.column),o.setUint8(s+2,e.row),a.set(U(e.idx),s+3),s+=11;return ce(a,i)}function ue(e){let t=3421674724,n=2216829733;for(let r=0;r<e.length;r++){n=(n^e[r])>>>0;let i=n*435;t=t*435+(n<<8>>>0)+Math.floor(i/4294967296)>>>0,n=i>>>0}return t.toString(16).padStart(8,`0`)+n.toString(16).padStart(8,`0`)}function de(e){return()=>{e=e+1831565813|0;let t=Math.imul(e^e>>>15,1|e);return t=t+Math.imul(t^t>>>7,61|t)^t,((t^t>>>14)>>>0)/2**32}}let W=16384,fe=1408,pe=65535,G=324121,me=.4,he=[[12,1/7e3,0,1/9e3,.7],[9,1/2300,1.1,1/3100,.4],[6,1/800,.3,1/1100,2],[3,1/150,.9,1/210,1.7]],ge=he.reduce((e,[t,n,,r])=>e+t*(n+r),0),_e=Float32Array.from({length:fe*9},(e,t)=>{let n=Math.floor(t/9),r=t%9,i=(n*(37+2*r)+11*r)%255-127;return r===0&&(i=n%255-127),r===1&&(i=20*Math.floor(n/255)-50),i/127*.04}),K=new TextEncoder,ve={version:1,crs:`synthetic`,height:`synthetic`,leaf:250,roots:[512e4,6144e3].map(e=>({level:12,e:0,n:e})).map(e=>({...e,idx:m(e,q(e,`idx`),`idx`)})),extent:{e:[44e4,9e5],n:[605e4,641e4],h:[0,80]}},ye=K.encode(JSON.stringify(ve));function be(){return async(t,n)=>{if(t.endsWith(`world.json`))return Se(ye,n);let r=g(t),i=t.slice(t.lastIndexOf(`.`)+1);if(!r||i!==`idx`&&i!==`pack`||!Ce(r)||r.hash!==q(r,i))throw new e(`no file ${t}`);return i===`idx`?Se(await ke(r),n):Ae(r,n,t)}}function xe(e,t){let n=30;for(let[r,i,a,o,s]of he)n+=r*Math.sin(i*e+a)*Math.sin(o*t+s);return n}function Se(e,t){return t?e.slice(t[0],t[1]):e.slice()}function q(e,t){return ue(K.encode(`${e.level}/${e.e}/${e.n}.${t}`))}function J(e,t,n){let{e:r,n:i}=ve.extent;return e<r[1]&&e+n>r[0]&&t<i[1]&&t+n>i[0]}function Ce({level:e,e:t,n}){let r=250*2**e;return e<=12&&t%r===0&&n%r===0&&J(t,n,r)}function we(e){return e===0?[1,0]:e===12?[16,15,14,13]:[e+1]}function Te(e){let t=0;for(let n=0;n<8;n++)t|=(e>>2*n&1)<<n;return t}function Ee(e){let t=[];for(let n of we(e.level)){let i=r*2**n,a=250*2**e.level/i;for(let r=0;r<a*a;r++){let a=Te(r),o=Te(r>>1);J(e.e+a*i,e.n+o*i,i)&&t.push({g:n,i:a,j:o})}}return t}function De(e,t){let n=e.level-t,r=250*2**n,i=[];for(let a=0;n>=0&&a<2**t;a++)for(let o=0;o<2**t;o++){let t={level:n,e:e.e+o*r,n:e.n+a*r};J(t.e,t.n,r)&&i.push({column:o,row:a,unit:t})}return i}function Oe(e,{g:t,i:n,j:i},a){let o=r*2**t,s=o/128,c=3*1.2*s,l=1.17*s,u=xe(e.e+(n+.5)*o,e.n+(i+.5)*o),d=ge*o/2,f=Float32Array.of(n*o-c,i*o-c,Math.max(0,u-d)-l,(n+1)*o+c,(i+1)*o+c,Math.min(60,u+d)+l),p=W/4;return{g:t,i:n,j:i,count:W,quadrants:[p,2*p,3*p],box:f,offset:a,length:G}}function ke(e){let t=Ee(e),n=[];for(let{column:t,row:r,unit:i}of De(e,1))n.push({quadrant:t+2*r,idx:q(i,`idx`)});let r=[];for(let t of[2,3])for(let{column:n,row:i,unit:a}of De(e,t))r.push({depth:t,column:n,row:i,idx:q(a,`idx`)});return le({level:e.level,e:e.e,n:e.n,pack:q(e,`pack`),packSize:t.length*324173+16,col:null,rows:t.map((t,n)=>Oe(e,t,n*G)),children:n,descendants:r})}function Ae(e,t,n){let r=Ee(e);if(!t||t[0]%G!=0||t[1]%G!=0||t[0]>=t[1]||t[1]>r.length*G)throw Error(`${n}: the synthetic world serves whole pages only`);let i=new Uint8Array(t[1]-t[0]);for(let n=0;n<i.length;n+=G)je(Ne(e,r[(t[0]+n)/G]),i,n);return i}function je(e,t,n){let r=n;for(let n=0;n<e.length;n+=pe){let i=Math.min(pe,e.length-n);t[r]=+(n+i===e.length),t[r+1]=i&255,t[r+2]=i>>8,t[r+3]=~i&255,t[r+4]=~i>>8&255,t.set(e.subarray(n,n+i),r+5),r+=5+i}}let Me=e=>de(parseInt(ue(K.encode(e)).slice(8),16));function Ne(e,{g:t,i,j:a}){let o=W,s=r*2**t/128,c=Me(`${e.level}/${e.e}/${e.n}/${t}/${i}/${a}`),l=new Float64Array(3*o),u=new Float32Array(4*o),d=new Float32Array(3*o),f=new Float32Array(o),p=new Float32Array(3*o),m=new Float32Array(9*o);for(let t=0;t<o;t++){let r=(i*128+t%128+.5+me*(2*c()-1))*s,o=(a*128+Math.floor(t/128)+.5+me*(2*c()-1))*s,h=xe(e.e+r,e.n+o);l[3*t]=r,l[3*t+1]=o,l[3*t+2]=h;let g=Math.PI*c(),_=.1/2*(2*c()-1);u[4*t]=Math.cos(g)*Math.cos(_),u[4*t+1]=Math.cos(g)*Math.sin(_),u[4*t+2]=Math.sin(g)*Math.sin(_),u[4*t+3]=Math.sin(g)*Math.cos(_);for(let e=0;e<2;e++)d[3*t+e]=Math.log(s*(.6+.6*c()));d[3*t+2]=Math.log(s*.15*(.5+.5*c())),f[t]=.35+.65*c();let v=h/60,y=.5+.5*Math.sin((e.e+r)/410)*Math.sin((e.n+o)/330);p[3*t]=(.32+.2*v+.1*y-.5)/n,p[3*t+1]=(.42+.15*y-.1*v-.5)/n,p[3*t+2]=(.25+.1*v+.04*c()-.5)/n;let b=t%fe;m.set(_e.subarray(9*b,9*(b+1)),9*t)}return ae({count:o,position:l,rotation:u,scale:d,alpha:f,dc:p,sh:m},{g:t,i,j:a}).payload}let Y=2**20,X=self,Z,Pe=/* @__PURE__ */ new Map,Q=/* @__PURE__ */ new Map,Fe=B(e=>X.postMessage(e,[]));X.onmessage=({data:e})=>{`fetched`in e?Fe.take(e):e.type===`init`?(Z=Ie(e),Z.catch(()=>void 0)):e.type===`abort`?Q.get(e.id)?.abort():Re(e)};async function Ie(e){return e.source===`synthetic`?{fetch:be(),cache:await $({...e,cache:`none`}),base:e.base}:{fetch:e.source===`page`?Fe.fetch:t,cache:await $(e),base:e.base}}async function $({cache:e,shard:t,shards:n}){return e===`memory`?N(256*Y):e===`none`?{get:async()=>void 0,put:async()=>void 0}:(t===0&&F(n),await P(await Le()/n,t)??N(256*Y))}async function Le(){try{let{quota:e}=await navigator.storage.estimate();return Math.min(4096*Y,e===void 0?1/0:e/2)}catch{return 4096*Y}}async function Re(t){let{id:n}=t,r=new AbortController;Q.set(n,r);try{if(!Z)throw Error(`stream worker used before init`);let e=await Z;if(t.type===`idx`)X.postMessage({id:n,value:await O(e,t.path)},[]);else{for await(let i of k(e,t.batch,Pe,r.signal))X.postMessage({id:n,page:i},ze(i));X.postMessage({id:n,done:!0},[])}}catch(t){let r=t instanceof Error?t.message:String(t);X.postMessage({id:n,error:r,missing:t instanceof e},[])}finally{Q.delete(n)}}function ze({page:e}){let t=/* @__PURE__ */ new Set;for(let n of[e.geometry,e.colour,e.params,e.clusters])t.add(n.buffer);return[...t]}})();", dt = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", ut], { type: "text/javascript;charset=utf-8" });
1874
+ function ft(e) {
1631
1875
  let t;
1632
1876
  try {
1633
- if (t = et && (self.URL || self.webkitURL).createObjectURL(et), !t) throw "";
1877
+ if (t = dt && (self.URL || self.webkitURL).createObjectURL(dt), !t) throw "";
1634
1878
  let n = new Worker(t, { name: e?.name });
1635
1879
  return n.addEventListener("error", () => {
1636
1880
  (self.URL || self.webkitURL).revokeObjectURL(t);
1637
1881
  }), n;
1638
1882
  } catch {
1639
- return new Worker("data:text/javascript;charset=utf-8," + encodeURIComponent($e), { name: e?.name });
1883
+ return new Worker("data:text/javascript;charset=utf-8," + encodeURIComponent(ut), { name: e?.name });
1640
1884
  }
1641
1885
  }
1642
1886
  //#endregion
1643
1887
  //#region src/engine/stream/loader.ts
1644
- var nt = class {
1888
+ var pt = class {
1645
1889
  #e;
1646
1890
  #t = /* @__PURE__ */ new Map();
1647
1891
  #n = 0;
1648
1892
  constructor(e) {
1649
1893
  let { base: t, cache: n, source: r, workers: i } = e;
1650
1894
  this.#e = Array.from({ length: i }, (e, a) => {
1651
- let o = new tt(), s = { worker: o }, c = typeof r == "function" ? Qe(r, (e, t) => o.postMessage(e, t)) : void 0;
1895
+ let o = new ft(), s = { worker: o }, c = typeof r == "function" ? lt(r, (e, t) => o.postMessage(e, t)) : void 0;
1652
1896
  o.onmessage = ({ data: e }) => {
1653
1897
  "id" in e ? this.#i(e) : c?.(e);
1654
1898
  }, o.onerror = () => this.#a(s);
@@ -1672,8 +1916,8 @@ var nt = class {
1672
1916
  return this.#r(this.#e[0], t);
1673
1917
  }
1674
1918
  batch(e, t, n) {
1675
- if (t.aborted) return Promise.reject(V());
1676
- let r = this.#e[rt(`${e.pack}#${e.window}`, this.#e.length)], i = {
1919
+ if (t.aborted) return Promise.reject(K());
1920
+ let r = this.#e[mt(`${e.pack}#${e.window}`, this.#e.length)], i = {
1677
1921
  type: "batch",
1678
1922
  id: ++this.#n,
1679
1923
  batch: e
@@ -1682,7 +1926,7 @@ var nt = class {
1682
1926
  }
1683
1927
  destroy() {
1684
1928
  for (let { worker: e } of this.#e) e.terminate();
1685
- for (let e of this.#t.values()) e.reject(V());
1929
+ for (let e of this.#t.values()) e.reject(K());
1686
1930
  }
1687
1931
  #r(e, t, n, r) {
1688
1932
  if (e.error) return Promise.reject(e.error);
@@ -1694,7 +1938,7 @@ var nt = class {
1694
1938
  s(), e.worker.postMessage({
1695
1939
  type: "abort",
1696
1940
  id: i
1697
- }), o(V());
1941
+ }), o(K());
1698
1942
  };
1699
1943
  this.#t.set(i, {
1700
1944
  slot: e,
@@ -1717,237 +1961,56 @@ var nt = class {
1717
1961
  for (let t of this.#t.values()) t.slot === e && t.reject(e.error);
1718
1962
  }
1719
1963
  };
1720
- function rt(e, t) {
1964
+ function mt(e, t) {
1721
1965
  let n = 2166136261;
1722
1966
  for (let t = 0; t < e.length; t++) n = Math.imul(n ^ e.charCodeAt(t), 16777619);
1723
1967
  return (n >>> 0) % t;
1724
1968
  }
1725
- function V() {
1969
+ function K() {
1726
1970
  return new DOMException("The request was aborted.", "AbortError");
1727
1971
  }
1728
1972
  //#endregion
1729
- //#region src/engine/world/tree.ts
1730
- var H = (e, t, n) => (e * 256 + t) * 256 + n, it = class {
1731
- roots = [];
1732
- pages = /* @__PURE__ */ new Map();
1733
- units = /* @__PURE__ */ new Map();
1734
- #e;
1735
- #t;
1736
- #n = 0;
1737
- #r = /* @__PURE__ */ new Map();
1738
- #i = /* @__PURE__ */ new Map();
1739
- constructor(e) {
1740
- this.#e = new Set(e.roots.map(U)), this.#t = Math.max(...e.roots.map((e) => e.level));
1741
- }
1742
- add(e, t) {
1743
- let n = U(e), r = this.units.get(n);
1744
- if (r) return r;
1745
- let o = /* @__PURE__ */ new Map(), s = {
1746
- level: e.level,
1747
- e: e.e,
1748
- n: e.n,
1749
- idx: e,
1750
- path: t,
1751
- parent: null,
1752
- pages: [],
1753
- grid: o
1754
- };
1755
- for (let t of e.rows) {
1756
- let n = t.box, r = {
1757
- id: this.#n++,
1758
- unit: s,
1759
- row: t,
1760
- g: t.g,
1761
- box: Float64Array.of(e.e + n[0], e.n + n[1], n[2], e.e + n[3], e.n + n[4], n[5]),
1762
- parent: null,
1763
- children: [
1764
- null,
1765
- null,
1766
- null,
1767
- null
1768
- ]
1769
- };
1770
- this.pages.set(r.id, r), s.pages.push(r), o.set(H(t.g, t.i, t.j), r);
1771
- }
1772
- this.units.set(n, s), this.#r.delete(n);
1773
- for (let t = e.level + 1; t <= this.#t; t++) {
1774
- let n = U(W(e, t));
1775
- this.#i.set(n, (this.#i.get(n) ?? 0) + 1);
1776
- }
1777
- let c = new Set(e.rows.map((e) => e.g)), l = Math.max(...c), u = e.level === 0 ? 0 : e.level + 1, d = new Set(e.children.map((e) => e.quadrant));
1778
- for (let e of s.pages) {
1779
- let { g: t, i: n, j: r } = e.row;
1780
- c.has(t + 1) && (e.parent = o.get(H(t + 1, n >> 1, r >> 1)) ?? null);
1781
- for (let i = 0; i < 4; i++) {
1782
- let a = 2 * n + (i & 1), s = 2 * r + (i >> 1);
1783
- c.has(t - 1) ? e.children[i] = o.get(H(t - 1, a, s)) ?? null : t === u && t > 0 && (e.children[i] = d.has((a >> 3) + 2 * (s >> 3)) ? void 0 : null);
1784
- }
1785
- }
1786
- if (this.#e.has(n)) for (let e of s.pages) e.g === l && this.roots.push(e);
1787
- else {
1788
- let t = this.units.get(U(W(e, e.level + 1)));
1789
- t && this.#o(t, s);
1790
- }
1791
- let f = a(s.idx);
1792
- for (let { key: t } of f) {
1793
- let n = t.level === e.level - 1 ? this.units.get(U(t)) : void 0;
1794
- n && this.#o(s, n);
1795
- }
1796
- for (let { key: e, hash: t } of f) {
1797
- let n = U(e);
1798
- if (this.units.has(n)) continue;
1799
- let r = this.#r.get(n);
1800
- if (r && r.from <= s.level) continue;
1801
- let a = this.#s(s, e), o = {
1802
- ...e,
1803
- path: i(e, t, "idx"),
1804
- over: a
1805
- };
1806
- this.#r.set(n, {
1807
- wanted: o,
1808
- from: s.level
1809
- });
1810
- }
1811
- return s;
1812
- }
1813
- wanted() {
1814
- return Array.from(this.#r.values(), (e) => e.wanted);
1815
- }
1816
- ground(e, t) {
1817
- for (let n of this.roots) {
1818
- let [r, i, a] = at(n);
1819
- if (e < r || e >= r + a || t < i || t >= i + a) continue;
1820
- let o = n;
1821
- for (;;) {
1822
- let n = +(e >= r + a / 2), s = +(t >= i + a / 2), c = o.children[n + 2 * s];
1823
- if (!c) return o.box[2];
1824
- o = c, [r, i, a] = at(o);
1825
- }
1826
- }
1827
- }
1828
- prune(e) {
1829
- let t = [], n = [...this.units.values()].sort((e, t) => e.level - t.level);
1830
- for (let r of n) {
1831
- let n = U(r);
1832
- if (this.#e.has(n) || this.#i.has(n) || r.pages.some((t) => e.has(t.id))) continue;
1833
- for (let e of r.pages) this.pages.delete(e.id), t.push(e.id);
1834
- let i = r.parent;
1835
- if (i) {
1836
- let e = 250 * 2 ** r.level, t = (r.e - i.e) / e, n = (r.n - i.n) / e, a = i.idx.children.some((e) => e.quadrant === t + 2 * n);
1837
- for (let e of this.#s(i, r)) e.children.fill(a ? void 0 : null);
1838
- }
1839
- this.units.delete(n);
1840
- for (let e = r.level + 1; e <= this.#t; e++) {
1841
- let t = U(W(r, e)), n = this.#i.get(t) - 1;
1842
- n ? this.#i.set(t, n) : this.#i.delete(t);
1843
- }
1844
- for (let { key: e } of a(r.idx)) this.#a(e);
1845
- this.#a(r);
1846
- }
1847
- return t;
1848
- }
1849
- #a(e) {
1850
- let t = U(e);
1851
- this.#r.delete(t);
1852
- for (let n = e.level + 1; n <= e.level + 3; n++) {
1853
- let r = this.units.get(U(W(e, n)));
1854
- if (!r) continue;
1855
- let o = a(r.idx).find((e) => U(e.key) === t);
1856
- if (!o) continue;
1857
- let s = {
1858
- level: e.level,
1859
- e: e.e,
1860
- n: e.n,
1861
- path: i(o.key, o.hash, "idx"),
1862
- over: this.#s(r, e)
1863
- };
1864
- this.#r.set(t, {
1865
- wanted: s,
1866
- from: r.level
1867
- });
1868
- return;
1869
- }
1870
- }
1871
- #o(e, t) {
1872
- t.parent = e;
1873
- let n = t.level + 1, r = 250 * 2 ** t.level, i = (t.e - e.e) / r, a = (t.n - e.n) / r, o = e.grid;
1874
- for (let n of this.#s(e, t)) n.children.fill(null);
1875
- for (let e of t.pages) {
1876
- if (e.g !== n) continue;
1877
- let t = 8 * i + e.row.i, r = 8 * a + e.row.j, s = o.get(H(n + 1, t >> 1, r >> 1)) ?? null;
1878
- e.parent = s, s && (s.children[(t & 1) + 2 * (r & 1)] = e);
1879
- }
1880
- }
1881
- #s(t, n) {
1882
- let r = t.level + 1, i = e * 2 ** r, a = (n.e - t.e) / i, o = (n.n - t.n) / i, s = 250 * 2 ** n.level / i, c = [];
1883
- for (let e = o; e < o + s; e++) for (let n = a; n < a + s; n++) {
1884
- let i = t.grid.get(H(r, n, e));
1885
- i && c.push(i);
1886
- }
1887
- return c;
1888
- }
1889
- };
1890
- function U(e) {
1891
- return `${e.level}/${e.e}/${e.n}`;
1892
- }
1893
- function W(e, t) {
1894
- let n = 250 * 2 ** t;
1895
- return {
1896
- level: t,
1897
- e: Math.floor(e.e / n) * n,
1898
- n: Math.floor(e.n / n) * n
1899
- };
1900
- }
1901
- function at(t) {
1902
- let n = e * 2 ** t.g;
1903
- return [
1904
- t.unit.e + t.row.i * n,
1905
- t.unit.n + t.row.j * n,
1906
- n
1907
- ];
1908
- }
1909
- //#endregion
1910
1973
  //#region src/engine/engine.ts
1911
- var G = class extends Error {
1974
+ var ht = class extends Error {
1912
1975
  constructor() {
1913
1976
  super("This viewer needs WebGPU. Update to the latest Chrome, Edge, Safari or Firefox."), this.name = "NoWebGpu";
1914
1977
  }
1915
1978
  };
1916
- async function ot(e, t) {
1979
+ async function gt(e, t) {
1917
1980
  let n = await p();
1918
- if (!n) throw new G();
1981
+ if (!n) throw new ht();
1919
1982
  let r = e.getContext("webgpu");
1920
- if (!r) throw n.device.destroy(), new G();
1983
+ if (!r) throw n.device.destroy(), new ht();
1921
1984
  r.configure({
1922
1985
  device: n.device,
1923
1986
  format: n.format,
1924
1987
  alphaMode: "opaque"
1925
1988
  }), (t.cache ?? "opfs") === "opfs" && navigator.storage?.persist().catch(() => !1);
1926
1989
  let { source: i = "http" } = t;
1927
- return new Ot(e, n, r, t, i === "synthetic" ? (await import("./synthetic-BeGCzYIv.js")).synthetic() : i === "http" ? s : i);
1990
+ return new Bt(e, n, r, t, i === "synthetic" ? (await import("./synthetic-BeGCzYIv.js")).synthetic() : i === "http" ? s : i);
1928
1991
  }
1929
- var K = .2, st = 25165824, ct = 2.5, lt = 1 + R, ut = 1024, dt = 2048, ft = 100, pt = 2, q = 300, mt = 100, ht = 250, gt = 300, _t = 1e3, vt = 400, yt = 5e3, bt = 60, J = Math.PI / 180, xt = "The viewer was closed.", St = "Another world was opened.";
1930
- function Ct(e, t) {
1992
+ var _t = .2, vt = 25165824, yt = 2.5, bt = 1 + R, xt = 1024, St = 2048, Ct = 100, wt = 2, Tt = 300, Et = 100, Dt = 250, Ot = 300, kt = 1e3, At = 400, jt = 5e3, Mt = 60, q = Math.PI / 180, Nt = "The viewer was closed.", Pt = "Another world was opened.";
1993
+ function Ft(e, t) {
1931
1994
  let n = Math.min(t.maxStorageBufferBindingSize, t.maxBufferSize), r = (t, r) => {
1932
1995
  let i = Math.ceil(e * t / 256) * 256;
1933
1996
  return Math.min(i, Math.floor(n / r / 256) * 256);
1934
1997
  };
1935
1998
  return {
1936
- pool: r(ct, 16),
1937
- frame: r(lt, 24)
1999
+ pool: r(yt, 16),
2000
+ frame: r(bt, 24)
1938
2001
  };
1939
2002
  }
1940
- function wt(e, t) {
2003
+ function It(e, t) {
1941
2004
  let [n, r, i] = t;
1942
- if (Math.hypot(n - e[0], r - e[1], i - e[2]) <= dt) return e;
1943
- let a = (e) => Math.round(e / ut) * ut;
2005
+ if (Math.hypot(n - e[0], r - e[1], i - e[2]) <= St) return e;
2006
+ let a = (e) => Math.round(e / xt) * xt;
1944
2007
  return [
1945
2008
  a(n),
1946
2009
  a(r),
1947
2010
  a(i)
1948
2011
  ];
1949
2012
  }
1950
- function Tt(e, t, n, r, i, a) {
2013
+ function Lt(e, t, n, r, i, a) {
1951
2014
  let o = 2 * e[0] / t[0] - 1, s = 1 - 2 * e[1] / t[1], c = [
1952
2015
  n * o / r[0],
1953
2016
  n * s / r[5],
@@ -1959,29 +2022,29 @@ function Tt(e, t, n, r, i, a) {
1959
2022
  d + a[2]
1960
2023
  ];
1961
2024
  }
1962
- function Et(e, t) {
2025
+ function Rt(e, t) {
1963
2026
  return [
1964
2027
  e.e - t[0],
1965
2028
  e.n - t[1],
1966
2029
  e.h - t[2]
1967
2030
  ];
1968
2031
  }
1969
- function Dt(e, t) {
1970
- e.push(t), e.length > bt && e.shift();
2032
+ function zt(e, t) {
2033
+ e.push(t), e.length > Mt && e.shift();
1971
2034
  }
1972
- var Ot = class {
2035
+ var Bt = class {
1973
2036
  #e;
1974
2037
  #t;
1975
2038
  #n;
1976
2039
  #r;
1977
2040
  #i;
1978
2041
  #a;
1979
- #o = new We();
2042
+ #o;
1980
2043
  #s;
1981
2044
  #c;
1982
2045
  #l;
1983
- #u = new Me();
1984
- #d = new Fe();
2046
+ #u = new Be();
2047
+ #d = new Ee();
1985
2048
  #f = (e) => this.#s.has(e);
1986
2049
  #p = null;
1987
2050
  #m = null;
@@ -2042,13 +2105,13 @@ var Ot = class {
2042
2105
  #ee = !1;
2043
2106
  #te = !1;
2044
2107
  constructor(e, t, n, r, i) {
2045
- this.#e = e, this.#t = t, this.#n = n, this.#r = r, this.#i = i, this.#a = { ...r.settings }, this.#O = [
2108
+ this.#e = e, this.#t = t, this.#n = n, this.#r = r, this.#i = i, this.#a = { ...r.settings }, this.#o = this.#re(), this.#O = [
2046
2109
  e.width,
2047
2110
  e.height,
2048
2111
  1
2049
2112
  ], this.#k = [Math.max(1, e.width), Math.max(1, e.height)];
2050
- let { pool: a, frame: o } = Ct(this.#ne, t.device.limits);
2051
- this.#s = new _(t.device, a), this.#c = new we(t, n, this.#s, o), this.#l = o, t.device.lost.then((e) => {
2113
+ let { pool: a, frame: o } = Ft(this.#ne, t.device.limits);
2114
+ this.#s = new _(t.device, a), this.#c = new Ce(t, n, this.#s, o), this.#l = o, t.device.lost.then((e) => {
2052
2115
  if (!this.#ee) {
2053
2116
  this.#te = !0, cancelAnimationFrame(this.#A);
2054
2117
  for (let t of this.#Z) t(e.message || e.reason);
@@ -2056,53 +2119,54 @@ var Ot = class {
2056
2119
  });
2057
2120
  }
2058
2121
  async open(e) {
2059
- if (this.#ee) throw Error(xt);
2122
+ if (this.#ee) throw Error(Nt);
2060
2123
  let t = ++this.#_, i = () => {
2061
- if (this.#ee) throw Error(xt);
2062
- if (t !== this.#_) throw Error(St);
2124
+ if (this.#ee) throw Error(Nt);
2125
+ if (t !== this.#_) throw Error(Pt);
2063
2126
  }, { cache: a = "opfs", source: o = "http" } = this.#r;
2064
2127
  o === "http" && (e = new URL(e, document.baseURI).href);
2065
2128
  let s = await this.#i(e, null), c = r(JSON.parse(new TextDecoder().decode(s)));
2066
2129
  i();
2067
- let l = new nt({
2130
+ let l = new pt({
2068
2131
  base: n(e),
2069
2132
  workers: Math.min(6, Math.max(2, navigator.hardwareConcurrency - 2)),
2070
2133
  cache: a,
2071
2134
  source: o
2072
- }), u = new it(c);
2135
+ }), u = new Ie(c);
2073
2136
  try {
2074
2137
  let e = await Promise.all(c.roots.map((e) => l.idx(e.idx)));
2075
2138
  i(), e.forEach((e, t) => u.add(e, c.roots[t].idx));
2076
2139
  } catch (e) {
2077
2140
  throw l.destroy(), e;
2078
2141
  }
2079
- return this.#g?.destroy(), this.#g = null, this.#h?.destroy(), this.#p = e, this.#m = u, this.#h = l, this.#N = null, this.#u = new Me(), this.#d = new Fe(), this.#s.clear(), this.#g = this.#ie(u, l), this.#I = !0, this.requestFrame(), c;
2142
+ return this.#g?.destroy(), this.#g = null, this.#h?.destroy(), this.#p = e, this.#m = u, this.#h = l, this.#N = null, this.#u = new Be(), this.#d = new Ee(), this.#s.clear(), this.#g = this.#oe(u, l), this.#I = !0, this.requestFrame(), c;
2080
2143
  }
2081
2144
  setCamera(e) {
2082
2145
  if (this.#ee) return;
2083
2146
  let t = performance.now(), n = t - this.#b, r = this.#y, i = this.#x;
2084
- if (n > ht) i.fill(0);
2147
+ if (n > Dt) i.fill(0);
2085
2148
  else if (n > 0) {
2086
- let t = 1 - Math.exp(-n / mt);
2149
+ let t = 1 - Math.exp(-n / Et);
2087
2150
  i[0] += t * ((e.e - r.e) / n - i[0]), i[1] += t * ((e.n - r.n) / n - i[1]), i[2] += t * ((e.h - r.h) / n - i[2]);
2088
2151
  }
2089
2152
  this.#b = t, this.#y = { ...e }, this.#F = !0, this.#$ || this.requestFrame();
2090
2153
  }
2091
2154
  configure(e) {
2092
2155
  if (this.#ee) return;
2156
+ let { budget: t, budgetRange: n } = this.#a;
2093
2157
  this.#a = {
2094
2158
  ...this.#a,
2095
2159
  ...e
2096
2160
  };
2097
- let { pool: t, frame: n } = Ct(this.#ne, this.#t.device.limits);
2098
- (t > this.#s.capacity || n > this.#l) && this.#re(t, n), this.#I = !0, this.requestFrame();
2161
+ let r = this.#a;
2162
+ (r.budget !== t || !((e, t) => e?.join() === t?.join())(r.budgetRange, n)) && (this.#o = this.#re(), this.#le()), this.#ie();
2099
2163
  }
2100
2164
  resize(e, t, n) {
2101
2165
  this.#ee || (this.#O = [
2102
2166
  e,
2103
2167
  t,
2104
2168
  n
2105
- ], this.#se());
2169
+ ], this.#le());
2106
2170
  }
2107
2171
  pick(e, t) {
2108
2172
  if (this.#ee || this.#te) return Promise.resolve(null);
@@ -2118,6 +2182,10 @@ var Ot = class {
2118
2182
  ground(e, t) {
2119
2183
  return this.#m?.ground(e, t);
2120
2184
  }
2185
+ async groundAt(e, t) {
2186
+ let n = this.#m, r = this.#h;
2187
+ if (n && r) return await n.load(e, t, (e) => r.idx(e)), this.#R = !0, this.requestFrame(), n.ground(e, t);
2188
+ }
2121
2189
  heights(e, t, n) {
2122
2190
  return this.#ee || this.#te ? Promise.resolve(null) : new Promise((r) => {
2123
2191
  this.#Y.push({
@@ -2131,8 +2199,8 @@ var Ot = class {
2131
2199
  stats() {
2132
2200
  let e = this.#s, t = this.#g, n = this.#c.stats();
2133
2201
  return {
2134
- frameMs: B(this.#W, .5),
2135
- cpuMs: B(this.#G, .5),
2202
+ frameMs: et(this.#W, .5),
2203
+ cpuMs: et(this.#G, .5),
2136
2204
  gpuMs: n.gpuMs,
2137
2205
  passes: n.passes,
2138
2206
  drawn: n.drawn,
@@ -2149,6 +2217,7 @@ var Ot = class {
2149
2217
  buffers: n.bytes
2150
2218
  },
2151
2219
  capacity: e.capacity,
2220
+ budget: this.#ne,
2152
2221
  scale: this.#o.scale,
2153
2222
  loading: t !== null && !t.idle
2154
2223
  };
@@ -2161,8 +2230,8 @@ var Ot = class {
2161
2230
  use(e) {
2162
2231
  if (this.#ee) return () => {};
2163
2232
  let t = e({
2164
- shade: (e) => this.#oe(this.#K, e, this.#c.shade(e)),
2165
- post: (e) => this.#oe(this.#q, e, this.#c.post(e))
2233
+ shade: (e) => this.#ce(this.#K, e, this.#c.shade(e)),
2234
+ post: (e) => this.#ce(this.#q, e, this.#c.post(e))
2166
2235
  });
2167
2236
  return () => {
2168
2237
  this.#ee || t();
@@ -2170,7 +2239,7 @@ var Ot = class {
2170
2239
  }
2171
2240
  warm() {
2172
2241
  let e = performance.now();
2173
- e - this.#V < _t || (this.#H = e + vt, this.requestFrame());
2242
+ e - this.#V < kt || (this.#H = e + At, this.requestFrame());
2174
2243
  }
2175
2244
  onFrame(e) {
2176
2245
  return this.#Q.add(e), () => this.#Q.delete(e);
@@ -2179,7 +2248,7 @@ var Ot = class {
2179
2248
  return this.#Z.add(e), () => this.#Z.delete(e);
2180
2249
  }
2181
2250
  requestFrame() {
2182
- this.#ee || this.#te || this.#r.manual || this.#A || (this.#A = requestAnimationFrame(this.#ce));
2251
+ this.#ee || this.#te || this.#r.manual || this.#A || (this.#A = requestAnimationFrame(this.#ue));
2183
2252
  }
2184
2253
  async step() {
2185
2254
  if (this.#ee || this.#te) return {
@@ -2187,7 +2256,7 @@ var Ot = class {
2187
2256
  gpuMs: 0,
2188
2257
  passes: {}
2189
2258
  };
2190
- let { done: e, cpuMs: t } = this.#le(!1);
2259
+ let { done: e, cpuMs: t } = this.#de(!1);
2191
2260
  await e;
2192
2261
  let { gpuMs: n, passes: r } = this.#c.stats();
2193
2262
  return {
@@ -2206,29 +2275,36 @@ var Ot = class {
2206
2275
  }
2207
2276
  }
2208
2277
  get #ne() {
2209
- let e = Math.floor(Ct(Infinity, this.#t.device.limits).frame / lt);
2210
- return Math.max(1, Math.min(this.#a.budget, e));
2278
+ return this.#o.budget;
2279
+ }
2280
+ #re() {
2281
+ let { budget: e, budgetRange: t = [e, e] } = this.#a, n = Math.floor(Ft(Infinity, this.#t.device.limits).frame / bt), r = (e) => Math.max(1, Math.min(e, n));
2282
+ return new tt(r(e), [r(t[0]), r(t[1])]);
2211
2283
  }
2212
- #re(e, t) {
2213
- this.#g?.destroy(), this.#g = null, this.#c.destroy(), this.#s.destroy(), this.#s = new _(this.#t.device, e), this.#s.setOrigin(this.#S);
2214
- let n = new we(this.#t, this.#n, this.#s, t);
2284
+ #ie() {
2285
+ let { pool: e, frame: t } = Ft(this.#ne, this.#t.device.limits);
2286
+ (e > this.#s.capacity || t > this.#l) && this.#ae(e, t), this.#I = !0, this.requestFrame();
2287
+ }
2288
+ #ae(e, t) {
2289
+ this.#s.grow(e), this.#c.destroy();
2290
+ let n = new Ce(this.#t, this.#n, this.#s, t);
2215
2291
  n.resize(...this.#k);
2216
2292
  for (let e of this.#K) e.off = n.shade(e.hook);
2217
2293
  for (let e of this.#q) e.off = n.post(e.hook);
2218
- this.#c = n, this.#l = t, this.#m && this.#h && (this.#g = this.#ie(this.#m, this.#h)), this.#N = null, this.#I = !0, this.requestFrame();
2294
+ this.#c = n, this.#l = t;
2219
2295
  }
2220
- #ie(e, t) {
2221
- return new Ze(e, t, this.#s, () => {
2296
+ #oe(e, t) {
2297
+ return new ct(e, t, this.#s, () => {
2222
2298
  this.#R = !0, this.requestFrame();
2223
- }, this.#ae);
2299
+ }, this.#se);
2224
2300
  }
2225
- #ae = () => {
2301
+ #se = () => {
2226
2302
  let e = this.#p, t = this.#m;
2227
2303
  this.#v || this.#ee || e === null || (this.#v = !0, this.open(e).then(() => this.#v = !1, () => {
2228
- this.#v = !1, this.#m === t && setTimeout(this.#ae, yt);
2304
+ this.#v = !1, this.#m === t && setTimeout(this.#se, jt);
2229
2305
  }));
2230
2306
  };
2231
- #oe(e, t, n) {
2307
+ #ce(e, t, n) {
2232
2308
  let r = {
2233
2309
  hook: t,
2234
2310
  off: n
@@ -2238,71 +2314,71 @@ var Ot = class {
2238
2314
  this.#ee || t < 0 || (e.splice(t, 1), r.off(), this.requestFrame());
2239
2315
  };
2240
2316
  }
2241
- #se() {
2242
- let [e, t, n] = this.#O, r = Ue(e, t, n, this.#o.scale), i = Math.max(1, Math.round(e * r)), a = Math.max(1, Math.round(t * r));
2317
+ #le() {
2318
+ let [e, t, n] = this.#O, r = $e(e, t, n, this.#o.scale), i = Math.max(1, Math.round(e * r)), a = Math.max(1, Math.round(t * r));
2243
2319
  (i !== this.#k[0] || a !== this.#k[1]) && (this.#k = [i, a], this.#e.width = i, this.#e.height = a, this.#c.resize(i, a), this.#I = !0, this.requestFrame());
2244
2320
  }
2245
- #ce = (e) => {
2321
+ #ue = (e) => {
2246
2322
  if (this.#A = 0, !(this.#ee || this.#te)) {
2247
- if (this.#j >= pt) this.#M = !0;
2323
+ if (this.#j >= wt) this.#M = !0;
2248
2324
  else {
2249
2325
  this.#$ = !0;
2250
2326
  for (let t of this.#Q) t(e);
2251
- this.#$ = !1, this.#j++, this.#le(!0).done.finally(() => {
2327
+ this.#$ = !1, this.#j++, this.#de(!0).done.finally(() => {
2252
2328
  this.#j--, this.#M && (this.#M = !1, this.requestFrame());
2253
2329
  });
2254
2330
  }
2255
2331
  }
2256
2332
  };
2257
- #le(e) {
2333
+ #de(e) {
2258
2334
  let t = performance.now(), n = this.#o, r = this.#s, i = this.#y, [a, o] = this.#k, s = this.#a.viewDistance, c = this.#F;
2259
2335
  this.#F = !1;
2260
- let l = wt(this.#S, [
2336
+ let l = It(this.#S, [
2261
2337
  i.e,
2262
2338
  i.n,
2263
2339
  i.h
2264
2340
  ]);
2265
2341
  l !== this.#S && (this.#S = l, r.setOrigin(l));
2266
- let u = Et(i, l);
2267
- y(this.#C, u, i.yaw * J, i.pitch * J), b(this.#w, i.fov * J, a / o, K, s);
2342
+ let u = Rt(i, l);
2343
+ y(this.#C, u, i.yaw * q, i.pitch * q), b(this.#w, i.fov * q, a / o, _t, s);
2268
2344
  let d = {
2269
2345
  time: t,
2270
2346
  width: a,
2271
2347
  height: o,
2272
2348
  view: new Float32Array(this.#C),
2273
2349
  proj: new Float32Array(this.#w),
2274
- near: K,
2350
+ near: _t,
2275
2351
  far: s,
2276
2352
  camera: Float32Array.from(u),
2277
2353
  origin: l
2278
2354
  };
2279
- Ee(this.#D, S(this.#E, d.proj, d.view));
2355
+ Te(this.#D, S(this.#E, d.proj, d.view));
2280
2356
  let f = this.#m, p = this.#g, m = this.#R || this.#I || this.#z, h = this.#N, g = h;
2281
2357
  if (f && p) {
2282
- let e = !p.idle && t - this.#P >= ft, n = this.#I || this.#R || this.#z || this.#L;
2283
- (!g || c || e || n) && (g = this.#ue(f, t, c));
2358
+ let e = !p.idle && t - this.#P >= Ct, n = this.#I || this.#R || this.#z || this.#L;
2359
+ (!g || c || e || n) && (g = this.#fe(f, t, c));
2284
2360
  }
2285
2361
  let _ = this.#B, v = this.#z;
2286
- g && p && (g !== h && (m || !h || g.cut.length !== h.cut.length || g.cut.some((e, t) => e !== h.cut[t])) && ({drawn: _, fading: v} = this.#d.update(g.cut, this.#f, t, this.#ne)), p.update(g, new Set(_.map((e) => e.id)), t)), r.upload(st) > 0 && (this.#R = !0), (_ !== this.#B || this.#R) && r.draw(_), this.#z = v, this.#B = _;
2287
- let x = this.#J.splice(0).map((e) => this.#fe(e, d)), C = this.#Y.splice(0).map((e) => this.#pe(e, l)), w = this.#c.render(d, this.#D, this.#r.sky, x, C), T = performance.now() - t;
2362
+ g && p && (g !== h && (m || !h || g.cut.length !== h.cut.length || g.cut.some((e, t) => e !== h.cut[t])) && ({drawn: _, fading: v} = this.#d.update(g.cut, this.#f, t, this.#ne)), p.update(g, new Set(_.map((e) => e.id)), t)), r.upload(vt) > 0 && (this.#R = !0), (_ !== this.#B || this.#R) && r.draw(_), this.#z = v, this.#B = _;
2363
+ let x = this.#J.splice(0).map((e) => this.#me(e, d)), C = this.#Y.splice(0).map((e) => this.#he(e, l)), w = this.#c.render(d, this.#D, this.#r.sky, x, C), T = performance.now() - t;
2288
2364
  if (!this.#R && !this.#L && !v && r.queued === 0 && (!p || p.idle) && (!g || g.load.length === 0 && g.idx.length === 0) && this.#X.length) {
2289
2365
  let e = this.#X.splice(0);
2290
2366
  w.then(() => e.forEach((e) => e()));
2291
2367
  }
2292
- e && this.#me(t, T, c);
2368
+ e && this.#ge(t, T, c);
2293
2369
  let E = t < this.#H;
2294
2370
  return (r.queued > 0 || this.#R || this.#L || v || n.scale < 1 || E) && this.requestFrame(), {
2295
2371
  done: w,
2296
2372
  cpuMs: T
2297
2373
  };
2298
2374
  }
2299
- #ue(e, t, n) {
2375
+ #fe(e, t, n) {
2300
2376
  let r = this.#y, i = this.#s, [a, o] = this.#k, s = this.#o.scale, c = this.#ne, l = {
2301
2377
  origin: this.#S,
2302
2378
  width: a / s,
2303
2379
  height: o / s,
2304
- tan: Math.tan(r.fov * J / 2),
2305
- near: K,
2380
+ tan: Math.tan(r.fov * q / 2),
2381
+ near: _t,
2306
2382
  far: this.#a.viewDistance
2307
2383
  }, u = {
2308
2384
  ...l,
@@ -2311,32 +2387,32 @@ var Ot = class {
2311
2387
  budget: c,
2312
2388
  capacity: i.capacity - Math.ceil(R * c),
2313
2389
  now: t,
2314
- predicted: n ? this.#de(l) : void 0
2390
+ predicted: n ? this.#pe(l) : void 0
2315
2391
  }, f = this.#u.plan(e, u, this.#f, d);
2316
2392
  return this.#N = f, this.#P = t, this.#I = !1, this.#L = d.predicted !== void 0, this.#R = !1, f;
2317
2393
  }
2318
- #de(e) {
2394
+ #pe(e) {
2319
2395
  let t = this.#x;
2320
2396
  if (t.every((e) => e === 0)) return;
2321
2397
  let n = this.#y, r = [
2322
- n.e + t[0] * q - e.origin[0],
2323
- n.n + t[1] * q - e.origin[1],
2324
- n.h + t[2] * q - e.origin[2]
2398
+ n.e + t[0] * Tt - e.origin[0],
2399
+ n.n + t[1] * Tt - e.origin[1],
2400
+ n.h + t[2] * Tt - e.origin[2]
2325
2401
  ];
2326
2402
  return {
2327
2403
  ...e,
2328
- view: y(this.#T, r, n.yaw * J, n.pitch * J)
2404
+ view: y(this.#T, r, n.yaw * q, n.pitch * q)
2329
2405
  };
2330
2406
  }
2331
- #fe({ x: e, y: t, resolve: n }, r) {
2332
- let i = this.#C.slice(), a = this.#S, o = [r.width, r.height], s = (n) => Tt([e, t], o, n, r.proj, i, a);
2407
+ #me({ x: e, y: t, resolve: n }, r) {
2408
+ let i = this.#C.slice(), a = this.#S, o = [r.width, r.height], s = (n) => Lt([e, t], o, n, r.proj, i, a);
2333
2409
  return {
2334
2410
  x: e,
2335
2411
  y: t,
2336
2412
  resolve: (e) => n(e === null ? null : s(e))
2337
2413
  };
2338
2414
  }
2339
- #pe({ west: e, south: t, cells: n, resolve: r }, i) {
2415
+ #he({ west: e, south: t, cells: n, resolve: r }, i) {
2340
2416
  return {
2341
2417
  x: e - i[0],
2342
2418
  y: t - i[1],
@@ -2347,21 +2423,23 @@ var Ot = class {
2347
2423
  }
2348
2424
  };
2349
2425
  }
2350
- #me(e, t, n) {
2351
- let r = this.#o, { gpuMs: i } = this.#c.stats(), a = e - this.#V <= Math.max(ht, 2 * i, 2 * t);
2352
- a && Dt(this.#W, e - this.#V), Dt(this.#G, t), this.#V = e, r.sample(i, e, a && n) && this.#se(), n ? this.#U = e : r.scale < 1 && e - this.#U > gt && (r.full(), this.#se());
2426
+ #ge(e, t, n) {
2427
+ let r = this.#o, { gpuMs: i } = this.#c.stats(), a = e - this.#V, o = a <= Math.max(Dt, 2 * i, 2 * t);
2428
+ o && zt(this.#W, a), zt(this.#G, t), this.#V = e;
2429
+ let s = r.budget;
2430
+ r.sample(a, e, o && n, this.#N?.planned ?? 0) && (this.#le(), r.budget !== s && this.#ie()), n ? this.#U = e : r.scale < 1 && e - this.#U > Ot && (r.full(), this.#le());
2353
2431
  }
2354
- }, kt = 45, At = {
2432
+ }, Vt = 45, Ht = {
2355
2433
  yaw: 0,
2356
2434
  pitch: -35,
2357
2435
  dist: 300
2358
- }, Y = {
2436
+ }, J = {
2359
2437
  pitch: [-90, -20],
2360
2438
  dist: [1, 2485],
2361
2439
  clearance: 10,
2362
2440
  floor: 50
2363
- }, jt = .1, X = Math.PI / 180, Mt = (e, [t, n]) => Math.min(n, Math.max(t, e)), Nt = (e) => (e % 360 + 360) % 360, Pt = (e, t) => (t - e + 540) % 360 - 180;
2364
- function Ft({ leaf: e, roots: t, extent: n }) {
2441
+ }, Ut = .1, Y = Math.PI / 180, Wt = (e, [t, n]) => Math.min(n, Math.max(t, e)), Gt = (e) => (e % 360 + 360) % 360, Kt = (e, t) => (t - e + 540) % 360 - 180;
2442
+ function qt({ leaf: e, roots: t, extent: n }) {
2365
2443
  let r = {
2366
2444
  e: (n.e[0] + n.e[1]) / 2,
2367
2445
  n: (n.n[0] + n.n[1]) / 2,
@@ -2378,7 +2456,7 @@ function Ft({ leaf: e, roots: t, extent: n }) {
2378
2456
  }
2379
2457
  return i;
2380
2458
  }
2381
- function It(e) {
2459
+ function Jt(e) {
2382
2460
  let t = e.split(",");
2383
2461
  if (t.length !== 6 || t.some((e) => e.trim() === "")) return null;
2384
2462
  let [n, r, i, a, o, s] = t.map(Number);
@@ -2398,7 +2476,7 @@ function It(e) {
2398
2476
  dist: s
2399
2477
  };
2400
2478
  }
2401
- function Lt(e) {
2479
+ function Yt(e) {
2402
2480
  let t = (e, t) => +e.toFixed(t);
2403
2481
  return [
2404
2482
  t(e.e, 2),
@@ -2409,19 +2487,19 @@ function Lt(e) {
2409
2487
  t(e.dist, 1)
2410
2488
  ].join(",");
2411
2489
  }
2412
- function Z(e, t, n = Y) {
2490
+ function X(e, t, n = J) {
2413
2491
  let r = {
2414
2492
  e: e.e,
2415
2493
  n: e.n,
2416
2494
  h: e.h,
2417
- yaw: Nt(e.yaw),
2418
- pitch: Mt(e.pitch, n.pitch),
2419
- dist: Mt(e.dist, n.dist)
2495
+ yaw: Gt(e.yaw),
2496
+ pitch: Wt(e.pitch, n.pitch),
2497
+ dist: Wt(e.dist, n.dist)
2420
2498
  };
2421
2499
  if (t) {
2422
- let e = Q(r), i = Math.max(n.floor, t(e.e, e.n) + n.clearance);
2500
+ let e = Z(r), i = Math.max(n.floor, t(e.e, e.n) + n.clearance);
2423
2501
  if (e.h < i) return {
2424
- ...Vt({
2502
+ ...$t({
2425
2503
  ...e,
2426
2504
  h: i
2427
2505
  }, r),
@@ -2430,42 +2508,42 @@ function Z(e, t, n = Y) {
2430
2508
  }
2431
2509
  return r;
2432
2510
  }
2433
- function Rt(e, t) {
2434
- let n = Math.cos(t * X);
2511
+ function Xt(e, t) {
2512
+ let n = Math.cos(t * Y);
2435
2513
  return {
2436
- e: Math.sin(e * X) * n,
2437
- n: Math.cos(e * X) * n,
2438
- h: Math.sin(t * X)
2514
+ e: Math.sin(e * Y) * n,
2515
+ n: Math.cos(e * Y) * n,
2516
+ h: Math.sin(t * Y)
2439
2517
  };
2440
2518
  }
2441
- function Q(e) {
2442
- let t = Rt(e.yaw, e.pitch);
2519
+ function Z(e) {
2520
+ let t = Xt(e.yaw, e.pitch);
2443
2521
  return {
2444
2522
  e: e.e - t.e * e.dist,
2445
2523
  n: e.n - t.n * e.dist,
2446
2524
  h: e.h - t.h * e.dist
2447
2525
  };
2448
2526
  }
2449
- function zt(e) {
2527
+ function Zt(e) {
2450
2528
  return {
2451
- ...Q(e),
2529
+ ...Z(e),
2452
2530
  yaw: e.yaw,
2453
2531
  pitch: e.pitch,
2454
2532
  fov: 45
2455
2533
  };
2456
2534
  }
2457
- function Bt(e, t, n) {
2458
- let r = Q(e), [i, a, o] = [
2535
+ function Qt(e, t, n) {
2536
+ let r = Z(e), [i, a, o] = [
2459
2537
  n.e - r.e,
2460
2538
  n.n - r.n,
2461
2539
  n.h - r.h
2462
- ], [s, c] = [Math.sin(e.yaw * X), Math.cos(e.yaw * X)], [l, u] = [Math.sin(e.pitch * X), Math.cos(e.pitch * X)], d = i * s + a * c, f = 2 * Math.tan(t * X / 2) * (d * u + o * l);
2540
+ ], [s, c] = [Math.sin(e.yaw * Y), Math.cos(e.yaw * Y)], [l, u] = [Math.sin(e.pitch * Y), Math.cos(e.pitch * Y)], d = i * s + a * c, f = 2 * Math.tan(t * Y / 2) * (d * u + o * l);
2463
2541
  return {
2464
2542
  x: (i * c - a * s) / f,
2465
2543
  y: (d * l - o * u) / f
2466
2544
  };
2467
2545
  }
2468
- function Vt(e, t) {
2546
+ function $t(e, t) {
2469
2547
  let [n, r, i] = [
2470
2548
  t.e - e.e,
2471
2549
  t.n - e.n,
@@ -2475,17 +2553,17 @@ function Vt(e, t) {
2475
2553
  e: t.e,
2476
2554
  n: t.n,
2477
2555
  h: t.h,
2478
- yaw: Nt(Math.atan2(n, r) / X),
2479
- pitch: Math.asin(i / a) / X,
2556
+ yaw: Gt(Math.atan2(n, r) / Y),
2557
+ pitch: Math.asin(i / a) / Y,
2480
2558
  dist: a
2481
2559
  };
2482
2560
  }
2483
- function Ht(e, t, n, r = Y) {
2484
- let i = Q(e), a = Z({
2561
+ function en(e, t, n, r = J) {
2562
+ let i = Z(e), a = X({
2485
2563
  ...e,
2486
2564
  yaw: t,
2487
2565
  pitch: n
2488
- }, void 0, r), o = Rt(a.yaw, a.pitch);
2566
+ }, void 0, r), o = Xt(a.yaw, a.pitch);
2489
2567
  return {
2490
2568
  ...a,
2491
2569
  e: i.e + o.e * a.dist,
@@ -2493,11 +2571,11 @@ function Ht(e, t, n, r = Y) {
2493
2571
  h: i.h + o.h * a.dist
2494
2572
  };
2495
2573
  }
2496
- function Ut(e) {
2497
- return e.dist * Math.max(Math.sin(-e.pitch * X), jt);
2574
+ function tn(e) {
2575
+ return e.dist * Math.max(Math.sin(-e.pitch * Y), Ut);
2498
2576
  }
2499
- function Wt(e, t, n, r) {
2500
- let [i, a] = [Math.sin(e.yaw * X), Math.cos(e.yaw * X)];
2577
+ function Q(e, t, n, r) {
2578
+ let [i, a] = [Math.sin(e.yaw * Y), Math.cos(e.yaw * Y)];
2501
2579
  return {
2502
2580
  ...e,
2503
2581
  e: e.e + t * a + n * i,
@@ -2505,30 +2583,30 @@ function Wt(e, t, n, r) {
2505
2583
  h: e.h + r
2506
2584
  };
2507
2585
  }
2508
- function Gt(e, t, n) {
2509
- return Wt(e, t, n * e.dist / Ut(e), 0);
2586
+ function nn(e, t, n) {
2587
+ return Q(e, t, n * e.dist / tn(e), 0);
2510
2588
  }
2511
- function Kt(e, t, n) {
2589
+ function rn(e, t, n) {
2512
2590
  return {
2513
2591
  e: e.e + (t.e - e.e) * n,
2514
2592
  n: e.n + (t.n - e.n) * n,
2515
2593
  h: e.h + (t.h - e.h) * n,
2516
- yaw: Nt(e.yaw + Pt(e.yaw, t.yaw) * n),
2594
+ yaw: Gt(e.yaw + Kt(e.yaw, t.yaw) * n),
2517
2595
  pitch: e.pitch + (t.pitch - e.pitch) * n,
2518
2596
  dist: e.dist * (t.dist / e.dist) ** n
2519
2597
  };
2520
2598
  }
2521
- function qt(e, t) {
2599
+ function an(e, t) {
2522
2600
  let n = e.dist * 1e-5;
2523
- return Math.abs(e.e - t.e) < n && Math.abs(e.n - t.n) < n && Math.abs(e.h - t.h) < n && Math.abs(e.dist - t.dist) < n && Math.abs(Pt(e.yaw, t.yaw)) < .001 && Math.abs(e.pitch - t.pitch) < .001;
2601
+ return Math.abs(e.e - t.e) < n && Math.abs(e.n - t.n) < n && Math.abs(e.h - t.h) < n && Math.abs(e.dist - t.dist) < n && Math.abs(Kt(e.yaw, t.yaw)) < .001 && Math.abs(e.pitch - t.pitch) < .001;
2524
2602
  }
2525
2603
  //#endregion
2526
2604
  //#region src/controls/controls.ts
2527
- var Jt = .2, Yt = .002, Xt = .005, Zt = .5, Qt = .05;
2528
- function $t(e, t) {
2605
+ var on = .2, sn = .002, cn = .005, ln = .5, un = .05;
2606
+ function dn(e, t) {
2529
2607
  return 2 * e * Math.tan(45 * Math.PI / 360) / Math.max(1, t);
2530
2608
  }
2531
- function en(e) {
2609
+ function fn(e) {
2532
2610
  let t = (t) => +!!e.has(t);
2533
2611
  return [
2534
2612
  t("KeyD") - t("KeyA") + t("ArrowRight") - t("ArrowLeft"),
@@ -2536,10 +2614,10 @@ function en(e) {
2536
2614
  t("KeyE") - t("KeyQ")
2537
2615
  ];
2538
2616
  }
2539
- function tn(e, t, n, r, i = Y) {
2540
- return qt(e, t) ? t : Z(Kt(e, t, 1 - Math.exp(-n / Qt)), r, i);
2617
+ function pn(e, t, n, r, i = J) {
2618
+ return an(e, t) ? t : X(rn(e, t, 1 - Math.exp(-n / un)), r, i);
2541
2619
  }
2542
- var nn = class {
2620
+ var mn = class {
2543
2621
  #e;
2544
2622
  #t;
2545
2623
  #n;
@@ -2556,7 +2634,7 @@ var nn = class {
2556
2634
  #p = /* @__PURE__ */ new Set();
2557
2635
  #m;
2558
2636
  #h = null;
2559
- constructor(e, t, n, r, i = Y) {
2637
+ constructor(e, t, n, r, i = J) {
2560
2638
  this.#e = e, this.#t = t, this.#n = n, this.#r = i, this.#d = this.#E(r), this.#f = this.#d, this.#m = t.onFrame(this.#T);
2561
2639
  let a = this.#i.signal;
2562
2640
  e.addEventListener("pointerdown", this.#g, { signal: a }), e.addEventListener("pointermove", this.#_, { signal: a });
@@ -2577,7 +2655,7 @@ var nn = class {
2577
2655
  return this.#p.add(e), () => this.#p.delete(e);
2578
2656
  }
2579
2657
  moveTo(e, t) {
2580
- let n = this.#d, r = Math.min(n.dist, At.dist);
2658
+ let n = this.#d, r = Math.min(n.dist, Ht.dist);
2581
2659
  this.#d = this.#E({
2582
2660
  ...n,
2583
2661
  e,
@@ -2604,7 +2682,7 @@ var nn = class {
2604
2682
  e.buttons & 6 || t && e.shiftKey ? $(this.#c, r, i) : t && $(this.#s, r, i);
2605
2683
  } else if (n) {
2606
2684
  let e = this.#C();
2607
- $(this.#c, e.x - n.x, e.y - n.y), this.#l += (n.d - e.d) * Xt;
2685
+ $(this.#c, e.x - n.x, e.y - n.y), this.#l += (n.d - e.d) * cn;
2608
2686
  } else $(this.#s, r, i);
2609
2687
  this.#w();
2610
2688
  };
@@ -2612,7 +2690,7 @@ var nn = class {
2612
2690
  this.#a.delete(e.pointerId);
2613
2691
  };
2614
2692
  #y = (e) => {
2615
- e.preventDefault(), this.#l += e.deltaY * Yt, this.#w();
2693
+ e.preventDefault(), this.#l += e.deltaY * sn, this.#w();
2616
2694
  };
2617
2695
  #b = (e) => {
2618
2696
  e.code === "KeyF" && !e.repeat && (this.#u = !this.#u), this.#o.add(e.code), this.#u && this.#w();
@@ -2624,7 +2702,7 @@ var nn = class {
2624
2702
  let t = await this.#t.pick(e.offsetX, e.offsetY);
2625
2703
  if (!t || this.#i.signal.aborted) return;
2626
2704
  let [n, r, i] = t;
2627
- this.#d = Z(Vt(Q(this.#f), {
2705
+ this.#d = X($t(Z(this.#f), {
2628
2706
  e: n,
2629
2707
  n: r,
2630
2708
  h: i
@@ -2647,43 +2725,43 @@ var nn = class {
2647
2725
  this.#h = e;
2648
2726
  let n = this.#d, [r, i] = this.#c;
2649
2727
  if (r || i) {
2650
- let e = $t(n.dist, this.#e.clientHeight);
2651
- n = Gt(n, -r * e, i * e);
2728
+ let e = dn(n.dist, this.#e.clientHeight);
2729
+ n = nn(n, -r * e, i * e);
2652
2730
  }
2653
2731
  let [a, o] = this.#s;
2654
2732
  if (a || o) {
2655
- let [e, t] = [n.yaw + a * Jt, n.pitch - o * Jt];
2656
- n = this.#u ? Ht(n, e, t, this.#r) : Z({
2733
+ let [e, t] = [n.yaw + a * on, n.pitch - o * on];
2734
+ n = this.#u ? en(n, e, t, this.#r) : X({
2657
2735
  ...n,
2658
2736
  yaw: e,
2659
2737
  pitch: t
2660
2738
  }, void 0, this.#r);
2661
2739
  }
2662
- this.#l && (n = Z({
2740
+ this.#l && (n = X({
2663
2741
  ...n,
2664
2742
  dist: n.dist * Math.exp(this.#l)
2665
2743
  }, void 0, this.#r)), this.#c = [0, 0], this.#s = [0, 0], this.#l = 0;
2666
- let [s, c, l] = en(this.#o), u = this.#u && (s !== 0 || l !== 0 || c !== 0);
2744
+ let [s, c, l] = fn(this.#o), u = this.#u && (s !== 0 || l !== 0 || c !== 0);
2667
2745
  if (u) {
2668
- let e = Zt * Ut(n) * t;
2669
- n = Wt(n, s * e, c * e, l * e);
2746
+ let e = ln * tn(n) * t;
2747
+ n = Q(n, s * e, c * e, l * e);
2670
2748
  }
2671
2749
  let d = this.#f;
2672
2750
  if (d !== n) {
2673
- this.#d = n = this.#E(n), this.#f = tn(d, n, t, this.#n, this.#r), this.#D();
2751
+ this.#d = n = this.#E(n), this.#f = pn(d, n, t, this.#n, this.#r), this.#D();
2674
2752
  for (let e of this.#p) e();
2675
2753
  }
2676
2754
  u || this.#f !== this.#d ? this.#w() : this.#h = null;
2677
2755
  };
2678
2756
  #E(e) {
2679
- return Z(e, this.#n, this.#r);
2757
+ return X(e, this.#n, this.#r);
2680
2758
  }
2681
2759
  #D() {
2682
- this.#t.setCamera(zt(this.#f));
2760
+ this.#t.setCamera(Zt(this.#f));
2683
2761
  }
2684
2762
  };
2685
2763
  function $(e, t, n) {
2686
2764
  e[0] += t, e[1] += n;
2687
2765
  }
2688
2766
  //#endregion
2689
- export { ae as S, qt as _, Q as a, ot as b, Wt as c, Ht as d, Vt as f, Bt as g, It as h, Kt as i, Lt as l, Gt as m, kt as n, Z as o, Ft as p, Y as r, At as s, nn as t, Ut as u, zt as v, Be as x, G as y };
2767
+ export { ae as S, an as _, Z as a, gt as b, Q as c, en as d, $t as f, Qt as g, Jt as h, rn as i, Yt as l, nn as m, Vt as n, X as o, qt as p, J as r, Ht as s, mn as t, tn as u, Zt as v, We as x, ht as y };