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<double> rows;
|
|
private List<double> 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<double>();
|
cols = new List<double>();
|
}
|
|
/// <summary>Creates a new ROI instance at the mouse position</summary>
|
/// <param name="midX">
|
/// x (=column) coordinate for interactive ROI
|
/// </param>
|
/// <param name="midY">
|
/// y (=row) coordinate for interactive ROI
|
/// </param>
|
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;
|
}
|
|
/// <summary>Paints the ROI into the supplied window</summary>
|
/// <param name="window">HALCON window</param>
|
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);
|
}
|
|
/// <summary>
|
/// Returns the distance of the ROI handle being
|
/// closest to the image point(x,y)
|
/// </summary>
|
/// <param name="x">x (=column) coordinate</param>
|
/// <param name="y">y (=row) coordinate</param>
|
/// <returns>
|
/// Distance of the closest ROI handle.
|
/// </returns>
|
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];
|
}
|
|
/// <summary>
|
/// Paints the active handle of the ROI object into the supplied window
|
/// </summary>
|
/// <param name="window">HALCON window</param>
|
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);
|
|
}
|
|
/// <summary>
|
/// Gets the HALCON region described by the ROI
|
/// </summary>
|
public override HRegion getRegion()
|
{
|
region.GenRegionPolygonFilled(new HTuple(rows.ToArray()), new HTuple(cols.ToArray()));
|
return region;
|
}
|
|
/// <summary>
|
/// Gets the model information described by
|
/// the interactive ROI
|
/// </summary>
|
public override HTuple getModelData()
|
{
|
HTuple val = new HTuple();
|
|
val.Append(new HTuple(rows.ToArray()));
|
val.Append(new HTuple(cols.ToArray()));
|
|
return val;
|
}
|
|
|
/// <summary>
|
/// Recalculates the shape of the ROI instance. Translation is
|
/// performed at the active handle of the ROI object
|
/// for the image coordinate (x,y)
|
/// </summary>
|
/// <param name="newX">x mouse coordinate</param>
|
/// <param name="newY">y mouse coordinate</param>
|
public override void moveByHandle(double newX, double newY)
|
{
|
var deltaRow = new List<double>();
|
var deltaCol = new List<double>();
|
|
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
|
|
}
|
}
|