Minor cleanup to triangulation

This commit is contained in:
Grant Sanderson
2021-02-02 16:43:24 -08:00
parent ddcd052036
commit 0c6149c9dd

View File

@ -343,7 +343,7 @@ def is_inside_triangle(p, a, b, c):
def norm_squared(v): def norm_squared(v):
return sum(v * v) return v[0] * v[0] + v[1] * v[1] + v[2] * v[2]
# TODO, fails for polygons drawn over themselves # TODO, fails for polygons drawn over themselves
@ -352,7 +352,7 @@ def earclip_triangulation(verts, rings):
Returns a list of indices giving a triangulation Returns a list of indices giving a triangulation
of a polygon, potentially with holes of a polygon, potentially with holes
- verts is an NxM numpy array of points with M > 2 - verts is a numpy array of points
- rings is a list of indices indicating where - rings is a list of indices indicating where
the ends of new paths are the ends of new paths are
@ -362,25 +362,27 @@ def earclip_triangulation(verts, rings):
loop_connections = dict() loop_connections = dict()
end0 = rings[0] end0 = rings[0]
for end1 in rings[1:]: for end1 in rings[1:]:
# Find the closet pair of points with the first # Find a close pair of points connecting the current
# from the current ring, and the second from the # ring to the next ring
# next ring
filtered_i, filtered_j = [
list(filter(
lambda i: i not in loop_connections,
indices
))
for indices in (range(0, end0), range(end0, end1))
]
i = filtered_i[np.argmin([ # Ignore indices already used for prior connections
# It's slightly faster to use L-infinity norm i_range = list(filter(
lambda i: i not in loop_connections,
range(0, end0)
))
j_range = list(range(end0, end1))
# Closet point on the first ring to the first point
# of the second ring
i = i_range[np.argmin([
norm_squared(verts[i] - verts[end0]) norm_squared(verts[i] - verts[end0])
for i in filtered_i for i in i_range
])] ])]
j = filtered_j[np.argmin([ # Closet point of the second ring to the aforementioned
# point of the first ring
j = j_range[np.argmin([
norm_squared(verts[i] - verts[j]) norm_squared(verts[i] - verts[j])
for j in filtered_j for j in j_range
])] ])]
# Connect the polygon at these points so that # Connect the polygon at these points so that