File: test_651_construction_polyline.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (259 lines) | stat: -rw-r--r-- 8,765 bytes parent folder | download
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
#  Copyright (c) 2021, Manfred Moitzi
#  License: MIT License
from typing import List
import math
import pytest
from ezdxf.math import ConstructionPolyline, Vec3


@pytest.fixture(scope="module")
def poly1() -> ConstructionPolyline:
    return ConstructionPolyline([(0, 0), (1, 0), (1, 1), (0, 1)])


@pytest.fixture(scope="module")
def poly2() -> ConstructionPolyline:
    return ConstructionPolyline([(0, 0), (1, 0), (1, 1), (0, 1)], close=True)


class TestSequenceInterface:
    def test_empty_polyline(self):
        cp = ConstructionPolyline([])
        assert len(cp) == 0

    def test_len_open_polyline(self, poly1):
        assert len(poly1) == 4

    def test_len_closed_polyline(self, poly2):
        assert len(poly2) == 5

    def test_get_single_vertex(self, poly1):
        assert poly1[1] == (1, 0)

    def test_get_last_vertex(self, poly1):
        assert poly1[-1] == (0, 1)

    def test_get_vertex_slice(self, poly1):
        assert list(poly1[1:3]) == [(1, 0), (1, 1)]

    def test_vertex_slice_has_same_type(self, poly1):
        assert isinstance(poly1[1:3], ConstructionPolyline)

    def test_is_immutable(self, poly1):
        with pytest.raises(TypeError):
            poly1[1] = (1, 0)
        with pytest.raises(TypeError):
            del poly1[1]


class TestExtendedData:
    """The data() method returns the distance from the start vertex along the
    polyline, the distance from the previous vertex and the vertex itself for
    a give index.

    """

    def test_empty_polyline_raises_Value_error(self):
        with pytest.raises(ValueError):
            ConstructionPolyline([]).data(0)

    def test_data_0(self, poly1):
        assert poly1.data(0) == (0, 0, (0, 0))

    def test_data_1(self, poly1):
        assert poly1.data(1) == (1, 1, (1, 0))

    def test_data_2(self, poly1):
        assert poly1.data(2) == (2, 1, (1, 1))

    def test_data_3(self, poly1):
        assert poly1.data(3) == (3, 1, (0, 1))

    def test_data_for_negative_index(self, poly1):
        assert poly1.data(-1) == (3, 1, (0, 1))
        assert poly1.data(-2) == (2, 1, (1, 1))

    def test_vertices_at_the_same_location(self):
        cp = ConstructionPolyline([(0, 0), (0, 0), (0, 0)])
        assert cp.data(0) == (0, 0, (0, 0))
        assert cp.data(1) == (0, 0, (0, 0))
        assert cp.data(2) == (0, 0, (0, 0))

    def test_raises_index_error(self, poly1):
        with pytest.raises(IndexError):
            poly1.data(4)


class TestLength:
    def test_empty_polyline(self):
        assert ConstructionPolyline([]).length == 0.0

    def test_open_polyline(self, poly1):
        assert poly1.length == 3.0

    def test_closed_polyline(self, poly2):
        assert poly2.length == 4.0

    def test_vertices_at_the_same_location(self):
        cp = ConstructionPolyline([(0, 0), (0, 0), (0, 0)])
        assert cp.length == 0.0


class TestIsClosed:
    def test_empty_polyline_is_not_closed(self):
        assert ConstructionPolyline([]).is_closed is False

    def test_polyline_with_too_few_vertices_is_not_closed(self):
        assert ConstructionPolyline([(0, 0)]).is_closed is False
        assert ConstructionPolyline([(0, 0), (0, 0)]).is_closed is False

    def test_poly1_is_not_closed(self, poly1):
        assert poly1.is_closed is False

    def test_poly2_is_closed(self, poly2):
        assert poly2.is_closed is True


class TestIndexAt:
    @pytest.mark.parametrize("d", [-1, 0, 1])
    def test_empty_polyline(self, d):
        assert ConstructionPolyline([]).index_at(d) == 0

    @pytest.mark.parametrize("d", [-1, 0, 1])
    def test_polyline_with_one_vertex(self, d):
        assert ConstructionPolyline([(1, 1)]).index_at(d) == 0

    def test_short_polyline(self):
        poly = ConstructionPolyline([(0, 0), (1, 0)])
        assert poly.index_at(0.0) == 0
        assert poly.index_at(0.5) == 1
        assert poly.index_at(1) == 1

    def test_long_polyline(self):
        poly = ConstructionPolyline([(0, 0), (1, 0), (2, 0), (3, 0)])
        assert poly.index_at(0.0) == 0
        assert poly.index_at(0.999) == 1
        assert poly.index_at(1.0) == 1
        assert poly.index_at(1.001) == 2
        assert poly.index_at(1.999) == 2
        assert poly.index_at(2.0) == 2
        assert poly.index_at(2.001) == 3
        assert poly.index_at(2.999) == 3
        assert poly.index_at(3.0) == 3
        assert poly.index_at(3.001) == 3


class TestVertexAt:
    def test_empty_polyline_raises_error(self):
        with pytest.raises(ValueError):
            ConstructionPolyline([]).vertex_at(0.0)

    def test_too_few_vertices_raises_error(self):
        with pytest.raises(ValueError):
            ConstructionPolyline([(0, 0)]).vertex_at(0.0)

    def test_out_of_range_raises_error(self, poly1):
        with pytest.raises(ValueError):
            poly1.vertex_at(-1)
        with pytest.raises(ValueError):
            poly1.vertex_at(poly1.length + 0.01)

    def test_interpolation_at_vertex_location(self, poly1):
        assert poly1.vertex_at(0.0) == (0, 0)
        assert poly1.vertex_at(1.0) == (1, 0)
        assert poly1.vertex_at(2.0) == (1, 1)
        assert poly1.vertex_at(3.0) == (0, 1)

    def test_interpolate_last_vertex_of_closed_polyline(self, poly2):
        assert poly2.vertex_at(0.0) == (0, 0)
        assert poly2.vertex_at(poly2.length) == (0, 0)

    def test_interpolate_at_first_edge(self, poly1):
        """Interpolate edge  (0, 0) -> (1, 0)"""
        assert poly1.vertex_at(0.1).isclose((0.1, 0))
        assert poly1.vertex_at(0.5).isclose((0.5, 0))
        assert poly1.vertex_at(0.9).isclose((0.9, 0))

    def test_interpolate_at_second_edge(self, poly1):
        """Interpolate edge  (1, 0) -> (1, 1)"""
        assert poly1.vertex_at(1.1).isclose((1.0, 0.1))
        assert poly1.vertex_at(1.5).isclose((1.0, 0.5))
        assert poly1.vertex_at(1.9).isclose((1.0, 0.9))

    def test_interpolation_for_coincident_vertices_in_front(self):
        cp = ConstructionPolyline([(0, 0), (0, 0), (0, 0), (1, 0)])
        assert cp.vertex_at(0.0).isclose((0.0, 0.0))
        assert cp.vertex_at(0.1).isclose((0.1, 0.0))
        assert cp.vertex_at(0.5).isclose((0.5, 0.0))
        assert cp.vertex_at(0.9).isclose((0.9, 0.0))
        assert cp.vertex_at(1.0).isclose((1.0, 0.0))

    def test_interpolation_for_coincident_vertices_after(self):
        cp = ConstructionPolyline([(0, 0), (1, 0), (1, 0), (1, 0)])
        assert cp.vertex_at(0.0).isclose((0.0, 0.0))
        assert cp.vertex_at(0.1).isclose((0.1, 0.0))
        assert cp.vertex_at(0.5).isclose((0.5, 0.0))
        assert cp.vertex_at(0.9).isclose((0.9, 0.0))
        assert cp.vertex_at(1.0).isclose((1.0, 0.0))

    def test_interpolation_for_coincident_vertices_between(self):
        cp = ConstructionPolyline(
            [(0, 0), (0.5, 0), (0.5, 0), (0.5, 0), (1, 0)]
        )
        assert cp.vertex_at(0.0).isclose((0.0, 0.0))
        assert cp.vertex_at(0.1).isclose((0.1, 0.0))
        assert cp.vertex_at(0.5).isclose((0.5, 0.0))
        assert cp.vertex_at(0.9).isclose((0.9, 0.0))
        assert cp.vertex_at(1.0).isclose((1.0, 0.0))


class TestDivide:
    @pytest.mark.parametrize("count", [-1, 0, 1])
    def test_raises_error_invalid_count(self, count, poly1):
        with pytest.raises(ValueError):
            list(poly1.divide(count))

    def test_divide_by_3(self, poly1):
        vertices: List[Vec3] = list(poly1.divide(3))
        assert vertices[0].isclose(poly1[0])
        assert vertices[1].isclose((1, 0.5))
        assert vertices[2].isclose(poly1[-1])

    def test_divide_by_length(self, poly1):
        vertices: List[Vec3] = list(poly1.divide_by_length(0.7))
        assert len(vertices) == 5
        assert vertices[-1].isclose(poly1.vertex_at(2.8))

    def test_divide_by_length_force_last_vertex(self, poly1):
        vertices: List[Vec3] = list(
            poly1.divide_by_length(0.7, force_last=True)
        )
        assert len(vertices) == 6
        assert vertices[-1].isclose(poly1[-1])


class TestApproximationAccuracy:
    def test_unit_circle(self):
        from ezdxf.entities import Circle

        # create unit circle
        circle: Circle = Circle.new(
            dxfattribs={"center": (0, 0), "radius": 1.0}
        )
        cp = ConstructionPolyline(circle.flattening(0.01))
        assert cp.is_closed is True
        assert len(cp) > 20
        assert abs(cp.length - math.tau) < 0.02

    def test_unit_circle_by_path(self):
        from ezdxf import path

        p = path.unit_circle()
        cp = ConstructionPolyline(p.flattening(0.01, segments=16))
        assert cp.is_closed is True
        assert len(cp) > 60
        assert abs(cp.length - math.tau) < 0.002


if __name__ == "__main__":
    pytest.main([__file__])