jace.tang
2023-02-11 ed8d469ccdc0e627d8f180bb92a9d78dbdb008b1
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
using System;
using HalconDotNet;
 
namespace Bro.UI.HalconDisplay.ViewROI
{
    /// <summary>
    /// This class demonstrates one of the possible implementations for a 
    /// circular ROI. ROICircle inherits from the base class ROI and 
    /// implements (besides other auxiliary methods) all virtual methods 
    /// defined in ROI.cs.
    /// </summary>
    public class ROICircle : ROI
    {
 
        private double radius;
        private double row1, col1;  // first handle
        private double midR, midC;  // second handle
 
 
        public ROICircle()
        {
            NumHandles = 2; // one at corner of circle + midpoint
            activeHandleIdx = 1;
        }
 
 
 
        /// <summary>Creates a new ROI instance at the mouse position</summary>
        public override void createROI(double midX, double midY)
        {
            midR = midY;
            midC = midX;
 
            radius = 50;
 
            row1 = midR;
            col1 = midC + radius;
        }
 
        /// <summary>Paints the ROI into the supplied window</summary>
        /// <param name="window">HALCON window</param>
        public override void draw(HalconDotNet.HWindow window)
        {
            window.DispCircle(midR, midC, radius);
            window.DispRectangle2(row1, col1, 0, 5, 5);
            window.DispRectangle2(midR, midC, 0, 5, 5);
        }
 
        /// <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, row1, col1); // border handle 
            val[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>
        public override void displayActive(HalconDotNet.HWindow window)
        {
 
            switch (activeHandleIdx)
            {
                case 0:
                    window.DispRectangle2(row1, col1, 0, 5, 5);
                    break;
                case 1:
                    window.DispRectangle2(midR, midC, 0, 5, 5);
                    break;
            }
        }
 
        /// <summary>Gets the HALCON region described by the ROI</summary>
        public override HRegion getRegion()
        {
            HRegion region = new HRegion();
            region.GenCircle(midR, midC, radius);
            return region;
        }
 
        public override double getDistanceFromStartPoint(double row, double col)
        {
            double sRow = midR; // assumption: we have an angle starting at 0.0
            double sCol = midC + 1 * radius;
 
            double angle = HMisc.AngleLl(midR, midC, sRow, sCol, midR, midC, row, col);
 
            if (angle < 0)
                angle += 2 * Math.PI;
 
            return (radius * angle);
        }
 
        /// <summary>
        /// Gets the model information described by 
        /// the  ROI
        /// </summary> 
        public override HTuple getModelData()
        {
            return new HTuple(new double[] { midR, midC, radius });
        }
 
        /// <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 shiftX,shiftY;
 
            switch (activeHandleIdx)
            {
                case 0: // handle at circle border
 
                    row1 = newY;
                    col1 = newX;
                    HOperatorSet.DistancePp(new HTuple(row1), new HTuple(col1),
                                            new HTuple(midR), new HTuple(midC),
                                            out distance);
 
                    radius = distance[0].D;
                    break;
                case 1: // midpoint 
 
                    shiftY = midR - newY;
                    shiftX = midC - newX;
 
                    midR = newY;
                    midC = newX;
 
                    row1 -= shiftY;
                    col1 -= shiftX;
                    break;
            }
        }
    }//end of class
}//end of namespace