File: test_708f_path_nesting.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (140 lines) | stat: -rw-r--r-- 3,981 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
#  Copyright (c) 2020, Manfred Moitzi
#  License: MIT License

import pytest
from ezdxf.render.forms import square, translate
from ezdxf.path import Path, nesting, from_vertices

EXTERIOR = list(translate(square(10), (-5, -5)))
EXT1_PATH = from_vertices(EXTERIOR)
EXT2_PATH = from_vertices(translate(EXTERIOR, (11, 0)))

CENTER_HOLE1 = list(translate(square(8), (-4, -4)))
CH1_PATH = from_vertices(CENTER_HOLE1)

CENTER_HOLE2 = list(translate(square(6), (-3, -3)))
CH2_PATH = from_vertices(CENTER_HOLE2)

LEFT_HOLE = list(translate(square(2.1), (-3, -1)))
LH_PATH = from_vertices(LEFT_HOLE)

RIGHT_HOLE = list(translate(square(2.0), (3, -1)))
RH_PATH = from_vertices(RIGHT_HOLE)

DETECTION_DATA = [
    pytest.param(
        # Each polygon is a list of paths
        [EXT1_PATH],
        [[EXT1_PATH]],
        id="1 path",
    ),
    pytest.param(
        # returns the path sorted by area, and reversed if equal sized
        [EXT1_PATH, EXT2_PATH],
        [[EXT2_PATH], [EXT1_PATH]],
        id="2 separated paths",
    ),
    pytest.param(
        [CH1_PATH, EXT1_PATH], [[EXT1_PATH, [CH1_PATH]]], id="1 nested sub-path"
    ),
    pytest.param(
        [CH1_PATH, EXT1_PATH, CH2_PATH],
        [[EXT1_PATH, [CH1_PATH, [CH2_PATH]]]],
        id="2 nested sub-path",
    ),
    pytest.param(
        [RH_PATH, LH_PATH, EXT1_PATH],
        [[EXT1_PATH, [LH_PATH], [RH_PATH]]],
        id="2 separated sub-paths",
    ),
]


@pytest.mark.parametrize("paths,polygons", DETECTION_DATA)
def test_fast_bbox_detection(paths, polygons):
    assert nesting.make_polygon_structure(paths) == polygons


@pytest.mark.parametrize(
    "polygons,exp_ccw,exp_cw",
    [
        pytest.param(
            [[EXT1_PATH]],
            [EXT1_PATH],  # ccw paths
            [],  # cw paths
            id="1 polygon",
        ),
        pytest.param(
            [[EXT1_PATH], [EXT1_PATH]],
            [EXT1_PATH, EXT1_PATH],  # ccw paths
            [],  # cw paths
            id="2 polygons",
        ),
        pytest.param(
            [[EXT1_PATH, [CH1_PATH]]],
            [EXT1_PATH],  # ccw paths
            [CH1_PATH],  # cw paths
            id="1 polygon 1 nested sub-polygon",
        ),
        pytest.param(
            [[EXT1_PATH, [CH1_PATH, [CH1_PATH]]]],
            [EXT1_PATH, CH1_PATH],  # ccw paths
            [CH1_PATH],  # cw paths
            id="1 polygon 2 nested sub-polygons",
        ),
        pytest.param(
            [[EXT1_PATH, [CH1_PATH], [CH1_PATH]]],
            [EXT1_PATH],  # ccw paths
            [CH1_PATH, CH1_PATH],  # cw paths
            id="1 polygon 2 separated sub-polygons",
        ),
    ],
)
def test_winding_deconstruction(polygons, exp_ccw, exp_cw):
    ccw, cw = nesting.winding_deconstruction(polygons)
    assert ccw == exp_ccw
    assert cw == exp_cw


@pytest.mark.parametrize(
    "polygons,n",
    [
        pytest.param([[EXT1_PATH]], 1, id="1 polygon"),
        pytest.param([[EXT1_PATH], [EXT1_PATH]], 2, id="2 polygons"),
        pytest.param(
            [[EXT1_PATH, [CH1_PATH]]], 2, id="1 polygon 1 nested sub-polygon"
        ),
        pytest.param(
            [[EXT1_PATH, [CH1_PATH, [CH1_PATH]]]],
            3,
            id="1 polygon 2 nested sub-polygons",
        ),
        pytest.param(
            [[EXT1_PATH, [CH1_PATH], [CH1_PATH]]],
            3,
            id="1 polygon 2 separated sub-polygons",
        ),
        pytest.param(
            [[EXT1_PATH, [CH1_PATH, [CH2_PATH]], [CH1_PATH, [CH2_PATH]]]],
            5,
            id="1 polygon 2 separated nested sub-polygons",
        ),
    ],
)
def test_flatten_polygons(polygons, n):
    nlists = 0
    npaths = 0
    for path in list(nesting.flatten_polygons(polygons)):
        if isinstance(path, Path):
            npaths += 1
        elif isinstance(path, list):
            nlists += 1
        else:
            raise TypeError("?")

    assert nlists == 0
    assert npaths == n


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