Skip to content

Commit

Permalink
Added a function to get the Trimesh slice
Browse files Browse the repository at this point in the history
# Add a function to get the trimesh Path2D slice
# Updated the bitmap_slice example to demonstrate new functions use
  • Loading branch information
drlukeparry committed Feb 19, 2021
1 parent 66f48fd commit bb2ebb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/example_bitmap_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
resolution = 25.4 / dpi

# Return the Path2D object from Trimesh by setting second argument to False
slice = solidPart.getVectorSlice(14.0, False)
slice = solidPart.getTrimeshSlice(14.0)

# Rasterise and cast to a numpy array
# The origin is set based on the minium XY bounding box of the part. Depending on the platform the user may
Expand Down
29 changes: 23 additions & 6 deletions pyslm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,10 @@ def partType(self) -> str:

return self._partType

def getVectorSlice(self, z: float, returnCoordPaths: bool = True,
simplificationFactor:bool = None, simplificationPreserveTopology: Optional[bool] = True) -> Any:
def getTrimeshSlice(self, z: float) -> trimesh.path.Path2D:
"""
The vector slice is created by using `trimesh` to slice the mesh into a polygon
The vector slice is created by using `trimesh` to slice the mesh into a polygon - returns a shapely polygon.
:param returnCoordPaths: If True returns a list of closed paths representing the polygon, otherwise Shapely Polygons
:param z: The slice's z-position
:return: The vector slice at the given z level
Expand Down Expand Up @@ -391,13 +389,32 @@ def getVectorSlice(self, z: float, returnCoordPaths: bool = True,
# Repairs the polygon boundary using a merge function built into Trimesh
planarSection.fill_gaps(planarSection.scale / 100)

return planarSection

def getVectorSlice(self, z: float, returnCoordPaths: bool = True,
simplificationFactor:bool = None, simplificationPreserveTopology: Optional[bool] = True) -> Any:
"""
The vector slice is created by using `trimesh` to slice the mesh into a polygon
:param z: The slice's z-position
:param returnCoordPaths: If True returns a list of closed paths representing the polygon, otherwise Shapely Polygons
:param simplificationFactor: Simplification factor used for the boundary
:param simplificationPreserveTopology: Preserves the slice's topology when using simplification algorithm
:return: The vector slice at the given z level
"""
planarSection = self.getTrimeshSlice(z)

if not planarSection:
return []

# Obtain a closed list of shapely polygons
polygons = planarSection.polygons_full

if simplificationFactor:
simpPolys = []

for polygon in polygons:
for polygon in polygons:
simpPolys.append(polygon.simplify(simplificationFactor, preserve_topology=simplificationPreserveTopology))

polygons = simpPolys
Expand Down Expand Up @@ -440,7 +457,7 @@ def getBitmapSlice(self, z: float, resolution: float, origin: Optional = None)
:return: A bitmap image for the current slice at position
"""

vectorSlice = self.getVectorSlice(z, False)
vectorSlice = self.getShapelySlice(z)

bitmapOrigin = self.boundingBox[:2] if origin is None else origin

Expand Down

0 comments on commit bb2ebb9

Please sign in to comment.