@newtone-dev/ui-react-web 0.65.0 → 0.67.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/icon/icon.css +8 -0
  2. package/dist/icon/icon.d.ts +7 -1
  3. package/dist/icon/icon.d.ts.map +1 -1
  4. package/dist/icon/icon.js +3 -2
  5. package/dist/icon/icon.js.map +1 -1
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1 -0
  9. package/dist/index.js.map +1 -1
  10. package/dist/nav-list/nav-list.css +18 -1
  11. package/dist/nav-list/nav-list.d.ts +23 -2
  12. package/dist/nav-list/nav-list.d.ts.map +1 -1
  13. package/dist/nav-list/nav-list.js +44 -6
  14. package/dist/nav-list/nav-list.js.map +1 -1
  15. package/dist/node-map/beads-gl.d.ts +39 -1
  16. package/dist/node-map/beads-gl.d.ts.map +1 -1
  17. package/dist/node-map/beads-gl.js +266 -34
  18. package/dist/node-map/beads-gl.js.map +1 -1
  19. package/dist/node-map/index.d.ts +1 -1
  20. package/dist/node-map/index.d.ts.map +1 -1
  21. package/dist/node-map/index.js +1 -1
  22. package/dist/node-map/index.js.map +1 -1
  23. package/dist/node-map/node-map-3d.d.ts +7 -1
  24. package/dist/node-map/node-map-3d.d.ts.map +1 -1
  25. package/dist/node-map/node-map-3d.js +524 -98
  26. package/dist/node-map/node-map-3d.js.map +1 -1
  27. package/dist/node-map/node-map.css +93 -3
  28. package/dist/node-map/node-map.d.ts +51 -2
  29. package/dist/node-map/node-map.d.ts.map +1 -1
  30. package/dist/node-map/node-map.js +9 -3
  31. package/dist/node-map/node-map.js.map +1 -1
  32. package/dist/node-map/space.d.ts +121 -0
  33. package/dist/node-map/space.d.ts.map +1 -1
  34. package/dist/node-map/space.js +551 -1
  35. package/dist/node-map/space.js.map +1 -1
  36. package/dist/pick/index.d.ts +29 -0
  37. package/dist/pick/index.d.ts.map +1 -0
  38. package/dist/pick/index.js +45 -0
  39. package/dist/pick/index.js.map +1 -0
  40. package/dist/pick/palettes.d.ts +2 -0
  41. package/dist/pick/palettes.d.ts.map +1 -0
  42. package/dist/pick/palettes.js +3 -0
  43. package/dist/pick/palettes.js.map +1 -0
  44. package/dist/pick/pick.css +634 -0
  45. package/dist/shade-picker/index.d.ts +5 -0
  46. package/dist/shade-picker/index.d.ts.map +1 -0
  47. package/dist/shade-picker/index.js +3 -0
  48. package/dist/shade-picker/index.js.map +1 -0
  49. package/dist/shade-picker/shade-picker.css +52 -0
  50. package/dist/shade-picker/shade-picker.d.ts +31 -0
  51. package/dist/shade-picker/shade-picker.d.ts.map +1 -0
  52. package/dist/shade-picker/shade-picker.js +61 -0
  53. package/dist/shade-picker/shade-picker.js.map +1 -0
  54. package/dist/split/split.css +1 -0
  55. package/dist/split/split.d.ts +9 -1
  56. package/dist/split/split.d.ts.map +1 -1
  57. package/dist/split/split.js +2 -2
  58. package/dist/split/split.js.map +1 -1
  59. package/package.json +13 -1
@@ -225,7 +225,7 @@ export function unturned(v, cam) {
225
225
  }
226
226
  /** Where a point falls, seen by `cam` with focal length `f`, in a box whose middle is (`cx`, `cy`). */
227
227
  export function seen(p, cam, f, cx, cy) {
228
- const { x: x1, y: y2, z: z2 } = turned(p.x - cam.target.x, p.y - cam.target.y, p.z - cam.target.z, cam);
228
+ const { x: x1, y: y2, z: z2, } = turned(p.x - cam.target.x, p.y - cam.target.y, p.z - cam.target.z, cam);
229
229
  const depth = cam.dist - z2;
230
230
  const k = f / Math.max(depth, 1e-3);
231
231
  return {
@@ -236,4 +236,554 @@ export function seen(p, cam, f, cx, cy) {
236
236
  v: { x: x1, y: y2, z: z2 },
237
237
  };
238
238
  }
239
+ /**
240
+ * The tree from `home` over the ties, breadth first. Neighbours are visited in the consumer's order of `nodes`, so a
241
+ * node with two neighbours one tie nearer hangs from the one listed first, the same every time.
242
+ */
243
+ export function treeOf(nodes, edges, home) {
244
+ const index = new Map(nodes.map((n, i) => [n.id, i]));
245
+ const near = new Map();
246
+ for (const e of edges) {
247
+ if (!index.has(e.from) || !index.has(e.to) || e.from === e.to)
248
+ continue;
249
+ (near.get(e.from) ?? near.set(e.from, []).get(e.from)).push(e.to);
250
+ (near.get(e.to) ?? near.set(e.to, []).get(e.to)).push(e.from);
251
+ }
252
+ for (const list of near.values())
253
+ list.sort((a, b) => index.get(a) - index.get(b));
254
+ const depth = new Map();
255
+ const parent = new Map();
256
+ const children = new Map();
257
+ if (!index.has(home))
258
+ return { home, depth, parent, children };
259
+ depth.set(home, 0);
260
+ const queue = [home];
261
+ for (let q = 0; q < queue.length; q++) {
262
+ const id = queue[q];
263
+ const kids = [];
264
+ for (const o of near.get(id) ?? []) {
265
+ if (depth.has(o))
266
+ continue;
267
+ depth.set(o, depth.get(id) + 1);
268
+ parent.set(o, id);
269
+ kids.push(o);
270
+ queue.push(o);
271
+ }
272
+ children.set(id, kids);
273
+ }
274
+ return { home, depth, parent, children };
275
+ }
276
+ /**
277
+ * A branch (s_map_branch): the node, everything that hangs from it in the tree from `home`, and the path back to home.
278
+ * Without a home the node's tree is rooted at itself, so its branch is everything joined to it. A node home cannot
279
+ * reach is its own branch with its direct ties.
280
+ */
281
+ export function branchOf(nodes, edges, home, id) {
282
+ const tree = treeOf(nodes, edges, home ?? id);
283
+ const out = new Set([id]);
284
+ if (!tree.depth.has(id)) {
285
+ for (const e of edges) {
286
+ if (e.from === id)
287
+ out.add(e.to);
288
+ if (e.to === id)
289
+ out.add(e.from);
290
+ }
291
+ return out;
292
+ }
293
+ for (let up = tree.parent.get(id); up !== undefined; up = tree.parent.get(up))
294
+ out.add(up);
295
+ const stack = [id];
296
+ while (stack.length) {
297
+ for (const c of tree.children.get(stack.pop()) ?? []) {
298
+ out.add(c);
299
+ stack.push(c);
300
+ }
301
+ }
302
+ return out;
303
+ }
304
+ const unit = (p) => {
305
+ const k = Math.hypot(p.x, p.y, p.z) || 1;
306
+ return { x: p.x / k, y: p.y / k, z: p.z / k };
307
+ };
308
+ const PHI = (1 + Math.sqrt(5)) / 2;
309
+ const GOLDEN = Math.PI * (3 - Math.sqrt(5));
310
+ /**
311
+ * `count` directions spread as evenly as can be: the corners of a regular shape where one exists (2: a line, 3: a
312
+ * triangle, 4: a tetrahedron, 6: an octahedron, 8: a cube, 12: an icosahedron, 20: a dodecahedron); otherwise a
313
+ * Fibonacci sphere eased apart by a fixed number of pushes. The same every time.
314
+ */
315
+ export function evenDirections(count) {
316
+ const v = (x, y, z) => unit({ x, y, z });
317
+ const s = [-1, 1];
318
+ if (count <= 0)
319
+ return [];
320
+ if (count === 1)
321
+ return [v(0, -1, 0)];
322
+ if (count === 2)
323
+ return [v(-1, 0, 0), v(1, 0, 0)];
324
+ if (count === 3)
325
+ return [0, 1, 2].map((k) => v(Math.cos(-Math.PI / 2 + (k * 2 * Math.PI) / 3), Math.sin(-Math.PI / 2 + (k * 2 * Math.PI) / 3), 0));
326
+ if (count === 4)
327
+ return [v(1, 1, 1), v(1, -1, -1), v(-1, 1, -1), v(-1, -1, 1)];
328
+ if (count === 6)
329
+ return [v(1, 0, 0), v(-1, 0, 0), v(0, 1, 0), v(0, -1, 0), v(0, 0, 1), v(0, 0, -1)];
330
+ const cube = s.flatMap((x) => s.flatMap((y) => s.map((z) => v(x, y, z))));
331
+ if (count === 8)
332
+ return cube;
333
+ if (count === 12)
334
+ return s.flatMap((a) => s.flatMap((b) => [v(0, a, b * PHI), v(a, b * PHI, 0), v(a * PHI, 0, b)]));
335
+ if (count === 20)
336
+ return [
337
+ ...cube,
338
+ ...s.flatMap((a) => s.flatMap((b) => [
339
+ v(0, a / PHI, b * PHI),
340
+ v(a / PHI, b * PHI, 0),
341
+ v(a * PHI, 0, b / PHI),
342
+ ])),
343
+ ];
344
+ const p = Array.from({ length: count }, (_, i) => {
345
+ const y = 1 - (2 * (i + 0.5)) / count;
346
+ const r = Math.sqrt(1 - y * y);
347
+ return v(Math.cos(i * GOLDEN) * r, y, Math.sin(i * GOLDEN) * r);
348
+ });
349
+ // Pushed apart on the sphere: each pulls away from all the others by 1/d², a step that shrinks as it settles.
350
+ // At most 200 rounds, fewer for a big count, so the pushes cost about the same however many there are.
351
+ const rounds = Math.max(20, Math.min(200, Math.floor(8_000_000 / (count * count))));
352
+ for (let round = 0; round < rounds; round++) {
353
+ const step = 0.12 / Math.sqrt(count) / (1 + round / 40);
354
+ const moves = p.map((a, i) => {
355
+ const m = { x: 0, y: 0, z: 0 };
356
+ p.forEach((b, j) => {
357
+ if (i === j)
358
+ return;
359
+ const d = { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
360
+ const d2 = Math.max(1e-6, d.x * d.x + d.y * d.y + d.z * d.z);
361
+ const k = 1 / (d2 * Math.sqrt(d2));
362
+ m.x += d.x * k;
363
+ m.y += d.y * k;
364
+ m.z += d.z * k;
365
+ });
366
+ return m;
367
+ });
368
+ p.forEach((a, i) => {
369
+ const m = moves[i];
370
+ const k = Math.hypot(m.x, m.y, m.z) || 1;
371
+ p[i] = unit({
372
+ x: a.x + (m.x / k) * step,
373
+ y: a.y + (m.y / k) * step,
374
+ z: a.z + (m.z / k) * step,
375
+ });
376
+ });
377
+ }
378
+ return p;
379
+ }
380
+ // Two directions square to `a` and to each other, chosen the same way every time.
381
+ function frameOf(a) {
382
+ const t = Math.abs(a.y) < 0.9 ? { x: 0, y: 1, z: 0 } : { x: 1, y: 0, z: 0 };
383
+ const e1 = unit({
384
+ x: a.y * t.z - a.z * t.y,
385
+ y: a.z * t.x - a.x * t.z,
386
+ z: a.x * t.y - a.y * t.x,
387
+ });
388
+ const e2 = {
389
+ x: a.y * e1.z - a.z * e1.y,
390
+ y: a.z * e1.x - a.x * e1.z,
391
+ z: a.x * e1.y - a.y * e1.x,
392
+ };
393
+ return [e1, e2];
394
+ }
395
+ const tilt = (a, e1, e2, polar, turn) => {
396
+ const [c, s] = [Math.cos(polar), Math.sin(polar)];
397
+ const [ct, st] = [Math.cos(turn), Math.sin(turn)];
398
+ return unit({
399
+ x: a.x * c + (e1.x * ct + e2.x * st) * s,
400
+ y: a.y * c + (e1.y * ct + e2.y * st) * s,
401
+ z: a.z * c + (e1.z * ct + e2.z * st) * s,
402
+ });
403
+ };
404
+ /** The box the burst is laid out for: its first view is judged as it would fall in this box (2026-12-05). */
405
+ export const BURST_VIEW = { width: 1180, height: 760 };
406
+ // A pill, as node-map-3d.tsx draws it: 24px high, 10px either side of its name, its name at most 40 characters; and
407
+ // about how wide a character of its name is (semibold 12px, a little over, so a guess never undersizes it).
408
+ const PILL_HALF = 12;
409
+ const PILL_PAD = 10;
410
+ const PILL_CHARS = 40;
411
+ const PILL_CHAR = 7;
412
+ /** How much a big map shrinks every bead: the same as node-map-3d.tsx's marks. */
413
+ export const crowdOf = (count) => Math.min(1, Math.max(0.6, Math.sqrt(50 / Math.max(1, count))));
414
+ /** A pill's half width and half height in CSS pixels, for its name and the scale depth gives it. */
415
+ export const pillHalf = (label, scale = 1) => {
416
+ const hh = PILL_HALF * scale;
417
+ return {
418
+ hw: Math.max(hh, ((Math.min(PILL_CHARS, label.length) * PILL_CHAR) / 2 + PILL_PAD) * scale),
419
+ hh,
420
+ };
421
+ };
422
+ /**
423
+ * The first view of a map, as node-map-3d.tsx opens it: everything fitted, then turned round `home`, standing back as
424
+ * far again as home is from the map's middle; a burst (`ball`) instead fits the ball round its home.
425
+ */
426
+ export function firstView(points, home, ball = false) {
427
+ const all = [...points];
428
+ const c = fitted(all);
429
+ if (!home)
430
+ return c;
431
+ if (ball) {
432
+ const r = Math.max(1, ...all.map((p) => Math.hypot(p.x - home.x, p.y - home.y, p.z - home.z)));
433
+ return { ...c, target: home, dist: r * FIT };
434
+ }
435
+ const off = Math.hypot(home.x - c.target.x, home.y - c.target.y, home.z - c.target.z);
436
+ return { ...c, target: home, dist: c.dist + off };
437
+ }
438
+ // Turned about `axis` by `angle` (Rodrigues).
439
+ const rotated = (p, axis, angle) => {
440
+ const [c, s] = [Math.cos(angle), Math.sin(angle)];
441
+ const d = p.x * axis.x + p.y * axis.y + p.z * axis.z;
442
+ const cx = axis.y * p.z - axis.z * p.y;
443
+ const cy = axis.z * p.x - axis.x * p.z;
444
+ const cz = axis.x * p.y - axis.y * p.x;
445
+ return {
446
+ x: p.x * c + cx * s + axis.x * d * (1 - c),
447
+ y: p.y * c + cy * s + axis.y * d * (1 - c),
448
+ z: p.z * c + cz * s + axis.z * d * (1 - c),
449
+ };
450
+ };
451
+ const overlapOf = (a, b) => Math.max(0, Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0)) *
452
+ Math.max(0, Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0));
453
+ const gapOf = (a, b) => Math.max(Math.max(a.x0, b.x0) - Math.min(a.x1, b.x1), Math.max(a.y0, b.y0) - Math.min(a.y1, b.y1));
454
+ /**
455
+ * The burst layout (s_map_even, s_map_outward), laid out for how it first looks in `view`: home in the middle; its
456
+ * direct children (the topics) at equal distances from it and from each other, as the corners of `evenDirections`,
457
+ * the shape turned so their pills stand apart at the first view; each topic's subtopic pills on a shell outside, in its
458
+ * cone, each put where it covers no pill already placed; everything deeper on thin shells further out, each a bead's
459
+ * width or two past the last (and staggered by its id), spread over its parent's cone (a chain bends a little each
460
+ * step), so the map fills its sphere instead of running out in spokes. Every node is farther from home than its parent.
461
+ * Nodes home cannot reach sit evenly on a shell outside everything. No simulation: the same graph, the same places.
462
+ */
463
+ export function burstOf(nodes, edges, home, length = 100, view = BURST_VIEW) {
464
+ const out = new Map();
465
+ const tree = treeOf(nodes, edges, home);
466
+ if (!tree.depth.has(home))
467
+ return out;
468
+ const byId = new Map(nodes.map((n) => [n.id, n]));
469
+ const isPill = (id) => byId.get(id)?.shape === 'pill';
470
+ const crowd = crowdOf(nodes.length);
471
+ const bead = (id) => (byId.get(id)?.r ?? 10) * crowd;
472
+ const f = focalOf(view.width, view.height);
473
+ const levels = [];
474
+ for (const [id, d] of tree.depth)
475
+ (levels[d] ??= []).push(id);
476
+ const deepest = levels.length - 1;
477
+ // How many nodes hang from each (itself included).
478
+ const weight = new Map();
479
+ for (let d = deepest; d >= 0; d--)
480
+ for (const id of levels[d])
481
+ weight.set(id, 1 + (tree.children.get(id) ?? []).reduce((s, c) => s + weight.get(c), 0));
482
+ const byWeight = (ids) => [...ids].sort((a, b) => weight.get(b) - weight.get(a) || ids.indexOf(a) - ids.indexOf(b));
483
+ // Laid out in pixels as the first view shows the target's depth: the map's reach is then about f / FIT.
484
+ const reach = (f / FIT) * 0.9;
485
+ // The statements' shells: a bead and a little apart, from the subtopics' shell out to the reach.
486
+ const deep = [];
487
+ for (let d = 3; d <= deepest; d++)
488
+ for (const id of levels[d])
489
+ deep.push(bead(id));
490
+ const step = Math.max(8, 2 * (deep.length ? deep.reduce((s, b) => s + b, 0) / deep.length : 6));
491
+ const STAGGER = 0.45;
492
+ const gap = Math.max(step * 1.2, 2 * PILL_HALF * 1.1);
493
+ const outer = reach - STAGGER * step;
494
+ const R3 = deepest >= 3 ? outer - (deepest - 3) * step : outer;
495
+ const R2 = deepest >= 3 ? R3 - gap : outer;
496
+ const topics = tree.children.get(home) ?? [];
497
+ const topicCone = Math.min(Math.PI / 2, Math.acos(Math.max(-1, 1 - 2 / Math.max(1, topics.length)))) * 0.9;
498
+ const placeAll = (dist) => {
499
+ const cam = { yaw: 0.5, pitch: -0.25, dist, target: { x: 0, y: 0, z: 0 } };
500
+ const boxOf = (id, p, pad = 2) => {
501
+ const v = seen(p, cam, f, 0, 0);
502
+ if (isPill(id)) {
503
+ const { hw, hh } = pillHalf(byId.get(id)?.label ?? '', Math.min(1.3, Math.max(0.75, v.s)));
504
+ return {
505
+ x0: v.x - hw - pad,
506
+ x1: v.x + hw + pad,
507
+ y0: v.y - hh - pad,
508
+ y1: v.y + hh + pad,
509
+ };
510
+ }
511
+ const r = bead(id) * Math.min(1.8, Math.max(0.45, v.s));
512
+ return { x0: v.x - r, x1: v.x + r, y0: v.y - r, y1: v.y + r };
513
+ };
514
+ const at = new Map([[home, { x: 0, y: 0, z: 0 }]]);
515
+ const dir = new Map();
516
+ const cone = new Map();
517
+ const placed = [boxOf(home, at.get(home))];
518
+ const put = (id, u, r) => {
519
+ dir.set(id, u);
520
+ at.set(id, { x: u.x * r, y: u.y * r, z: u.z * r });
521
+ };
522
+ // Topics: the regular shape, turned (of a fixed set of turns) so that its pills stand farthest apart; then the
523
+ // nearest shell that keeps them apart.
524
+ const corners = evenDirections(topics.length);
525
+ const R1For = (r, turnedCorners) => {
526
+ const boxes = topics.map((t, i) => boxOf(t, {
527
+ x: turnedCorners[i].x * r,
528
+ y: turnedCorners[i].y * r,
529
+ z: turnedCorners[i].z * r,
530
+ }));
531
+ let worst = Infinity;
532
+ boxes.forEach((a, i) => {
533
+ worst = Math.min(worst, gapOf(a, placed[0]));
534
+ for (let j = i + 1; j < boxes.length; j++)
535
+ worst = Math.min(worst, gapOf(a, boxes[j]));
536
+ });
537
+ return worst;
538
+ };
539
+ let best = corners;
540
+ let bestGap = -Infinity;
541
+ const trialR = R2 * 0.5;
542
+ const axes = evenDirections(24);
543
+ for (const axis of [{ x: 0, y: 1, z: 0 }, ...axes]) {
544
+ for (let k = 0; k < 12; k++) {
545
+ const turnedCorners = corners.map((c) => rotated(c, axis, (k * Math.PI) / 12));
546
+ const g = R1For(trialR, turnedCorners);
547
+ if (g > bestGap + 1e-9) {
548
+ bestGap = g;
549
+ best = turnedCorners;
550
+ }
551
+ }
552
+ }
553
+ let R1 = R2 * 0.35;
554
+ for (let k = 0; k <= 10; k++) {
555
+ R1 = R2 * (0.35 + k * 0.04);
556
+ if (R1For(R1, best) >= 4)
557
+ break;
558
+ }
559
+ topics.forEach((t, i) => {
560
+ put(t, best[i], R1);
561
+ cone.set(t, topicCone);
562
+ placed.push(boxOf(t, at.get(t)));
563
+ });
564
+ // Subtopic pills: each where it covers the least of the pills placed so far, near its topic when there is a
565
+ // choice; then the rest of each topic's children, as everything deeper is placed.
566
+ // The topics with the most subtopic pills first, while there is the most room.
567
+ const pillsOf = (t) => (tree.children.get(t) ?? []).filter(isPill).length;
568
+ for (const t of [...topics].sort((x, y) => pillsOf(y) - pillsOf(x) || topics.indexOf(x) - topics.indexOf(y))) {
569
+ const a = dir.get(t);
570
+ const [e1, e2] = frameOf(a);
571
+ const spin = hashOf(t) * 2 * Math.PI;
572
+ const kids = byWeight(tree.children.get(t) ?? []);
573
+ const total = kids.reduce((s, k) => s + weight.get(k), 0) || 1;
574
+ for (const k of kids.filter(isPill)) {
575
+ let pick = null;
576
+ let pickCost = Infinity;
577
+ // Within its topic's cone first; a little past it only when that is the only room.
578
+ for (let pi = 1; pi <= 11; pi++) {
579
+ const polar = topicCone * (0.1 + pi * 0.11);
580
+ for (let ai = 0; ai < 24; ai++) {
581
+ const u = tilt(a, e1, e2, polar, spin + (ai * 2 * Math.PI) / 24);
582
+ for (const r of [
583
+ R2,
584
+ R2 * 0.95,
585
+ R2 * 0.9,
586
+ R2 * 0.85,
587
+ Math.max(R2 * 0.79, R1 * 1.3),
588
+ ]) {
589
+ const box = boxOf(k, { x: u.x * r, y: u.y * r, z: u.z * r });
590
+ let cost = 0;
591
+ for (const o of placed)
592
+ cost += overlapOf(box, o);
593
+ cost = cost * 1000 + pi + (pi > 8 ? 50 : 0);
594
+ if (cost < pickCost) {
595
+ pickCost = cost;
596
+ pick = { u, r, box };
597
+ }
598
+ }
599
+ }
600
+ }
601
+ put(k, pick.u, pick.r);
602
+ placed.push(pick.box);
603
+ cone.set(k, topicCone * Math.min(0.7, Math.max(0.3, Math.sqrt(weight.get(k) / total))));
604
+ }
605
+ }
606
+ // Beads already placed, on a grid of the screen, so each next one can look for room among its neighbours alone.
607
+ const CELL = 32;
608
+ const grid = new Map();
609
+ const cellOf = (x, y) => (Math.floor(x / CELL) + 4096) * 8192 + Math.floor(y / CELL) + 4096;
610
+ const crowdAt = (x, y, r) => {
611
+ let cost = 0;
612
+ for (let gx = Math.floor((x - r - 24) / CELL); gx <= Math.floor((x + r + 24) / CELL); gx++)
613
+ for (let gy = Math.floor((y - r - 24) / CELL); gy <= Math.floor((y + r + 24) / CELL); gy++)
614
+ for (const o of grid.get((gx + 4096) * 8192 + gy + 4096) ?? []) {
615
+ const into = o.r + r + 2 - Math.hypot(o.x - x, o.y - y);
616
+ if (into > 0)
617
+ cost += into * into;
618
+ }
619
+ return cost;
620
+ };
621
+ const addBead = (id, p) => {
622
+ const v = seen(p, cam, f, 0, 0);
623
+ const r = bead(id) * Math.min(1.8, Math.max(0.45, v.s));
624
+ const key = cellOf(v.x, v.y);
625
+ (grid.get(key) ?? grid.set(key, []).get(key)).push({
626
+ x: v.x,
627
+ y: v.y,
628
+ r,
629
+ depth: v.depth,
630
+ });
631
+ };
632
+ // A bead's cost at a place: how far it runs into beads already placed, and over a pill.
633
+ const beadCost = (id, p) => {
634
+ const v = seen(p, cam, f, 0, 0);
635
+ const r = bead(id) * Math.min(1.8, Math.max(0.45, v.s));
636
+ let cost = crowdAt(v.x, v.y, r);
637
+ const box = { x0: v.x - r, x1: v.x + r, y0: v.y - r, y1: v.y + r };
638
+ for (const o of placed)
639
+ cost += overlapOf(box, o) * 4;
640
+ return cost;
641
+ };
642
+ addBead(home, at.get(home));
643
+ // Everything else, breadth first: spread over its parent's cone (a ring for a few, a sunflower for many; one
644
+ // alone bends a little off its parent's way), on its depth's shell; then each bead moved, within its own cone
645
+ // and its shell's stagger, to where it covers and is covered least at the first view.
646
+ const radiusOf = (id, d) => d === 1
647
+ ? R1
648
+ : d === 2
649
+ ? R2 * (0.9 + 0.1 * hashOf(id))
650
+ : R3 + (d - 3 + STAGGER * hashOf(id + '~')) * step;
651
+ const settle = (id, d, u, own) => {
652
+ if (isPill(id))
653
+ return put(id, u, radiusOf(id, d));
654
+ const [e1, e2] = frameOf(u);
655
+ const radii = d >= 3
656
+ ? [
657
+ R3 + (d - 3) * step,
658
+ R3 + (d - 3 + STAGGER * 0.5) * step,
659
+ R3 + (d - 3 + STAGGER) * step,
660
+ ]
661
+ : [radiusOf(id, d)];
662
+ let bestU = u;
663
+ let bestR = radii[0];
664
+ let bestCost = Infinity;
665
+ const spin = hashOf(id) * 2 * Math.PI;
666
+ for (const r of radii) {
667
+ for (let ring = 0; ring <= 3; ring++) {
668
+ const ways = ring === 0 ? 1 : 6 + 2 * ring;
669
+ for (let w = 0; w < ways; w++) {
670
+ const c = ring === 0
671
+ ? u
672
+ : tilt(u, e1, e2, own * 0.4 * ring, spin + (w * 2 * Math.PI) / ways);
673
+ const cost = beadCost(id, { x: c.x * r, y: c.y * r, z: c.z * r }) + ring * 0.01;
674
+ if (cost < bestCost) {
675
+ bestCost = cost;
676
+ bestU = c;
677
+ bestR = r;
678
+ }
679
+ }
680
+ }
681
+ if (bestCost < 0.02)
682
+ break;
683
+ }
684
+ put(id, bestU, bestR);
685
+ addBead(id, at.get(id));
686
+ };
687
+ for (let d = 0; d < deepest; d++) {
688
+ for (const id of levels[d]) {
689
+ const kids = byWeight((tree.children.get(id) ?? []).filter((k) => !at.has(k)));
690
+ if (!kids.length)
691
+ continue;
692
+ const a = dir.get(id) ?? { x: 0, y: -1, z: 0 };
693
+ const alpha = cone.get(id) ?? topicCone;
694
+ const [e1, e2] = frameOf(a);
695
+ const spin = hashOf(id) * 2 * Math.PI;
696
+ const n = kids.length;
697
+ kids.forEach((k, i) => {
698
+ const r = radiusOf(k, d + 1);
699
+ // Never narrower than a few beads' width at its own shell, so a crowd does not stack on one ray.
700
+ const least = (3 * bead(k)) / Math.max(r, 1);
701
+ if (n === 1) {
702
+ cone.set(k, Math.max(alpha * 0.75, least));
703
+ settle(k, d + 1, tilt(a, e1, e2, Math.max(0.35 * alpha, least), spin), cone.get(k));
704
+ return;
705
+ }
706
+ const own = Math.max((0.9 * alpha) / Math.sqrt(n), least);
707
+ const most = Math.max(alpha - own * 0.5, alpha * 0.6, least * Math.sqrt(n));
708
+ const polar = n <= 5 ? most * 0.75 : most * Math.sqrt((i + 0.5) / n);
709
+ const turn = n <= 5 ? spin + (i * 2 * Math.PI) / n : spin + i * GOLDEN;
710
+ cone.set(k, own);
711
+ settle(k, d + 1, tilt(a, e1, e2, polar, turn), own);
712
+ });
713
+ }
714
+ }
715
+ return at;
716
+ };
717
+ // Laid out for its first view, which fits the ball of `reach` round home.
718
+ const at = placeAll(FIT * reach);
719
+ const k = length / 100;
720
+ for (const [id, p] of at)
721
+ out.set(id, { x: p.x * k, y: p.y * k, z: p.z * k });
722
+ const lost = nodes.filter((n) => !tree.depth.has(n.id));
723
+ if (lost.length) {
724
+ const r = (reach + 2 * step) * k;
725
+ evenDirections(lost.length).forEach((u, i) => out.set(lost[i].id, { x: u.x * r, y: u.y * r, z: u.z * r }));
726
+ }
727
+ return out;
728
+ }
729
+ /** The median length of the consumer's own flat ties, or 100: the unit both layouts are measured in. */
730
+ export function tieLength(nodes, edges) {
731
+ const at = new Map(nodes.map((n) => [n.id, n]));
732
+ const given = edges
733
+ .map((e) => [at.get(e.from), at.get(e.to)])
734
+ .filter(([a, b]) => a && b && a !== b)
735
+ .map(([a, b]) => Math.hypot(a.x - b.x, a.y - b.y))
736
+ .sort((a, b) => a - b);
737
+ return given.length ? given[Math.floor(given.length / 2)] || 100 : 100;
738
+ }
739
+ /** Where every node sits in depth: the burst from `home` when asked for and there is a home, else `spaceOf`. */
740
+ export function layoutOf(nodes, edges, layout = 'force', home) {
741
+ if (layout === 'burst' && home && nodes.some((n) => n.id === home))
742
+ return burstOf(nodes, edges, home, tieLength(nodes, edges));
743
+ return spaceOf(nodes, edges);
744
+ }
745
+ /**
746
+ * Where each pill stands on the screen this frame (s_map_pills, 2026-12-05): pills are drawn over everything, so none
747
+ * may stand on another. Topics take their places first, then the nearer pills; a pill that would stand on one already
748
+ * placed moves up or down by its own height, then aside by half its width, until it is free (at most four steps
749
+ * away), and failing that tries the same a fifth smaller (`shrink`); one that finds no room keeps its place and is
750
+ * `faded`. About pills² × 25 box tests, for a few dozen pills.
751
+ */
752
+ export function placePills(pills) {
753
+ const out = new Map();
754
+ const ranked = [...pills].sort((a, b) => Number(b.topic) - Number(a.topic) || a.depth - b.depth);
755
+ const taken = [];
756
+ const ways = [[0, 0]];
757
+ for (let k = 1; k <= 4; k++)
758
+ ways.push([0, -k], [0, k]);
759
+ for (const sx of [-1, 1])
760
+ for (let k = 0; k <= 2; k++)
761
+ ways.push([sx, k === 0 ? 0 : k % 2 ? -1 : 1]);
762
+ for (const p of ranked) {
763
+ let place = null;
764
+ // Its own size first; then a fifth smaller, before it is faded.
765
+ for (const shrink of [1, 0.8]) {
766
+ const [hw, hh] = [p.hw * shrink, p.hh * shrink];
767
+ for (const [wx, wy] of ways) {
768
+ const x = p.x + wx * hw;
769
+ const y = p.y + wy * (2 * hh + 3);
770
+ const b = { x0: x - hw - 2, x1: x + hw + 2, y0: y - hh - 2, y1: y + hh + 2 };
771
+ if (!taken.some((o) => overlapOf(b, o) > 0)) {
772
+ place = { x, y, shrink };
773
+ taken.push(b);
774
+ break;
775
+ }
776
+ }
777
+ if (place)
778
+ break;
779
+ }
780
+ out.set(p.id, {
781
+ x: place?.x ?? p.x,
782
+ y: place?.y ?? p.y,
783
+ faded: !place,
784
+ shrink: place?.shrink ?? 1,
785
+ });
786
+ }
787
+ return out;
788
+ }
239
789
  //# sourceMappingURL=space.js.map