File: test_closed_path.py

package info (click to toggle)
python-pypathlib 0.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 156 kB
  • sloc: python: 360; makefile: 30
file content (176 lines) | stat: -rw-r--r-- 4,746 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
import numpy

import pypathlib


def test_show():
    path = pypathlib.ClosedPath([[0.0, 0.0], [1.0, 0.0], [1.1, 1.1], [0.1, 1.0]])
    path.show()
    return


def test_convex():
    path = pypathlib.ClosedPath([[0.0, 0.0], [1.0, 0.0], [1.1, 1.1], [0.1, 1.0]])

    ref = 1.045
    assert abs(path.area - ref) < 1.0e-12 * ref
    assert path.positive_orientation
    assert all(path.is_convex_node)
    return


def test_orientation():
    path = pypathlib.ClosedPath([[0.1, 1.0], [1.1, 1.1], [1.0, 0.0], [0.0, 0.0]])

    ref = 1.045
    assert abs(path.area - ref) < 1.0e-12 * ref
    assert not path.positive_orientation
    assert all(path.is_convex_node)
    return


def test_concave():
    path = pypathlib.ClosedPath(
        [[0.0, 0.0], [1.0, 0.0], [0.9, 0.5], [1.1, 1.1], [0.1, 1.0]]
    )

    ref = 0.965
    assert abs(path.area - ref) < 1.0e-12 * ref
    assert path.positive_orientation
    assert numpy.array_equal(path.is_convex_node, [True, True, False, True, True])
    return


def test_concave_counterclock():
    path = pypathlib.ClosedPath(
        [[0.1, 1.0], [1.1, 1.1], [0.9, 0.5], [1.0, 0.0], [0.0, 0.0]]
    )

    ref = 0.965
    assert abs(path.area - ref) < 1.0e-12 * ref
    assert not path.positive_orientation
    assert numpy.array_equal(path.is_convex_node, [True, True, False, True, True])
    return


def test_squared_distance():
    path = pypathlib.ClosedPath(
        [[0.0, 0.0], [1.0, 0.0], [0.9, 0.5], [1.0, 1.0], [0.0, 1.0]]
    )

    dist = path.squared_distance(
        [[0.2, 0.1], [0.5, 0.5], [1.0, 0.5], [0.0, 1.1], [-0.1, 1.1], [1.0, 1.0]]
    )
    ref = numpy.array([0.01, 0.16, 1.0 / 104.0, 0.01, 0.02, 0.0])
    assert numpy.all(numpy.abs(dist - ref) < 1.0e-12)
    return


def test_distance():
    path = pypathlib.ClosedPath(
        [[0.0, 0.0], [1.0, 0.0], [0.9, 0.5], [1.0, 1.0], [0.0, 1.0]]
    )

    dist = path.distance(
        [[0.2, 0.1], [0.5, 0.5], [1.0, 0.5], [0.0, 1.1], [-0.1, 1.1], [1.0, 1.0]]
    )
    ref = numpy.array([0.1, 0.4, numpy.sqrt(1.0 / 104.0), 0.1, numpy.sqrt(2) / 10, 0.0])
    assert numpy.all(numpy.abs(dist - ref) < 1.0e-12)
    return


def test_signed_distance():
    path = pypathlib.ClosedPath(
        [[0.0, 0.0], [1.0, 0.0], [0.9, 0.5], [1.0, 1.0], [0.0, 1.0]]
    )

    dist = path.signed_distance(
        [[0.2, 0.1], [0.5, 0.5], [1.0, 0.5], [0.0, 1.1], [-0.1, 1.1], [1.0, 1.0]]
    )
    print(dist)
    ref = numpy.array(
        [-0.1, -0.4, numpy.sqrt(1.0 / 104.0), 0.1, numpy.sqrt(2) / 10, 0.0]
    )
    assert numpy.all(numpy.abs(dist - ref) < 1.0e-12)
    return


def test_inside():
    path = pypathlib.ClosedPath(
        [[0.0, 0.0], [1.0, 0.0], [0.9, 0.5], [1.0, 1.0], [0.0, 1.0]]
    )

    contains_points = path.contains_points(
        [[0.2, 0.1], [0.5, 0.5], [1.0, 0.5], [0.0, 1.1], [-0.1, 1.1], [1.0, 1.0]]
    )
    assert numpy.array_equal(contains_points, [True, True, False, False, False, True])
    return


def test_closest_points():
    path = pypathlib.ClosedPath(
        [[0.0, 0.0], [1.0, 0.0], [0.9, 0.5], [1.0, 1.0], [0.0, 1.0]]
    )

    closest_points = path.closest_points(
        [[0.2, 0.1], [0.5, 0.5], [1.0, 0.5], [0.0, 1.1], [-0.1, 1.1], [1.0, 1.0]]
    )

    ref = numpy.array(
        [
            [0.2, 0.0],
            [0.9, 0.5],
            [9.0384615384615385e-01, 5.1923076923076927e-01],
            [0.0, 1.0],
            [0.0, 1.0],
            [1.0, 1.0],
        ]
    )
    assert numpy.all(numpy.abs(closest_points - ref) < 1.0e-12)
    return


def test_signed_squared_distance():
    path = pypathlib.ClosedPath(
        [[0.0, 0.0], [1.0, 0.0], [0.9, 0.5], [1.0, 1.0], [0.0, 1.0]]
    )

    dist = path.signed_squared_distance(
        [[0.2, 0.1], [0.5, 0.5], [1.0, 0.5], [0.0, 1.1], [-0.1, 1.1], [1.0, 1.0]]
    )
    ref = numpy.array([-0.01, -0.16, 1.0 / 104.0, 0.01, 0.02, 0.0])
    assert numpy.all(numpy.abs(dist - ref) < 1.0e-12)
    return


def test_sharp_angle():
    path = pypathlib.ClosedPath(
        [
            [0.0, 0.0],
            [1.0, 0.0],
            [1.0, 0.45],
            [0.6, 0.5],
            [1.0, 0.55],
            [1.0, 1.0],
            [0.0, 1.0],
        ]
    )

    contains_points = path.contains_points([[0.5, 0.4], [0.5, 0.6]])
    assert numpy.all(contains_points)

    dist = path.signed_squared_distance([[0.5, 0.4], [0.5, 0.6]])
    ref = numpy.array([-0.02, -0.02])
    assert numpy.all(numpy.abs(dist - ref) < 1.0e-12)
    return


# def test_two_points():
#     path = pypathlib.ClosedPath([[-0.5, 1.0], [+0.5, 1.0]])
#     contains_points = path.contains_points([[0.0, 0.0], [0.0, 2.0]])
#     assert numpy.array_equal(contains_points, [False, False])
#     return


if __name__ == "__main__":
    test_sharp_angle()