using CvBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using CWindowTool;
using System.IO;
using Newtonsoft.Json;
namespace CvImageTool.IntersectionLL
{
public class IntersectionLLTool
{
public string[] selectStr = { "all", "first", "last" }; //选择项变量字符串数组
public FileINI INI = new FileINI();
public IntersectionLLParam intersectionLLParam; //匹配参数
private string cProcessSettingFilePath; //当前流程配置文件的路径
private string cImageOptSettingFilePath; //当前算法参数文件路径
public string imageSelect = ""; //图像输入参数选择项
public string line1Select = ""; //直线1输入参数选择项
public string line2Select = ""; //直线2输入参数选择项
private int cProcessIndex = -1; //当前归属流程的索引
private int cImageOptIndex = -1; //当前归属算法的索引
public bool isShowResultLine = false;
public bool isShowResult = false;
public bool isShowMeasureLines = false;
public HObject Image;
public HTuple Line01;
public HTuple Line02;
private List<BaseProcess> process;
private CWindows[] cCWindows;
public IntersectionLLTool(int cProcessIndex, int cImageOptIndex,
string cProcessSettingFilePath, string cImageOptSettingFilePath,
List<BaseProcess> processes, CWindows[] cWindows)
{
this.cProcessSettingFilePath = cProcessSettingFilePath;
this.cImageOptSettingFilePath = cImageOptSettingFilePath;
this.cProcessIndex = cProcessIndex;
this.cImageOptIndex = cImageOptIndex;
this.process = processes;
this.cCWindows = cWindows;
intersectionLLParam = new IntersectionLLParam();
Image = new HObject();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool ReadFile(string cProcessDescription)
{
imageSelect = ""; line1Select = ""; line2Select = "";
if (!File.Exists(cProcessSettingFilePath) || !File.Exists(cImageOptSettingFilePath)) return false;
imageSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Image", "", cProcessSettingFilePath);
line1Select = FileINI.ReadValueFromIniFile(cProcessDescription, "Line1", "", cProcessSettingFilePath);
line2Select = FileINI.ReadValueFromIniFile(cProcessDescription, "Line2", "", cProcessSettingFilePath);
isShowResultLine = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResultLine", "", cProcessSettingFilePath));
isShowResult = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResult", "", cProcessSettingFilePath));
isShowMeasureLines = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowMeasureLines", "", cProcessSettingFilePath));
intersectionLLParam = JsonConvert.DeserializeObject<IntersectionLLParam>(File.ReadAllText(cImageOptSettingFilePath));
return true;
}
/// <summary>
/// 写配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool WriteFile(string cProcessDescription)
{
FileINI.WriteValueFromIniFile(cProcessDescription, "Image", imageSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "Line1", line1Select, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "Line2", line2Select, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResultLine", isShowResultLine.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResult", isShowResult.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowMeasureLines", isShowMeasureLines.ToString(), cProcessSettingFilePath);
var json = JsonConvert.SerializeObject(intersectionLLParam, Formatting.Indented);
File.WriteAllText(cImageOptSettingFilePath, json);
return true;
}
public void Running(out HObject MearsureLine, out HObject Cross, out HTuple intersectionPoint, out HTuple angle)
{
MearsureLine = new HObject(); Cross = new HObject(); intersectionPoint = new HTuple(); angle = new HTuple();
//获取直线1
string[] imageOptAndParamNameL1 = line1Select.Split('_');
ProcessTool.Instance.FindImageOpt(process[cProcessIndex], imageOptAndParamNameL1[0], out BaseImageOpt byImageOpt, out int byIndex);
if (byIndex >= 0)
process[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamNameL1[1], out Line01);
else
Line01 = null;
//获取直线2
string[] imageOptAndParamNameL2 = line2Select.Split('_');
byIndex = -1;
ProcessTool.Instance.FindImageOpt(process[cProcessIndex], imageOptAndParamNameL2[0], out byImageOpt, out byIndex);
if (byIndex >= 0)
process[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamNameL2[1], out Line02);
else
Line02 = null;
if (Line01 == null || Line02 == null || Line01.Length < 4 || Line02.Length < 4) return;
HOperatorSet.SetSystem("clip_region", "false");
//计算直线距离
HOperatorSet.IntersectionLl(Line01[0], Line01[1], Line01[2], Line01[3],
Line02[0], Line02[1], Line02[2], Line02[3], out HTuple row, out HTuple col, out HTuple isParall);
HOperatorSet.AngleLl(Line01[0], Line01[1], Line01[2], Line01[3], Line02[0], Line02[1], Line02[2], Line02[3], out angle);
angle = AngleConvert.Rads2Degress(angle);
HObject L1, L2;
HOperatorSet.DistancePp(row, col, Line01[0], Line01[1], out HTuple d1);
HOperatorSet.DistancePp(row, col, Line01[2], Line01[3], out HTuple d2);
if (d1 > d2)
{
HTuple lRow = new HTuple();
HTuple lCol = new HTuple();
lRow.Append(Line01[0]);
lRow.Append(row);
lCol.Append(Line01[1]);
lCol.Append(col);
HOperatorSet.GenContourPolygonXld(out L1, lRow, lCol);
//HOperatorSet.GenRegionLine(out L1, Line01[0], Line01[1], row, col);
}
else
{
HTuple lRow = new HTuple();
HTuple lCol = new HTuple();
lRow.Append(Line01[2]);
lRow.Append(row);
lCol.Append(Line01[3]);
lCol.Append(col);
HOperatorSet.GenContourPolygonXld(out L1, lRow, lCol);
//HOperatorSet.GenRegionLine(out L1, row, col, Line01[2], Line01[3]);
}
HOperatorSet.DistancePp(row, col, Line02[0], Line02[1], out d1);
HOperatorSet.DistancePp(row, col, Line02[2], Line02[3], out d2);
if (d1 > d2)
{
HTuple lRow = new HTuple();
HTuple lCol = new HTuple();
lRow.Append(Line02[0]);
lRow.Append(row);
lCol.Append(Line02[1]);
lCol.Append(col);
HOperatorSet.GenContourPolygonXld(out L2, lRow, lCol);
//HOperatorSet.GenRegionLine(out L2, Line02[0], Line02[1], row, col);
}
else
{
HTuple lRow = new HTuple();
HTuple lCol = new HTuple();
lRow.Append(Line02[2]);
lRow.Append(row);
lCol.Append(Line02[3]);
lCol.Append(col);
HOperatorSet.GenContourPolygonXld(out L2, lRow, lCol);
//HOperatorSet.GenRegionLine(out L2, row, col, Line02[2], Line02[3]);
}
HOperatorSet.ConcatObj(L1, L2, out MearsureLine);
HOperatorSet.GenCrossContourXld(out Cross, row, col, 6, 1);
L1.Dispose(); L2.Dispose();
intersectionPoint = row;
intersectionPoint.Append(col);
}
}
}