using HalconDotNet; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bro.UI.hdisplay.ViewROI { public class ROIPolygon : ROI { private List rows; private List cols; private double midR, midC; // midpoint HRegion region; private static ROIPolygon instance; protected override int NumHandles { get => rows.Count + 1; } public static ROIPolygon GetCurrentInstance() { if (instance == null) instance = new ROIPolygon(); return instance; } private ROIPolygon() { midR = 0; midC = 0; rows = new List(); cols = new List(); } /// Creates a new ROI instance at the mouse position /// /// x (=column) coordinate for interactive ROI /// /// /// y (=row) coordinate for interactive ROI /// public override void createROI(double x, double y) { region = new HRegion(); rows.Add(y); cols.Add(x); } public void CreateROIEnd() { HOperatorSet.GenRegionPolygon(out HObject hObject, new HTuple(rows.ToArray()), new HTuple(cols.ToArray())); HOperatorSet.AreaCenter(hObject, out HTuple area, out HTuple row, out HTuple col); midR = row; midC = col; } public void DisposeCurrentInstance() { instance = null; } /// Paints the ROI into the supplied window /// HALCON window public override void draw(HalconDotNet.HWindow window) { if (rows.Count > 1) { region.GenRegionPolygonFilled(new HTuple(rows.ToArray()), new HTuple(cols.ToArray())); window.DispRegion(region); } for (int i = 0; i < rows.Count; i++) { window.DispRectangle2(rows[i], cols[i], 0, 5, 5); } if (midR != 0 || midC != 0) window.DispRectangle2(midR, midC, 0, 5, 5); } /// /// Returns the distance of the ROI handle being /// closest to the image point(x,y) /// /// x (=column) coordinate /// y (=row) coordinate /// /// Distance of the closest ROI handle. /// public override double distToClosestHandle(double x, double y) { double max = 10000; double[] val = new double[NumHandles]; for (int i = 0; i < rows.Count; i++) { val[i] = HMisc.DistancePp(y, x, rows[i], cols[i]); } val[NumHandles - 1] = HMisc.DistancePp(y, x, midR, midC); // midpoint for (int i = 0; i < NumHandles; i++) { if (val[i] < max) { max = val[i]; activeHandleIdx = i; } }// end of for return val[activeHandleIdx]; } /// /// Paints the active handle of the ROI object into the supplied window /// /// HALCON window public override void displayActive(HalconDotNet.HWindow window) { if (activeHandleIdx == rows.Count) window.DispRectangle2(midR, midC, 0, 5, 5); else window.DispRectangle2(rows[activeHandleIdx], cols[activeHandleIdx], 0, 5, 5); } /// /// Gets the HALCON region described by the ROI /// public override HRegion getRegion() { region.GenRegionPolygonFilled(new HTuple(rows.ToArray()), new HTuple(cols.ToArray())); return region; } /// /// Gets the model information described by /// the interactive ROI /// public override HTuple getModelData() { HTuple val = new HTuple(); val.Append(new HTuple(rows.ToArray())); val.Append(new HTuple(cols.ToArray())); return val; } /// /// Recalculates the shape of the ROI instance. Translation is /// performed at the active handle of the ROI object /// for the image coordinate (x,y) /// /// x mouse coordinate /// y mouse coordinate public override void moveByHandle(double newX, double newY) { var deltaRow = new List(); var deltaCol = new List(); for (int i = 0; i < rows.Count; i++) { deltaRow.Add(rows[i] - midR); deltaCol.Add(cols[i] - midC); } if (activeHandleIdx == rows.Count) { midR = newY; midC = newX; for (int i = 0; i < rows.Count; i++) { rows[i] = midR + deltaRow[i]; cols[i] = midC + deltaCol[i]; } } else { rows[activeHandleIdx] = newY; cols[activeHandleIdx] = newX; } }//end of method } }