/
finder.pde
237 lines (197 loc) · 6.11 KB
/
finder.pde
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
// based on: http://dcgi.felk.cvut.cz/home/sykorad/Sykora08-EGVE.pdf
class Finder {
LB lb;
public Finder (LB lb) {
this.lb = lb;
}
ArrayList<State> findCircles(int[][] boundary, State boundingState, int circleCount) {
int pointCount = boundary.length;
int totalPointCount = pointCount;
if(pointCount < 3)
return new ArrayList<State>();
int size = (int) max(boundingState.ssize.x, boundingState.ssize.y);
int xbox = (int) (boundingState.sposition.x - boundingState.ssize.x/2.0);
int ybox = (int) (boundingState.sposition.y - boundingState.ssize.y/2.0);
if(size > 200)
return new ArrayList<State>();
ArrayList<State> circles = new ArrayList<State>();
if(lb.debug && lb.debugView == 2 && !lb.pauseRender){
stroke(128, 0, 128);
strokeWeight(1);
if(boundary!=null){
beginShape(POINTS);
for(int j=0;j<pointCount;j++){
vertex(boundary[j][0], boundary[j][1]);
}
endShape();
}
}
float lastCircleProbability = lb.firstCircleThreshold/lb.minNextCircleThresholdRatio;
int maxCircles = circleCount;
while(maxCircles-- > 0){
int[][] histogram = new int[size][size];
int votes = 0;
int threshold = 100;
int maxValue = 0;
int[] maxCoords = new int[2];
int cycleLimit = 50000;
int voteLimit = 3*pointCount;
while(votes < voteLimit && cycleLimit-- > 0) {
int[] p1 = boundary[(int) random(pointCount)];
int[] p2 = boundary[(int) random(pointCount)];
int[] p3 = boundary[(int) random(pointCount)];
// optimalizovat?
// nevybíráme stejné body
if(p1 == p2 || p2 == p3 || p1 == p3)
continue;
int x32 = p3[0] - p2[0];
int x13 = p1[0] - p3[0];
int x21 = p2[0] - p1[0];
int y32 = p3[1] - p2[1];
int y13 = p1[1] - p3[1];
int y21 = p2[1] - p1[1];
int delitelx = (p1[0]*y32 + p2[0]*y13 + p3[0]*y21)*2;
int delitely = (p1[1]*x32 + p2[1]*x13 + p3[1]*x21)*2;
// body jsou na přímce, fuj!
if(delitelx == 0 || delitely == 0)
continue;
int d1 = p1[0]*p1[0] + p1[1]*p1[1];
int d2 = p2[0]*p2[0] + p2[1]*p2[1];
int d3 = p3[0]*p3[0] + p3[1]*p3[1];
int cx = (d1*y32 + d2*y13 + d3*y21)/delitelx - xbox;
int cy = (d1*x32 + d2*x13 + d3*x21)/delitely - ybox;
if(cx > 0 && cx < size && cy > 0 && cy < size){
histogram[cx][cy]++;
votes++;
if(histogram[cx][cy] > maxValue){
maxValue = histogram[cx][cy];
maxCoords[0] = cx;
maxCoords[1] = cy;
}
if(maxValue > threshold)
break;
}
}
maxCoords[0] += xbox;
maxCoords[1] += ybox;
int[] radiusHistogram = new int[size];
int maxRadiusValue = 0;
int maxRadius = 0;
for (int i = 0; i < pointCount; i++) {
int[] p = boundary[i];
int dx = maxCoords[0] - p[0];
int dy = maxCoords[1] - p[1];
int r = int(sqrt(dx*dx+dy*dy));
if(r < size){
radiusHistogram[r]++;
if(radiusHistogram[r] > maxRadiusValue){
maxRadiusValue = radiusHistogram[r];
maxRadius = r;
}
}
}
if(maxRadiusValue <= 0)
break;
int avgRadius = 0;
int sumRadius = 0;
for (int i = 0; i < size; i++) {
if(radiusHistogram[i] > 0.9*maxRadiusValue){
avgRadius += i*radiusHistogram[i];
sumRadius += radiusHistogram[i];
}
}
avgRadius /= sumRadius;
int avgDiameter = 2*avgRadius;
boolean[] pixelDelete = new boolean[pointCount];
int newPointCount = 0;
for (int i = 0; i < pointCount; i++) {
int[] p = boundary[i];
int dx = maxCoords[0] - p[0];
int dy = maxCoords[1] - p[1];
if(abs(sqrt(dx*dx+dy*dy) - avgRadius) < 2){
pixelDelete[i] = true;
}
else {
pixelDelete[i] = false;
newPointCount++;
}
}
if(maxValue > 1 && lb.debug && lb.debugView == 2 && !lb.pauseRender){
strokeWeight(1);
noFill();
stroke(255, 0, 0);
rect(xbox, ybox, size, size);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if(histogram[x][y] > 0){
stroke(histogram[x][y]/float(maxValue)*255.0, 0, 0);
point(x+xbox, y+ybox);
}
}
}
}
float circleProbability = maxValue*maxRadiusValue/float(votes) * (pointCount - newPointCount)/pointCount;
if(lastCircleProbability*lb.minNextCircleThresholdRatio < circleProbability){
color globColor = lb.m.average(maxCoords[0]-avgRadius, maxCoords[1]-avgRadius, maxCoords[0]+avgRadius, maxCoords[1]+avgRadius);
PVector globPosition = new PVector(maxCoords[0], maxCoords[1]);
PVector globSize = new PVector(avgDiameter, avgDiameter);
State state = new State(globColor, globPosition, globSize, lb.frameTimestamp);
circles.add(state);
if(lb.debug && lb.debugView == 2 && !lb.pauseRender){
stroke(0, 255, 0, 100);
textAlign(CENTER, TOP);
strokeWeight(3);
ellipse(maxCoords[0], maxCoords[1], avgDiameter, avgDiameter);
fill(0, 255, 0);
text(circleProbability, maxCoords[0], maxCoords[1] + avgRadius);
}
}
else {
if(lb.debug && lb.debugView == 2 && !lb.pauseRender){
stroke(255, 0, 0, 100);
textAlign(CENTER, TOP);
strokeWeight(3);
ellipse(maxCoords[0], maxCoords[1], avgDiameter, avgDiameter);
fill(0, 255, 0);
text(circleProbability, maxCoords[0], maxCoords[1] + avgRadius);
}
break;
}
lastCircleProbability = circleProbability;
if(newPointCount < lb.minPointCount)
break;
int[][] newBoundary = new int[newPointCount][2];
int index = 0;
for (int i = 0; i < pointCount; i++) {
if(!pixelDelete[i]){
newBoundary[index++] = boundary[i];
}
}
pointCount = newPointCount;
boundary = newBoundary;
}
if(lb.debug && lb.debugView == 2 && !lb.pauseRender){
stroke(255);
strokeWeight(1);
if(boundary!=null){
beginShape(POINTS);
for(int j=0;j<pointCount;j++){
vertex(boundary[j][0], boundary[j][1]);
}
endShape();
}
}
if(circles.size() == 2){
State c1 = circles.get(0);
State c2 = circles.get(1);
float dx = c1.sposition.x-c2.sposition.x;
float dy = c1.sposition.y-c2.sposition.y;
float d = sqrt(dx*dx+dy*dy);
if(d + c1.ssize.x < c2.ssize.x + 2)
circles.remove(0);
if(d + c2.ssize.x < c1.ssize.x + 2)
circles.remove(1);
}
return circles;
}
};