144 lines
7.8 KiB
C#
144 lines
7.8 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;
|
|
|
|
// 坐标转换
|
|
bool transformApplied = false;
|
|
double tDist = 0;
|
|
if (item.BoolParams.TryGetValue("PtLine_EnableTrans", out bool doXf) && doXf)
|
|
{
|
|
if (item.StringParams.TryGetValue("PtLine_TransMat", out var xfName) && !string.IsNullOrEmpty(xfName))
|
|
{
|
|
var sysVar = func.GlobalModel.VariablesModel.SystemVariables.FirstOrDefault(v => v.Name == xfName);
|
|
if (sysVar?.Value != null)
|
|
{
|
|
Mat transformMat = MatExtension.DeserializeAffineMat((string)sysVar.Value);
|
|
using (transformMat)
|
|
{
|
|
Point2f XfPt(double px, double py)
|
|
{
|
|
if (transformMat.Rows == 2)
|
|
return new Point2f(
|
|
(float)(px * transformMat.At<double>(0, 0) + py * transformMat.At<double>(0, 1) + transformMat.At<double>(0, 2)),
|
|
(float)(px * transformMat.At<double>(1, 0) + py * transformMat.At<double>(1, 1) + transformMat.At<double>(1, 2)));
|
|
double w = px * transformMat.At<double>(2, 0) + py * transformMat.At<double>(2, 1) + transformMat.At<double>(2, 2);
|
|
if (Math.Abs(w) < 1e-10) return new Point2f(0, 0);
|
|
return new Point2f(
|
|
(float)((px * transformMat.At<double>(0, 0) + py * transformMat.At<double>(0, 1) + transformMat.At<double>(0, 2)) / w),
|
|
(float)((px * transformMat.At<double>(1, 0) + py * transformMat.At<double>(1, 1) + transformMat.At<double>(1, 2)) / w));
|
|
}
|
|
|
|
var tf = XfPt(line.From.X, line.From.Y);
|
|
var tt = XfPt(line.To.X, line.To.Y);
|
|
var tp = XfPt(point.X, point.Y);
|
|
|
|
// 变换后重新计算距离
|
|
double nx1 = tf.X, ny1 = tf.Y, nx2 = tt.X, ny2 = tt.Y, nx0 = tp.X, ny0 = tp.Y;
|
|
double nA = ny2 - ny1, nB = nx1 - nx2, nC = nx2 * ny1 - nx1 * ny2;
|
|
double nDenom = Math.Sqrt(nA * nA + nB * nB);
|
|
if (nDenom >= 1e-10)
|
|
{
|
|
double _tDist = Math.Abs(nA * nx0 + nB * ny0 + nC) / nDenom;
|
|
double nDot = (nx0 - nx1) * (nx2 - nx1) + (ny0 - ny1) * (ny2 - ny1);
|
|
double nT = nDot / (nDenom * nDenom);
|
|
double nFootX = Math.Round(nx1 + nT * (nx2 - nx1), 3);
|
|
double nFootY = Math.Round(ny1 + nT * (ny2 - ny1), 3);
|
|
|
|
// UI 显示转换后值
|
|
func.Params.ResultDoubles["PointLineDist_Distance"] = Math.Round(_tDist, 3);
|
|
func.Params.ResultDoubles["PointLineDist_FootX"] = nFootX;
|
|
func.Params.ResultDoubles["PointLineDist_FootY"] = nFootY;
|
|
|
|
transformApplied = true;
|
|
tDist = Math.Round(_tDist, 3);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (transformApplied)
|
|
{
|
|
// 转换后距离写入 TransOutVar
|
|
var xfVarName = item.StringParams.TryGetValue("PtLine_TransOutVar", out var xov) && !string.IsNullOrEmpty(xov) ? xov : null;
|
|
if (xfVarName != null)
|
|
{
|
|
var xfVar = func.Params.Variables.FirstOrDefault(v => v.Name == xfVarName);
|
|
if (xfVar == null)
|
|
{
|
|
xfVar = new LocalVariableItem(xfVarName, "Double", 0.0);
|
|
System.Windows.Application.Current.Dispatcher.Invoke(() => func.Params.Variables.Add(xfVar));
|
|
}
|
|
xfVar.RawValue = tDist;
|
|
}
|
|
}
|
|
|
|
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,
|
|
$"{func.Params.ResultDoubles["PointLineDist_Distance"]:F3}", Colors.Lime, 48));
|
|
_ = func.RenderDrawAsync();
|
|
item.Flow.Done = true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|