forked from mozman/ezdxf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_708e_path_shapes.py
148 lines (114 loc) · 4.34 KB
/
test_708e_path_shapes.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
# Copyright (c) 2021, Manfred Moitzi
# License: MIT License
import pytest
import math
from ezdxf.math import basic_transformation, BoundingBox, Matrix44
from ezdxf.path import shapes, bbox, triangulate
def test_unit_circle():
circle = shapes.unit_circle()
assert circle.has_curves
assert len(circle) == 4, "expected 4 cubic Bèzier segments"
assert circle.start.isclose((1, 0, 0))
assert circle.end.isclose((1, 0, 0))
def test_scale_circle():
m = shapes.elliptic_transformation(radius=2)
circle = shapes.unit_circle(transform=m)
assert circle.start.isclose((2, 0, 0))
def test_move_circle():
m = shapes.elliptic_transformation(center=(-1, 0, 0))
circle = shapes.unit_circle(transform=m)
assert circle.start.isclose((0, 0, 0))
def test_wedge():
wedge = shapes.wedge(0, math.pi / 2)
assert wedge.has_curves
assert wedge.has_lines
assert wedge.is_closed is True
assert len(wedge) == 3, "expected 2 lines and 1 cubic Bèzier segment"
assert wedge.start.isclose((0, 0, 0)), "has to start at the center"
assert wedge.end.isclose((0, 0, 0)), "has to end at the center"
extends = bbox([wedge])
assert extends.extmin.isclose((0.0, 0.0))
assert extends.extmax.isclose((1.0, 1.0))
def test_ellipse():
m = shapes.elliptic_transformation(ratio=0.5)
circle = shapes.unit_circle(transform=m)
extends = bbox([circle])
assert extends.extmin.isclose((-1.0, -0.5))
assert extends.extmax.isclose((1.0, 0.5))
def test_rotated_ellipse():
m = shapes.elliptic_transformation(ratio=0.5, rotation=math.pi / 2)
circle = shapes.unit_circle(transform=m)
extends = bbox([circle])
assert extends.extmin.isclose((-0.5, -1.0))
assert extends.extmax.isclose((0.5, 1.0))
@pytest.mark.parametrize(
"w,h",
[
(0, 0),
(1, 0),
(0, 1),
(-1, 1),
(1, -1),
],
)
def test_invalid_rect_dimensions(w, h):
with pytest.raises(ValueError):
shapes.rect(w, h)
def test_rectangle():
rect = shapes.rect(3, 2)
extends = bbox([rect])
assert extends.extmin.isclose((-1.5, -1.0))
assert extends.extmax.isclose((1.5, 1.0))
assert rect.is_closed is True
def test_transformed_rectangle():
m = basic_transformation(z_rotation=math.pi / 2)
rect = shapes.rect(3, 2, transform=m)
extends = bbox([rect])
assert extends.extmin.isclose((-1.0, -1.5))
assert extends.extmax.isclose((1.0, 1.5))
def test_ngon():
# basic ngon tool is tested here: test_702_render_forms.py
square = shapes.ngon(4, length=1.0)
# first vertex is on th x-axis => square is rotated about 45 deg:
extends = bbox([square])
d = math.sin(math.pi / 4)
assert extends.extmin.isclose((-d, -d))
assert extends.extmax.isclose((d, d))
assert square.is_closed is True
def test_star():
# basic star tool is tested here: test_702_render_forms.py
star4 = shapes.star(4, r1=1, r2=0.5)
# first vertex is on th x-axis!
extends = bbox([star4])
assert extends.extmin.isclose((-1, -1))
assert extends.extmax.isclose((1, 1))
assert star4.is_closed is True
def test_gear():
# basic gear tool is tested here: test_702_render_forms.py
gear = shapes.gear(16, 0.1, 0.3, 0.2, 1.0)
# first vertex is on th x-axis!
extends = bbox([gear])
assert extends.extmin.isclose((-0.999, -0.999), abs_tol=1e-3)
assert extends.extmax.isclose((0.999, 0.999), abs_tol=1e-3)
assert gear.is_closed is True
@pytest.mark.parametrize("ccw", [True, False])
def test_helix_positive_pitch_goes_up(ccw):
h = shapes.helix(radius=2.0, pitch=1.0, turns=10, ccw=ccw)
bbox = BoundingBox(h.flattening(0.1))
assert bbox.extmax.isclose((2, 2, 10))
assert bbox.extmin.isclose((-2, -2, 0))
assert h.end.isclose((2, 0, 10))
@pytest.mark.parametrize("ccw", [True, False])
def test_helix_negative_pitch_goes_down(ccw):
h = shapes.helix(radius=2.0, pitch=-1.0, turns=10, ccw=ccw)
bbox = BoundingBox(h.flattening(0.1))
assert bbox.extmax.isclose((2, 2, 0))
assert bbox.extmin.isclose((-2, -2, -10))
assert h.end.isclose((2, 0, -10))
def test_triangulate_doughnut():
circle0 = shapes.unit_circle(segments=8)
circle1 = circle0.transform(Matrix44.scale(3))
triangles = list(triangulate([circle1, circle0], 0.01))
assert len(triangles) == 258
if __name__ == "__main__":
pytest.main([__file__])