-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.py
359 lines (291 loc) · 11.7 KB
/
fields.py
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from geometry import Node, Circle, enumerate_intersecting_leaves, enumerate_pairs, \
contact_3circle_inside, contact_3circle, norm2, norm, enumerate_leaves
import Cgeometry
import numpy as np
######################################################################
# Reference implementation
######################################################################
def generate_circles_gravity(c0, r0, c1, r1, radiuses):
"""
Make a field with a set of circles inside (c0, r0)
The first circle is (c1, r1)
The other ones have radiuses in the list of radiuses, with priority to the ones
with lowest y.
"""
circles = [(c1, r1)]
tot1 = tot2 = 0
for i, r3 in enumerate(radiuses):
nc = len(circles)
c3s = []
# check contact with great circle
for k in range(nc):
c2, r2 = circles[k]
for c3 in contact_3circle_inside(c2, r2, c0, r0, r3):
for l in range(nc):
tot1 += 1
if l == k:
continue
cl, rl = circles[l]
if norm2(c3 - cl) < (rl + r3) ** 2:
break
else:
c3s.append(c3)
tot2 += 1
for j in range(nc):
c1, r1 = circles[j]
for k in range(j + 1, nc):
c2, r2 = circles[k]
for c3 in contact_3circle(c1, r1, c2, r2, r3):
# check if intersection with other circles
for l in range(nc):
tot1 += 1
if l == k or l == j:
continue
cl, rl = circles[l]
if norm2(c3 - cl) < (rl + r3) ** 2:
break
else:
if norm(c0 - c3) + r3 < r0:
c3s.append(c3)
tot2 += 1
# impossible to put this circle in the field
# print(f"{i=:} REF c3s=", c3s)
if len(c3s) == 0:
continue
if True:
# pick the c3 that has lowest y
c3s.sort(key=lambda x: x[1])
else:
# pick the c3 that is closest to 0
c3s.sort(key=lambda x: norm2(x))
c3 = c3s[0]
# print("REF c3=", c3)
circles.append((c3, r3))
print(f"{i=:} nb circles: {len(circles)} "
f"nb c3: {len(c3s)} {tot1=:} {tot2=:}", end="\r", flush=True)
return circles
######################################################################
# Construction with kdtree
######################################################################
def circles_intersect(cir1, cir2):
return norm(cir1.c - cir2.c) < cir1.r + cir2.r
def any_intersection(root, circle, exclude=None):
if exclude is None:
seen = set()
else:
seen = set(exclude)
for leaf in enumerate_intersecting_leaves(root, circle):
for circle2 in leaf.circles:
# print(f"KDTREE {leaf.path} {circle} check {circle2}")
if circle2 in seen:
continue
seen.add(circle2)
if circles_intersect(circle, circle2):
# print(" INTER")
return True
return False
def any_intersection_flat(circles, circle, exclude=None):
if exclude is None:
seen = set()
else:
seen = set(exclude)
for circle2 in circles:
if circle2 in seen:
continue
# print(f"FLAT {circle} check {circle2}")
if circles_intersect(circle, circle2):
# print(" INTER")
return True
return False
def generate_circles_gravity_kdtree(c0, r0, c1, r1, radiuses):
circles = [Circle(c1, r1, name=-1)]
root = Node(
(c0[0] - r0, c0[1] - r0, c0[0] + r0, c0[1] + r0),
""
)
root.add_circle(circles[0])
tot1 = tot2 = 0
for rno, r3 in enumerate(radiuses):
nc = len(circles)
c3s = []
# check contact with great circle
for cir2 in circles:
for c3 in contact_3circle_inside(cir2.c, cir2.r, c0, r0, r3):
if not any_intersection(root, Circle(c3, r3), exclude=[cir2]):
c3s.append(c3)
tot2 += 1
seen = set()
def handle_circle_pair(cir1, cir2):
if (cir1, cir2) in seen or (cir2, cir1) in seen:
return
seen.add((cir1, cir2))
for c3 in contact_3circle(cir1.c, cir1.r, cir2.c, cir2.r, r3):
# check if intersection with other circles
if not any_intersection(
root, Circle(c3, r3),
exclude=[cir1, cir2]):
if norm(c0 - c3) + r3 < r0:
c3s.append(c3)
if True:
# handle pairs of circles within a leaf
for leaf in enumerate_leaves(root):
nc = len(leaf.circles)
for i in range(nc):
for j in range(i + 1, nc):
handle_circle_pair(leaf.circles[i], leaf.circles[j])
# handle pairs of circles within 2 leaves
for leaf1, leaf2 in enumerate_pairs(root, 2 * r3):
for ci in leaf1.circles:
for cj in leaf2.circles:
if ci != cj:
handle_circle_pair(ci, cj)
else:
# brute force
nc = len(circles)
for i in range(nc):
for j in range(i + 1, nc):
handle_circle_pair(circles[i], circles[j])
if len(c3s) == 0:
continue
# pick the c3 that has lowest y
c3s.sort(key=lambda x: x[1])
c3 = c3s[0]
cir3 = Circle(c3, r3, name=rno)
circles.append(cir3)
root.add_circle(cir3)
print(f"iter {rno} nb circles: {len(circles)} "
f"nb c3: {len(c3s)} {tot1=:} {tot2=:}", end="\r", flush=True)
print()
# root.display()
return circles
######################################################################
# Construction with C version
######################################################################
def contact_3circle_C(cir2, cir0, r3, inside_c2=False):
c31 = Cgeometry.Vec2()
c32 = Cgeometry.Vec2()
nres = Cgeometry.contact_3circle(cir2.c, cir2.r, cir0.c, cir0.r, r3, c31, c32, inside_c2)
if nres == 0:
return []
assert nres == 2
return [c31, c32]
def intersects_any_circle_C(cir, circles, exclude):
for cir1 in circles:
if cir1.id in exclude:
continue
if cir1.intersects(cir):
return True
return False
def make_circle_C(c, r, id=-1):
if type(c) == np.ndarray:
c = Cgeometry.Vec2(float(c[0]), float(c[1]))
return Cgeometry.Circle(c.x, c.y, r, id)
def generate_circles_gravity_C(c0, r0, c1, r1, radiuses):
cir0 = make_circle_C(c0, r0)
circles = [make_circle_C(c1, r1, -1)]
tot1 = tot2 = 0
for i, r3 in enumerate(radiuses):
nc = len(circles)
c3s = []
# check contact with great circle
for k in range(nc):
cir2 = circles[k]
for c3 in contact_3circle_C(cir2, cir0, r3, inside_c2=True):
cir3 = make_circle_C(c3, r3)
if not intersects_any_circle_C(cir3, circles, exclude=[cir2.id]):
c3s.append(c3)
tot2 += 1
for j in range(nc):
cir1 = circles[j]
for k in range(j + 1, nc):
cir2 = circles[k]
for c3 in contact_3circle_C(cir1, cir2, r3):
cir3 = make_circle_C(c3, r3)
if not intersects_any_circle_C(cir3, circles, exclude=[cir1.id, cir2.id]):
if cir0.c.distance(c3) + r3 < r0:
c3s.append(c3)
tot2 += 1
# print("c3s=", c3s)
if len(c3s) == 0:
continue
# pick the c3 that has lowest y
c3s.sort(key=lambda c3: c3.y)
c3 = c3s[0]
# print(f"{c3=:}")
circles.append(make_circle_C(c3, r3, i))
print(f"{i=:} nb circles: {len(circles)} "
f"nb c3: {len(c3s)} {tot1=:} {tot2=:}", end="\r", flush=True)
return circles
def intersects_any_circle_C_kdtree(kdtree, cir, exclude):
seen = set(exclude)
for node in Cgeometry.IntersectingLeavesIterator(kdtree, cir):
for shape in Cgeometry.ShapeVectorIterator(node.shapes):
if shape.id in seen:
continue
seen.add(shape.id)
cir2 = Cgeometry.downcast_Circle(shape)
if cir2.intersects(cir):
return True
return False
def generate_circles_gravity_C_kdtree(c0, r0, c1, r1, radiuses):
cir0 = make_circle_C(c0, r0)
circles = [make_circle_C(c1, r1, -1)]
kdtree = Cgeometry.KDTree(Cgeometry.BBox(
cir0.c.x - cir0.r, cir0.c.y - cir0.r,
cir0.c.x + cir0.r, cir0.c.y + cir0.r
))
kdtree.add_shape(circles[0])
tot1 = tot2 = 0
for it, r3 in enumerate(radiuses):
nc = len(circles)
c3s = []
# check contact with great circle
for k in range(nc):
cir2 = circles[k]
for c3 in contact_3circle_C(cir2, cir0, r3, inside_c2=True):
cir3 = make_circle_C(c3, r3)
if not intersects_any_circle_C_kdtree(kdtree, cir3, exclude=[cir2.id]):
c3s.append(c3)
tot2 += 1
seen = set()
def handle_circle_pair(cir1, cir2):
k = tuple(sorted([cir1.id, cir2.id]))
# print("TRY", k)
if k in seen:
return
seen.add(k)
for c3 in contact_3circle_C(cir1, cir2, r3):
#print("TRY", c3)
cir3 = make_circle_C(c3, r3)
if not intersects_any_circle_C_kdtree(kdtree, cir3, exclude=[cir1.id, cir2.id]):
if cir0.c.distance(c3) + r3 < r0:
c3s.append(c3)
# handle pairs of circles within a leaf
for leaf in Cgeometry.LeafIterator(kdtree):
leaf_circles = [
Cgeometry.downcast_Circle(shape) for shape in Cgeometry.ShapeVectorIterator(leaf.shapes)
]
# print(leaf_circles)
nc = len(leaf_circles)
for i in range(nc):
for j in range(i + 1, nc):
handle_circle_pair(leaf_circles[i], leaf_circles[j])
# handle pairs of circles between 2 leaves
for twoleaves in Cgeometry.TwoLeavesIterator(kdtree, r3):
for cir1 in Cgeometry.ShapeVectorIterator(twoleaves.node1.shapes):
cir1 = Cgeometry.downcast_Circle(cir1)
for cir2 in Cgeometry.ShapeVectorIterator(twoleaves.node2.shapes):
cir2 = Cgeometry.downcast_Circle(cir2)
handle_circle_pair(cir1, cir2)
# print(f"{it=:} NEW c3s=", c3s)
if len(c3s) == 0:
continue
# pick the c3 that has lowest y
c3s.sort(key=lambda c3: c3.y)
c3 = c3s[0]
# print(f"NEW {c3=:}")
circles.append(make_circle_C(c3, r3, it))
kdtree.add_shape(make_circle_C(c3, r3, it))
print(f"{i=:} nb circles: {len(circles)} "
f"nb c3: {len(c3s)} {tot1=:} {tot2=:}", end="\r", flush=True)
return circles