Skip to content

Instantly share code, notes, and snippets.

@mlubej
Created April 20, 2020 20:55
Show Gist options
  • Save mlubej/1755e0620be64086597a0903b146f16c to your computer and use it in GitHub Desktop.
Save mlubej/1755e0620be64086597a0903b146f16c to your computer and use it in GitHub Desktop.
def get_unique_orientations(poly):
"""
Out of all possible rotations and flips, take into account the polygon symmetry and
obtain the unique polygon orientations.
:param poly: input polygon
:return: array of unique settings of (rotation, flip)
"""
cx, cy = poly.centroid.x, poly.centroid.y
pts = np.array(poly.checkers)[:, :-1]
checkers = np.array(poly.checkers)[:, -1]
pts = pts[checkers == 1]
poly = poly.difference(MultiPoint(pts + 0.5).buffer(0.2))
rots = [0, 90, 180, 270]
flips = [1, -1]
iterables = [rots, flips]
settings = np.array(list(itertools.product(*iterables)))
unique_polys = []
unique_settings = []
for rot, flip in settings:
p = poly
p = rotate(p, rot, origin=[cx, cy])
p = scale(p, flip, origin=[cx, cy, 0])
if np.any([p.difference(u).area < 1e-5 for u in unique_polys]):
continue
unique_polys.append(p)
unique_settings.append([rot, flip])
return unique_settings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment