BaseHatcher

class pyslm.hatching.BaseHatcher

Bases: abc.ABC

The BaseHatcher class provides common methods used for generating the ‘contour’ and infill ‘hatch’ scan vectors for a geometry slice typically a multi-polygon region.

The class provides an interface tp generate a variety of hatching patterns used. The developer should re-implement a subclass and re-define the abstract method, BaseHatcher.hatch(), which will be called.

The user typically specifies a boundary, which may be offset the boundary of region using offsetBoundary(). This is typically performed before generating the infill. Following offsetting, the a series of hatch lines are generated using generateHatching() to fill the entire boundary region using polygonBoundingBox(). To obtain the final clipped infill, the hatches are clipped using clipLines() which are clipped in the same sequential order they are generated using a technique explained further in the class method. The generated scan paths should be stored into collections of LayerGeometry accordingly.

For all polygon manipulation operations used for offsetting and clipping, internally this calls provides automatic conversion to the integer coordinate system used by ClipperLib by internally calling scaleToClipper() and scaleFromClipper().

Attributes Summary

PYCLIPPER_SCALEFACTOR

The scaling factor used for polygon clipping and offsetting in PyClipper for the decimal component of each polygon coordinate.

Methods Summary

boundaryBoundingBox(boundaries)

Returns the bounding box of a list of boundaries, typically generated by the tree representation in PyClipper.

clipContourLines(paths, contourPaths)

This function clips a series of (contour paths) across a closed polygon using Pyclipper.

clipLines(paths, lines)

This function clips a series of lines (hatches) across a closed polygon using Pyclipper.

clipLines2(lines)

clipperToHatchArray(coords)

A helper method which converts the raw polygon edge lists returned by PyClipper into a numpy array.

error()

Returns the accuracy of the polygon clipping depending on the chosen scale factor PYCLIPPER_SCALEFACTOR.

generateHatching(paths, hatchSpacing[, …])

Generates un-clipped hatches which is guaranteed to cover the entire polygon region base on the maximum extent of the polygon bounding box

hatch(boundaryFeature)

The hatch method should be re-implemented by a child class to generate a Layer containing the scan vectors used for manufacturing the layer.

offsetBoundary(paths, offset)

Offsets a single path for a single polygon.

offsetPolygons(polygons, offset)

Offsets a set of boundaries across a collection of polygons by the offset parameter.

polygonBoundingBox(obj)

Returns the bounding box of the polygon - typically this represents a single shape with an exterior and a list of boundaries within an array

scaleFromClipper(feature)

Transforms geometry created from pyclipper upscaled integer coordinates back to the original floating-point coordinate system.

scaleToClipper(feature)

Transforms geometry created to pyclipper by upscaling into the integer coordinates from the original floating point coordinate system.

Attributes Documentation

PYCLIPPER_SCALEFACTOR = 100000.0

The scaling factor used for polygon clipping and offsetting in PyClipper for the decimal component of each polygon coordinate. This should be set to inverse of the required decimal tolerance i.e. 0.01 requires a minimum scale factor of 100. This scaling factor is used in scaleToClipper() and scaleFromClipper().

Note:

From experience, 1e4, mostly works, however, there are some artefacts generated during clipping hatch vectors. Therefore at a small peformance cost 1e5 is recommended.

Methods Documentation

static boundaryBoundingBox(boundaries)

Returns the bounding box of a list of boundaries, typically generated by the tree representation in PyClipper.

Parameters:

boundaries – A list of polygon

Returns:

A (1x6) numpy array

static clipContourLines(paths, contourPaths)

This function clips a series of (contour paths) across a closed polygon using Pyclipper.

Note

The order is guaranteed from the list of lines used, so these do not require sorting. However, the position may require additional sorting to cater for the user’s requirements.

Parameters:
  • paths – The set of boundary paths for trimming the lines

  • contourPaths (List[ndarray]) – The un-trimmed complex open paths to be clipped

Returns:

A list of trimmed lines (open paths)

static clipLines(paths, lines)

This function clips a series of lines (hatches) across a closed polygon using Pyclipper.

Note

The order is guaranteed from the list of lines used, so these do not require sorting usually. However, the position may require additional sorting to cater for the user’s requirements.

Parameters:
  • paths – The set of boundary paths for trimming the lines

  • lines – The un-trimmed lines to clip from the boundary

Returns:

A list of trimmed lines (open paths)

clipLines2(lines)
static clipperToHatchArray(coords)

A helper method which converts the raw polygon edge lists returned by PyClipper into a numpy array.

Parameters:

coords (ndarray) – The list of hatches generated from pyclipper

Return type:

ndarray

Returns:

The hatch coordinates transfromed into a (n x 2 x 3) numpy array.

classmethod error()

Returns the accuracy of the polygon clipping depending on the chosen scale factor PYCLIPPER_SCALEFACTOR.

Return type:

float

generateHatching(paths, hatchSpacing, hatchAngle=90.0)

Generates un-clipped hatches which is guaranteed to cover the entire polygon region base on the maximum extent of the polygon bounding box

Parameters:
  • paths – Boundary paths to generate hatches to cover

  • hatchSpacing (float) – Hatch Spacing to use

  • hatchAngle (Optional[float]) – Hatch angle (degrees) to rotate the scan vectors

Return type:

ndarray

Returns:

Returns the list of un-clipped scan vectors

abstract hatch(boundaryFeature)

The hatch method should be re-implemented by a child class to generate a Layer containing the scan vectors used for manufacturing the layer.

Parameters:

boundaryFeature – The collection of boundaries of closed polygons within a layer.

Raises:

NotImplementedError

Return type:

Optional[Layer]

static offsetBoundary(paths, offset)

Offsets a single path for a single polygon.

Parameters:
  • paths – Closed polygon path list for offsetting

  • offset (float) – The offset applied to the poylgon

Returns:

A list of boundaries offset from the subject

static offsetPolygons(polygons, offset)

Offsets a set of boundaries across a collection of polygons by the offset parameter.

Note

Note that if any polygons are expanded overlap with adjacent polygons, the offsetting will NOT unify into a single shape.

Parameters:
  • polygons – A list of closed polygons which are individually offset from each other.

  • offset (float) – The offset distance applied to the polygon

Returns:

A list of boundaries offset from the subject

static polygonBoundingBox(obj)

Returns the bounding box of the polygon - typically this represents a single shape with an exterior and a list of boundaries within an array

Parameters:

obj (Any) – Geometry object

Return type:

ndarray

Returns:

A (1x6) numpy array representing the bounding box of a polygon

static scaleFromClipper(feature)

Transforms geometry created from pyclipper upscaled integer coordinates back to the original floating-point coordinate system.

Parameters:

feature (Any) – The geometry to scale to pyclipper

Returns:

The scaled geometry

static scaleToClipper(feature)

Transforms geometry created to pyclipper by upscaling into the integer coordinates from the original floating point coordinate system.

Parameters:

feature (Any) – The geometry to scale to pyclipper

Returns:

The scaled geometry