@ac-kit/math-graph 0.0.0-stage → 0.2.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/CHANGELOG.md +183 -0
- package/LICENSE +28 -0
- package/README.md +118 -2
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/layout/_force-forces.d.ts +227 -0
- package/dist/layout/_force-forces.d.ts.map +1 -0
- package/dist/layout/_force-forces.js +1572 -0
- package/dist/layout/_force-forces.js.map +1 -0
- package/dist/layout/_hierarchy-table.d.ts +34 -0
- package/dist/layout/_hierarchy-table.d.ts.map +1 -0
- package/dist/layout/_hierarchy-table.js +131 -0
- package/dist/layout/_hierarchy-table.js.map +1 -0
- package/dist/layout/_index-graph.d.ts +32 -0
- package/dist/layout/_index-graph.d.ts.map +1 -0
- package/dist/layout/_index-graph.js +51 -0
- package/dist/layout/_index-graph.js.map +1 -0
- package/dist/layout/_layer-order.d.ts +138 -0
- package/dist/layout/_layer-order.d.ts.map +1 -0
- package/dist/layout/_layer-order.js +954 -0
- package/dist/layout/_layer-order.js.map +1 -0
- package/dist/layout/_layered-acyclic.d.ts +28 -0
- package/dist/layout/_layered-acyclic.d.ts.map +1 -0
- package/dist/layout/_layered-acyclic.js +179 -0
- package/dist/layout/_layered-acyclic.js.map +1 -0
- package/dist/layout/_layered-position.d.ts +56 -0
- package/dist/layout/_layered-position.d.ts.map +1 -0
- package/dist/layout/_layered-position.js +369 -0
- package/dist/layout/_layered-position.js.map +1 -0
- package/dist/layout/_layered-rank.d.ts +58 -0
- package/dist/layout/_layered-rank.d.ts.map +1 -0
- package/dist/layout/_layered-rank.js +529 -0
- package/dist/layout/_layered-rank.js.map +1 -0
- package/dist/layout/_tree-common.d.ts +38 -0
- package/dist/layout/_tree-common.d.ts.map +1 -0
- package/dist/layout/_tree-common.js +73 -0
- package/dist/layout/_tree-common.js.map +1 -0
- package/dist/layout/_treemap-tiling.d.ts +73 -0
- package/dist/layout/_treemap-tiling.d.ts.map +1 -0
- package/dist/layout/_treemap-tiling.js +247 -0
- package/dist/layout/_treemap-tiling.js.map +1 -0
- package/dist/layout/circular-layout.d.ts +27 -0
- package/dist/layout/circular-layout.d.ts.map +1 -0
- package/dist/layout/circular-layout.js +35 -0
- package/dist/layout/circular-layout.js.map +1 -0
- package/dist/layout/create-force-step.d.ts +127 -0
- package/dist/layout/create-force-step.d.ts.map +1 -0
- package/dist/layout/create-force-step.js +167 -0
- package/dist/layout/create-force-step.js.map +1 -0
- package/dist/layout/dendrogram-layout.d.ts +32 -0
- package/dist/layout/dendrogram-layout.d.ts.map +1 -0
- package/dist/layout/dendrogram-layout.js +110 -0
- package/dist/layout/dendrogram-layout.js.map +1 -0
- package/dist/layout/force-directed-layout.d.ts +42 -0
- package/dist/layout/force-directed-layout.d.ts.map +1 -0
- package/dist/layout/force-directed-layout.js +112 -0
- package/dist/layout/force-directed-layout.js.map +1 -0
- package/dist/layout/layered-layout.d.ts +109 -0
- package/dist/layout/layered-layout.d.ts.map +1 -0
- package/dist/layout/layered-layout.js +124 -0
- package/dist/layout/layered-layout.js.map +1 -0
- package/dist/layout/partition-layout.d.ts +31 -0
- package/dist/layout/partition-layout.d.ts.map +1 -0
- package/dist/layout/partition-layout.js +78 -0
- package/dist/layout/partition-layout.js.map +1 -0
- package/dist/layout/phyllotaxis-layout.d.ts +17 -0
- package/dist/layout/phyllotaxis-layout.d.ts.map +1 -0
- package/dist/layout/phyllotaxis-layout.js +32 -0
- package/dist/layout/phyllotaxis-layout.js.map +1 -0
- package/dist/layout/sankey-layout.d.ts +113 -0
- package/dist/layout/sankey-layout.d.ts.map +1 -0
- package/dist/layout/sankey-layout.js +641 -0
- package/dist/layout/sankey-layout.js.map +1 -0
- package/dist/layout/tidy-tree-layout.d.ts +25 -0
- package/dist/layout/tidy-tree-layout.d.ts.map +1 -0
- package/dist/layout/tidy-tree-layout.js +197 -0
- package/dist/layout/tidy-tree-layout.js.map +1 -0
- package/dist/layout/treemap-layout.d.ts +63 -0
- package/dist/layout/treemap-layout.d.ts.map +1 -0
- package/dist/layout/treemap-layout.js +126 -0
- package/dist/layout/treemap-layout.js.map +1 -0
- package/dist/layout/types.d.ts +90 -0
- package/dist/layout/types.d.ts.map +1 -0
- package/dist/layout/types.js +2 -0
- package/dist/layout/types.js.map +1 -0
- package/package.json +68 -3
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { assertFinite, assertInteger, assertNonNegative, describeValue, } from "@ac-kit/core";
|
|
2
|
+
import { applyCentering, applyGravity, applyLinkForce, CollideForce, integrate, ManyBodyForce, } from "./_force-forces.js";
|
|
3
|
+
import { buildIndexGraph } from "./_index-graph.js";
|
|
4
|
+
function readTrait(items, read, fallback, label, isValid) {
|
|
5
|
+
const values = new Float64Array(items.length);
|
|
6
|
+
for (let index = 0; index < items.length; index++) {
|
|
7
|
+
const value = read === undefined ? fallback(index) : read(items[index], index);
|
|
8
|
+
if (!isValid(value)) {
|
|
9
|
+
throw new RangeError(`createForceStep: ${label} ${index} is invalid, got ${describeValue(value)}`);
|
|
10
|
+
}
|
|
11
|
+
values[index] = value;
|
|
12
|
+
}
|
|
13
|
+
return values;
|
|
14
|
+
}
|
|
15
|
+
function isFiniteNonNegative(value) {
|
|
16
|
+
return Number.isFinite(value) && value >= 0;
|
|
17
|
+
}
|
|
18
|
+
/** The link columns without their self-links, which exert no force. */
|
|
19
|
+
function withoutSelfLinks(sources, targets, distances, strengths, biases, iterations) {
|
|
20
|
+
let count = 0;
|
|
21
|
+
for (let link = 0; link < sources.length; link++) {
|
|
22
|
+
if (sources[link] !== targets[link]) {
|
|
23
|
+
count++;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const data = {
|
|
27
|
+
sources: new Uint32Array(count),
|
|
28
|
+
targets: new Uint32Array(count),
|
|
29
|
+
distances: new Float64Array(count),
|
|
30
|
+
strengths: new Float64Array(count),
|
|
31
|
+
biases: new Float64Array(count),
|
|
32
|
+
iterations,
|
|
33
|
+
};
|
|
34
|
+
let kept = 0;
|
|
35
|
+
for (let link = 0; link < sources.length; link++) {
|
|
36
|
+
if (sources[link] !== targets[link]) {
|
|
37
|
+
data.sources[kept] = sources[link];
|
|
38
|
+
data.targets[kept] = targets[link];
|
|
39
|
+
data.distances[kept] = distances[link];
|
|
40
|
+
data.strengths[kept] = strengths[link];
|
|
41
|
+
data.biases[kept] = biases[link];
|
|
42
|
+
kept++;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Reads traits once (degrees, biases, strengths, distances, charges, radii into
|
|
49
|
+
* typed arrays) and returns the step for this graph. The simulation's state —
|
|
50
|
+
* positions, velocities, alpha schedule, pins — stays with the caller (an
|
|
51
|
+
* animation loop); the step owns only derived constants and scratch (one
|
|
52
|
+
* `Quadtree` per force, aggregate buffers).
|
|
53
|
+
*
|
|
54
|
+
* Complexity: O(n + m) to create; per step O(n log n + m) Barnes–Hut (O(n²)
|
|
55
|
+
* exact many-body), collision O(n log n) expected, O(n²) when the discs overlap
|
|
56
|
+
* everywhere.
|
|
57
|
+
*
|
|
58
|
+
* @throws {RangeError} A link index outside `[0, nodes.length)` or not an
|
|
59
|
+
* integer, a non-finite or negative trait value naming its row, an option out
|
|
60
|
+
* of range.
|
|
61
|
+
*/
|
|
62
|
+
export function createForceStep(nodes, links, traits, options) {
|
|
63
|
+
const { velocityDecay = 0.4, approximation = "barnesHut", theta = 0.9, distanceMin = 1, distanceMax = Infinity, centerX = 0, centerY = 0, centerStrength = 1, gravity = 0, collideStrength = 1, collideIterations = 1, linkIterations = 1, compatibility, random, } = options;
|
|
64
|
+
if (typeof random !== "function") {
|
|
65
|
+
throw new TypeError(`createForceStep: random must be a function, got ${describeValue(random)}`);
|
|
66
|
+
}
|
|
67
|
+
if (!(velocityDecay >= 0 && velocityDecay <= 1)) {
|
|
68
|
+
throw new RangeError(`createForceStep: velocityDecay must be in [0, 1], got ${describeValue(velocityDecay)}`);
|
|
69
|
+
}
|
|
70
|
+
if (approximation !== "barnesHut" && approximation !== "exact") {
|
|
71
|
+
throw new RangeError(`createForceStep: approximation must be "barnesHut" or "exact", got ${describeValue(approximation)}`);
|
|
72
|
+
}
|
|
73
|
+
if (!(Number.isFinite(theta) && theta > 0)) {
|
|
74
|
+
throw new RangeError(`createForceStep: theta must be finite and positive, got ${describeValue(theta)}`);
|
|
75
|
+
}
|
|
76
|
+
assertNonNegative(distanceMin, "createForceStep: distanceMin");
|
|
77
|
+
assertNonNegative(distanceMax, "createForceStep: distanceMax", {
|
|
78
|
+
isInfinityAllowed: true,
|
|
79
|
+
});
|
|
80
|
+
if (distanceMax < distanceMin) {
|
|
81
|
+
throw new RangeError(`createForceStep: distanceMax ${distanceMax} is below distanceMin ${distanceMin}`);
|
|
82
|
+
}
|
|
83
|
+
assertFinite(centerX, "createForceStep: centerX");
|
|
84
|
+
assertFinite(centerY, "createForceStep: centerY");
|
|
85
|
+
assertFinite(centerStrength, "createForceStep: centerStrength");
|
|
86
|
+
assertNonNegative(gravity, "createForceStep: gravity");
|
|
87
|
+
if (!(collideStrength >= 0 && collideStrength <= 1)) {
|
|
88
|
+
throw new RangeError(`createForceStep: collideStrength must be in [0, 1], got ${describeValue(collideStrength)}`);
|
|
89
|
+
}
|
|
90
|
+
assertInteger(collideIterations, "createForceStep: collideIterations", 1);
|
|
91
|
+
if (compatibility !== undefined && compatibility !== "d3-force") {
|
|
92
|
+
throw new RangeError(`createForceStep: compatibility must be "d3-force" or absent, got ${describeValue(compatibility)}`);
|
|
93
|
+
}
|
|
94
|
+
const isD3 = compatibility === "d3-force";
|
|
95
|
+
assertInteger(linkIterations, "createForceStep: linkIterations", 1);
|
|
96
|
+
const nodeCount = nodes.length;
|
|
97
|
+
const graph = buildIndexGraph(nodeCount, links, traits, "createForceStep");
|
|
98
|
+
const degrees = (node) => graph.outDegrees[node] + graph.inDegrees[node];
|
|
99
|
+
const charges = readTrait(nodes, traits.chargeOf, () => -30, "charge of node", Number.isFinite);
|
|
100
|
+
const radii = traits.radiusOf === undefined
|
|
101
|
+
? undefined
|
|
102
|
+
: readTrait(nodes, traits.radiusOf, () => 0, "radius of node", isFiniteNonNegative);
|
|
103
|
+
const distances = readTrait(links, traits.linkDistanceOf, () => 30, "distance of link", isFiniteNonNegative);
|
|
104
|
+
const strengths = readTrait(links, traits.linkStrengthOf, (link) => 1 /
|
|
105
|
+
Math.min(degrees(graph.sources[link]), degrees(graph.targets[link])), "strength of link", isFiniteNonNegative);
|
|
106
|
+
const biases = new Float64Array(links.length);
|
|
107
|
+
for (let link = 0; link < links.length; link++) {
|
|
108
|
+
const source = degrees(graph.sources[link]);
|
|
109
|
+
biases[link] = source / (source + degrees(graph.targets[link]));
|
|
110
|
+
}
|
|
111
|
+
const linkData = isD3
|
|
112
|
+
? {
|
|
113
|
+
sources: graph.sources,
|
|
114
|
+
targets: graph.targets,
|
|
115
|
+
distances,
|
|
116
|
+
strengths,
|
|
117
|
+
biases,
|
|
118
|
+
iterations: linkIterations,
|
|
119
|
+
}
|
|
120
|
+
: withoutSelfLinks(graph.sources, graph.targets, distances, strengths, biases, linkIterations);
|
|
121
|
+
const manyBody = new ManyBodyForce(charges, {
|
|
122
|
+
approximation,
|
|
123
|
+
theta,
|
|
124
|
+
distanceMin,
|
|
125
|
+
distanceMax,
|
|
126
|
+
random,
|
|
127
|
+
isD3,
|
|
128
|
+
});
|
|
129
|
+
const collide = radii === undefined
|
|
130
|
+
? undefined
|
|
131
|
+
: new CollideForce(radii, collideStrength, collideIterations, random, isD3);
|
|
132
|
+
const keep = 1 - velocityDecay;
|
|
133
|
+
return (positions, velocities, alpha, fixedPositions) => {
|
|
134
|
+
if (positions.length !== nodeCount * 2) {
|
|
135
|
+
throw new RangeError(`createForceStep: positions must hold ${nodeCount * 2} numbers, got ${positions.length}`);
|
|
136
|
+
}
|
|
137
|
+
if (velocities.length !== nodeCount * 2) {
|
|
138
|
+
throw new RangeError(`createForceStep: velocities must hold ${nodeCount * 2} numbers, got ${velocities.length}`);
|
|
139
|
+
}
|
|
140
|
+
if (fixedPositions !== undefined &&
|
|
141
|
+
fixedPositions.length !== nodeCount * 2) {
|
|
142
|
+
throw new RangeError(`createForceStep: fixedPositions must hold ${nodeCount * 2} numbers, got ${fixedPositions.length}`);
|
|
143
|
+
}
|
|
144
|
+
if (fixedPositions !== undefined) {
|
|
145
|
+
for (let index = 0; index < fixedPositions.length; index++) {
|
|
146
|
+
const pin = fixedPositions[index];
|
|
147
|
+
if (!Number.isNaN(pin) && !Number.isFinite(pin)) {
|
|
148
|
+
throw new RangeError(`createForceStep: fixedPositions[${index}] must be finite or NaN, got ${describeValue(pin)}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (!(alpha >= 0 && alpha <= 1)) {
|
|
153
|
+
throw new RangeError(`createForceStep: alpha must be in [0, 1], got ${describeValue(alpha)}`);
|
|
154
|
+
}
|
|
155
|
+
applyLinkForce(positions, velocities, alpha, linkData, random);
|
|
156
|
+
manyBody.apply(positions, velocities, alpha);
|
|
157
|
+
collide?.apply(positions, velocities);
|
|
158
|
+
if (gravity !== 0) {
|
|
159
|
+
applyGravity(positions, velocities, alpha, gravity, centerX, centerY);
|
|
160
|
+
}
|
|
161
|
+
if (centerStrength !== 0) {
|
|
162
|
+
applyCentering(positions, centerStrength, centerX, centerY);
|
|
163
|
+
}
|
|
164
|
+
integrate(positions, velocities, keep, fixedPositions);
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=create-force-step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-force-step.js","sourceRoot":"","sources":["../../src/layout/create-force-step.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,aAAa,GACb,MAAM,cAAc,CAAC;AAGtB,OAAO,E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,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;oBACjD,MAAM,IAAI,UAAU,CACnB,mCAAmC,KAAK,gCAAgC,aAAa,CAAC,GAAG,CAAC,EAAE,CAC5F,CAAC;gBACH,CAAC;YACF,CAAC;QACF,CAAC;QACD,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,UAAU,CACnB,iDAAiD,aAAa,CAAC,KAAK,CAAC,EAAE,CACvE,CAAC;QACH,CAAC;QAED,cAAc,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC/D,QAAQ,CAAC,KAAK,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;QAC7C,OAAO,EAAE,KAAK,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;QACtC,IAAI,OAAO,KAAK,CAAC,EAAE,CAAC;YACnB,YAAY,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QACvE,CAAC;QACD,IAAI,cAAc,KAAK,CAAC,EAAE,CAAC;YAC1B,cAAc,CAAC,SAAS,EAAE,cAAc,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC7D,CAAC;QACD,SAAS,CAAC,SAAS,EAAE,UAAU,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
import type { HierarchyPointLayout, HierarchyWalkOptions, TreeSizeOptions, TreeTraits } from "./types.js";
|
|
3
|
+
export type DendrogramTraits<TNode> = TreeTraits<TNode> & {
|
|
4
|
+
/**
|
|
5
|
+
* Merge height (0 at leaves). Present: y = height · (1 − h / maxH), so the
|
|
6
|
+
* highest merge sits at the top and leaves at the bottom; inversions (a child
|
|
7
|
+
* higher than its parent) are drawn as given. Absent: every leaf at the same
|
|
8
|
+
* depth and each parent one level above its highest child. Finite, ≥ 0; read
|
|
9
|
+
* once per row, in pre-order.
|
|
10
|
+
*/
|
|
11
|
+
readonly heightOf?: Callable<[node: TNode, index: number], number>;
|
|
12
|
+
};
|
|
13
|
+
export type DendrogramLayoutOptions = HierarchyWalkOptions & TreeSizeOptions;
|
|
14
|
+
/**
|
|
15
|
+
* Leaves evenly spaced in pre-order along x (`separationOf` apart), each parent
|
|
16
|
+
* at the mean x of its children — the layout of a dendrogram or a cluster tree.
|
|
17
|
+
* With `width`/`height` the leaves are inset half a separation from the box
|
|
18
|
+
* edges; with `nodeWidth`/`nodeHeight` the root sits at x = 0 and the highest
|
|
19
|
+
* row at y = 0, `nodeHeight` per unit of height (or per level without
|
|
20
|
+
* `heightOf`). All heights 0 put every row at y = 0.
|
|
21
|
+
*
|
|
22
|
+
* The arrangement is the one scipy's `dendrogram` and d3's `cluster` draw.
|
|
23
|
+
*
|
|
24
|
+
* Complexity: O(n) time and memory, no recursion on depth. Separations and
|
|
25
|
+
* sizes are meant to lie within [1e−150, 1e150]; beyond that sums and quotients
|
|
26
|
+
* can underflow or overflow to NaN or ∞.
|
|
27
|
+
*
|
|
28
|
+
* @throws {RangeError} A size that is not finite and > 0, a height that is not
|
|
29
|
+
* finite and ≥ 0, or a separation that is not finite and > 0.
|
|
30
|
+
*/
|
|
31
|
+
export declare function dendrogramLayout<TNode>(root: TNode, traits: DendrogramTraits<TNode>, options?: DendrogramLayoutOptions): HierarchyPointLayout<TNode>;
|
|
32
|
+
//# sourceMappingURL=dendrogram-layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dendrogram-layout.d.ts","sourceRoot":"","sources":["../../src/layout/dendrogram-layout.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAQ7C,OAAO,KAAK,EACX,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EACf,UAAU,EACV,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,gBAAgB,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC,GAAG;IACzD;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;CACnE,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG,oBAAoB,GAAG,eAAe,CAAC;AAE7E;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EACrC,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAC/B,OAAO,GAAE,uBAA4B,GACnC,oBAAoB,CAAC,KAAK,CAAC,CA8F7B"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { assertNonNegative } from "@ac-kit/core";
|
|
2
|
+
import { buildHierarchyTable } from "./_hierarchy-table.js";
|
|
3
|
+
import { buildSiblingLinks, createCheckedSeparation, resolveTreeSize, } from "./_tree-common.js";
|
|
4
|
+
/**
|
|
5
|
+
* Leaves evenly spaced in pre-order along x (`separationOf` apart), each parent
|
|
6
|
+
* at the mean x of its children — the layout of a dendrogram or a cluster tree.
|
|
7
|
+
* With `width`/`height` the leaves are inset half a separation from the box
|
|
8
|
+
* edges; with `nodeWidth`/`nodeHeight` the root sits at x = 0 and the highest
|
|
9
|
+
* row at y = 0, `nodeHeight` per unit of height (or per level without
|
|
10
|
+
* `heightOf`). All heights 0 put every row at y = 0.
|
|
11
|
+
*
|
|
12
|
+
* The arrangement is the one scipy's `dendrogram` and d3's `cluster` draw.
|
|
13
|
+
*
|
|
14
|
+
* Complexity: O(n) time and memory, no recursion on depth. Separations and
|
|
15
|
+
* sizes are meant to lie within [1e−150, 1e150]; beyond that sums and quotients
|
|
16
|
+
* can underflow or overflow to NaN or ∞.
|
|
17
|
+
*
|
|
18
|
+
* @throws {RangeError} A size that is not finite and > 0, a height that is not
|
|
19
|
+
* finite and ≥ 0, or a separation that is not finite and > 0.
|
|
20
|
+
*/
|
|
21
|
+
export function dendrogramLayout(root, traits, options = {}) {
|
|
22
|
+
const size = resolveTreeSize(options, "dendrogramLayout");
|
|
23
|
+
const table = buildHierarchyTable(root, traits.childrenOf, { budget: options.budget }, "dendrogramLayout");
|
|
24
|
+
const { nodes, parentIndices, depths } = table;
|
|
25
|
+
const count = nodes.length;
|
|
26
|
+
const positions = new Float64Array(count * 2);
|
|
27
|
+
const separation = createCheckedSeparation(table, traits.separationOf, "dendrogramLayout");
|
|
28
|
+
const { firstChild, lastChild } = buildSiblingLinks(parentIndices);
|
|
29
|
+
// Height of each row above the base line, in the units of the y axis.
|
|
30
|
+
const heights = new Float64Array(count);
|
|
31
|
+
const { heightOf } = traits;
|
|
32
|
+
if (heightOf === undefined) {
|
|
33
|
+
// Children have higher rows than their parent: one reverse pass.
|
|
34
|
+
for (let row = count - 1; row > 0; row--) {
|
|
35
|
+
const parent = parentIndices[row];
|
|
36
|
+
if (heights[row] + 1 > heights[parent]) {
|
|
37
|
+
heights[parent] = heights[row] + 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
for (let row = 0; row < count; row++) {
|
|
43
|
+
const height = heightOf(nodes[row], row);
|
|
44
|
+
assertNonNegative(height, `dendrogramLayout: height of row ${row}`);
|
|
45
|
+
heights[row] = height;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let highest = 0;
|
|
49
|
+
for (let row = 0; row < count; row++) {
|
|
50
|
+
if (heights[row] > highest) {
|
|
51
|
+
highest = heights[row];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const xs = new Float64Array(count);
|
|
55
|
+
const childSums = new Float64Array(count);
|
|
56
|
+
const childCounts = new Uint32Array(count);
|
|
57
|
+
for (let row = 1; row < count; row++) {
|
|
58
|
+
childCounts[parentIndices[row]]++;
|
|
59
|
+
}
|
|
60
|
+
let previousLeaf = -1;
|
|
61
|
+
for (let row = 0; row < count; row++) {
|
|
62
|
+
if (firstChild[row] < 0) {
|
|
63
|
+
xs[row] =
|
|
64
|
+
previousLeaf < 0
|
|
65
|
+
? 0
|
|
66
|
+
: xs[previousLeaf] + separation(row, previousLeaf);
|
|
67
|
+
previousLeaf = row;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (let row = count - 1; row >= 0; row--) {
|
|
71
|
+
if (firstChild[row] >= 0) {
|
|
72
|
+
xs[row] = childSums[row] / childCounts[row];
|
|
73
|
+
}
|
|
74
|
+
if (row > 0) {
|
|
75
|
+
childSums[parentIndices[row]] += xs[row];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Margins matter only inside a box; a lone root is centred whatever the separation.
|
|
79
|
+
let lowEdge = -0.5;
|
|
80
|
+
let highEdge = 0.5;
|
|
81
|
+
if (!size.isPerNode && count > 1) {
|
|
82
|
+
const firstLeaf = descend_(0, firstChild);
|
|
83
|
+
const lastLeaf = descend_(0, lastChild);
|
|
84
|
+
lowEdge = xs[firstLeaf] - separation(firstLeaf, lastLeaf) / 2;
|
|
85
|
+
highEdge = xs[lastLeaf] + separation(lastLeaf, firstLeaf) / 2;
|
|
86
|
+
}
|
|
87
|
+
const rootX = xs[0];
|
|
88
|
+
for (let row = 0; row < count; row++) {
|
|
89
|
+
const level = heights[row];
|
|
90
|
+
if (size.isPerNode) {
|
|
91
|
+
positions[row * 2] = (xs[row] - rootX) * size.horizontal;
|
|
92
|
+
positions[row * 2 + 1] = (highest - level) * size.vertical;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
positions[row * 2] =
|
|
96
|
+
((xs[row] - lowEdge) / (highEdge - lowEdge)) * size.horizontal;
|
|
97
|
+
positions[row * 2 + 1] =
|
|
98
|
+
highest > 0 ? (1 - level / highest) * size.vertical : 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { nodes, parentIndices, depths, positions };
|
|
102
|
+
}
|
|
103
|
+
function descend_(row, next) {
|
|
104
|
+
let current = row;
|
|
105
|
+
while (next[current] >= 0) {
|
|
106
|
+
current = next[current];
|
|
107
|
+
}
|
|
108
|
+
return current;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=dendrogram-layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dendrogram-layout.js","sourceRoot":"","sources":["../../src/layout/dendrogram-layout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAGjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EACN,iBAAiB,EACjB,uBAAuB,EACvB,eAAe,GACf,MAAM,mBAAmB,CAAC;AAqB3B;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,gBAAgB,CAC/B,IAAW,EACX,MAA+B,EAC/B,OAAO,GAA4B,EAAE;IAErC,MAAM,IAAI,GAAG,eAAe,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC;IAC1D,MAAM,KAAK,GAAG,mBAAmB,CAChC,IAAI,EACJ,MAAM,CAAC,UAAU,EACjB,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,EAC1B,kBAAkB,CAClB,CAAC;IACF,MAAM,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC;IAC3B,MAAM,SAAS,GAAG,IAAI,YAAY,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;IAC9C,MAAM,UAAU,GAAG,uBAAuB,CACzC,KAAK,EACL,MAAM,CAAC,YAAY,EACnB,kBAAkB,CAClB,CAAC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AAC,CAAC,CAAC,GAAG,KAAK,GAAG,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1D,CAAC;IACF,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;AACpD,CAAC;AAED,SAAS,QAAQ,CAAC,GAAW,EAAE,IAAgB;IAC9C,IAAI,OAAO,GAAG,GAAG,CAAC;IAClB,OAAO,IAAI,CAAC,OAAO,CAAE,IAAI,CAAC,EAAE,CAAC;QAC5B,OAAO,GAAG,IAAI,CAAC,OAAO,CAAE,CAAC;IAC1B,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import type { ForceOptions, ForceTraits } from "./create-force-step.js";
|
|
3
|
+
import type { AsyncLayoutOptions } from "./types.js";
|
|
4
|
+
export type ForceDirectedLayoutOptions = ForceOptions & {
|
|
5
|
+
/** 2 per node; NaN entries take `phyllotaxisLayout`'s position (d3). */
|
|
6
|
+
readonly initialPositions?: Float64Array;
|
|
7
|
+
/** 2 per node; NaN = free on that axis. */
|
|
8
|
+
readonly fixedPositions?: Float64Array;
|
|
9
|
+
/** Steps run. Default ⌈log(alphaMin) / log(1 − alphaDecay)⌉ = 300. */
|
|
10
|
+
readonly iterations?: number;
|
|
11
|
+
/** Default 1, in [0, 1]. */
|
|
12
|
+
readonly alpha?: number;
|
|
13
|
+
/** Default 0.001, in (0, 1). */
|
|
14
|
+
readonly alphaMin?: number;
|
|
15
|
+
/** Default 1 − alphaMin^(1/300), in [0, 1]. */
|
|
16
|
+
readonly alphaDecay?: number;
|
|
17
|
+
/** Default 0, in [0, 1]. */
|
|
18
|
+
readonly alphaTarget?: number;
|
|
19
|
+
/** Charged n + m before every step. */
|
|
20
|
+
readonly budget?: WorkBudget;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* `createForceStep` run for `iterations` steps from rest, alpha updated before
|
|
24
|
+
* each step as d3 (`alpha += (alphaTarget − alpha)·alphaDecay`):
|
|
25
|
+
* `simulation.stop().tick(300)`. Pinned axes start at their pin; the other NaN
|
|
26
|
+
* initial coordinates take the phyllotaxis position of their node (both axes
|
|
27
|
+
* together, as d3). Returns new positions, 2 per node.
|
|
28
|
+
*
|
|
29
|
+
* Complexity: O(iterations · (n log n + m)), O(iterations · n²) exact.
|
|
30
|
+
*
|
|
31
|
+
* @throws {RangeError} The conditions of `createForceStep`, a position buffer
|
|
32
|
+
* of the wrong length or holding ±Infinity, an option out of range. Whatever
|
|
33
|
+
* `budget.spend` throws propagates.
|
|
34
|
+
*/
|
|
35
|
+
export declare function forceDirectedLayout<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: ForceTraits<TNode, TLink>, options: ForceDirectedLayoutOptions): Float64Array;
|
|
36
|
+
/**
|
|
37
|
+
* {@link forceDirectedLayout} yielding to the event loop after every step; the
|
|
38
|
+
* result equals the sync form's. Validation errors reject. Aborting rejects
|
|
39
|
+
* with `signal.reason` before the next step and discards the work.
|
|
40
|
+
*/
|
|
41
|
+
export declare function forceDirectedLayoutAsync<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: ForceTraits<TNode, TLink>, options: ForceDirectedLayoutOptions & AsyncLayoutOptions): Promise<Float64Array>;
|
|
42
|
+
//# sourceMappingURL=force-directed-layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"force-directed-layout.d.ts","sourceRoot":"","sources":["../../src/layout/force-directed-layout.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAExE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD,MAAM,MAAM,0BAA0B,GAAG,YAAY,GAAG;IACvD,wEAAwE;IACxE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,YAAY,CAAC;IACzC,2CAA2C;IAC3C,QAAQ,CAAC,cAAc,CAAC,EAAE,YAAY,CAAC;IACvC,sEAAsE;IACtE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,4BAA4B;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,gCAAgC;IAChC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,4BAA4B;IAC5B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,uCAAuC;IACvC,QAAQ,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC;CAC7B,CAAC;AAuHF;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,EAC/C,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,MAAM,EAAE,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,EACjC,OAAO,EAAE,0BAA0B,GACjC,YAAY,CAEd;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,KAAK,EACpD,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,KAAK,EAAE,SAAS,KAAK,EAAE,EACvB,MAAM,EAAE,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,EACjC,OAAO,EAAE,0BAA0B,GAAG,kBAAkB,GACtD,OAAO,CAAC,YAAY,CAAC,CAMvB"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { assertInteger, describeValue, runInTimeSlices, runToCompletion, } from "@ac-kit/core";
|
|
2
|
+
import { createForceStep } from "./create-force-step.js";
|
|
3
|
+
import { phyllotaxisLayout } from "./phyllotaxis-layout.js";
|
|
4
|
+
function checkPositions(positions, nodeCount, name) {
|
|
5
|
+
if (positions === undefined) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (positions.length !== nodeCount * 2) {
|
|
9
|
+
throw new RangeError(`forceDirectedLayout: ${name} must hold ${nodeCount * 2} numbers, got ${positions.length}`);
|
|
10
|
+
}
|
|
11
|
+
for (let index = 0; index < positions.length; index++) {
|
|
12
|
+
const value = positions[index];
|
|
13
|
+
if (!Number.isNaN(value) && !Number.isFinite(value)) {
|
|
14
|
+
throw new RangeError(`forceDirectedLayout: ${name}[${index}] must be finite or NaN, got ${describeValue(value)}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function unitInterval(value, name, isOpen) {
|
|
19
|
+
const isValid = isOpen ? value > 0 && value < 1 : value >= 0 && value <= 1;
|
|
20
|
+
if (!isValid) {
|
|
21
|
+
throw new RangeError(`forceDirectedLayout: ${name} must be in ${isOpen ? "(0, 1)" : "[0, 1]"}, got ${describeValue(value)}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function* forceSteps_(nodes, links, traits, options) {
|
|
25
|
+
const { initialPositions, fixedPositions, alphaMin = 0.001, alphaTarget = 0, budget, ...stepOptions } = options;
|
|
26
|
+
let alpha = options.alpha ?? 1;
|
|
27
|
+
const alphaDecay = options.alphaDecay ?? 1 - Math.pow(alphaMin, 1 / 300);
|
|
28
|
+
unitInterval(alphaMin, "alphaMin", true);
|
|
29
|
+
unitInterval(alpha, "alpha", false);
|
|
30
|
+
unitInterval(alphaDecay, "alphaDecay", false);
|
|
31
|
+
unitInterval(alphaTarget, "alphaTarget", false);
|
|
32
|
+
const nodeCount = nodes.length;
|
|
33
|
+
checkPositions(initialPositions, nodeCount, "initialPositions");
|
|
34
|
+
checkPositions(fixedPositions, nodeCount, "fixedPositions");
|
|
35
|
+
let iterations = options.iterations;
|
|
36
|
+
if (iterations === undefined) {
|
|
37
|
+
// The default decay gives exactly 300 ticks; rounding must not make it 301.
|
|
38
|
+
iterations =
|
|
39
|
+
options.alphaDecay === undefined
|
|
40
|
+
? 300
|
|
41
|
+
: Math.ceil(Math.log(alphaMin) / Math.log1p(-alphaDecay));
|
|
42
|
+
if (!Number.isFinite(iterations)) {
|
|
43
|
+
throw new RangeError("forceDirectedLayout: iterations is required when alphaDecay is 0");
|
|
44
|
+
}
|
|
45
|
+
// alphaDecay = 1 reaches the target in one tick, which the log gives as 0.
|
|
46
|
+
if (iterations === 0 && alpha > alphaTarget) {
|
|
47
|
+
iterations = 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
assertInteger(iterations, "forceDirectedLayout: iterations", 0);
|
|
51
|
+
const step = createForceStep(nodes, links, traits, stepOptions);
|
|
52
|
+
// Snapshots: the run may be suspended while the caller reuses its buffers.
|
|
53
|
+
const pins = fixedPositions?.slice();
|
|
54
|
+
const positions = initialPositions?.slice() ?? new Float64Array(nodeCount * 2);
|
|
55
|
+
if (initialPositions === undefined) {
|
|
56
|
+
positions.fill(NaN);
|
|
57
|
+
}
|
|
58
|
+
let placed;
|
|
59
|
+
for (let node = 0; node < nodeCount; node++) {
|
|
60
|
+
if (Number.isNaN(positions[2 * node]) ||
|
|
61
|
+
Number.isNaN(positions[2 * node + 1])) {
|
|
62
|
+
placed ??= phyllotaxisLayout(nodeCount);
|
|
63
|
+
positions[2 * node] = placed[2 * node];
|
|
64
|
+
positions[2 * node + 1] = placed[2 * node + 1];
|
|
65
|
+
}
|
|
66
|
+
for (let axis = 0; axis < 2; axis++) {
|
|
67
|
+
const pin = pins === undefined ? NaN : pins[2 * node + axis];
|
|
68
|
+
if (!Number.isNaN(pin)) {
|
|
69
|
+
positions[2 * node + axis] = pin;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const velocities = new Float64Array(nodeCount * 2);
|
|
74
|
+
const cost = nodeCount + links.length;
|
|
75
|
+
for (let iteration = 0; iteration < iterations; iteration++) {
|
|
76
|
+
// Before the charge, so that an abort seen here costs no tick.
|
|
77
|
+
yield;
|
|
78
|
+
budget?.spend(cost);
|
|
79
|
+
alpha = Math.min(1, Math.max(0, alpha + (alphaTarget - alpha) * alphaDecay));
|
|
80
|
+
step(positions, velocities, alpha, pins);
|
|
81
|
+
}
|
|
82
|
+
return positions;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* `createForceStep` run for `iterations` steps from rest, alpha updated before
|
|
86
|
+
* each step as d3 (`alpha += (alphaTarget − alpha)·alphaDecay`):
|
|
87
|
+
* `simulation.stop().tick(300)`. Pinned axes start at their pin; the other NaN
|
|
88
|
+
* initial coordinates take the phyllotaxis position of their node (both axes
|
|
89
|
+
* together, as d3). Returns new positions, 2 per node.
|
|
90
|
+
*
|
|
91
|
+
* Complexity: O(iterations · (n log n + m)), O(iterations · n²) exact.
|
|
92
|
+
*
|
|
93
|
+
* @throws {RangeError} The conditions of `createForceStep`, a position buffer
|
|
94
|
+
* of the wrong length or holding ±Infinity, an option out of range. Whatever
|
|
95
|
+
* `budget.spend` throws propagates.
|
|
96
|
+
*/
|
|
97
|
+
export function forceDirectedLayout(nodes, links, traits, options) {
|
|
98
|
+
return runToCompletion(forceSteps_(nodes, links, traits, options));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* {@link forceDirectedLayout} yielding to the event loop after every step; the
|
|
102
|
+
* result equals the sync form's. Validation errors reject. Aborting rejects
|
|
103
|
+
* with `signal.reason` before the next step and discards the work.
|
|
104
|
+
*/
|
|
105
|
+
export function forceDirectedLayoutAsync(nodes, links, traits, options) {
|
|
106
|
+
const { signal, sliceMs, ...layoutOptions } = options;
|
|
107
|
+
return runInTimeSlices(forceSteps_(nodes, links, traits, layoutOptions), {
|
|
108
|
+
signal,
|
|
109
|
+
sliceMs,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=force-directed-layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|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AAC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AACpE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACvC,KAAuB,EACvB,KAAuB,EACvB,MAAiC,EACjC,OAAwD;IAExD,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,aAAa,EAAE,GAAG,OAAO,CAAC;IACtD,OAAO,eAAe,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE;QACxE,MAAM;QACN,OAAO;KACP,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Callable, WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import type { CycleRemovalMethod } from "./_layered-acyclic.js";
|
|
3
|
+
import type { AsyncLayoutOptions, NetworkTraits } from "./types.js";
|
|
4
|
+
export type LayeredTraits<TNode, TLink> = NetworkTraits<TNode, TLink> & {
|
|
5
|
+
/** Node box size (default 0: points); finite, ≥ 0. */
|
|
6
|
+
readonly widthOf?: Callable<[node: TNode, index: number], number>;
|
|
7
|
+
readonly heightOf?: Callable<[node: TNode, index: number], number>;
|
|
8
|
+
/** Ranking and ordering weight. Default 1, finite ≥ 0. */
|
|
9
|
+
readonly weightOf?: Callable<[link: TLink, index: number], number>;
|
|
10
|
+
/** Minimum rank span. Default 1; a positive integer. */
|
|
11
|
+
readonly minLengthOf?: Callable<[link: TLink, index: number], number>;
|
|
12
|
+
};
|
|
13
|
+
export type LayeredLayoutOptions = {
|
|
14
|
+
/** Rank direction. Default "down" (ranks grow with y). */
|
|
15
|
+
readonly direction?: "down" | "up" | "right" | "left";
|
|
16
|
+
/** Gap between nodes of a rank, beyond their sizes. Default 50; finite, ≥ 0. */
|
|
17
|
+
readonly nodeSeparation?: number;
|
|
18
|
+
/** Gap between ranks, beyond their sizes. Default 50; finite, ≥ 0. */
|
|
19
|
+
readonly rankSeparation?: number;
|
|
20
|
+
/** Gap around the bends of long links. Default 10; finite, ≥ 0. */
|
|
21
|
+
readonly edgeSeparation?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Default "networkSimplex" (Gansner et al. 1993, minimises Σ weight·span);
|
|
24
|
+
* "longestPath" is O(V + E) and stacks sinks on the last rank.
|
|
25
|
+
*/
|
|
26
|
+
readonly ranking?: "networkSimplex" | "longestPath";
|
|
27
|
+
/**
|
|
28
|
+
* Links reversed to break cycles: "depthFirst" (default) or "greedy"
|
|
29
|
+
* (Eades–Lin–Smyth 1993).
|
|
30
|
+
*/
|
|
31
|
+
readonly cycleRemoval?: CycleRemovalMethod;
|
|
32
|
+
/**
|
|
33
|
+
* Ordering stops after this many sweeps, or sifting rounds, without fewer
|
|
34
|
+
* crossings. Default 4.
|
|
35
|
+
*/
|
|
36
|
+
readonly orderingPatience?: number;
|
|
37
|
+
/**
|
|
38
|
+
* `"thorough"` (default) ends the crossing reduction with sifting
|
|
39
|
+
* (Matuszewski, Schönfeld and Molitor 1999), O(k · e) per round and layer of
|
|
40
|
+
* k vertices and e segments, which leaves local minima the sweeps and
|
|
41
|
+
* transposes stop in; `"fast"` stops after the sweeps and transposes.
|
|
42
|
+
*/
|
|
43
|
+
readonly orderingEffort?: "fast" | "thorough";
|
|
44
|
+
/**
|
|
45
|
+
* Charged per network-simplex pivot (V + E), per ordering sweep and transpose
|
|
46
|
+
* pass (V′ + E′), and per sifted layer (k · (k + 2e)).
|
|
47
|
+
*/
|
|
48
|
+
readonly budget?: WorkBudget;
|
|
49
|
+
};
|
|
50
|
+
export type LayeredLayout = {
|
|
51
|
+
/** Node centres, 2 per node. */
|
|
52
|
+
readonly positions: Float64Array;
|
|
53
|
+
/** Rank per node, 0 first. */
|
|
54
|
+
readonly ranks: Uint32Array;
|
|
55
|
+
/** Position within its rank, 0 first. */
|
|
56
|
+
readonly orders: Uint32Array;
|
|
57
|
+
/**
|
|
58
|
+
* Polyline vertices, 2 each: per link, from the source's box border through
|
|
59
|
+
* each bend to the target's border.
|
|
60
|
+
*/
|
|
61
|
+
readonly edgePoints: Float64Array;
|
|
62
|
+
/**
|
|
63
|
+
* `linkCount + 1` offsets: link i is vertices `[edgeStarts[i], edgeStarts[i
|
|
64
|
+
*
|
|
65
|
+
* - 1])`. Self-loops are empty.
|
|
66
|
+
*/
|
|
67
|
+
readonly edgeStarts: Uint32Array;
|
|
68
|
+
/**
|
|
69
|
+
* 1 where link i was reversed to break a cycle; its polyline still runs
|
|
70
|
+
* source → target.
|
|
71
|
+
*/
|
|
72
|
+
readonly reversed: Uint8Array;
|
|
73
|
+
/** Size of the box holding every node and bend, origin (0, 0). */
|
|
74
|
+
readonly width: number;
|
|
75
|
+
readonly height: number;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Layered drawing of a directed graph (Sugiyama, Tagawa & Toda 1981): cycle
|
|
79
|
+
* removal, ranking, long links split by dummy vertices, crossing reduction by
|
|
80
|
+
* barycentric layer sweeps with exact bilayer crossing counts (Barth, Jünger &
|
|
81
|
+
* Mutzel 2004), coordinates by Brandes & Köpf 2001 (balanced median of four
|
|
82
|
+
* alignments). Disconnected parts share the ranks. Parallel links each get a
|
|
83
|
+
* path. Deterministic.
|
|
84
|
+
*
|
|
85
|
+
* The result is exactly the "down" layout turned: "up" flips the rank axis,
|
|
86
|
+
* "right" and "left" swap the axes (and flip, for "left") with the node sizes
|
|
87
|
+
* swapped, so widths always read along x and heights along y.
|
|
88
|
+
*
|
|
89
|
+
* Complexity: cycle removal O(V + E); ranking O(V + E) longest path, network
|
|
90
|
+
* simplex polynomial and near-linear in practice (cap with `budget`); with D
|
|
91
|
+
* dummy vertices, V′ = V + D and E′ = E + D: ordering O(sweeps · E′ log V′),
|
|
92
|
+
* coordinates O(V′ + E′). D grows with the rank span of the links, bounded by
|
|
93
|
+
* `minLengthOf`.
|
|
94
|
+
*
|
|
95
|
+
* @throws {RangeError} A link index outside `[0, nodes.length)` or not an
|
|
96
|
+
* integer, an invalid size, weight, minimum length or option, naming the
|
|
97
|
+
* row.
|
|
98
|
+
*/
|
|
99
|
+
export declare function layeredLayout<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: LayeredTraits<TNode, TLink>, options?: LayeredLayoutOptions): LayeredLayout;
|
|
100
|
+
/**
|
|
101
|
+
* {@link layeredLayout} yielding to the event loop after cycle removal, after
|
|
102
|
+
* every network-simplex round and pivot, after every ordering sweep and
|
|
103
|
+
* transpose pass, and before the coordinates; the result equals the sync
|
|
104
|
+
* form's. Validation errors reject. Aborting rejects with `signal.reason` at
|
|
105
|
+
* the next of those points and discards the work. A single pivot or sweep of a
|
|
106
|
+
* very large graph can still overshoot `sliceMs`.
|
|
107
|
+
*/
|
|
108
|
+
export declare function layeredLayoutAsync<TNode, TLink>(nodes: readonly TNode[], links: readonly TLink[], traits: LayeredTraits<TNode, TLink>, options?: LayeredLayoutOptions & AsyncLayoutOptions): Promise<LayeredLayout>;
|
|
109
|
+
//# sourceMappingURL=layered-layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layered-layout.d.ts","sourceRoot":"","sources":["../../src/layout/layered-layout.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAKzD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAGhE,OAAO,KAAK,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEpE,MAAM,MAAM,aAAa,CAAC,KAAK,EAAE,KAAK,IAAI,aAAa,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG;IACvE,sDAAsD;IACtD,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;IAClE,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CA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