# trimesh.sample

## sample.py

Randomly sample surface and volume of meshes.

trimesh.sample.sample_surface(mesh, count, face_weight=None, sample_color=False)

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.

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

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

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