I'm trying to work with voxels. I have a closed mesh object, but here I'll use the supplied example mesh. What I would like to do is convert the mesh to a filled voxel grid.
The below code takes a mesh and turns it into a voxel grid using pyvista, however internally the voxel grid is hollow.
import numpy as np
import pyvista as pv
from pyvista import examples
# Load a surface to voxelize
surface = examples.download_foot_bones()
surface
voxels = pv.voxelize(surface, density=surface.length / 500)
voxels.plot(opacity=1.00)
Is there a way to fill the internal hollows with cells? If not I assume there would be a numpy way to fill in the array and then convert that to a filled voxel grid?
I believe you are misled by the representation of the voxels. Since the voxels are tightly packed in the plot, you cannot see internal surfaces even with partial opacity. In other words, the voxelisation is already dense.
We can extract the center of each cell in the voxelised grid, and notice that it's dense in the mesh:
(you might have to manually activate the scalars on the mesh to also get the colourmap).
This produces something like this when zoomed in:
If the voxel mesh were hollow, we would only see spheres along the surface of the mesh. Instead we have densely packed points filling up the entire space.
For more concrete proof, consider a voxelised tetrahedron:
Now if we run this (slow and undocumented) internal helper called
atomize
on it, it will shrink each voxel, allowing us to peek through them:Especially moving the mesh around in 3d makes it obvious that all the internal voxels are there, they are merely obscured by the outermost voxels.