visualproof 0.4.3 → 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 (82) hide show
  1. package/README.md +16 -7
  2. package/dist/assets/latex/visualproof.sty +19 -9
  3. package/dist/assets/tips/tour-agent.webp +0 -0
  4. package/dist/assets/tips/tour-arrange.webp +0 -0
  5. package/dist/assets/tips/tour-artwork.webp +0 -0
  6. package/dist/assets/tips/tour-checks.webp +0 -0
  7. package/dist/assets/tips/tour-keys.webp +0 -0
  8. package/dist/assets/tips/tour-library.webp +0 -0
  9. package/dist/assets/tips/tour-project.webp +0 -0
  10. package/dist/assets/tips/tour-review.webp +0 -0
  11. package/dist/assets/tips/tour-share.webp +0 -0
  12. package/dist/assets/tips/tour-source.webp +0 -0
  13. package/dist/assets/tips/tour-target.webp +0 -0
  14. package/dist/assets/tips/tour-view.webp +0 -0
  15. package/dist/assets/tips/tour.json +117 -112
  16. package/dist/assets/tips/tour.webp +0 -0
  17. package/dist/mcp/visualproof-mcp.mjs +7 -3
  18. package/dist/server/access.js +37 -1
  19. package/dist/server/accesspage.js +130 -0
  20. package/dist/server/agent.js +11 -5
  21. package/dist/server/agentlibrary.js +1 -1
  22. package/dist/server/agentviews.js +1 -1
  23. package/dist/server/artcmd.js +1 -1
  24. package/dist/server/askfollow.js +1 -4
  25. package/dist/server/buildwatch.js +133 -0
  26. package/dist/server/deploy.js +844 -0
  27. package/dist/server/doctor.js +15 -3
  28. package/dist/server/events.js +20 -5
  29. package/dist/server/export.js +17 -4
  30. package/dist/server/fetchurl.js +30 -13
  31. package/dist/server/film.js +1 -1
  32. package/dist/server/history.js +3 -1
  33. package/dist/server/historyapi.js +100 -50
  34. package/dist/server/historythread.js +206 -0
  35. package/dist/server/hover.js +8 -0
  36. package/dist/server/identity.js +25 -1
  37. package/dist/server/library.js +10 -3
  38. package/dist/server/live.js +206 -28
  39. package/dist/server/lostfound.js +3 -2
  40. package/dist/server/main.js +361 -50
  41. package/dist/server/poppler.js +48 -10
  42. package/dist/server/renames.js +10 -5
  43. package/dist/server/review.js +32 -2
  44. package/dist/server/reviewapi.js +1 -1
  45. package/dist/server/status.js +144 -50
  46. package/dist/server/talk.js +7 -2
  47. package/dist/server/usage.js +51 -0
  48. package/dist/server/views.js +21 -7
  49. package/dist/shared/board.js +2 -1
  50. package/dist/shared/collab.js +14 -8
  51. package/dist/shared/deploy.js +35 -0
  52. package/dist/shared/doc/latex.js +5 -3
  53. package/dist/shared/doc/outline.js +6 -3
  54. package/dist/shared/layout.js +297 -70
  55. package/dist/shared/libm.js +366 -0
  56. package/dist/shared/library.js +11 -6
  57. package/dist/shared/palette.js +1 -1
  58. package/dist/shared/review.js +7 -5
  59. package/dist/shared/search.js +7 -3
  60. package/dist/shared/separate.js +556 -32
  61. package/dist/shared/views.js +8 -1
  62. package/dist/web/bundle/app.js +3595 -1520
  63. package/dist/web/bundle/edges.js +44 -16
  64. package/dist/web/bundle/layout.js +1026 -80
  65. package/dist/web/index.html +2 -2
  66. package/dist/web/src/artwork.js +25 -4
  67. package/dist/web/src/axiomring.js +42 -0
  68. package/dist/web/src/bundles.js +52 -16
  69. package/dist/web/src/canvas.js +109 -54
  70. package/dist/web/src/overview.js +4 -1
  71. package/dist/web/src/past.js +28 -0
  72. package/dist/web/src/scene.js +13 -12
  73. package/dist/web/src/selection.js +47 -4
  74. package/dist/web/src/status.js +32 -2
  75. package/dist/web/src/theme.js +108 -0
  76. package/dist/web/style.css +181 -45
  77. package/docs/agent-api/following.md +1 -1
  78. package/docs/agent-api/history.md +3 -0
  79. package/docs/agent-api/library.md +1 -1
  80. package/docs/agent-api/requests.md +4 -1
  81. package/docs/agent-api/views.md +1 -1
  82. package/package.json +1 -1
@@ -12,8 +12,15 @@
12
12
  //
13
13
  // A board may hide levels of its tree (ViewSpec decision 24): a hidden cluster's sub-clusters and
14
14
  // declarations sit in its parent, and the tree is laid out as it then is.
15
+ //
16
+ // The same graph gives the same picture on every engine: the server's Node, the page's browser, a
17
+ // follower's other browser. Engines round sines, logarithms and lengths each their own way in the
18
+ // last digit, and the settling below makes a different picture of a different last digit, so the
19
+ // layout computes them itself (shared/libm.ts) from the operations every engine rounds alike; its
20
+ // sorts are total orders, and nothing in it reads the clock or a random number.
15
21
  import { packEnclose, packSiblings } from "d3-hierarchy";
16
- import { Separator } from "./separate.js";
22
+ import { acos, cos, hypot, log1p, sin } from "./libm.js";
23
+ import { BoxGrid, Separator } from "./separate.js";
17
24
  const LEAF_R = 5;
18
25
  /** gap between declarations that touch, as the page packs them */
19
26
  const GAP = 2.5;
@@ -114,7 +121,7 @@ function potentials(c) {
114
121
  const diag = new Float64Array(m);
115
122
  const join = (a, b, ab, ba) => {
116
123
  const tot = ab + ba;
117
- const g = Math.log1p(tot);
124
+ const g = log1p(tot);
118
125
  const drop = (ab - ba) / tot;
119
126
  rhs[a] += g * drop;
120
127
  diag[a] += g;
@@ -174,22 +181,31 @@ function potentials(c) {
174
181
  const separator = new Separator(2 * LEAF_R + GAP);
175
182
  /** declarations sit on a hexagonal lattice this far apart: touching, with room for rounding */
176
183
  const PITCH = 2 * LEAF_R + 0.3;
184
+ const sq = (v) => v * v;
185
+ /** the directions a sub-cluster is tried in round a placed one */
186
+ const ANGLES = 24;
187
+ const COS = Float64Array.from({ length: ANGLES }, (_, s) => cos((2 * Math.PI * s) / ANGLES));
188
+ const SIN = Float64Array.from({ length: ANGLES }, (_, s) => sin((2 * Math.PI * s) / ANGLES));
177
189
  /**
178
190
  * The children settle into a disc by potential, the highest on top. Each one's height is where the
179
191
  * area of everything above it ends (the share of a disc above a chord). Sub-clusters, largest
180
192
  * first, take the free spot nearest their height, sideways being cheap; declarations then fill the
181
193
  * hexagonal lattice cells nearest the centre that the sub-clusters leave free, the highest
182
194
  * potential in the highest cell.
195
+ *
196
+ * What a spot is checked against is found in a grid of the discs placed (shared/separate.ts,
197
+ * BoxGrid), which gives what checking every one of them gave, so that a folder of 1,800 files
198
+ * settles in time proportional to its size.
183
199
  */
184
200
  function settle(items, phi) {
185
201
  const n = items.length;
186
202
  const order = [...items.keys()].sort((p, q) => phi[q] - phi[p] || p - q);
187
203
  let total = 0;
188
204
  for (const it of items)
189
- total += Math.PI * (it.r + 0.3) ** 2;
205
+ total += Math.PI * sq(it.r + 0.3);
190
206
  const R = Math.sqrt(total / (Math.PI * 0.78));
191
207
  // the share of a unit disc's area above the chord at height t
192
- const above = (t) => (Math.acos(t) - t * Math.sqrt(1 - t * t)) / Math.PI;
208
+ const above = (t) => (acos(t) - t * Math.sqrt(1 - t * t)) / Math.PI;
193
209
  const heightOf = (f) => {
194
210
  let lo = -1, hi = 1;
195
211
  for (let k = 0; k < 40; k++) {
@@ -204,7 +220,7 @@ function settle(items, phi) {
204
220
  const share = new Float64Array(n);
205
221
  let acc = 0;
206
222
  for (const k of order) {
207
- const a = Math.PI * (items[k].r + 0.3) ** 2;
223
+ const a = Math.PI * sq(items[k].r + 0.3);
208
224
  share[k] = heightOf((acc + a / 2) / total);
209
225
  acc += a;
210
226
  }
@@ -221,6 +237,15 @@ function settle(items, phi) {
221
237
  });
222
238
  return;
223
239
  }
240
+ // the sub-clusters' sizes: the grids' cells about as wide as a middling one, the largest tenth
241
+ // filed by their boxes and the others by their centres
242
+ const radii = Float64Array.from(big, (k) => items[k].r).sort();
243
+ const rMax = radii[radii.length - 1];
244
+ const rPoint = radii[Math.floor(0.9 * (radii.length - 1))];
245
+ const cell = Math.max(PITCH, 2 * radii[radii.length >> 1]);
246
+ const grid = new BoxGrid();
247
+ /** so few that looking at each is quicker than the grid */
248
+ const few = big.length < 32;
224
249
  // placed as tall as the estimate says, then stretched or squeezed until it comes out round
225
250
  let scale = R;
226
251
  for (let pass = 0; pass < 3; pass++) {
@@ -241,28 +266,46 @@ function settle(items, phi) {
241
266
  function place(ty) {
242
267
  // 1. sub-clusters, largest first
243
268
  const px = [], py = [], pr = [];
269
+ // the placed ones in a grid over where they can go, every target height inside it
270
+ let t0 = 0, t1 = 0;
271
+ for (const k of big) {
272
+ t0 = Math.min(t0, ty[k]);
273
+ t1 = Math.max(t1, ty[k]);
274
+ }
275
+ const span = 3 * (R + Math.max(-t0, t1)) + 2 * rMax;
276
+ if (!few)
277
+ 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);
278
+ const reachFree = rPoint + 0.3 + 1e-6;
244
279
  const free = (x, y, r) => {
245
- for (let j = 0; j < px.length; j++) {
280
+ if (few) {
281
+ for (let j = 0; j < px.length; j++) {
282
+ const need = r + pr[j] + 0.3;
283
+ if (sq(x - px[j]) + sq(y - py[j]) < need * need)
284
+ return false;
285
+ }
286
+ return true;
287
+ }
288
+ const e = r + reachFree;
289
+ const m = grid.find(x - e, y - e, x + e, y + e);
290
+ for (let f = 0; f < m; f++) {
291
+ const j = grid.found[f];
246
292
  const need = r + pr[j] + 0.3;
247
- if ((x - px[j]) ** 2 + (y - py[j]) ** 2 < need * need)
293
+ if (sq(x - px[j]) + sq(y - py[j]) < need * need)
248
294
  return false;
249
295
  }
250
296
  return true;
251
297
  };
252
- const cost = (x, y, t) => (y - t) ** 2 + 0.3 * x * x;
253
- const ANGLES = 24;
298
+ const cost = (x, y, t) => sq(y - t) + 0.3 * x * x;
254
299
  for (const k of big) {
255
300
  const r = items[k].r;
256
301
  const t = ty[k];
257
302
  let bx = 0, by = t, best = free(0, t, r) ? 0 : Infinity;
258
303
  if (best > 0) {
259
304
  // touching one of the placed discs nearest the target
260
- const near = px.map((_, j) => j).sort((a, b) => Math.hypot(px[a], py[a] - t) - pr[a] - (Math.hypot(px[b], py[b] - t) - pr[b])).slice(0, 12);
261
- for (const j of near) {
305
+ for (const j of nearest(px, py, pr, t)) {
262
306
  const d = r + pr[j] + 0.3;
263
307
  for (let s = 0; s < ANGLES; s++) {
264
- const a = (2 * Math.PI * s) / ANGLES;
265
- const x = px[j] + d * Math.cos(a), y = py[j] + d * Math.sin(a);
308
+ const x = px[j] + d * COS[s], y = py[j] + d * SIN[s];
266
309
  const c = cost(x, y, t);
267
310
  if (c < best && free(x, y, r)) {
268
311
  best = c;
@@ -280,6 +323,8 @@ function settle(items, phi) {
280
323
  }
281
324
  items[k].x = bx;
282
325
  items[k].y = by;
326
+ if (!few)
327
+ grid.add(px.length, bx, by, r <= rPoint ? 0 : r + 0.3 + 1e-6);
283
328
  px.push(bx);
284
329
  py.push(by);
285
330
  pr.push(r);
@@ -315,6 +360,17 @@ function settle(items, phi) {
315
360
  if (leaves.length) {
316
361
  const rowH = (PITCH * Math.sqrt(3)) / 2;
317
362
  let Rl = Math.sqrt(leaves.length) * PITCH * 0.55 + 2 * PITCH;
363
+ // the discs by the boxes a cell's centre must stay out of
364
+ let x0 = 0, y0 = 0, x1 = 0, y1 = 0;
365
+ for (let q = 0; q < px.length; q++) {
366
+ x0 = Math.min(x0, px[q] - pr[q]);
367
+ x1 = Math.max(x1, px[q] + pr[q]);
368
+ y0 = Math.min(y0, py[q] - pr[q]);
369
+ y1 = Math.max(y1, py[q] + pr[q]);
370
+ }
371
+ grid.reset(Math.max(cell, Math.sqrt(((x1 - x0) * (y1 - y0)) / (8 * px.length + 64))), px.length, x0, y0, x1, y1);
372
+ for (let q = 0; q < px.length; q++)
373
+ grid.add(q, px[q], py[q], pr[q] + LEAF_R + 0.3 + 1e-6);
318
374
  let cells = [];
319
375
  for (let attempt = 0; attempt < 60; attempt++) {
320
376
  cells = [];
@@ -323,15 +379,18 @@ function settle(items, phi) {
323
379
  const y = j * rowH;
324
380
  const off = j & 1 ? PITCH / 2 : 0;
325
381
  const cols = Math.ceil(Rl / PITCH) + 1;
382
+ const gj = grid.row(y);
326
383
  for (let i = -cols; i <= cols; i++) {
327
384
  const x = i * PITCH + off;
328
385
  const d2 = x * x + y * y;
329
386
  if (d2 > Rl * Rl)
330
387
  continue;
331
388
  let ok = true;
332
- for (let q = 0; q < px.length && ok; q++) {
389
+ const m = grid.cell(grid.col(x), gj, grid.wideOnes(0));
390
+ for (let f = 0; f < m && ok; f++) {
391
+ const q = grid.found[f];
333
392
  const need = pr[q] + LEAF_R + 0.3;
334
- if ((x - px[q]) ** 2 + (y - py[q]) ** 2 < need * need)
393
+ if (sq(x - px[q]) + sq(y - py[q]) < need * need)
335
394
  ok = false;
336
395
  }
337
396
  if (ok)
@@ -350,6 +409,57 @@ function settle(items, phi) {
350
409
  });
351
410
  }
352
411
  }
412
+ /** The (at most) twelve placed discs nearest (0, t) — by the distance to their edge, the first
413
+ * placed first among equals — found ring by ring of cells round it: a disc not found yet is at
414
+ * least as far as the rings reach, less the largest radius filed by its centre. */
415
+ function nearest(px, py, pr, t) {
416
+ const key = (j) => hypot(px[j], py[j] - t) - pr[j];
417
+ const byKey = (a, b, ka, kb) => ka - kb || a - b;
418
+ if (px.length <= 12 || few) {
419
+ const ks = px.map((_, j) => key(j));
420
+ return px.map((_, j) => j).sort((a, b) => byKey(a, b, ks[a], ks[b])).slice(0, 12);
421
+ }
422
+ const qi = grid.col(0), qj = grid.row(t);
423
+ const nx = grid.columns, ny = grid.rows, w = grid.width;
424
+ const seen = new Set();
425
+ const cand = [], ks = [];
426
+ for (let k = 0;; k++) {
427
+ // the discs too wide to file by their cells, with the first ring
428
+ const wide = k === 0 ? grid.wideOnes(0) : 0;
429
+ for (let f = 0; f < wide; f++) {
430
+ const b = grid.found[f];
431
+ if (seen.has(b))
432
+ continue;
433
+ seen.add(b);
434
+ cand.push(b);
435
+ ks[b] = key(b);
436
+ }
437
+ for (let j = qj - k; j <= qj + k; j++) {
438
+ if (j < 0 || j >= ny)
439
+ continue;
440
+ const edge = j === qj - k || j === qj + k;
441
+ for (let i = qi - k; i <= qi + k; i += edge ? 1 : 2 * k) {
442
+ if (i < 0 || i >= nx)
443
+ continue;
444
+ const m = grid.cell(i, j, 0);
445
+ for (let f = 0; f < m; f++) {
446
+ const b = grid.found[f];
447
+ if (seen.has(b))
448
+ continue;
449
+ seen.add(b);
450
+ cand.push(b);
451
+ ks[b] = key(b);
452
+ }
453
+ }
454
+ }
455
+ const all = qi - k <= 0 && qi + k >= nx - 1 && qj - k <= 0 && qj + k >= ny - 1;
456
+ if (cand.length >= 12 || all) {
457
+ cand.sort((a, b) => byKey(a, b, ks[a], ks[b]));
458
+ if (all || ks[cand[11]] + 1e-6 < k * w - rPoint)
459
+ return cand.slice(0, 12);
460
+ }
461
+ }
462
+ }
353
463
  function finish() {
354
464
  // nothing should touch now; if anything does, it is pushed apart
355
465
  const B = {
@@ -644,7 +754,7 @@ class Occupancy {
644
754
  for (let i = gx - 1; i <= gx + 1; i++) {
645
755
  for (let j = gy - 1; j <= gy + 1; j++) {
646
756
  for (const [px, py] of this.cells.get(`${i},${j}`) ?? []) {
647
- if ((px - x) ** 2 + (py - y) ** 2 < SPACING * SPACING)
757
+ if (sq(px - x) + sq(py - y) < SPACING * SPACING)
648
758
  return false;
649
759
  }
650
760
  }
@@ -655,15 +765,15 @@ class Occupancy {
655
765
  /** The free spot nearest to (x, y), searched outward on a hexagonal spiral; `discs` are
656
766
  * sub-clusters the spot must stay out of. */
657
767
  function nearestFree(occ, x, y, discs = []) {
658
- const ok = (px, py) => occ.free(px, py) && discs.every((d) => (d.x - px) ** 2 + (d.y - py) ** 2 >= (d.r + LEAF_R) ** 2);
768
+ const ok = (px, py) => occ.free(px, py) && discs.every((d) => sq(d.x - px) + sq(d.y - py) >= sq(d.r + LEAF_R));
659
769
  if (ok(x, y))
660
770
  return [x, y];
661
771
  for (let ring = 1; ring < 400; ring++) {
662
772
  const n = 6 * ring;
663
773
  for (let k = 0; k < n; k++) {
664
774
  const a = (2 * Math.PI * k) / n;
665
- const px = x + Math.cos(a) * ring * SPACING;
666
- const py = y + Math.sin(a) * ring * SPACING;
775
+ const px = x + cos(a) * ring * SPACING;
776
+ const py = y + sin(a) * ring * SPACING;
667
777
  if (ok(px, py))
668
778
  return [px, py];
669
779
  }
@@ -682,7 +792,7 @@ export function stabilize(fresh, nodes, mem) {
682
792
  return fresh;
683
793
  const MOVE = 0.1;
684
794
  // the top level has no circle: heights there are measured against the extent of everything
685
- const extent = (xs) => Math.max(1, ...xs.map(([x, y, r]) => Math.hypot(x, y) + r));
795
+ const extent = (xs) => xs.reduce((m, [x, y, r]) => Math.max(m, hypot(x, y) + r), 1);
686
796
  const freshTop = extent(fresh.clusters.filter((c) => c.parent < 0).map((c) => [c.x, c.y, c.r]));
687
797
  const oldTop = extent(Object.values(mem.clusters));
688
798
  /** an offset in a frame of radius `r`, kept if its height did not move much */
@@ -713,6 +823,10 @@ export function stabilize(fresh, nodes, mem) {
713
823
  const x = [...fresh.x];
714
824
  const y = [...fresh.y];
715
825
  const occ = new Occupancy();
826
+ // each cluster's sub-clusters, and the top level's
827
+ const kids = cs.map(() => []);
828
+ const top = [];
829
+ cs.forEach((k, i) => (k.parent >= 0 ? kids[k.parent] : top).push(i));
716
830
  const placed = new Uint8Array(nodes.length);
717
831
  nodes.forEach((n, i) => {
718
832
  const old = mem.nodes[n.name];
@@ -745,12 +859,12 @@ export function stabilize(fresh, nodes, mem) {
745
859
  ty = cs[c].y + (fresh.y[i] - fresh.clusters[c].y);
746
860
  }
747
861
  // stay out of the sub-clusters of its own cluster, as they stand now
748
- const discs = cs.filter((k) => k.parent === c).map((k) => ({ x: k.x, y: k.y, r: k.r }));
862
+ const discs = (c >= 0 ? kids[c] : top).map((k) => ({ x: cs[k].x, y: cs[k].y, r: cs[k].r }));
749
863
  [x[i], y[i]] = nearestFree(occ, tx, ty, discs).map(round);
750
864
  occ.add(x[i], y[i]);
751
865
  // a disc grows to hold what was placed outside it, and so do the discs around it
752
866
  for (let k = c; k >= 0; k = cs[k].parent) {
753
- const d = Math.hypot(x[i] - cs[k].x, y[i] - cs[k].y) + LEAF_R + 4;
867
+ const d = hypot(x[i] - cs[k].x, y[i] - cs[k].y) + LEAF_R + 4;
754
868
  if (d > cs[k].r)
755
869
  cs[k].r = round(d);
756
870
  }
@@ -810,6 +924,147 @@ export function regroup(tree, nodes, mem, at) {
810
924
  const L = withLocal({ x, y, lx: [], ly: [], cluster: tree.cluster, clusters: cs });
811
925
  return anew.size ? relax(L, anew) : L;
812
926
  }
927
+ /**
928
+ * The siblings of one parent pushed apart, as relax does: up to 40 rounds, each pushing every pair
929
+ * that overlaps — a cluster (the first `nc` items) against every item after it — in that order, the
930
+ * smaller one moving more; `DX`, `DY` add up how far each item moved. Returns how many pushes there
931
+ * were. While a cluster meets the others in turn nothing moves but it and the one it meets, so what
932
+ * it may meet is found in a grid of where they were (shared/separate.ts, BoxGrid), which gives what
933
+ * looking at every pair gave, to the last bit.
934
+ */
935
+ function apart(nc, n, X, Y, R, DX, DY) {
936
+ const push = (a, b) => {
937
+ let dx = X[b] - X[a], dy = Y[b] - Y[a];
938
+ let d = hypot(dx, dy);
939
+ // packed discs touch exactly: only a real overlap counts
940
+ const need = R[a] + R[b];
941
+ if (d >= need - 0.5)
942
+ return 0;
943
+ if (d < 1e-6) {
944
+ dx = 1;
945
+ dy = 0;
946
+ d = 1;
947
+ }
948
+ const over = need - d;
949
+ const wa = (R[b] * R[b]) / (R[a] * R[a] + R[b] * R[b]); // the smaller one moves more
950
+ const ax = (-dx / d) * over * wa, ay = (-dy / d) * over * wa;
951
+ X[a] += ax;
952
+ Y[a] += ay;
953
+ DX[a] += ax;
954
+ DY[a] += ay;
955
+ const bx = (dx / d) * over * (1 - wa), by = (dy / d) * over * (1 - wa);
956
+ X[b] += bx;
957
+ Y[b] += by;
958
+ DX[b] += bx;
959
+ DY[b] += by;
960
+ return 1;
961
+ };
962
+ let moved = 0;
963
+ if (n < 64) {
964
+ for (let iter = 0; iter < 40; iter++) {
965
+ let any = false;
966
+ for (let a = 0; a < nc; a++)
967
+ for (let b = a + 1; b < n; b++)
968
+ if (push(a, b))
969
+ (any = true), moved++;
970
+ if (!any)
971
+ break;
972
+ }
973
+ return moved;
974
+ }
975
+ // the largest tenth filed by their boxes, the others by their centres; slacks a fraction of the
976
+ // middling radius
977
+ const radii = R.slice().sort();
978
+ const rPoint = radii[Math.floor(0.9 * (n - 1))], mid = radii[n >> 1];
979
+ const slackFiled = mid / 4, slackLook = mid / 2;
980
+ const reachOut = slackLook + rPoint + slackFiled;
981
+ const grid = new BoxGrid();
982
+ const seen = new Int32Array(n).fill(-1);
983
+ let cand = new Int32Array(64);
984
+ const rects = [];
985
+ /** what cluster `a` may meet seen from (qx, qy), in the cells no earlier look of its turn visited:
986
+ * those after `after`, in order, into the candidates from `from` on, where `to` ended them */
987
+ const gather = (a, qx, qy, after, from, to) => {
988
+ const e = R[a] + reachOut;
989
+ const i0 = grid.col(qx - e), i1 = grid.col(qx + e), j0 = grid.row(qy - e), j1 = grid.row(qy + e);
990
+ let m = rects.length ? 0 : grid.wideOnes(0);
991
+ for (let j = j0; j <= j1; j++) {
992
+ for (let i = i0; i <= i1; i++) {
993
+ let done = false;
994
+ for (let v = 0; v < rects.length && !done; v += 4)
995
+ done = i >= rects[v] && i <= rects[v + 1] && j >= rects[v + 2] && j <= rects[v + 3];
996
+ if (!done)
997
+ m = grid.cell(i, j, m);
998
+ }
999
+ }
1000
+ rects.push(i0, i1, j0, j1);
1001
+ if (cand.length < to + m) {
1002
+ const c = new Int32Array(Math.max(2 * cand.length, to + m));
1003
+ c.set(cand.subarray(0, to));
1004
+ cand = c;
1005
+ }
1006
+ let k = to;
1007
+ for (let f = 0; f < m; f++) {
1008
+ const b = grid.found[f];
1009
+ if (seen[b] === a)
1010
+ continue;
1011
+ seen[b] = a;
1012
+ if (b > after)
1013
+ cand[k++] = b;
1014
+ }
1015
+ if (from === to)
1016
+ cand.subarray(from, k).sort();
1017
+ else {
1018
+ for (let j = to; j < k; j++) {
1019
+ const q = cand[j];
1020
+ let i = j;
1021
+ while (i > from && cand[i - 1] > q) {
1022
+ cand[i] = cand[i - 1];
1023
+ i--;
1024
+ }
1025
+ cand[i] = q;
1026
+ }
1027
+ }
1028
+ return k;
1029
+ };
1030
+ const s2 = sq(slackLook * 0.999), f2 = sq(slackFiled * 0.999);
1031
+ for (let iter = 0; iter < 40; iter++) {
1032
+ let any = false;
1033
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
1034
+ for (let k = 0; k < n; k++) {
1035
+ x0 = Math.min(x0, X[k]);
1036
+ x1 = Math.max(x1, X[k]);
1037
+ y0 = Math.min(y0, Y[k]);
1038
+ y1 = Math.max(y1, Y[k]);
1039
+ }
1040
+ grid.reset(Math.max(1, 0.75 * (rPoint + reachOut), Math.sqrt(((x1 - x0) * (y1 - y0)) / (4 * n + 64))), n, x0, y0, x1, y1);
1041
+ for (let k = 0; k < n; k++)
1042
+ grid.add(k, X[k], Y[k], R[k] <= rPoint ? 0 : R[k] + slackFiled);
1043
+ seen.fill(-1);
1044
+ for (let a = 0; a < nc; a++) {
1045
+ let qx = X[a], qy = Y[a];
1046
+ rects.length = 0;
1047
+ let nc2 = gather(a, qx, qy, a, 0, 0);
1048
+ for (let ci = 0; ci < nc2;) {
1049
+ const b = cand[ci++];
1050
+ if (!push(a, b))
1051
+ continue;
1052
+ any = true;
1053
+ moved++;
1054
+ if (sq(X[b] - grid.fx[b]) + sq(Y[b] - grid.fy[b]) > f2)
1055
+ grid.add(b, X[b], Y[b], grid.fe[b]);
1056
+ if (sq(X[a] - qx) + sq(Y[a] - qy) > s2) {
1057
+ qx = X[a];
1058
+ qy = Y[a];
1059
+ nc2 = gather(a, qx, qy, b, ci, nc2);
1060
+ }
1061
+ }
1062
+ }
1063
+ if (!any)
1064
+ break;
1065
+ }
1066
+ return moved;
1067
+ }
813
1068
  /** Clusters that grew in place (new members) or arrived (new clusters) can overlap their
814
1069
  * siblings. Push siblings apart in each parent's local frame, smallest moving most, bottom-up so
815
1070
  * a parent sees its children's final sizes; then let each parent grow to hold its children and
@@ -831,52 +1086,24 @@ export function relax(L, only) {
831
1086
  const separate = (kc, kn) => {
832
1087
  if (!kc.length)
833
1088
  return 0;
834
- const items = [...kc.map((c) => ({ c, i: -1, r: cs[c].r })), ...kn.map((i) => ({ c: -1, i, r: LEAF_R }))];
835
- const pos = (it) => (it.c >= 0 ? [cs[it.c].lx, cs[it.c].ly] : [L.lx[it.i], L.ly[it.i]]);
836
- const shift = (it, dx, dy) => {
837
- if (it.c >= 0) {
838
- cs[it.c].lx += dx;
839
- cs[it.c].ly += dy;
840
- dcx[it.c] += dx;
841
- dcy[it.c] += dy;
842
- }
843
- else {
844
- L.lx[it.i] += dx;
845
- L.ly[it.i] += dy;
846
- dnx[it.i] += dx;
847
- dny[it.i] += dy;
848
- }
849
- };
850
- let moved = 0;
851
- for (let iter = 0; iter < 40; iter++) {
852
- let any = false;
853
- // only pairs with a cluster in them: declarations among themselves were placed apart
854
- for (let a = 0; a < kc.length; a++) {
855
- for (let b = a + 1; b < items.length; b++) {
856
- const A = items[a], B = items[b];
857
- const [ax, ay] = pos(A), [bx, by] = pos(B);
858
- let dx = bx - ax, dy = by - ay;
859
- let d = Math.hypot(dx, dy);
860
- // packed discs touch exactly: only a real overlap counts
861
- const need = A.r + B.r;
862
- if (d >= need - 0.5)
863
- continue;
864
- if (d < 1e-6) {
865
- dx = 1;
866
- dy = 0;
867
- d = 1;
868
- }
869
- const push = need - d;
870
- const wa = (B.r * B.r) / (A.r * A.r + B.r * B.r); // the smaller one moves more
871
- shift(A, (-dx / d) * push * wa, (-dy / d) * push * wa);
872
- shift(B, (dx / d) * push * (1 - wa), (dy / d) * push * (1 - wa));
873
- any = true;
874
- moved++;
875
- }
876
- }
877
- if (!any)
878
- break;
879
- }
1089
+ const nc = kc.length, n = nc + kn.length;
1090
+ const X = new Float64Array(n), Y = new Float64Array(n), R = new Float64Array(n);
1091
+ const DX = new Float64Array(n), DY = new Float64Array(n);
1092
+ kc.forEach((c, j) => ([X[j], Y[j], R[j]] = [cs[c].lx, cs[c].ly, cs[c].r]));
1093
+ kn.forEach((i, j) => ([X[nc + j], Y[nc + j], R[nc + j]] = [L.lx[i], L.ly[i], LEAF_R]));
1094
+ const moved = apart(nc, n, X, Y, R, DX, DY);
1095
+ kc.forEach((c, j) => {
1096
+ cs[c].lx = X[j];
1097
+ cs[c].ly = Y[j];
1098
+ dcx[c] += DX[j];
1099
+ dcy[c] += DY[j];
1100
+ });
1101
+ kn.forEach((i, j) => {
1102
+ L.lx[i] = X[nc + j];
1103
+ L.ly[i] = Y[nc + j];
1104
+ dnx[i] += DX[nc + j];
1105
+ dny[i] += DY[nc + j];
1106
+ });
880
1107
  return moved;
881
1108
  };
882
1109
  const look = only ? new Set(only) : null;
@@ -888,9 +1115,9 @@ export function relax(L, only) {
888
1115
  // grow to hold what moved
889
1116
  let reach = 0;
890
1117
  for (const k of kidsC[c])
891
- reach = Math.max(reach, Math.hypot(cs[k].lx, cs[k].ly) + cs[k].r);
1118
+ reach = Math.max(reach, hypot(cs[k].lx, cs[k].ly) + cs[k].r);
892
1119
  for (const i of kidsN[c])
893
- reach = Math.max(reach, Math.hypot(L.lx[i], L.ly[i]) + LEAF_R);
1120
+ reach = Math.max(reach, hypot(L.lx[i], L.ly[i]) + LEAF_R);
894
1121
  const was = cs[c].r;
895
1122
  cs[c].r = round(Math.max(cs[c].r, reach + 6));
896
1123
  // a parent that grew may now cover its siblings: its own parent is looked at in its turn