trimesh.path.polygons module¶
- trimesh.path.polygons.edges_to_polygons(edges: ndarray[tuple[int, ...], dtype[int64]], vertices: ndarray[tuple[int, ...], dtype[float64]])¶
Given an edge list of indices and associated vertices representing lines, generate a list of polygons.
- Parameters:
edges ((n, 2)) – Indexes of vertices which represent lines
vertices ((m, 2)) – 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.identifier(polygon: Polygon) ndarray[tuple[int, ...], dtype[float64]] ¶
Return a vector containing values representative of a particular polygon.
- Parameters:
polygon (shapely.geometry.Polygon) – Input geometry
- Returns:
identifier – Values which should be unique for this polygon.
- Return type:
(8,) float
- trimesh.path.polygons.medial_axis(polygon: Polygon, resolution: float | floating | int | integer | unsignedinteger | None = 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=None, show=True, axes=None, **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_bounds(polygon, matrix=None)¶
Get the transformed axis aligned bounding box of a shapely Polygon object.
- Parameters:
polygon (shapely.geometry.Polygon) – Polygon pre-transform
matrix ((3, 3) float or None.) – Homogeneous transform moving polygon in space
- Returns:
bounds – Axis aligned bounding box of transformed polygon.
- Return type:
(2, 2) float
- trimesh.path.polygons.polygon_obb(polygon: Polygon | ndarray[tuple[int, ...], dtype[_ScalarType_co]])¶
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: Iterable[Polygon])¶
Find the OBBs for a list of shapely.geometry.Polygons
- trimesh.path.polygons.projected(mesh, normal, origin=None, ignore_sign=True, rpad=1e-05, apad=None, tol_dot=1e-10, precise: bool = False)¶
Project a mesh onto a plane and then extract the polygon that outlines the mesh projection on that plane.
Note that this will ignore back-faces, which is only relevant if the source mesh isn’t watertight.
Also padding: this generates a result by unioning the polygons of multiple connected regions, which requires the polygons be padded by a distance so that a polygon union produces a single coherent result. This distance is calculated as: apad + (rpad * scale)
- Parameters:
mesh (trimesh.Trimesh) – Source geometry
check (bool) – If True make sure is flat
normal ((3,) float) – Normal to extract flat pattern along
origin (None or (3,) float) – Origin of plane to project mesh onto
ignore_sign (bool) – Allow a projection from the normal vector in either direction: this provides a substantial speedup on watertight meshes where the direction is irrelevant but if you have a triangle soup and want to discard backfaces you should set this to False.
rpad (float) – Proportion to pad polygons by before unioning and then de-padding result by to avoid zero-width gaps.
apad (float) – Absolute padding to pad polygons by before unioning and then de-padding result by to avoid zero-width gaps.
tol_dot (float) – Tolerance for discarding on-edge triangles.
max_regions (int) – Raise an exception if the mesh has more than this number of disconnected regions to fail quickly before unioning.
- Returns:
projected – Outline of source mesh
- Return type:
shapely.geometry.Polygon or None
- Raises:
ValueError – If max_regions is exceeded
- 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: Polygon, resolution: float, clip=None)¶
Return a version of a polygon with boundaries re-sampled 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.second_moments(polygon: Polygon, return_centered=False)¶
Calculate the second moments of area of a polygon from the boundary.
- Parameters:
polygon (shapely.geometry.Polygon) – Closed polygon.
return_centered (bool) – Get second moments for a frame with origin at the centroid and perform a principal axis transformation.
- Returns:
moments ((3,) float) – The values of [Ixx, Iyy, Ixy]
principal_moments ((2,) float) – Principal second moments of inertia: [Imax, Imin] Only returned if centered.
alpha (float) – Angle by which the polygon needs to be rotated, so the principal axis align with the X and Y axis. Only returned if centered.
transform ((3, 3) float) – Transformation matrix which rotates the polygon by alpha. Only returned if centered.
- 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