using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Media3D; 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, string? label = null, double? labelOffsetX = null) { Nodes.Add(start); Nodes.Add(end); var p = new Pipe(start, end, label, verticalFirst, default, labelOffsetX); 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, default, 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, bool sideways = false) { Nodes.Add(start); Nodes.Add(end); var p = new Inlet(start, end, sideways); 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 points, Func? valid = null, Func? cost = null) { if (points.Count() < 2) return null; var start = points.First(); List paths = [new(start, [])]; foreach (var end in points.Skip(1)) { if (valid != null && start != points.First() && !valid(start)) return null; var newPaths = new List(); foreach (var path in paths) { foreach (var subPath in GetPaths(start, end, visited: [.. path.Hops.Select(h => h.Node)], valid)) { newPaths.Add(new(path.Start, [.. path.Hops, .. subPath.Hops])); } } if (newPaths.Count == 0) return null; paths = newPaths; start = end; } if (paths.Count == 0) { return null; } else { cost ??= p => 0; return paths.OrderBy(cost).First(); } } public IEnumerable GetPaths(INode start, INode end, HashSet? visited = null, Func? valid = 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 = []; foreach (var o in start.Outputs) { var e = o.IsTwoWay && o.End == start ? o.Start : o.End; if (e == end) { yield return new(start, [(o, e)]); continue; } else if (!valid(e) || visited.Contains(e)) { continue; } foreach (var p in GetPaths(e, end, visited, valid)) { yield return new(start, [(o, e), .. p.Hops]); } } } public void Draw(Canvas canvas, double scale) { var bg = new Rectangle() { Fill = Brushes.White, Width = 8000, Height = 8000, }; SetPos(bg, -4000, -4000, 1); canvas.Children.Add(bg); foreach (var e in Edges) { e.Draw(canvas); e.Scale(scale); } foreach (var n in Nodes) { n.Draw(canvas); n.Scale(scale); } } public void Scale(double scale) { foreach (var e in Edges) { e.Scale(scale); } foreach (var n in Nodes) { n.Scale(scale); } } public static double ToPx(double v, double scale) { return Math.Round(v * scale); } public static Point ToPx(double x, double y, double scale, double dxpx = 0, double dypx = 0, bool round = true) { var p = new Point(x * scale + dxpx, y * scale + dypx); return round ? new(Math.Round(p.X), Math.Round(p.Y)) : p; } public static Point ToPx(Point p, double scale) { return ToPx(p.X, p.Y, scale); } public static double ToBorder(double scale) { return scale > 10 ? 4 : scale > 7.5 ? 3 : scale > 4 ? 2 : 1; } public static void SetPos(FrameworkElement? s, double x, double y, double scale, double dxpx = 0, double dypx = 0) { s?.SetValue(Canvas.LeftProperty, ToPx(x, scale) + dxpx); s?.SetValue(Canvas.TopProperty, ToPx(y, scale) + dypx); } public static void SetWH(Shape? s, double w, double h, double scale) { s?.Width = ToPx(w, scale); s?.Height = ToPx(h, scale); } public static void SetCenter(Shape? s, double cx, double cy, double w, double h, double scale) { Graph.SetWH(s, w, h, scale); Graph.SetPos(s, cx - w / 2, cy - h / 2, scale); } public ISet GetHover(double x, double y, double scale) { return Nodes.Where(n => n.IsInside(x / scale, y / scale)).ToHashSet(); } public void Update(ISet hovering, bool isPressed) { foreach (var e in Edges) { e.Update(); } foreach (var n in Nodes) { n.Update(hovering.Contains(n), isPressed); } } } }