|
| 1 | +import { ConcentricLayoutInputs } from 'layout/concentric2d'; |
| 2 | +import { buildNodeEdges } from './layoutUtils'; |
| 3 | +import { Vector3 } from 'three'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Generates a point on a Fibonacci sphere. |
| 7 | + * @param i |
| 8 | + * @param n |
| 9 | + * @param r |
| 10 | + */ |
| 11 | +function fibonacciSpherePoint(i: number, n: number, r: number) { |
| 12 | + const phi = Math.acos(1 - (2 * (i + 0.5)) / n); |
| 13 | + const theta = Math.PI * (1 + Math.sqrt(5)) * (i + 0.5); |
| 14 | + const x = r * Math.sin(phi) * Math.cos(theta); |
| 15 | + const y = r * Math.sin(phi) * Math.sin(theta); |
| 16 | + const z = r * Math.cos(phi); |
| 17 | + |
| 18 | + return new Vector3(x, y, z); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Concentric layout algorithm for 3D graphs. |
| 23 | + * @param graph |
| 24 | + * @param radius |
| 25 | + * @param drags |
| 26 | + * @param getNodePosition |
| 27 | + * @param concentricSpacing |
| 28 | + */ |
| 29 | +export function concentric3d({ |
| 30 | + graph, |
| 31 | + radius = 40, |
| 32 | + drags, |
| 33 | + getNodePosition, |
| 34 | + concentricSpacing = 100 |
| 35 | +}: ConcentricLayoutInputs) { |
| 36 | + const { nodes, edges } = buildNodeEdges(graph); |
| 37 | + |
| 38 | + const layout: Record<string, { x: number; y: number; z: number }> = {}; |
| 39 | + |
| 40 | + const getNodesInLevel = (level: number) => { |
| 41 | + const circumference = 2 * Math.PI * (radius + level * concentricSpacing); |
| 42 | + const minNodeSpacing = 40; |
| 43 | + return Math.floor(circumference / minNodeSpacing); |
| 44 | + }; |
| 45 | + |
| 46 | + const fixedLevelMap = new Map<number, string[]>(); |
| 47 | + const dynamicNodes: { id: string; metric: number }[] = []; |
| 48 | + |
| 49 | + // Split nodes: fixed-level and dynamic |
| 50 | + for (const node of nodes) { |
| 51 | + const data = graph.getNodeAttribute(node.id, 'data'); |
| 52 | + const level = data?.level; |
| 53 | + |
| 54 | + if (typeof level === 'number' && level >= 0) { |
| 55 | + if (!fixedLevelMap.has(level)) { |
| 56 | + fixedLevelMap.set(level, []); |
| 57 | + } |
| 58 | + fixedLevelMap.get(level)!.push(node.id); |
| 59 | + } else { |
| 60 | + dynamicNodes.push({ id: node.id, metric: graph.degree(node.id) }); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Sort dynamic nodes by degree |
| 65 | + dynamicNodes.sort((a, b) => b.metric - a.metric); |
| 66 | + |
| 67 | + // Fill layout for fixed-level nodes (3D spherical placement) |
| 68 | + for (const [level, nodeIds] of fixedLevelMap.entries()) { |
| 69 | + const count = nodeIds.length; |
| 70 | + const r = radius + level * concentricSpacing; |
| 71 | + |
| 72 | + for (const [i, id] of nodeIds.entries()) { |
| 73 | + const pos = fibonacciSpherePoint(i, count, r); |
| 74 | + layout[id] = { x: pos.x, y: pos.y, z: pos.z }; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Determine which levels are partially used and which are available |
| 79 | + const occupiedLevels = new Set(fixedLevelMap.keys()); |
| 80 | + let dynamicLevel = 0; |
| 81 | + |
| 82 | + let i = 0; |
| 83 | + while (i < dynamicNodes.length) { |
| 84 | + // Skip occupied levels |
| 85 | + while (occupiedLevels.has(dynamicLevel)) { |
| 86 | + dynamicLevel++; |
| 87 | + } |
| 88 | + |
| 89 | + const nodesInLevel = getNodesInLevel(dynamicLevel); |
| 90 | + const r = radius + dynamicLevel * concentricSpacing; |
| 91 | + |
| 92 | + for (let j = 0; j < nodesInLevel && i < dynamicNodes.length; j++) { |
| 93 | + const pos = fibonacciSpherePoint(j, nodesInLevel, r); |
| 94 | + layout[dynamicNodes[i].id] = { x: pos.x, y: pos.y, z: pos.z }; |
| 95 | + i++; |
| 96 | + } |
| 97 | + |
| 98 | + dynamicLevel++; |
| 99 | + } |
| 100 | + |
| 101 | + return { |
| 102 | + step() { |
| 103 | + return true; |
| 104 | + }, |
| 105 | + getNodePosition(id: string) { |
| 106 | + if (getNodePosition) { |
| 107 | + const pos = getNodePosition(id, { graph, drags, nodes, edges }); |
| 108 | + if (pos) { |
| 109 | + return pos; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (drags?.[id]?.position) { |
| 114 | + return drags[id].position as any; |
| 115 | + } |
| 116 | + |
| 117 | + return layout[id]; |
| 118 | + } |
| 119 | + }; |
| 120 | +} |
0 commit comments