trimesh.sample module

sample.py

Randomly sample surface and volume of meshes.

Functions

sample_surface(mesh, count)

Sample the surface of a mesh, returning the specified

sample_surface_even(mesh, count[, radius])

Sample the surface of a mesh, returning samples which are VERY approximately evenly spaced.

sample_surface_sphere(count)

Correctly pick random points on the surface of a unit sphere

volume_mesh(mesh, count)

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

volume_rectangular(extents, count[, transform])

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

trimesh.sample.sample_surface(mesh, count)

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

Returns

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

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

trimesh.sample.sample_surface_even(mesh, count, radius=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

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)

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)

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, transform=None)

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