Search code examples
pythongraphics3dvtkmayavi

Basic 3D voxel grid in Mayavi


I'm trying to visualize a 3D array through Mayavi in Python. I simply want to create a structured 3D voxel grid in which I can show some pre-specified voxel-space-filling points. I do not think that I want

The only example that I can find that I think is relatively relevant is this MRI example. I can use the following code to get a somewhat workable example:

import numpy as np
from mayavi import mlab

data = (100, 100, 100)
data = np.zeros(data)
data[0:50, 50:70, 0:50] = 1
data[0:50, 0:20, 0:50] = 1

src = mlab.pipeline.scalar_field(data)
outer = mlab.pipeline.iso_surface(src)

mlab.show()

This is able to generate the following images: enter image description here enter image description here As you can see, not all sides of the boxes are generated, even though those points have the same value as the sides of the boxes that are generated.

Is there a way to visualize every single point in the numpy array that has value equal to 1? I am fine if there is no iso-surface visualization -- in fact, I would prefer some Minecraft-esque blocky voxel visualization.


Solution

  • Hi

    import mayavi.mlab
    import numpy
    
    data = (100, 100, 100)
    data = numpy.zeros(data)
    data[0:50, 50:70, 0:50] = 1
    data[0:50, 0:20, 0:50] = 1
    
    xx, yy, zz = numpy.where(data == 1)
    
    mayavi.mlab.points3d(xx, yy, zz,
                         mode="cube",
                         color=(0, 1, 0),
                         scale_factor=1)
    
    mayavi.mlab.show()
    

    enter image description here