291 lines
10 KiB
C#
291 lines
10 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using CogCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Cognex.VisionPro;
|
|
using Cognex.VisionPro.Caliper;
|
|
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
|
|
namespace CogPattern {
|
|
public class LineIntersectionRunParam : IRunParam, ICloneable {
|
|
public int PatternId1 { get; set; } = -1;
|
|
public int PatternId2 { get; set; } = -1;
|
|
|
|
public object Clone() {
|
|
return new LineIntersectionRunParam() {
|
|
PatternId1 = PatternId1,
|
|
PatternId2 = PatternId2
|
|
};
|
|
}
|
|
}
|
|
|
|
public class LineIntersection : PatternBase, IPatternFinder, IPatternBlock, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".json";
|
|
private PatternBlockParamExt _paramExt = null;
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
|
|
public LineIntersection(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"LineIntersection_{patternInfo.GetPatternIndex()}");
|
|
Init();
|
|
}
|
|
|
|
private void Init() {
|
|
if (E1.OK != Load()) {
|
|
Create();
|
|
}
|
|
}
|
|
|
|
public string GetPatternFilePath(string patternName = PatternConstants.FILE_NAME_PATTERN) {
|
|
return Path.Combine(new string[] {
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
patternName + FILE_EXT_PATTERN
|
|
});
|
|
}
|
|
|
|
public int Load() {
|
|
_paramExt = LoadParamExt();
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Save() {
|
|
return SaveParamExt(_paramExt);
|
|
}
|
|
|
|
public int Create() {
|
|
_paramExt = LoadParamExt();
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Remove() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (Directory.Exists(patternDirPath)) {
|
|
Directory.Delete(patternDirPath, recursive: true);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public ROI GetFindROI() {
|
|
// 수정 필요
|
|
return new ROI();
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
return new LineIntersectionRunParam() {
|
|
PatternId1 = _paramExt.PatternId1,
|
|
PatternId2 = _paramExt.PatternId2
|
|
};
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
var param = runParam as LineIntersectionRunParam;
|
|
if (param is null) {
|
|
return E1.GENERIC;
|
|
}
|
|
_paramExt.PatternId1 = param.PatternId1;
|
|
_paramExt.PatternId2 = param.PatternId2;
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
Results = new List<LineIntersectionResult>()
|
|
};
|
|
}
|
|
|
|
public object GetTool() {
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
var cogCameraDisplay = cameraDisplay as ICogCameraDisplay;
|
|
|
|
foreach (var result in patternResult.Results) {
|
|
var intersectionResult = result as LineIntersectionResult;
|
|
DrawLinePatternResult(patternResult, intersectionResult.Line1PatternResult, cogCameraDisplay);
|
|
DrawLinePatternResult(patternResult, intersectionResult.Line2PatternResult, cogCameraDisplay);
|
|
}
|
|
|
|
// Cross
|
|
if (patternResult.Status == eFoundStatus.FoundOK) {
|
|
var crosses = new List<Cross>();
|
|
|
|
foreach (BaseResult baseResult in patternResult.Results) {
|
|
var result = baseResult as LineIntersectionResult;
|
|
var color = GraphicColors.GetFindResultColor(eFoundStatus.NotFound, isFixturePattern);
|
|
var cross = new Cross(result.ImagePosition.Clone() as ImagePoint, 50);
|
|
crosses.Add(cross);
|
|
|
|
cogCameraDisplay.SetPatternTracking(cross);
|
|
cogCameraDisplay.AddCross(cross, color);
|
|
|
|
// Text
|
|
var msgs = new List<string>();
|
|
msgs.Add(string.Format("I({0:0.0}, {1:0.0})", result.ImagePosition.X, result.ImagePosition.Y));
|
|
msgs.Add(result.ConvertMaterialPosition());
|
|
msgs.Add(string.Format("TactTime : {0:0.000}", patternResult.TactTimeInMilliSec));
|
|
msgs.Add(string.Format("ID : {0}_{1}", patternResult.PatternIndex.PatternId, patternResult.PatternIndex.PatternSubId));
|
|
|
|
cogCameraDisplay.AddTextList(
|
|
color, new ImagePoint(result.ImagePosition.X, result.ImagePosition.Y), msgs);
|
|
}
|
|
cogCameraDisplay.DrawPatternTrackings(crosses);
|
|
}
|
|
}
|
|
|
|
private void DrawLinePatternResult(PatternResult patternResult, PatternResult linePatternResult, ICogCameraDisplay cogCameraDisplay) {
|
|
if (linePatternResult.Results != null) {
|
|
foreach (var pr in linePatternResult.Results) {
|
|
var lineFinderResult = pr as LineFinderResult;
|
|
if (lineFinderResult != null) {
|
|
CogMeasureLine.Draw((CogFindLineTool)lineFinderResult.ContextId, patternResult, cogCameraDisplay);
|
|
CogMeasureLine.DrawCaliperROI((CogFindLineTool)lineFinderResult.ContextId, cogCameraDisplay, linePatternResult.FindRoi);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
#region IPatternBlock
|
|
public List<int> GetPatternBlockIds() {
|
|
return new List<int>() { _paramExt.PatternId1, _paramExt.PatternId2 };
|
|
}
|
|
|
|
public PatternResult Find(List<PatternResult> patternResults) {
|
|
if (patternResults == null || patternResults.Count != 2) {
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
TactTimeInMilliSec = 0
|
|
};
|
|
}
|
|
|
|
var intersectionResults = new List<LineIntersectionResult>();
|
|
var line1Results = patternResults[0].Results as List<LineFinderResult>;
|
|
var line2Results = patternResults[1].Results as List<LineFinderResult>;
|
|
|
|
if (line1Results == null ||
|
|
line2Results == null ||
|
|
line1Results.Count != 1 ||
|
|
line2Results.Count != 1) {
|
|
|
|
intersectionResults.Add(
|
|
new LineIntersectionResult(
|
|
new ImagePoint(),
|
|
new ImagePoint(),
|
|
patternResults[0],
|
|
patternResults[1]));
|
|
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
Results = intersectionResults,
|
|
TactTimeInMilliSec = 0
|
|
};
|
|
}
|
|
|
|
var line1Result = line1Results[0];
|
|
var line2Result = line2Results[0];
|
|
|
|
intersectionResults.Add(new LineIntersectionResult(
|
|
NativePattern.GetIntersectionPoint<ImagePoint>(line1Result.LineEquation, line2Result.LineEquation),
|
|
new ImagePoint(),
|
|
patternResults[0],
|
|
patternResults[1]));
|
|
|
|
return new PatternResult() {
|
|
Status = (patternResults.Count > 0) ? eFoundStatus.FoundOK : eFoundStatus.NotFound,
|
|
Results = intersectionResults,
|
|
TactTimeInMilliSec = 0
|
|
};
|
|
}
|
|
|
|
public Bitmap GetThumbnail(List<ROI> findRois) {
|
|
if (findRois == null || findRois.Count != 2) {
|
|
return ThumbnailUtil.GetDefaultImage();
|
|
}
|
|
|
|
try {
|
|
var imageSize = ThumbnailUtil.GetImageSize();
|
|
var center = new ImagePoint(imageSize.Width / 2, imageSize.Height / 2);
|
|
var thumnailImage = new Bitmap(
|
|
imageSize.Width, imageSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
|
var pen = new Pen(new SolidBrush(Color.FromKnownColor(KnownColor.White)), 3);
|
|
|
|
using (var graphics = Graphics.FromImage(thumnailImage)) {
|
|
foreach (var findRoi in findRois) {
|
|
var lineStart = MathUtil.Rotate(new ImagePoint(0, (int)center.Y), findRoi.Angle, center);
|
|
var lineEnd = MathUtil.Rotate(new ImagePoint(imageSize.Width, (int)center.Y), findRoi.Angle, center);
|
|
graphics.DrawLine(
|
|
pen,
|
|
new Point((int)lineStart.X, (int)lineStart.Y),
|
|
new Point((int)lineEnd.X, (int)lineEnd.Y));
|
|
}
|
|
}
|
|
return thumnailImage;
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
|
|
return ThumbnailUtil.GetDefaultImage();
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
FreeContexts();
|
|
}
|
|
|
|
private void FreeContexts() {
|
|
//CogMeasureCommon.FreeContext(_contextId);
|
|
//_contextId = MIL.M_NULL;
|
|
}
|
|
#endregion
|
|
|
|
#region Line Intersection Specific
|
|
public bool IsParamExtExist() {
|
|
return PatternBlockParamExt.Exists(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
public PatternBlockParamExt LoadParamExt() {
|
|
return PatternBlockParamExt.Load(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
public int SaveParamExt(PatternBlockParamExt patternBlockParam) {
|
|
return PatternBlockParamExt.Save(
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
patternBlockParam);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|