/* * Copyright (c) 2025 LG Electronics Inc. * SPDX-License-Identifier: LicenseRef-LGE-Proprietary */ using Aurora.Api.DataType; using VisionCommon; using Matrox.MatroxImagingLibrary; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MilUI { public class RulerMode : DisplayMode { private MIL_ID _displayId; private MIL_ID _graphicContext; private MIL_ID _graphicListId; private ImageSize _imageSize; private StaticPoint _pixelSize; private MilGraphics _milGraphics; private MilGraphics _tempMilGraphics; private ImagePoint _measurePointStart = new ImagePoint(); private ImagePoint _measurePointMove = new ImagePoint(); private ImagePoint _measurePointEnd = new ImagePoint(); private List _colors; private bool _isMouseRighttDown = false; private int _colorIndex = 0; private int _crossLength = 30; public RulerMode( MIL_ID displayId, MIL_ID graphicContext, MIL_ID graphicListId, ImageSize imageSize, StaticPoint pixelSize) { _displayId = displayId; _graphicContext = graphicContext; _graphicListId = graphicListId; _imageSize = imageSize; _pixelSize = pixelSize; _milGraphics = new MilGraphics(_displayId, _graphicListId); _tempMilGraphics = new MilGraphics(_displayId, _graphicListId); _colors = new List() { MIL.M_COLOR_YELLOW, MIL.M_COLOR_MAGENTA }; } public MIL_INT GetColor() { return _colors[_colorIndex]; } public void ChangeColor() { _colorIndex++; if (_colors.Count <= _colorIndex) { _colorIndex = 0; } } public override void MouseLeftDown(MIL_ID EventID) { } public override void MouseMove(MIL_ID EventID) { if (_isMouseRighttDown) { var currentMousePoint = GetMousePoint(EventID); if (_measurePointMove.X != currentMousePoint.X || _measurePointMove.Y != currentMousePoint.Y) { MIL_INT combinationKeys = 0; MIL.MdispGetHookInfo(EventID, MIL.M_COMBINATION_KEYS, ref combinationKeys); AddMeasureInfo(GetColor(), _tempMilGraphics, combinationKeys, _measurePointStart, currentMousePoint); } _measurePointMove = currentMousePoint; } } public override void MouseRightDown(MIL_ID EventID) { MIL_INT combinationKeys = 0; MIL.MdispGetHookInfo(EventID, MIL.M_COMBINATION_KEYS, ref combinationKeys); if (!_isMouseRighttDown) { _isMouseRighttDown = true; _measurePointStart = GetMousePoint(EventID); DisplayDrawer.AddCross( _displayId, _graphicContext, _graphicListId, GetColor(), _milGraphics, new Cross(_measurePointStart, _crossLength)); } else { _isMouseRighttDown = false; _measurePointEnd = GetMousePoint(EventID); AddMeasureInfo(GetColor(), _milGraphics, combinationKeys, _measurePointStart, _measurePointEnd); ChangeColor(); } } private bool IsLocatedInImageArea(ImageSize imageSize, ImagePoint p) { return (p.X < imageSize.Width) && (p.Y < imageSize.Height); } public override void GraphicSelectionModified(MIL_ID EventID) { } public override void Clear() { _milGraphics.Clear(); _tempMilGraphics.Clear(); } private void AddMeasureInfo( MIL_INT color, MilGraphics milGraphics, MIL_INT combinationKeys, ImagePoint pStart, ImagePoint pEnd) { if ((combinationKeys & MIL.M_KEY_SHIFT) == MIL.M_KEY_SHIFT) { if (IsHorizontalLine(pStart, pEnd)) { pEnd.Y = pStart.Y; } else { pEnd.X = pStart.X; } } _tempMilGraphics.Clear(); DisplayDrawer.AddCross(_displayId, _graphicContext, _graphicListId, color, milGraphics, new Cross(pEnd, _crossLength)); DisplayDrawer.AddLine( _graphicContext, _graphicListId, color, milGraphics, new Line(pStart, pEnd), false); var msgs = GetDistanceText(pStart, pEnd); msgs.Add(GetAngleText(pStart, pEnd)); DisplayDrawer.AddTextList( _displayId, _graphicContext, _graphicListId, color, milGraphics, _imageSize, new ImagePoint(pEnd.X + 10, pEnd.Y + 10), msgs); } private List GetDistanceText(ImagePoint p1, ImagePoint p2) { var pixelDistance = MathUtil.CalculateDistance(p1, p2); var worldDistance = GetDistance(p1, p2, _pixelSize); return new List() { string.Format("{0:0.0000} [px]", pixelDistance), string.Format("{0:0.0000} [mm]", worldDistance) }; } private double GetDistance(ImagePoint p1, ImagePoint p2, StaticPoint scale) { return Math.Sqrt( Math.Pow((p1.X - p2.X) * scale.X, 2) + Math.Pow((p1.Y - p2.Y) * scale.Y, 2)); } private string GetAngleText(ImagePoint p1, ImagePoint p2) { return string.Format("{0:0.0000} [deg]", GetLineAngle(p1, p2).Degree); } private bool IsVerticalLine(ImagePoint p1, ImagePoint p2) { return !IsHorizontalLine(p1, p2); } private bool IsHorizontalLine(ImagePoint p1, ImagePoint p2) { var lineAngle = GetLineAngle(p1, p2); return Math.Abs(lineAngle.Degree) < 45 || Math.Abs(lineAngle.Degree) > (90 + 45); } public static Angle GetLineAngle(ImagePoint p1, ImagePoint p2) { double diffX = p2.X - p1.X; double diffY = p2.Y - p1.Y; return Angle.CreateWithRadian(Math.Atan2(diffY, diffX)); } } }