Skip to content

Commit

Permalink
test point_in_polygon edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Oct 19, 2023
1 parent e195d01 commit 0c939fd
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions autotest/test_util_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,60 @@ def test_does_isclockwise_work():
assert bool(rslt) is False, "is_clockwise() failed"


def test_point_in_polygon_basic():
def debug_plot(grid, cell, xpts, ypts, mask):
import matplotlib.pyplot as plt

grid.plot()
plt.plot(*list(zip(*cell)))
plt.scatter(xpts[mask], ypts[mask], c="red")
# plt.show()


def test_point_in_polygon_interior():
grid = GridCases().structured_small()
cell = grid.verts[grid.iverts[0]].tolist()
xpts = grid.xcellcenters
ypts = grid.ycellcenters
poly = grid.verts[grid.iverts[0]].tolist()
mask = point_in_polygon(xpts, ypts, poly)
mask = point_in_polygon(xpts, ypts, cell)
assert mask.sum() == 1
assert mask[0, 0] == True
debug_plot(grid, cell, xpts, ypts, mask)


def test_point_in_polygon_vertices():
grid = GridCases().structured_small()
cell = grid.verts[grid.iverts[0]].tolist()
xpts, ypts = list(zip(*cell))
xpts = np.array([xpts])
ypts = np.array([ypts])
mask = point_in_polygon(xpts, ypts, cell)
assert mask.sum() == 1 # only bottom left (due to axis-alignment)
debug_plot(grid, cell, xpts, ypts, mask)

# move points inside boundary
xpts[0, 2] = xpts[0, 2] - 0.001
ypts[0, 2] = ypts[0, 2] + 0.001
xpts[0, 1] = xpts[0, 1] - 0.001
ypts[0, 1] = ypts[0, 1] - 0.001
xpts[0, 0] = xpts[0, 0] + 0.001
ypts[0, 0] = ypts[0, 0] - 0.001
mask = point_in_polygon(xpts, ypts, cell)
assert mask.sum() == 4 # expect all points
debug_plot(grid, cell, xpts, ypts, mask)


def test_point_in_polygon_faces():
grid = GridCases().structured_small()
cell = grid.verts[grid.iverts[0]].tolist()
xpts_v, ypts_v = list(zip(*cell))
xpts_v = np.array([xpts_v])
ypts_v = np.array([ypts_v])
xpts = np.array(
[[xpts_v[0, 0], xpts_v[0, 2], np.mean(xpts_v), np.mean(xpts_v)]]
)
ypts = np.array(
[[np.mean(ypts_v), np.mean(ypts_v), ypts_v[0, 0], ypts_v[0, 2]]]
)
mask = point_in_polygon(xpts, ypts, cell)
assert mask.sum() == 2 # only inner faces
debug_plot(grid, cell, xpts, ypts, mask)

0 comments on commit 0c939fd

Please sign in to comment.