Repository navigation
Expand file tree
/
Copy pathgraph-model.js
More file actions
58 lines (53 loc) · 1.81 KB
/
Copy pathgraph-model.js
File metadata and controls
58 lines (53 loc) · 1.81 KB
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
export function createGraphModel(raw) {
// ---------- graph model ----------
const zones = raw.zones.map((z, i) => ({ ...z, index: i }));
const nodes = raw.nodes.map((n, i) => ({
...n,
index: i,
r: 2.5 + Math.min(12, Math.sqrt(n.in + n.out) * 0.55),
x: 0,
y: 0,
vx: 0,
vy: 0,
fx: null,
fy: null,
entry: null,
slot: null,
}));
const pairs = raw.edges.map(([a, b, kind, flags]) => ({ a, b, kind, flags, display: -1 }));
const nodePairs = nodes.map(() => []);
pairs.forEach((p, i) => {
nodePairs[p.a].push(i);
if (p.b !== p.a) nodePairs[p.b].push(i);
});
return { zones, nodes, pairs, nodePairs };
}
export function refreshPairKinds(pairs, enabledKinds, hideReachable) {
for (const pair of pairs) {
const hidden = hideReachable && pair.kind === 0 && (pair.flags & 2) !== 0;
pair.display = enabledKinds.has(pair.kind) && !hidden ? pair.kind : -1;
}
}
export function visibleCycleFocus(cycles, pairs, nodes, zoneVisible) {
const pairByDirection = new Map(pairs.map((pair, index) => [`${pair.a}:${pair.b}`, index]));
const focusNodes = new Set();
const focusPairs = new Set();
for (const cycle of cycles) {
const pathPairs = [];
for (let i = 1; i < cycle.path.length; i++) {
const from = cycle.path[i - 1];
const to = cycle.path[i];
const index = pairByDirection.get(`${from}:${to}`);
if (index === undefined || !zoneVisible[nodes[from].z] || !zoneVisible[nodes[to].z]) break;
if (pairs[index].display < 0) break;
pathPairs.push(index);
}
if (pathPairs.length !== cycle.path.length - 1 || !pathPairs.length) continue;
for (const index of pathPairs) {
focusPairs.add(index);
focusNodes.add(pairs[index].a);
focusNodes.add(pairs[index].b);
}
}
return { focusNodes, focusPairs };
}