73 lines
2.2 KiB
C#
73 lines
2.2 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 Path? GetPath(INode start, INode end, Func<INode, bool>? valid = null) {
|
|
if (!Nodes.Contains(start) || !Nodes.Contains(end)) throw new ArgumentException("Start/end node not contained in graph");
|
|
valid ??= a => true;
|
|
List<(IEdge, INode)> hops = [];
|
|
Path? path = null;
|
|
foreach (var o in start.Outputs) {
|
|
if (!valid(o.End)) {
|
|
continue;
|
|
} else if (o.End == end) {
|
|
return new(start, [(o, o.End)]);
|
|
}
|
|
|
|
var p = GetPath(o.End, end, valid);
|
|
if (p != null && (path == null || p.Value.Hops.Count() + 1 < path.Value.Hops.Count())) {
|
|
path = new(start, [(o, o.End), ..p.Value.Hops]);
|
|
}
|
|
}
|
|
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> Update(double x, double y) {
|
|
HashSet<INode> hover = new();
|
|
foreach (var e in Edges) {
|
|
e.Update(x, y);
|
|
}
|
|
foreach (var n in Nodes) {
|
|
if (n.Update(x, y)) {
|
|
hover.Add(n);
|
|
}
|
|
}
|
|
return hover;
|
|
}
|
|
}
|
|
}
|