-
Notifications
You must be signed in to change notification settings - Fork 571
/
test_arc.py
96 lines (77 loc) · 3.61 KB
/
test_arc.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
try:
from . import generic as g
except BaseException:
import generic as g
class ArcTests(g.unittest.TestCase):
def test_center(self):
from trimesh.path.arc import arc_center
test_points = [[[0, 0], [1.0, 1], [2, 0]]]
test_results = [[[1, 0], 1.0]]
points = test_points[0]
res_center, res_radius = test_results[0]
center_info = arc_center(points)
C, R, N, angle = (center_info['center'], # NOQA
center_info['radius'],
center_info['normal'],
center_info['span'])
assert abs(R - res_radius) < g.tol_path.zero
assert g.trimesh.util.euclidean(C, res_center) < g.tol_path.zero
def test_center_random(self):
from trimesh.path.arc import arc_center
# Test that arc centers work on well formed random points in 2D and 3D
min_angle = g.np.radians(2)
count = 1000
center_3D = (g.np.random.random((count, 3)) - .5) * 50
center_2D = center_3D[:, 0:2]
radii = g.np.clip(g.np.random.random(count) * 100, min_angle, g.np.inf)
angles = g.np.random.random((count, 2)) * \
(g.np.pi - min_angle) + min_angle
angles = g.np.column_stack((g.np.zeros(count),
g.np.cumsum(angles, axis=1)))
points_2D = g.np.column_stack((g.np.cos(angles[:, 0]),
g.np.sin(angles[:, 0]),
g.np.cos(angles[:, 1]),
g.np.sin(angles[:, 1]),
g.np.cos(angles[:, 2]),
g.np.sin(angles[:, 2]))).reshape((-1, 6))
points_2D *= radii.reshape((-1, 1))
points_2D += g.np.tile(center_2D, (1, 3))
points_2D = points_2D.reshape((-1, 3, 2))
points_3D = g.np.column_stack((points_2D.reshape((-1, 2)),
g.np.tile(center_3D[:, 2].reshape((-1, 1)),
(1, 3)).reshape(-1))).reshape((-1, 3, 3))
for center, radius, three in zip(center_2D,
radii,
points_2D):
info = arc_center(three)
assert g.np.allclose(center, info['center'])
assert g.np.allclose(radius, info['radius'])
for center, radius, three in zip(center_3D,
radii,
points_3D):
transform = g.trimesh.transformations.random_rotation_matrix()
center = g.trimesh.transformations.transform_points([center], transform)[
0]
three = g.trimesh.transformations.transform_points(
three, transform)
info = arc_center(three)
assert g.np.allclose(center, info['center'])
assert g.np.allclose(radius, info['radius'])
def test_multiroot(self):
"""
Test a Path2D object containing polygons nested in
the interiors of other polygons.
"""
inner = g.trimesh.creation.annulus(r_min=.5, r_max=.6)
outer = g.trimesh.creation.annulus(r_min=.9, r_max=1.0)
m = inner + outer
s = m.section(plane_normal=[0, 0, 1],
plane_origin=[0, 0, 0])
p = s.to_planar()[0]
assert len(p.polygons_closed) == 4
assert len(p.polygons_full) == 2
assert len(p.root) == 2
g.check_path2D(p)
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()