alvin.chen
2019-11-29 edf127edc771f715c49f86e91e768633f0a0ed93
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using HalconDotNet;
 
namespace HalconTools
{
 
    /// <summary>
    /// This class implements an ROI shaped as a circular
    /// arc. ROICircularArc inherits from the base class ROI and 
    /// implements (besides other auxiliary methods) all virtual methods 
    /// defined in ROI.cs.
    /// </summary>
    public class ROICircularArc : ROI
    {
        //handles
        private double midR, midC;       // 0. handle: midpoint
        private double sizeR, sizeC;     // 1. handle        
        private double startR, startC;   // 2. handle
        private double extentR, extentC; // 3. handle
 
        //model data to specify the arc
        private double radius;
        private double startPhi, extentPhi; // -2*PI <= x <= 2*PI
 
        //display attributes
        private HXLDCont  contour;
        private HXLDCont  arrowHandleXLD;
        private string    circDir;
        private double    TwoPI;
        private double    PI;
 
 
        public ROICircularArc()
        {
            NumHandles = 4;         // midpoint handle + three handles on the arc
            activeHandleIdx = 0;
            contour = new HXLDCont();
            circDir = "";
 
            TwoPI = 2 * Math.PI;
            PI = Math.PI;
 
            arrowHandleXLD = new HXLDCont();
            arrowHandleXLD.GenEmptyObj();
        }
 
        /// <summary>Creates a new ROI instance at the mouse position</summary>
        public override void createROI(double midX, double midY)
        {
            midR = midY;
            midC = midX;
 
            radius = 100;
 
            sizeR = midR;
            sizeC = midC - radius;
 
            startPhi = PI * 0.25;
            extentPhi = PI * 1.5;
            circDir = "positive";
 
            determineArcHandles();
            updateArrowHandle();
        }
 
        /// <summary>Paints the ROI into the supplied window</summary>
        /// <param name="window">HALCON window</param>
        public override void draw(HalconDotNet.HWindow window)
        {
            contour.Dispose();
            contour.GenCircleContourXld(midR, midC, radius, startPhi,
                                        (startPhi + extentPhi), circDir, 1.0);
            window.DispObj(contour);
            window.DispRectangle2(sizeR, sizeC, 0, 5, 5);
            window.DispRectangle2(midR, midC, 0, 5, 5);
            window.DispRectangle2(startR, startC, startPhi, 10, 2);
            window.DispObj(arrowHandleXLD);
        }
 
        /// <summary> 
        /// Returns the distance of the ROI handle being
        /// closest to the image point(x,y)
        /// </summary>
        public override double distToClosestHandle(double x, double y)
        {
            double max = 10000;
            double [] val = new double[NumHandles];
 
            val[0] = HMisc.DistancePp(y, x, midR, midC);       // midpoint 
            val[1] = HMisc.DistancePp(y, x, sizeR, sizeC);     // border handle 
            val[2] = HMisc.DistancePp(y, x, startR, startC);   // border handle 
            val[3] = HMisc.DistancePp(y, x, extentR, extentC); // border handle 
 
            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>
        public override void displayActive(HalconDotNet.HWindow window)
        {
            switch (activeHandleIdx)
            {
                case 0:
                    window.DispRectangle2(midR, midC, 0, 5, 5);
                    break;
                case 1:
                    window.DispRectangle2(sizeR, sizeC, 0, 5, 5);
                    break;
                case 2:
                    window.DispRectangle2(startR, startC, startPhi, 10, 2);
                    break;
                case 3:
                    window.DispObj(arrowHandleXLD);
                    break;
            }
        }
 
        /// <summary> 
        /// Recalculates the shape of the ROI. Translation is 
        /// performed at the active handle of the ROI object 
        /// for the image coordinate (x,y)
        /// </summary>
        public override void moveByHandle(double newX, double newY)
        {
            HTuple distance;
            double dirX, dirY, prior, next, valMax, valMin;
 
            switch (activeHandleIdx)
            {
                case 0: // midpoint 
                    dirY = midR - newY;
                    dirX = midC - newX;
 
                    midR = newY;
                    midC = newX;
 
                    sizeR -= dirY;
                    sizeC -= dirX;
 
                    determineArcHandles();
                    break;
 
                case 1: // handle at circle border                  
                    sizeR = newY;
                    sizeC = newX;
 
                    HOperatorSet.DistancePp(new HTuple(sizeR), new HTuple(sizeC),
                                            new HTuple(midR), new HTuple(midC), out distance);
                    radius = distance[0].D;
                    determineArcHandles();
                    break;
 
                case 2: // start handle for arc                
                    dirY = newY - midR;
                    dirX = newX - midC;
 
                    startPhi = Math.Atan2(-dirY, dirX);
 
                    if (startPhi < 0)
                        startPhi = PI + (startPhi + PI);
 
                    setStartHandle();
                    prior = extentPhi;
                    extentPhi = HMisc.AngleLl(midR, midC, startR, startC, midR, midC, extentR, extentC);
 
                    if (extentPhi < 0 && prior > PI * 0.8)
                        extentPhi = (PI + extentPhi) + PI;
                    else if (extentPhi > 0 && prior < -PI * 0.7)
                        extentPhi = -PI - (PI - extentPhi);
 
                    break;
 
                case 3: // end handle for arc
                    dirY = newY - midR;
                    dirX = newX - midC;
 
                    prior = extentPhi;
                    next = Math.Atan2(-dirY, dirX);
 
                    if (next < 0)
                        next = PI + (next + PI);
 
                    if (circDir == "positive" && startPhi >= next)
                        extentPhi = (next + TwoPI) - startPhi;
                    else if (circDir == "positive" && next > startPhi)
                        extentPhi = next - startPhi;
                    else if (circDir == "negative" && startPhi >= next)
                        extentPhi = -1.0 * (startPhi - next);
                    else if (circDir == "negative" && next > startPhi)
                        extentPhi = -1.0 * (startPhi + TwoPI - next);
 
                    valMax = Math.Max(Math.Abs(prior), Math.Abs(extentPhi));
                    valMin = Math.Min(Math.Abs(prior), Math.Abs(extentPhi));
 
                    if ((valMax - valMin) >= PI)
                        extentPhi = (circDir == "positive") ? -1.0 * valMin : valMin;
 
                    setExtentHandle();
                    break;
            }
 
            circDir = (extentPhi < 0) ? "negative" : "positive";
            updateArrowHandle();
        }
 
        /// <summary>Gets the HALCON region described by the ROI</summary>
        public override HRegion getRegion()
        {
            HRegion region;
            contour.Dispose();
            contour.GenCircleContourXld(midR, midC, radius, startPhi, (startPhi + extentPhi), circDir, 1.0);
            region = new HRegion(contour);
            return region;
        }
 
        /// <summary>
        /// Gets the model information described by the ROI
        /// </summary> 
        public override HTuple getModelData()
        {
            return new HTuple(new double[] { midR, midC, radius, startPhi, extentPhi });
        }
 
        /// <summary>
        /// Auxiliary method to determine the positions of the second and
        /// third handle.
        /// </summary>
        private void determineArcHandles()
        {
            setStartHandle();
            setExtentHandle();
        }
 
        /// <summary> 
        /// Auxiliary method to recalculate the start handle for the arc 
        /// </summary>
        private void setStartHandle()
        {
            startR = midR - radius * Math.Sin(startPhi);
            startC = midC + radius * Math.Cos(startPhi);
        }
 
        /// <summary>
        /// Auxiliary method to recalculate the extent handle for the arc
        /// </summary>
        private void setExtentHandle()
        {
            extentR = midR - radius * Math.Sin(startPhi + extentPhi);
            extentC = midC + radius * Math.Cos(startPhi + extentPhi);
        }
 
        /// <summary>
        /// Auxiliary method to display an arrow at the extent arc position
        /// </summary>
        private void updateArrowHandle()
        {
            double row1, col1, row2, col2;
            double rowP1, colP1, rowP2, colP2;
            double length,dr,dc, halfHW, sign, angleRad;
            double headLength = 15;
            double headWidth  = 15;
 
            arrowHandleXLD.Dispose();
            arrowHandleXLD.GenEmptyObj();
 
            row2 = extentR;
            col2 = extentC;
            angleRad = (startPhi + extentPhi) + Math.PI * 0.5;
 
            sign = (circDir == "negative") ? -1.0 : 1.0;
            row1 = row2 + sign * Math.Sin(angleRad) * 20;
            col1 = col2 - sign * Math.Cos(angleRad) * 20;
 
            length = HMisc.DistancePp(row1, col1, row2, col2);
            if (length == 0)
                length = -1;
 
            dr = (row2 - row1) / length;
            dc = (col2 - col1) / length;
 
            halfHW = headWidth / 2.0;
            rowP1 = row1 + (length - headLength) * dr + halfHW * dc;
            rowP2 = row1 + (length - headLength) * dr - halfHW * dc;
            colP1 = col1 + (length - headLength) * dc - halfHW * dr;
            colP2 = col1 + (length - headLength) * dc + halfHW * dr;
 
            if (length == -1)
                arrowHandleXLD.GenContourPolygonXld(row1, col1);
            else
                arrowHandleXLD.GenContourPolygonXld(new HTuple(new double[] { row1, row2, rowP1, row2, rowP2, row2 }),
                    new HTuple(new double[] { col1, col2, colP1, col2, colP2, col2 }));
        }
 
    }//end of class
}//end of namespace