两点距离

This commit is contained in:
tfarcraw 2026-07-21 18:57:28 +08:00
parent ff69308edb
commit c3b7b0afd7
6 changed files with 130 additions and 0 deletions

View File

@ -120,6 +120,9 @@ public partial class VisionConfigViewModel
case VisionFlowType.灰度模板匹配:
ContentRegion = new GrayTemplateMatch();
break;
case VisionFlowType.点到点距离:
ContentRegion = new PointToPointDistance();
break;
}
if (ContentRegion != null)
@ -430,6 +433,9 @@ public partial class VisionConfigViewModel
newFlow.StringParams["TemplateName"] = "GrayMatch_Template";
newFlow.StringParams["GrayTemplateMatch_PosVar"] = "灰度模板匹配_Pos";
break;
case VisionFlowType.点到点距离:
newFlow.StringParams["ResultVar"] = "点到点距离";
break;
default:
break;
}

View File

@ -0,0 +1,55 @@
<UserControl x:Class="PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm.PointToPointDistance"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PLCSharp.VVMs.Vision.VisionFlowHandler"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vision="clr-namespace:PLCSharp.VVMs.Vision"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vision:VisionConfigViewModel}"
d:DesignHeight="200" d:DesignWidth="300">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel>
<GroupBox Header="参数">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Text="点1变量" VerticalAlignment="Center" />
<ComboBox Grid.Row="0" Grid.Column="1"
ItemsSource="{Binding AllVariables}"
SelectedValue="{Binding SelectVisionFlow.StringParams[Point1Var], Mode=TwoWay}"
SelectedValuePath="Name" DisplayMemberPath="Name" />
<TextBlock Grid.Row="1" Text="点2变量" VerticalAlignment="Center" />
<ComboBox Grid.Row="1" Grid.Column="1"
ItemsSource="{Binding AllVariables}"
SelectedValue="{Binding SelectVisionFlow.StringParams[Point2Var], Mode=TwoWay}"
SelectedValuePath="Name" DisplayMemberPath="Name" />
<TextBlock Grid.Row="2" Text="结果变量" VerticalAlignment="Center" />
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding SelectVisionFlow.StringParams[ResultVar], Mode=TwoWay, TargetNullValue=点到点距离}" />
<TextBlock Grid.Row="3" Text="距离" VerticalAlignment="Center" />
<TextBox Grid.Row="3" Grid.Column="1"
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointDist_Distance], Mode=OneWay}"
IsReadOnly="True" Background="#FFF0F0F0" />
<TextBlock Grid.Row="4" Text="ΔX" VerticalAlignment="Center" />
<TextBox Grid.Row="4" Grid.Column="1"
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointDist_DeltaX], Mode=OneWay}"
IsReadOnly="True" Background="#FFF0F0F0" />
<TextBlock Grid.Row="5" Text="ΔY" VerticalAlignment="Center" />
<TextBox Grid.Row="5" Grid.Column="1"
Text="{Binding SelectedVisionFunction.Params.ResultDoubles[PointDist_DeltaY], Mode=OneWay}"
IsReadOnly="True" Background="#FFF0F0F0" />
</Grid>
</GroupBox>
</StackPanel>
</ScrollViewer>
</UserControl>

View File

@ -0,0 +1,12 @@
using System.Windows.Controls;
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
{
public partial class PointToPointDistance : UserControl
{
public PointToPointDistance()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,53 @@
using OpenCvSharp;
using System.Windows.Media;
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm
{
/// <summary>
/// 点到点距离工具 —— 读取两个 Pos 局部变量,计算欧几里德距离。
/// </summary>
public class PointToPointDistanceHandler : IVisionFlowHandler
{
public VisionFlowType Type => VisionFlowType.点到点距离;
public bool Execute(VisionFunction func, VisionFlow item)
{
var var1Name = item.StringParams.TryGetValue("Point1Var", out var p1) && !string.IsNullOrEmpty(p1) ? p1 : "None";
var var2Name = item.StringParams.TryGetValue("Point2Var", out var p2) && !string.IsNullOrEmpty(p2) ? p2 : "None";
var var1 = func.Params.Variables.FirstOrDefault(v => v.Name == var1Name);
var var2 = func.Params.Variables.FirstOrDefault(v => v.Name == var2Name);
if (var1 == null) throw new Exception($"变量 '{var1Name}' 不存在!");
if (var2 == null) throw new Exception($"变量 '{var2Name}' 不存在!");
if (var1.RawValue is not Pos pos1) throw new Exception($"变量 '{var1Name}' 不是 Pos 类型!");
if (var2.RawValue is not Pos pos2) throw new Exception($"变量 '{var2Name}' 不是 Pos 类型!");
double dist = Math.Sqrt((pos2.X - pos1.X) * (pos2.X - pos1.X) + (pos2.Y - pos1.Y) * (pos2.Y - pos1.Y));
dist = Math.Round(dist, 3);
func.Params.ResultDoubles["PointDist_Distance"] = dist;
func.Params.ResultDoubles["PointDist_DeltaX"] = Math.Round(pos2.X - pos1.X, 3);
func.Params.ResultDoubles["PointDist_DeltaY"] = Math.Round(pos2.Y - pos1.Y, 3);
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(pos1.X, pos1.Y, pos2.X, pos2.Y, Colors.Lime, 2));
func.DrawCommands.Add(DrawCommand.FilledCircle(pos1.X, pos1.Y, 4, Colors.Cyan));
func.DrawCommands.Add(DrawCommand.FilledCircle(pos2.X, pos2.Y, 4, Colors.Cyan));
func.DrawCommands.Add(DrawCommand.TextBlock(
(pos1.X + pos2.X) / 2 + 5, (pos1.Y + pos2.Y) / 2 - 5,
$"{dist:F3}", Colors.Lime, 16));
item.Flow.Done = true;
return true;
}
}
}

View File

@ -338,6 +338,7 @@ namespace PLCSharp.VVMs.Vision
[VisionFlowType.卡尺寻边] = new CaliperFindEdgeHandler(),
[VisionFlowType.ORB匹配] = new ORBMatchHandler(),
[VisionFlowType.灰度模板匹配] = new GrayTemplateMatchHandler(),
[VisionFlowType.点到点距离] = new PointToPointDistanceHandler(),
[VisionFlowType.卡尺找圆] = new CaliperFindCircleHandler(),
[VisionFlowType.卡尺找旋转矩形] = new CaliperFindRectHandler(),
[VisionFlowType.清除绘制] = new ClearDrawHandler(),

View File

@ -81,6 +81,9 @@
[FlowCategory("算法")]
灰度模板匹配 = 40,
[FlowCategory("算法")]
点到点距离 = 41,
}