trimesh.voxel.base

voxel.py

Convert meshes to a simple voxel data structure and back again.

class trimesh.voxel.base.VoxelGrid(encoding, transform=None, metadata=None)

Bases: Geometry

Store 3D voxels.

__init__(encoding, transform=None, metadata=None)
apply_transform(matrix)
as_boxes(colors=None, **kwargs)

A rough Trimesh representation of the voxels with a box for each filled voxel.

Parameters:

colors (None, (3,) or (4,) float or uint8) – (X, Y, Z, 3) or (X, Y, Z, 4) float or uint8 Where matrix.shape == (X, Y, Z)

Returns:

mesh – Mesh with one box per filled cell.

Return type:

trimesh.Trimesh

property bounds
copy()
property element_volume
property encoding

Encoding object providing the occupancy grid.

See trimesh.voxel.encoding for implementations.

export(file_obj=None, file_type=None, **kwargs)

Export the current VoxelGrid.

Parameters:
  • file_obj (file-like or str) – File or file-name to export to.

  • file_type (None or str) – Only ‘binvox’ currently supported.

Returns:

export – Value of export.

Return type:

bytes

property extents
fill(method='holes', **kwargs)

Mutates self by filling in the encoding according to morphology.fill.

Parameters:
  • method (hashable) – Implementation key, one of trimesh.voxel.morphology.fill.fillers keys

  • **kwargs (dict) – Additional kwargs passed through to the keyed implementation.

Returns:

self – After replacing encoding with a filled version.

Return type:

VoxelGrid

property filled_count

int, number of occupied voxels in the grid.

hollow()

Mutates self by removing internal voxels leaving only surface elements.

Surviving elements are those in encoding that are adjacent to an empty voxel where adjacency is controlled by structure.

Returns:

self – After replacing encoding with a surface version.

Return type:

VoxelGrid

indices_to_points(indices)
property is_empty
is_filled(point)

Query points to see if the voxel cells they lie in are filled or not.

Parameters:

point ((n, 3) float) – Points in space

Returns:

is_filled – Is cell occupied or not for each point

Return type:

(n,) bool

property marching_cubes

A marching cubes Trimesh representation of the voxels.

No effort was made to clean or smooth the result in any way; it is merely the result of applying the scikit-image measure.marching_cubes function to self.encoding.dense.

Returns:

meshed – Representing the current voxel object as returned by marching cubes algorithm.

Return type:

trimesh.Trimesh

property matrix

Return a DENSE matrix of the current voxel encoding.

Returns:

dense – Numpy array of dense matrix Shortcut to voxel.encoding.dense

Return type:

(a, b, c) bool

property pitch

Uniform scaling factor representing the side length of each voxel.

Returns:

pitch – Pitch of the voxels.

Return type:

float

Raises:

RuntimeError – If self.transformation has rotation or shear components of has non-uniform scaling.

property points

The center of each filled cell as a list of points.

Returns:

points – Points in space.

Return type:

(self.filled, 3) float

points_to_indices(points)

Convert points to indices in the matrix array.

Parameters:

points ((n, 3) float, point in space)

Returns:

indices

Return type:

(n, 3) int array of indices into self.encoding

revoxelized(shape)

Create a new VoxelGrid without rotations, reflections or shearing.

Parameters:

shape ((3, int)) – The shape of the returned VoxelGrid.

Returns:

vox – Of the given shape with possibly non-uniform scale and translation transformation matrix.

Return type:

VoxelGrid

property scale

3-element float representing per-axis scale.

Raises a RuntimeError if self.transform has rotation or shear components.

property shape

3-tuple of ints denoting shape of occupancy grid.

show(*args, **kwargs)

Convert the current set of voxels into a trimesh for visualization and show that via its built- in preview method.

property sparse_indices

(n, 3) int array of sparse indices of occupied voxels.

strip()

Mutate self by stripping leading/trailing planes of zeros.

Return type:

self after mutation occurs in-place

property transform

4x4 homogeneous transformation matrix.

property translation

Location of voxel at [0, 0, 0].

property volume

What is the volume of the filled cells in the current voxel object.

Returns:

volume – Volume of filled cells.

Return type:

float