using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using CvBase;
using CWindowTool;
using System.IO;
using Newtonsoft.Json;
namespace CvImageTool.GenLineByIntersectLL
{
public class GenLineByIntersectLLTool
{
public FileINI INI = new FileINI();
public GenLineByIntersectLLParam genLineByIntersectLLParam; //抓圆参数类
private string cProcessSettingFilePath; //当前流程配置文件的路径
private string cImageOptSettingFilePath; //当前算法参数文件路径
public string imageSelect = ""; //图像输入参数选择项
public string MLSelect = ""; //仿射矩阵输入参数选择项
public string AngleSelect = ""; //角度输入参数选择项
public string IntersectionPointSelect = ""; //角度输入参数选择项
private int cProcessIndex = -1; //当前归属流程的索引
private int cImageOptIndex = -1; //当前归属算法的索引
public bool isShowMeasureLine = false;
public bool isShowResultLine = false;
public bool isShowResultLinePoint = false;
public HObject Image;
public HTuple MLine;
public HObject RLine;
public HTuple Angle;
public HTuple IntersectionPoint;
private List<BaseProcess> processes;
private CWindows[] cCWindows;
public GenLineByIntersectLLTool(int cProcessIndex, int cImageOptIndex, string cDescription,
string cProcessSettingFilePath, string cImageOptSettingFilePath, List<BaseProcess> baseProcesses, CWindows[] CWindows)
{
this.cProcessSettingFilePath = cProcessSettingFilePath;
this.cImageOptSettingFilePath = cImageOptSettingFilePath;
this.cProcessIndex = cProcessIndex;
this.cImageOptIndex = cImageOptIndex;
this.cCWindows = CWindows;
this.processes = baseProcesses;
genLineByIntersectLLParam = new GenLineByIntersectLLParam();
Image = new HObject();
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool ReadFile(string cProcessDescription)
{
imageSelect = ""; MLSelect = ""; AngleSelect = "";
if (!File.Exists(cProcessSettingFilePath) || !File.Exists(cImageOptSettingFilePath)) return false;
imageSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "imageSelect", "", cProcessSettingFilePath);
MLSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "MLSelect", "", cProcessSettingFilePath);
AngleSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "AngleSelect", "", cProcessSettingFilePath);
IntersectionPointSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "IntersectionPointSelect", "", cProcessSettingFilePath);
isShowMeasureLine = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowMeasureLine", "", cProcessSettingFilePath));
isShowResultLine = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResultLine", "", cProcessSettingFilePath));
isShowResultLinePoint = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResultLinePoint", "", cProcessSettingFilePath));
genLineByIntersectLLParam = JsonConvert.DeserializeObject<GenLineByIntersectLLParam>(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, "imageSelect", imageSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "MLSelect", MLSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "AngleSelect", AngleSelect.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "IntersectionPointSelect", IntersectionPointSelect.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowMeasureLine", isShowMeasureLine.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResultLine", isShowResultLine.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResultLinePoint", isShowResultLinePoint.ToString(), cProcessSettingFilePath);
var json = JsonConvert.SerializeObject(genLineByIntersectLLParam, Formatting.Indented);
File.WriteAllText(cImageOptSettingFilePath, json);
return true;
}
public void Running(out HObject measureLine, out HObject resultLine, out HTuple LinePoint)
{
HOperatorSet.GenEmptyObj(out measureLine);
HOperatorSet.GenEmptyObj(out resultLine);
LinePoint = new HTuple();
if (Image == null) return;
//获取测量直线
string[] imageOptAndParamName = MLSelect.Split('_');
ProcessTool.Instance.FindImageOpt(processes[cProcessIndex], imageOptAndParamName[0], out BaseImageOpt byImageOpt, out int byIndex);
if (byIndex >= 0)
processes[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamName[1], out MLine);
else
MLine = null;
if (MLine == null || MLine.Length == 0) return;
//获取角度
imageOptAndParamName = AngleSelect.Split('_');
ProcessTool.Instance.FindImageOpt(processes[cProcessIndex], imageOptAndParamName[0], out byImageOpt, out byIndex);
if (byIndex >= 0)
processes[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamName[1], out Angle);
else
Angle = null;
if (Angle == null || Angle.Length == 0) return;
//获取交点
imageOptAndParamName = IntersectionPointSelect.Split('_');
ProcessTool.Instance.FindImageOpt(processes[cProcessIndex], imageOptAndParamName[0], out byImageOpt, out byIndex);
if (byIndex >= 0)
processes[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamName[1], out IntersectionPoint);
else
IntersectionPoint = null;
if (IntersectionPoint == null || IntersectionPoint.Length == 0) return;
//计算旋转角度和仿射矩阵
Angle = Angle / 2.0 * genLineByIntersectLLParam.AngleSigam + genLineByIntersectLLParam.AngleOffset;
//HOperatorSet.VectorAngleToRigid(IntersectionPoint[0], IntersectionPoint[1], 0, IntersectionPoint[0],
// IntersectionPoint[1], Angle, out HTuple Matrix1);
HOperatorSet.HomMat2dIdentity(out HTuple Matrix1);
HOperatorSet.HomMat2dRotate(Matrix1, Angle, IntersectionPoint[0], IntersectionPoint[1], out HTuple Matrix2);
HOperatorSet.HomMat2dTranslate(Matrix2, genLineByIntersectLLParam.YOffset, genLineByIntersectLLParam.XOffset, out HTuple Matrix3);
HTuple LROW = new HTuple();
LROW.Append(MLine[0]);
LROW.Append(MLine[2]);
HTuple LCOL = new HTuple();
LCOL.Append(MLine[1]);
LCOL.Append(MLine[3]);
HOperatorSet.SetSystem("clip_region", "false");
HOperatorSet.AffineTransPoint2d(Matrix3, LROW, LCOL, out HTuple row, out HTuple col);
//HOperatorSet.GenRegionLine(out resultLine, row[0], col[0], row[1], col[1]);
HTuple lRow = new HTuple();
HTuple lCol = new HTuple();
lRow.Append(row[0]);
lRow.Append(row[1]);
lCol.Append(col[0]);
lCol.Append(col[1]);
HOperatorSet.GenContourPolygonXld(out resultLine, lRow, lCol);
//HOperatorSet.GenRegionLine(out measureLine, LROW[0], LCOL[0], LROW[1], LCOL[1]);
lRow = new HTuple();
lCol = new HTuple();
lRow.Append(LROW[0]);
lRow.Append(LROW[1]);
lCol.Append(LCOL[0]);
lCol.Append(LCOL[1]);
HOperatorSet.GenContourPolygonXld(out measureLine, lRow, lCol);
LinePoint = row[0];
LinePoint.Append(col[0]);
LinePoint.Append(row[1]);
LinePoint.Append(col[1]);
}
}
}