Implement real force graph layout
This commit is contained in:
parent
ebcb68d33d
commit
57dc01e2e3
2 changed files with 362 additions and 13 deletions
|
|
@ -2446,7 +2446,7 @@ function handleViewportPointerDown(
|
|||
event.currentTarget.setPointerCapture(event.pointerId);
|
||||
}
|
||||
|
||||
function isMiddlePointerEvent(event: ReactPointerEvent<HTMLElement>) {
|
||||
function isMiddlePointerEvent(event: ReactPointerEvent<HTMLElement | SVGSVGElement>) {
|
||||
return event.button === 1 || (event.buttons & 4) === 4;
|
||||
}
|
||||
|
||||
|
|
@ -2506,7 +2506,7 @@ function isInteractiveArticleTarget(target: EventTarget) {
|
|||
return target instanceof Element && Boolean(target.closest("a, button, input, textarea, select, summary, [role='button']"));
|
||||
}
|
||||
|
||||
function isArticleContentTarget(target: EventTarget) {
|
||||
function isArticleContentTarget(target: EventTarget | null) {
|
||||
if (!(target instanceof Element)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,16 @@ type LayoutViewport = {
|
|||
height: number;
|
||||
};
|
||||
|
||||
type ForceLayoutInput = {
|
||||
graphKey: GraphKey;
|
||||
graph: EpiphanyGraph;
|
||||
sizes: Array<{ width: number; height: number }>;
|
||||
edgePairs: number[];
|
||||
nodeDegrees: Map<string, number>;
|
||||
linkCounts: Map<string, number>;
|
||||
viewport?: LayoutViewport;
|
||||
};
|
||||
|
||||
export async function layoutEpiphanyGraphs(
|
||||
state: EpiphanyGraphsState,
|
||||
mode: EpiphanyGraphLayoutModeConfig = "layered",
|
||||
|
|
@ -57,17 +67,21 @@ async function layoutGraph(
|
|||
edgePairs.push(source, target);
|
||||
}
|
||||
|
||||
const coordinates = await layoutWithRustSolver({
|
||||
nodeWeights: new Float32Array(
|
||||
graph.nodes.map((node, index) => {
|
||||
const degree = nodeDegrees.get(node.id) ?? 0;
|
||||
const linkCount = linkCounts.get(node.id) ?? 0;
|
||||
return Math.max(0.25, 1 + degree * 0.16 + linkCount * 0.24 + (sizes[index].width * sizes[index].height) / 80_000);
|
||||
}),
|
||||
),
|
||||
edgePairs: new Uint32Array(edgePairs),
|
||||
...rustSolverConfigFor(graphKey, mode),
|
||||
});
|
||||
const coordinates = usesForceLayout(mode)
|
||||
? layoutWithDeterministicForce({
|
||||
graphKey,
|
||||
graph,
|
||||
sizes,
|
||||
edgePairs,
|
||||
nodeDegrees,
|
||||
linkCounts,
|
||||
viewport,
|
||||
})
|
||||
: await layoutWithRustSolver({
|
||||
nodeWeights: buildNodeWeights(graph, sizes, nodeDegrees, linkCounts),
|
||||
edgePairs: new Uint32Array(edgePairs),
|
||||
...rustSolverConfigFor(graphKey, mode),
|
||||
});
|
||||
|
||||
const positionedNodes: PositionedNode[] = graph.nodes.map((node, index) => {
|
||||
const size = sizes[index];
|
||||
|
|
@ -133,6 +147,10 @@ function sizingAlgorithmForMode(mode: EpiphanyGraphLayoutMode): NodeSizingMode {
|
|||
return mode === "layered" ? "layered" : "compact";
|
||||
}
|
||||
|
||||
function usesForceLayout(mode: EpiphanyGraphLayoutMode) {
|
||||
return mode === "force" || mode === "combined-force";
|
||||
}
|
||||
|
||||
function rustSolverConfigFor(graphKey: GraphKey, mode: EpiphanyGraphLayoutMode) {
|
||||
if (mode === "layered") {
|
||||
return {
|
||||
|
|
@ -160,6 +178,337 @@ function rustSolverConfigFor(graphKey: GraphKey, mode: EpiphanyGraphLayoutMode)
|
|||
};
|
||||
}
|
||||
|
||||
function buildNodeWeights(
|
||||
graph: EpiphanyGraph,
|
||||
sizes: Array<{ width: number; height: number }>,
|
||||
nodeDegrees: Map<string, number>,
|
||||
linkCounts: Map<string, number>,
|
||||
) {
|
||||
return new Float32Array(
|
||||
graph.nodes.map((node, index) => {
|
||||
const degree = nodeDegrees.get(node.id) ?? 0;
|
||||
const linkCount = linkCounts.get(node.id) ?? 0;
|
||||
return Math.max(0.25, 1 + degree * 0.16 + linkCount * 0.24 + (sizes[index].width * sizes[index].height) / 80_000);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function layoutWithDeterministicForce({
|
||||
graphKey,
|
||||
graph,
|
||||
sizes,
|
||||
edgePairs,
|
||||
nodeDegrees,
|
||||
linkCounts,
|
||||
viewport,
|
||||
}: ForceLayoutInput) {
|
||||
const nodeCount = graph.nodes.length;
|
||||
const coordinates = new Float32Array(nodeCount * 4);
|
||||
|
||||
if (nodeCount === 0) {
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
const maxDegree = Math.max(1, ...graph.nodes.map((node) => nodeDegrees.get(node.id) ?? 0));
|
||||
const baseWidth = Math.max(viewport?.width ?? 0, 900);
|
||||
const baseHeight = Math.max(viewport?.height ?? 0, 620);
|
||||
const graphWidth = Math.max(baseWidth * 1.35, Math.sqrt(nodeCount) * 250);
|
||||
const graphHeight = Math.max(baseHeight * 1.35, Math.sqrt(nodeCount) * 200);
|
||||
const centerX = graphWidth / 2;
|
||||
const centerY = graphHeight / 2;
|
||||
const area = graphWidth * graphHeight;
|
||||
const naturalDistance = Math.sqrt(area / Math.max(1, nodeCount)) * (graphKey === "architecture" ? 0.62 : 0.72);
|
||||
const sections = sortedNodeSections(graph);
|
||||
const sectionCenters = buildSectionCenters(sections, centerX, centerY, graphWidth, graphHeight);
|
||||
const positions = new Float64Array(nodeCount * 2);
|
||||
const velocities = new Float64Array(nodeCount * 2);
|
||||
|
||||
for (let index = 0; index < nodeCount; index += 1) {
|
||||
const node = graph.nodes[index];
|
||||
const section = nodeSection(node.id);
|
||||
const sectionCenter = sectionCenters.get(section) ?? { x: centerX, y: centerY };
|
||||
const degree = nodeDegrees.get(node.id) ?? 0;
|
||||
const hubPull = degree / maxDegree;
|
||||
const angle = seededAngle(`${node.id}:angle`);
|
||||
const radiusSeed = seededUnit(`${node.id}:radius`);
|
||||
const sectionRadius = Math.min(graphWidth, graphHeight) * 0.1;
|
||||
const radius = sectionRadius * (0.28 + radiusSeed * 0.72) * (1 - hubPull * 0.78);
|
||||
const linkedPull = Math.min(1, (linkCounts.get(node.id) ?? 0) / 10);
|
||||
const centrality = Math.max(hubPull, linkedPull);
|
||||
const sectionMix = 0.62 - centrality * 0.52;
|
||||
const mixedCenterX = lerp(centerX, sectionCenter.x, sectionMix);
|
||||
const mixedCenterY = lerp(centerY, sectionCenter.y, sectionMix);
|
||||
|
||||
positions[index * 2] = mixedCenterX + Math.cos(angle) * radius;
|
||||
positions[index * 2 + 1] = mixedCenterY + Math.sin(angle) * radius;
|
||||
}
|
||||
|
||||
const iterations = graphKey === "architecture" ? 620 : 440;
|
||||
let temperature = Math.min(graphWidth, graphHeight) * 0.065;
|
||||
const minTemperature = Math.max(10, naturalDistance * 0.045);
|
||||
|
||||
for (let pass = 0; pass < iterations; pass += 1) {
|
||||
const displacements = new Float64Array(nodeCount * 2);
|
||||
|
||||
applyRepulsion(positions, displacements, sizes, naturalDistance);
|
||||
applyEdgeAttraction(positions, displacements, edgePairs, graph, nodeDegrees, naturalDistance);
|
||||
applySectionGravity(positions, displacements, graph, nodeDegrees, linkCounts, sectionCenters, centerX, centerY, maxDegree);
|
||||
applyCollisionPush(positions, displacements, sizes, graph.nodes, nodeDegrees, maxDegree);
|
||||
applyBoundaryGravity(positions, displacements, graphWidth, graphHeight, centerX, centerY);
|
||||
|
||||
for (let index = 0; index < nodeCount; index += 1) {
|
||||
const offset = index * 2;
|
||||
const degree = nodeDegrees.get(graph.nodes[index].id) ?? 0;
|
||||
const damping = 0.64 + (degree / maxDegree) * 0.16;
|
||||
const dx = clamp(displacements[offset], -temperature, temperature);
|
||||
const dy = clamp(displacements[offset + 1], -temperature, temperature);
|
||||
velocities[offset] = (velocities[offset] + dx) * damping;
|
||||
velocities[offset + 1] = (velocities[offset + 1] + dy) * damping;
|
||||
positions[offset] += velocities[offset] * 0.08;
|
||||
positions[offset + 1] += velocities[offset + 1] * 0.08;
|
||||
}
|
||||
|
||||
temperature = Math.max(minTemperature, temperature * 0.992);
|
||||
}
|
||||
|
||||
for (let index = 0; index < nodeCount; index += 1) {
|
||||
coordinates[index * 4] = positions[index * 2];
|
||||
coordinates[index * 4 + 1] = positions[index * 2 + 1];
|
||||
}
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
function applyRepulsion(
|
||||
positions: Float64Array,
|
||||
displacements: Float64Array,
|
||||
sizes: Array<{ width: number; height: number }>,
|
||||
naturalDistance: number,
|
||||
) {
|
||||
const nodeCount = sizes.length;
|
||||
|
||||
for (let leftIndex = 0; leftIndex < nodeCount; leftIndex += 1) {
|
||||
const leftOffset = leftIndex * 2;
|
||||
|
||||
for (let rightIndex = leftIndex + 1; rightIndex < nodeCount; rightIndex += 1) {
|
||||
const rightOffset = rightIndex * 2;
|
||||
let dx = positions[leftOffset] - positions[rightOffset];
|
||||
let dy = positions[leftOffset + 1] - positions[rightOffset + 1];
|
||||
let distance = Math.hypot(dx, dy);
|
||||
|
||||
if (distance < 0.01) {
|
||||
const angle = seededAngle(`${leftIndex}:${rightIndex}:repel`);
|
||||
dx = Math.cos(angle);
|
||||
dy = Math.sin(angle);
|
||||
distance = 1;
|
||||
}
|
||||
|
||||
const sizeBias = Math.sqrt((sizes[leftIndex].width * sizes[leftIndex].height + sizes[rightIndex].width * sizes[rightIndex].height) / 2) / 130;
|
||||
const force = (naturalDistance * naturalDistance * (0.34 + sizeBias * 0.16)) / Math.max(1, distance);
|
||||
const fx = (dx / distance) * force;
|
||||
const fy = (dy / distance) * force;
|
||||
|
||||
displacements[leftOffset] += fx;
|
||||
displacements[leftOffset + 1] += fy;
|
||||
displacements[rightOffset] -= fx;
|
||||
displacements[rightOffset + 1] -= fy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyEdgeAttraction(
|
||||
positions: Float64Array,
|
||||
displacements: Float64Array,
|
||||
edgePairs: number[],
|
||||
graph: EpiphanyGraph,
|
||||
nodeDegrees: Map<string, number>,
|
||||
naturalDistance: number,
|
||||
) {
|
||||
for (let edgeIndex = 0; edgeIndex < edgePairs.length; edgeIndex += 2) {
|
||||
const sourceIndex = edgePairs[edgeIndex];
|
||||
const targetIndex = edgePairs[edgeIndex + 1];
|
||||
const sourceOffset = sourceIndex * 2;
|
||||
const targetOffset = targetIndex * 2;
|
||||
const dx = positions[targetOffset] - positions[sourceOffset];
|
||||
const dy = positions[targetOffset + 1] - positions[sourceOffset + 1];
|
||||
const distance = Math.max(1, Math.hypot(dx, dy));
|
||||
const sourceDegree = nodeDegrees.get(graph.nodes[sourceIndex].id) ?? 0;
|
||||
const targetDegree = nodeDegrees.get(graph.nodes[targetIndex].id) ?? 0;
|
||||
const desired = naturalDistance * (0.62 + Math.min(sourceDegree, targetDegree) * 0.012);
|
||||
const force = (distance - desired) * 0.085;
|
||||
const fx = (dx / distance) * force;
|
||||
const fy = (dy / distance) * force;
|
||||
|
||||
displacements[sourceOffset] += fx;
|
||||
displacements[sourceOffset + 1] += fy;
|
||||
displacements[targetOffset] -= fx;
|
||||
displacements[targetOffset + 1] -= fy;
|
||||
}
|
||||
}
|
||||
|
||||
function applySectionGravity(
|
||||
positions: Float64Array,
|
||||
displacements: Float64Array,
|
||||
graph: EpiphanyGraph,
|
||||
nodeDegrees: Map<string, number>,
|
||||
linkCounts: Map<string, number>,
|
||||
sectionCenters: Map<string, { x: number; y: number }>,
|
||||
centerX: number,
|
||||
centerY: number,
|
||||
maxDegree: number,
|
||||
) {
|
||||
for (let index = 0; index < graph.nodes.length; index += 1) {
|
||||
const node = graph.nodes[index];
|
||||
const offset = index * 2;
|
||||
const degree = nodeDegrees.get(node.id) ?? 0;
|
||||
const linkCount = linkCounts.get(node.id) ?? 0;
|
||||
const linkedPull = Math.min(1, linkCount / 10);
|
||||
const hubPull = Math.max(degree / maxDegree, linkedPull);
|
||||
const sectionCenter = sectionCenters.get(nodeSection(node.id)) ?? { x: centerX, y: centerY };
|
||||
const targetX = lerp(sectionCenter.x, centerX, hubPull * 0.95);
|
||||
const targetY = lerp(sectionCenter.y, centerY, hubPull * 0.95);
|
||||
const strength = 0.018 + hubPull * 0.09 + Math.min(linkCount, 8) * 0.003;
|
||||
|
||||
displacements[offset] += (targetX - positions[offset]) * strength;
|
||||
displacements[offset + 1] += (targetY - positions[offset + 1]) * strength;
|
||||
}
|
||||
}
|
||||
|
||||
function applyCollisionPush(
|
||||
positions: Float64Array,
|
||||
displacements: Float64Array,
|
||||
sizes: Array<{ width: number; height: number }>,
|
||||
nodes: EpiphanyGraph["nodes"],
|
||||
nodeDegrees: Map<string, number>,
|
||||
maxDegree: number,
|
||||
) {
|
||||
const nodeCount = sizes.length;
|
||||
|
||||
for (let leftIndex = 0; leftIndex < nodeCount; leftIndex += 1) {
|
||||
const leftOffset = leftIndex * 2;
|
||||
|
||||
for (let rightIndex = leftIndex + 1; rightIndex < nodeCount; rightIndex += 1) {
|
||||
const rightOffset = rightIndex * 2;
|
||||
const dx = positions[rightOffset] - positions[leftOffset];
|
||||
const dy = positions[rightOffset + 1] - positions[leftOffset + 1];
|
||||
const gapX = (sizes[leftIndex].width + sizes[rightIndex].width) / 2 + 44;
|
||||
const gapY = (sizes[leftIndex].height + sizes[rightIndex].height) / 2 + 34;
|
||||
const overlapX = gapX - Math.abs(dx);
|
||||
const overlapY = gapY - Math.abs(dy);
|
||||
|
||||
if (overlapX <= 0 || overlapY <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const leftDegree = nodeDegrees.get(nodes[leftIndex].id) ?? 0;
|
||||
const rightDegree = nodeDegrees.get(nodes[rightIndex].id) ?? 0;
|
||||
const leftWeight = 1 + (leftDegree / maxDegree) * 1.8;
|
||||
const rightWeight = 1 + (rightDegree / maxDegree) * 1.8;
|
||||
const totalWeight = leftWeight + rightWeight;
|
||||
|
||||
if (overlapX < overlapY) {
|
||||
const direction = dx >= 0 ? 1 : -1;
|
||||
const push = overlapX * 0.42;
|
||||
displacements[leftOffset] -= direction * push * (rightWeight / totalWeight);
|
||||
displacements[rightOffset] += direction * push * (leftWeight / totalWeight);
|
||||
} else {
|
||||
const direction = dy >= 0 ? 1 : -1;
|
||||
const push = overlapY * 0.42;
|
||||
displacements[leftOffset + 1] -= direction * push * (rightWeight / totalWeight);
|
||||
displacements[rightOffset + 1] += direction * push * (leftWeight / totalWeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyBoundaryGravity(
|
||||
positions: Float64Array,
|
||||
displacements: Float64Array,
|
||||
graphWidth: number,
|
||||
graphHeight: number,
|
||||
centerX: number,
|
||||
centerY: number,
|
||||
) {
|
||||
const nodeCount = positions.length / 2;
|
||||
const marginX = graphWidth * 0.1;
|
||||
const marginY = graphHeight * 0.1;
|
||||
|
||||
for (let index = 0; index < nodeCount; index += 1) {
|
||||
const offset = index * 2;
|
||||
const x = positions[offset];
|
||||
const y = positions[offset + 1];
|
||||
|
||||
displacements[offset] += (centerX - x) * 0.006;
|
||||
displacements[offset + 1] += (centerY - y) * 0.006;
|
||||
|
||||
if (x < marginX) {
|
||||
displacements[offset] += (marginX - x) * 0.08;
|
||||
} else if (x > graphWidth - marginX) {
|
||||
displacements[offset] -= (x - (graphWidth - marginX)) * 0.08;
|
||||
}
|
||||
|
||||
if (y < marginY) {
|
||||
displacements[offset + 1] += (marginY - y) * 0.08;
|
||||
} else if (y > graphHeight - marginY) {
|
||||
displacements[offset + 1] -= (y - (graphHeight - marginY)) * 0.08;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sortedNodeSections(graph: EpiphanyGraph) {
|
||||
return Array.from(new Set(graph.nodes.map((node) => nodeSection(node.id)))).sort((left, right) =>
|
||||
left.localeCompare(right),
|
||||
);
|
||||
}
|
||||
|
||||
function buildSectionCenters(
|
||||
sections: string[],
|
||||
centerX: number,
|
||||
centerY: number,
|
||||
graphWidth: number,
|
||||
graphHeight: number,
|
||||
) {
|
||||
const centers = new Map<string, { x: number; y: number }>();
|
||||
const radiusX = graphWidth * 0.27;
|
||||
const radiusY = graphHeight * 0.26;
|
||||
|
||||
sections.forEach((section, index) => {
|
||||
const angle = -Math.PI / 2 + (index / Math.max(1, sections.length)) * Math.PI * 2;
|
||||
centers.set(section, {
|
||||
x: centerX + Math.cos(angle) * radiusX,
|
||||
y: centerY + Math.sin(angle) * radiusY,
|
||||
});
|
||||
});
|
||||
|
||||
return centers;
|
||||
}
|
||||
|
||||
function nodeSection(nodeId: string) {
|
||||
if (nodeId.startsWith("section:")) {
|
||||
return nodeId.slice("section:".length);
|
||||
}
|
||||
|
||||
return nodeId.includes("/") ? nodeId.split("/")[0] : "Root";
|
||||
}
|
||||
|
||||
function seededAngle(seed: string) {
|
||||
return seededUnit(seed) * Math.PI * 2;
|
||||
}
|
||||
|
||||
function seededUnit(seed: string) {
|
||||
let hash = 2166136261;
|
||||
for (let index = 0; index < seed.length; index += 1) {
|
||||
hash ^= seed.charCodeAt(index);
|
||||
hash = Math.imul(hash, 16777619);
|
||||
}
|
||||
|
||||
return (hash >>> 0) / 4294967295;
|
||||
}
|
||||
|
||||
function lerp(start: number, end: number, amount: number) {
|
||||
return start + (end - start) * clamp(amount, 0, 1);
|
||||
}
|
||||
|
||||
function normalizeNodeBounds(nodes: PositionedNode[]): PositionedNode[] {
|
||||
const bounds = layoutBounds(nodes);
|
||||
if (bounds.minX >= 0 && bounds.minY >= 0) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue