trimesh.path package

Module contents

Classes

Path2D([entities, vertices, metadata, process])

Path3D([entities, vertices, metadata, process])

class trimesh.path.Path2D(entities=None, vertices=None, metadata=None, process=True)

Bases: trimesh.path.path.Path

Methods

apply_obb()

Transform the current path so that its OBB is axis aligned and OBB center is at the origin.

connected_paths(path_id[, include_self])

Given an index of self.paths find other paths which overlap with that path.

extrude(height, **kwargs)

Extrude the current 2D path into a 3D mesh.

medial_axis([resolution, clip])

Find the approximate medial axis based on a voronoi diagram of evenly spaced points on the boundary of the polygon.

plot_discrete([show, annotations])

Plot the closed curves of the path.

plot_entities([show, annotations, color])

Plot the entities of the path, with no notion of topology

rasterize(pitch, origin[, resolution, fill, …])

Rasterize a Path2D object into a boolean image (“mode 1”).

sample(count, **kwargs)

Use rejection sampling to generate random points inside a polygon.

show([annotations])

Plot the current Path2D object using matplotlib.

simplify(**kwargs)

Return a version of the current path with colinear segments merged, and circles entities replacing segmented circular paths.

simplify_spline([smooth, verbose])

Convert paths into b-splines.

split()

If the current Path2D consists of n ‘root’ curves,

to_3D([transform])

Convert 2D path to 3D path on the XY plane.

triangulate(**kwargs)

Create a region- aware triangulation of the 2D path.

Attributes

area

Return the area of the polygons interior.

body_count

centroid

Return the centroid of the path object.

enclosure

Networkx Graph object of polygon enclosure.

enclosure_directed

Networkx DiGraph of polygon enclosure

enclosure_shell

A dictionary of path indexes which are ‘shell’ paths, and values of ‘hole’ paths.

identifier

A unique identifier for the path.

identifier_md5

Return an MD5 of the identifier

length

The total discretized length of every entity.

obb

Get a transform that centers and aligns the OBB of the referenced vertices with the XY axis.

path_valid

returns: path_valid – which are valid polygons

polygons_closed

Cycles in the vertex graph, as shapely.geometry.Polygons.

polygons_full

A list of shapely.geometry.Polygon objects with interiors created by checking which closed polygons enclose which other polygons.

root

Which indexes of self.paths/self.polygons_closed are root curves.

apply_obb()

Transform the current path so that its OBB is axis aligned and OBB center is at the origin.

Returns

obb – Homogeneous transformation matrix

Return type

(3, 3) float

property area

Return the area of the polygons interior.

Returns

area

Return type

float, total area of polygons minus interiors

property body_count
property centroid

Return the centroid of the path object.

Returns

centroid – Approximate centroid of the path

Return type

(d,) float

connected_paths(path_id, include_self=False)

Given an index of self.paths find other paths which overlap with that path.

Parameters
  • path_id (int) – Index of self.paths

  • include_self (bool) – Should the result include path_id or not

Returns

path_ids – Indexes of self.paths that overlap input path_id

Return type

(n, ) int

property enclosure

Networkx Graph object of polygon enclosure.

property enclosure_directed

Networkx DiGraph of polygon enclosure

property enclosure_shell

A dictionary of path indexes which are ‘shell’ paths, and values of ‘hole’ paths.

Returns

corresponding

Return type

dict, {index of self.paths of shell : [indexes of holes]}

extrude(height, **kwargs)

Extrude the current 2D path into a 3D mesh.

Parameters
  • height (float, how far to extrude the profile) –

  • kwargs (passed directly to meshpy.triangle.build:) –

    triangle.build(mesh_info,

    verbose=False, refinement_func=None, attributes=False, volume_constraints=True, max_volume=None, allow_boundary_steiner=True, allow_volume_steiner=True, quality_meshing=True, generate_edges=None, generate_faces=False, min_angle=None)

Returns

mesh

Return type

trimesh object representing extruded polygon

property identifier

A unique identifier for the path.

Returns

identifier

Return type

(5,) float, unique identifier

property identifier_md5

Return an MD5 of the identifier

property length

The total discretized length of every entity.

Returns

length

Return type

float, summed length of every entity

medial_axis(resolution=None, clip=None)

Find the approximate medial axis based on a voronoi diagram of evenly spaced points on the boundary of the polygon.

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

  • clip (None, or (2,) float) – Min, max number of samples

Returns

medial – Contains only medial axis of Path

Return type

Path2D object

property obb

Get a transform that centers and aligns the OBB of the referenced vertices with the XY axis.

Returns

obb – Homogeneous transformation matrix

Return type

(3, 3) float

property path_valid

returns: path_valid – which are valid polygons :rtype: (n,) bool, indexes of self.paths self.polygons_closed

plot_discrete(show=False, annotations=True)

Plot the closed curves of the path.

plot_entities(show=False, annotations=True, color=None)

Plot the entities of the path, with no notion of topology

property polygons_closed

Cycles in the vertex graph, as shapely.geometry.Polygons. These are polygon objects for every closed circuit, with no notion of whether a polygon is a hole or an area. Every polygon in this list will have an exterior, but NO interiors.

Returns

polygons_closed

Return type

(n,) list of shapely.geometry.Polygon objects

property polygons_full

A list of shapely.geometry.Polygon objects with interiors created by checking which closed polygons enclose which other polygons.

Returns

full – Polygons containing interiors

Return type

(len(self.root),) shapely.geometry.Polygon

rasterize(pitch, origin, resolution=None, fill=True, width=None, **kwargs)

Rasterize a Path2D object into a boolean image (“mode 1”).

Parameters
  • pitch (float, length in model space of a pixel edge) –

  • origin ((2,) float, origin position in model space) –

  • resolution ((2,) int, resolution in pixel space) –

  • fill (bool, if True will return closed regions as filled) –

  • width (int, if not None will draw outline this wide (pixels)) –

Returns

raster

Return type

PIL.Image object, mode 1

property root

Which indexes of self.paths/self.polygons_closed are root curves. Also known as ‘shell’ or ‘exterior.

Returns

root

Return type

(n,) int, list of indexes

sample(count, **kwargs)

Use rejection sampling to generate random points inside a polygon.

Parameters
  • count (int) – Number of points to return If there are multiple bodies, there will be up to count * bodies points returned

  • factor (float) – How many points to test per loop IE, count * factor

  • max_iter (int,) – Maximum number of intersection loops to run, total points sampled is count * factor * max_iter

Returns

hit – Random points inside polygon

Return type

(n, 2) float

show(annotations=True)

Plot the current Path2D object using matplotlib.

simplify(**kwargs)

Return a version of the current path with colinear segments merged, and circles entities replacing segmented circular paths.

Returns

simplified

Return type

Path2D object

simplify_spline(smooth=0.0002, verbose=False)

Convert paths into b-splines.

Parameters
  • smooth (float) – How much the spline should smooth the curve

  • verbose (bool) – Print detailed log messages

Returns

simplified – Discrete curves replaced with splines

Return type

Path2D

split()

If the current Path2D consists of n ‘root’ curves, split them into a list of n Path2D objects

Returns

split – Each connected region and interiors

Return type

(n,) list of Path2D objects

to_3D(transform=None)

Convert 2D path to 3D path on the XY plane.

Parameters

transform ((4, 4) float) – If passed, will transform vertices. If not passed and ‘to_3D’ is in metadata that transform will be used.

Returns

path_3D

Return type

Path3D version of current path

triangulate(**kwargs)

Create a region- aware triangulation of the 2D path.

Parameters

**kwargs (dict) – Passed to trimesh.creation.triangulate_polygon

Returns

  • vertices ((n, 2) float) – 2D vertices of triangulation

  • faces ((n, 3) int) – Indexes of vertices for triangles

class trimesh.path.Path3D(entities=None, vertices=None, metadata=None, process=True)

Bases: trimesh.path.path.Path

Methods

plot_discrete([show])

Plot closed curves

plot_entities([show])

Plot discrete version of entities without regards for connectivity.

show(**kwargs)

Show the current Path3D object.

to_planar([to_2D, normal, check])

Check to see if current vectors are all coplanar.

plot_discrete(show=False)

Plot closed curves

Parameters

show (bool) – If False will not execute matplotlib.pyplot.show

plot_entities(show=False)

Plot discrete version of entities without regards for connectivity.

Parameters

show (bool) – If False will not execute matplotlib.pyplot.show

show(**kwargs)

Show the current Path3D object.

to_planar(to_2D=None, normal=None, check=True)

Check to see if current vectors are all coplanar.

If they are, return a Path2D and a transform which will transform the 2D representation back into 3 dimensions

Parameters
  • to_2D ((4,4) float) – Homogeneous transformation matrix to apply, If not passed a plane will be fitted to vertices.

  • normal ((3,) float, or None) – Approximate normal of direction of plane If to_2D is not specified sign will be applied to fit plane normal

  • check (bool) – If True: Raise a ValueError if points aren’t coplanar

Returns

  • planar (trimesh.path.Path2D) – Current path transformed onto plane

  • to_3D ((4,4) float) – Homeogenous transformation to move planar back into 3D space