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
@@ -1,14 +1,248 @@
1
1
  // Separating the children of one cluster: the page's scene (web/src/scene.ts, "the layout event")
2
2
  // and the layout (shared/layout.ts, on the server and for boards on the page) both use it.
3
3
  //
4
- // Bodies are discs: a few large ones (sub-clusters) and up to a few thousand small ones
5
- // (declarations, all of radius LEAF_R). A round pushes every overlapping pair apart; what holds its
6
- // place (a drop) stays against what does not, and of two drops the later one stays. Only pairs
7
- // touching something that moved in the previous round are looked at again, and small bodies find
8
- // their neighbours through a hash grid kept in typed arrays, so a drop in a cluster of 2,000
9
- // declarations settles in milliseconds rather than rebuilding everything every round.
4
+ // Bodies are discs: large ones (sub-clusters, a few or a few thousand: a folder of 1,800 files) and
5
+ // up to a few thousand small ones (declarations, all of radius LEAF_R). A round pushes every
6
+ // overlapping pair apart, one pair after another in a fixed order, each push seeing the ones before
7
+ // it; what holds its place (a drop) stays against what does not, and of two drops the later one
8
+ // stays. Only pairs touching something that moved in the previous round are looked at again.
9
+ //
10
+ // A pair that does not overlap is not pushed, so a round need only find the pairs that do, in the
11
+ // order the plain pass takes them — every large body against every later large one, then against
12
+ // the small ones; then the small ones among themselves — to give the plain pass's result to the last
13
+ // bit without looking at the pairs that are far apart. The large bodies are filed in a grid, the
14
+ // small ones in another, each where it was when the round began; one that moves further than a
15
+ // slack is filed again where it is. While one large body meets the others in turn, nothing moves but
16
+ // it and the one it meets, so what it may meet is known from where they are, and looked for again
17
+ // only once it has moved further than a slack itself. Small ones find their neighbours among
18
+ // themselves through a hash grid kept in typed arrays, made at the start of the round. So a drop in a
19
+ // cluster of 2,000 declarations settles in milliseconds, and so do the rounds that settle a cluster
20
+ // of 1,800 files, which the plain pass took most of a minute over (NavierStokesAndEuler by file).
21
+ import { hypot } from "./libm.js";
10
22
  /** Packed bodies touch exactly; only a real overlap counts. */
11
23
  const SLACK = 0.5;
24
+ /** the fewest large bodies a round files in grids: with fewer, it looks at every pair */
25
+ const INDEXED = 200;
26
+ /** the most cells a box is filed in; a wider one is found by every look */
27
+ const WIDE = 4096;
28
+ /** the most cells a grid has: wider than that, its cells are made larger */
29
+ const CELLS = 1 << 20;
30
+ /**
31
+ * Bodies filed in the cells of a square grid over a region, in typed arrays: each by its centre, in
32
+ * one cell, moved from it when filed again; or by a box round it, in every cell the box covers,
33
+ * leaving its old entries behind when filed again. What a box covers is found by visiting its cells.
34
+ * A place outside the region counts in the cell at its edge, so nothing is ever lost: what is found
35
+ * is a superset of what was filed there, for the caller to test exactly.
36
+ */
37
+ export class BoxGrid {
38
+ size = 1;
39
+ ox = 0;
40
+ oy = 0;
41
+ nx = 1;
42
+ ny = 1;
43
+ /** per cell, the first body filed by its centre and the first box entry */
44
+ headP = new Int32Array(1);
45
+ headE = new Int32Array(1);
46
+ /** per body filed by its centre: its cell, and its neighbours in the cell's list */
47
+ at = new Int32Array(0);
48
+ nextP = new Int32Array(0);
49
+ prevP = new Int32Array(0);
50
+ nextE = new Int32Array(0);
51
+ bodyE = new Int32Array(0);
52
+ used = 0;
53
+ stamp = new Int32Array(0);
54
+ token = 0;
55
+ /** boxes too wide to file by their cells (or not numbers), which every look finds */
56
+ wide = [];
57
+ isWide = new Uint8Array(0);
58
+ /** what the last look found */
59
+ found = new Int32Array(16);
60
+ /** where each body was filed, and how far its box reaches from there (0: by its centre) */
61
+ fx = new Float64Array(0);
62
+ fy = new Float64Array(0);
63
+ fe = new Float64Array(0);
64
+ /** Empty, for bodies numbered below `n`, cells `size` wide over [x0, x1] × [y0, y1]. */
65
+ reset(size, n, x0, y0, x1, y1) {
66
+ let s = size > 0 && Number.isFinite(size) ? size : 1;
67
+ if (!(x0 <= x1 && y0 <= y1 && Number.isFinite(x0 + x1 + y0 + y1)))
68
+ x0 = y0 = x1 = y1 = 0;
69
+ while ((Math.floor(x1 / s) - Math.floor(x0 / s) + 1) * (Math.floor(y1 / s) - Math.floor(y0 / s) + 1) > CELLS)
70
+ s *= 2;
71
+ this.size = s;
72
+ this.ox = Math.floor(x0 / s);
73
+ this.oy = Math.floor(y0 / s);
74
+ this.nx = Math.floor(x1 / s) - this.ox + 1;
75
+ this.ny = Math.floor(y1 / s) - this.oy + 1;
76
+ const cells = this.nx * this.ny;
77
+ if (this.headP.length < cells) {
78
+ this.headP = new Int32Array(cells);
79
+ this.headE = new Int32Array(cells);
80
+ }
81
+ this.headP.fill(-1, 0, cells);
82
+ this.headE.fill(-1, 0, cells);
83
+ this.used = 0;
84
+ this.wide.length = 0;
85
+ if (this.stamp.length < n) {
86
+ this.stamp = new Int32Array(n);
87
+ this.isWide = new Uint8Array(n);
88
+ this.at = new Int32Array(n);
89
+ this.nextP = new Int32Array(n);
90
+ this.prevP = new Int32Array(n);
91
+ this.fx = new Float64Array(n);
92
+ this.fy = new Float64Array(n);
93
+ this.fe = new Float64Array(n);
94
+ this.token = 0;
95
+ }
96
+ else
97
+ this.isWide.fill(0, 0, n);
98
+ this.at.fill(-1, 0, n);
99
+ }
100
+ /** the column (row) of a place, at the region's edge if outside it; NaN where it is not a number */
101
+ col(x) {
102
+ const c = Math.floor(x / this.size) - this.ox;
103
+ return c < 0 ? 0 : c >= this.nx ? this.nx - 1 : c;
104
+ }
105
+ row(y) {
106
+ const c = Math.floor(y / this.size) - this.oy;
107
+ return c < 0 ? 0 : c >= this.ny ? this.ny - 1 : c;
108
+ }
109
+ /** the grid's columns and rows, and the width of its cells */
110
+ get columns() {
111
+ return this.nx;
112
+ }
113
+ get rows() {
114
+ return this.ny;
115
+ }
116
+ get width() {
117
+ return this.size;
118
+ }
119
+ /** How many cells the box [x0, x1] × [y0, y1] covers. */
120
+ cells(x0, y0, x1, y1) {
121
+ return (this.col(x1) - this.col(x0) + 1) * (this.row(y1) - this.row(y0) + 1);
122
+ }
123
+ /** File body `k` at (x, y): by its centre where `e` is 0, else by the box reaching `e` from there
124
+ * every way. Filed again the way it was filed first. */
125
+ add(k, x, y, e) {
126
+ this.fx[k] = x;
127
+ this.fy[k] = y;
128
+ this.fe[k] = e;
129
+ if (e === 0) {
130
+ const i = this.col(x), j = this.row(y);
131
+ if (!(i >= 0 && j >= 0)) {
132
+ if (!this.isWide[k])
133
+ this.wide.push(k);
134
+ this.isWide[k] = 1;
135
+ return;
136
+ }
137
+ const c = j * this.nx + i;
138
+ const was = this.at[k];
139
+ if (was === c)
140
+ return;
141
+ if (was >= 0) {
142
+ const p = this.prevP[k], q = this.nextP[k];
143
+ if (p >= 0)
144
+ this.nextP[p] = q;
145
+ else
146
+ this.headP[was] = q;
147
+ if (q >= 0)
148
+ this.prevP[q] = p;
149
+ }
150
+ this.at[k] = c;
151
+ this.prevP[k] = -1;
152
+ this.nextP[k] = this.headP[c];
153
+ if (this.headP[c] >= 0)
154
+ this.prevP[this.headP[c]] = k;
155
+ this.headP[c] = k;
156
+ return;
157
+ }
158
+ const i0 = this.col(x - e), i1 = this.col(x + e);
159
+ const j0 = this.row(y - e), j1 = this.row(y + e);
160
+ const count = (i1 - i0 + 1) * (j1 - j0 + 1);
161
+ if (!(count <= WIDE)) {
162
+ if (!this.isWide[k])
163
+ this.wide.push(k);
164
+ this.isWide[k] = 1;
165
+ return;
166
+ }
167
+ const need = this.used + count;
168
+ if (need > this.nextE.length) {
169
+ const cap = Math.max(need, 2 * this.nextE.length, 64);
170
+ const nn = new Int32Array(cap), nb = new Int32Array(cap);
171
+ nn.set(this.nextE.subarray(0, this.used));
172
+ nb.set(this.bodyE.subarray(0, this.used));
173
+ this.nextE = nn;
174
+ this.bodyE = nb;
175
+ }
176
+ for (let j = j0; j <= j1; j++) {
177
+ for (let i = i0; i <= i1; i++) {
178
+ const h = j * this.nx + i;
179
+ const en = this.used++;
180
+ this.bodyE[en] = k;
181
+ this.nextE[en] = this.headE[h];
182
+ this.headE[h] = en;
183
+ }
184
+ }
185
+ }
186
+ /** The bodies filed in the cells of [x0, x1] × [y0, y1], each once, into `found`; how many. */
187
+ find(x0, y0, x1, y1) {
188
+ const i0 = this.col(x0), i1 = this.col(x1);
189
+ const j0 = this.row(y0), j1 = this.row(y1);
190
+ const m = this.wideOnes(0);
191
+ let n = m;
192
+ for (let j = j0; j <= j1; j++)
193
+ for (let i = i0; i <= i1; i++)
194
+ n = this.cell(i, j, n);
195
+ // each once
196
+ if (this.token >= 0x3fffffff) {
197
+ this.stamp.fill(0);
198
+ this.token = 0;
199
+ }
200
+ const t = ++this.token;
201
+ const stamp = this.stamp, found = this.found;
202
+ let k = 0;
203
+ for (let f = 0; f < n; f++) {
204
+ const b = found[f];
205
+ if (stamp[b] === t)
206
+ continue;
207
+ stamp[b] = t;
208
+ found[k++] = b;
209
+ }
210
+ return k;
211
+ }
212
+ /** The bodies filed in the cell at column `i` and row `j`, appended to `found` from `m` on (one
213
+ * filed by a box perhaps more than once); where they end. */
214
+ cell(i, j, m) {
215
+ const c = j * this.nx + i;
216
+ let found = this.found;
217
+ const nextP = this.nextP;
218
+ for (let k = this.headP[c]; k >= 0; k = nextP[k]) {
219
+ if (m === found.length)
220
+ found = this.grow();
221
+ found[m++] = k;
222
+ }
223
+ const nextE = this.nextE, bodyE = this.bodyE;
224
+ for (let en = this.headE[c]; en >= 0; en = nextE[en]) {
225
+ if (m === found.length)
226
+ found = this.grow();
227
+ found[m++] = bodyE[en];
228
+ }
229
+ return m;
230
+ }
231
+ /** The bodies too wide to file by their cells, appended to `found` from `m` on. */
232
+ wideOnes(m) {
233
+ for (const k of this.wide) {
234
+ if (m === this.found.length)
235
+ this.grow();
236
+ this.found[m++] = k;
237
+ }
238
+ return m;
239
+ }
240
+ grow() {
241
+ const f = new Int32Array(2 * this.found.length);
242
+ f.set(this.found);
243
+ return (this.found = f);
244
+ }
245
+ }
12
246
  export class Separator {
13
247
  head = new Int32Array(0);
14
248
  next = new Int32Array(0);
@@ -16,6 +250,27 @@ export class Separator {
16
250
  moved = new Uint8Array(0);
17
251
  movedNext = new Uint8Array(0);
18
252
  movedSmall = new Int32Array(0);
253
+ /** each body's place among the large ones and among the small ones, or -1 */
254
+ rankBig = new Int32Array(0);
255
+ rankSmall = new Int32Array(0);
256
+ /** the large bodies (up to `rPoint` by their centres, the larger by their boxes), the small ones */
257
+ bigs = new BoxGrid();
258
+ smalls = new BoxGrid();
259
+ rPoint = 0;
260
+ /** the largest small body */
261
+ rSmall = 0;
262
+ /** how far a body may move from where it is filed before it is filed again, and how far the large
263
+ * body meeting the others may move from where it looked before it looks again */
264
+ slackFiled = 1;
265
+ slackLook = 1;
266
+ /** the places in their list of what one large body may meet, in order; which bodies its turn has
267
+ * seen; how many the grid found at the last look */
268
+ cand = new Int32Array(64);
269
+ seen = new Int32Array(0);
270
+ turn = 0;
271
+ looked = 0;
272
+ /** whether this round files the bodies in the grids */
273
+ indexed = false;
19
274
  cell;
20
275
  constructor(cell) {
21
276
  this.cell = cell;
@@ -26,6 +281,10 @@ export class Separator {
26
281
  this.moved = new Uint8Array(n);
27
282
  this.movedNext = new Uint8Array(n);
28
283
  this.movedSmall = new Int32Array(n);
284
+ this.rankBig = new Int32Array(n);
285
+ this.rankSmall = new Int32Array(n);
286
+ this.seen = new Int32Array(n);
287
+ this.turn = 0;
29
288
  }
30
289
  let size = 64;
31
290
  while (size < 2 * nSmall)
@@ -46,13 +305,79 @@ export class Separator {
46
305
  this.head[h] = k;
47
306
  }
48
307
  }
49
- /** Push a and b apart if they overlap; marks what moved. */
308
+ /** Each body's place in its list; the grids' slacks, from the large bodies' sizes. */
309
+ rank(B) {
310
+ const { big, small } = B;
311
+ this.rankBig.fill(-1, 0, B.n);
312
+ this.rankSmall.fill(-1, 0, B.n);
313
+ for (let p = 0; p < big.length; p++)
314
+ this.rankBig[big[p]] = p;
315
+ for (let s = 0; s < small.length; s++)
316
+ this.rankSmall[small[s]] = s;
317
+ let rs = 0;
318
+ for (let s = 0; s < small.length; s++)
319
+ rs = Math.max(rs, B.r[small[s]]);
320
+ this.rSmall = rs;
321
+ // the largest tenth filed by their boxes, the others by their centres; slacks a fraction of the
322
+ // middling large body
323
+ const r = Float64Array.from(big, (k) => B.r[k]).sort();
324
+ const mid = r.length ? r[r.length >> 1] : this.cell / 2;
325
+ this.rPoint = r.length ? r[Math.floor(0.9 * (r.length - 1))] : 0;
326
+ this.slackFiled = mid / 4;
327
+ this.slackLook = mid / 2;
328
+ }
329
+ /** File every body where it is now, over the region they cover. */
330
+ file(B) {
331
+ const { big, small } = B;
332
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
333
+ for (let k = 0; k < B.n; k++) {
334
+ const x = B.x[k], y = B.y[k];
335
+ if (x < x0)
336
+ x0 = x;
337
+ if (x > x1)
338
+ x1 = x;
339
+ if (y < y0)
340
+ y0 = y;
341
+ if (y > y1)
342
+ y1 = y;
343
+ }
344
+ // cells about as wide as a middling look's radius, or wider where the region holds many more
345
+ // cells than bodies
346
+ const area = (x1 - x0) * (y1 - y0);
347
+ 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);
348
+ for (let p = 0; p < big.length; p++) {
349
+ const k = big[p];
350
+ this.bigs.add(k, B.x[k], B.y[k], B.r[k] <= this.rPoint ? 0 : B.r[k] + this.slackFiled);
351
+ }
352
+ this.smalls.reset(Math.max(this.cell, Math.sqrt(area / (4 * small.length + 64))), B.n, x0, y0, x1, y1);
353
+ for (let s = 0; s < small.length; s++) {
354
+ const k = small[s];
355
+ this.smalls.add(k, B.x[k], B.y[k], 0);
356
+ }
357
+ }
358
+ /** How far beyond its own radius a look of a large body reaches, among the large ones or the
359
+ * small ones: to where what may meet it before it looks again was filed. */
360
+ lookReach(small) {
361
+ return this.slackLook + (small ? this.rSmall : this.rPoint) + this.slackFiled;
362
+ }
363
+ /** A body that moved: filed again once it is further than the slack from where it was filed. */
364
+ track(B, k) {
365
+ if (!this.indexed)
366
+ return;
367
+ const g = this.rankBig[k] >= 0 ? this.bigs : this.smalls;
368
+ const dx = B.x[k] - g.fx[k], dy = B.y[k] - g.fy[k];
369
+ const s = this.slackFiled * 0.999;
370
+ if (dx * dx + dy * dy > s * s)
371
+ g.add(k, B.x[k], B.y[k], g.fe[k]);
372
+ }
373
+ /** Push a and b apart if they overlap; marks what moved. 0 when they do not overlap, else 1 if a
374
+ * moved, 2 if b did, 3 if both. */
50
375
  push(B, a, b) {
51
376
  const dx = B.x[b] - B.x[a], dy = B.y[b] - B.y[a];
52
377
  const need = B.r[a] + B.r[b];
53
378
  const d2 = dx * dx + dy * dy;
54
379
  if (d2 >= (need - SLACK) * (need - SLACK))
55
- return false;
380
+ return 0;
56
381
  const pa = B.pinned[a], pb = B.pinned[b];
57
382
  const fixA = pa && (!pb || B.seq[a] > B.seq[b]);
58
383
  const fixB = pb && (!pa || B.seq[b] > B.seq[a]);
@@ -61,23 +386,205 @@ export class Separator {
61
386
  const ra = B.r[a] * B.r[a], rb = B.r[b] * B.r[b];
62
387
  const wa = fixA ? 0 : fixB ? 1 : rb / (ra + rb);
63
388
  const ux = d2 ? dx / d : 1, uy = d2 ? dy / d : 0;
389
+ let m = 0;
64
390
  if (wa > 0) {
65
391
  B.x[a] -= ux * over * wa;
66
392
  B.y[a] -= uy * over * wa;
67
393
  this.movedNext[a] = 1;
394
+ m |= 1;
68
395
  }
69
396
  if (wa < 1) {
70
397
  B.x[b] += ux * over * (1 - wa);
71
398
  B.y[b] += uy * over * (1 - wa);
72
399
  this.movedNext[b] = 1;
400
+ m |= 2;
73
401
  }
74
- return true;
402
+ return m;
75
403
  }
76
404
  overlap(B, a, b) {
77
405
  const dx = B.x[b] - B.x[a], dy = B.y[b] - B.y[a];
78
406
  const need = B.r[a] + B.r[b] - SLACK;
79
407
  return dx * dx + dy * dy < need * need;
80
408
  }
409
+ /** How many cells a look of large body `a` from (qx, qy) visits. */
410
+ lookCells(B, a, qx, qy, small) {
411
+ const r = B.r[a] + this.lookReach(small);
412
+ return (small ? this.smalls : this.bigs).cells(qx - r, qy - r, qx + r, qy + r);
413
+ }
414
+ /** What large body `a` may meet among the large ones (`small` false) or the small ones (true),
415
+ * looking from (qx, qy) — it stays within the slack of there until it looks again — in its turn
416
+ * `t`: the cells of the look its earlier looks have not visited, and in them what it has not seen
417
+ * yet in this turn, their places in their list after place `after` put in order into the
418
+ * candidates from `from` on, where `to` ended them. Returns where they end now; `this.looked` is
419
+ * how many the grid found. */
420
+ gather(B, a, qx, qy, small, after, t, from, to) {
421
+ const g = small ? this.smalls : this.bigs;
422
+ const rank = small ? this.rankSmall : this.rankBig;
423
+ const reach = B.r[a] + this.lookReach(small);
424
+ const i0 = g.col(qx - reach), i1 = g.col(qx + reach);
425
+ const j0 = g.row(qy - reach), j1 = g.row(qy + reach);
426
+ const rects = this.rects;
427
+ const nr = this.nRects;
428
+ let m = nr === 0 ? g.wideOnes(0) : 0;
429
+ for (let j = j0; j <= j1; j++) {
430
+ for (let i = i0; i <= i1; i++) {
431
+ let done = false;
432
+ for (let v = 0; v < nr && !done; v++)
433
+ done = i >= rects[4 * v] && i <= rects[4 * v + 1] && j >= rects[4 * v + 2] && j <= rects[4 * v + 3];
434
+ if (!done)
435
+ m = g.cell(i, j, m);
436
+ }
437
+ }
438
+ if (nr < rects.length / 4) {
439
+ rects[4 * nr] = i0;
440
+ rects[4 * nr + 1] = i1;
441
+ rects[4 * nr + 2] = j0;
442
+ rects[4 * nr + 3] = j1;
443
+ this.nRects = nr + 1;
444
+ }
445
+ else
446
+ this.nRects = 0; // too many looks to remember: the next one visits all its cells again
447
+ this.looked = m;
448
+ if (this.cand.length < to + m) {
449
+ const c = new Int32Array(Math.max(2 * this.cand.length, to + m));
450
+ c.set(this.cand.subarray(0, to));
451
+ this.cand = c;
452
+ }
453
+ const cand = this.cand, found = g.found, seen = this.seen;
454
+ let n = to;
455
+ for (let f = 0; f < m; f++) {
456
+ const k = found[f];
457
+ if (seen[k] === t)
458
+ continue;
459
+ seen[k] = t;
460
+ const q = rank[k];
461
+ if (q > after)
462
+ cand[n++] = q;
463
+ }
464
+ // in order among those still to come: all of them at first, the few new ones later
465
+ if (from === to) {
466
+ if (n - from > 1)
467
+ cand.subarray(from, n).sort();
468
+ }
469
+ else {
470
+ for (let j = to; j < n; j++) {
471
+ const q = cand[j];
472
+ let i = j;
473
+ while (i > from && cand[i - 1] > q) {
474
+ cand[i] = cand[i - 1];
475
+ i--;
476
+ }
477
+ cand[i] = q;
478
+ }
479
+ }
480
+ return n;
481
+ }
482
+ /** the cells each look of the turn visited, as column and row ranges */
483
+ rects = new Int32Array(4 * 16);
484
+ nRects = 0;
485
+ /**
486
+ * Large body big[p] against the later large ones (`small` false) or against the small ones, in
487
+ * their order, as the plain pass takes them, from the grid. Where the grid would visit more cells,
488
+ * or find more bodies, than the plain pass would look at — a crowd, where everything touches
489
+ * everything — the plain pass takes over from where the grid had come to.
490
+ */
491
+ against(B, p, small, nMovedSmall) {
492
+ const list = small ? B.small : B.big;
493
+ const moved = this.moved;
494
+ const a = B.big[p];
495
+ let qx = B.x[a], qy = B.y[a];
496
+ const after = small ? -1 : p;
497
+ // what the plain pass would cost, in pairs; a cell visited costs about four, a body found two
498
+ let budget = small ? (moved[a] ? list.length : nMovedSmall) : list.length - p - 1;
499
+ if (budget === 0)
500
+ return false;
501
+ if (!(4 * this.lookCells(B, a, qx, qy, small) <= budget))
502
+ return this.plain(B, p, small, after, nMovedSmall);
503
+ if (this.turn >= 0x3fffffff) {
504
+ // the marks start again before they could wrap round
505
+ this.seen.fill(0);
506
+ this.turn = 0;
507
+ }
508
+ const t = ++this.turn;
509
+ this.nRects = 0;
510
+ let nc = this.gather(B, a, qx, qy, small, after, t, 0, 0);
511
+ budget -= 2 * this.looked;
512
+ const s2 = this.slackLook * 0.999 * (this.slackLook * 0.999);
513
+ let any = false;
514
+ for (let ci = 0; ci < nc;) {
515
+ const q = this.cand[ci++];
516
+ const b = list[q];
517
+ if (!(moved[a] || moved[b]))
518
+ continue;
519
+ const m = this.push(B, a, b);
520
+ if (!m)
521
+ continue;
522
+ any = true;
523
+ if (m & 2)
524
+ this.track(B, b);
525
+ // a moved: what it may meet now, from where it is, once it is further than the slack
526
+ if (m & 1) {
527
+ const dx = B.x[a] - qx, dy = B.y[a] - qy;
528
+ if (dx * dx + dy * dy > s2) {
529
+ qx = B.x[a];
530
+ qy = B.y[a];
531
+ if (!(4 * this.lookCells(B, a, qx, qy, small) <= budget))
532
+ return this.plain(B, p, small, q, nMovedSmall) || any;
533
+ nc = this.gather(B, a, qx, qy, small, q, t, ci, nc);
534
+ budget -= 2 * this.looked;
535
+ if (budget < 0)
536
+ return this.plain(B, p, small, q, nMovedSmall) || any;
537
+ }
538
+ }
539
+ }
540
+ return any;
541
+ }
542
+ /** The plain pass of large body big[p] against the list's bodies after place `after`: the later
543
+ * large ones, or the small ones (those that moved, where big[p] did not). */
544
+ plain(B, p, small, after, nMovedSmall) {
545
+ const moved = this.moved;
546
+ const a = B.big[p];
547
+ let any = false;
548
+ if (!small) {
549
+ const big = B.big;
550
+ for (let q = after + 1; q < big.length; q++) {
551
+ const b = big[q];
552
+ if (!(moved[a] || moved[b]))
553
+ continue;
554
+ const m = this.push(B, a, b);
555
+ if (m) {
556
+ any = true;
557
+ if (m & 2)
558
+ this.track(B, b);
559
+ }
560
+ }
561
+ }
562
+ else if (moved[a]) {
563
+ const list = B.small;
564
+ for (let s = after + 1; s < list.length; s++) {
565
+ const m = this.push(B, a, list[s]);
566
+ if (m) {
567
+ any = true;
568
+ if (m & 2)
569
+ this.track(B, list[s]);
570
+ }
571
+ }
572
+ }
573
+ else {
574
+ for (let s = 0; s < nMovedSmall; s++) {
575
+ const b = this.movedSmall[s];
576
+ if (after >= 0 && this.rankSmall[b] <= after)
577
+ continue;
578
+ const m = this.push(B, a, b);
579
+ if (m) {
580
+ any = true;
581
+ if (m & 2)
582
+ this.track(B, b);
583
+ }
584
+ }
585
+ }
586
+ return any;
587
+ }
81
588
  /**
82
589
  * Up to `rounds` rounds of separation. After each round a body further from the origin than
83
590
  * `limit[k]` is brought back to it (null: no limit). Returns whether anything still moved in
@@ -86,6 +593,10 @@ export class Separator {
86
593
  separate(B, rounds, limit) {
87
594
  const { big, small } = B;
88
595
  this.ensure(B.n, small.length);
596
+ // the grids serve the large bodies' turns alone, where there are enough of them to be worth it
597
+ const indexed = (this.indexed = big.length >= INDEXED);
598
+ if (indexed)
599
+ this.rank(B);
89
600
  const moved = this.moved, next = this.movedNext;
90
601
  moved.fill(1, 0, B.n);
91
602
  let any = false;
@@ -93,28 +604,18 @@ export class Separator {
93
604
  any = false;
94
605
  next.fill(0, 0, B.n);
95
606
  this.build(B);
607
+ if (indexed)
608
+ this.file(B);
96
609
  let nMovedSmall = 0;
97
610
  for (let s = 0; s < small.length; s++)
98
611
  if (moved[small[s]])
99
612
  this.movedSmall[nMovedSmall++] = small[s];
100
613
  // large against large, and against the small ones (all of them when the large one moved)
101
614
  for (let p = 0; p < big.length; p++) {
102
- const a = big[p];
103
- for (let q = p + 1; q < big.length; q++) {
104
- const b = big[q];
105
- if ((moved[a] || moved[b]) && this.push(B, a, b))
106
- any = true;
107
- }
108
- if (moved[a]) {
109
- for (let s = 0; s < small.length; s++)
110
- if (this.push(B, a, small[s]))
111
- any = true;
112
- }
113
- else {
114
- for (let s = 0; s < nMovedSmall; s++)
115
- if (this.push(B, a, this.movedSmall[s]))
116
- any = true;
117
- }
615
+ if (indexed ? this.against(B, p, false, nMovedSmall) : this.plain(B, p, false, p, nMovedSmall))
616
+ any = true;
617
+ if (indexed ? this.against(B, p, true, nMovedSmall) : this.plain(B, p, true, -1, nMovedSmall))
618
+ any = true;
118
619
  }
119
620
  // small against their grid neighbours, where one of the two moved
120
621
  for (let s = 0; s < nMovedSmall; s++) {
@@ -134,7 +635,7 @@ export class Separator {
134
635
  }
135
636
  if (limit) {
136
637
  for (let k = 0; k < B.n; k++) {
137
- const d = Math.hypot(B.x[k], B.y[k]);
638
+ const d = hypot(B.x[k], B.y[k]);
138
639
  if (d > limit[k]) {
139
640
  B.x[k] *= limit[k] / d;
140
641
  B.y[k] *= limit[k] / d;
@@ -152,13 +653,36 @@ export class Separator {
152
653
  overlaps(B) {
153
654
  const { big, small } = B;
154
655
  this.ensure(B.n, small.length);
656
+ const indexed = big.length >= INDEXED;
657
+ if (indexed) {
658
+ this.rank(B);
659
+ this.file(B);
660
+ }
155
661
  for (let p = 0; p < big.length; p++) {
156
- for (let q = p + 1; q < big.length; q++)
157
- if (this.overlap(B, big[p], big[q]))
158
- return true;
159
- for (let s = 0; s < small.length; s++)
160
- if (this.overlap(B, big[p], small[s]))
161
- return true;
662
+ const a = big[p];
663
+ const x = B.x[a], y = B.y[a];
664
+ // against the later large ones, then against the small ones: from the grids, or each in turn
665
+ for (let pass = 0; pass < 2; pass++) {
666
+ const toSmall = pass === 1;
667
+ const list = toSmall ? small : big;
668
+ const from = toSmall ? 0 : p + 1;
669
+ if (from >= list.length)
670
+ continue;
671
+ if (!indexed || !(this.lookCells(B, a, x, y, toSmall) <= list.length - from)) {
672
+ for (let q = from; q < list.length; q++)
673
+ if (this.overlap(B, a, list[q]))
674
+ return true;
675
+ continue;
676
+ }
677
+ const g = toSmall ? this.smalls : this.bigs;
678
+ const reach = B.r[a] + this.lookReach(toSmall);
679
+ const m = g.find(x - reach, y - reach, x + reach, y + reach);
680
+ for (let f = 0; f < m; f++) {
681
+ const b = g.found[f];
682
+ if ((toSmall || this.rankBig[b] > p) && this.overlap(B, a, b))
683
+ return true;
684
+ }
685
+ }
162
686
  }
163
687
  this.build(B);
164
688
  for (let s = 0; s < small.length; s++) {