using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; namespace PamhagenSysCtrl.Helpers.Pipeline { public class Graph { public HashSet Nodes = []; public HashSet 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? 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 Update(double x, double y) { HashSet 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; } } }