784 lines
30 KiB
C#
784 lines
30 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using Aurora.Api.DataType;
|
|
using CogCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Cognex.VisionPro;
|
|
using CogCamera;
|
|
using CogPattern;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using VisionCalibrator;
|
|
using Cognex.VisionPro.Display;
|
|
using Cognex.VisionPro.PMAlign;
|
|
|
|
namespace CogUI {
|
|
/// <summary>
|
|
/// Interaction logic for CameraDisplay.xaml
|
|
/// </summary>
|
|
public partial class CameraDisplay : UserControl, ICogCameraDisplay, IUpdateRecipe, IDisposable {
|
|
private Logger _Logger;
|
|
private CamConfig _CamConfig;
|
|
private CameraBase _CameraDev;
|
|
private FovInfo _FovInfo;
|
|
private FovRecipe _FovRecipe;
|
|
private PatternRecipe _PatternRecipe;
|
|
private Calibrator _Calibrator;
|
|
private ImageSize _ImageSize;
|
|
private eDisplayType _DisplayType;
|
|
private IDisposable _EventDisposable;
|
|
private Dictionary<eRoiIdx, PatternInfo> _PatternInfos = new Dictionary<eRoiIdx, PatternInfo>();
|
|
|
|
private GridGuideLine _gridGuideLine;
|
|
private CogRuler _CogRuler;
|
|
private CogPointMarker _trainOrigin;
|
|
private CogRectangleAffine _trainRoi;
|
|
//private CogRectangleAffine _findRoi;
|
|
private Dictionary<eRoiIdx, CogRectangleAffine> _findRois = new Dictionary<eRoiIdx, CogRectangleAffine>();
|
|
|
|
private bool _Initialized = false;
|
|
|
|
public CameraDisplay() {
|
|
InitializeComponent();
|
|
}
|
|
|
|
public void SetPatternInfo(PatternInfo patternInfo, eRoiIdx roiIdx = eRoiIdx.Roi0) {
|
|
_PatternInfos[roiIdx] = patternInfo;
|
|
}
|
|
|
|
public int Init(CameraBase cameraDev, eDisplayType displayType, CamConfig camConfig, FovInfo fovInfo, FovRecipe fovRecipe) {
|
|
if (_Initialized) {
|
|
return E1.OK;
|
|
}
|
|
|
|
_Logger = new Logger(string.Format("CameraDisplay[{0}]", fovRecipe.FovName));
|
|
_CameraDev = cameraDev;
|
|
_CamConfig = camConfig;
|
|
_DisplayType = displayType;
|
|
_FovInfo = fovInfo;
|
|
_FovRecipe = fovRecipe;
|
|
_PatternRecipe = VisionRecipeManager.GetPatternRecipe(_FovInfo.ToSubSysInfo(), _FovInfo.FovId);
|
|
_Calibrator = CalibratorManager.GetCalibrator(_FovInfo);
|
|
_CogRuler = new CogRuler(cogDisplay, fovInfo);
|
|
_ImageSize = _CameraDev?.GetImageSize();
|
|
|
|
InitCogDisplay();
|
|
CreateGraphics();
|
|
DrawGuideLines();
|
|
|
|
_Initialized = true;
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private void InitCogDisplay() {
|
|
cogDisplay.AutoFitWithGraphics = true;
|
|
cogDisplay.VerticalScrollBar = false;
|
|
cogDisplay.HorizontalScrollBar = false;
|
|
cogDisplay.BackColor = System.Drawing.Color.Black;
|
|
cogDisplay.AutoFit = true;
|
|
cogDisplay.ContextFitImage += CogDisplay_ContextFitImage;
|
|
cogDisplay.MouseWheel += CogDisplay_MouseWheel;
|
|
cogDisplay.MouseMove += CogDisplay_MouseMove;
|
|
|
|
UpdateDefaultStatusBar();
|
|
}
|
|
|
|
private void CogDisplay_ContextFitImage(object sender, EventArgs e) {
|
|
UpdateCurrentZoomToStatusBar(e);
|
|
}
|
|
|
|
private void CogDisplay_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) {
|
|
UpdateCurrentZoomToStatusBar(e);
|
|
}
|
|
|
|
private void CogDisplay_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
|
|
UpdateCurrentPointToStatusBar(e);
|
|
}
|
|
|
|
private void UpdateDefaultStatusBar() {
|
|
curZoom.Text = $"Zoom ({0:F3})";
|
|
var defaultPoint = new ImagePoint(_ImageSize.Width, _ImageSize.Height);
|
|
curImagePoint.Text = defaultPoint.ToString();
|
|
curStaticPoint.Text = defaultPoint.ToString();
|
|
}
|
|
|
|
private void CreateGraphics() {
|
|
CreateTrainOrigin();
|
|
CreateTrainRoi();
|
|
CreateFindRoi();
|
|
_gridGuideLine = new GridGuideLine(cogDisplay, _FovInfo);
|
|
}
|
|
|
|
public void DrawGuideLines() {
|
|
var calFovRecipe = VisionRecipeManager.GetCalFovRecipe(_FovInfo);
|
|
var centerPos = new ImagePoint(_ImageSize.Width / 2, _ImageSize.Height / 2);
|
|
var color = CogColorConstants.Orange;
|
|
var lineStyle = CogGraphicLineStyleConstants.Dot;
|
|
|
|
_gridGuideLine.DrawGuideLines(_ImageSize, centerPos, 1, lineStyle, color, CogConstants.KEY_GUIDE_LINE, calFovRecipe.ErrRotationX);
|
|
//_gridGuideLine.DrawRefLine(_ImageSize, calFovRecipe.ErrRotationX);
|
|
//_gridGuideLine.DrawCoordDirection(_ImageSize);
|
|
}
|
|
|
|
private void CreateTrainOrigin() {
|
|
_trainOrigin = new CogPointMarker();
|
|
_trainOrigin.GraphicDOFEnable = CogPointMarkerDOFConstants.Position;
|
|
_trainOrigin.Interactive = true;
|
|
_trainOrigin.Color = GraphicColors.PatternTrainROI;
|
|
_trainOrigin.DragColor = GraphicColors.PatternTrainROI;
|
|
_trainOrigin.SelectedColor = GraphicColors.SelectedROI;
|
|
_trainOrigin.LineWidthInScreenPixels = CogConstants.LINE_WIDTH_IN_SCREEN_PIXELS;
|
|
_trainOrigin.DraggingStopped += new CogDraggingStoppedEventHandler(TrainOriginDraggingStopped);
|
|
_trainOrigin.Visible = false;
|
|
}
|
|
|
|
private void CreateTrainRoi() {
|
|
_trainRoi = new CogRectangleAffine();
|
|
_trainRoi.GraphicDOFEnable = CogRectangleAffineDOFConstants.All;
|
|
_trainRoi.Interactive = true;
|
|
_trainRoi.Color = GraphicColors.PatternTrainROI;
|
|
_trainRoi.DragColor = GraphicColors.PatternTrainROI;
|
|
_trainRoi.SelectedColor = GraphicColors.SelectedROI;
|
|
_trainRoi.LineWidthInScreenPixels = CogConstants.LINE_WIDTH_IN_SCREEN_PIXELS;
|
|
_trainRoi.Changed += Changed_TrainRoi;
|
|
_trainRoi.Visible = false;
|
|
}
|
|
|
|
private void CreateFindRoi(eRoiIdx roiIdx = eRoiIdx.Roi0) {
|
|
if (!_findRois.ContainsKey(roiIdx)) {
|
|
_findRois[roiIdx] = new CogRectangleAffine();
|
|
_findRois[roiIdx].GraphicDOFEnable = CogRectangleAffineDOFConstants.All;
|
|
_findRois[roiIdx].Interactive = true;
|
|
_findRois[roiIdx].Color = GraphicColors.PatternFindROI;
|
|
_findRois[roiIdx].DragColor = GraphicColors.PatternFindROI;
|
|
_findRois[roiIdx].SelectedColor = GraphicColors.SelectedROI;
|
|
_findRois[roiIdx].LineWidthInScreenPixels = CogConstants.LINE_WIDTH_IN_SCREEN_PIXELS;
|
|
_findRois[roiIdx].Changed += Changed_FindRoi;
|
|
_findRois[roiIdx].Visible = false;
|
|
}
|
|
}
|
|
|
|
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) {
|
|
if (IsLoaded && (e.WidthChanged || e.HeightChanged)) {
|
|
FitImage();
|
|
}
|
|
}
|
|
|
|
private void UserControl_Loaded(object sender, RoutedEventArgs e) {
|
|
FitImage();
|
|
}
|
|
|
|
public void ClearImage() {
|
|
CogEnvironment.ClearImage(cogDisplay.Image);
|
|
}
|
|
|
|
public int FitImage(ImageSize imageSize) {
|
|
try {
|
|
return FitImage();
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public int FitImage() {
|
|
try {
|
|
cogDisplay.Fit(true);
|
|
UpdateCurrentZoomToStatusBar(null);
|
|
|
|
return E1.OK;
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public int SetImage(object image) {
|
|
ICogImage srcImage = (ICogImage)image;
|
|
ICogImage dstImage = null;
|
|
|
|
if (cogDisplay.Image == null) {
|
|
dstImage = new CogImage8Grey(srcImage.Width, srcImage.Height);
|
|
}
|
|
else {
|
|
dstImage = cogDisplay.Image;
|
|
}
|
|
|
|
CogEnvironment.CopyImage(srcImage, ref dstImage);
|
|
cogDisplay.Image = dstImage;
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public bool IsSetImage() {
|
|
if (cogDisplay.Image != null) {
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public object GetImageObject() {
|
|
return (object)cogDisplay.Image;
|
|
}
|
|
|
|
public ICogImage GetImage() {
|
|
return cogDisplay.Image;
|
|
}
|
|
|
|
public CamRecipe GetCamRecipe() {
|
|
return _FovRecipe.CamRecipe;
|
|
}
|
|
|
|
public FovInfo GetDisplayFovInfo() {
|
|
return _FovInfo;
|
|
}
|
|
|
|
public eDisplayType GetDisplayType() {
|
|
return _DisplayType;
|
|
}
|
|
|
|
public CogDisplay GetDisplay() {
|
|
return cogDisplay;
|
|
}
|
|
|
|
public int RulerModeOn(eDisplayMode displayMode) {
|
|
switch (displayMode) {
|
|
case eDisplayMode.DistanceAngle:
|
|
_CogRuler.SetDistanceAngleMode();
|
|
break;
|
|
case eDisplayMode.Position:
|
|
_CogRuler.SetPositionMode();
|
|
break;
|
|
case eDisplayMode.Circle:
|
|
_CogRuler.SetCircleMode();
|
|
break;
|
|
case eDisplayMode.MovePosition:
|
|
_CogRuler.SetMovePositionMode();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int RulerModeOff() {
|
|
_CogRuler.Clear(true);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public void SetWideGridGuideLine() {
|
|
if (cogDisplay.Image != null) {
|
|
_gridGuideLine.WideOn(CogEnvironment.GetImageSize(cogDisplay.Image));
|
|
}
|
|
}
|
|
|
|
public void SetNarrowGridGuideLine() {
|
|
if (cogDisplay.Image != null) {
|
|
_gridGuideLine.NarrowOn(CogEnvironment.GetImageSize(cogDisplay.Image));
|
|
}
|
|
}
|
|
|
|
public void SetWideGridGuideLine_ErrRotation() {
|
|
if (cogDisplay.Image != null) {
|
|
_gridGuideLine.WideOn(CogEnvironment.GetImageSize(cogDisplay.Image), true);
|
|
}
|
|
}
|
|
|
|
public void SetNarrowGridGuideLine_ErrRotation() {
|
|
if (cogDisplay.Image != null) {
|
|
_gridGuideLine.NarrowOn(CogEnvironment.GetImageSize(cogDisplay.Image), true);
|
|
}
|
|
}
|
|
|
|
public void ClearGridGuidLine() {
|
|
_gridGuideLine.Clear();
|
|
}
|
|
|
|
public int SetPatternTracking(Cross newCross) {
|
|
if (_PatternRecipe.TrackingParam.Enabled) {
|
|
PatternTrackingManager.AddCross(_FovInfo, newCross);
|
|
}
|
|
return E1.OK;
|
|
}
|
|
|
|
public int DrawPatternTrackings(List<Cross> newCrosses) {
|
|
ClearPatternTrackingGraphics();
|
|
|
|
if (_PatternRecipe.TrackingParam.Enabled) {
|
|
Application.Current.Dispatcher.BeginInvoke(
|
|
new System.Action(() => {
|
|
var color = ColorConverter.ConvertColor(eColor.Orange);
|
|
var crossTrackings = PatternTrackingManager.GetCrosses(_FovInfo, _PatternRecipe.TrackingParam.Count + 1);
|
|
RemoveDuplicateCross(newCrosses, crossTrackings);
|
|
DisplayDrawer.AddCrosses(cogDisplay, color, crossTrackings, CogConstants.KEY_PATTERN_TRACKING);
|
|
}));
|
|
}
|
|
return E1.OK;
|
|
}
|
|
|
|
private void RemoveDuplicateCross(List<Cross> newCrosses, List<Cross> crossTrackings) {
|
|
foreach (Cross newCross in newCrosses) {
|
|
for (int i = 0; i < crossTrackings.Count; i++) {
|
|
if (crossTrackings[i].Equals(newCross)) {
|
|
crossTrackings.Remove(newCross);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 레퍼런스 위치에 십자가 그리기
|
|
/// </summary>
|
|
/// <param name="length">십자가 길이</param>
|
|
/// <param name="color">십자가 색상</param>
|
|
public void DrawRefCross(int length = 10, eColor color = eColor.Yellow) {
|
|
var refPointRecipe = VisionRecipeManager.GetRefPointRecipe(_FovInfo);
|
|
if (refPointRecipe == null) {
|
|
_Logger.E($"refPointRecipe is null");
|
|
return;
|
|
}
|
|
|
|
ImagePoint refPos = GetRefPos(_ImageSize);
|
|
AddCross(new Cross(refPos, length), color);
|
|
}
|
|
|
|
private ImagePoint GetRefPos(ImageSize imageSize) {
|
|
var calibrator = CalibratorManager.GetCalibrator(_FovInfo);
|
|
var staticRefPoint = VisionRecipeManager.GetRefPoint(_FovInfo);
|
|
var imageRefPoint = calibrator.TransformToImagePoint(staticRefPoint, imageSize);
|
|
|
|
return new ImagePoint(imageRefPoint.X, imageRefPoint.Y);
|
|
}
|
|
|
|
public int AddCross(Cross cross, eColor color) {
|
|
return AddCross(cross, ColorConverter.ConvertColor(color));
|
|
}
|
|
|
|
public int AddCross(Cross cross, CogColorConstants color) {
|
|
DisplayDrawer.AddCross(cogDisplay, color, cross, CogConstants.KEY_CROSS);
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddCrosses(List<Cross> crosses, CogColorConstants color) {
|
|
DisplayDrawer.AddCrosses(cogDisplay, color, crosses, CogConstants.KEY_CROSS);
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddLine(eColor color, ImagePoint p1, ImagePoint p2) {
|
|
return AddLine(ColorConverter.ConvertColor(color), p1, p2);
|
|
}
|
|
|
|
public int AddLine(CogColorConstants color, ImagePoint p1, ImagePoint p2) {
|
|
DisplayDrawer.AddLine(cogDisplay, color, p1, p2, CogConstants.KEY_LINE);
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddVectors(CogColorConstants color, ImagePoint startPoint, List<ImagePoint> endPoints) {
|
|
DisplayDrawer.AddVectors(cogDisplay, color, startPoint, endPoints, CogConstants.KEY_VECTOR);
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddTrainOrigin(Cross trainOrigin) {
|
|
if (HasTrainOrigin()) {
|
|
DisplayDrawer.RemoveInteractiveGraphic(cogDisplay, CogConstants.KEY_TRAIN_ORIGIN);
|
|
}
|
|
|
|
int markerSize = 100;
|
|
_trainOrigin.SetCenterRotationSize(trainOrigin.Origin.X, trainOrigin.Origin.Y, trainOrigin.Angle.Radian, markerSize);
|
|
_trainOrigin.Visible = true;
|
|
DisplayDrawer.AddInteractiveGraphic(cogDisplay, _trainOrigin, CogConstants.KEY_TRAIN_ORIGIN, false);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private void TrainOriginDraggingStopped(object sender, CogDraggingEventArgs e) {
|
|
try {
|
|
CogPointMarker marker = e.DragGraphic as CogPointMarker;
|
|
_Logger.FuncI($"{_trainOrigin.X}, {_trainOrigin.Y}");
|
|
}
|
|
catch (Exception ex) {
|
|
_Logger.FuncE("{0}", ex.Message);
|
|
}
|
|
}
|
|
|
|
public Cross GetTrainOrigin() {
|
|
if (!HasTrainOrigin()) {
|
|
return new Cross(new ImagePoint(0, 0));
|
|
}
|
|
|
|
return new Cross(new ImagePoint(_trainOrigin.X, _trainOrigin.Y));
|
|
}
|
|
|
|
public bool HasTrainOrigin() {
|
|
return DisplayDrawer.ExistsGetInteractiveGraphic(cogDisplay, CogConstants.KEY_TRAIN_ORIGIN);
|
|
}
|
|
|
|
public void AddRoi(ROI roi, eColor color, bool isRotatable = false) {
|
|
if (ROI.IsNullOrEmpty(roi)) {
|
|
DisplayDrawer.AddRoi(cogDisplay, ColorConverter.ConvertColor(color), roi, CogConstants.KEY_ROI);
|
|
}
|
|
}
|
|
|
|
public int AddTrainRoi(ImagePoint imageCenter, bool isRotatable = false) {
|
|
var length = 200;
|
|
return AddTrainRoi(new ROI(imageCenter, length, length, new Angle()), isRotatable);
|
|
}
|
|
|
|
public int AddTrainRoi(ROI trainRoi, bool isRotatable = false) {
|
|
if (HasTrainRoi()) {
|
|
DisplayDrawer.RemoveInteractiveGraphic(cogDisplay, CogConstants.KEY_TRAIN_ROI);
|
|
}
|
|
|
|
_trainRoi.Visible = true;
|
|
// trainRoi의 Angle은 패턴 로드 시점에 trainOrigin에 의해 미리 Set되어 있다.
|
|
_trainRoi.SetCenterLengthsRotationSkew(trainRoi.CenterX, trainRoi.CenterY, trainRoi.Width, trainRoi.Height, trainRoi.Angle.Radian, 0);
|
|
//_trainRoi.SetCenterLengthsRotationSkew(trainRoi.CenterX, trainRoi.CenterY, trainRoi.Width, trainRoi.Height, trainRoi.Angle.Radian, 0);
|
|
DisplayDrawer.AddInteractiveGraphic(cogDisplay, _trainRoi, CogConstants.KEY_TRAIN_ROI, false);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private void Changed_TrainRoi(object sender, CogChangedEventArgs e) {
|
|
try {
|
|
var trainRoiAffine = sender as CogRectangleAffine;
|
|
_trainOrigin.Rotation = trainRoiAffine.Rotation;
|
|
if (_GraEventSubscribers?.Count > 0) {
|
|
_GraEventSubscribers[_PatternInfos[eRoiIdx.Roi0]]?.UpdateAngleOfRoi(eGraRoiType.TrainRoi, CogMisc.RadToDeg(trainRoiAffine.Rotation));
|
|
}
|
|
}
|
|
catch (Exception ex) {
|
|
_Logger.FuncE("{0}", ex.Message);
|
|
}
|
|
}
|
|
|
|
public ROI GetTrainRoi() {
|
|
if (!HasTrainRoi()) {
|
|
return new ROI(0, 0, 0, 0);
|
|
}
|
|
|
|
var convParam = ConvertDoubleToInt(_trainRoi.CenterX, _trainRoi.CenterY, _trainRoi.SideXLength, _trainRoi.SideYLength);
|
|
return new ROI(
|
|
convParam.centerX, convParam.centerY, convParam.width, convParam.height, Angle.CreateWithDegree(CogMisc.RadToDeg(_trainRoi.Rotation)));
|
|
}
|
|
|
|
private (double centerX, double centerY, int width, int height) ConvertDoubleToInt(double centerX, double centerY, double width, double height) {
|
|
int left = (int)Math.Round(centerX - width / 2.0);
|
|
int top = (int)Math.Round(centerY - height / 2.0);
|
|
int right = (int)Math.Round(centerX + width / 2.0);
|
|
int bottom = (int)Math.Round(centerY + height / 2.0);
|
|
|
|
int intWidth = right - left;
|
|
int intHeight = bottom - top;
|
|
|
|
double dbCenterX = left + intWidth * 0.5;
|
|
double dbCenterY = top + intHeight * 0.5;
|
|
return (dbCenterX, dbCenterY, intWidth, intHeight);
|
|
}
|
|
public void UpdateAngleOfTrainRoi(double roiAngle) {
|
|
if (HasTrainRoi()) {
|
|
_trainRoi.Rotation = CogMisc.DegToRad(roiAngle);
|
|
}
|
|
}
|
|
|
|
public bool HasTrainRoi() {
|
|
return DisplayDrawer.ExistsGetInteractiveGraphic(cogDisplay, CogConstants.KEY_TRAIN_ROI);
|
|
}
|
|
|
|
public int AddDefaultFindRoi(ImageSize imageSize, bool isRotatable = false) {
|
|
if (HasFindRoi()) {
|
|
DisplayDrawer.RemoveInteractiveGraphic(cogDisplay, string.Format(CogConstants.KEY_FIND_ROI, eRoiIdx.Roi0));
|
|
}
|
|
|
|
var roi = RoiUtil.GetDefaultFindRoi(imageSize);
|
|
return AddFindRoi(roi, isRotatable);
|
|
}
|
|
|
|
public int AddFindRoi(ROI findRoi, bool isRotatable = false) {
|
|
if (HasFindRoi()) {
|
|
DisplayDrawer.RemoveInteractiveGraphic(cogDisplay, string.Format(CogConstants.KEY_FIND_ROI, findRoi.RoiIdx));
|
|
}
|
|
|
|
_findRois[findRoi.RoiIdx].Color = ColorConverter.ConvertColor(findRoi.RoiColor);
|
|
_findRois[findRoi.RoiIdx].SetCenterLengthsRotationSkew(findRoi.CenterX, findRoi.CenterY, findRoi.Width, findRoi.Height, findRoi.Angle.Radian, 0);
|
|
_findRois[findRoi.RoiIdx].Visible = true;
|
|
DisplayDrawer.AddInteractiveGraphic(cogDisplay, _findRois[findRoi.RoiIdx], string.Format(CogConstants.KEY_FIND_ROI, findRoi.RoiIdx), false);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private void Changed_FindRoi(object sender, CogChangedEventArgs e) {
|
|
try {
|
|
var findRoiAffine = sender as CogRectangleAffine;
|
|
if (_GraEventSubscribers?.Count > 0) {
|
|
_GraEventSubscribers[_PatternInfos[eRoiIdx.Roi0]]?.UpdateAngleOfRoi(eGraRoiType.FindRoi, CogMisc.RadToDeg(findRoiAffine.Rotation));
|
|
}
|
|
}
|
|
catch (Exception ex) {
|
|
_Logger.FuncE("{0}", ex.Message);
|
|
}
|
|
}
|
|
|
|
public ROI GetFindRoi(eRoiIdx roiIdx = eRoiIdx.Roi0) {
|
|
if (!HasFindRoi()) {
|
|
return new ROI();
|
|
}
|
|
|
|
var convParam = ConvertDoubleToInt(_findRois[roiIdx].CenterX, _findRois[roiIdx].CenterY, _findRois[roiIdx].SideXLength, _findRois[roiIdx].SideYLength);
|
|
return new ROI(
|
|
convParam.centerX, convParam.centerY, convParam.width, convParam.height, Angle.CreateWithDegree(CogMisc.RadToDeg(_findRois[roiIdx].Rotation)));
|
|
}
|
|
|
|
|
|
public void UpdateAngleOfFindRoi(double roiAngle) {
|
|
if (HasFindRoi()) {
|
|
_findRois[eRoiIdx.Roi0].Rotation = CogMisc.DegToRad(roiAngle);
|
|
}
|
|
}
|
|
|
|
public bool HasFindRoi(eRoiIdx roiIdx = eRoiIdx.Roi0) {
|
|
return DisplayDrawer.ExistsGetInteractiveGraphic(cogDisplay, string.Format(CogConstants.KEY_FIND_ROI, roiIdx));
|
|
}
|
|
|
|
public void ClearFindRoi(eRoiIdx roiIdx = eRoiIdx.Roi0) {
|
|
if (HasFindRoi(roiIdx)) {
|
|
ClearFindGraphics();
|
|
}
|
|
}
|
|
|
|
public void DrawTrainEdge(PatternAgentBase patternAgent, PatternInfo patternInfo) {
|
|
DisplayDrawer.AddTrainEdge(cogDisplay, patternAgent, patternInfo);
|
|
}
|
|
|
|
public void DrawBlobEdge(PatternAgentBase patternAgent, PatternInfo patternInfo) {
|
|
DisplayDrawer.AddBlobEdge(cogDisplay, patternAgent, patternInfo);
|
|
}
|
|
|
|
public int AddPatternResultFindRoi(CogDisplay cogDisplay, CogColorConstants color, List<ROI> findRois) {
|
|
foreach (var roi in findRois) {
|
|
AddPatternResultFindRoi(cogDisplay, color, roi);
|
|
}
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddPatternResultFindRoi(CogDisplay cogDisplay, CogColorConstants color, ROI findRoi) {
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
var imageSize = CogEnvironment.GetImageSize(GetImage());
|
|
var defaultFindRoi = RoiUtil.GetDefaultFindRoi(imageSize);
|
|
DisplayDrawer.AddRoi(cogDisplay, color, defaultFindRoi, string.Format(CogConstants.KEY_FIND_ROI, eRoiIdx.Roi0));
|
|
}
|
|
else {
|
|
DisplayDrawer.AddRoi(cogDisplay, color, findRoi, string.Format(CogConstants.KEY_FIND_ROI, eRoiIdx.Roi0));
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddTextList(eColor color, ImagePoint p, List<string> msgs) {
|
|
return AddTextList(ColorConverter.ConvertColor(color), p, msgs);
|
|
}
|
|
|
|
public int AddTextList(object color, ImagePoint p, List<string> msgs) {
|
|
DisplayDrawer.AddTextList(cogDisplay, (CogColorConstants)color, p, msgs, CogConstants.KEY_PATTERN_RESULT);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddTextList(object color, ImagePoint p, List<string> msgs, int lineInterval) {
|
|
DisplayDrawer.AddTextList(cogDisplay, (CogColorConstants)color, p, msgs, CogConstants.KEY_PATTERN_RESULT, lineInterval);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddColorTextList(ImagePoint p, List<ColorText> colorTextList) {
|
|
DisplayDrawer.AddColorTextList(cogDisplay, p, colorTextList, CogConstants.KEY_PATTERN_RESULT);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int AddColorTextList(ImagePoint p, List<ColorText> colorTextList, int lineInterval) {
|
|
DisplayDrawer.AddColorTextList(cogDisplay, p, colorTextList, CogConstants.KEY_PATTERN_RESULT, lineInterval);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int ClearAllGraphics() {
|
|
DisplayDrawer.ClearGraphics(cogDisplay);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int ClearMeasureGraphics(eGraphicToClear graphicToClear = eGraphicToClear.ALL) {
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_PATTERN_RESULT);
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_CROSS);
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_LINE);
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_BLOB);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int ClearTrainGraphics(eGraphicToClear graphicToClear = eGraphicToClear.ALL) {
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_TRAIN_ORIGIN);
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_TRAIN_ROI);
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_TRAIN_RESULT);
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_MASK);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int ClearFindGraphics(eGraphicToClear graphicToClear = eGraphicToClear.ALL) {
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, string.Format(CogConstants.KEY_FIND_ROI, eRoiIdx.Roi0));
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_CALIPER_ROI);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int ClearPatternTrackingGraphics(eGraphicToClear graphicToClear = eGraphicToClear.ALL) {
|
|
DisplayDrawer.RemoveGraphic(cogDisplay, graphicToClear, CogConstants.KEY_PATTERN_TRACKING);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public void ClearFindLabelNumGraphicsAll() {
|
|
//foreach (var itemPair in _findLabelNumGraphicsDic) {
|
|
// MilGraphics milGraphics = itemPair.Value;
|
|
// milGraphics?.Clear();
|
|
//}
|
|
//_findLabelNumGraphicsDic.Clear();
|
|
}
|
|
|
|
// 메인 화면에 _FovInfo에 해당하는 디스플레이만 보이기
|
|
public void SetVisibility() {
|
|
var fovUiPos = VisionRecipeManager.GetFovUiPos(_FovRecipe, _DisplayType);
|
|
CameraViewManager.Instance.SetVisibilities(_FovInfo, fovUiPos, _DisplayType);
|
|
}
|
|
|
|
private void cogDisplay_DoubleClick(object sender, EventArgs e) {
|
|
FitImage();
|
|
}
|
|
|
|
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
|
|
bool isVisible = (bool)e.NewValue;
|
|
if (isVisible == true) {
|
|
RefreshVisionRecipes();
|
|
}
|
|
}
|
|
|
|
public void RefreshVisionRecipes() {
|
|
if (_FovInfo != null) {
|
|
_FovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo);
|
|
_PatternRecipe = VisionRecipeManager.GetPatternRecipe(_FovInfo.ToSubSysInfo(), _FovInfo.FovId);
|
|
}
|
|
}
|
|
|
|
public void UpdateRecipeFromFile() {
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public void UpdateRecipeFromUI() {
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private void UpdateCurrentZoomToStatusBar(EventArgs e) {
|
|
if (cogDisplay.Image == null) {
|
|
return;
|
|
}
|
|
|
|
double imageZoom = Math.Round(cogDisplay.Zoom, 2);
|
|
double displayZoom = GetDisplayZoom();
|
|
|
|
Application.Current.Dispatcher.BeginInvoke(
|
|
new System.Action(() => {
|
|
curZoom.Text = $"Zoom (X{imageZoom / displayZoom:F2}, {imageZoom:F2})";
|
|
}));
|
|
}
|
|
|
|
private double GetDisplayZoom() {
|
|
double displayZoomX = (double)cogDisplay.ClientSize.Width / cogDisplay.Image.Width;
|
|
double displayZoomY = (double)cogDisplay.ClientSize.Height / cogDisplay.Image.Height;
|
|
|
|
return Math.Round(Math.Min(displayZoomX, displayZoomY), 2);
|
|
}
|
|
|
|
private void UpdateCurrentPointToStatusBar(System.Windows.Forms.MouseEventArgs e) {
|
|
if (cogDisplay.Image == null) {
|
|
return;
|
|
}
|
|
|
|
var screenPoint = new ImagePoint(e.X, e.Y);
|
|
var imagePoint = _CogRuler.TransformScreenPointToImagePoint(screenPoint);
|
|
curImagePoint.Text = imagePoint.ToString();
|
|
|
|
if (_Calibrator != null) {
|
|
var staticPoint = _Calibrator.TransformToCalibratedSpace(imagePoint, _ImageSize);
|
|
curStaticPoint.Text = staticPoint.ToString();
|
|
}
|
|
}
|
|
|
|
#region TrainRoi & FindRoi Changed Event
|
|
private Dictionary<PatternInfo, IGraEventSubscriber> _GraEventSubscribers = new Dictionary<PatternInfo, IGraEventSubscriber>();
|
|
|
|
public void SetGraEventSubscriber(PatternInfo patternInfo, object viewModel) {
|
|
_GraEventSubscribers[patternInfo] = viewModel as IGraEventSubscriber;
|
|
}
|
|
|
|
private void ClearGraEventSubscribers() {
|
|
_GraEventSubscribers.Clear();
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
if (!_Initialized) {
|
|
return;
|
|
}
|
|
|
|
RemoveObservable();
|
|
RemoveHookFunc();
|
|
RemoveGraphics();
|
|
ClearGraEventSubscribers();
|
|
|
|
_Initialized = false;
|
|
}
|
|
|
|
private void RemoveHookFunc() {
|
|
cogDisplay.ContextFitImage -= CogDisplay_ContextFitImage;
|
|
cogDisplay.MouseWheel -= CogDisplay_MouseWheel;
|
|
cogDisplay.MouseMove -= CogDisplay_MouseMove;
|
|
}
|
|
|
|
private void RemoveGraphics() {
|
|
ClearAllGraphics();
|
|
_gridGuideLine = null;
|
|
_trainOrigin = null;
|
|
_trainRoi = null;
|
|
for (int i = 0; i < _findRois.Count; i++) {
|
|
_findRois[(eRoiIdx)i] = null;
|
|
}
|
|
}
|
|
|
|
private void RemoveObservable() {
|
|
_EventDisposable?.Dispose();
|
|
_EventDisposable = null;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|