-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_geometry.py
349 lines (258 loc) · 10.2 KB
/
test_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
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
import unittest
import geometry
import fields
import Cgeometry
import numpy as np
class TestCircle(unittest.TestCase):
def test2(self):
c1 = np.array([0.4, 0.3]); r1 = 0.10
c2 = np.array([0.7, 0.5]); r2 = 0.25
r3 = 0.15
c3s = geometry.contact_3circle(c1, r1, c2, r2, r3)
for c3 in c3s:
np.testing.assert_almost_equal(geometry.norm(c1 - c3), r1 + r3)
np.testing.assert_almost_equal(geometry.norm(c2 - c3), r2 + r3)
def test3C(self):
def to_vec2(x):
return Cgeometry.Vec2(x[0], x[1])
c1 = np.array([0.4, 0.3]); r1 = 0.10
c2 = np.array([0.7, 0.5]); r2 = 0.25
r3 = 0.15
c31 = Cgeometry.Vec2()
c32 = Cgeometry.Vec2()
nv = Cgeometry.contact_3circle(to_vec2(c1), r1, to_vec2(c2), r2, r3, c31, c32)
self.assertEqual(nv, 2)
c3s = [
np.array([c31.x, c31.y]),
np.array([c32.x, c32.y])
]
for c3 in c3s:
np.testing.assert_almost_equal(geometry.norm(c1 - c3), r1 + r3)
np.testing.assert_almost_equal(geometry.norm(c2 - c3), r2 + r3)
def random_bbox(rs):
xmin, xmax, ymin, ymax = rs.rand(4)
if xmax < xmin:
xmin, xmax = xmax, xmin
if ymax < ymin:
ymin, ymax = ymax, ymin
return (xmin, ymin, xmax + 0.1, ymax + 0.1)
def transpose_bbox(bbox):
xmin, ymin, xmax, ymax = bbox
return (ymin, xmin, ymax, xmax)
def plot_bbox(bbox):
xmin, ymin, xmax, ymax = bbox
pyplot.gca().add_patch(
pyplot.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, color=np.random.rand(3))
)
class TestBBoxDistance(unittest.TestCase):
def test_symmetry(self):
rs = np.random.RandomState(456)
for a in range(50):
bbox1 = random_bbox(rs)
bbox2 = random_bbox(rs)
d12 = geometry.bbox_distance(bbox1, bbox2)
d21 = geometry.bbox_distance(bbox2, bbox1)
# print(bbox1, bbox2, d12, d21)
np.testing.assert_almost_equal(d12, d21)
# + transpose
bbox1t = transpose_bbox(bbox1)
bbox2t = transpose_bbox(bbox2)
d12t = geometry.bbox_distance(bbox1t, bbox2t)
np.testing.assert_almost_equal(d12, d12t)
def check_consistent(root, circles):
if root.is_leaf:
set1 = set()
for circle in root.circles:
if circle.intersects_bbox(root.bbox):
set1.add(str(circle))
set2 = set()
for circle in circles:
if circle.intersects_bbox(root.bbox):
set2.add(str(circle))
assert set1 == set2
else:
check_consistent(root.child1, circles)
check_consistent(root.child2, circles)
def check_consistent_C(root, flat_shapes):
#print(root.bbox)
if root.is_leaf():
set1 = set()
for shape in flat_shapes:
if shape.intersects(root.bbox):
set1.add(shape.id)
set2 = set()
it = Cgeometry.ShapeVectorIterator(root.shapes)
while it.has_next():
shape = it.next()
if shape.intersects(root.bbox):
set2.add(shape.id)
# print("SETS", set1, set2)
assert set1 == set2
else:
check_consistent_C(root.child1, flat_shapes)
check_consistent_C(root.child2, flat_shapes)
class TestKDTree(unittest.TestCase):
def test_build(self):
rs = np.random.RandomState(456)
circles = [
geometry.Circle(rs.rand(2), rs.rand(1) ** 5)
for _ in range(50)
]
root = geometry.Node((-2, -2, 2, 2), path="")
for circle in circles:
root.add_circle(circle)
check_consistent(root, circles)
def test_enumerate_pairs(self):
rs = np.random.RandomState(123)
circles = [
geometry.Circle(rs.rand(2), rs.rand(1) ** 5)
for _ in range(50)
]
root = geometry.Node((-2, -2, 2, 2), path="")
dis = 0.012
for circle in circles:
root.add_circle(circle)
ref = {}
for leaf1 in geometry.enumerate_leaves(root):
for leaf2 in geometry.enumerate_leaves(root):
if leaf1 != leaf2 and geometry.bbox_distance(leaf1.bbox, leaf2.bbox) < dis:
x = [str(leaf1), str(leaf2)]
x.sort()
ref[tuple(x)] = (leaf1, leaf2)
new = {}
for leaf1, leaf2 in geometry.enumerate_pairs(root, dis):
x = [str(leaf1), str(leaf2)]
x.sort()
x = tuple(x)
assert x not in new
new[x] = (leaf1, leaf2)
self.assertEqual(ref.keys(), new.keys())
def enumerate_leaves_ref_C(root):
if root.is_leaf():
yield root
else:
yield from enumerate_leaves_ref_C(root.child1)
yield from enumerate_leaves_ref_C(root.child2)
class TestKDTreeC(unittest.TestCase):
def make_test_kdtree(self, seed=456, ncircle=50):
rs = np.random.RandomState(456)
circles = [
Cgeometry.Circle(rs.rand(), rs.rand(), rs.rand() ** 5, i)
for i in range(ncircle)
]
kdtree = Cgeometry.KDTree(Cgeometry.BBox(-2, -2, 2, 2))
kdtree.max_per_leaf = 4
for circle in circles:
kdtree.add_shape(circle)
return circles, kdtree
def test_build_C(self):
circles, kdtree = self.make_test_kdtree()
check_consistent_C(kdtree.root, circles)
def test_enumerate_leaves(self):
circles, kdtree = self.make_test_kdtree()
sp = Cgeometry.swig_ptr_as_int
ref_leaves = set([
sp(n) for n in enumerate_leaves_ref_C(kdtree.root)
])
new_leaves = set(sp(n) for n in Cgeometry.LeafIterator(kdtree))
self.assertEqual(ref_leaves, new_leaves)
def test_enumerate_intersecting_leaves(self):
circles, kdtree = self.make_test_kdtree()
cir = Cgeometry.Circle(0.25, 0.1, 0.1)
sp = Cgeometry.swig_ptr_as_int
ref_leaves = set([
sp(n) for n in enumerate_leaves_ref_C(kdtree.root)
if cir.intersects(n.bbox)
])
new_leaves = set(sp(n) for n in Cgeometry.IntersectingLeavesIterator(kdtree, cir))
self.assertEqual(ref_leaves, new_leaves)
def test_enumerate_pairs(self):
circles, kdtree = self.make_test_kdtree(ncircle=30)
dis = 0.012
sp = Cgeometry.swig_ptr_as_int
ref = {}
print("NB circles", len(circles))
print("NB leaves", len([leaf1 for leaf1 in Cgeometry.LeafIterator(kdtree)]))
for leaf1 in Cgeometry.LeafIterator(kdtree):
for leaf2 in Cgeometry.LeafIterator(kdtree):
if sp(leaf1) != sp(leaf2) and leaf1.bbox.distance(leaf2.bbox) < dis:
x = tuple(sorted([sp(leaf1), sp(leaf2)]))
ref[tuple(x)] = (leaf1, leaf2)
new = {}
for twoleaves in Cgeometry.TwoLeavesIterator(kdtree, dis):
leaf1, leaf2 = twoleaves.node1, twoleaves.node2
x = tuple(sorted([sp(leaf1), sp(leaf2)]))
assert x not in new
new[x] = (leaf1, leaf2)
# print(list(ref.keys()))
# print(list(new.keys()))
self.assertEqual(ref.keys(), new.keys())
class TestField(unittest.TestCase):
def test_kdtree(self):
nc = 25
rs = np.random.RandomState(345)
radiuses = [0.5 * rs.rand() ** 3 for _ in range(nc)]
# radiuses.sort(reverse=True)
r0 = radiuses[0]
circles_ref = fields.generate_circles_gravity(
np.array([0, 0]), 1,
np.array([0, -1 + r0]), r0,
radiuses[1:]
)
circles_new = fields.generate_circles_gravity_kdtree(
np.array([0, 0]), 1,
np.array([0, -1 + r0]), r0,
radiuses[1:]
)
def rd(x):
m = 1e6
return np.floor(x * m) / m
circles_ref_s = set((rd(c[0]), rd(c[1]), rd(r)) for (c, r) in circles_ref)
circles_new_s = set((rd(cir.c[0]), rd(cir.c[1]), rd(cir.r)) for cir in circles_new)
self.assertEqual(circles_ref_s, circles_new_s)
def test_C(self):
rs = np.random.RandomState(345)
nc = 50
radiuses = [0.5 * rs.rand() ** 3 for _ in range(nc)]
# radiuses.sort(reverse=True)
r0 = radiuses[0]
circles_ref = fields.generate_circles_gravity(
np.array([0, 0]), 1,
np.array([0, -1 + r0]), r0,
radiuses[1:]
)
print()
circles_new = fields.generate_circles_gravity_C(
np.array([0, 0]), 1,
np.array([0, -1 + r0]), r0,
radiuses[1:]
)
def rd(x):
m = 1e6
return np.floor(x * m) / m
circles_ref_s = set((rd(c[0]), rd(c[1]), rd(r)) for (c, r) in circles_ref)
circles_new_s = set((rd(cir.c.x), rd(cir.c.y), rd(cir.r)) for cir in circles_new)
self.assertEqual(circles_ref_s, circles_new_s)
def test_C_kdtree(self):
rs = np.random.RandomState(345)
nc = 50
radiuses = [0.5 * rs.rand() ** 3 for _ in range(nc)]
# radiuses.sort(reverse=True)
r0 = radiuses[0]
circles_ref = fields.generate_circles_gravity(
np.array([0, 0]), 1,
np.array([0, -1 + r0]), r0,
radiuses[1:]
)
print()
circles_new = fields.generate_circles_gravity_C_kdtree(
np.array([0, 0]), 1,
np.array([0, -1 + r0]), r0,
radiuses[1:]
)
def rd(x):
m = 1e6
return np.floor(x * m) / m
circles_ref_s = set((rd(c[0]), rd(c[1]), rd(r)) for (c, r) in circles_ref)
circles_new_s = set((rd(cir.c.x), rd(cir.c.y), rd(cir.r)) for cir in circles_new)
self.assertEqual(circles_ref_s, circles_new_s)