Skip to content

Commit

Permalink
General fixes and documentation updates in pyslm.hatching
Browse files Browse the repository at this point in the history
  • Loading branch information
drlukeparry committed Dec 10, 2023
1 parent f63169f commit e6610ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
9 changes: 2 additions & 7 deletions pyslm/hatching/hatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def getExposurePoints(layer: Layer, models: List[Model], includePowerDeposited:

exposurePoints = []


for layerGeom in layer.geometry:

# Get the model given the mid
Expand Down Expand Up @@ -161,7 +160,6 @@ class BaseHatcher(abc.ABC):
:meth:`scaleToClipper` and :meth:`scaleFromClipper`.
"""


PYCLIPPER_SCALEFACTOR = 1e5
"""
The scaling factor used for polygon clipping and offsetting in `PyClipr <https://pypi.org/project/pyclipr/>`_
Expand Down Expand Up @@ -199,7 +197,6 @@ def error(cls) -> float:
"""
return 1. / cls.PYCLIPPER_SCALEFACTOR


@staticmethod
def offsetPolygons(polygons, offset: float):
"""
Expand Down Expand Up @@ -227,9 +224,6 @@ def offsetBoundary(paths, offset: float):

pc = pyclipr.ClipperOffset()
pc.scaleFactor = int(BaseHatcher.PYCLIPPER_SCALEFACTOR)

#for path in paths:
# pc.addPath(path, pyclipr.JoinType.Round, pyclipr.EndType.Polygon)
pc.addPaths(paths, pyclipr.JoinType.Round)

# Perform the offseting operation
Expand Down Expand Up @@ -264,7 +258,7 @@ def polygonBoundingBox(obj: Any) -> np.ndarray:
return bbox

@staticmethod
def boundaryBoundingBox(boundaries):
def boundaryBoundingBox(boundaries) -> np.array:
"""
Returns the bounding box of a list of boundaries, typically generated by the tree representation in Pyclipr.
Expand Down Expand Up @@ -305,6 +299,7 @@ def clipLines(paths, lines):
pc2.addPaths(lines.reshape(-1,2,3), pyclipr.Subject, True)
pc2.addPaths(paths, pyclipr.Clip)
out = pc2.execute(pyclipr.Intersection, pyclipr.FillRule.NonZero, returnOpenPaths=True, returnZ=True)

lineXY = np.array(out[1])
lineZ = np.array(out[3])

Expand Down
31 changes: 26 additions & 5 deletions pyslm/hatching/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import trimesh.path.polygons
import shapely.geometry

from shapely.geometry import Polygon, LinearRing
from shapely.geometry import Polygon
from skimage.measure import approximate_polygon


Expand Down Expand Up @@ -86,8 +86,14 @@ def from3DHatchArray(hatchVectors: np.ndarray) -> np.ndarray:

return hatchVectors.reshape(-1, 2)

def poly2Paths(polygons: Union[shapely.geometry.Polygon, shapely.geometry.MultiPolygon]):

def poly2Paths(polygons: Union[shapely.geometry.Polygon, shapely.geometry.MultiPolygon]) -> List[np.array]:
"""
Converts a Shapely Polygon or MultiPolygon to a list of paths
:param polygons: A polygon to convert to individual paths
:return: A list of paths (interior and exterior) for each polygon
"""
paths = []

if isinstance(polygons, shapely.geometry.MultiPolygon):
Expand All @@ -104,16 +110,31 @@ def poly2Paths(polygons: Union[shapely.geometry.Polygon, shapely.geometry.MultiP
return paths


def paths2clipper(paths: Any):
def paths2clipper(paths: Any) -> List[np.array]:
"""
Converts a list of paths to clipper format input
:param paths: Paths for conversion
:return: The list of coordinates for each path
"""
return [np.hstack([path, np.arange(len(path)).reshape(-1, 1)]) for path in paths]

def clipper2Paths(paths, scaleFactor: float, close:bool = False):

def clipper2Paths(paths, scaleFactor: float, close: bool = False) -> List[np.array]:
"""
Returns scaled closed paths from clipper paths
:param paths: The input clipper paths
:param scaleFactor: Scale factor to apply
:param close: If True, the paths are closed by appending the first point to the end of the path
:return:
"""
out = [np.array(path)[:,:2]/scaleFactor for path in paths]

if close:
outPaths = []
for path in out:
outPaths.append(np.vstack([out, out[0,:]]))
outPaths.append(np.vstack([path, path[0,:]]))
out = outPaths

return out
Expand Down

0 comments on commit e6610ea

Please sign in to comment.