Skip to content

Commit

Permalink
feat: Orthogonal router
Browse files Browse the repository at this point in the history
  • Loading branch information
zHaytam committed Mar 9, 2021
1 parent e359f5a commit 1c1fe02
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Blazor.Diagrams.Core/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Blazor.Diagrams.Core
{
public delegate Point[] Router(Diagram diagram, BaseLinkModel link, Point from, Point to);
public delegate Point[] Router(Diagram diagram, BaseLinkModel link);

public delegate PathGeneratorResult PathGenerator(Diagram diagram, BaseLinkModel link, Point[] route);

Expand Down
6 changes: 6 additions & 0 deletions src/Blazor.Diagrams.Core/Models/Core/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ public double DistanceTo(Point other)

public static Point operator -(Point a, Point b) => new Point(a.X - b.X, a.Y - b.Y);
public static Point operator +(Point a, Point b) => new Point(a.X + b.X, a.Y + b.Y);

public void Deconstruct(out double x, out double y)
{
x = X;
y = Y;
}
}
}
37 changes: 36 additions & 1 deletion src/Blazor.Diagrams.Core/Models/Core/Rectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,47 @@ public Rectangle(Point position, Size size)
public bool Overlap(Rectangle r)
=> Left < r.Right && Right > r.Left && Top < r.Bottom && Bottom > r.Top;

public bool Intersects(Rectangle r)
{
var thisX = Left;
var thisY = Top;
var thisW = Width;
var thisH = Height;
var rectX = r.Left;
var rectY = r.Top;
var rectW = r.Width;
var rectH = r.Height;
return (rectX < thisX + thisW) && (thisX < (rectX + rectW)) && (rectY < thisY + thisH) && (thisY < rectY + rectH);
}

public Rectangle Inflate(double horizontal, double vertical)
=> new Rectangle(Left - horizontal, Top - vertical, Right + horizontal, Bottom + vertical);

public Rectangle Union(Rectangle r)
{
var x1 = Math.Min(Left, r.Left);
var x2 = Math.Max(Left + Width, r.Left + r.Width);
var y1 = Math.Min(Top, r.Top);
var y2 = Math.Max(Top + Height, r.Top + r.Height);
return new Rectangle(x1, y1, x2, y2);
}

public bool ContainsPoint(Point point) => ContainsPoint(point.X, point.Y);

public bool ContainsPoint(double x, double y)
=> x >= Left && x <= Right && y >= Top && y <= Bottom;

public Point Center => new Point(Left + Width / 2, Top + Height / 2);
public Point NorthEast => new Point(Right, Top);
public Point SouthEast => new Point(Right, Bottom);
public Point SouthWest => new Point(Left, Bottom);
public Point NorthWest => new Point(Left, Top);
public Point East => new Point(Right, Top + Height / 2);
public Point North => new Point(Left + Width / 2, Top);
public Point South => new Point(Left + Width / 2, Bottom);
public Point West => new Point(Left, Top + Height / 2);

public override string ToString()
=> $"Rectangle(width={Width}, height={Height}, top={Top}, right={Right}, bottom={Bottom}, left={Left})";
=> $"Rectangle(width={Width}, height={Height}, top={Top}, right={Right}, bottom={Bottom}, left={Left})";
}
}
23 changes: 22 additions & 1 deletion src/Blazor.Diagrams.Core/Models/NodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,28 @@ public virtual void UpdatePositionSilently(double deltaX, double deltaY)
Refresh();
}

public Rectangle? GetBounds() => Size == null ? null : new Rectangle(Position, Size);
public Rectangle? GetBounds(bool includePorts = false)
{
if (Size == null)
return null;

if (!includePorts)
return new Rectangle(Position, Size);

var leftPort = GetPort(PortAlignment.Left);
var topPort = GetPort(PortAlignment.Top);
var rightPort = GetPort(PortAlignment.Right);
var bottomPort = GetPort(PortAlignment.Bottom);

var left = leftPort == null ? Position.X : Math.Min(Position.X, leftPort.Position.X);
var top = topPort == null ? Position.Y : Math.Min(Position.Y, topPort.Position.Y);
var right = rightPort == null ? Position.X + Size!.Width :
Math.Max(rightPort.Position.X + rightPort.Size.Width, Position.X + Size!.Width);
var bottom = bottomPort == null ? Position.Y + Size!.Height :
Math.Max(bottomPort.Position.Y + bottomPort.Size.Height, Position.Y + Size!.Height);

return new Rectangle(left, top, right, bottom);
}

private void UpdatePortPositions(double deltaX, double deltaY)
{
Expand Down
10 changes: 7 additions & 3 deletions src/Blazor.Diagrams.Core/Routers/Routers.Normal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ namespace Blazor.Diagrams.Core
{
public static partial class Routers
{
public static Point[] Normal(Diagram _, BaseLinkModel link, Point from, Point to)
public static Point[] Normal(Diagram _, BaseLinkModel link)
{
var route = new Point[link.Vertices.Count + 2];
route[0] = from;
route[0] = link.SourceMarker == null ? link.SourcePort.MiddlePosition : GetPortPositionBasedOnAlignment(link.SourcePort);

if (link.Vertices.Count > 0)
{
Array.Copy(link.Vertices.Select(v => v.Position).ToArray(), 0, route, 1, link.Vertices.Count);
}
route[^1] = to;

route[^1] = link.TargetPort == null
? link.OnGoingPosition!
: link.TargetMarker == null ? link.TargetPort.MiddlePosition : GetPortPositionBasedOnAlignment(link.TargetPort);
return route;
}
}
Expand Down
Loading

0 comments on commit 1c1fe02

Please sign in to comment.