@diagrammo/dgmo 0.32.1 → 0.34.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.
- package/.cursorrules +7 -2
- package/.github/copilot-instructions.md +7 -2
- package/.windsurfrules +7 -2
- package/SKILL.md +7 -2
- package/dist/advanced.cjs +1434 -443
- package/dist/advanced.d.cts +41 -1
- package/dist/advanced.d.ts +41 -1
- package/dist/advanced.js +1433 -443
- package/dist/auto.cjs +1242 -318
- package/dist/auto.js +91 -91
- package/dist/auto.mjs +1242 -318
- package/dist/cli.cjs +138 -136
- package/dist/index.cjs +1308 -317
- package/dist/index.d.cts +35 -1
- package/dist/index.d.ts +35 -1
- package/dist/index.js +1307 -317
- package/docs/ai-integration.md +7 -2
- package/docs/language-reference.md +39 -8
- package/package.json +2 -2
- package/src/advanced.ts +6 -1
- package/src/boxes-and-lines/label-placement.ts +339 -0
- package/src/boxes-and-lines/layout-grouped.ts +909 -0
- package/src/boxes-and-lines/layout-search.ts +34 -3
- package/src/boxes-and-lines/layout.ts +32 -7
- package/src/boxes-and-lines/parser.ts +3 -2
- package/src/boxes-and-lines/renderer.ts +67 -139
- package/src/c4/layout.ts +2 -1
- package/src/c4/parser.ts +3 -2
- package/src/c4/renderer.ts +3 -2
- package/src/cli.ts +68 -0
- package/src/er/parser.ts +2 -1
- package/src/er/renderer.ts +2 -1
- package/src/gantt/parser.ts +3 -5
- package/src/gantt/renderer.ts +7 -6
- package/src/index.ts +6 -1
- package/src/infra/parser.ts +2 -1
- package/src/infra/renderer.ts +4 -3
- package/src/journey-map/parser.ts +27 -8
- package/src/journey-map/renderer.ts +26 -9
- package/src/kanban/parser.ts +4 -3
- package/src/kanban/renderer.ts +6 -5
- package/src/map/completion.ts +116 -0
- package/src/map/layout.ts +4 -3
- package/src/map/parser.ts +3 -2
- package/src/mindmap/parser.ts +4 -3
- package/src/mindmap/renderer.ts +2 -1
- package/src/org/layout.ts +3 -2
- package/src/org/parser.ts +3 -2
- package/src/org/renderer.ts +5 -4
- package/src/pert/parser.ts +3 -2
- package/src/sequence/renderer.ts +10 -12
- package/src/sequence/tag-resolution.ts +2 -1
- package/src/sitemap/layout.ts +3 -2
- package/src/sitemap/parser.ts +36 -2
- package/src/sitemap/renderer.ts +12 -6
- package/src/timeline/renderer.ts +4 -3
- package/src/utils/legend-d3.ts +4 -3
- package/src/utils/legend-svg.ts +3 -2
- package/src/utils/tag-groups.ts +28 -7
- package/src/visualizations/parse.ts +2 -1
|
@@ -0,0 +1,909 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// Boxes and Lines — tier-banded grouped layered candidate generator
|
|
3
|
+
// ============================================================
|
|
4
|
+
//
|
|
5
|
+
// The default dagre placement engine hands `[Group]` containers to dagre as
|
|
6
|
+
// compound clusters and lets dagre rank everything globally. Dagre keeps each
|
|
7
|
+
// cluster contiguous but has NO notion of tier ORDER between sibling groups and
|
|
8
|
+
// NO cycle-aware routing. Two pathologies follow for tiered architecture
|
|
9
|
+
// diagrams (Clients → Edge → Services → Data style):
|
|
10
|
+
//
|
|
11
|
+
// 1. Back-edge ballooning — an edge that runs UP-tier (a Services node → a
|
|
12
|
+
// Clients node) is shortened by dagre hoisting its source to the top of the
|
|
13
|
+
// group, ballooning the whole group and squashing its siblings.
|
|
14
|
+
// 2. Tier collision — a group with one member that is topologically DEEPER
|
|
15
|
+
// than its siblings (an intra-tier `A -> B` edge) forces that group's band
|
|
16
|
+
// to span two ranks; a sibling group at the deeper rank is then placed
|
|
17
|
+
// SIDE-BY-SIDE instead of below, and the cross edges spray horizontally.
|
|
18
|
+
//
|
|
19
|
+
// This generator fixes both by making groups first-class TIERS: derive a tier
|
|
20
|
+
// order from the inter-group flow, give each top-level group/unit a DISJOINT,
|
|
21
|
+
// ORDERED band of ranks, rank members locally inside their band, minimize
|
|
22
|
+
// crossings under a group-contiguity constraint, and route the few against-tier
|
|
23
|
+
// edges around the periphery (reusing the layered engine's loop routing).
|
|
24
|
+
//
|
|
25
|
+
// ADDITIVE, like layout-layered.ts: the caller throws these into the same
|
|
26
|
+
// placement-search pool and the spline-crossing scorer keeps a grouped candidate
|
|
27
|
+
// ONLY when it strictly lowers total badness. A worse candidate is never chosen,
|
|
28
|
+
// so there is no regression risk. Declines (returns []) on cases it does not yet
|
|
29
|
+
// model (group-endpoint edges, collapsed groups, out-of-range node counts).
|
|
30
|
+
|
|
31
|
+
import type { ParsedBoxesAndLines } from './types';
|
|
32
|
+
import {
|
|
33
|
+
NODE_WIDTH,
|
|
34
|
+
NODE_HEIGHT,
|
|
35
|
+
type BLLayoutResult,
|
|
36
|
+
type BLLayoutEdge,
|
|
37
|
+
type BLLayoutNode,
|
|
38
|
+
type BLLayoutGroup,
|
|
39
|
+
} from './layout';
|
|
40
|
+
|
|
41
|
+
type Size = { width: number; height: number };
|
|
42
|
+
type Pt = { x: number; y: number };
|
|
43
|
+
|
|
44
|
+
const NODESEP = 50; // cross-axis gap between siblings in a rank
|
|
45
|
+
const RANKSEP = 60; // rank-axis gap between adjacent ranks WITHIN a band
|
|
46
|
+
const DUMMY_THICK = 10; // cross-axis footprint of a long-edge dummy
|
|
47
|
+
const MARGIN = 40;
|
|
48
|
+
// Group-box padding + label-zone — MUST match renderer.ts / layout-search.ts so
|
|
49
|
+
// the rects this generator emits are scored against the same metric the dagre
|
|
50
|
+
// candidates are.
|
|
51
|
+
const GROUP_PAD = 16;
|
|
52
|
+
const GROUP_LABEL_ZONE = 32;
|
|
53
|
+
// Extra cross-axis gap between two adjacent top-level groups so their padded
|
|
54
|
+
// boxes (and the lower one's label zone) never collide.
|
|
55
|
+
const GROUP_GAP = 24;
|
|
56
|
+
// Extra rank-axis gap added when crossing a TIER boundary — clears the lower
|
|
57
|
+
// group's label zone plus both groups' vertical pad.
|
|
58
|
+
const TIER_GAP = GROUP_LABEL_ZONE + 2 * GROUP_PAD;
|
|
59
|
+
|
|
60
|
+
// Edge-straightening weights for coordinate assignment (dagre-style): keep long
|
|
61
|
+
// dummy chains ruler-straight so they hug one side instead of sweeping across.
|
|
62
|
+
const W_REAL_REAL = 1;
|
|
63
|
+
const W_REAL_DUMMY = 2;
|
|
64
|
+
const W_DUMMY_DUMMY = 8;
|
|
65
|
+
|
|
66
|
+
function rng(s: number) {
|
|
67
|
+
return () => {
|
|
68
|
+
s |= 0;
|
|
69
|
+
s = (s + 0x6d2b79f5) | 0;
|
|
70
|
+
let t = Math.imul(s ^ (s >>> 15), 1 | s);
|
|
71
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
72
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function median(vals: number[]): number {
|
|
77
|
+
if (vals.length === 0) return -1;
|
|
78
|
+
vals.sort((x, y) => x - y);
|
|
79
|
+
const m = Math.floor(vals.length / 2);
|
|
80
|
+
if (vals.length % 2 === 1) return vals[m]!;
|
|
81
|
+
if (vals.length === 2) return (vals[0]! + vals[1]!) / 2;
|
|
82
|
+
const left = vals[m - 1]! - vals[0]!;
|
|
83
|
+
const right = vals[vals.length - 1]! - vals[m]!;
|
|
84
|
+
return left + right === 0
|
|
85
|
+
? (vals[m - 1]! + vals[m]!) / 2
|
|
86
|
+
: (vals[m - 1]! * right + vals[m]! * left) / (left + right);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Break cycles by DFS (mark edges that close a cycle) and return each edge in a
|
|
90
|
+
// strict from→to (lower-rank → higher-rank) orientation plus the reversed set.
|
|
91
|
+
function acyclicOrient(
|
|
92
|
+
ids: readonly string[],
|
|
93
|
+
edges: readonly { from: string; to: string; idx: number }[]
|
|
94
|
+
): { dag: { from: string; to: string; idx: number; rev: boolean }[] } {
|
|
95
|
+
const adj = new Map<string, { to: string; idx: number }[]>();
|
|
96
|
+
for (const l of ids) adj.set(l, []);
|
|
97
|
+
for (const e of edges) adj.get(e.from)?.push({ to: e.to, idx: e.idx });
|
|
98
|
+
const reversed = new Set<number>();
|
|
99
|
+
const state = new Map<string, 0 | 1 | 2>();
|
|
100
|
+
for (const l of ids) state.set(l, 0);
|
|
101
|
+
const stack: string[] = [];
|
|
102
|
+
const visit = (root: string): void => {
|
|
103
|
+
// Iterative DFS (architecture diagrams can chain deep; avoid call-stack).
|
|
104
|
+
stack.push(root);
|
|
105
|
+
const it = new Map<string, number>();
|
|
106
|
+
while (stack.length) {
|
|
107
|
+
const u = stack[stack.length - 1]!;
|
|
108
|
+
if (state.get(u) === 0) state.set(u, 1);
|
|
109
|
+
const list = adj.get(u)!;
|
|
110
|
+
let i = it.get(u) ?? 0;
|
|
111
|
+
let descended = false;
|
|
112
|
+
for (; i < list.length; i++) {
|
|
113
|
+
const { to, idx } = list[i]!;
|
|
114
|
+
const st = state.get(to)!;
|
|
115
|
+
if (st === 1) reversed.add(idx);
|
|
116
|
+
else if (st === 0) {
|
|
117
|
+
it.set(u, i + 1);
|
|
118
|
+
stack.push(to);
|
|
119
|
+
descended = true;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (!descended) {
|
|
124
|
+
it.set(u, list.length);
|
|
125
|
+
state.set(u, 2);
|
|
126
|
+
stack.pop();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
for (const l of ids) if (state.get(l) === 0) visit(l);
|
|
131
|
+
const dag = edges.map((e) =>
|
|
132
|
+
reversed.has(e.idx)
|
|
133
|
+
? { from: e.to, to: e.from, idx: e.idx, rev: true }
|
|
134
|
+
: { from: e.from, to: e.to, idx: e.idx, rev: false }
|
|
135
|
+
);
|
|
136
|
+
return { dag };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Longest-path ranking over an acyclic edge set. Returns null if a cycle slips
|
|
140
|
+
// through (caller bails). Ranks start at 0.
|
|
141
|
+
function longestPath(
|
|
142
|
+
ids: readonly string[],
|
|
143
|
+
dag: readonly { from: string; to: string }[]
|
|
144
|
+
): Map<string, number> | null {
|
|
145
|
+
const out = new Map<string, string[]>();
|
|
146
|
+
const inDeg = new Map<string, number>();
|
|
147
|
+
for (const l of ids) {
|
|
148
|
+
out.set(l, []);
|
|
149
|
+
inDeg.set(l, 0);
|
|
150
|
+
}
|
|
151
|
+
for (const d of dag) {
|
|
152
|
+
out.get(d.from)!.push(d.to);
|
|
153
|
+
inDeg.set(d.to, (inDeg.get(d.to) ?? 0) + 1);
|
|
154
|
+
}
|
|
155
|
+
const rank = new Map<string, number>();
|
|
156
|
+
for (const l of ids) rank.set(l, 0);
|
|
157
|
+
const queue = ids.filter((l) => (inDeg.get(l) ?? 0) === 0);
|
|
158
|
+
const indeg2 = new Map(inDeg);
|
|
159
|
+
let seen = 0;
|
|
160
|
+
while (queue.length) {
|
|
161
|
+
const u = queue.shift()!;
|
|
162
|
+
seen++;
|
|
163
|
+
for (const v of out.get(u)!) {
|
|
164
|
+
rank.set(v, Math.max(rank.get(v)!, rank.get(u)! + 1));
|
|
165
|
+
indeg2.set(v, indeg2.get(v)! - 1);
|
|
166
|
+
if (indeg2.get(v) === 0) queue.push(v);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return seen === ids.length ? rank : null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface LNode {
|
|
173
|
+
id: string;
|
|
174
|
+
dummy: boolean;
|
|
175
|
+
rank: number; // GLOBAL rank (tier band offset + local rank)
|
|
176
|
+
cross: number;
|
|
177
|
+
thick: number; // cross-axis footprint
|
|
178
|
+
depth: number; // rank-axis footprint
|
|
179
|
+
realW: number;
|
|
180
|
+
realH: number;
|
|
181
|
+
unit: string; // top-level unit key (group label or synthetic singleton)
|
|
182
|
+
/** Group path top→direct (group labels) for contiguity ordering. */
|
|
183
|
+
path: string[];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Build tier-banded layered candidates for a GROUPED boxes-and-lines graph.
|
|
188
|
+
* Returns [] when the graph has no groups, has group-endpoint edges, or is out
|
|
189
|
+
* of the supported size range — the dagre path handles those.
|
|
190
|
+
*/
|
|
191
|
+
export function groupedTierCandidates(
|
|
192
|
+
parsed: ParsedBoxesAndLines,
|
|
193
|
+
sizes: ReadonlyMap<string, Size>,
|
|
194
|
+
opts?: { restarts?: number; keepK?: number }
|
|
195
|
+
): BLLayoutResult[] {
|
|
196
|
+
if (parsed.groups.length === 0) return [];
|
|
197
|
+
const nCount = parsed.nodes.length;
|
|
198
|
+
if (nCount < 3 || nCount > 60) return [];
|
|
199
|
+
|
|
200
|
+
const isTB = parsed.direction === 'TB';
|
|
201
|
+
const labels = parsed.nodes.map((n) => n.label);
|
|
202
|
+
const labelSet = new Set(labels);
|
|
203
|
+
|
|
204
|
+
// Decline on group-endpoint edges (`__group_<label>` source/target) — not yet
|
|
205
|
+
// modelled here; dagre routes those.
|
|
206
|
+
for (const e of parsed.edges)
|
|
207
|
+
if (e.source.startsWith('__group_') || e.target.startsWith('__group_'))
|
|
208
|
+
return [];
|
|
209
|
+
|
|
210
|
+
// ── Group hierarchy ────────────────────────────────────────
|
|
211
|
+
const groupByLabel = new Map(parsed.groups.map((g) => [g.label, g]));
|
|
212
|
+
const directGroupOf = new Map<string, string>();
|
|
213
|
+
for (const grp of parsed.groups)
|
|
214
|
+
for (const c of grp.children)
|
|
215
|
+
if (labelSet.has(c)) directGroupOf.set(c, grp.label);
|
|
216
|
+
// Top-level group of a label: walk parentGroup to the root.
|
|
217
|
+
const topGroupOf = (groupLabel: string): string => {
|
|
218
|
+
let cur = groupLabel;
|
|
219
|
+
const guard = new Set<string>();
|
|
220
|
+
while (true) {
|
|
221
|
+
const g = groupByLabel.get(cur);
|
|
222
|
+
if (!g?.parentGroup || guard.has(cur)) return cur;
|
|
223
|
+
guard.add(cur);
|
|
224
|
+
cur = g.parentGroup;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
// Group path (top→direct) for a node, [] if ungrouped.
|
|
228
|
+
const pathOf = (label: string): string[] => {
|
|
229
|
+
const direct = directGroupOf.get(label);
|
|
230
|
+
if (!direct) return [];
|
|
231
|
+
const chain: string[] = [direct];
|
|
232
|
+
let cur = direct;
|
|
233
|
+
const guard = new Set<string>([direct]);
|
|
234
|
+
while (true) {
|
|
235
|
+
const g = groupByLabel.get(cur);
|
|
236
|
+
if (!g?.parentGroup || guard.has(g.parentGroup)) break;
|
|
237
|
+
chain.push(g.parentGroup);
|
|
238
|
+
guard.add(g.parentGroup);
|
|
239
|
+
cur = g.parentGroup;
|
|
240
|
+
}
|
|
241
|
+
return chain.reverse();
|
|
242
|
+
};
|
|
243
|
+
// Unit = top-level group label, or a synthetic singleton for ungrouped nodes.
|
|
244
|
+
const SINGLE = '\x00node\x00';
|
|
245
|
+
const unitOf = (label: string): string => {
|
|
246
|
+
const direct = directGroupOf.get(label);
|
|
247
|
+
return direct ? topGroupOf(direct) : SINGLE + label;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// Real, non-self edges with both endpoints present.
|
|
251
|
+
const edges = parsed.edges
|
|
252
|
+
.map((e, i) => ({ e, i }))
|
|
253
|
+
.filter(
|
|
254
|
+
({ e }) =>
|
|
255
|
+
labelSet.has(e.source) &&
|
|
256
|
+
labelSet.has(e.target) &&
|
|
257
|
+
e.source !== e.target
|
|
258
|
+
);
|
|
259
|
+
if (edges.length === 0) return [];
|
|
260
|
+
|
|
261
|
+
// ── Tier order over units (group-flow DAG) ─────────────────
|
|
262
|
+
const unitOfNode = new Map<string, string>();
|
|
263
|
+
for (const l of labels) unitOfNode.set(l, unitOf(l));
|
|
264
|
+
const units = Array.from(new Set(labels.map((l) => unitOfNode.get(l)!)));
|
|
265
|
+
const interUnit = edges
|
|
266
|
+
.filter(({ e }) => unitOfNode.get(e.source) !== unitOfNode.get(e.target))
|
|
267
|
+
.map(({ e, i }) => ({
|
|
268
|
+
from: unitOfNode.get(e.source)!,
|
|
269
|
+
to: unitOfNode.get(e.target)!,
|
|
270
|
+
idx: i,
|
|
271
|
+
}));
|
|
272
|
+
const { dag: unitDag } = acyclicOrient(units, interUnit);
|
|
273
|
+
const tier = longestPath(
|
|
274
|
+
units,
|
|
275
|
+
unitDag.map((d) => ({ from: d.from, to: d.to }))
|
|
276
|
+
);
|
|
277
|
+
if (!tier) return [];
|
|
278
|
+
|
|
279
|
+
// ── Local rank inside each unit (intra-unit DAG) ───────────
|
|
280
|
+
const localRank = new Map<string, number>();
|
|
281
|
+
const unitMembers = new Map<string, string[]>();
|
|
282
|
+
for (const l of labels) {
|
|
283
|
+
const u = unitOfNode.get(l)!;
|
|
284
|
+
(unitMembers.get(u) ?? unitMembers.set(u, []).get(u)!).push(l);
|
|
285
|
+
}
|
|
286
|
+
for (const [u, members] of unitMembers) {
|
|
287
|
+
const memberSet = new Set(members);
|
|
288
|
+
const intra = edges
|
|
289
|
+
.filter(
|
|
290
|
+
({ e }) =>
|
|
291
|
+
unitOfNode.get(e.source) === u &&
|
|
292
|
+
unitOfNode.get(e.target) === u &&
|
|
293
|
+
memberSet.has(e.source) &&
|
|
294
|
+
memberSet.has(e.target)
|
|
295
|
+
)
|
|
296
|
+
.map(({ e, i }) => ({ from: e.source, to: e.target, idx: i }));
|
|
297
|
+
const { dag } = acyclicOrient(members, intra);
|
|
298
|
+
const lr = longestPath(
|
|
299
|
+
members,
|
|
300
|
+
dag.map((d) => ({ from: d.from, to: d.to }))
|
|
301
|
+
);
|
|
302
|
+
if (!lr) return [];
|
|
303
|
+
for (const m of members) localRank.set(m, lr.get(m)!);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// ── Stack tiers into disjoint rank bands ───────────────────
|
|
307
|
+
const maxTier = Math.max(...units.map((u) => tier.get(u)!));
|
|
308
|
+
const bandHeight: number[] = Array.from({ length: maxTier + 1 }, () => 1);
|
|
309
|
+
for (const u of units) {
|
|
310
|
+
const h = Math.max(
|
|
311
|
+
...unitMembers.get(u)!.map((m) => localRank.get(m)! + 1)
|
|
312
|
+
);
|
|
313
|
+
const t = tier.get(u)!;
|
|
314
|
+
bandHeight[t] = Math.max(bandHeight[t]!, h);
|
|
315
|
+
}
|
|
316
|
+
const tierBase: number[] = [];
|
|
317
|
+
let accBase = 0;
|
|
318
|
+
for (let t = 0; t <= maxTier; t++) {
|
|
319
|
+
tierBase[t] = accBase;
|
|
320
|
+
accBase += bandHeight[t]!;
|
|
321
|
+
}
|
|
322
|
+
const tierStartRanks = new Set(tierBase.slice(1)); // first rank of each tier>0
|
|
323
|
+
|
|
324
|
+
const globalRank = new Map<string, number>();
|
|
325
|
+
for (const l of labels)
|
|
326
|
+
globalRank.set(
|
|
327
|
+
l,
|
|
328
|
+
tierBase[tier.get(unitOfNode.get(l)!)!]! + localRank.get(l)!
|
|
329
|
+
);
|
|
330
|
+
const maxRank = Math.max(...labels.map((l) => globalRank.get(l)!));
|
|
331
|
+
|
|
332
|
+
// ── Node model + edge classification by global rank ────────
|
|
333
|
+
const node = new Map<string, LNode>();
|
|
334
|
+
for (const n of parsed.nodes) {
|
|
335
|
+
const s = sizes.get(n.label) ?? { width: NODE_WIDTH, height: NODE_HEIGHT };
|
|
336
|
+
node.set(n.label, {
|
|
337
|
+
id: n.label,
|
|
338
|
+
dummy: false,
|
|
339
|
+
rank: globalRank.get(n.label)!,
|
|
340
|
+
cross: 0,
|
|
341
|
+
thick: isTB ? s.width : s.height,
|
|
342
|
+
depth: isTB ? s.height : s.width,
|
|
343
|
+
realW: s.width,
|
|
344
|
+
realH: s.height,
|
|
345
|
+
unit: unitOfNode.get(n.label)!,
|
|
346
|
+
path: pathOf(n.label),
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
interface EChain {
|
|
351
|
+
edgeIdx: number;
|
|
352
|
+
chain: string[];
|
|
353
|
+
}
|
|
354
|
+
const chains: EChain[] = [];
|
|
355
|
+
const segs: { a: string; b: string }[] = [];
|
|
356
|
+
const backEdges: { edgeIdx: number; src: string; tgt: string }[] = [];
|
|
357
|
+
const flatEdges: { edgeIdx: number; a: string; b: string }[] = [];
|
|
358
|
+
for (const { e, i } of edges) {
|
|
359
|
+
const rs = globalRank.get(e.source)!;
|
|
360
|
+
const rt = globalRank.get(e.target)!;
|
|
361
|
+
if (rs === rt) {
|
|
362
|
+
flatEdges.push({ edgeIdx: i, a: e.source, b: e.target });
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (rt < rs) {
|
|
366
|
+
backEdges.push({ edgeIdx: i, src: e.source, tgt: e.target });
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
// Forward (down-tier): dummy chain across intervening ranks.
|
|
370
|
+
const chain: string[] = [e.source];
|
|
371
|
+
let prev = e.source;
|
|
372
|
+
for (let r = rs + 1; r < rt; r++) {
|
|
373
|
+
const id = `__d${i}_${r}`;
|
|
374
|
+
node.set(id, {
|
|
375
|
+
id,
|
|
376
|
+
dummy: true,
|
|
377
|
+
rank: r,
|
|
378
|
+
cross: 0,
|
|
379
|
+
thick: DUMMY_THICK,
|
|
380
|
+
depth: 0,
|
|
381
|
+
realW: 0,
|
|
382
|
+
realH: 0,
|
|
383
|
+
unit: SINGLE + id,
|
|
384
|
+
path: [],
|
|
385
|
+
});
|
|
386
|
+
segs.push({ a: prev, b: id });
|
|
387
|
+
chain.push(id);
|
|
388
|
+
prev = id;
|
|
389
|
+
}
|
|
390
|
+
segs.push({ a: prev, b: e.target });
|
|
391
|
+
chain.push(e.target);
|
|
392
|
+
chains.push({ edgeIdx: i, chain });
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Rank buckets + adjacency for ordering.
|
|
396
|
+
const ranks: string[][] = Array.from({ length: maxRank + 1 }, () => []);
|
|
397
|
+
for (const n of node.values()) ranks[n.rank]!.push(n.id);
|
|
398
|
+
const up = new Map<string, string[]>();
|
|
399
|
+
const down = new Map<string, string[]>();
|
|
400
|
+
for (const id of node.keys()) {
|
|
401
|
+
up.set(id, []);
|
|
402
|
+
down.set(id, []);
|
|
403
|
+
}
|
|
404
|
+
for (const s of segs) {
|
|
405
|
+
down.get(s.a)!.push(s.b);
|
|
406
|
+
up.get(s.b)!.push(s.a);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// ── Ordering: wmedian + transpose (crossing minimization) ──
|
|
410
|
+
const orderOf = new Map<string, number>();
|
|
411
|
+
const gapCrossings = (r: number): number => {
|
|
412
|
+
const lower = ranks[r + 1];
|
|
413
|
+
if (!lower) return 0;
|
|
414
|
+
const es: { p: number; q: number }[] = [];
|
|
415
|
+
for (const a of ranks[r]!) {
|
|
416
|
+
const pa = orderOf.get(a)!;
|
|
417
|
+
for (const b of down.get(a)!) es.push({ p: pa, q: orderOf.get(b)! });
|
|
418
|
+
}
|
|
419
|
+
let c = 0;
|
|
420
|
+
for (let i = 0; i < es.length; i++)
|
|
421
|
+
for (let j = i + 1; j < es.length; j++) {
|
|
422
|
+
const A = es[i]!,
|
|
423
|
+
B = es[j]!;
|
|
424
|
+
if ((A.p - B.p) * (A.q - B.q) < 0) c++;
|
|
425
|
+
}
|
|
426
|
+
return c;
|
|
427
|
+
};
|
|
428
|
+
const totalCrossings = (): number => {
|
|
429
|
+
let c = 0;
|
|
430
|
+
for (let r = 0; r < maxRank; r++) c += gapCrossings(r);
|
|
431
|
+
return c;
|
|
432
|
+
};
|
|
433
|
+
const wmedianRank = (r: number, useUp: boolean): void => {
|
|
434
|
+
const ids = ranks[r]!;
|
|
435
|
+
const neigh = useUp ? up : down;
|
|
436
|
+
const med = new Map<string, number>();
|
|
437
|
+
for (const id of ids)
|
|
438
|
+
med.set(id, median(neigh.get(id)!.map((x) => orderOf.get(x)!)));
|
|
439
|
+
const movable = ids
|
|
440
|
+
.filter((id) => med.get(id)! >= 0)
|
|
441
|
+
.sort((a, b) => med.get(a)! - med.get(b)!);
|
|
442
|
+
const out = new Array<string | null>(ids.length).fill(null);
|
|
443
|
+
for (const id of ids) if (med.get(id)! < 0) out[orderOf.get(id)!] = id;
|
|
444
|
+
let mi = 0;
|
|
445
|
+
for (let slot = 0; slot < out.length; slot++)
|
|
446
|
+
if (out[slot] === null) out[slot] = movable[mi++]!;
|
|
447
|
+
const final = out as string[];
|
|
448
|
+
final.forEach((id, i) => orderOf.set(id, i));
|
|
449
|
+
ranks[r] = final;
|
|
450
|
+
};
|
|
451
|
+
const transpose = (): void => {
|
|
452
|
+
let improved = true;
|
|
453
|
+
let guard = 0;
|
|
454
|
+
while (improved && guard++ < 12) {
|
|
455
|
+
improved = false;
|
|
456
|
+
for (let r = 0; r <= maxRank; r++) {
|
|
457
|
+
const ids = ranks[r]!;
|
|
458
|
+
for (let i = 0; i < ids.length - 1; i++) {
|
|
459
|
+
const before =
|
|
460
|
+
(r > 0 ? gapCrossings(r - 1) : 0) +
|
|
461
|
+
(r < maxRank ? gapCrossings(r) : 0);
|
|
462
|
+
const a = ids[i]!,
|
|
463
|
+
b = ids[i + 1]!;
|
|
464
|
+
ids[i] = b;
|
|
465
|
+
ids[i + 1] = a;
|
|
466
|
+
orderOf.set(b, i);
|
|
467
|
+
orderOf.set(a, i + 1);
|
|
468
|
+
const after =
|
|
469
|
+
(r > 0 ? gapCrossings(r - 1) : 0) +
|
|
470
|
+
(r < maxRank ? gapCrossings(r) : 0);
|
|
471
|
+
if (after < before) improved = true;
|
|
472
|
+
else {
|
|
473
|
+
ids[i] = a;
|
|
474
|
+
ids[i + 1] = b;
|
|
475
|
+
orderOf.set(a, i);
|
|
476
|
+
orderOf.set(b, i + 1);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
const initOrder = (seed: number): void => {
|
|
483
|
+
const r = seed === 0 ? null : rng(seed);
|
|
484
|
+
for (let rr = 0; rr <= maxRank; rr++) {
|
|
485
|
+
const ids = ranks[rr]!.slice();
|
|
486
|
+
if (r)
|
|
487
|
+
for (let i = ids.length - 1; i > 0; i--) {
|
|
488
|
+
const j = Math.floor(r() * (i + 1));
|
|
489
|
+
[ids[i], ids[j]] = [ids[j]!, ids[i]!];
|
|
490
|
+
}
|
|
491
|
+
ranks[rr] = ids;
|
|
492
|
+
ids.forEach((id, i) => orderOf.set(id, i));
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const restarts =
|
|
497
|
+
opts?.restarts ?? (nCount <= 16 ? 20 : nCount <= 30 ? 12 : 6);
|
|
498
|
+
const keepK = opts?.keepK ?? 3;
|
|
499
|
+
const found: { sig: string; cross: number; order: Map<string, number> }[] =
|
|
500
|
+
[];
|
|
501
|
+
for (let s = 0; s < restarts; s++) {
|
|
502
|
+
initOrder(s);
|
|
503
|
+
let best = totalCrossings();
|
|
504
|
+
let bestSnap = new Map(orderOf);
|
|
505
|
+
for (let iter = 0; iter < 8; iter++) {
|
|
506
|
+
if (iter % 2 === 0)
|
|
507
|
+
for (let r = 1; r <= maxRank; r++) wmedianRank(r, true);
|
|
508
|
+
else for (let r = maxRank - 1; r >= 0; r--) wmedianRank(r, false);
|
|
509
|
+
transpose();
|
|
510
|
+
const c = totalCrossings();
|
|
511
|
+
if (c < best) {
|
|
512
|
+
best = c;
|
|
513
|
+
bestSnap = new Map(orderOf);
|
|
514
|
+
}
|
|
515
|
+
if (c === 0) break;
|
|
516
|
+
}
|
|
517
|
+
for (const [k, v] of bestSnap) orderOf.set(k, v);
|
|
518
|
+
for (let r = 0; r <= maxRank; r++)
|
|
519
|
+
ranks[r]!.sort((a, b) => orderOf.get(a)! - orderOf.get(b)!);
|
|
520
|
+
const sig = ranks.map((r) => r.join(',')).join('|');
|
|
521
|
+
if (!found.some((f) => f.sig === sig))
|
|
522
|
+
found.push({ sig, cross: best, order: new Map(bestSnap) });
|
|
523
|
+
}
|
|
524
|
+
found.sort((a, b) => a.cross - b.cross);
|
|
525
|
+
const keep = found.slice(0, keepK);
|
|
526
|
+
if (keep.length === 0) return [];
|
|
527
|
+
|
|
528
|
+
// ── Realize each kept ordering ─────────────────────────────
|
|
529
|
+
const results: BLLayoutResult[] = [];
|
|
530
|
+
for (const cand of keep) {
|
|
531
|
+
for (const [k, v] of cand.order) orderOf.set(k, v);
|
|
532
|
+
const rk: string[][] = Array.from({ length: maxRank + 1 }, () => []);
|
|
533
|
+
for (const n of node.values()) rk[n.rank]!.push(n.id);
|
|
534
|
+
for (let r = 0; r <= maxRank; r++)
|
|
535
|
+
rk[r]!.sort((a, b) => cand.order.get(a)! - cand.order.get(b)!);
|
|
536
|
+
results.push(realize(rk));
|
|
537
|
+
}
|
|
538
|
+
return results;
|
|
539
|
+
|
|
540
|
+
function weight(a: LNode, b: LNode): number {
|
|
541
|
+
if (a.dummy && b.dummy) return W_DUMMY_DUMMY;
|
|
542
|
+
if (a.dummy || b.dummy) return W_REAL_DUMMY;
|
|
543
|
+
return W_REAL_REAL;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Place ordered ids at desired cross-coords, preserving order and per-gap
|
|
547
|
+
// minimum separation (block-merging / PAV). `gap[i]` is the required
|
|
548
|
+
// center-to-center distance between ids[i-1] and ids[i].
|
|
549
|
+
function placeWithSeparation(
|
|
550
|
+
ids: string[],
|
|
551
|
+
desired: number[],
|
|
552
|
+
gap: number[]
|
|
553
|
+
): void {
|
|
554
|
+
const n = ids.length;
|
|
555
|
+
if (n === 0) return;
|
|
556
|
+
const off: number[] = [0];
|
|
557
|
+
for (let i = 1; i < n; i++) off[i] = off[i - 1]! + gap[i]!;
|
|
558
|
+
const d = desired.map((v, i) => v - off[i]!);
|
|
559
|
+
type Block = {
|
|
560
|
+
pos: number;
|
|
561
|
+
count: number;
|
|
562
|
+
sumOffset: number;
|
|
563
|
+
first: number;
|
|
564
|
+
};
|
|
565
|
+
const blocks: Block[] = [];
|
|
566
|
+
for (let i = 0; i < n; i++) {
|
|
567
|
+
let b: Block = { pos: d[i]!, count: 1, sumOffset: d[i]!, first: i };
|
|
568
|
+
while (blocks.length && blocks[blocks.length - 1]!.pos >= b.pos) {
|
|
569
|
+
const prev = blocks.pop()!;
|
|
570
|
+
const count = prev.count + b.count;
|
|
571
|
+
const sumOffset = prev.sumOffset + b.sumOffset;
|
|
572
|
+
b = { pos: sumOffset / count, count, sumOffset, first: prev.first };
|
|
573
|
+
}
|
|
574
|
+
blocks.push(b);
|
|
575
|
+
}
|
|
576
|
+
const xs = new Array<number>(n);
|
|
577
|
+
for (const b of blocks)
|
|
578
|
+
for (let k = 0; k < b.count; k++) xs[b.first + k] = b.pos;
|
|
579
|
+
for (let i = 0; i < n; i++) node.get(ids[i]!)!.cross = xs[i]! + off[i]!;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// Required center-to-center gap between two adjacent nodes in a rank, widened
|
|
583
|
+
// at group boundaries so padded group boxes don't collide.
|
|
584
|
+
function gapBetween(a: LNode, b: LNode): number {
|
|
585
|
+
const base = a.thick / 2 + b.thick / 2 + NODESEP;
|
|
586
|
+
if (a.dummy || b.dummy) return base;
|
|
587
|
+
if (a.unit !== b.unit) return base + 2 * GROUP_PAD + GROUP_GAP;
|
|
588
|
+
// Same top unit but different direct group → nested sibling boxes.
|
|
589
|
+
const da = a.path[a.path.length - 1];
|
|
590
|
+
const db = b.path[b.path.length - 1];
|
|
591
|
+
if (da !== db && (da || db)) return base + 2 * GROUP_PAD;
|
|
592
|
+
return base;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function realize(rk: string[][]): BLLayoutResult {
|
|
596
|
+
// Rank-axis band centres — extra gap when crossing a tier boundary.
|
|
597
|
+
const bandDepth = rk.map((ids) =>
|
|
598
|
+
Math.max(0, ...ids.map((id) => node.get(id)!.depth))
|
|
599
|
+
);
|
|
600
|
+
const bandCenter: number[] = [];
|
|
601
|
+
let acc = MARGIN;
|
|
602
|
+
for (let r = 0; r <= maxRank; r++) {
|
|
603
|
+
if (r > 0) acc += RANKSEP + (tierStartRanks.has(r) ? TIER_GAP : 0);
|
|
604
|
+
bandCenter[r] = acc + bandDepth[r]! / 2;
|
|
605
|
+
acc += bandDepth[r]!;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// Cross-axis: initial spread, then iterate median alignment.
|
|
609
|
+
const gapsFor = (ids: string[]): number[] =>
|
|
610
|
+
ids.map((id, i) =>
|
|
611
|
+
i === 0 ? 0 : gapBetween(node.get(ids[i - 1]!)!, node.get(id)!)
|
|
612
|
+
);
|
|
613
|
+
for (let r = 0; r <= maxRank; r++) {
|
|
614
|
+
const ids = rk[r]!;
|
|
615
|
+
const gap = gapsFor(ids);
|
|
616
|
+
let x = MARGIN;
|
|
617
|
+
for (let i = 0; i < ids.length; i++) {
|
|
618
|
+
const n = node.get(ids[i]!)!;
|
|
619
|
+
x += gap[i]!;
|
|
620
|
+
n.cross = i === 0 ? MARGIN + n.thick / 2 : x;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
const ITER = 18;
|
|
624
|
+
for (let it = 0; it < ITER; it++) {
|
|
625
|
+
const downPass = it % 2 === 0;
|
|
626
|
+
const order = downPass
|
|
627
|
+
? Array.from({ length: maxRank + 1 }, (_, i) => i)
|
|
628
|
+
: Array.from({ length: maxRank + 1 }, (_, i) => maxRank - i);
|
|
629
|
+
for (const r of order) {
|
|
630
|
+
const ids = rk[r]!;
|
|
631
|
+
const desired = ids.map((id) => {
|
|
632
|
+
const n = node.get(id)!;
|
|
633
|
+
let sw = 0,
|
|
634
|
+
sx = 0;
|
|
635
|
+
for (const nb of up.get(id)!) {
|
|
636
|
+
const w = weight(n, node.get(nb)!);
|
|
637
|
+
sw += w;
|
|
638
|
+
sx += w * node.get(nb)!.cross;
|
|
639
|
+
}
|
|
640
|
+
for (const nb of down.get(id)!) {
|
|
641
|
+
const w = weight(n, node.get(nb)!);
|
|
642
|
+
sw += w;
|
|
643
|
+
sx += w * node.get(nb)!.cross;
|
|
644
|
+
}
|
|
645
|
+
return sw > 0 ? sx / sw : n.cross;
|
|
646
|
+
});
|
|
647
|
+
placeWithSeparation(ids, desired, gapsFor(ids));
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// Normalize so min content cross = MARGIN.
|
|
652
|
+
let minC = Infinity;
|
|
653
|
+
for (const n of node.values()) minC = Math.min(minC, n.cross - n.thick / 2);
|
|
654
|
+
const shiftC = MARGIN - minC;
|
|
655
|
+
for (const n of node.values()) n.cross += shiftC;
|
|
656
|
+
|
|
657
|
+
const coord = (n: LNode): Pt =>
|
|
658
|
+
isTB
|
|
659
|
+
? { x: n.cross, y: bandCenter[n.rank]! }
|
|
660
|
+
: { x: bandCenter[n.rank]!, y: n.cross };
|
|
661
|
+
|
|
662
|
+
const layoutNodes: BLLayoutNode[] = parsed.nodes.map((pn) => {
|
|
663
|
+
const n = node.get(pn.label)!;
|
|
664
|
+
const c = coord(n);
|
|
665
|
+
return {
|
|
666
|
+
label: pn.label,
|
|
667
|
+
x: c.x,
|
|
668
|
+
y: c.y,
|
|
669
|
+
width: n.realW,
|
|
670
|
+
height: n.realH,
|
|
671
|
+
};
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
const chainById = new Map<number, string[]>();
|
|
675
|
+
for (const ch of chains) chainById.set(ch.edgeIdx, ch.chain);
|
|
676
|
+
|
|
677
|
+
// Peripheral routing for against-tier (back) edges. Each loops around the
|
|
678
|
+
// nearer side; lanes nest multiple returns so they don't overlap.
|
|
679
|
+
let minCross = Infinity,
|
|
680
|
+
maxCross = -Infinity;
|
|
681
|
+
for (const n of node.values()) {
|
|
682
|
+
if (n.dummy) continue;
|
|
683
|
+
minCross = Math.min(minCross, n.cross - n.thick / 2);
|
|
684
|
+
maxCross = Math.max(maxCross, n.cross + n.thick / 2);
|
|
685
|
+
}
|
|
686
|
+
const GAP = 34;
|
|
687
|
+
const LANE = 28;
|
|
688
|
+
const faceLim = (depth: number): number => Math.max(0, depth / 2 - 6);
|
|
689
|
+
const sideOf = (b: { src: string; tgt: string }): number => {
|
|
690
|
+
const d = node.get(b.src)!.cross - node.get(b.tgt)!.cross;
|
|
691
|
+
return d >= 0 ? 1 : -1;
|
|
692
|
+
};
|
|
693
|
+
const backSorted = backEdges
|
|
694
|
+
.map((b) => ({
|
|
695
|
+
...b,
|
|
696
|
+
side: sideOf(b),
|
|
697
|
+
span: Math.abs(node.get(b.src)!.rank - node.get(b.tgt)!.rank),
|
|
698
|
+
}))
|
|
699
|
+
.sort((a, b) => a.side - b.side || a.span - b.span);
|
|
700
|
+
const sideCounts = new Map<number, number>();
|
|
701
|
+
for (const b of backSorted)
|
|
702
|
+
sideCounts.set(b.side, (sideCounts.get(b.side) ?? 0) + 1);
|
|
703
|
+
const laneCounter = new Map<number, number>();
|
|
704
|
+
const backPoints = new Map<number, Pt[]>();
|
|
705
|
+
for (const b of backSorted) {
|
|
706
|
+
const s = b.side;
|
|
707
|
+
const k = laneCounter.get(s) ?? 0;
|
|
708
|
+
laneCounter.set(s, k + 1);
|
|
709
|
+
const K = sideCounts.get(s)!;
|
|
710
|
+
const src = node.get(b.src)!;
|
|
711
|
+
const tgt = node.get(b.tgt)!;
|
|
712
|
+
const laneC =
|
|
713
|
+
s > 0 ? maxCross + GAP + k * LANE : minCross - GAP - k * LANE;
|
|
714
|
+
const srcCtr = bandCenter[src.rank]!;
|
|
715
|
+
const tgtCtr = bandCenter[tgt.rank]!;
|
|
716
|
+
const frac = K > 1 ? (k + 1) / (K + 1) : 0;
|
|
717
|
+
const srcR = srcCtr - frac * faceLim(src.depth);
|
|
718
|
+
const tgtR = tgtCtr + frac * faceLim(tgt.depth);
|
|
719
|
+
const m = (c: number, rr: number): Pt =>
|
|
720
|
+
isTB ? { x: c, y: rr } : { x: rr, y: c };
|
|
721
|
+
const off = 10;
|
|
722
|
+
const srcEdgeC = src.cross + s * (src.thick / 2 + off);
|
|
723
|
+
const tgtEdgeC = tgt.cross + s * (tgt.thick / 2 + off);
|
|
724
|
+
// src is the LOWER-rank endpoint (higher global rank); tgt is UP-tier.
|
|
725
|
+
backPoints.set(b.edgeIdx, [
|
|
726
|
+
m(src.cross, srcCtr),
|
|
727
|
+
m(srcEdgeC, srcR),
|
|
728
|
+
m(laneC, srcR),
|
|
729
|
+
m(laneC, tgtR),
|
|
730
|
+
m(tgtEdgeC, tgtR),
|
|
731
|
+
m(tgt.cross, tgtCtr),
|
|
732
|
+
]);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Flat (same-rank) edges: a shallow arc bowing off the rank band so it does
|
|
736
|
+
// not run straight through any node sitting between the two endpoints.
|
|
737
|
+
const flatPoints = new Map<number, Pt[]>();
|
|
738
|
+
for (const f of flatEdges) {
|
|
739
|
+
const a = node.get(f.a)!;
|
|
740
|
+
const b = node.get(f.b)!;
|
|
741
|
+
const ctr = bandCenter[a.rank]!;
|
|
742
|
+
const bow = ctr - (a.depth / 2 + 26);
|
|
743
|
+
const m = (c: number, rr: number): Pt =>
|
|
744
|
+
isTB ? { x: c, y: rr } : { x: rr, y: c };
|
|
745
|
+
flatPoints.set(f.edgeIdx, [
|
|
746
|
+
m(a.cross, ctr),
|
|
747
|
+
m(a.cross, bow),
|
|
748
|
+
m(b.cross, bow),
|
|
749
|
+
m(b.cross, ctr),
|
|
750
|
+
]);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
const layoutEdges: BLLayoutEdge[] = [];
|
|
754
|
+
for (let i = 0; i < parsed.edges.length; i++) {
|
|
755
|
+
const e = parsed.edges[i]!;
|
|
756
|
+
if (!labelSet.has(e.source) || !labelSet.has(e.target)) continue;
|
|
757
|
+
let points: Pt[];
|
|
758
|
+
if (e.source === e.target) {
|
|
759
|
+
const c = coord(node.get(e.source)!);
|
|
760
|
+
points = [c, c];
|
|
761
|
+
} else if (backPoints.has(i)) points = backPoints.get(i)!;
|
|
762
|
+
else if (flatPoints.has(i)) points = flatPoints.get(i)!;
|
|
763
|
+
else {
|
|
764
|
+
const chain = chainById.get(i);
|
|
765
|
+
if (!chain) continue;
|
|
766
|
+
points = chain.map((id) => coord(node.get(id)!));
|
|
767
|
+
}
|
|
768
|
+
layoutEdges.push({
|
|
769
|
+
source: e.source,
|
|
770
|
+
target: e.target,
|
|
771
|
+
...(e.label !== undefined && { label: e.label }),
|
|
772
|
+
bidirectional: e.bidirectional,
|
|
773
|
+
lineNumber: e.lineNumber,
|
|
774
|
+
points,
|
|
775
|
+
yOffset: 0,
|
|
776
|
+
parallelCount: 1,
|
|
777
|
+
metadata: e.metadata,
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// ── Group rects (bottom-up so parents enclose nested children) ──
|
|
782
|
+
const nodeBox = new Map(
|
|
783
|
+
layoutNodes.map((n) => [
|
|
784
|
+
n.label,
|
|
785
|
+
{
|
|
786
|
+
l: n.x - n.width / 2,
|
|
787
|
+
r: n.x + n.width / 2,
|
|
788
|
+
t: n.y - n.height / 2,
|
|
789
|
+
b: n.y + n.height / 2,
|
|
790
|
+
},
|
|
791
|
+
])
|
|
792
|
+
);
|
|
793
|
+
// Order groups so children are computed before parents (deepest first).
|
|
794
|
+
const depthOf = (label: string): number => {
|
|
795
|
+
let d = 0;
|
|
796
|
+
let cur: string | undefined = label;
|
|
797
|
+
const guard = new Set<string>();
|
|
798
|
+
while (cur && !guard.has(cur)) {
|
|
799
|
+
guard.add(cur);
|
|
800
|
+
cur = groupByLabel.get(cur)?.parentGroup;
|
|
801
|
+
if (cur) d++;
|
|
802
|
+
}
|
|
803
|
+
return d;
|
|
804
|
+
};
|
|
805
|
+
const childGroups = new Map<string, string[]>();
|
|
806
|
+
for (const g of parsed.groups)
|
|
807
|
+
if (g.parentGroup)
|
|
808
|
+
(
|
|
809
|
+
childGroups.get(g.parentGroup) ??
|
|
810
|
+
childGroups.set(g.parentGroup, []).get(g.parentGroup)!
|
|
811
|
+
).push(g.label);
|
|
812
|
+
const rectByGroup = new Map<
|
|
813
|
+
string,
|
|
814
|
+
{ l: number; r: number; t: number; b: number }
|
|
815
|
+
>();
|
|
816
|
+
const ordered = [...parsed.groups].sort(
|
|
817
|
+
(a, b) => depthOf(b.label) - depthOf(a.label)
|
|
818
|
+
);
|
|
819
|
+
const layoutGroups: BLLayoutGroup[] = [];
|
|
820
|
+
for (const grp of ordered) {
|
|
821
|
+
let l = Infinity,
|
|
822
|
+
r = -Infinity,
|
|
823
|
+
t = Infinity,
|
|
824
|
+
bm = -Infinity;
|
|
825
|
+
const acc = (box: {
|
|
826
|
+
l: number;
|
|
827
|
+
r: number;
|
|
828
|
+
t: number;
|
|
829
|
+
b: number;
|
|
830
|
+
}): void => {
|
|
831
|
+
l = Math.min(l, box.l);
|
|
832
|
+
r = Math.max(r, box.r);
|
|
833
|
+
t = Math.min(t, box.t);
|
|
834
|
+
bm = Math.max(bm, box.b);
|
|
835
|
+
};
|
|
836
|
+
for (const c of grp.children) {
|
|
837
|
+
const nb = nodeBox.get(c);
|
|
838
|
+
if (nb) acc(nb);
|
|
839
|
+
}
|
|
840
|
+
for (const sub of childGroups.get(grp.label) ?? []) {
|
|
841
|
+
const sr = rectByGroup.get(sub);
|
|
842
|
+
if (sr) acc(sr);
|
|
843
|
+
}
|
|
844
|
+
if (!Number.isFinite(l)) continue; // empty group — skip
|
|
845
|
+
const x0 = l - GROUP_PAD;
|
|
846
|
+
const x1 = r + GROUP_PAD;
|
|
847
|
+
const y0 = t - GROUP_LABEL_ZONE;
|
|
848
|
+
const y1 = bm + GROUP_PAD;
|
|
849
|
+
rectByGroup.set(grp.label, { l: x0, r: x1, t: y0, b: y1 });
|
|
850
|
+
layoutGroups.push({
|
|
851
|
+
label: grp.label,
|
|
852
|
+
lineNumber: grp.lineNumber,
|
|
853
|
+
x: (x0 + x1) / 2,
|
|
854
|
+
y: (y0 + y1) / 2,
|
|
855
|
+
width: x1 - x0,
|
|
856
|
+
height: y1 - y0,
|
|
857
|
+
collapsed: false,
|
|
858
|
+
childCount: grp.children.length,
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// Content bbox over nodes, edge points, and group rects; shift to margin.
|
|
863
|
+
let minX = Infinity,
|
|
864
|
+
minY = Infinity,
|
|
865
|
+
maxX = -Infinity,
|
|
866
|
+
maxY = -Infinity;
|
|
867
|
+
const ext = (x0: number, y0: number, x1: number, y1: number): void => {
|
|
868
|
+
minX = Math.min(minX, x0);
|
|
869
|
+
minY = Math.min(minY, y0);
|
|
870
|
+
maxX = Math.max(maxX, x1);
|
|
871
|
+
maxY = Math.max(maxY, y1);
|
|
872
|
+
};
|
|
873
|
+
for (const n of layoutNodes)
|
|
874
|
+
ext(
|
|
875
|
+
n.x - n.width / 2,
|
|
876
|
+
n.y - n.height / 2,
|
|
877
|
+
n.x + n.width / 2,
|
|
878
|
+
n.y + n.height / 2
|
|
879
|
+
);
|
|
880
|
+
for (const e of layoutEdges)
|
|
881
|
+
for (const p of e.points) ext(p.x, p.y, p.x, p.y);
|
|
882
|
+
for (const g of layoutGroups)
|
|
883
|
+
ext(
|
|
884
|
+
g.x - g.width / 2,
|
|
885
|
+
g.y - g.height / 2,
|
|
886
|
+
g.x + g.width / 2,
|
|
887
|
+
g.y + g.height / 2
|
|
888
|
+
);
|
|
889
|
+
const sx = MARGIN - minX;
|
|
890
|
+
const sy = MARGIN - minY;
|
|
891
|
+
const shift = sx !== 0 || sy !== 0;
|
|
892
|
+
return {
|
|
893
|
+
nodes: shift
|
|
894
|
+
? layoutNodes.map((n) => ({ ...n, x: n.x + sx, y: n.y + sy }))
|
|
895
|
+
: layoutNodes,
|
|
896
|
+
edges: shift
|
|
897
|
+
? layoutEdges.map((e) => ({
|
|
898
|
+
...e,
|
|
899
|
+
points: e.points.map((p) => ({ x: p.x + sx, y: p.y + sy })),
|
|
900
|
+
}))
|
|
901
|
+
: layoutEdges,
|
|
902
|
+
groups: shift
|
|
903
|
+
? layoutGroups.map((g) => ({ ...g, x: g.x + sx, y: g.y + sy }))
|
|
904
|
+
: layoutGroups,
|
|
905
|
+
width: maxX + sx + MARGIN,
|
|
906
|
+
height: maxY + sy + MARGIN,
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
}
|