Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/Layout/ForceDirectedLayouter.cs

82 lines
3.2 KiB
C#
Raw Normal View History

2023-12-21 22:20:38 +08:00
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
namespace AIStudio.Wpf.DiagramDesigner.Layout
{
/// <summary>
/// Reposition the nodes in a network using a physics-based approach.
/// The nodes are interpreted as point masses, and the connections are represented
/// by springs. This system, along with a few additional forces such as friction and a
/// horizontal force, is then simulated to calculate the new position of the nodes.
/// </summary>
public class ForceDirectedLayouter
{
/// <summary>
/// Layout the nodes in the network.
/// </summary>
/// <param name="config">The configuration to use.</param>
/// <param name="maxIterations">The maximum amount of iterations after which the physics simulation ends.</param>
public void Layout(Configuration config, int maxIterations)
{
var engine = new Engine();
var state = new BufferedState();
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
// Move each node so no two nodes have the exact same position.
engine.ApplyRandomShift(config.Network);
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
int deltaT = (int)Math.Ceiling(10.0 / (double)config.UpdatesPerIteration);
for (int i = 0; i < maxIterations * config.UpdatesPerIteration; i++)
{
engine.Update(deltaT, state, config);
}
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
foreach (var newNodePosition in state.NodePositions)
{
newNodePosition.Key.Position = new Point(newNodePosition.Value.X, newNodePosition.Value.Y);
}
}
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
/// <summary>
/// Layout the nodes in the network, updating the user interface at each iteration.
/// This method, contrary to Layout(), lets users see the simulation as it happens.
/// The cancellation token should be used to end the simulation.
/// </summary>
/// <param name="config">The configuration to use.</param>
/// <param name="token">A cancellation token to end the layout process.</param>
/// <returns>The async task</returns>
public async Task LayoutAsync(Configuration config, CancellationToken token)
{
var engine = new Engine();
var state = new LiveState();
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
// Move each node so no two nodes have the exact same position.
engine.ApplyRandomShift(config.Network);
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
DateTime start = DateTime.Now;
TimeSpan t = TimeSpan.Zero;
do
{
// Current real time
var newT = DateTime.Now - start;
var deltaT = newT - t;
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
// Current modified time
//int virtT = (int)(t.Milliseconds * Settings.TimeModifier);
int virtDeltaT = (int)(deltaT.Milliseconds * config.TimeModifier);
int virtDeltaTPerUpdate = virtDeltaT / config.UpdatesPerIteration;
for (int i = 0; i < config.UpdatesPerIteration; i++)
{
// Modified time in this update step
engine.Update(virtDeltaTPerUpdate, state, config);
}
2023-12-21 19:51:50 +08:00
2023-12-21 22:20:38 +08:00
t = newT;
await Task.Delay(14, token);
} while (!token.IsCancellationRequested);
}
}
}