Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/PathGenerators/PathGenerators.Smooth.cs

127 lines
6.0 KiB
C#
Raw Normal View History

2023-01-08 09:22:37 +08:00
using System;
2023-01-12 23:02:53 +08:00
using System.Linq;
using AIStudio.Wpf.DiagramDesigner.Geometrys;
2023-01-08 09:22:37 +08:00
namespace AIStudio.Wpf.DiagramDesigner
{
public static partial class PathGenerators
{
2023-02-19 21:38:28 +08:00
2023-01-08 09:22:37 +08:00
2023-01-24 17:53:04 +08:00
public static PathGeneratorResult Smooth(IDiagramViewModel _, ConnectionViewModel link, PointBase[] route, PointBase source, PointBase target)
2023-01-08 09:22:37 +08:00
{
route = ConcatRouteAndSourceAndTarget(route, source, target);
if (route.Length > 2)
return CurveThroughPoints(route, link);
route = GetRouteWithCurvePoints(link, route);
2023-01-15 11:59:51 +08:00
2023-02-04 11:16:39 +08:00
double sourceAngle = SourceMarkerAdjustement(route, link.GetSourceMarkerWidth(), link.GetSourceMarkerHeight());
double targetAngle = TargetMarkerAdjustement(route, link.GetSinkMarkerWidth(), link.GetSinkMarkerHeight());
2023-01-12 23:02:53 +08:00
DoShift(route, link);
2023-01-08 09:22:37 +08:00
var path = FormattableString.Invariant($"M {route[0].X} {route[0].Y} C {route[1].X} {route[1].Y}, {route[2].X} {route[2].Y}, {route[3].X} {route[3].Y}");
2023-02-04 12:19:08 +08:00
return new PathGeneratorResult(new[] { path }, sourceAngle, route[0], targetAngle, route[route.Length - 1]);
2023-01-08 09:22:37 +08:00
}
2023-01-24 17:53:04 +08:00
private static PathGeneratorResult CurveThroughPoints(PointBase[] route, ConnectionViewModel link)
2023-01-08 09:22:37 +08:00
{
2023-02-04 11:16:39 +08:00
double sourceAngle = SourceMarkerAdjustement(route, link.GetSourceMarkerWidth(), link.GetSourceMarkerHeight());
double targetAngle = TargetMarkerAdjustement(route, link.GetSinkMarkerWidth(), link.GetSinkMarkerHeight());
2023-01-08 09:22:37 +08:00
2023-01-12 23:02:53 +08:00
BezierSpline.GetCurveControlPoints(route, out var firstControlPoints, out var secondControlPoints);
2023-01-21 22:01:10 +08:00
DoShift(route, link);
DoShift(firstControlPoints, link);
DoShift(secondControlPoints, link);
var paths = new string[firstControlPoints.Length];
2023-01-08 09:22:37 +08:00
for (var i = 0; i < firstControlPoints.Length; i++)
{
var cp1 = firstControlPoints[i];
var cp2 = secondControlPoints[i];
paths[i] = FormattableString.Invariant($"M {route[i].X} {route[i].Y} C {cp1.X} {cp1.Y}, {cp2.X} {cp2.Y}, {route[i + 1].X} {route[i + 1].Y}");
}
// Todo: adjust marker positions based on closest control points
2023-02-04 12:19:08 +08:00
return new PathGeneratorResult(paths, sourceAngle, route[0], targetAngle, route[route.Length - 1]);
2023-01-08 09:22:37 +08:00
}
2023-01-24 17:53:04 +08:00
private static PointBase[] GetRouteWithCurvePoints(ConnectionViewModel link, PointBase[] route)
2023-01-08 09:22:37 +08:00
{
if (link.IsPortless)
{
if (Math.Abs(route[0].X - route[1].X) >= Math.Abs(route[0].Y - route[1].Y))
{
var cX = (route[0].X + route[1].X) / 2;
return new[] { route[0], new PointBase(cX, route[0].Y), new PointBase(cX, route[1].Y), route[1] };
}
else
{
var cY = (route[0].Y + route[1].Y) / 2;
return new[] { route[0], new PointBase(route[0].X, cY), new PointBase(route[1].X, cY), route[1] };
}
}
else
{
var cX = (route[0].X + route[1].X) / 2;
var cY = (route[0].Y + route[1].Y) / 2;
2023-02-16 23:02:43 +08:00
var sourceOrientation = link.SourceConnectorInfo?.Orientation;
if (sourceOrientation == ConnectorOrientation.None)//按照线条的四象限来处理。
{
var slope = (route[1].Y - route[0].Y) / (route[1].X - route[0].X);
2023-02-19 21:38:28 +08:00
if (Math.Abs(slope) < link.SmoothAutoSlope)
{
if (route[1].X > route[0].X)
{
sourceOrientation = ConnectorOrientation.Right;
}
else
{
sourceOrientation = ConnectorOrientation.Left;
}
}
else
{
if (route[1].Y > route[0].Y)//Y轴方向是反的
{
sourceOrientation = ConnectorOrientation.Bottom;
}
else
{
sourceOrientation = ConnectorOrientation.Top;
}
}
2023-02-16 23:02:43 +08:00
}
2023-02-19 21:38:28 +08:00
var curvePointA = GetCurvePoint(route[0].X, route[0].Y, cX, cY, link.SmoothMargin, sourceOrientation);
var curvePointB = GetCurvePoint(route[1].X, route[1].Y, cX, cY, link.SmoothMargin, link.SinkConnectorInfo?.Orientation);
2023-01-08 09:22:37 +08:00
return new[] { route[0], curvePointA, curvePointB, route[1] };
}
}
2023-02-19 21:38:28 +08:00
private static PointBase GetCurvePoint(double pX, double pY, double cX, double cY, double smoothMargin, ConnectorOrientation? alignment)
2023-01-08 09:22:37 +08:00
{
2023-02-19 21:38:28 +08:00
var margin = Math.Min(smoothMargin, Math.Pow(Math.Pow(pX - cX, 2) + Math.Pow(pY - cY, 2), .5));
2023-01-08 09:22:37 +08:00
switch (alignment)
{
case ConnectorOrientation.Top: return new PointBase(pX, Math.Min(pY - margin, cY));
case ConnectorOrientation.Bottom: return new PointBase(pX, Math.Max(pY + margin, cY));
case ConnectorOrientation.TopRight: return new PointBase(Math.Max(pX + margin, cX), Math.Min(pY - margin, cY));
case ConnectorOrientation.BottomRight: return new PointBase(Math.Max(pX + margin, cX), Math.Max(pY + margin, cY));
case ConnectorOrientation.Right: return new PointBase(Math.Max(pX + margin, cX), pY);
case ConnectorOrientation.Left: return new PointBase(Math.Min(pX - margin, cX), pY);
case ConnectorOrientation.BottomLeft: return new PointBase(Math.Min(pX - margin, cX), Math.Max(pY + margin, cY));
case ConnectorOrientation.TopLeft: return new PointBase(Math.Min(pX - margin, cX), Math.Min(pY - margin, cY));
default: return new PointBase(cX, cY);
};
}
2023-01-12 23:02:53 +08:00
2023-01-08 09:22:37 +08:00
}
}