trimesh.sample

sample.py

Randomly sample surface and volume of meshes.

trimesh.sample.sample_surface(mesh, count: int | integer | unsignedinteger, face_weight: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, sample_color=False, seed=None)

Sample the surface of a mesh, returning the specified number of points

For individual triangle sampling uses this method: http://mathworld.wolfram.com/TrianglePointPicking.html

Parameters:
  • mesh (trimesh.Trimesh) – Geometry to sample the surface of

  • count (int) – Number of points to return

  • face_weight (None or len(mesh.faces) float) – Weight faces by a factor other than face area. If None will be the same as face_weight=mesh.area

  • sample_color (bool) – Option to calculate the color of the sampled points. Default is False.

  • seed (None or int) – If passed as an integer will provide deterministic results otherwise pulls the seed from operating system entropy.

Returns:

  • samples ((count, 3) float) – Points in space on the surface of mesh

  • face_index ((count,) int) – Indices of faces for each sampled point

  • colors ((count, 4) float) – Colors of each sampled point Returns only when the sample_color is True

trimesh.sample.sample_surface_even(mesh, count: int | integer | unsignedinteger, radius: float | floating | int | integer | unsignedinteger | None = None, seed=None)

Sample the surface of a mesh, returning samples which are VERY approximately evenly spaced. This is accomplished by sampling and then rejecting pairs that are too close together.

Note that since it is using rejection sampling it may return fewer points than requested (i.e. n < count). If this is the case a log.warning will be emitted.

Parameters:
  • mesh (trimesh.Trimesh) – Geometry to sample the surface of

  • count (int) – Number of points to return

  • radius (None or float) – Removes samples below this radius

  • seed (None or int) – Provides deterministic values

Returns:

  • samples ((n, 3) float) – Points in space on the surface of mesh

  • face_index ((n,) int) – Indices of faces for each sampled point

trimesh.sample.sample_surface_sphere(count: int) ndarray[Any, dtype[float64]]

Correctly pick random points on the surface of a unit sphere

Uses this method: http://mathworld.wolfram.com/SpherePointPicking.html

Parameters:

count (int) – Number of points to return

Returns:

points – Random points on the surface of a unit sphere

Return type:

(count, 3) float

trimesh.sample.volume_mesh(mesh, count: int | integer | unsignedinteger) ndarray[Any, dtype[float64]]

Use rejection sampling to produce points randomly distributed in the volume of a mesh.

Parameters:
  • mesh (trimesh.Trimesh) – Geometry to sample

  • count (int) – Number of points to return

Returns:

samples – Points in the volume of the mesh where n <= count

Return type:

(n, 3) float

trimesh.sample.volume_rectangular(extents, count: int | integer | unsignedinteger, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None) ndarray[Any, dtype[float64]]

Return random samples inside a rectangular volume, useful for sampling inside oriented bounding boxes.

Parameters:
  • extents ((3,) float) – Side lengths of rectangular solid

  • count (int) – Number of points to return

  • transform ((4, 4) float) – Homogeneous transformation matrix

Returns:

samples – Points in requested volume

Return type:

(count, 3) float