Voxelizing a Triangle Mesh

Point-Cloud-Utils supports rasterizing triangle meshes to voxels as well as generating cube meshes from those voxels.

import numpy as np
import point_cloud_utils as pcu

vox_size = 1.0 / 256.0  # Size of each voxel
vox_origin = [0, 0, 0]  # location of the bottom-left-back corner of the [0, 0, 0] voxel
v, f = pcu.load_mesh_vf("truck.ply")

# Voxelize the input mesh, vox_ijk is an array of integer voxel coordinates
vox_ijk = pcu.voxelize_triangle_mesh(v, f, vox_size, vox_origin)

# Generate a cube mesh of voxels with a spacing of 0.02 voxels between each cube
cube_v, cube_f = pcu.voxel_grid_geometry(vox_ijk, vox_size, vox_origin, gap_fraction=0.02)

Mesh to voxelize
Vizualized voxels with gaps
Left: Input mesh to voxelize. Right: Visualization of voxelized mesh.