有模板时计算偏移

This commit is contained in:
tfarcraw 2026-07-24 12:27:58 +08:00
parent bb3e687df9
commit 07a516806e

View File

@ -171,35 +171,36 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
}
// 有模板时计算偏移
if (item.DoubleParams.TryGetValue("TemplateLineStartX", out double tsx) &&
item.DoubleParams.TryGetValue("TemplateLineStartY", out double tsy) &&
item.DoubleParams.TryGetValue("TemplateLineEndX", out double tex) &&
item.DoubleParams.TryGetValue("TemplateLineEndY", out double tey))
if (item.BoolParams.TryGetValue("Caliper_OutOffset", out bool needOffset) && needOffset)
{
double cenX = (sx + ex) / 2, cenY = (sy + ey) / 2;
double tCenX = (tsx + tex) / 2, tCenY = (tsy + tey) / 2;
double offsetX = Math.Round(cenX - tCenX, 3);
double offsetY = Math.Round(cenY - tCenY, 3);
double tAngle = Math.Atan2(tey - tsy, tex - tsx);
var lineInls = new Line2D(inls[0].X, inls[0].Y, inls[^1].X, inls[^1].Y);
double fc = (0 + 0.5) / numCalipers;
double fcx = sx + fc * lineDx, fcy = sy + fc * lineDy;
var caliperFirst = new Line2D(fcx - halfLen * pdx, fcy - halfLen * pdy, fcx + halfLen * pdx, fcy + halfLen * pdy);
var cs = Intersect(lineInls, caliperFirst);
double lc = (numCalipers - 1 + 0.5) / numCalipers;
double lcx = sx + lc * lineDx, lcy = sy + lc * lineDy;
var caliperLast = new Line2D(lcx - halfLen * pdx, lcy - halfLen * pdy, lcx + halfLen * pdx, lcy + halfLen * pdy);
var ce = Intersect(lineInls, caliperLast);
double cAngle = Math.Atan2(ce.Y - cs.Y, ce.X - cs.X);
double offsetAngle = Math.Round((cAngle - tAngle) * 180 / Math.PI, 3);
func.Params.ResultDoubles["LineOffsetX"] = offsetX;
func.Params.ResultDoubles["LineOffsetY"] = offsetY;
func.Params.ResultDoubles["LineOffsetAngle"] = offsetAngle;
if (!item.BoolParams.TryGetValue("Caliper_OutOffset", out bool skipOff) || skipOff)
// 需要输出偏移 → 必须有模板
if (item.DoubleParams.TryGetValue("TemplateLineStartX", out double tsx) &&
item.DoubleParams.TryGetValue("TemplateLineStartY", out double tsy) &&
item.DoubleParams.TryGetValue("TemplateLineEndX", out double tex) &&
item.DoubleParams.TryGetValue("TemplateLineEndY", out double tey))
{
double cenX = (sx + ex) / 2, cenY = (sy + ey) / 2;
double tCenX = (tsx + tex) / 2, tCenY = (tsy + tey) / 2;
double offsetX = Math.Round(cenX - tCenX, 3);
double offsetY = Math.Round(cenY - tCenY, 3);
double tAngle = Math.Atan2(tey - tsy, tex - tsx);
var lineInls = new Line2D(inls[0].X, inls[0].Y, inls[^1].X, inls[^1].Y);
double fc = (0 + 0.5) / numCalipers;
double fcx = sx + fc * lineDx, fcy = sy + fc * lineDy;
var caliperFirst = new Line2D(fcx - halfLen * pdx, fcy - halfLen * pdy, fcx + halfLen * pdx, fcy + halfLen * pdy);
var cs = Intersect(lineInls, caliperFirst);
double lc = (numCalipers - 1 + 0.5) / numCalipers;
double lcx = sx + lc * lineDx, lcy = sy + lc * lineDy;
var caliperLast = new Line2D(lcx - halfLen * pdx, lcy - halfLen * pdy, lcx + halfLen * pdx, lcy + halfLen * pdy);
var ce = Intersect(lineInls, caliperLast);
double cAngle = Math.Atan2(ce.Y - cs.Y, ce.X - cs.X);
double offsetAngle = Math.Round((cAngle - tAngle) * 180 / Math.PI, 3);
func.Params.ResultDoubles["LineOffsetX"] = offsetX;
func.Params.ResultDoubles["LineOffsetY"] = offsetY;
func.Params.ResultDoubles["LineOffsetAngle"] = offsetAngle;
var offVarName = item.StringParams.TryGetValue("LineOffsetVar", out var ov) && !string.IsNullOrEmpty(ov) ? ov : "找线_Offset";
var offVar = func.Params.Variables.FirstOrDefault(v => v.Name == offVarName);
if (offVar == null)
@ -209,6 +210,11 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
}
offVar.RawValue = new Pos(offsetX, offsetY, 0, offsetAngle);
}
else
{
item.Flow.FailMessage = "偏移输出已勾选但未配置模板,请先计算模板位置!";
return false;
}
}
// 坐标转换
@ -219,7 +225,7 @@ namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
var sysVar = func.GlobalModel.VariablesModel.SystemVariables.FirstOrDefault(v => v.Name == xfName);
if (sysVar?.Value != null)
{
using var xfMat = MatExtension.DeserializeAffineMat(sysVar.Value.ToString());
using Mat xfMat = MatExtension.DeserializeAffineMat((string)sysVar.Value);
double xs = 1, ys = 1;
if (xfMat.Rows >= 2 && xfMat.Cols >= 2)
{