using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using HalconDotNet;
namespace HDisplay.ViewROI
{
///
/// This class is a base class containing virtual methods for handling
/// ROIs. Therefore, an inheriting class needs to define/override these
/// methods to provide the ROIController with the necessary information on
/// its (= the ROIs) shape and position. The example project provides
/// derived ROI shapes for rectangles, lines, circles, and circular arcs.
/// To use other shapes you must derive a new class from the base class
/// ROI and implement its methods.
///
public class ROI : INotifyPropertyChanged
{
protected virtual int TypeIndex { get; set; } = -1;
protected void Set(ref T prop, T value, [CallerMemberName] String propertyName = null)
{
if (object.Equals(prop, value)) return;
prop = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// class members of inheriting ROI classes
protected int NumHandles;
protected int activeHandleIdx;
///
/// Flag to define the ROI to be 'positive' or 'negative'.
///
protected int OperatorFlag;
/// Parameter to define the line style of the ROI.
public HTuple flagLineStyle;
/// Constant for a positive ROI flag.
public const int POSITIVE_FLAG = ROIController.MODE_ROI_POS;
/// Constant for a negative ROI flag.
public const int NEGATIVE_FLAG = ROIController.MODE_ROI_NEG;
public const int ROI_TYPE_LINE = 10;
public const int ROI_TYPE_CIRCLE = 11;
public const int ROI_TYPE_CIRCLEARC = 12;
public const int ROI_TYPE_RECTANCLE1 = 13;
public const int ROI_TYPE_RECTANGLE2 = 14;
protected HTuple posOperation = new HTuple();
protected HTuple negOperation = new HTuple(new int[] { 2, 2 });
public event PropertyChangedEventHandler PropertyChanged;
/// Constructor of abstract ROI class.
public ROI() { }
/// Creates a new ROI instance at the mouse position.
///
/// x (=column) coordinate for ROI
///
///
/// y (=row) coordinate for ROI
///
public virtual void createROI(double midX, double midY) { }
/// Paints the ROI into the supplied window.
/// HALCON window
public virtual void draw(HalconDotNet.HWindow window) { }
///
/// 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 virtual double distToClosestHandle(double x, double y)
{
return 0.0;
}
///
/// Paints the active handle of the ROI object into the supplied window.
///
/// HALCON window
public virtual void displayActive(HalconDotNet.HWindow window) { }
///
/// Recalculates the shape of the ROI. Translation is
/// performed at the active handle of the ROI object
/// for the image coordinate (x,y).
///
/// x (=column) coordinate
/// y (=row) coordinate
public virtual void moveByHandle(double x, double y) { }
/// Gets the HALCON region described by the ROI.
public virtual HRegion getRegion()
{
return null;
}
public virtual double getDistanceFromStartPoint(double row, double col)
{
return 0.0;
}
///
/// Gets the model information described by
/// the ROI.
///
public virtual HTuple getModelData()
{
return null;
}
public virtual HTuple getModelDataWithTypeIndex()
{
return new HTuple(TypeIndex).TupleUnion(getModelData());
}
public virtual void SetRoi(HTuple data)
{
}
/// Number of handles defined for the ROI.
/// Number of handles
public int getNumHandles()
{
return NumHandles;
}
/// Gets the active handle of the ROI.
/// Index of the active handle (from the handle list)
public int getActHandleIdx()
{
return activeHandleIdx;
}
///
/// Gets the sign of the ROI object, being either
/// 'positive' or 'negative'. This sign is used when creating a model
/// region for matching applications from a list of ROIs.
///
public int getOperatorFlag()
{
return OperatorFlag;
}
///
/// Sets the sign of a ROI object to be positive or negative.
/// The sign is used when creating a model region for matching
/// applications by summing up all positive and negative ROI models
/// created so far.
///
/// Sign of ROI object
public void setOperatorFlag(int flag)
{
OperatorFlag = flag;
switch (OperatorFlag)
{
case ROI.POSITIVE_FLAG:
flagLineStyle = posOperation;
break;
case ROI.NEGATIVE_FLAG:
flagLineStyle = negOperation;
break;
default:
flagLineStyle = posOperation;
break;
}
}
}//end of class
}//end of namespace