visualproof 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +16 -7
  2. package/dist/assets/tips/tour-agent.webp +0 -0
  3. package/dist/assets/tips/tour-arrange.webp +0 -0
  4. package/dist/assets/tips/tour-artwork.webp +0 -0
  5. package/dist/assets/tips/tour-checks.webp +0 -0
  6. package/dist/assets/tips/tour-keys.webp +0 -0
  7. package/dist/assets/tips/tour-library.webp +0 -0
  8. package/dist/assets/tips/tour-project.webp +0 -0
  9. package/dist/assets/tips/tour-review.webp +0 -0
  10. package/dist/assets/tips/tour-share.webp +0 -0
  11. package/dist/assets/tips/tour-source.webp +0 -0
  12. package/dist/assets/tips/tour-target.webp +0 -0
  13. package/dist/assets/tips/tour-view.webp +0 -0
  14. package/dist/assets/tips/tour.json +117 -112
  15. package/dist/assets/tips/tour.webp +0 -0
  16. package/dist/server/access.js +3 -1
  17. package/dist/server/accesspage.js +130 -0
  18. package/dist/server/agent.js +6 -1
  19. package/dist/server/buildwatch.js +133 -0
  20. package/dist/server/deploy.js +844 -0
  21. package/dist/server/doctor.js +15 -3
  22. package/dist/server/film.js +1 -1
  23. package/dist/server/historyapi.js +100 -50
  24. package/dist/server/historythread.js +206 -0
  25. package/dist/server/hover.js +8 -0
  26. package/dist/server/live.js +187 -36
  27. package/dist/server/main.js +256 -30
  28. package/dist/server/review.js +27 -7
  29. package/dist/server/status.js +119 -46
  30. package/dist/server/usage.js +51 -0
  31. package/dist/shared/board.js +2 -1
  32. package/dist/shared/deploy.js +35 -0
  33. package/dist/shared/layout.js +297 -70
  34. package/dist/shared/libm.js +366 -0
  35. package/dist/shared/palette.js +1 -1
  36. package/dist/shared/separate.js +556 -32
  37. package/dist/web/bundle/app.js +1972 -826
  38. package/dist/web/bundle/edges.js +2 -1
  39. package/dist/web/bundle/layout.js +1026 -80
  40. package/dist/web/src/canvas.js +47 -34
  41. package/dist/web/src/overview.js +4 -1
  42. package/dist/web/src/scene.js +13 -12
  43. package/dist/web/src/status.js +28 -0
  44. package/dist/web/src/theme.js +108 -0
  45. package/dist/web/style.css +102 -35
  46. package/docs/agent-api/history.md +3 -0
  47. package/docs/agent-api/requests.md +4 -3
  48. package/package.json +1 -1
@@ -187,8 +187,529 @@ function siblings_default(circles) {
187
187
  return circles;
188
188
  }
189
189
 
190
+ // src/shared/libm.ts
191
+ var f64 = new Float64Array(1);
192
+ var u32 = new Uint32Array(f64.buffer);
193
+ var HI = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1 ? 1 : 0;
194
+ var LO = 1 - HI;
195
+ function hiWord(x) {
196
+ f64[0] = x;
197
+ return u32[HI] | 0;
198
+ }
199
+ function loWord(x) {
200
+ f64[0] = x;
201
+ return u32[LO] >>> 0;
202
+ }
203
+ function withHi(x, hi) {
204
+ f64[0] = x;
205
+ u32[HI] = hi >>> 0;
206
+ return f64[0];
207
+ }
208
+ function withLo(x, lo) {
209
+ f64[0] = x;
210
+ u32[LO] = lo >>> 0;
211
+ return f64[0];
212
+ }
213
+ function fromWords(hi, lo) {
214
+ u32[HI] = hi >>> 0;
215
+ u32[LO] = lo >>> 0;
216
+ return f64[0];
217
+ }
218
+ function hypot(x, y) {
219
+ const ax = Math.abs(x), ay = Math.abs(y);
220
+ if (ax === Infinity || ay === Infinity) return Infinity;
221
+ if (ax !== ax || ay !== ay) return NaN;
222
+ const max = ax > ay ? ax : ay;
223
+ if (max === 0) return 0;
224
+ const a2 = ax / max, b = ay / max;
225
+ return Math.sqrt(a2 * a2 + b * b) * max;
226
+ }
227
+ var PI = 3.141592653589793;
228
+ var PIO2_HI = 1.5707963267948966;
229
+ var PIO2_LO = 6123233995736766e-32;
230
+ var pS0 = 0.16666666666666666;
231
+ var pS1 = -0.3255658186224009;
232
+ var pS2 = 0.20121253213486293;
233
+ var pS3 = -0.04005553450067941;
234
+ var pS4 = 7915349942898145e-19;
235
+ var pS5 = 3479331075960212e-20;
236
+ var qS1 = -2.403394911734414;
237
+ var qS2 = 2.0209457602335057;
238
+ var qS3 = -0.6882839716054533;
239
+ var qS4 = 0.07703815055590194;
240
+ function acos(x) {
241
+ const hx = hiWord(x);
242
+ const ix = hx & 2147483647;
243
+ if (ix >= 1072693248) {
244
+ if ((ix - 1072693248 | loWord(x)) === 0) return hx > 0 ? 0 : PI + 2 * PIO2_LO;
245
+ return NaN;
246
+ }
247
+ if (ix < 1071644672) {
248
+ if (ix <= 1012924416) return PIO2_HI + PIO2_LO;
249
+ const z = x * x;
250
+ const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
251
+ const q = 1 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
252
+ const r = p / q;
253
+ return PIO2_HI - (x - (PIO2_LO - x * r));
254
+ } else if (hx < 0) {
255
+ const z = (1 + x) * 0.5;
256
+ const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
257
+ const q = 1 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
258
+ const s = Math.sqrt(z);
259
+ const r = p / q;
260
+ const w = r * s - PIO2_LO;
261
+ return PI - 2 * (s + w);
262
+ } else {
263
+ const z = (1 - x) * 0.5;
264
+ const s = Math.sqrt(z);
265
+ const df = withLo(s, 0);
266
+ const c2 = (z - df * df) / (s + df);
267
+ const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
268
+ const q = 1 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
269
+ const r = p / q;
270
+ const w = r * s + c2;
271
+ return 2 * (df + w);
272
+ }
273
+ }
274
+ var LN2_HI = 0.6931471803691238;
275
+ var LN2_LO = 19082149292705877e-26;
276
+ var Lp1 = 0.6666666666666735;
277
+ var Lp2 = 0.3999999999940942;
278
+ var Lp3 = 0.2857142874366239;
279
+ var Lp4 = 0.22222198432149784;
280
+ var Lp5 = 0.1818357216161805;
281
+ var Lp6 = 0.15313837699209373;
282
+ var Lp7 = 0.14798198605116586;
283
+ function log1p(x) {
284
+ const hx = hiWord(x);
285
+ const ax = hx & 2147483647;
286
+ let k = 1;
287
+ let f = 0, c2 = 0, hu = 0;
288
+ if (hx < 1071284858) {
289
+ if (ax >= 1072693248) return x === -1 ? -Infinity : NaN;
290
+ if (ax < 1042284544) {
291
+ if (ax < 1016070144) return x;
292
+ return x - x * x * 0.5;
293
+ }
294
+ if (hx > 0 || hx <= (3218259652 | 0)) {
295
+ k = 0;
296
+ f = x;
297
+ hu = 1;
298
+ }
299
+ }
300
+ if (hx >= 2146435072) return x + x;
301
+ if (k !== 0) {
302
+ let u;
303
+ if (hx < 1128267776) {
304
+ u = 1 + x;
305
+ hu = hiWord(u);
306
+ k = (hu >> 20) - 1023;
307
+ c2 = k > 0 ? 1 - (u - x) : x - (u - 1);
308
+ c2 /= u;
309
+ } else {
310
+ u = x;
311
+ hu = hiWord(u);
312
+ k = (hu >> 20) - 1023;
313
+ c2 = 0;
314
+ }
315
+ hu &= 1048575;
316
+ if (hu < 434334) {
317
+ u = withHi(u, hu | 1072693248);
318
+ } else {
319
+ k += 1;
320
+ u = withHi(u, hu | 1071644672);
321
+ hu = 1048576 - hu >> 2;
322
+ }
323
+ f = u - 1;
324
+ }
325
+ const hfsq = 0.5 * f * f;
326
+ if (hu === 0) {
327
+ if (f === 0) {
328
+ if (k === 0) return 0;
329
+ c2 += k * LN2_LO;
330
+ return k * LN2_HI + c2;
331
+ }
332
+ const R2 = hfsq * (1 - 0.6666666666666666 * f);
333
+ if (k === 0) return f - R2;
334
+ return k * LN2_HI - (R2 - (k * LN2_LO + c2) - f);
335
+ }
336
+ const s = f / (2 + f);
337
+ const z = s * s;
338
+ const R = z * (Lp1 + z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 + z * (Lp6 + z * Lp7))))));
339
+ if (k === 0) return f - (hfsq - s * (hfsq + R));
340
+ return k * LN2_HI - (hfsq - (s * (hfsq + R) + (k * LN2_LO + c2)) - f);
341
+ }
342
+ var S1 = -0.16666666666666632;
343
+ var S2 = 0.00833333333332249;
344
+ var S3 = -1984126982985795e-19;
345
+ var S4 = 27557313707070068e-22;
346
+ var S5 = -25050760253406863e-24;
347
+ var S6 = 158969099521155e-24;
348
+ function kernelSin(x, y, iy) {
349
+ const ix = hiWord(x) & 2147483647;
350
+ if (ix < 1044381696 && Math.trunc(x) === 0) return x;
351
+ const z = x * x;
352
+ const v = z * x;
353
+ const r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
354
+ if (iy === 0) return x + v * (S1 + z * r);
355
+ return x - (z * (0.5 * y - v * r) - y - v * S1);
356
+ }
357
+ var C1 = 0.0416666666666666;
358
+ var C2 = -0.001388888888887411;
359
+ var C3 = 2480158728947673e-20;
360
+ var C4 = -27557314351390663e-23;
361
+ var C5 = 2087572321298175e-24;
362
+ var C6 = -11359647557788195e-27;
363
+ function kernelCos(x, y) {
364
+ const ix = hiWord(x) & 2147483647;
365
+ if (ix < 1044381696 && Math.trunc(x) === 0) return 1;
366
+ const z = x * x;
367
+ const r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
368
+ if (ix < 1070805811) return 1 - (0.5 * z - (z * r - x * y));
369
+ const qx = ix > 1072234496 ? 0.28125 : fromWords(ix - 2097152, 0);
370
+ const iz = 0.5 * z - qx;
371
+ const a2 = 1 - qx;
372
+ return a2 - (iz - (z * r - x * y));
373
+ }
374
+ var INVPIO2 = 0.6366197723675814;
375
+ var PIO2_1 = 1.5707963267341256;
376
+ var PIO2_1T = 6077100506506192e-26;
377
+ var PIO2_2 = 6077100506303966e-26;
378
+ var PIO2_2T = 20222662487959506e-37;
379
+ var PIO2_3 = 20222662487111665e-37;
380
+ var PIO2_3T = 84784276603689e-45;
381
+ var NPIO2_HW = [
382
+ 1073291771,
383
+ 1074340347,
384
+ 1074977148,
385
+ 1075388923,
386
+ 1075800698,
387
+ 1076025724,
388
+ 1076231611,
389
+ 1076437499,
390
+ 1076643386,
391
+ 1076849274,
392
+ 1076971356,
393
+ 1077074300,
394
+ 1077177244,
395
+ 1077280187,
396
+ 1077383131,
397
+ 1077486075,
398
+ 1077589019,
399
+ 1077691962,
400
+ 1077794906,
401
+ 1077897850,
402
+ 1077968460,
403
+ 1078019932,
404
+ 1078071404,
405
+ 1078122876,
406
+ 1078174348,
407
+ 1078225820,
408
+ 1078277292,
409
+ 1078328763,
410
+ 1078380235,
411
+ 1078431707,
412
+ 1078483179,
413
+ 1078534651
414
+ ];
415
+ var red = new Float64Array(2);
416
+ function remPio2(x) {
417
+ const hx = hiWord(x);
418
+ const ix = hx & 2147483647;
419
+ if (ix <= 1072243195) {
420
+ red[0] = x;
421
+ red[1] = 0;
422
+ return 0;
423
+ }
424
+ if (ix < 1073928572) {
425
+ if (hx > 0) {
426
+ let z2 = x - PIO2_1;
427
+ if (ix !== 1073291771) {
428
+ red[0] = z2 - PIO2_1T;
429
+ red[1] = z2 - red[0] - PIO2_1T;
430
+ } else {
431
+ z2 -= PIO2_2;
432
+ red[0] = z2 - PIO2_2T;
433
+ red[1] = z2 - red[0] - PIO2_2T;
434
+ }
435
+ return 1;
436
+ }
437
+ let z = x + PIO2_1;
438
+ if (ix !== 1073291771) {
439
+ red[0] = z + PIO2_1T;
440
+ red[1] = z - red[0] + PIO2_1T;
441
+ } else {
442
+ z += PIO2_2;
443
+ red[0] = z + PIO2_2T;
444
+ red[1] = z - red[0] + PIO2_2T;
445
+ }
446
+ return -1;
447
+ }
448
+ if (ix > 1094263291) throw new RangeError("libm: argument out of range");
449
+ let t = Math.abs(x);
450
+ const n = Math.trunc(t * INVPIO2 + 0.5);
451
+ const fn = n;
452
+ let r = t - fn * PIO2_1;
453
+ let w = fn * PIO2_1T;
454
+ if (n < 32 && ix !== NPIO2_HW[n - 1]) {
455
+ red[0] = r - w;
456
+ } else {
457
+ const j = ix >> 20;
458
+ red[0] = r - w;
459
+ let i = j - (hiWord(red[0]) >> 20 & 2047);
460
+ if (i > 16) {
461
+ t = r;
462
+ w = fn * PIO2_2;
463
+ r = t - w;
464
+ w = fn * PIO2_2T - (t - r - w);
465
+ red[0] = r - w;
466
+ i = j - (hiWord(red[0]) >> 20 & 2047);
467
+ if (i > 49) {
468
+ t = r;
469
+ w = fn * PIO2_3;
470
+ r = t - w;
471
+ w = fn * PIO2_3T - (t - r - w);
472
+ red[0] = r - w;
473
+ }
474
+ }
475
+ }
476
+ red[1] = r - red[0] - w;
477
+ if (hx < 0) {
478
+ red[0] = -red[0];
479
+ red[1] = -red[1];
480
+ return -n;
481
+ }
482
+ return n;
483
+ }
484
+ function sin(x) {
485
+ const ix = hiWord(x) & 2147483647;
486
+ if (ix <= 1072243195) return kernelSin(x, 0, 0);
487
+ if (ix >= 2146435072) return NaN;
488
+ const n = remPio2(x);
489
+ switch (n & 3) {
490
+ case 0:
491
+ return kernelSin(red[0], red[1], 1);
492
+ case 1:
493
+ return kernelCos(red[0], red[1]);
494
+ case 2:
495
+ return -kernelSin(red[0], red[1], 1);
496
+ default:
497
+ return -kernelCos(red[0], red[1]);
498
+ }
499
+ }
500
+ function cos(x) {
501
+ const ix = hiWord(x) & 2147483647;
502
+ if (ix <= 1072243195) return kernelCos(x, 0);
503
+ if (ix >= 2146435072) return NaN;
504
+ const n = remPio2(x);
505
+ switch (n & 3) {
506
+ case 0:
507
+ return kernelCos(red[0], red[1]);
508
+ case 1:
509
+ return -kernelSin(red[0], red[1], 1);
510
+ case 2:
511
+ return -kernelCos(red[0], red[1]);
512
+ default:
513
+ return kernelSin(red[0], red[1], 1);
514
+ }
515
+ }
516
+
190
517
  // src/shared/separate.ts
191
518
  var SLACK = 0.5;
519
+ var INDEXED = 200;
520
+ var WIDE = 4096;
521
+ var CELLS = 1 << 20;
522
+ var BoxGrid = class {
523
+ size = 1;
524
+ ox = 0;
525
+ oy = 0;
526
+ nx = 1;
527
+ ny = 1;
528
+ /** per cell, the first body filed by its centre and the first box entry */
529
+ headP = new Int32Array(1);
530
+ headE = new Int32Array(1);
531
+ /** per body filed by its centre: its cell, and its neighbours in the cell's list */
532
+ at = new Int32Array(0);
533
+ nextP = new Int32Array(0);
534
+ prevP = new Int32Array(0);
535
+ nextE = new Int32Array(0);
536
+ bodyE = new Int32Array(0);
537
+ used = 0;
538
+ stamp = new Int32Array(0);
539
+ token = 0;
540
+ /** boxes too wide to file by their cells (or not numbers), which every look finds */
541
+ wide = [];
542
+ isWide = new Uint8Array(0);
543
+ /** what the last look found */
544
+ found = new Int32Array(16);
545
+ /** where each body was filed, and how far its box reaches from there (0: by its centre) */
546
+ fx = new Float64Array(0);
547
+ fy = new Float64Array(0);
548
+ fe = new Float64Array(0);
549
+ /** Empty, for bodies numbered below `n`, cells `size` wide over [x0, x1] × [y0, y1]. */
550
+ reset(size, n, x0, y0, x1, y1) {
551
+ let s = size > 0 && Number.isFinite(size) ? size : 1;
552
+ if (!(x0 <= x1 && y0 <= y1 && Number.isFinite(x0 + x1 + y0 + y1))) x0 = y0 = x1 = y1 = 0;
553
+ while ((Math.floor(x1 / s) - Math.floor(x0 / s) + 1) * (Math.floor(y1 / s) - Math.floor(y0 / s) + 1) > CELLS) s *= 2;
554
+ this.size = s;
555
+ this.ox = Math.floor(x0 / s);
556
+ this.oy = Math.floor(y0 / s);
557
+ this.nx = Math.floor(x1 / s) - this.ox + 1;
558
+ this.ny = Math.floor(y1 / s) - this.oy + 1;
559
+ const cells = this.nx * this.ny;
560
+ if (this.headP.length < cells) {
561
+ this.headP = new Int32Array(cells);
562
+ this.headE = new Int32Array(cells);
563
+ }
564
+ this.headP.fill(-1, 0, cells);
565
+ this.headE.fill(-1, 0, cells);
566
+ this.used = 0;
567
+ this.wide.length = 0;
568
+ if (this.stamp.length < n) {
569
+ this.stamp = new Int32Array(n);
570
+ this.isWide = new Uint8Array(n);
571
+ this.at = new Int32Array(n);
572
+ this.nextP = new Int32Array(n);
573
+ this.prevP = new Int32Array(n);
574
+ this.fx = new Float64Array(n);
575
+ this.fy = new Float64Array(n);
576
+ this.fe = new Float64Array(n);
577
+ this.token = 0;
578
+ } else this.isWide.fill(0, 0, n);
579
+ this.at.fill(-1, 0, n);
580
+ }
581
+ /** the column (row) of a place, at the region's edge if outside it; NaN where it is not a number */
582
+ col(x) {
583
+ const c2 = Math.floor(x / this.size) - this.ox;
584
+ return c2 < 0 ? 0 : c2 >= this.nx ? this.nx - 1 : c2;
585
+ }
586
+ row(y) {
587
+ const c2 = Math.floor(y / this.size) - this.oy;
588
+ return c2 < 0 ? 0 : c2 >= this.ny ? this.ny - 1 : c2;
589
+ }
590
+ /** the grid's columns and rows, and the width of its cells */
591
+ get columns() {
592
+ return this.nx;
593
+ }
594
+ get rows() {
595
+ return this.ny;
596
+ }
597
+ get width() {
598
+ return this.size;
599
+ }
600
+ /** How many cells the box [x0, x1] × [y0, y1] covers. */
601
+ cells(x0, y0, x1, y1) {
602
+ return (this.col(x1) - this.col(x0) + 1) * (this.row(y1) - this.row(y0) + 1);
603
+ }
604
+ /** File body `k` at (x, y): by its centre where `e` is 0, else by the box reaching `e` from there
605
+ * every way. Filed again the way it was filed first. */
606
+ add(k, x, y, e) {
607
+ this.fx[k] = x;
608
+ this.fy[k] = y;
609
+ this.fe[k] = e;
610
+ if (e === 0) {
611
+ const i = this.col(x), j = this.row(y);
612
+ if (!(i >= 0 && j >= 0)) {
613
+ if (!this.isWide[k]) this.wide.push(k);
614
+ this.isWide[k] = 1;
615
+ return;
616
+ }
617
+ const c2 = j * this.nx + i;
618
+ const was = this.at[k];
619
+ if (was === c2) return;
620
+ if (was >= 0) {
621
+ const p = this.prevP[k], q = this.nextP[k];
622
+ if (p >= 0) this.nextP[p] = q;
623
+ else this.headP[was] = q;
624
+ if (q >= 0) this.prevP[q] = p;
625
+ }
626
+ this.at[k] = c2;
627
+ this.prevP[k] = -1;
628
+ this.nextP[k] = this.headP[c2];
629
+ if (this.headP[c2] >= 0) this.prevP[this.headP[c2]] = k;
630
+ this.headP[c2] = k;
631
+ return;
632
+ }
633
+ const i0 = this.col(x - e), i1 = this.col(x + e);
634
+ const j0 = this.row(y - e), j1 = this.row(y + e);
635
+ const count = (i1 - i0 + 1) * (j1 - j0 + 1);
636
+ if (!(count <= WIDE)) {
637
+ if (!this.isWide[k]) this.wide.push(k);
638
+ this.isWide[k] = 1;
639
+ return;
640
+ }
641
+ const need = this.used + count;
642
+ if (need > this.nextE.length) {
643
+ const cap = Math.max(need, 2 * this.nextE.length, 64);
644
+ const nn = new Int32Array(cap), nb = new Int32Array(cap);
645
+ nn.set(this.nextE.subarray(0, this.used));
646
+ nb.set(this.bodyE.subarray(0, this.used));
647
+ this.nextE = nn;
648
+ this.bodyE = nb;
649
+ }
650
+ for (let j = j0; j <= j1; j++) {
651
+ for (let i = i0; i <= i1; i++) {
652
+ const h = j * this.nx + i;
653
+ const en = this.used++;
654
+ this.bodyE[en] = k;
655
+ this.nextE[en] = this.headE[h];
656
+ this.headE[h] = en;
657
+ }
658
+ }
659
+ }
660
+ /** The bodies filed in the cells of [x0, x1] × [y0, y1], each once, into `found`; how many. */
661
+ find(x0, y0, x1, y1) {
662
+ const i0 = this.col(x0), i1 = this.col(x1);
663
+ const j0 = this.row(y0), j1 = this.row(y1);
664
+ const m2 = this.wideOnes(0);
665
+ let n = m2;
666
+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) n = this.cell(i, j, n);
667
+ if (this.token >= 1073741823) {
668
+ this.stamp.fill(0);
669
+ this.token = 0;
670
+ }
671
+ const t = ++this.token;
672
+ const stamp = this.stamp, found = this.found;
673
+ let k = 0;
674
+ for (let f = 0; f < n; f++) {
675
+ const b = found[f];
676
+ if (stamp[b] === t) continue;
677
+ stamp[b] = t;
678
+ found[k++] = b;
679
+ }
680
+ return k;
681
+ }
682
+ /** The bodies filed in the cell at column `i` and row `j`, appended to `found` from `m` on (one
683
+ * filed by a box perhaps more than once); where they end. */
684
+ cell(i, j, m2) {
685
+ const c2 = j * this.nx + i;
686
+ let found = this.found;
687
+ const nextP = this.nextP;
688
+ for (let k = this.headP[c2]; k >= 0; k = nextP[k]) {
689
+ if (m2 === found.length) found = this.grow();
690
+ found[m2++] = k;
691
+ }
692
+ const nextE = this.nextE, bodyE = this.bodyE;
693
+ for (let en = this.headE[c2]; en >= 0; en = nextE[en]) {
694
+ if (m2 === found.length) found = this.grow();
695
+ found[m2++] = bodyE[en];
696
+ }
697
+ return m2;
698
+ }
699
+ /** The bodies too wide to file by their cells, appended to `found` from `m` on. */
700
+ wideOnes(m2) {
701
+ for (const k of this.wide) {
702
+ if (m2 === this.found.length) this.grow();
703
+ this.found[m2++] = k;
704
+ }
705
+ return m2;
706
+ }
707
+ grow() {
708
+ const f = new Int32Array(2 * this.found.length);
709
+ f.set(this.found);
710
+ return this.found = f;
711
+ }
712
+ };
192
713
  var Separator = class {
193
714
  head = new Int32Array(0);
194
715
  next = new Int32Array(0);
@@ -196,6 +717,27 @@ var Separator = class {
196
717
  moved = new Uint8Array(0);
197
718
  movedNext = new Uint8Array(0);
198
719
  movedSmall = new Int32Array(0);
720
+ /** each body's place among the large ones and among the small ones, or -1 */
721
+ rankBig = new Int32Array(0);
722
+ rankSmall = new Int32Array(0);
723
+ /** the large bodies (up to `rPoint` by their centres, the larger by their boxes), the small ones */
724
+ bigs = new BoxGrid();
725
+ smalls = new BoxGrid();
726
+ rPoint = 0;
727
+ /** the largest small body */
728
+ rSmall = 0;
729
+ /** how far a body may move from where it is filed before it is filed again, and how far the large
730
+ * body meeting the others may move from where it looked before it looks again */
731
+ slackFiled = 1;
732
+ slackLook = 1;
733
+ /** the places in their list of what one large body may meet, in order; which bodies its turn has
734
+ * seen; how many the grid found at the last look */
735
+ cand = new Int32Array(64);
736
+ seen = new Int32Array(0);
737
+ turn = 0;
738
+ looked = 0;
739
+ /** whether this round files the bodies in the grids */
740
+ indexed = false;
199
741
  cell;
200
742
  constructor(cell) {
201
743
  this.cell = cell;
@@ -206,6 +748,10 @@ var Separator = class {
206
748
  this.moved = new Uint8Array(n);
207
749
  this.movedNext = new Uint8Array(n);
208
750
  this.movedSmall = new Int32Array(n);
751
+ this.rankBig = new Int32Array(n);
752
+ this.rankSmall = new Int32Array(n);
753
+ this.seen = new Int32Array(n);
754
+ this.turn = 0;
209
755
  }
210
756
  let size = 64;
211
757
  while (size < 2 * nSmall) size <<= 1;
@@ -224,12 +770,65 @@ var Separator = class {
224
770
  this.head[h] = k;
225
771
  }
226
772
  }
227
- /** Push a and b apart if they overlap; marks what moved. */
773
+ /** Each body's place in its list; the grids' slacks, from the large bodies' sizes. */
774
+ rank(B) {
775
+ const { big, small } = B;
776
+ this.rankBig.fill(-1, 0, B.n);
777
+ this.rankSmall.fill(-1, 0, B.n);
778
+ for (let p = 0; p < big.length; p++) this.rankBig[big[p]] = p;
779
+ for (let s = 0; s < small.length; s++) this.rankSmall[small[s]] = s;
780
+ let rs = 0;
781
+ for (let s = 0; s < small.length; s++) rs = Math.max(rs, B.r[small[s]]);
782
+ this.rSmall = rs;
783
+ const r = Float64Array.from(big, (k) => B.r[k]).sort();
784
+ const mid = r.length ? r[r.length >> 1] : this.cell / 2;
785
+ this.rPoint = r.length ? r[Math.floor(0.9 * (r.length - 1))] : 0;
786
+ this.slackFiled = mid / 4;
787
+ this.slackLook = mid / 2;
788
+ }
789
+ /** File every body where it is now, over the region they cover. */
790
+ file(B) {
791
+ const { big, small } = B;
792
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
793
+ for (let k = 0; k < B.n; k++) {
794
+ const x = B.x[k], y = B.y[k];
795
+ if (x < x0) x0 = x;
796
+ if (x > x1) x1 = x;
797
+ if (y < y0) y0 = y;
798
+ if (y > y1) y1 = y;
799
+ }
800
+ const area = (x1 - x0) * (y1 - y0);
801
+ this.bigs.reset(Math.max(this.cell, 0.75 * (this.rPoint + this.lookReach(false)), Math.sqrt(area / (4 * big.length + 64))), B.n, x0, y0, x1, y1);
802
+ for (let p = 0; p < big.length; p++) {
803
+ const k = big[p];
804
+ this.bigs.add(k, B.x[k], B.y[k], B.r[k] <= this.rPoint ? 0 : B.r[k] + this.slackFiled);
805
+ }
806
+ this.smalls.reset(Math.max(this.cell, Math.sqrt(area / (4 * small.length + 64))), B.n, x0, y0, x1, y1);
807
+ for (let s = 0; s < small.length; s++) {
808
+ const k = small[s];
809
+ this.smalls.add(k, B.x[k], B.y[k], 0);
810
+ }
811
+ }
812
+ /** How far beyond its own radius a look of a large body reaches, among the large ones or the
813
+ * small ones: to where what may meet it before it looks again was filed. */
814
+ lookReach(small) {
815
+ return this.slackLook + (small ? this.rSmall : this.rPoint) + this.slackFiled;
816
+ }
817
+ /** A body that moved: filed again once it is further than the slack from where it was filed. */
818
+ track(B, k) {
819
+ if (!this.indexed) return;
820
+ const g = this.rankBig[k] >= 0 ? this.bigs : this.smalls;
821
+ const dx = B.x[k] - g.fx[k], dy = B.y[k] - g.fy[k];
822
+ const s = this.slackFiled * 0.999;
823
+ if (dx * dx + dy * dy > s * s) g.add(k, B.x[k], B.y[k], g.fe[k]);
824
+ }
825
+ /** Push a and b apart if they overlap; marks what moved. 0 when they do not overlap, else 1 if a
826
+ * moved, 2 if b did, 3 if both. */
228
827
  push(B, a2, b) {
229
828
  const dx = B.x[b] - B.x[a2], dy = B.y[b] - B.y[a2];
230
829
  const need = B.r[a2] + B.r[b];
231
830
  const d2 = dx * dx + dy * dy;
232
- if (d2 >= (need - SLACK) * (need - SLACK)) return false;
831
+ if (d2 >= (need - SLACK) * (need - SLACK)) return 0;
233
832
  const pa = B.pinned[a2], pb = B.pinned[b];
234
833
  const fixA = pa && (!pb || B.seq[a2] > B.seq[b]);
235
834
  const fixB = pb && (!pa || B.seq[b] > B.seq[a2]);
@@ -238,23 +837,179 @@ var Separator = class {
238
837
  const ra = B.r[a2] * B.r[a2], rb = B.r[b] * B.r[b];
239
838
  const wa = fixA ? 0 : fixB ? 1 : rb / (ra + rb);
240
839
  const ux = d2 ? dx / d : 1, uy = d2 ? dy / d : 0;
840
+ let m2 = 0;
241
841
  if (wa > 0) {
242
842
  B.x[a2] -= ux * over * wa;
243
843
  B.y[a2] -= uy * over * wa;
244
844
  this.movedNext[a2] = 1;
845
+ m2 |= 1;
245
846
  }
246
847
  if (wa < 1) {
247
848
  B.x[b] += ux * over * (1 - wa);
248
849
  B.y[b] += uy * over * (1 - wa);
249
850
  this.movedNext[b] = 1;
851
+ m2 |= 2;
250
852
  }
251
- return true;
853
+ return m2;
252
854
  }
253
855
  overlap(B, a2, b) {
254
856
  const dx = B.x[b] - B.x[a2], dy = B.y[b] - B.y[a2];
255
857
  const need = B.r[a2] + B.r[b] - SLACK;
256
858
  return dx * dx + dy * dy < need * need;
257
859
  }
860
+ /** How many cells a look of large body `a` from (qx, qy) visits. */
861
+ lookCells(B, a2, qx, qy, small) {
862
+ const r = B.r[a2] + this.lookReach(small);
863
+ return (small ? this.smalls : this.bigs).cells(qx - r, qy - r, qx + r, qy + r);
864
+ }
865
+ /** What large body `a` may meet among the large ones (`small` false) or the small ones (true),
866
+ * looking from (qx, qy) — it stays within the slack of there until it looks again — in its turn
867
+ * `t`: the cells of the look its earlier looks have not visited, and in them what it has not seen
868
+ * yet in this turn, their places in their list after place `after` put in order into the
869
+ * candidates from `from` on, where `to` ended them. Returns where they end now; `this.looked` is
870
+ * how many the grid found. */
871
+ gather(B, a2, qx, qy, small, after, t, from, to) {
872
+ const g = small ? this.smalls : this.bigs;
873
+ const rank = small ? this.rankSmall : this.rankBig;
874
+ const reach = B.r[a2] + this.lookReach(small);
875
+ const i0 = g.col(qx - reach), i1 = g.col(qx + reach);
876
+ const j0 = g.row(qy - reach), j1 = g.row(qy + reach);
877
+ const rects = this.rects;
878
+ const nr = this.nRects;
879
+ let m2 = nr === 0 ? g.wideOnes(0) : 0;
880
+ for (let j = j0; j <= j1; j++) {
881
+ for (let i = i0; i <= i1; i++) {
882
+ let done = false;
883
+ for (let v = 0; v < nr && !done; v++) done = i >= rects[4 * v] && i <= rects[4 * v + 1] && j >= rects[4 * v + 2] && j <= rects[4 * v + 3];
884
+ if (!done) m2 = g.cell(i, j, m2);
885
+ }
886
+ }
887
+ if (nr < rects.length / 4) {
888
+ rects[4 * nr] = i0;
889
+ rects[4 * nr + 1] = i1;
890
+ rects[4 * nr + 2] = j0;
891
+ rects[4 * nr + 3] = j1;
892
+ this.nRects = nr + 1;
893
+ } else this.nRects = 0;
894
+ this.looked = m2;
895
+ if (this.cand.length < to + m2) {
896
+ const c2 = new Int32Array(Math.max(2 * this.cand.length, to + m2));
897
+ c2.set(this.cand.subarray(0, to));
898
+ this.cand = c2;
899
+ }
900
+ const cand = this.cand, found = g.found, seen = this.seen;
901
+ let n = to;
902
+ for (let f = 0; f < m2; f++) {
903
+ const k = found[f];
904
+ if (seen[k] === t) continue;
905
+ seen[k] = t;
906
+ const q = rank[k];
907
+ if (q > after) cand[n++] = q;
908
+ }
909
+ if (from === to) {
910
+ if (n - from > 1) cand.subarray(from, n).sort();
911
+ } else {
912
+ for (let j = to; j < n; j++) {
913
+ const q = cand[j];
914
+ let i = j;
915
+ while (i > from && cand[i - 1] > q) {
916
+ cand[i] = cand[i - 1];
917
+ i--;
918
+ }
919
+ cand[i] = q;
920
+ }
921
+ }
922
+ return n;
923
+ }
924
+ /** the cells each look of the turn visited, as column and row ranges */
925
+ rects = new Int32Array(4 * 16);
926
+ nRects = 0;
927
+ /**
928
+ * Large body big[p] against the later large ones (`small` false) or against the small ones, in
929
+ * their order, as the plain pass takes them, from the grid. Where the grid would visit more cells,
930
+ * or find more bodies, than the plain pass would look at — a crowd, where everything touches
931
+ * everything — the plain pass takes over from where the grid had come to.
932
+ */
933
+ against(B, p, small, nMovedSmall) {
934
+ const list = small ? B.small : B.big;
935
+ const moved = this.moved;
936
+ const a2 = B.big[p];
937
+ let qx = B.x[a2], qy = B.y[a2];
938
+ const after = small ? -1 : p;
939
+ let budget = small ? moved[a2] ? list.length : nMovedSmall : list.length - p - 1;
940
+ if (budget === 0) return false;
941
+ if (!(4 * this.lookCells(B, a2, qx, qy, small) <= budget)) return this.plain(B, p, small, after, nMovedSmall);
942
+ if (this.turn >= 1073741823) {
943
+ this.seen.fill(0);
944
+ this.turn = 0;
945
+ }
946
+ const t = ++this.turn;
947
+ this.nRects = 0;
948
+ let nc = this.gather(B, a2, qx, qy, small, after, t, 0, 0);
949
+ budget -= 2 * this.looked;
950
+ const s2 = this.slackLook * 0.999 * (this.slackLook * 0.999);
951
+ let any = false;
952
+ for (let ci = 0; ci < nc; ) {
953
+ const q = this.cand[ci++];
954
+ const b = list[q];
955
+ if (!(moved[a2] || moved[b])) continue;
956
+ const m2 = this.push(B, a2, b);
957
+ if (!m2) continue;
958
+ any = true;
959
+ if (m2 & 2) this.track(B, b);
960
+ if (m2 & 1) {
961
+ const dx = B.x[a2] - qx, dy = B.y[a2] - qy;
962
+ if (dx * dx + dy * dy > s2) {
963
+ qx = B.x[a2];
964
+ qy = B.y[a2];
965
+ if (!(4 * this.lookCells(B, a2, qx, qy, small) <= budget)) return this.plain(B, p, small, q, nMovedSmall) || any;
966
+ nc = this.gather(B, a2, qx, qy, small, q, t, ci, nc);
967
+ budget -= 2 * this.looked;
968
+ if (budget < 0) return this.plain(B, p, small, q, nMovedSmall) || any;
969
+ }
970
+ }
971
+ }
972
+ return any;
973
+ }
974
+ /** The plain pass of large body big[p] against the list's bodies after place `after`: the later
975
+ * large ones, or the small ones (those that moved, where big[p] did not). */
976
+ plain(B, p, small, after, nMovedSmall) {
977
+ const moved = this.moved;
978
+ const a2 = B.big[p];
979
+ let any = false;
980
+ if (!small) {
981
+ const big = B.big;
982
+ for (let q = after + 1; q < big.length; q++) {
983
+ const b = big[q];
984
+ if (!(moved[a2] || moved[b])) continue;
985
+ const m2 = this.push(B, a2, b);
986
+ if (m2) {
987
+ any = true;
988
+ if (m2 & 2) this.track(B, b);
989
+ }
990
+ }
991
+ } else if (moved[a2]) {
992
+ const list = B.small;
993
+ for (let s = after + 1; s < list.length; s++) {
994
+ const m2 = this.push(B, a2, list[s]);
995
+ if (m2) {
996
+ any = true;
997
+ if (m2 & 2) this.track(B, list[s]);
998
+ }
999
+ }
1000
+ } else {
1001
+ for (let s = 0; s < nMovedSmall; s++) {
1002
+ const b = this.movedSmall[s];
1003
+ if (after >= 0 && this.rankSmall[b] <= after) continue;
1004
+ const m2 = this.push(B, a2, b);
1005
+ if (m2) {
1006
+ any = true;
1007
+ if (m2 & 2) this.track(B, b);
1008
+ }
1009
+ }
1010
+ }
1011
+ return any;
1012
+ }
258
1013
  /**
259
1014
  * Up to `rounds` rounds of separation. After each round a body further from the origin than
260
1015
  * `limit[k]` is brought back to it (null: no limit). Returns whether anything still moved in
@@ -263,6 +1018,8 @@ var Separator = class {
263
1018
  separate(B, rounds, limit) {
264
1019
  const { big, small } = B;
265
1020
  this.ensure(B.n, small.length);
1021
+ const indexed = this.indexed = big.length >= INDEXED;
1022
+ if (indexed) this.rank(B);
266
1023
  const moved = this.moved, next = this.movedNext;
267
1024
  moved.fill(1, 0, B.n);
268
1025
  let any = false;
@@ -270,19 +1027,12 @@ var Separator = class {
270
1027
  any = false;
271
1028
  next.fill(0, 0, B.n);
272
1029
  this.build(B);
1030
+ if (indexed) this.file(B);
273
1031
  let nMovedSmall = 0;
274
1032
  for (let s = 0; s < small.length; s++) if (moved[small[s]]) this.movedSmall[nMovedSmall++] = small[s];
275
1033
  for (let p = 0; p < big.length; p++) {
276
- const a2 = big[p];
277
- for (let q = p + 1; q < big.length; q++) {
278
- const b = big[q];
279
- if ((moved[a2] || moved[b]) && this.push(B, a2, b)) any = true;
280
- }
281
- if (moved[a2]) {
282
- for (let s = 0; s < small.length; s++) if (this.push(B, a2, small[s])) any = true;
283
- } else {
284
- for (let s = 0; s < nMovedSmall; s++) if (this.push(B, a2, this.movedSmall[s])) any = true;
285
- }
1034
+ if (indexed ? this.against(B, p, false, nMovedSmall) : this.plain(B, p, false, p, nMovedSmall)) any = true;
1035
+ if (indexed ? this.against(B, p, true, nMovedSmall) : this.plain(B, p, true, -1, nMovedSmall)) any = true;
286
1036
  }
287
1037
  for (let s = 0; s < nMovedSmall; s++) {
288
1038
  const a2 = this.movedSmall[s];
@@ -299,7 +1049,7 @@ var Separator = class {
299
1049
  }
300
1050
  if (limit) {
301
1051
  for (let k = 0; k < B.n; k++) {
302
- const d = Math.hypot(B.x[k], B.y[k]);
1052
+ const d = hypot(B.x[k], B.y[k]);
303
1053
  if (d > limit[k]) {
304
1054
  B.x[k] *= limit[k] / d;
305
1055
  B.y[k] *= limit[k] / d;
@@ -316,9 +1066,31 @@ var Separator = class {
316
1066
  overlaps(B) {
317
1067
  const { big, small } = B;
318
1068
  this.ensure(B.n, small.length);
1069
+ const indexed = big.length >= INDEXED;
1070
+ if (indexed) {
1071
+ this.rank(B);
1072
+ this.file(B);
1073
+ }
319
1074
  for (let p = 0; p < big.length; p++) {
320
- for (let q = p + 1; q < big.length; q++) if (this.overlap(B, big[p], big[q])) return true;
321
- for (let s = 0; s < small.length; s++) if (this.overlap(B, big[p], small[s])) return true;
1075
+ const a2 = big[p];
1076
+ const x = B.x[a2], y = B.y[a2];
1077
+ for (let pass = 0; pass < 2; pass++) {
1078
+ const toSmall = pass === 1;
1079
+ const list = toSmall ? small : big;
1080
+ const from = toSmall ? 0 : p + 1;
1081
+ if (from >= list.length) continue;
1082
+ if (!indexed || !(this.lookCells(B, a2, x, y, toSmall) <= list.length - from)) {
1083
+ for (let q = from; q < list.length; q++) if (this.overlap(B, a2, list[q])) return true;
1084
+ continue;
1085
+ }
1086
+ const g = toSmall ? this.smalls : this.bigs;
1087
+ const reach = B.r[a2] + this.lookReach(toSmall);
1088
+ const m2 = g.find(x - reach, y - reach, x + reach, y + reach);
1089
+ for (let f = 0; f < m2; f++) {
1090
+ const b = g.found[f];
1091
+ if ((toSmall || this.rankBig[b] > p) && this.overlap(B, a2, b)) return true;
1092
+ }
1093
+ }
322
1094
  }
323
1095
  this.build(B);
324
1096
  for (let s = 0; s < small.length; s++) {
@@ -375,7 +1147,7 @@ function potentials(c2) {
375
1147
  const diag = new Float64Array(m2);
376
1148
  const join = (a2, b, ab, ba) => {
377
1149
  const tot = ab + ba;
378
- const g = Math.log1p(tot);
1150
+ const g = log1p(tot);
379
1151
  const drop = (ab - ba) / tot;
380
1152
  rhs[a2] += g * drop;
381
1153
  diag[a2] += g;
@@ -427,13 +1199,17 @@ function potentials(c2) {
427
1199
  }
428
1200
  var separator = new Separator(2 * LEAF_R + GAP);
429
1201
  var PITCH = 2 * LEAF_R + 0.3;
1202
+ var sq = (v) => v * v;
1203
+ var ANGLES = 24;
1204
+ var COS = Float64Array.from({ length: ANGLES }, (_, s) => cos(2 * Math.PI * s / ANGLES));
1205
+ var SIN = Float64Array.from({ length: ANGLES }, (_, s) => sin(2 * Math.PI * s / ANGLES));
430
1206
  function settle(items, phi) {
431
1207
  const n = items.length;
432
1208
  const order = [...items.keys()].sort((p, q) => phi[q] - phi[p] || p - q);
433
1209
  let total = 0;
434
- for (const it of items) total += Math.PI * (it.r + 0.3) ** 2;
1210
+ for (const it of items) total += Math.PI * sq(it.r + 0.3);
435
1211
  const R = Math.sqrt(total / (Math.PI * 0.78));
436
- const above = (t) => (Math.acos(t) - t * Math.sqrt(1 - t * t)) / Math.PI;
1212
+ const above = (t) => (acos(t) - t * Math.sqrt(1 - t * t)) / Math.PI;
437
1213
  const heightOf = (f) => {
438
1214
  let lo = -1, hi = 1;
439
1215
  for (let k = 0; k < 40; k++) {
@@ -446,7 +1222,7 @@ function settle(items, phi) {
446
1222
  const share = new Float64Array(n);
447
1223
  let acc = 0;
448
1224
  for (const k of order) {
449
- const a2 = Math.PI * (items[k].r + 0.3) ** 2;
1225
+ const a2 = Math.PI * sq(items[k].r + 0.3);
450
1226
  share[k] = heightOf((acc + a2 / 2) / total);
451
1227
  acc += a2;
452
1228
  }
@@ -461,6 +1237,12 @@ function settle(items, phi) {
461
1237
  });
462
1238
  return;
463
1239
  }
1240
+ const radii = Float64Array.from(big, (k) => items[k].r).sort();
1241
+ const rMax = radii[radii.length - 1];
1242
+ const rPoint = radii[Math.floor(0.9 * (radii.length - 1))];
1243
+ const cell = Math.max(PITCH, 2 * radii[radii.length >> 1]);
1244
+ const grid = new BoxGrid();
1245
+ const few = big.length < 32;
464
1246
  let scale = R;
465
1247
  for (let pass = 0; pass < 3; pass++) {
466
1248
  place2(share.map((t) => -t * scale));
@@ -478,26 +1260,41 @@ function settle(items, phi) {
478
1260
  finish();
479
1261
  function place2(ty) {
480
1262
  const px = [], py = [], pr = [];
1263
+ let t0 = 0, t1 = 0;
1264
+ for (const k of big) {
1265
+ t0 = Math.min(t0, ty[k]);
1266
+ t1 = Math.max(t1, ty[k]);
1267
+ }
1268
+ const span = 3 * (R + Math.max(-t0, t1)) + 2 * rMax;
1269
+ if (!few) grid.reset(Math.max(cell, Math.sqrt(2 * span * (t1 - t0 + 2 * span) / (8 * big.length + 64))), big.length, -span, t0 - span, span, t1 + span);
1270
+ const reachFree = rPoint + 0.3 + 1e-6;
481
1271
  const free = (x, y, r) => {
482
- for (let j = 0; j < px.length; j++) {
1272
+ if (few) {
1273
+ for (let j = 0; j < px.length; j++) {
1274
+ const need = r + pr[j] + 0.3;
1275
+ if (sq(x - px[j]) + sq(y - py[j]) < need * need) return false;
1276
+ }
1277
+ return true;
1278
+ }
1279
+ const e = r + reachFree;
1280
+ const m2 = grid.find(x - e, y - e, x + e, y + e);
1281
+ for (let f = 0; f < m2; f++) {
1282
+ const j = grid.found[f];
483
1283
  const need = r + pr[j] + 0.3;
484
- if ((x - px[j]) ** 2 + (y - py[j]) ** 2 < need * need) return false;
1284
+ if (sq(x - px[j]) + sq(y - py[j]) < need * need) return false;
485
1285
  }
486
1286
  return true;
487
1287
  };
488
- const cost = (x, y, t) => (y - t) ** 2 + 0.3 * x * x;
489
- const ANGLES = 24;
1288
+ const cost = (x, y, t) => sq(y - t) + 0.3 * x * x;
490
1289
  for (const k of big) {
491
1290
  const r = items[k].r;
492
1291
  const t = ty[k];
493
1292
  let bx = 0, by = t, best = free(0, t, r) ? 0 : Infinity;
494
1293
  if (best > 0) {
495
- const near = px.map((_, j) => j).sort((a2, b) => Math.hypot(px[a2], py[a2] - t) - pr[a2] - (Math.hypot(px[b], py[b] - t) - pr[b])).slice(0, 12);
496
- for (const j of near) {
1294
+ for (const j of nearest(px, py, pr, t)) {
497
1295
  const d = r + pr[j] + 0.3;
498
1296
  for (let s = 0; s < ANGLES; s++) {
499
- const a2 = 2 * Math.PI * s / ANGLES;
500
- const x = px[j] + d * Math.cos(a2), y = py[j] + d * Math.sin(a2);
1297
+ const x = px[j] + d * COS[s], y = py[j] + d * SIN[s];
501
1298
  const c2 = cost(x, y, t);
502
1299
  if (c2 < best && free(x, y, r)) {
503
1300
  best = c2;
@@ -512,6 +1309,7 @@ function settle(items, phi) {
512
1309
  }
513
1310
  items[k].x = bx;
514
1311
  items[k].y = by;
1312
+ if (!few) grid.add(px.length, bx, by, r <= rPoint ? 0 : r + 0.3 + 1e-6);
515
1313
  px.push(bx);
516
1314
  py.push(by);
517
1315
  pr.push(r);
@@ -544,6 +1342,15 @@ function settle(items, phi) {
544
1342
  if (leaves.length) {
545
1343
  const rowH = PITCH * Math.sqrt(3) / 2;
546
1344
  let Rl = Math.sqrt(leaves.length) * PITCH * 0.55 + 2 * PITCH;
1345
+ let x0 = 0, y0 = 0, x1 = 0, y1 = 0;
1346
+ for (let q = 0; q < px.length; q++) {
1347
+ x0 = Math.min(x0, px[q] - pr[q]);
1348
+ x1 = Math.max(x1, px[q] + pr[q]);
1349
+ y0 = Math.min(y0, py[q] - pr[q]);
1350
+ y1 = Math.max(y1, py[q] + pr[q]);
1351
+ }
1352
+ grid.reset(Math.max(cell, Math.sqrt((x1 - x0) * (y1 - y0) / (8 * px.length + 64))), px.length, x0, y0, x1, y1);
1353
+ for (let q = 0; q < px.length; q++) grid.add(q, px[q], py[q], pr[q] + LEAF_R + 0.3 + 1e-6);
547
1354
  let cells = [];
548
1355
  for (let attempt = 0; attempt < 60; attempt++) {
549
1356
  cells = [];
@@ -552,14 +1359,17 @@ function settle(items, phi) {
552
1359
  const y = j * rowH;
553
1360
  const off = j & 1 ? PITCH / 2 : 0;
554
1361
  const cols = Math.ceil(Rl / PITCH) + 1;
1362
+ const gj = grid.row(y);
555
1363
  for (let i = -cols; i <= cols; i++) {
556
1364
  const x = i * PITCH + off;
557
1365
  const d2 = x * x + y * y;
558
1366
  if (d2 > Rl * Rl) continue;
559
1367
  let ok = true;
560
- for (let q = 0; q < px.length && ok; q++) {
1368
+ const m2 = grid.cell(grid.col(x), gj, grid.wideOnes(0));
1369
+ for (let f = 0; f < m2 && ok; f++) {
1370
+ const q = grid.found[f];
561
1371
  const need = pr[q] + LEAF_R + 0.3;
562
- if ((x - px[q]) ** 2 + (y - py[q]) ** 2 < need * need) ok = false;
1372
+ if (sq(x - px[q]) + sq(y - py[q]) < need * need) ok = false;
563
1373
  }
564
1374
  if (ok) cells.push([x, y, d2]);
565
1375
  }
@@ -575,6 +1385,48 @@ function settle(items, phi) {
575
1385
  });
576
1386
  }
577
1387
  }
1388
+ function nearest(px, py, pr, t) {
1389
+ const key = (j) => hypot(px[j], py[j] - t) - pr[j];
1390
+ const byKey = (a2, b, ka, kb) => ka - kb || a2 - b;
1391
+ if (px.length <= 12 || few) {
1392
+ const ks2 = px.map((_, j) => key(j));
1393
+ return px.map((_, j) => j).sort((a2, b) => byKey(a2, b, ks2[a2], ks2[b])).slice(0, 12);
1394
+ }
1395
+ const qi = grid.col(0), qj = grid.row(t);
1396
+ const nx = grid.columns, ny = grid.rows, w = grid.width;
1397
+ const seen = /* @__PURE__ */ new Set();
1398
+ const cand = [], ks = [];
1399
+ for (let k = 0; ; k++) {
1400
+ const wide = k === 0 ? grid.wideOnes(0) : 0;
1401
+ for (let f = 0; f < wide; f++) {
1402
+ const b = grid.found[f];
1403
+ if (seen.has(b)) continue;
1404
+ seen.add(b);
1405
+ cand.push(b);
1406
+ ks[b] = key(b);
1407
+ }
1408
+ for (let j = qj - k; j <= qj + k; j++) {
1409
+ if (j < 0 || j >= ny) continue;
1410
+ const edge = j === qj - k || j === qj + k;
1411
+ for (let i = qi - k; i <= qi + k; i += edge ? 1 : 2 * k) {
1412
+ if (i < 0 || i >= nx) continue;
1413
+ const m2 = grid.cell(i, j, 0);
1414
+ for (let f = 0; f < m2; f++) {
1415
+ const b = grid.found[f];
1416
+ if (seen.has(b)) continue;
1417
+ seen.add(b);
1418
+ cand.push(b);
1419
+ ks[b] = key(b);
1420
+ }
1421
+ }
1422
+ }
1423
+ const all = qi - k <= 0 && qi + k >= nx - 1 && qj - k <= 0 && qj + k >= ny - 1;
1424
+ if (cand.length >= 12 || all) {
1425
+ cand.sort((a2, b) => byKey(a2, b, ks[a2], ks[b]));
1426
+ if (all || ks[cand[11]] + 1e-6 < k * w - rPoint) return cand.slice(0, 12);
1427
+ }
1428
+ }
1429
+ }
578
1430
  function finish() {
579
1431
  const B = {
580
1432
  n,
@@ -807,7 +1659,7 @@ var Occupancy = class {
807
1659
  for (let i = gx - 1; i <= gx + 1; i++) {
808
1660
  for (let j = gy - 1; j <= gy + 1; j++) {
809
1661
  for (const [px, py] of this.cells.get(`${i},${j}`) ?? []) {
810
- if ((px - x) ** 2 + (py - y) ** 2 < SPACING * SPACING) return false;
1662
+ if (sq(px - x) + sq(py - y) < SPACING * SPACING) return false;
811
1663
  }
812
1664
  }
813
1665
  }
@@ -815,14 +1667,14 @@ var Occupancy = class {
815
1667
  }
816
1668
  };
817
1669
  function nearestFree(occ, x, y, discs = []) {
818
- const ok = (px, py) => occ.free(px, py) && discs.every((d) => (d.x - px) ** 2 + (d.y - py) ** 2 >= (d.r + LEAF_R) ** 2);
1670
+ const ok = (px, py) => occ.free(px, py) && discs.every((d) => sq(d.x - px) + sq(d.y - py) >= sq(d.r + LEAF_R));
819
1671
  if (ok(x, y)) return [x, y];
820
1672
  for (let ring = 1; ring < 400; ring++) {
821
1673
  const n = 6 * ring;
822
1674
  for (let k = 0; k < n; k++) {
823
1675
  const a2 = 2 * Math.PI * k / n;
824
- const px = x + Math.cos(a2) * ring * SPACING;
825
- const py = y + Math.sin(a2) * ring * SPACING;
1676
+ const px = x + cos(a2) * ring * SPACING;
1677
+ const py = y + sin(a2) * ring * SPACING;
826
1678
  if (ok(px, py)) return [px, py];
827
1679
  }
828
1680
  }
@@ -833,7 +1685,7 @@ function stabilize(fresh, nodes, mem) {
833
1685
  const survivors = nodes.filter((n) => mem.nodes[n.name]).length;
834
1686
  if (survivors < nodes.length * 0.5) return fresh;
835
1687
  const MOVE = 0.1;
836
- const extent = (xs) => Math.max(1, ...xs.map(([x2, y2, r]) => Math.hypot(x2, y2) + r));
1688
+ const extent = (xs) => xs.reduce((m2, [x2, y2, r]) => Math.max(m2, hypot(x2, y2) + r), 1);
837
1689
  const freshTop = extent(fresh.clusters.filter((c2) => c2.parent < 0).map((c2) => [c2.x, c2.y, c2.r]));
838
1690
  const oldTop = extent(Object.values(mem.clusters));
839
1691
  const keeps = (fy, fr, oy, or) => Math.abs(fy / fr - oy / or) <= MOVE;
@@ -861,6 +1713,9 @@ function stabilize(fresh, nodes, mem) {
861
1713
  const x = [...fresh.x];
862
1714
  const y = [...fresh.y];
863
1715
  const occ = new Occupancy();
1716
+ const kids = cs.map(() => []);
1717
+ const top = [];
1718
+ cs.forEach((k, i) => (k.parent >= 0 ? kids[k.parent] : top).push(i));
864
1719
  const placed = new Uint8Array(nodes.length);
865
1720
  nodes.forEach((n, i) => {
866
1721
  const old = mem.nodes[n.name];
@@ -887,16 +1742,130 @@ function stabilize(fresh, nodes, mem) {
887
1742
  tx = cs[c2].x + (fresh.x[i] - fresh.clusters[c2].x);
888
1743
  ty = cs[c2].y + (fresh.y[i] - fresh.clusters[c2].y);
889
1744
  }
890
- const discs = cs.filter((k) => k.parent === c2).map((k) => ({ x: k.x, y: k.y, r: k.r }));
1745
+ const discs = (c2 >= 0 ? kids[c2] : top).map((k) => ({ x: cs[k].x, y: cs[k].y, r: cs[k].r }));
891
1746
  [x[i], y[i]] = nearestFree(occ, tx, ty, discs).map(round);
892
1747
  occ.add(x[i], y[i]);
893
1748
  for (let k = c2; k >= 0; k = cs[k].parent) {
894
- const d = Math.hypot(x[i] - cs[k].x, y[i] - cs[k].y) + LEAF_R + 4;
1749
+ const d = hypot(x[i] - cs[k].x, y[i] - cs[k].y) + LEAF_R + 4;
895
1750
  if (d > cs[k].r) cs[k].r = round(d);
896
1751
  }
897
1752
  });
898
1753
  return relax(withLocal({ x, y, lx: [], ly: [], cluster: fresh.cluster, clusters: cs }));
899
1754
  }
1755
+ function apart(nc, n, X, Y, R, DX, DY) {
1756
+ const push = (a2, b) => {
1757
+ let dx = X[b] - X[a2], dy = Y[b] - Y[a2];
1758
+ let d = hypot(dx, dy);
1759
+ const need = R[a2] + R[b];
1760
+ if (d >= need - 0.5) return 0;
1761
+ if (d < 1e-6) {
1762
+ dx = 1;
1763
+ dy = 0;
1764
+ d = 1;
1765
+ }
1766
+ const over = need - d;
1767
+ const wa = R[b] * R[b] / (R[a2] * R[a2] + R[b] * R[b]);
1768
+ const ax = -dx / d * over * wa, ay = -dy / d * over * wa;
1769
+ X[a2] += ax;
1770
+ Y[a2] += ay;
1771
+ DX[a2] += ax;
1772
+ DY[a2] += ay;
1773
+ const bx = dx / d * over * (1 - wa), by = dy / d * over * (1 - wa);
1774
+ X[b] += bx;
1775
+ Y[b] += by;
1776
+ DX[b] += bx;
1777
+ DY[b] += by;
1778
+ return 1;
1779
+ };
1780
+ let moved = 0;
1781
+ if (n < 64) {
1782
+ for (let iter = 0; iter < 40; iter++) {
1783
+ let any = false;
1784
+ for (let a2 = 0; a2 < nc; a2++) for (let b = a2 + 1; b < n; b++) if (push(a2, b)) any = true, moved++;
1785
+ if (!any) break;
1786
+ }
1787
+ return moved;
1788
+ }
1789
+ const radii = R.slice().sort();
1790
+ const rPoint = radii[Math.floor(0.9 * (n - 1))], mid = radii[n >> 1];
1791
+ const slackFiled = mid / 4, slackLook = mid / 2;
1792
+ const reachOut = slackLook + rPoint + slackFiled;
1793
+ const grid = new BoxGrid();
1794
+ const seen = new Int32Array(n).fill(-1);
1795
+ let cand = new Int32Array(64);
1796
+ const rects = [];
1797
+ const gather = (a2, qx, qy, after, from, to) => {
1798
+ const e = R[a2] + reachOut;
1799
+ const i0 = grid.col(qx - e), i1 = grid.col(qx + e), j0 = grid.row(qy - e), j1 = grid.row(qy + e);
1800
+ let m2 = rects.length ? 0 : grid.wideOnes(0);
1801
+ for (let j = j0; j <= j1; j++) {
1802
+ for (let i = i0; i <= i1; i++) {
1803
+ let done = false;
1804
+ for (let v = 0; v < rects.length && !done; v += 4) done = i >= rects[v] && i <= rects[v + 1] && j >= rects[v + 2] && j <= rects[v + 3];
1805
+ if (!done) m2 = grid.cell(i, j, m2);
1806
+ }
1807
+ }
1808
+ rects.push(i0, i1, j0, j1);
1809
+ if (cand.length < to + m2) {
1810
+ const c2 = new Int32Array(Math.max(2 * cand.length, to + m2));
1811
+ c2.set(cand.subarray(0, to));
1812
+ cand = c2;
1813
+ }
1814
+ let k = to;
1815
+ for (let f = 0; f < m2; f++) {
1816
+ const b = grid.found[f];
1817
+ if (seen[b] === a2) continue;
1818
+ seen[b] = a2;
1819
+ if (b > after) cand[k++] = b;
1820
+ }
1821
+ if (from === to) cand.subarray(from, k).sort();
1822
+ else {
1823
+ for (let j = to; j < k; j++) {
1824
+ const q = cand[j];
1825
+ let i = j;
1826
+ while (i > from && cand[i - 1] > q) {
1827
+ cand[i] = cand[i - 1];
1828
+ i--;
1829
+ }
1830
+ cand[i] = q;
1831
+ }
1832
+ }
1833
+ return k;
1834
+ };
1835
+ const s2 = sq(slackLook * 0.999), f2 = sq(slackFiled * 0.999);
1836
+ for (let iter = 0; iter < 40; iter++) {
1837
+ let any = false;
1838
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
1839
+ for (let k = 0; k < n; k++) {
1840
+ x0 = Math.min(x0, X[k]);
1841
+ x1 = Math.max(x1, X[k]);
1842
+ y0 = Math.min(y0, Y[k]);
1843
+ y1 = Math.max(y1, Y[k]);
1844
+ }
1845
+ grid.reset(Math.max(1, 0.75 * (rPoint + reachOut), Math.sqrt((x1 - x0) * (y1 - y0) / (4 * n + 64))), n, x0, y0, x1, y1);
1846
+ for (let k = 0; k < n; k++) grid.add(k, X[k], Y[k], R[k] <= rPoint ? 0 : R[k] + slackFiled);
1847
+ seen.fill(-1);
1848
+ for (let a2 = 0; a2 < nc; a2++) {
1849
+ let qx = X[a2], qy = Y[a2];
1850
+ rects.length = 0;
1851
+ let nc2 = gather(a2, qx, qy, a2, 0, 0);
1852
+ for (let ci = 0; ci < nc2; ) {
1853
+ const b = cand[ci++];
1854
+ if (!push(a2, b)) continue;
1855
+ any = true;
1856
+ moved++;
1857
+ if (sq(X[b] - grid.fx[b]) + sq(Y[b] - grid.fy[b]) > f2) grid.add(b, X[b], Y[b], grid.fe[b]);
1858
+ if (sq(X[a2] - qx) + sq(Y[a2] - qy) > s2) {
1859
+ qx = X[a2];
1860
+ qy = Y[a2];
1861
+ nc2 = gather(a2, qx, qy, b, ci, nc2);
1862
+ }
1863
+ }
1864
+ }
1865
+ if (!any) break;
1866
+ }
1867
+ return moved;
1868
+ }
900
1869
  function relax(L, only) {
901
1870
  const cs = L.clusters;
902
1871
  const kidsC = cs.map(() => []);
@@ -909,47 +1878,24 @@ function relax(L, only) {
909
1878
  const dnx = new Float64Array(L.x.length), dny = new Float64Array(L.x.length);
910
1879
  const separate = (kc, kn) => {
911
1880
  if (!kc.length) return 0;
912
- const items = [...kc.map((c2) => ({ c: c2, i: -1, r: cs[c2].r })), ...kn.map((i) => ({ c: -1, i, r: LEAF_R }))];
913
- const pos = (it) => it.c >= 0 ? [cs[it.c].lx, cs[it.c].ly] : [L.lx[it.i], L.ly[it.i]];
914
- const shift = (it, dx, dy) => {
915
- if (it.c >= 0) {
916
- cs[it.c].lx += dx;
917
- cs[it.c].ly += dy;
918
- dcx[it.c] += dx;
919
- dcy[it.c] += dy;
920
- } else {
921
- L.lx[it.i] += dx;
922
- L.ly[it.i] += dy;
923
- dnx[it.i] += dx;
924
- dny[it.i] += dy;
925
- }
926
- };
927
- let moved = 0;
928
- for (let iter = 0; iter < 40; iter++) {
929
- let any = false;
930
- for (let a2 = 0; a2 < kc.length; a2++) {
931
- for (let b = a2 + 1; b < items.length; b++) {
932
- const A = items[a2], B = items[b];
933
- const [ax, ay] = pos(A), [bx, by] = pos(B);
934
- let dx = bx - ax, dy = by - ay;
935
- let d = Math.hypot(dx, dy);
936
- const need = A.r + B.r;
937
- if (d >= need - 0.5) continue;
938
- if (d < 1e-6) {
939
- dx = 1;
940
- dy = 0;
941
- d = 1;
942
- }
943
- const push = need - d;
944
- const wa = B.r * B.r / (A.r * A.r + B.r * B.r);
945
- shift(A, -dx / d * push * wa, -dy / d * push * wa);
946
- shift(B, dx / d * push * (1 - wa), dy / d * push * (1 - wa));
947
- any = true;
948
- moved++;
949
- }
950
- }
951
- if (!any) break;
952
- }
1881
+ const nc = kc.length, n = nc + kn.length;
1882
+ const X = new Float64Array(n), Y = new Float64Array(n), R = new Float64Array(n);
1883
+ const DX = new Float64Array(n), DY = new Float64Array(n);
1884
+ kc.forEach((c2, j) => [X[j], Y[j], R[j]] = [cs[c2].lx, cs[c2].ly, cs[c2].r]);
1885
+ kn.forEach((i, j) => [X[nc + j], Y[nc + j], R[nc + j]] = [L.lx[i], L.ly[i], LEAF_R]);
1886
+ const moved = apart(nc, n, X, Y, R, DX, DY);
1887
+ kc.forEach((c2, j) => {
1888
+ cs[c2].lx = X[j];
1889
+ cs[c2].ly = Y[j];
1890
+ dcx[c2] += DX[j];
1891
+ dcy[c2] += DY[j];
1892
+ });
1893
+ kn.forEach((i, j) => {
1894
+ L.lx[i] = X[nc + j];
1895
+ L.ly[i] = Y[nc + j];
1896
+ dnx[i] += DX[nc + j];
1897
+ dny[i] += DY[nc + j];
1898
+ });
953
1899
  return moved;
954
1900
  };
955
1901
  const look = only ? new Set(only) : null;
@@ -957,8 +1903,8 @@ function relax(L, only) {
957
1903
  if (look && !look.has(c2)) continue;
958
1904
  if (separate(kidsC[c2], kidsN[c2]) === 0) continue;
959
1905
  let reach = 0;
960
- for (const k of kidsC[c2]) reach = Math.max(reach, Math.hypot(cs[k].lx, cs[k].ly) + cs[k].r);
961
- for (const i of kidsN[c2]) reach = Math.max(reach, Math.hypot(L.lx[i], L.ly[i]) + LEAF_R);
1906
+ for (const k of kidsC[c2]) reach = Math.max(reach, hypot(cs[k].lx, cs[k].ly) + cs[k].r);
1907
+ for (const i of kidsN[c2]) reach = Math.max(reach, hypot(L.lx[i], L.ly[i]) + LEAF_R);
962
1908
  const was = cs[c2].r;
963
1909
  cs[c2].r = round(Math.max(cs[c2].r, reach + 6));
964
1910
  if (look && cs[c2].r !== was) look.add(cs[c2].parent);