Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to Join Polygons #112

Merged
merged 2 commits into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions src/polygonizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'generatePolygons',
]

from collections import defaultdict
from enum import IntFlag
import itertools

Expand Down Expand Up @@ -494,8 +495,16 @@ def find(l, x):

assert checkPoly(outer_poly)

# Emit outer polygon
yield outer_poly
points_ = dict(
zip(
reversed(outer_poly),
range(len(outer_poly) - 1, -1, -1),
))
points = defaultdict(set)
for p in outer_poly:
points[p].add(0)

poly = [(outer_poly, points_)]

# Calculate bounding box
x_min, y_min, x_max, y_max = x, y, x, y
Expand Down Expand Up @@ -535,12 +544,57 @@ def find(l, x):

assert checkPoly(inner_poly)

# Emit inner polygon
yield inner_poly
j = len(poly)
poly.append((
inner_poly,
dict(
zip(
reversed(inner_poly),
range(len(inner_poly) - 1, -1, -1),
)),
))
for i in inner_poly:
points[i].add(j)

# Restore iterator value
x, y = p

# Try to join polygons
for i in range(len(poly) - 1, -1, -1):
inner_poly, points_ = poly[i]

for v in points.values():
if len(v) > 1 and i in v:
j = min(v)
break
else:
continue

outer_poly, points__ = poly[j]

for i_, p in enumerate(inner_poly):
j_ = points__.get(p)
if j_ is not None:
break
else:
raise RuntimeError(
f'Should not happened {inner_poly} {outer_poly}')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically guarantees it will happen at least once


outer_poly[j_:j_] = [*inner_poly[i_:], *inner_poly[:i_]]
points__.update(
zip(
reversed(outer_poly),
range(len(outer_poly) - 1, -1, -1),
))

del poly[i]
for v in points.values():
v.discard(i)

# Emit polygons
for i, _ in poly:
yield i


def checkPoly(poly):
for i in range(-1, len(poly) - 1):
Expand Down