Browse Source

move force to top-level

master
limina1 8 months ago
parent
commit
205b8e74be
  1. 54
      src/lib/navigator/EventNetwork/utils/starForceSimulation.ts

54
src/lib/navigator/EventNetwork/utils/starForceSimulation.ts

@ -112,26 +112,28 @@ export function createStarSimulation( @@ -112,26 +112,28 @@ export function createStarSimulation(
}
/**
* Creates a custom radial force that keeps content nodes in orbit around their star center
* Applies the radial force to keep content nodes in orbit around their star center
* @param nodes - The array of network nodes
* @param nodeToCenter - Map of content node IDs to their star center node
* @param targetDistance - The desired distance from center to content node
* @param alpha - The current simulation alpha
*/
function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any {
// Build a map of content nodes to their star centers
const nodeToCenter = new Map<string, NetworkNode>();
links.forEach(link => {
const source = link.source as NetworkNode;
const target = link.target as NetworkNode;
if (source.kind === 30040 && target.kind === 30041) {
nodeToCenter.set(target.id, source);
}
});
// Custom force function
function force(alpha: number) {
function applyRadialForce(
nodes: NetworkNode[],
nodeToCenter: Map<string, NetworkNode>,
targetDistance: number,
alpha: number
): void {
nodes.forEach(node => {
if (node.kind === 30041) {
const center = nodeToCenter.get(node.id);
if (center && center.x != null && center.y != null && node.x != null && node.y != null) {
if (
center &&
center.x != null &&
center.y != null &&
node.x != null &&
node.y != null
) {
// Calculate desired position
const dx = node.x - center.x;
const dy = node.y - center.y;
@ -139,7 +141,6 @@ function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any { @@ -139,7 +141,6 @@ function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any {
if (distance > 0) {
// Normalize and apply force
const targetDistance = STAR_LINK_DISTANCE;
const force = (distance - targetDistance) * alpha * 0.3; // Reduced force
node.vx = (node.vx || 0) - (dx / distance) * force;
node.vy = (node.vy || 0) - (dy / distance) * force;
@ -147,6 +148,25 @@ function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any { @@ -147,6 +148,25 @@ function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any {
}
}
});
}
/**
* Creates a custom radial force that keeps content nodes in orbit around their star center
*/
function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any {
// Build a map of content nodes to their star centers
const nodeToCenter = new Map<string, NetworkNode>();
links.forEach(link => {
const source = link.source as NetworkNode;
const target = link.target as NetworkNode;
if (source.kind === 30040 && target.kind === 30041) {
nodeToCenter.set(target.id, source);
}
});
function force(alpha: number) {
applyRadialForce(nodes, nodeToCenter, STAR_LINK_DISTANCE, alpha);
}
force.initialize = function(_: NetworkNode[]) {

Loading…
Cancel
Save