patrick.xu
2021-03-31 1278e0f9858b191610be756b2f8afd4c7ea2b5e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using HalconDotNet;
 
namespace HalconTools
{
 
    /// <summary>
    /// The class MeasurementEdge describes single-edge measurement
    /// and inherits from the base class Measurement. Virtual methods 
    /// defined in the base class are customized here to apply
    /// HALCON operators for single-edge extraction.
    /// </summary>
    public class MeasurementEdge : Measurement
    {
        /// <summary>
        /// Result container for the edge information returned
        /// by the HALCON measure operator.
        /// </summary>
        private EdgeResult mResult;
        /// <summary>
        /// Result container for the edge information converted
        /// into world coordinates. If calibration data is not available,
        /// the variable contains the same information as mResult.
        /// </summary>
        private EdgeResult mResultWorld;
 
 
        /// <summary>
        /// Creates a measurement object for the provided ROI instance.
        /// </summary>
        /// <param name="roi">ROI instance</param>
        /// <param name="mAssist">Reference to controller class</param>
        public MeasurementEdge(ROI roi, MeasureAssistant mAssist)
            : base(roi, mAssist)
        {
            mResult = new EdgeResult();
            mResultWorld = new EdgeResult();
            UpdateMeasure();
        }
 
        /// <summary>
        /// Triggers an update of the measure results because of  
        /// changes in the parameter setup or a recreation of the measure 
        /// object caused by an update in the ROI model.
        /// </summary>               
        public override void UpdateResults()
        {
            if (mHandle == null)
                return;
 
            mMeasAssist.exceptionText = "";
 
            try
            {
                mHandle.MeasurePos(mMeasAssist.mImage,
                                   mMeasAssist.mSigma, mMeasAssist.mThresh,
                                   mMeasAssist.mTransition, mMeasAssist.mPosition,
                                   out mResult.rowEdge, out mResult.colEdge,
                                   out mResult.amplitude, out mResult.distance);
 
                if (mMeasAssist.mIsCalibValid && mMeasAssist.mTransWorldCoord)
                {
                    Rectify(mResult.rowEdge, mResult.colEdge, out mResultWorld.rowEdge, out mResultWorld.colEdge);
                    mResultWorld.distance = Distance(mResult.rowEdge, mResult.colEdge, mResult.rowEdge, mResult.colEdge, 1);
                    mResultWorld.amplitude = mResult.amplitude;
                }
                else
                {
                    mResultWorld = new EdgeResult(mResult);
                }
            }
            catch (HOperatorException e)
            {
                mEdgeXLD.Dispose();
                mMeasAssist.exceptionText = e.Message;
                mResultWorld = new EdgeResult();
                return;
            }
            UpdateXLD();
        }
 
        /// <summary>Updates display object for measured edge results</summary>
        public override void UpdateXLD()
        {
            double width, phi, cRow, cCol, radius;
 
            if (mHandle == null && ((int)mHandle.Handle < 0))
                return;
 
            mMeasAssist.exceptionText = "";
            width = mMeasAssist.mDispROIWidth ? mMeasAssist.mRoiWidth : mMeasAssist.mDispEdgeLength;
            mEdgeXLD.Dispose();
            mEdgeXLD.GenEmptyObj();
 
            try
            {
                if (mROIType == ROI.ROI_TYPE_LINE)
                {
                    phi = mMeasROI[2].D;
 
                    for (int i = 0; i < mResult.rowEdge.Length; i++)
                        mEdgeXLD = mEdgeXLD.ConcatObj(DetermineEdgeLine(mResult.rowEdge[i].D, mResult.colEdge[i].D, phi, width));
 
                }
                else if (mROIType == ROI.ROI_TYPE_CIRCLEARC)
                {
                    cRow = mROICoord[0].D;
                    cCol = mROICoord[1].D;
                    radius = mROICoord[2].D;
 
                    for (int i = 0; i < mResult.rowEdge.Length; i++)
                        mEdgeXLD = mEdgeXLD.ConcatObj(DetermineEdgeCircularArc(mResult.rowEdge[i].D, mResult.colEdge[i].D, cRow, cCol, radius, width));
                }
            }
            catch (HOperatorException e)
            {
                mMeasAssist.exceptionText = e.Message;
            }
        }
 
        /// <summary>Returns measurement result.</summary>
        public override MeasureResult getMeasureResultData()
        {
            return mResultWorld;
        }
 
        /// <summary>Clears measurement result.</summary>
        public override void ClearResultData()
        {
            mResultWorld = new EdgeResult();
        }
 
    }//end of class
}//end of namespace