snarled/snarl/clipper.py

51 lines
1.4 KiB
Python
Raw Normal View History

2022-03-31 00:01:45 -07:00
"""
Wrappers to simplify some pyclipper functions
"""
2022-03-27 23:43:52 -07:00
from typing import Sequence, Optional, List
from numpy.typing import ArrayLike
from pyclipper import (
Pyclipper, PT_CLIP, PT_SUBJECT, CT_UNION, CT_INTERSECTION, PFT_NONZERO, PFT_EVENODD,
PyPolyNode,
)
from .types import contour_t
def union_nonzero(shapes: Sequence[ArrayLike]) -> Optional[PyPolyNode]:
if not shapes:
return None
pc = Pyclipper()
pc.AddPaths(shapes, PT_CLIP, closed=True)
result = pc.Execute2(CT_UNION, PFT_NONZERO, PFT_NONZERO)
return result
def union_evenodd(shapes: Sequence[ArrayLike]) -> List[contour_t]:
if not shapes:
return []
pc = Pyclipper()
pc.AddPaths(shapes, PT_CLIP, closed=True)
return pc.Execute(CT_UNION, PFT_EVENODD, PFT_EVENODD)
def intersection_evenodd(
subject_shapes: Sequence[ArrayLike],
clip_shapes: Sequence[ArrayLike],
) -> List[contour_t]:
if not subject_shapes or not clip_shapes:
return []
pc = Pyclipper()
pc.AddPaths(subject_shapes, PT_SUBJECT, closed=True)
pc.AddPaths(clip_shapes, PT_CLIP, closed=True)
return pc.Execute(CT_INTERSECTION, PFT_EVENODD, PFT_EVENODD)
def hier2oriented(polys: Sequence[PyPolyNode]) -> List[ArrayLike]:
contours = []
for poly in polys:
contours.append(poly.Contour)
contours += [hole.Contour for hole in poly.Childs]
return contours