trimesh.path.segments module

segments.py

Deal with (n, 2, 3) line segments.

trimesh.path.segments.clean(segments: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], digits: int = 10) ndarray[Any, dtype[float64]]

Clean up line segments by unioning the ranges of colinear segments.

Parameters:
  • segments ((n, 2, 2) or (n, 2, 3)) – Line segments in space.

  • digits – How many digits to consider.

Returns:

cleaned – Where m <= n

Return type:

(m, 2, 2) or (m, 2, 3)

trimesh.path.segments.colinear_pairs(segments, radius=0.01, angle=0.01, length=None)

Find pairs of segments which are colinear.

Parameters:
  • segments ((n, 2, (2, 3)) float) – Two or three dimensional line segments

  • radius (float) – Maximum radius line origins can differ and be considered colinear

  • angle (float) – Maximum angle in radians segments can differ and still be considered colinear

  • length (None or float) – If specified, will additionally require that pairs have a vertex within this distance.

Returns:

pairs – Indexes of segments which are colinear

Return type:

(m, 2) int

trimesh.path.segments.extrude(segments, height, double_sided=False)

Extrude 2D line segments into 3D triangles.

Parameters:
  • segments ((n, 2, 2) float) – 2D line segments

  • height (float) – Distance to extrude along Z

  • double_sided (bool) – If true, return 4 triangles per segment

Returns:

  • vertices ((n, 3) float) – Vertices in space

  • faces ((n, 3) int) – Indices of vertices forming triangles

trimesh.path.segments.length(segments, summed=True)

Extrude 2D line segments into 3D triangles.

Parameters:
  • segments ((n, 2, 2) float) – 2D line segments

  • height (float) – Distance to extrude along Z

  • double_sided (bool) – If true, return 4 triangles per segment

Returns:

  • vertices ((n, 3) float) – Vertices in space

  • faces ((n, 3) int) – Indices of vertices forming triangles

trimesh.path.segments.parameters_to_segments(origins: ndarray[Any, dtype[float64]], vectors: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], parameters: ndarray[Any, dtype[float64]])

Convert a parametric line segment representation to a two point line segment representation

Parameters:
  • origins ((n, 3) float) – Line origin point

  • vectors ((n, 3) float) – Unit line directions

  • parameters ((n, 2) float) – Start and end distance pairs for each line

Returns:

segments – Line segments defined by start and end points

Return type:

(n, 2, 3) float

trimesh.path.segments.resample(segments, maxlen, return_index=False, return_count=False)

Resample line segments until no segment is longer than maxlen.

Parameters:
  • segments ((n, 2, 2|3) float) – 2D line segments

  • maxlen (float) – The maximum length of a line segment

  • return_index (bool) – Return the index of the source segment

  • return_count (bool) – Return how many segments each original was split into

Returns:

  • resampled ((m, 2, 2|3) float) – Line segments where no segment is longer than maxlen

  • index ((m,) int) – [OPTIONAL] The index of segments resampled came from

  • count ((n,) int) – [OPTIONAL] The count of the original segments

trimesh.path.segments.segments_to_parameters(segments: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])

For 3D line segments defined by two points, turn them in to an origin defined as the closest point along the line to the zero origin as well as a direction vector and start and end parameter.

Parameters:

segments ((n, 2, 3) float) – Line segments defined by start and end points

Returns:

  • origins ((n, 3) float) – Point on line closest to [0, 0, 0]

  • vectors ((n, 3) float) – Unit line directions

  • parameters ((n, 2) float) – Start and end distance pairs for each line

trimesh.path.segments.split(segments, points, atol=1e-05)

Find any points that lie on a segment (not an endpoint) and then split that segment into two segments.

We are basically going to find the distance between point and both segment vertex, and see if it is with tolerance of the segment length.

Parameters:
  • segments ((n, 2, (2, 3) float) – Line segments in space

  • points ((n, (2, 3)) float) – Points in space

  • atol (float) – Absolute tolerance for distances

Returns:

split – Line segments in space, split at vertices

Return type:

(n, 2, (3 | 3) float

trimesh.path.segments.to_svg(segments, digits=4, matrix=None, merge=True)

Convert (n, 2, 2) line segments to an SVG path string.

Parameters:
  • segments ((n, 2, 2) float) – Line segments to convert

  • digits (int) – Number of digits to include in SVG string

  • matrix (None or (3, 3) float) – Homogeneous 2D transformation to apply before export

Returns:

path – SVG path string with one line per segment IE: ‘M 0.1 0.2 L 10 12’

Return type:

str

trimesh.path.segments.unique(segments, digits=5)

Find unique non-zero line segments.

Parameters:
  • segments ((n, 2, (2|3)) float) – Line segments in space

  • digits (int) – How many digits to consider when merging vertices

Returns:

unique – Segments with duplicates merged

Return type:

(m, 2, (2|3)) float