Add path finding
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
public class Graph {
|
||||
|
||||
protected HashSet<INode> Nodes = [];
|
||||
protected HashSet<IEdge> Edges = [];
|
||||
public HashSet<INode> Nodes = [];
|
||||
public HashSet<IEdge> Edges = [];
|
||||
|
||||
public Graph() {
|
||||
|
||||
@@ -20,7 +22,32 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
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);
|
||||
}
|
||||
@@ -29,13 +56,17 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
}
|
||||
}
|
||||
|
||||
public void Update(double x, double y) {
|
||||
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) {
|
||||
n.Update(x, y);
|
||||
if (n.Update(x, y)) {
|
||||
hover.Add(n);
|
||||
}
|
||||
}
|
||||
return hover;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
public INode Start { get; }
|
||||
public INode End { get; }
|
||||
public bool Orientation { get; }
|
||||
public bool Highlight { get; set; }
|
||||
|
||||
public void Draw(Canvas canvas);
|
||||
public bool Update(double x, double y);
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
public record struct Path(INode Start, IEnumerable<(IEdge Edge, INode Node)> Hops);
|
||||
}
|
||||
@@ -9,6 +9,12 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
public INode Start { get; set; }
|
||||
public INode End { get; set; }
|
||||
public bool Orientation { get; set; }
|
||||
public bool Highlight { get; set; }
|
||||
|
||||
private Line? _outer1;
|
||||
private Line? _outer2;
|
||||
private Line? _inner1;
|
||||
private Line? _inner2;
|
||||
|
||||
public Pipe(INode start, INode end, bool orientation = false) {
|
||||
Start = start;
|
||||
@@ -23,41 +29,43 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
var y2 = Orientation ? End.CenterY : Start.CenterY;
|
||||
var x3 = End.CenterX;
|
||||
var y3 = End.CenterY;
|
||||
var a1 = new Line() {
|
||||
_outer1 = new Line() {
|
||||
X1 = x1, Y1 = y1, X2 = x2, Y2 = y2,
|
||||
StrokeThickness = 10,
|
||||
Stroke = Brushes.Black,
|
||||
StrokeStartLineCap = PenLineCap.Square,
|
||||
StrokeEndLineCap = PenLineCap.Square,
|
||||
};
|
||||
var a2 = new Line() {
|
||||
_inner1 = new Line() {
|
||||
X1 = x1, Y1 = y1, X2 = x2, Y2 = y2,
|
||||
StrokeThickness = 6,
|
||||
Stroke = Brushes.Gray,
|
||||
Stroke = Brushes.DarkGray,
|
||||
StrokeStartLineCap = PenLineCap.Square,
|
||||
StrokeEndLineCap = PenLineCap.Square,
|
||||
};
|
||||
var b1 = new Line() {
|
||||
_outer2 = new Line() {
|
||||
X1 = x2, Y1 = y2, X2 = x3, Y2 = y3,
|
||||
StrokeThickness = 10,
|
||||
Stroke = Brushes.Black,
|
||||
StrokeStartLineCap = PenLineCap.Square,
|
||||
StrokeEndLineCap = PenLineCap.Square,
|
||||
};
|
||||
var b2 = new Line() {
|
||||
_inner2 = new Line() {
|
||||
X1 = x2, Y1 = y2, X2 = x3, Y2 = y3,
|
||||
StrokeThickness = 6,
|
||||
Stroke = Brushes.Gray,
|
||||
Stroke = Brushes.DarkGray,
|
||||
StrokeStartLineCap = PenLineCap.Square,
|
||||
StrokeEndLineCap = PenLineCap.Square,
|
||||
};
|
||||
canvas.Children.Add(a1);
|
||||
canvas.Children.Add(b1);
|
||||
canvas.Children.Add(a2);
|
||||
canvas.Children.Add(b2);
|
||||
canvas.Children.Add(_outer1);
|
||||
canvas.Children.Add(_outer2);
|
||||
canvas.Children.Add(_inner1);
|
||||
canvas.Children.Add(_inner2);
|
||||
}
|
||||
|
||||
public bool Update(double x, double y) {
|
||||
_inner1?.Stroke = Highlight ? Brushes.Green: Brushes.DarkGray;
|
||||
_inner2?.Stroke = Highlight ? Brushes.Green : Brushes.DarkGray;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
public class PipeJoin : INode {
|
||||
|
||||
public const double SIZE = 18;
|
||||
public const double POINT = 4;
|
||||
|
||||
public double CenterX { get; set; }
|
||||
public double CenterY { get; set; }
|
||||
public string Label { get; } = "";
|
||||
|
||||
public ISet<IEdge> Inputs { get; init; }
|
||||
|
||||
public ISet<IEdge> Outputs { get; init; }
|
||||
|
||||
private Polygon? _rect;
|
||||
|
||||
public PipeJoin(double x, double y) {
|
||||
CenterX = x;
|
||||
CenterY = y;
|
||||
@@ -21,18 +26,53 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
}
|
||||
|
||||
public void Draw(Canvas canvas) {
|
||||
var e = new Rectangle() {
|
||||
Height = 18,
|
||||
Width = 18,
|
||||
Fill = Brushes.Black,
|
||||
bool? top = Inputs.Any(e => e.Start.CenterY < CenterY && (!e.Orientation || e.Start.CenterX == CenterX)) ? false : Outputs.Any(e => e.End.CenterY < CenterY && (e.Orientation || e.Start.CenterX == CenterX)) ? true : null;
|
||||
bool? right = Inputs.Any(e => e.Start.CenterX > CenterX && (e.Orientation || e.Start.CenterY == CenterY)) ? false : Outputs.Any(e => e.End.CenterX > CenterX && (!e.Orientation || e.Start.CenterY == CenterY)) ? true : null;
|
||||
bool? bottom = Inputs.Any(e => e.Start.CenterY > CenterY && (!e.Orientation || e.Start.CenterX == CenterX)) ? false : Outputs.Any(e => e.End.CenterY > CenterY && (e.Orientation || e.Start.CenterX == CenterX)) ? true : null;
|
||||
bool? left = Inputs.Any(e => e.Start.CenterX < CenterX && (e.Orientation || e.Start.CenterY == CenterY)) ? false : Outputs.Any(e => e.End.CenterX < CenterX && (!e.Orientation || e.Start.CenterY == CenterY)) ? true : null;
|
||||
|
||||
var points = new List<Point> {
|
||||
new(0, 0)
|
||||
};
|
||||
e.SetValue(Canvas.LeftProperty, CenterX - 9);
|
||||
e.SetValue(Canvas.TopProperty, CenterY - 9);
|
||||
canvas.Children.Add(e);
|
||||
if (top == true) {
|
||||
points.AddRange([new(SIZE / 2 - POINT, 0), new(SIZE / 2, -POINT), new(SIZE / 2 + POINT, 0)]);
|
||||
} else if (top == false) {
|
||||
points.AddRange([new(SIZE / 2 - POINT, 0), new(SIZE / 2, POINT), new(SIZE / 2 + POINT, 0)]);
|
||||
}
|
||||
points.Add(new(SIZE, 0));
|
||||
if (right == true) {
|
||||
points.AddRange([new(SIZE, SIZE / 2 - POINT), new(SIZE + POINT, SIZE / 2), new(SIZE, SIZE / 2 + POINT)]);
|
||||
} else if (right == false) {
|
||||
points.AddRange([new(SIZE, SIZE / 2 - POINT), new(SIZE - POINT, SIZE / 2), new(SIZE, SIZE / 2 + POINT)]);
|
||||
}
|
||||
points.Add(new(SIZE, SIZE));
|
||||
if (bottom == true) {
|
||||
points.AddRange([new(SIZE / 2 + POINT, SIZE), new(SIZE / 2, SIZE + POINT), new(SIZE / 2 - POINT, SIZE)]);
|
||||
} else if (bottom == false) {
|
||||
points.AddRange([new(SIZE / 2 + POINT, SIZE), new(SIZE / 2, SIZE - POINT), new(SIZE / 2 - POINT, SIZE)]);
|
||||
}
|
||||
points.Add(new(0, SIZE));
|
||||
if (left == true) {
|
||||
points.AddRange([new(0, SIZE / 2 + POINT), new(-POINT, SIZE / 2), new(0, SIZE / 2 - POINT)]);
|
||||
} else if (left == false) {
|
||||
points.AddRange([new(0, SIZE / 2 + POINT), new(POINT, SIZE / 2), new(0, SIZE / 2 - POINT)]);
|
||||
}
|
||||
|
||||
_rect = new Polygon() {
|
||||
Points = [..points],
|
||||
Fill = Brushes.Black,
|
||||
Stroke = Brushes.Black,
|
||||
StrokeThickness = 2,
|
||||
};
|
||||
_rect.SetValue(Canvas.LeftProperty, CenterX - SIZE / 2);
|
||||
_rect.SetValue(Canvas.TopProperty, CenterY - SIZE / 2);
|
||||
canvas.Children.Add(_rect);
|
||||
}
|
||||
|
||||
public bool Update(double x, double y) {
|
||||
return false;
|
||||
var hover = Math.Abs(x - CenterX) <= SIZE / 2 && Math.Abs(y - CenterY) <= SIZE / 2;
|
||||
_rect?.Stroke = hover ? Brushes.Black : Brushes.Black;
|
||||
return hover;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
public class Pump : INode {
|
||||
|
||||
public const double RADIUS = 24;
|
||||
|
||||
public double CenterX { get; set; }
|
||||
public double CenterY { get; set; }
|
||||
public string Label { get; set; }
|
||||
|
||||
public ISet<IEdge> Inputs { get; init; }
|
||||
|
||||
public ISet<IEdge> Outputs { get; init; }
|
||||
|
||||
private Ellipse? _circle;
|
||||
private TextBlock? _text;
|
||||
|
||||
public Pump(string label, double x, double y) {
|
||||
CenterX = x;
|
||||
CenterY = y;
|
||||
@@ -20,7 +26,16 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
}
|
||||
|
||||
public void Draw(Canvas canvas) {
|
||||
|
||||
_circle = new() {
|
||||
Height = RADIUS,
|
||||
Width = RADIUS,
|
||||
Fill = Brushes.WhiteSmoke,
|
||||
Stroke = Brushes.Black,
|
||||
StrokeThickness = 2,
|
||||
};
|
||||
_circle.SetValue(Canvas.LeftProperty, CenterX - RADIUS / 2);
|
||||
_circle.SetValue(Canvas.TopProperty, CenterY - RADIUS / 2);
|
||||
canvas.Children.Add(_circle);
|
||||
}
|
||||
|
||||
public bool Update(double x, double y) {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
public class Valve : INode {
|
||||
|
||||
public const double RADIUS = 24;
|
||||
|
||||
public double CenterX { get; set; }
|
||||
public double CenterY { get; set; }
|
||||
public string Label { get; set; }
|
||||
@@ -17,7 +20,8 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
protected Func<bool> CallbackIsOpen;
|
||||
protected Func<bool> CallbackWantOpen;
|
||||
|
||||
private Ellipse Me;
|
||||
private Ellipse? _circle;
|
||||
private TextBlock? _text;
|
||||
|
||||
public Valve(string label, double x, double y, Func<bool> cbOpen, Func<bool> cbWant) {
|
||||
CenterX = x;
|
||||
@@ -30,21 +34,32 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
|
||||
}
|
||||
|
||||
public void Draw(Canvas canvas) {
|
||||
Me = new Ellipse() {
|
||||
Height = 20,
|
||||
Width = 20,
|
||||
Fill = Brushes.Gray,
|
||||
_circle = new() {
|
||||
Height = RADIUS,
|
||||
Width = RADIUS,
|
||||
Fill = Brushes.DarkGray,
|
||||
Stroke = Brushes.Black,
|
||||
StrokeThickness = 2,
|
||||
};
|
||||
Me.SetValue(Canvas.LeftProperty, CenterX - 10);
|
||||
Me.SetValue(Canvas.TopProperty, CenterY - 10);
|
||||
canvas.Children.Add(Me);
|
||||
_circle.SetValue(Canvas.LeftProperty, CenterX - RADIUS / 2);
|
||||
_circle.SetValue(Canvas.TopProperty, CenterY - RADIUS / 2);
|
||||
_text = new() {
|
||||
Text = Label,
|
||||
FontSize = 10,
|
||||
Width = RADIUS,
|
||||
Height = RADIUS,
|
||||
TextAlignment = TextAlignment.Center,
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
};
|
||||
_text.SetValue(Canvas.LeftProperty, CenterX - RADIUS / 2);
|
||||
_text.SetValue(Canvas.TopProperty, CenterY - RADIUS / 2 + 5);
|
||||
canvas.Children.Add(_circle);
|
||||
canvas.Children.Add(_text);
|
||||
}
|
||||
|
||||
public bool Update(double x, double y) {
|
||||
var hover = Math.Sqrt(Math.Pow(CenterX - x, 2) + Math.Pow(CenterY - y, 2)) <= 10;
|
||||
Me.Fill = hover ? Brushes.LightGray : Brushes.Gray;
|
||||
var hover = Math.Sqrt(Math.Pow(CenterX - x, 2) + Math.Pow(CenterY - y, 2)) <= RADIUS / 2;
|
||||
_circle?.Fill = hover ? Brushes.LightGray : Brushes.DarkGray;
|
||||
return hover;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user