-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.py
298 lines (231 loc) · 8.03 KB
/
geometry.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
import numpy as np
##########################################################
# 3 tangent circles
##########################################################
dot = np.dot
def solve_2nd_degree(a, b, c):
delta = b ** 2 - 4 * a * c
if delta < 0 or a == 0:
return []
return (
(-b + np.sqrt(delta)) / (2 * a),
(-b - np.sqrt(delta)) / (2 * a)
)
def find_origin(a, b):
""" find one point for the line defined by a.x = b"""
ax, ay = a
if np.abs(ax) > np.abs(ay):
o = np.array([b/ax, 0])
else:
o = np.array([0, b/ay])
# print(np.dot(o, a) - b)
return o
def norm2(x):
return np.dot(x, x)
def norm(x):
return np.sqrt(norm2(x))
def contact_3circle(
c1, r1,
c2, r2,
r3):
"""
Find c3 st. (c1, r1), (c2, r2) and (c3, r3) are tangent
"""
b = ((r1 + r3)**2 - (r2 + r3)**2 - norm2(c1) + norm2(c2)) / 2
a = c2 - c1
# a.c3 = b
aorth = np.array([a[1], -a[0]])
o = find_origin(a, b)
# c3 = o + t * aorth, find t
oprime = o - c1
ts = solve_2nd_degree(
norm2(aorth),
2 * dot(oprime, aorth),
norm2(oprime) - (r1 + r3)**2
)
return [o + t * aorth for t in ts]
def contact_3circle_inside(
c1, r1,
c2, r2,
r3):
"""
Find c3 st. (c1, r1), (c2, r2) and (c3, r3) are tangent
but c1 and c3 are inside c2, r2.
"""
b = ((r1 + r3)**2 - (r2 - r3)**2 - norm2(c1) + norm2(c2)) / 2
a = c2 - c1
# a.c3 = b
aorth = np.array([a[1], -a[0]])
o = find_origin(a, b)
# c3 = o + t * aorth, find t
oprime = o - c1
ts = solve_2nd_degree(
norm2(aorth),
2 * dot(oprime, aorth),
norm2(oprime) - (r1 + r3)**2
)
return [o + t * aorth for t in ts]
##########################################################
# KDTree for circles
# From here on we use the Circle object
##########################################################
def intersect_range(amin, amax, bmin, bmax):
return not(bmax < amin or amax < bmin)
def circle_intersects_bbox(circle, bbox):
c, r = circle
x, y = c
xmin, ymin, xmax, ymax = bbox
# horizontal intersect
if x + r < xmin:
return False
elif x < xmin:
yo = np.sqrt(r ** 2 - (xmin - x) ** 2)
return intersect_range(y - yo, y + yo, ymin, ymax)
elif x < xmax:
return intersect_range(y - r, y + r, ymin, ymax)
elif x - r < xmax:
yo = np.sqrt(r ** 2 - (x - xmax) ** 2)
return intersect_range(y - yo, y + yo, ymin, ymax)
else:
return False
def filter_with_bbox(circles, bbox):
return list(filter(
lambda circle: circle_intersects_bbox((circle.c, circle.r), bbox),
circles
))
class Circle:
def __init__(self, c, r, name=None):
self.c = c
self.r = r
self.name = name
def intersects_bbox(self, bbox):
return circle_intersects_bbox((self.c, self.r), bbox)
def __hash__(self):
return hash("%s %s" % (self.c, self.r))
def __str__(self):
return "(c=[%g %g], r=%g%s)" % (
self.c[0], self.c[1], self.r,
" %s" % self.name if self.name is not None else ""
)
max_per_leaf = 10
class Node:
def __init__(self, bbox, path, circles=None):
self.bbox = bbox
self.path = path
self.is_leaf = True
self.circles = circles if circles else []
def surface(self):
xmin, ymin, xmax, ymax = self.bbox
return (xmax - xmin) * (ymax - ymin)
def split(self):
xmin, ymin, xmax, ymax = self.bbox
if xmax - xmin > ymax - ymin:
# split vertically
xmid = (xmin + xmax) / 2
bbox1 = (xmin, ymin, xmid, ymax)
bbox2 = (xmid, ymin, xmax, ymax)
else:
# split horizontally
ymid = (ymin + ymax) / 2
bbox1 = (xmin, ymin, xmax, ymid)
bbox2 = (xmin, ymid, xmax, ymax)
circles = self.circles
del self.circles
self.child1 = Node(bbox1, self.path + "1", filter_with_bbox(circles, bbox1))
self.child2 = Node(bbox2, self.path + "2", filter_with_bbox(circles, bbox2))
self.is_leaf = False
def add_circle(self, circle):
if not circle.intersects_bbox(self.bbox):
return
if self.is_leaf:
self.circles.append(circle)
if len(self.circles) > max_per_leaf:
self.split()
else:
self.child1.add_circle(circle)
self.child2.add_circle(circle)
def display(self):
bb = ','.join("%g" % x for x in self.bbox)
print(f"{self.path} is_leaf={self.is_leaf} bbox={bb}")
if self.is_leaf:
for circle in self.circles:
print(" " * len(self.path), circle)
else:
self.child1.display()
self.child2.display()
##########################################################
# Explore KDTree
##########################################################
def enumerate_intersecting_leaves(root, circle):
if not circle.intersects_bbox(root.bbox):
return
if root.is_leaf:
yield root
else:
yield from enumerate_intersecting_leaves(root.child1, circle)
yield from enumerate_intersecting_leaves(root.child2, circle)
def enumerate_leaves(root):
""" enumerate all leaves in the KTree """
if root.is_leaf:
yield root
else:
yield from enumerate_leaves(root.child1)
yield from enumerate_leaves(root.child2)
def range_distance(amin, amax, bmin, bmax):
if amax < bmin:
return bmin - amax
if bmax < amin:
return amin - bmax
# the ranges intersect
return 0
def nearest_corner(bbox, xy):
""" find nearest corner of the bbox w.r.t. xy"""
x, y = xy
xmin, ymin, xmax, ymax = bbox
return (
xmin if x < (xmin + xmax) / 2 else xmax,
ymin if y < (ymin + ymax) / 2 else ymax
)
def bbox_distance(bbox1, bbox2):
""" minimum distance between contents of 2 bounding boxes """
xmin1, ymin1, xmax1, ymax1 = bbox1
xmin2, ymin2, xmax2, ymax2 = bbox2
dy = range_distance(ymin1, ymax1, ymin2, ymax2)
dx = range_distance(xmin1, xmax1, xmin2, xmax2)
if dx != 0 and dy != 0:
# then it is a corner-wise distance
# find which corner...
x1, y1 = nearest_corner(bbox1, (xmin2, ymin2))
x2, y2 = nearest_corner(bbox2, (xmin1, ymin1))
return np.hypot(x1 - x2, y1 - y2)
else:
return max(dx, dy)
def enumerate_pairs_2(root1, root2, distance):
# check if the nodes are close enough
d = bbox_distance(root1.bbox, root2.bbox)
if d > distance:
return
#print(pref, end='\r', flush=True)
#time.sleep(0.02)
if root1.is_leaf and root2.is_leaf:
yield root1, root2
elif root1.is_leaf:
yield from enumerate_pairs_2(root1, root2.child1, distance)
yield from enumerate_pairs_2(root1, root2.child2, distance)
elif root2.is_leaf:
yield from enumerate_pairs_2(root1.child1, root2, distance)
yield from enumerate_pairs_2(root1.child2, root2, distance)
else:
yield from enumerate_pairs_2(root1.child1, root2.child1, distance)
yield from enumerate_pairs_2(root1.child1, root2.child2, distance)
yield from enumerate_pairs_2(root1.child2, root2.child1, distance)
yield from enumerate_pairs_2(root1.child2, root2.child2, distance)
def enumerate_pairs(root, dis):
""" yield pairs of leaves such that the min distance between leave bbox
is < distance"""
if root.is_leaf:
return
else:
yield from enumerate_pairs_2(root.child1, root.child2, dis)
yield from enumerate_pairs(root.child1, dis)
yield from enumerate_pairs(root.child2, dis)