import type { TopologyDataset } from "../topology/TopologyDatasetTransition.ts"; import { resolveTopologyLayoutConnectorRouting } from "../topology/sceneConfig.ts"; import type { TopologyLayoutMode, TopologySceneLayout } from "../topology/sceneConfig.ts"; import type { PresentationAnchorDefinition, PresentationChildDefinition, PresentationSlideDefinition, PresentationTerritoryDefinition, PresentationTerritoryLinkDefinition, FanDirection, SpiralDirection, } from "./deck.ts"; export const PRESENTATION_SLIDE_SEEDS: Record< string, { anchors: readonly PresentationAnchorDefinition[] } > = { "homepage-opening": { anchors: ["Dashboard", "Timeline", "Organization", "Knowledge", "Incidents", "Architecture"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: index === 0 ? "Opening" : "Context" }], })), }, "operational-dashboard": { anchors: ["Edge", "Delivery", "Access", "Application", "Data"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Frontend", "API", "Services", "Cache", "Database"][index] }], })), }, timeline: { anchors: ["Start", "Career", "Current"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Role", "Organization", "Milestone"][index] }], })), }, organization: { anchors: ["Services", "People", "Stats"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Platform", "Team", "Employees"][index] }], })), }, "personal-knowledge": { anchors: ["Identity", "Work", "Writing"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Profile", "Experience", "Journal"][index] }], })), }, "incident-response": { anchors: ["Detection", "Response", "Recovery"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Alert", "Action", "Resolution"][index] }], })), }, "system-architecture": { anchors: ["Edge", "Delivery", "Application", "Data", "Infrastructure", "External"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Ingress", "Routing", "Services", "Storage", "Runtime", "Dependencies"][index] }], })), }, "knowledge-graph": { anchors: ["People", "Organizations", "Topics"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Profiles", "Companies", "References"][index] }], })), }, "cloud-infrastructure": { anchors: ["Network", "Compute", "Storage"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Gateway", "Workers", "Database"][index] }], })), }, "ci-cd-pipeline": { anchors: ["Source", "Build", "Release"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Repository", "Checks", "Deploy"][index] }], })), }, }; const axialRing = [ { q: 0, r: -2 }, { q: 2, r: -2 }, { q: 2, r: 0 }, { q: 0, r: 2 }, { q: -2, r: 2 }, { q: -2, r: 0 }, ] as const; const sectorAxes = [ { main: { q: 0, r: -1 }, spread: { q: 1, r: 0 } }, { main: { q: 1, r: -1 }, spread: { q: 0, r: -1 } }, { main: { q: 1, r: 0 }, spread: { q: 1, r: -1 } }, { main: { q: 0, r: 1 }, spread: { q: 1, r: 0 } }, { main: { q: -1, r: 1 }, spread: { q: 0, r: 1 } }, { main: { q: -1, r: 0 }, spread: { q: -1, r: 1 } }, ] as const; function scaleAxial(vector: { q: number; r: number }, multiplier: number) { return { q: vector.q * multiplier, r: vector.r * multiplier }; } function addAxial(...vectors: Array<{ q: number; r: number }>) { return vectors.reduce( (acc, vector) => ({ q: acc.q + vector.q, r: acc.r + vector.r }), { q: 0, r: 0 }, ); } function areAxialEqual(a: { q: number; r: number }, b: { q: number; r: number }) { return a.q === b.q && a.r === b.r; } function rotateAxialClockwise( vector: { q: number; r: number }, turns: number, ) { let result = { q: vector.q, r: vector.r }; const normalizedTurns = ((turns % 6) + 6) % 6; for (let index = 0; index < normalizedTurns; index += 1) { result = { q: -result.r, r: result.q + result.r }; } return result; } function createCascadeTreeSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { interface CascadeTreeInputNode { id: string; label: string; subtitle?: string; variant: "root" | "anchor" | "entity"; size: "root" | "lg" | "md"; active: boolean; selected?: boolean; children: readonly CascadeTreeInputNode[]; } interface PositionedCascadeTreeNode { id: string; label: string; subtitle?: string; q: number; r: number; variant: "root" | "anchor" | "entity"; size: "root" | "lg" | "md"; active: boolean; selected?: boolean; anchorId?: string; } interface PositionedCascadeTreeBranch { nodes: PositionedCascadeTreeNode[]; relationships: Array<{ from: string; to: string; active: true }>; left: number; right: number; nextCursor: number; } const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const tree: CascadeTreeInputNode = { id: rootId, label: definition.title, subtitle: "Root", variant: "root", size: "root", active: true, selected: true, children: seed.anchors.map((anchor) => ({ id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", variant: "anchor", size: "lg", active: true, children: anchor.children.map((child) => ({ id: `presentation-${definition.id}-node-${anchor.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", variant: "entity", size: "md", active: true, children: [], })), })), }; function layoutBranch( node: CascadeTreeInputNode, depth: number, cursor: number, parentId?: string, ): PositionedCascadeTreeBranch { const depthStep = 3; const leafWidth = 2; const siblingGap = 2; if (node.children.length === 0) { const left = cursor; const right = cursor + leafWidth; const q = (left + right) / 2; const r = depth * depthStep; const positionedNode: PositionedCascadeTreeNode = { id: node.id, label: node.label, subtitle: node.subtitle, q, r, variant: node.variant, size: node.size, active: node.active, selected: node.selected, anchorId: parentId, }; return { nodes: [positionedNode], relationships: parentId ? [{ from: parentId, to: node.id, active: true as const }] : [], left, right, nextCursor: right + siblingGap, }; } const childBranches: PositionedCascadeTreeBranch[] = []; let nextCursor = cursor; for (const child of node.children) { const childBranch = layoutBranch(child, depth + 1, nextCursor, node.id); childBranches.push(childBranch); nextCursor = childBranch.nextCursor; } const left = childBranches[0]?.left ?? cursor; const right = childBranches[childBranches.length - 1]?.right ?? cursor + leafWidth; const q = (left + right) / 2; const r = depth * depthStep; const positionedNode: PositionedCascadeTreeNode = { id: node.id, label: node.label, subtitle: node.subtitle, q, r, variant: node.variant, size: node.size, active: node.active, selected: node.selected, anchorId: parentId, }; return { nodes: [positionedNode, ...childBranches.flatMap((branch) => branch.nodes)], relationships: [ ...(parentId ? [{ from: parentId, to: node.id, active: true as const }] : []), ...childBranches.flatMap((branch) => branch.relationships), ], left, right, nextCursor: right + siblingGap, }; } const laidOut = layoutBranch(tree, 0, 0); const root = laidOut.nodes[0]; const qOffset = -(root?.q ?? 0); const nodes = laidOut.nodes.map((node) => ({ ...node, q: node.q + qOffset, })); return { nodes, relationships: laidOut.relationships, }; } function createSwimlanesSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const laneSpacing = 4; const stepSpacing = 3; const laneOffsetStart = -Math.floor((seed.anchors.length - 1) / 2) * laneSpacing; const laneRoots = seed.anchors.map((anchor, index) => { const laneOffset = laneOffsetStart + index * laneSpacing; return { id: `presentation-${definition.id}-lane-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Lane", q: 0, r: laneOffset, variant: "root" as const, size: "root" as const, active: true, selected: index === 0, source: anchor, }; }); const laneSteps = laneRoots.flatMap((laneRoot) => { return laneRoot.source.children.map((child, childIndex) => ({ id: `presentation-${definition.id}-node-${laneRoot.source.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Step", q: laneRoot.q + (childIndex + 1) * stepSpacing, r: laneRoot.r, variant: "entity" as const, size: "md" as const, anchorId: laneRoot.id, })); }); const allNodes = [ ...laneRoots.map(({ source: _source, ...laneRoot }) => laneRoot), ...laneSteps, ]; const qOffset = -Math.round(Math.min(...allNodes.map((node) => node.q))); const rOffset = -Math.round((Math.min(...allNodes.map((node) => node.r)) + Math.max(...allNodes.map((node) => node.r))) / 2); const positionedLaneRoots = laneRoots.map(({ source: _source, ...laneRoot }) => ({ ...laneRoot, q: laneRoot.q + qOffset, r: laneRoot.r + rOffset, })); const positionedLaneSteps = laneSteps.map((step) => ({ ...step, q: step.q + qOffset, r: step.r + rOffset, })); const relationships = positionedLaneRoots.flatMap((laneRoot) => { const branch = positionedLaneSteps.filter((step) => step.anchorId === laneRoot.id); return branch.map((step, index) => ({ from: index === 0 ? laneRoot.id : branch[index - 1].id, to: step.id, active: true as const, })); }); return { nodes: [...positionedLaneRoots, ...positionedLaneSteps], relationships, }; } function createFanSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const root = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const fanDirection = definition.fanDirection ?? "E"; const fanSlotsByDirection: Record> = { E: [ { q: 1, r: -1 }, { q: 1, r: 0 }, { q: 0, r: 1 }, ], NE: [ { q: 1, r: 0 }, { q: 1, r: -1 }, { q: 0, r: -1 }, ], SE: [ { q: 1, r: 0 }, { q: 0, r: 1 }, { q: -1, r: 1 }, ], W: [ { q: -1, r: 1 }, { q: -1, r: 0 }, { q: 0, r: -1 }, ], NW: [ { q: 0, r: -1 }, { q: -1, r: 0 }, { q: -1, r: 1 }, ], SW: [ { q: -1, r: 0 }, { q: 0, r: 1 }, { q: 1, r: 0 }, ], }; const directionRotation: Record = { E: 0, SE: 1, SW: 2, W: 3, NW: 4, NE: 5, }; const slotOrder = seed.anchors.length === 1 ? [1] : seed.anchors.length === 2 ? [0, 2] : [0, 1, 2]; const anchors = seed.anchors.map((anchor, index) => { const slots = fanSlotsByDirection[fanDirection]; const slot = slotOrder[index % slotOrder.length]; const band = Math.floor(index / slotOrder.length); const vector = slots[slot] ?? slots[1]; const distance = 4 + band * 4; return { id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", q: vector.q * distance, r: vector.r * distance, variant: "anchor" as const, size: "lg" as const, active: true, source: anchor, }; }); const childStart = rotateAxialClockwise({ q: 2, r: -2 }, directionRotation[fanDirection]); const childStep = rotateAxialClockwise({ q: -2, r: 4 }, directionRotation[fanDirection]); const children = anchors.flatMap((anchor) => { return anchor.source.children.map((child, childIndex) => { const childOffset = addAxial( childStart, scaleAxial(childStep, childIndex), ); return { id: `presentation-${definition.id}-node-${anchor.source.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", q: anchor.q + childOffset.q, r: anchor.r + childOffset.r, variant: "entity" as const, size: "md" as const, anchorId: anchor.id, }; }); }); const anchorRelationships = anchors.map((anchor) => ({ from: rootId, to: anchor.id, active: true as const, })); const childRelationships = anchors.flatMap((anchor) => { const branch = children.filter((child) => child.anchorId === anchor.id); return definition.fanChildLinkMode === "direct" ? branch.map((child) => ({ from: anchor.id, to: child.id, active: true as const, })) : branch.map((child, index) => ({ from: index === 0 ? anchor.id : branch[index - 1].id, to: child.id, active: true as const, })); }); return { nodes: [root, ...anchors, ...children], relationships: [...anchorRelationships, ...childRelationships], }; } function createBasinSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const root = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; function rotateDirections(directions: readonly { q: number; r: number }[], offset: number) { const normalizedOffset = ((offset % directions.length) + directions.length) % directions.length; return [...directions.slice(normalizedOffset), ...directions.slice(0, normalizedOffset)]; } function createSpiralOffsets(count: number, orientationIndex = 0) { const positions: Array<{ q: number; r: number }> = []; const directions = rotateDirections( [ { q: 1, r: 0 }, { q: 1, r: -1 }, { q: 0, r: -1 }, { q: -1, r: 0 }, { q: -1, r: 1 }, { q: 0, r: 1 }, ], orientationIndex, ); for (let distance = 1; positions.length < count; distance += 1) { let coord = scaleAxial(directions[4], distance); for (const direction of directions) { for (let step = 0; step < distance; step += 1) { positions.push(coord); if (positions.length >= count) { return positions; } coord = addAxial(coord, direction); } } } return positions; } const largestBasinSize = Math.max(1, ...seed.anchors.map((anchor) => anchor.children.length)); const anchorDistance = Math.max(4, Math.ceil(Math.sqrt(largestBasinSize)) * 2); const anchorPositions = createSpiralOffsets(seed.anchors.length).map((offset) => scaleAxial(offset, anchorDistance)); const anchors = seed.anchors.map((anchor, index) => { const position = anchorPositions[index] ?? { q: 0, r: 0 }; return { id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", q: position.q, r: position.r, variant: "anchor" as const, size: "lg" as const, active: true, source: anchor, }; }); const children = anchors.flatMap((anchor, anchorIndex) => { const childPositions = createSpiralOffsets(anchor.source.children.length, anchorIndex); return anchor.source.children.map((child, childIndex) => { const offset = childPositions[childIndex] ?? { q: 0, r: 0 }; return { id: `presentation-${definition.id}-node-${anchor.source.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", q: anchor.q + offset.q, r: anchor.r + offset.r, variant: "entity" as const, size: "md" as const, anchorId: anchor.id, }; }); }); return { nodes: [root, ...anchors.map(({ source: _source, ...anchor }) => anchor), ...children], relationships: [ ...anchors.map((anchor) => ({ from: rootId, to: anchor.id, active: true as const, })), ...anchors.flatMap((anchor) => { const branch = children.filter((child) => child.anchorId === anchor.id); return branch.map((child) => ({ from: anchor.id, to: child.id, active: true as const, })); }), ], }; } function createMatrixSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const rootQ = matrixLayout.rootQ ?? 0; const rootR = matrixLayout.rootR ?? 0; const columnSpacing = 4; const rowSpacing = 2; const columnCount = seed.anchors.length; const root = { id: rootId, label: definition.title, subtitle: "Root", q: rootQ, r: rootR, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const rowTemplates = seed.anchors.flatMap((anchor) => anchor.children); const rowLabels = rowTemplates.reduce((rows, row) => { if (!rows.some((existing) => existing.id === row.id)) { rows.push(row); } return rows; }, []); const rowAnchors = rowLabels.map((row, index) => ({ id: `presentation-${definition.id}-row-${row.id}`, label: row.label, subtitle: row.subtitle ?? "Row", q: rootQ - 1 - index, r: rootR + rowSpacing + index * rowSpacing, variant: "anchor" as const, size: "lg" as const, active: true, source: row, })); const anchors = seed.anchors.map((anchor, index) => ({ id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Column", q: rootQ + columnSpacing + index * columnSpacing, r: rootR, variant: "anchor" as const, size: "lg" as const, active: true, source: anchor, })); const children = anchors.flatMap((anchor) => { return anchor.source.children.map((child, childIndex) => ({ id: `presentation-${definition.id}-node-${anchor.source.id}-${child.id}`, label: `${anchor.label} / ${child.label}`, subtitle: child.subtitle ?? "Cell", q: anchor.q - (childIndex + 1), r: rootR + rowSpacing + childIndex * rowSpacing, variant: "entity" as const, size: "md" as const, anchorId: anchor.id, })); }); return { nodes: [root, ...rowAnchors, ...anchors, ...children], relationships: [ ...rowAnchors.map((anchor) => ({ from: rootId, to: anchor.id, active: true as const, })), ...anchors.map((anchor) => ({ from: rootId, to: anchor.id, active: true as const, })), ...rowAnchors.flatMap((anchor) => { const branch = children.filter((child) => child.id.endsWith(`-${anchor.source.id}`)); return branch.map((child) => ({ from: anchor.id, to: child.id, active: true as const, })); }), ...anchors.flatMap((anchor) => { const branch = children.filter((child) => child.anchorId === anchor.id); return branch.map((child) => ({ from: anchor.id, to: child.id, active: true as const, })); }), ], }; } function createOrbitalClustersSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const root = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const anchorOrbit = axialRing.map((offset) => scaleAxial(offset, 2)); const childOrbit = axialRing.map((offset) => scaleAxial(offset, 1)); const anchors = seed.anchors.map((anchor, index) => { const band = Math.floor(index / anchorOrbit.length); const orbit = anchorOrbit[index % anchorOrbit.length]; const position = scaleAxial(orbit, band + 1); return { id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", q: position.q, r: position.r, variant: "anchor" as const, size: "lg" as const, active: true, source: anchor, }; }); const children = anchors.flatMap((anchor) => { return anchor.source.children.map((child, childIndex) => { const orbitBand = Math.floor(childIndex / childOrbit.length); const orbit = childOrbit[childIndex % childOrbit.length]; const localOffset = scaleAxial(orbit, orbitBand + 1); return { id: `presentation-${definition.id}-node-${anchor.source.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", q: anchor.q + localOffset.q, r: anchor.r + localOffset.r, variant: "entity" as const, size: "md" as const, anchorId: anchor.id, }; }); }); return { nodes: [root, ...anchors.map(({ source: _source, ...anchor }) => anchor), ...children], relationships: [ ...anchors.map((anchor, index) => ({ from: index === 0 ? rootId : anchors[index - 1].id, to: anchor.id, active: true as const, })), ...anchors.flatMap((anchor) => { const branch = children.filter((child) => child.anchorId === anchor.id); return branch.map((child) => ({ from: anchor.id, to: child.id, active: true as const, })); }), ], }; } function createSpiralSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const root = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; type SpiralSequenceItem = { id: string; label: string; subtitle?: string; kind: "anchor" | "entity"; anchorId: string; }; const sequence: SpiralSequenceItem[] = []; for (const anchor of seed.anchors) { const anchorId = `presentation-${definition.id}-anchor-${anchor.id}`; sequence.push({ id: anchorId, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", kind: "anchor", anchorId, }); for (const child of anchor.children) { sequence.push({ id: `presentation-${definition.id}-node-${anchor.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", kind: "entity", anchorId, }); } } const spiralDirection = definition.spiralDirection ?? "W"; const spiralDirectionOffset: Record = { W: 0, NW: 1, NE: 2, E: 3, SE: 4, SW: 5, }; const spiralRingAdvance: Record = { W: { q: -1, r: 1 }, NW: { q: -1, r: 0 }, NE: { q: 0, r: -1 }, E: { q: 1, r: -1 }, SE: { q: 1, r: 0 }, SW: { q: 0, r: 1 }, }; const spiralDirections = [ { q: 1, r: 0 }, { q: 1, r: -1 }, { q: 0, r: -1 }, { q: -1, r: 0 }, { q: -1, r: 1 }, { q: 0, r: 1 }, ] as const; const spiralPositions: Array<{ q: number; r: number }> = []; let previousRingExit: { q: number; r: number } | undefined; function findNearestRingIndex( ringPositions: Array<{ q: number; r: number }>, target: { q: number; r: number }, ): number { const exactIndex = ringPositions.findIndex((ringPosition) => areAxialEqual(ringPosition, target)); if (exactIndex >= 0) { return exactIndex; } let bestIndex = 0; let bestDistance = Number.POSITIVE_INFINITY; for (let index = 0; index < ringPositions.length; index += 1) { const ringPosition = ringPositions[index]; const deltaQ = ringPosition.q - target.q; const deltaR = ringPosition.r - target.r; const deltaS = -deltaQ - deltaR; const distance = Math.max(Math.abs(deltaQ), Math.abs(deltaR), Math.abs(deltaS)); if (distance < bestDistance) { bestDistance = distance; bestIndex = index; } } return bestIndex; } for (let ringIndex = 1; spiralPositions.length < sequence.length; ringIndex += 1) { const ringPositions: Array<{ q: number; r: number }> = []; let position = scaleAxial({ q: -1, r: 1 }, ringIndex * 2); for (const direction of spiralDirections) { for (let step = 0; step < ringIndex; step += 1) { ringPositions.push(position); position = addAxial(position, scaleAxial(direction, 2)); } } const ringStart = previousRingExit ? addAxial(previousRingExit, scaleAxial(spiralRingAdvance[spiralDirection], 2)) : ringPositions[spiralDirectionOffset[spiralDirection] % ringPositions.length] ?? ringPositions[0]; const startIndex = findNearestRingIndex(ringPositions, ringStart); const orderedRingPositions = startIndex === 0 ? ringPositions : [...ringPositions.slice(startIndex), ...ringPositions.slice(0, startIndex)]; for (const ringPosition of orderedRingPositions) { if (spiralPositions.length >= sequence.length) { break; } spiralPositions.push(ringPosition); previousRingExit = ringPosition; } } const positionedSequence = sequence.map((item, index) => { const position = spiralPositions[index] ?? { q: 0, r: 0 }; return { id: item.id, label: item.label, subtitle: item.subtitle, q: position.q, r: position.r, variant: item.kind, size: item.kind === "anchor" ? ("lg" as const) : ("md" as const), active: true, anchorId: item.kind === "entity" ? item.anchorId : undefined, }; }); const relationships = [rootId, ...positionedSequence.map((item) => item.id)].map((id, index, all) => { if (index === 0) return null; return { from: all[index - 1], to: id, active: true as const, }; }).filter((relationship): relationship is { from: string; to: string; active: true } => Boolean(relationship)); return { nodes: [root, ...positionedSequence], relationships, }; } function createLinearSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const rawRoot = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const rawAnchors = seed.anchors.map((anchor, index) => ({ id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", q: (index + 1) * 3, r: 0, variant: "anchor" as const, size: "lg" as const, active: true, source: anchor, })); const rawChildren = rawAnchors.flatMap((anchor) => anchor.source.children.map((child, childIndex) => ({ id: `presentation-${definition.id}-node-${anchor.source.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", q: anchor.q, r: (childIndex + 1) * 2, variant: "entity" as const, size: "md" as const, anchorId: anchor.id, }))); const allPositions = [rawRoot, ...rawAnchors, ...rawChildren]; const qOffset = -Math.round((Math.min(...allPositions.map((node) => node.q)) + Math.max(...allPositions.map((node) => node.q))) / 2); const rOffset = -Math.round((Math.min(...allPositions.map((node) => node.r)) + Math.max(...allPositions.map((node) => node.r))) / 2); const root = { ...rawRoot, q: rawRoot.q + qOffset, r: rawRoot.r + rOffset }; const anchors = rawAnchors.map(({ source: _source, ...anchor }) => ({ ...anchor, q: anchor.q + qOffset, r: anchor.r + rOffset, })); const children = rawChildren.map((child) => ({ ...child, q: child.q + qOffset, r: child.r + rOffset, })); const anchorRelationships = anchors.map((anchor, index) => ({ from: index === 0 ? rootId : anchors[index - 1].id, to: anchor.id, active: true, })); const childRelationships = anchors.flatMap((anchor) => { const branch = children.filter((child) => child.anchorId === anchor.id); return branch.map((child, index) => ({ from: index === 0 ? anchor.id : branch[index - 1].id, to: child.id, active: true, })); }); return { nodes: [root, ...anchors, ...children], relationships: [...anchorRelationships, ...childRelationships], }; } function createFishspineSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const rawRoot = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const rawAnchors = seed.anchors.map((anchor, index) => ({ id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", q: (index + 1) * 2, r: 0, variant: "anchor" as const, size: "lg" as const, active: true, source: anchor, })); const rawChildren = rawAnchors.flatMap((anchor, anchorIndex) => anchor.source.children.map((child, childIndex) => ({ id: `presentation-${definition.id}-node-${anchor.source.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", q: anchor.q + (anchorIndex % 2 === 0 ? 0 : (childIndex + 1) * 2), r: anchorIndex % 2 === 0 ? 2 : -2, variant: "entity" as const, size: "md" as const, anchorId: anchor.id, }))); const allPositions = [rawRoot, ...rawAnchors, ...rawChildren]; const qOffset = -Math.round((Math.min(...allPositions.map((node) => node.q)) + Math.max(...allPositions.map((node) => node.q))) / 2); const rOffset = -Math.round((Math.min(...allPositions.map((node) => node.r)) + Math.max(...allPositions.map((node) => node.r))) / 2); const root = { ...rawRoot, q: rawRoot.q + qOffset, r: rawRoot.r + rOffset }; const anchors = rawAnchors.map(({ source: _source, ...anchor }) => ({ ...anchor, q: anchor.q + qOffset, r: anchor.r + rOffset, })); const children = rawChildren.map((child) => ({ ...child, q: child.q + qOffset, r: child.r + rOffset, })); const anchorRelationships = anchors.map((anchor, index) => ({ from: index === 0 ? rootId : anchors[index - 1].id, to: anchor.id, active: true, })); const childRelationships = anchors.flatMap((anchor) => { const branch = children.filter((child) => child.anchorId === anchor.id); return branch.map((child, index) => ({ from: index === 0 ? anchor.id : branch[index - 1].id, to: child.id, active: true, })); }); return { nodes: [root, ...anchors, ...children], relationships: [...anchorRelationships, ...childRelationships], }; } function createRadialSectorsSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const root = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const anchors = seed.anchors.map((anchor, index) => { const sector = sectorAxes[index % sectorAxes.length]; const band = Math.floor(index / sectorAxes.length); const distance = 5 + band * 4; const position = scaleAxial(sector.main, distance); return { id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", q: position.q, r: position.r, variant: "anchor" as const, size: "lg" as const, active: true, source: anchor, sectorIndex: index % sectorAxes.length, }; }); const children = anchors.flatMap((anchor) => { const sector = sectorAxes[anchor.sectorIndex]; const localRing = axialRing.map((offset) => scaleAxial(offset, 2)); const incomingOffset = scaleAxial( { q: -sector.main.q, r: -sector.main.r }, 2, ); const orderedRing = [ ...localRing.filter((offset) => !areAxialEqual(offset, incomingOffset)), incomingOffset, ]; return anchor.source.children.map((child, childIndex) => { const ringBand = Math.floor(childIndex / orderedRing.length); const ringOffset = orderedRing[childIndex % orderedRing.length]; const outwardOffset = scaleAxial(sector.main, 2 + ringBand * 2); const position = addAxial( { q: anchor.q, r: anchor.r }, ringOffset, outwardOffset, ); return { id: `presentation-${definition.id}-node-${anchor.source.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", q: position.q, r: position.r, variant: "entity" as const, size: "md" as const, anchorId: anchor.id, }; }); }); return { nodes: [root, ...anchors.map(({ source: _source, sectorIndex: _sectorIndex, ...anchor }) => anchor), ...children], relationships: [ ...anchors.map((anchor, index) => ({ from: index === 0 ? rootId : anchors[index - 1].id, to: anchor.id, active: true, })), ...anchors.flatMap((anchor) => { const branch = children.filter((child) => child.anchorId === anchor.id); return branch.map((child, index) => ({ from: index === 0 ? anchor.id : branch[index - 1].id, to: child.id, active: true, })); }), ], }; } function createPairedComparisonSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const comparison = definition.comparison ?? { left: { id: "left", label: "Left", subtitle: "Comparison world", topologyLayout: "linear", anchors: seed.anchors.slice(0, Math.max(1, Math.ceil(seed.anchors.length / 2))), }, right: { id: "right", label: "Right", subtitle: "Comparison world", topologyLayout: "linear", anchors: seed.anchors.slice(Math.max(1, Math.ceil(seed.anchors.length / 2))), }, links: [], }; function buildWorld( world: NonNullable["left"], suffix: "left" | "right", ) { return createPresentationTopology({ ...definition, id: definition.id + "-" + suffix, title: world.label, subtitle: world.subtitle ?? definition.subtitle, topologyLayout: world.topologyLayout ?? "linear", comparison: undefined, anchors: world.anchors, }); } function getBounds(nodes: Array<{ q: number; r: number }>) { const minQ = Math.min(...nodes.map((node) => node.q)); const maxQ = Math.max(...nodes.map((node) => node.q)); const minR = Math.min(...nodes.map((node) => node.r)); const maxR = Math.max(...nodes.map((node) => node.r)); return { minQ, maxQ, minR, maxR, width: maxQ - minQ, height: maxR - minR, centerQ: (minQ + maxQ) / 2, centerR: (minR + maxR) / 2, }; } function shiftTopology(topology: TopologyDataset, qOffset: number, rOffset: number, boundaryId?: string): TopologyDataset { return { ...topology, nodes: topology.nodes.map((node) => ({ ...node, q: node.q + qOffset, r: node.r + rOffset, boundaryId: node.boundaryId ?? boundaryId, })), relationships: topology.relationships ?? [], }; } const leftTopology = buildWorld(comparison.left, "left"); const rightTopology = buildWorld(comparison.right, "right"); const leftBounds = getBounds(leftTopology.nodes); const rightBounds = getBounds(rightTopology.nodes); const separation = Math.max(18, Math.ceil((leftBounds.width + rightBounds.width) / 2) + 8); const positionedLeft = shiftTopology( leftTopology, Math.round(-separation / 2 - leftBounds.centerQ), Math.round(-leftBounds.centerR), comparison.left.id, ); const positionedRight = shiftTopology( rightTopology, Math.round(separation / 2 - rightBounds.centerQ), Math.round(-rightBounds.centerR), comparison.right.id, ); const links = (comparison.links ?? []).flatMap((link) => { const state = link.state ?? "related"; const edge = { from: link.fromNode, to: link.toNode, state, muted: state === "related", active: state === "active", disabled: state === "disabled", } as const; return [edge]; }); return { nodes: [...positionedLeft.nodes, ...positionedRight.nodes], relationships: [...(positionedLeft.relationships ?? []), ...(positionedRight.relationships ?? []), ...links], }; } function createTerritoriesSlideTopology( definition: PresentationSlideDefinition, seed: { anchors: readonly PresentationAnchorDefinition[] }, ): TopologyDataset { const territoryDefinitions = (definition.territories?.length ? definition.territories : [{ id: "territory", label: definition.title, subtitle: definition.subtitle, topologyLayout: "ring", anchors: seed.anchors, }]) as readonly PresentationTerritoryDefinition[]; function createTerritoryTopology(territory: PresentationTerritoryDefinition, index: number) { const localDefinition = createPresentationTopology({ ...definition, id: definition.id + "-" + territory.id, title: territory.label, subtitle: territory.subtitle ?? definition.subtitle, topologyLayout: territory.topologyLayout ?? "ring", anchors: territory.anchors, territories: [], territoryLinks: [], }); const localRoot = localDefinition.nodes.find((node) => node.variant === "root") ?? localDefinition.nodes[0]; const ring = axialRing.map((offset) => scaleAxial(offset, 1)); const band = Math.floor(index / ring.length); const placement = scaleAxial(ring[index % ring.length] ?? { q: 0, r: 0 }, 8 + band * 8); const qShift = placement.q - (localRoot?.q ?? 0); const rShift = placement.r - (localRoot?.r ?? 0); return { rootId: localRoot?.id ?? ("presentation-" + definition.id + "-" + territory.id + "-root"), nodes: localDefinition.nodes.map((node) => ({ ...node, q: node.q + qShift, r: node.r + rShift, variant: node.id === localRoot?.id ? ("cluster" as const) : node.variant, size: node.id === localRoot?.id ? ("lg" as const) : node.size, boundaryId: territory.id, })), relationships: localDefinition.relationships ?? [], }; } const territoryTopologies = territoryDefinitions.map((territory, index) => createTerritoryTopology(territory, index)); const worldRoot = { id: "presentation-" + definition.id + "-root", label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const maxSpan = Math.max( 1, ...territoryTopologies.map((topology) => { const minQ = Math.min(...topology.nodes.map((node) => node.q)); const maxQ = Math.max(...topology.nodes.map((node) => node.q)); const minR = Math.min(...topology.nodes.map((node) => node.r)); const maxR = Math.max(...topology.nodes.map((node) => node.r)); return Math.max(maxQ - minQ, maxR - minR); }), ); const spacing = Math.max(6, Math.ceil(maxSpan / 6) + Math.ceil(territoryDefinitions.length / 4) + 2); const positionedTerritories = territoryTopologies.map((topology, index) => { const band = Math.floor(index / axialRing.length); const offset = scaleAxial(axialRing[index % axialRing.length] ?? { q: 0, r: 0 }, spacing * (band + 1)); const root = topology.nodes.find((node) => node.id === topology.rootId) ?? topology.nodes[0]; const qShift = offset.q - (root?.q ?? 0); const rShift = offset.r - (root?.r ?? 0); return { rootId: topology.rootId, nodes: topology.nodes.map((node) => ({ ...node, q: node.q + qShift, r: node.r + rShift, boundaryId: node.boundaryId, })), relationships: topology.relationships, }; }); const territoryRootIds = new Map(territoryDefinitions.map((territory, index) => [territory.id, positionedTerritories[index]?.rootId ?? ""])); const territoryLinks = (definition.territoryLinks ?? []).flatMap((link: PresentationTerritoryLinkDefinition) => { const fromId = territoryRootIds.get(link.fromTerritory); const toId = territoryRootIds.get(link.toTerritory); if (!fromId || !toId) return []; return [{ from: fromId, to: toId, kind: "cross-territory", state: link.state ?? "related", muted: (link.state ?? "related") === "related", active: link.state === "active", disabled: link.state === "disabled", }]; }); return { nodes: [ worldRoot, ...positionedTerritories.flatMap((territory) => territory.nodes), ], relationships: [ ...positionedTerritories.map((territory) => ({ from: worldRoot.id, to: territory.rootId, active: true as const, })), ...positionedTerritories.flatMap((territory) => territory.relationships ?? []), ...territoryLinks, ], }; } export function createPresentationTopology(definition: PresentationSlideDefinition): TopologyDataset { const catalogSeed = PRESENTATION_SLIDE_SEEDS[definition.id] ?? { anchors: ["Context", "Relationships", "Content"].map((label, index) => ({ id: label.toLowerCase(), label, children: [{ id: `child-${index}`, label: ["Overview", "Related", "Details"][index] }], })), }; const seed = definition.anchors.length > 0 ? definition : { ...definition, anchors: catalogSeed.anchors }; const connectorRouting = definition.connectorRouting ?? resolveTopologyLayoutConnectorRouting(definition.topologyLayout ?? "ring"); if (definition.topologyLayout === "linear") { return { ...createLinearSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "fishspine") { return { ...createFishspineSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "radial-sectors") { return { ...createRadialSectorsSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "fan") { return { ...createFanSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "matrix") { return { ...createMatrixSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "orbital-clusters") { return { ...createOrbitalClustersSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "spiral") { return { ...createSpiralSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "cascade-tree") { return { ...createCascadeTreeSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "swimlanes") { return { ...createSwimlanesSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "basin") { return { ...createBasinSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "paired-comparison") { return { ...createPairedComparisonSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } if (definition.topologyLayout === "territories") { return { ...createTerritoriesSlideTopology(definition, seed), connectorRouting, boundary: definition.boundary ?? null }; } const rootId = `presentation-${definition.id}-root`; const matrixLayout = definition.matrixLayout ?? {}; const columnSpacing = matrixLayout.columnSpacing ?? 4; const rowSpacing = matrixLayout.rowSpacing ?? 2; const columnOriginR = matrixLayout.columnOriginR ?? -2; const rowOriginR = matrixLayout.rowOriginR ?? 0; const rootQOffset = matrixLayout.rootQOffset ?? -4; const rootROffset = matrixLayout.rootROffset ?? 0; const root = { id: rootId, label: definition.title, subtitle: "Root", q: 0, r: 0, variant: "root" as const, size: "root" as const, active: true, selected: true, }; const anchors = seed.anchors.map((anchor, index) => { const position = axialRing[index % axialRing.length]; return { id: `presentation-${definition.id}-anchor-${anchor.id}`, label: anchor.label, subtitle: anchor.subtitle ?? "Anchor", q: position.q, r: position.r, variant: "anchor" as const, size: "lg" as const, active: true, }; }); const children = seed.anchors.flatMap((anchor, anchorIndex) => { const position = axialRing[anchorIndex % axialRing.length]; return anchor.children.map((child, childIndex) => ({ id: `presentation-${definition.id}-node-${anchor.id}-${child.id}`, label: child.label, subtitle: child.subtitle ?? "Node", q: position.q + Math.sign(position.q) * (childIndex + 1), r: position.r + Math.sign(position.r) * (childIndex + 1), variant: "entity" as const, size: "md" as const, anchorId: `presentation-${definition.id}-anchor-${anchor.id}`, })); }); return { nodes: [root, ...anchors, ...children], relationships: anchors.flatMap((anchor) => [ { from: rootId, to: anchor.id, active: true }, ...children .filter((child) => child.anchorId === anchor.id) .map((child) => ({ from: anchor.id, to: child.id, active: true })), ]), connectorRouting, boundary: definition.boundary ?? null, }; } export type { PresentationAnchorDefinition, PresentationChildDefinition, PresentationSlideDefinition, }; export type LayoutPresentationScene = TopologySceneLayout; export type LayoutTopologyMode = TopologyLayoutMode;