trimesh.path.polygons module

Functions

edges_to_polygons(edges, vertices)

Given an edge list of indices and associated vertices representing lines, generate a list of polygons.

enclosure_tree(polygons)

Given a list of shapely polygons with only exteriors, find which curves represent the exterior shell or root curve and which represent holes which penetrate the exterior.

medial_axis(polygon[, resolution, clip])

Given a shapely polygon, find the approximate medial axis using a voronoi diagram of evenly spaced points on the boundary of the polygon.

paths_to_polygons(paths[, scale])

Given a sequence of connected points turn them into valid shapely Polygon objects.

plot_polygon(polygon[, show])

Plot a shapely polygon using matplotlib.

polygon_hash(polygon)

Return a vector containing values representitive of a particular polygon.

polygon_obb(polygon)

Find the oriented bounding box of a Shapely polygon.

polygon_scale(polygon)

For a Polygon object return the diagonal length of the AABB.

polygons_obb(polygons)

Find the OBBs for a list of shapely.geometry.Polygons

random_polygon([segments, radius])

Generate a random polygon with a maximum number of sides and approximate radius.

repair_invalid(polygon[, scale, rtol])

Given a shapely.geometry.Polygon, attempt to return a valid version of the polygon through buffering tricks.

resample_boundaries(polygon, resolution[, clip])

Return a version of a polygon with boundaries resampled to a specified resolution.

sample(polygon, count[, factor, max_iter])

Use rejection sampling to generate random points inside a polygon.

stack_boundaries(boundaries)

Stack the boundaries of a polygon into a single (n, 2) list of vertices.

transform_polygon(polygon, matrix)

Transform a polygon by a a 2D homogeneous transform.

trimesh.path.polygons.edges_to_polygons(edges, vertices)

Given an edge list of indices and associated vertices representing lines, generate a list of polygons.

Parameters
  • edges ((n, 2) int) – Indexes of vertices which represent lines

  • vertices ((m, 2) float) – Vertices in 2D space

Returns

polygons – Polygon objects with interiors

Return type

(p,) shapely.geometry.Polygon

trimesh.path.polygons.enclosure_tree(polygons)

Given a list of shapely polygons with only exteriors, find which curves represent the exterior shell or root curve and which represent holes which penetrate the exterior.

This is done with an R-tree for rough overlap detection, and then exact polygon queries for a final result.

Parameters

polygons ((n,) shapely.geometry.Polygon) – Polygons which only have exteriors and may overlap

Returns

  • roots ((m,) int) – Index of polygons which are root

  • contains (networkx.DiGraph) – Edges indicate a polygon is contained by another polygon

trimesh.path.polygons.medial_axis(polygon, resolution=None, clip=None)

Given a shapely polygon, find the approximate medial axis using a voronoi diagram of evenly spaced points on the boundary of the polygon.

Parameters
  • polygon (shapely.geometry.Polygon) – The source geometry

  • resolution (float) – Distance between each sample on the polygon boundary

  • clip (None, or (2,) int) – Clip sample count to min of clip[0] and max of clip[1]

Returns

  • edges ((n, 2) int) – Vertex indices representing line segments on the polygon’s medial axis

  • vertices ((m, 2) float) – Vertex positions in space

trimesh.path.polygons.paths_to_polygons(paths, scale=None)

Given a sequence of connected points turn them into valid shapely Polygon objects.

Parameters
  • paths ((n,) sequence) – Of (m, 2) float closed paths

  • scale (float) – Approximate scale of drawing for precision

Returns

polys – Filled with Polygon or None

Return type

(p,) list

trimesh.path.polygons.plot_polygon(polygon, show=True, **kwargs)

Plot a shapely polygon using matplotlib.

Parameters
  • polygon (shapely.geometry.Polygon) – Polygon to be plotted

  • show (bool) – If True will display immediately

  • **kwargs – Passed to plt.plot

trimesh.path.polygons.polygon_hash(polygon)

Return a vector containing values representitive of a particular polygon.

Parameters

polygon (shapely.geometry.Polygon) – Input geometry

Returns

hashed – Representitive values representing input polygon

Return type

(6), float

trimesh.path.polygons.polygon_obb(polygon)

Find the oriented bounding box of a Shapely polygon.

The OBB is always aligned with an edge of the convex hull of the polygon.

Parameters

polygons (shapely.geometry.Polygon) – Input geometry

Returns

  • transform ((3, 3) float) – Transformation matrix which will move input polygon from its original position to the first quadrant where the AABB is the OBB

  • extents ((2,) float) – Extents of transformed polygon

trimesh.path.polygons.polygon_scale(polygon)

For a Polygon object return the diagonal length of the AABB.

Parameters

polygon (shapely.geometry.Polygon) – Source geometry

Returns

scale – Length of AABB diagonal

Return type

float

trimesh.path.polygons.polygons_obb(polygons)

Find the OBBs for a list of shapely.geometry.Polygons

trimesh.path.polygons.random_polygon(segments=8, radius=1.0)

Generate a random polygon with a maximum number of sides and approximate radius.

Parameters
  • segments (int) – The maximum number of sides the random polygon will have

  • radius (float) – The approximate radius of the polygon desired

Returns

polygon – Geometry object with random exterior and no interiors.

Return type

shapely.geometry.Polygon

trimesh.path.polygons.repair_invalid(polygon, scale=None, rtol=0.5)

Given a shapely.geometry.Polygon, attempt to return a valid version of the polygon through buffering tricks.

Parameters
  • polygon (shapely.geometry.Polygon) – Source geometry

  • rtol (float) – How close does a perimeter have to be

  • scale (float or None) – For numerical precision reference

Returns

repaired – Repaired polygon

Return type

shapely.geometry.Polygon

Raises

ValueError – If polygon can’t be repaired

trimesh.path.polygons.resample_boundaries(polygon, resolution, clip=None)

Return a version of a polygon with boundaries resampled to a specified resolution.

Parameters
  • polygon (shapely.geometry.Polygon) – Source geometry

  • resolution (float) – Desired distance between points on boundary

  • clip ((2,) int) – Upper and lower bounds to clip number of samples to avoid exploding count

Returns

kwargs – Keyword args for a Polygon constructor Polygon(**kwargs)

Return type

dict

trimesh.path.polygons.sample(polygon, count, factor=1.5, max_iter=10)

Use rejection sampling to generate random points inside a polygon.

Parameters
  • polygon (shapely.geometry.Polygon) – Polygon that will contain points

  • count (int) – Number of points to return

  • factor (float) – How many points to test per loop

  • max_iter (int) – Maximum number of intersection checks is: > count * factor * max_iter

Returns

hit – Random points inside polygon where n <= count

Return type

(n, 2) float

trimesh.path.polygons.stack_boundaries(boundaries)

Stack the boundaries of a polygon into a single (n, 2) list of vertices.

Parameters

boundaries (dict) – With keys ‘shell’, ‘holes’

Returns

stacked – Stacked vertices

Return type

(n, 2) float

trimesh.path.polygons.transform_polygon(polygon, matrix)

Transform a polygon by a a 2D homogeneous transform.

Parameters
  • polygon (shapely.geometry.Polygon) – 2D polygon to be transformed.

  • matrix ((3, 3) float) – 2D homogeneous transformation.

Returns

result – Polygon transformed by matrix.

Return type

shapely.geometry.Polygon