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
|
import numpy as np
from dmsh.geometry 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()
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)
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)
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 np.array_equal(path.is_convex_node, [True, True, False, True, True])
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 np.array_equal(path.is_convex_node, [True, True, False, True, True])
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 = np.array([0.01, 0.16, 1.0 / 104.0, 0.01, 0.02, 0.0])
assert np.all(np.abs(dist - ref) < 1.0e-12)
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 = np.array([0.1, 0.4, np.sqrt(1.0 / 104.0), 0.1, np.sqrt(2) / 10, 0.0])
assert np.all(np.abs(dist - ref) < 1.0e-12)
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 = np.array([-0.1, -0.4, np.sqrt(1.0 / 104.0), 0.1, np.sqrt(2) / 10, 0.0])
assert np.all(np.abs(dist - ref) < 1.0e-12)
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 np.array_equal(contains_points, [True, True, False, False, False, True])
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 + 1.0e-12],
[0.0, 1.1],
[-0.1, 1.1],
[1.0, 1.0],
]
)
ref = np.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 np.all(np.abs(closest_points - ref) < 1.0e-12)
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 = np.array([-0.01, -0.16, 1.0 / 104.0, 0.01, 0.02, 0.0])
assert np.all(np.abs(dist - ref) < 1.0e-12)
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 np.all(contains_points)
dist = path.signed_squared_distance([[0.5, 0.4], [0.5, 0.6]])
ref = np.array([-0.02, -0.02])
assert np.all(np.abs(dist - ref) < 1.0e-12)
def test_project_distance():
path = pypathlib.ClosedPath(
[
[0.0, 0.0],
[1.5, 0.4],
[1.0, 1.0],
]
)
closest_points = path.closest_points(
[
[0.5, 0.1],
[0.5, 0.2],
[0.5, 0.3],
[0.5, 0.4],
[0.5, 0.5],
]
)
# closest_points = np.array([4.9170124481327798e-01, 1.3112033195020747e-01])
# closest_points = np.array([4.9170124481327804e-01, 1.3112033195020747e-01])
# the projected point should be _on_ the polygon
dist = path.distance(closest_points)
assert np.all(dist < 1.0e-12)
# 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 np.array_equal(contains_points, [False, False])
if __name__ == "__main__":
test_closest_points()
|