File size: 5,980 Bytes
2471af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { forceCollide, forceSimulation, type SimulationNodeDatum } from "d3-force"
import type { Rel, Repo } from "./types"

export type GNode = SimulationNodeDatum & {
  id: string
  rel: Rel | "official"
  dlall: number
  depth: number
  ring: number
  r: number
  sub: number
  parent: GNode | null
  kids: GNode[]
  repo: Repo | null
  x: number
  y: number
}

export type Ring = { ring: number; inner: number; outer: number; count: number }

export type Galaxy = { root: GNode; nodes: GNode[]; rings: Ring[]; extent: number }

const R_CENTER = 64
const R_MIN = 2.6
const R_MAX = 44
const PAD = 1.6
const PACK = 0.5 // share of a band's area the bubbles may fill
const FIRST_GAP = 30
const RING_GAP = 34
const MAX_RING = 3 // generation 3 and beyond share the outer ring
const GOLDEN = Math.PI * (3 - Math.sqrt(5))

/** Deterministic 0..1 from a string, so the picture is the same on every load. */
function hash(s: string): number {
  let h = 2166136261
  for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619)
  return ((h >>> 0) % 100000) / 100000
}

/**
 * The official model sits in the middle; every derived model sits on the ring of its generation
 * (built directly on the official model, on a derivative, or further out), near the model it is
 * built on. Bubble size grows with all-time downloads on a log scale: downloads span 0 to ~10^8,
 * and an area-true scale would leave one giant bubble and a thousand dots.
 */
export function layoutGalaxy(repos: Repo[], base: string, officialDl: number): Galaxy {
  const root: GNode = {
    id: base, rel: "official", dlall: officialDl, depth: 0, ring: 0, r: R_CENTER, sub: 0,
    parent: null, kids: [], repo: null, x: 0, y: 0, fx: 0, fy: 0,
  }
  const byId = new Map<string, GNode>([[base, root]])
  for (const repo of repos) {
    byId.set(repo.id, {
      id: repo.id, rel: repo.rel, dlall: repo.dlall ?? 0, depth: 0, ring: 0, r: R_MIN, sub: 0,
      parent: null, kids: [], repo, x: 0, y: 0,
    })
  }
  const nodes = [...byId.values()].filter((n) => n !== root)
  for (const n of nodes) {
    n.parent = byId.get(n.repo!.up) ?? root
    n.parent.kids.push(n)
  }
  // depth by walking up (the data has no cycles; the guard keeps a bad file from hanging the page)
  for (const n of nodes) {
    let d = 0
    for (let p: GNode | null = n; p && p !== root && d < 50; p = p.parent) d++
    n.depth = d
    n.ring = Math.min(d, MAX_RING)
  }
  const subOf = (n: GNode): number => (n.sub = n.dlall + n.kids.reduce((a, k) => a + subOf(k), 0))
  subOf(root)

  const maxDl = Math.max(1, ...nodes.map((n) => n.dlall))
  const lmax = Math.log10(1 + maxDl)
  for (const n of nodes) n.r = R_MIN + (R_MAX - R_MIN) * Math.pow(Math.log10(1 + n.dlall) / lmax, 1.7)

  // Each ring is as thick as its bubbles need, and at least as thick as its largest bubble.
  const rings: Ring[] = []
  let inner = R_CENTER + FIRST_GAP
  for (let k = 1; k <= MAX_RING; k++) {
    const inRing = nodes.filter((n) => n.ring === k)
    if (!inRing.length) break
    const area = inRing.reduce((a, n) => a + Math.PI * (n.r + PAD) ** 2, 0) / PACK
    const biggest = Math.max(...inRing.map((n) => n.r))
    const outer = Math.max(Math.sqrt(inner * inner + area / Math.PI), inner + 2 * biggest + 10)
    rings.push({ ring: k, inner, outer, count: inRing.length })
    inner = outer + RING_GAP
  }
  const ringOf = (n: GNode) => rings[n.ring - 1]

  // Start positions: generation one spread by the golden angle (big ones first, so they don't
  // bunch up); everything else starts at its parent's angle, fanned out a little.
  const angle = new Map<GNode, number>()
  const first = nodes.filter((n) => n.ring === 1).sort((a, b) => b.sub - a.sub)
  first.forEach((n, i) => angle.set(n, i * GOLDEN))
  const queue = [...first]
  while (queue.length) {
    const p = queue.shift()!
    const kids = [...p.kids].sort((a, b) => b.sub - a.sub)
    kids.forEach((k, j) => {
      const side = j % 2 ? -1 : 1
      angle.set(k, angle.get(p)! + side * Math.ceil(j / 2) * 0.03)
      queue.push(k)
    })
  }
  for (const n of nodes) {
    const g = ringOf(n)
    const rad = g.inner + n.r + (g.outer - g.inner - 2 * n.r) * hash(n.id)
    const a = angle.get(n) ?? hash(n.id + "a") * 2 * Math.PI
    n.x = Math.cos(a) * rad
    n.y = Math.sin(a) * rad
  }

  const all = [root, ...nodes]
  const sim = forceSimulation<GNode>(all)
    .force("collide", forceCollide<GNode>((n) => n.r + PAD).strength(0.9).iterations(2))
    .force("band", bandForce(ringOf, 0.35))
    .force("kin", kinForce(0.06))
    .stop()
  for (let i = 0; i < 260; i++) sim.tick()

  const extent = Math.max(...all.map((n) => Math.hypot(n.x, n.y) + n.r), rings.at(-1)?.outer ?? 0)
  return { root, nodes, rings, extent }
}

/** Keeps each bubble inside its generation's ring. */
function bandForce(ringOf: (n: GNode) => Ring, strength: number) {
  let ns: GNode[] = []
  const f = () => {
    for (const n of ns) {
      if (n.depth === 0) continue
      const g = ringOf(n)
      const d = Math.hypot(n.x, n.y) || 1
      const lo = g.inner + n.r
      const hi = Math.max(lo, g.outer - n.r)
      const target = d < lo ? lo : d > hi ? hi : d
      if (target === d) continue
      const k = ((target - d) / d) * strength
      n.vx = (n.vx ?? 0) + n.x * k
      n.vy = (n.vy ?? 0) + n.y * k
    }
  }
  f.initialize = (n: GNode[]) => (ns = n)
  return f
}

/** Pulls a model round the circle towards the model it is built on. */
function kinForce(strength: number) {
  let ns: GNode[] = []
  const f = (alpha: number) => {
    for (const n of ns) {
      const p = n.parent
      if (!p || p.depth === 0) continue
      const a = Math.atan2(n.y, n.x)
      const d = Math.atan2(Math.sin(Math.atan2(p.y, p.x) - a), Math.cos(Math.atan2(p.y, p.x) - a))
      const k = d * Math.hypot(n.x, n.y) * strength * alpha
      n.vx = (n.vx ?? 0) - Math.sin(a) * k
      n.vy = (n.vy ?? 0) + Math.cos(a) * k
    }
  }
  f.initialize = (n: GNode[]) => (ns = n)
  return f
}