/* * Copyright (c) 2025 LG Electronics Inc. * SPDX-License-Identifier: LicenseRef-LGE-Proprietary */ using Aurora.Api; using MilCommon; using VisionCommon; using Hermes; using Matrox.MatroxImagingLibrary; using System; using System.Collections.Generic; using Aurora.Api.DataType; namespace MilPattern { public enum ePolarity { Any = MIL.M_ANY, Negative = MIL.M_NEGATIVE, Positive = MIL.M_POSITIVE, Opposite = MIL.M_OPPOSITE } internal static class MilMeasureCommon { private static Logger _Logger = new Logger(typeof(MilMeasureCommon).Name); public static MIL_ID CreateContext(MIL_ID milSystem, MIL_ID markerType) { MIL_ID contextId = MIL.M_NULL; MIL.MmeasAllocMarker(milSystem, markerType, MIL.M_DEFAULT, ref contextId); MIL.MmeasSetMarker(contextId, MIL.M_SUB_REGIONS_NUMBER, 10, MIL.M_NULL); MIL.MmeasSetMarker(contextId, MIL.M_BOX_ANGLE_TOLERANCE, 360, MIL.M_NULL); // Default : 각도 제한 없음 return contextId; } public static MIL_ID LoadContext(MIL_ID milSystem, string filePath) { MIL_ID contextId = MIL.M_NULL; MIL.MmeasRestoreMarker(filePath, milSystem, MIL.M_DEFAULT, ref contextId); return contextId; } public static int SaveContext(MIL_ID contextId, string filePath) { if (contextId != MIL.M_NULL && filePath != null) { MIL.MmeasSaveMarker(filePath, contextId, MIL.M_DEFAULT); return E1.OK; } return E1.GENERIC; } public static void FreeContext(MIL_ID contextId) { if (contextId != MIL.M_NULL) { MIL.MmeasFree(contextId); } } private static MIL_ID CreateResultContext(MIL_ID contextId) { var milSystem = GetSystemOfContext(contextId); return MIL.MmeasAllocResult(milSystem, MIL.M_DEFAULT, MIL.M_NULL); } private static MIL_ID GetSystemOfContext(MIL_ID contextId) { return (MIL_ID)MIL.MmeasInquire(contextId, MIL.M_OWNER_SYSTEM, MIL.M_NULL, MIL.M_NULL); } public static ROI GetFindROI(MIL_ID contextId) { double boxCenterX = 0, boxCenterY = 0; double width = 0, height = 0; double angleDeg = 0; MIL.MmeasInquire(contextId, MIL.M_BOX_CENTER, ref boxCenterX, ref boxCenterY); MIL.MmeasInquire(contextId, MIL.M_BOX_SIZE, ref width, ref height); MIL.MmeasInquire(contextId, MIL.M_BOX_ANGLE, ref angleDeg, MIL.M_NULL); return new ROI( new ImagePoint(boxCenterX, boxCenterY), (int)width, (int)height, Angle.CreateWithDegree(angleDeg)); } public static int SetFindROI(MIL_ID contextId, ROI findRoi) { MIL.MmeasSetMarker(contextId, MIL.M_BOX_ANGLE_REFERENCE, MIL.M_BOX_CENTER, MIL.M_NULL); MIL.MmeasSetMarker(contextId, MIL.M_BOX_CENTER, findRoi.CenterX, findRoi.CenterY); MIL.MmeasSetMarker(contextId, MIL.M_BOX_SIZE, findRoi.Width, findRoi.Height); MIL.MmeasSetMarker(contextId, MIL.M_BOX_ANGLE, findRoi.Angle.Degree, MIL.M_NULL); return E1.OK; } public static PatternResult Find( MIL_ID contextId, MIL_ID image, Func funcExtractResults, ROI findRoi) { var patternResult = new PatternResult(); var oldFindRoi = GetFindROI(contextId); try { SetFindROI(contextId, findRoi); patternResult = Find(contextId, image, funcExtractResults); } catch (Exception e) { _Logger.FuncE(e.Message); } finally { SetFindROI(contextId, oldFindRoi); } return patternResult; } public static PatternResult Find( MIL_ID contextId, MIL_ID image, Func funcExtractResults) { MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, MIL.M_NULL); if (MilEnvironment.IsColorImage(image)) { var grayImageId = MilEnvironment.ConvertToGray(image); MIL.MmeasFindMarker(MIL.M_DEFAULT, grayImageId, contextId, MIL.M_DEFAULT); MilEnvironment.FreeImage(grayImageId); } else { MIL.MmeasFindMarker(MIL.M_DEFAULT, image, contextId, MIL.M_DEFAULT); } double tactTime = 0; MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref tactTime); return funcExtractResults(contextId, tactTime); } public static void DrawSubPositions(MIL_ID contextId, IMilCameraDisplay milCameraDisplay) { MIL_INT subRegionNumber = 0; MIL.MmeasInquire(contextId, MIL.M_SUB_REGIONS_NUMBER + MIL.M_TYPE_MIL_INT, ref subRegionNumber, MIL.M_NULL); if (0 < subRegionNumber) { var subEdgeFirstX = new double[subRegionNumber]; var subEdgeFirstY = new double[subRegionNumber]; var subEdgeSecondX = new double[subRegionNumber]; var subEdgeSecondY = new double[subRegionNumber]; MIL.MmeasGetResult(contextId, MIL.M_SUB_EDGES_POSITION + MIL.M_EDGE_FIRST, subEdgeFirstX, subEdgeFirstY); MIL.MmeasGetResult(contextId, MIL.M_SUB_EDGES_POSITION + MIL.M_EDGE_SECOND, subEdgeSecondX, subEdgeSecondY); int crossLength = 3; var crosses = new List(); for (int i = 0; i < subEdgeFirstX.Length; i++) { crosses.Add(new Cross(new ImagePoint(subEdgeFirstX[i], subEdgeFirstY[i]), crossLength)); crosses.Add(new Cross(new ImagePoint(subEdgeSecondX[i], subEdgeSecondY[i]), crossLength)); } milCameraDisplay.AddCrosses(crosses, GraphicColors.PatternAdditionInfo); } } public static void DrawPosition(MIL_ID contextId, IMilCameraDisplay milCameraDisplay) { MIL_INT numOfResult = 0; MIL.MmeasGetResult(contextId, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, ref numOfResult); if (0 < numOfResult) { double positionX = 0, positionY = 0; var crosses = new List(); MIL.MmeasGetResultSingle(contextId, MIL.M_POSITION, ref positionX, ref positionY, 0); var color = GraphicColors.PatternFoundOK; var cross = new Cross(new ImagePoint(positionX, positionY), 5); crosses.Add(cross); milCameraDisplay.SetPatternTracking(cross); milCameraDisplay.AddCross(cross, color); milCameraDisplay.DrawPatternTrackings(crosses); } } public static Line GetMinLengthLine( LineEquation lineEquation, ImageSize imageSize) { var line1 = new Line( new ImagePoint(0, lineEquation.GetY(0)), new ImagePoint(imageSize.Width, lineEquation.GetY(imageSize.Width))); var line2 = new Line( new ImagePoint(lineEquation.GetX(0), 0), new ImagePoint(lineEquation.GetX(imageSize.Height), imageSize.Height)); return line1.Length < line2.Length ? line1 : line2; } } }