73 lines
3.7 KiB
C#
73 lines
3.7 KiB
C#
using OpenCvSharp;
|
|
using System.Windows.Media;
|
|
|
|
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
|
|
{
|
|
/// <summary>
|
|
/// 点到线距离工具 —— 读取一个 Pos 变量和一个 Line 变量,计算点到直线的垂直距离。
|
|
/// </summary>
|
|
public class PointToLineDistanceHandler : IVisionFlowHandler
|
|
{
|
|
public VisionFlowType Type => VisionFlowType.点到线距离;
|
|
|
|
public bool Execute(VisionFunction func, VisionFlow item)
|
|
{
|
|
var pointVarName = item.StringParams.TryGetValue("PointVar", out var pv) && !string.IsNullOrEmpty(pv) ? pv : "None";
|
|
var lineVarName = item.StringParams.TryGetValue("LineVar", out var lv) && !string.IsNullOrEmpty(lv) ? lv : "None";
|
|
|
|
var pointVar = func.Params.Variables.FirstOrDefault(v => v.Name == pointVarName);
|
|
var lineVar = func.Params.Variables.FirstOrDefault(v => v.Name == lineVarName);
|
|
if (pointVar == null) { item.Flow.FailMessage = $"变量 '{pointVarName}' 不存在!"; return false; }
|
|
if (lineVar == null) { item.Flow.FailMessage = $"变量 '{lineVarName}' 不存在!"; return false; }
|
|
if (pointVar.RawValue is not Pos point) { item.Flow.FailMessage = $"变量 '{pointVarName}' 不是 Pos 类型!"; return false; }
|
|
if (lineVar.RawValue is not Line line) { item.Flow.FailMessage = $"变量 '{lineVarName}' 不是 Line 类型!"; return false; }
|
|
|
|
// 点到直线垂直距离公式
|
|
double x1 = line.From.X, y1 = line.From.Y;
|
|
double x2 = line.To.X, y2 = line.To.Y;
|
|
double x0 = point.X, y0 = point.Y;
|
|
|
|
double A = y2 - y1;
|
|
double B = x1 - x2;
|
|
double C = x2 * y1 - x1 * y2;
|
|
double denom = Math.Sqrt(A * A + B * B);
|
|
|
|
if (denom < 1e-10) { item.Flow.FailMessage = "直线长度为零!"; return false; }
|
|
|
|
double dist = Math.Abs(A * x0 + B * y0 + C) / denom;
|
|
dist = Math.Round(dist, 3);
|
|
|
|
// 垂足坐标
|
|
double dot = (x0 - x1) * (x2 - x1) + (y0 - y1) * (y2 - y1);
|
|
double t = dot / (denom * denom);
|
|
double footX = Math.Round(x1 + t * (x2 - x1), 3);
|
|
double footY = Math.Round(y1 + t * (y2 - y1), 3);
|
|
|
|
func.Params.ResultDoubles["PointLineDist_Distance"] = dist;
|
|
func.Params.ResultDoubles["PointLineDist_FootX"] = footX;
|
|
func.Params.ResultDoubles["PointLineDist_FootY"] = footY;
|
|
|
|
var resultVarName = item.StringParams.TryGetValue("ResultVar", out var rv) && !string.IsNullOrEmpty(rv) ? rv : "点到线距离";
|
|
var resultVar = func.Params.Variables.FirstOrDefault(v => v.Name == resultVarName);
|
|
if (resultVar == null)
|
|
{
|
|
resultVar = new LocalVariableItem(resultVarName, "double", 0.0);
|
|
System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(resultVar));
|
|
}
|
|
resultVar.RawValue = dist;
|
|
|
|
// 可视化
|
|
func.DrawCommands.Add(DrawCommand.Line(line.From.X, line.From.Y, line.To.X, line.To.Y, Colors.Lime, 2));
|
|
func.DrawCommands.Add(DrawCommand.Line(point.X, point.Y, footX, footY, Colors.Cyan, 1));
|
|
func.DrawCommands.Add(DrawCommand.FilledCircle(point.X, point.Y, 4, Colors.Yellow));
|
|
func.DrawCommands.Add(DrawCommand.FilledCircle(footX, footY, 3, Colors.Cyan));
|
|
func.DrawCommands.Add(DrawCommand.TextBlock(
|
|
(point.X + footX) / 2, (point.Y + footY) / 2 - 10,
|
|
$"{dist:F3}", Colors.Lime, 16));
|
|
_ = func.RenderDrawAsync();
|
|
item.Flow.Done = true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|