using HalconDotNet; using LB_SmartVisionCommon; using LB_VisionControls; using LB_VisionProcesses.Alogrithms.Halcon; using OpenCvSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LB_VisionProcesses.Alogrithms { public class IntersectionLine2Tool : TAlgorithm { public IntersectionLine2Tool() { strProcessClass = "LB_VisionProcesses.Alogrithms.IntersectionLine2Tool"; strProcessName = "线线交点工具"; Params.Inputs.Add("基准线段", new HSegment()); Params.Inputs.Add("相交线段", new HSegment()); Params.Inputs.Add("是否期望有交点", true); Params.Inputs.Add("要求交点在线段上", false); Params.Outputs.Add("X", 0.0); Params.Outputs.Add("Y", 0.0); Params.Outputs.Add("Angle", 0.0); Params.Outputs.Add("HPoint", new HPoint()); } /// /// 算子逻辑 /// public override void TAlgorithmMain() { #region 初始化变量 #endregion try { #region 裁剪区域 #endregion #region 算子逻辑 Record = new ObjectRecord(); var oBaseSegment = Params.Inputs["基准线段"]; var oInterSegment = Params.Inputs["相交线段"]; bool bIntersection = Convert.ToBoolean(Params.Inputs["是否期望有交点"]); bool bOnSegment = Convert.ToBoolean(Params.Inputs["要求交点在线段上"]); double Angle = 0; Point2d? InterPoint = null; if (!(oBaseSegment is HSegment) || !(oInterSegment is HSegment)) { Msg = "输入的线段类型错误"; Result = false; AsyncLogHelper.Error(Msg); return; } HSegment BaseSegment = (HSegment)oBaseSegment; HSegment InterSegment = (HSegment)oInterSegment; try { InterPoint = TAlgorithm.GetLineIntersection(BaseSegment.StartPoint.ToPoint2d(), BaseSegment.EndPoint.ToPoint2d() , InterSegment.StartPoint.ToPoint2d(), InterSegment.EndPoint.ToPoint2d(), bOnSegment); HOperatorSet.AngleLl(BaseSegment.StartPoint.Row, BaseSegment.StartPoint.Column, BaseSegment.EndPoint.Row, BaseSegment.EndPoint.Column , InterSegment.StartPoint.Row, InterSegment.StartPoint.Column, InterSegment.EndPoint.Row, InterSegment.EndPoint.Column, out HTuple hv_Phi); Angle = hv_Phi.D * 180.0 / Math.PI; } catch { Angle = 0; InterPoint = null; } #endregion #region 结果处理 if (InterPoint == null) { Params.Outputs["X"] = 0; Params.Outputs["Y"] = 0; Params.Outputs["Angle"] = Angle; Params.Outputs["HPoint"] = new HPoint(); } else { HOperatorSet.GenCrossContourXld(out HObject CrossXld, InterPoint.Value.Y, InterPoint.Value.X, 20, 0); Record.AddXld(CrossXld); Params.Outputs["X"] = InterPoint.Value.X; Params.Outputs["Y"] = InterPoint.Value.Y; Params.Outputs["Angle"] = Angle; Params.Outputs["HPoint"] = new HPoint(InterPoint.Value.X, InterPoint.Value.Y); } #endregion #region 生成OutputImage给后续处理 try { OutputImage = InputImage; } catch (Exception ex) { Msg = "生成OutputImage失败,原因是:" + ex.ToString(); Result = false; AsyncLogHelper.Error(Msg); return; } #endregion if (Msg == "运行超时") { Result = false; return; } if (bIntersection && InterPoint == null) { Msg = "两条线段没有交点"; Result = false; } else if (!bIntersection && InterPoint != null) { Msg = "两条线段有交点"; Result = false; } AsyncLogHelper.Error(Msg); return; } catch (Exception ex) { Msg = "运行失败,原因是:" + ex.ToString().TrimEnd(); OutputImage = null; Result = false; AsyncLogHelper.Error(Msg); return; } finally { bCompleted = true; #region 内存释放 #endregion } } } }