Files
pamhagen-sysctrl/PamhagenSysCtrl/Helpers/Pipeline/Graph.cs
T
2026-07-15 00:06:26 +02:00

123 lines
4.1 KiB
C#

using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class Graph {
public HashSet<INode> Nodes = [];
public HashSet<IEdge> Edges = [];
public Graph() {
}
public Pipe CreatePipe(INode start, INode end, bool verticalFirst = false) {
Nodes.Add(start);
Nodes.Add(end);
var p = new Pipe(start, end, verticalFirst);
start.Outputs.Add(p);
end.Inputs.Add(p);
Edges.Add(p);
return p;
}
public Pipe CreateTwoWayPipe(INode n1, INode n2, bool verticalFirst = false) {
Nodes.Add(n1);
Nodes.Add(n2);
var p = new Pipe(n1, n2, verticalFirst, twoWay: true);
n1.Inputs.Add(p);
n1.Outputs.Add(p);
n2.Inputs.Add(p);
n2.Outputs.Add(p);
Edges.Add(p);
return p;
}
public Inlet CreateInlet(INode start, ISink end) {
Nodes.Add(start);
Nodes.Add(end);
var p = new Inlet(start, end, start.CenterX > end.CenterX);
start.Outputs.Add(p);
end.Inputs.Add(p);
Edges.Add(p);
return p;
}
public Outlet CreateOutlet(ISource start, INode end) {
Nodes.Add(start);
Nodes.Add(end);
var p = new Outlet(start, end);
start.Outputs.Add(p);
end.Inputs.Add(p);
Edges.Add(p);
return p;
}
public Path? GetPath(IEnumerable<INode> points, Func<INode, bool>? valid = null) {
if (points.Count() < 2) throw new ArgumentException("Path is too short");
var start = points.First();
var path = new Path(start, []);
foreach (var end in points.Skip(1)) {
if (GetPath(start, end, valid, visited: [..path.Hops.Select(h => h.Node)]) is not Path p)
return null;
path.Hops = [..path.Hops, ..p.Hops];
start = end;
}
return path;
}
public Path? GetPath(INode start, INode end, Func<INode, bool>? valid = null, HashSet<INode>? visited = null) {
if (!Nodes.Contains(start) || !Nodes.Contains(end)) throw new ArgumentException("Start/end node not contained in graph");
valid ??= a => true;
visited = [.. visited ?? [], start];
List<(IEdge, INode)> hops = [];
Path? path = null;
foreach (var o in start.Outputs) {
var e = o.IsTwoWay && o.End == start ? o.Start : o.End;
if (e == end) {
return new(start, [(o, e)]);
} else if (!valid(e) || visited.Contains(e)) {
continue;
}
var p = GetPath(e, end, valid, visited);
if (p != null) {
var p2 = new Path(start, [(o, e), .. p.Value.Hops]);
if (path == null || p2.Bends < path.Value.Bends || (p2.Bends == path.Value.Bends && p2.Length < path.Value.Length)) {
path = p2;
}
}
}
return path != null ? new(start, path.Value.Hops) : null;
}
public void Draw(Canvas canvas) {
canvas.Children.Add(new Rectangle() {
Fill = Brushes.White,
Width = 5000,
Height = 5000,
});
foreach (var e in Edges) {
e.Draw(canvas);
}
foreach (var n in Nodes) {
n.Draw(canvas);
}
}
public ISet<INode> GetHover(double x, double y) {
return Nodes.Where(n => n.IsInside(x, y)).ToHashSet();
}
public void Update(ISet<INode> hovering, bool isPressed) {
foreach (var e in Edges) {
e.Update();
}
foreach (var n in Nodes) {
n.Update(hovering.Contains(n), isPressed);
}
}
}
}