You can try mayavi.mlab
:
Usage
from mayavi import mlab
import numpy as np
def draw3d_mayavi(array, path):
mlab.contour3d(array.astype(np.int32)) # a window would pop up
mlab.savefig(path)
mlab.clf() # clear the scene to generate a new one
mayavi's recontruction is meant for generating 3D heatmap models of the array, so you have to put in a numeric one with 1s and 0s.
Note
There are some drawbacks:
A window will pop out, you have to clear it in your code if you want to make multiple models.
The model reconstructed is .obj and can be very large. If you look closer at the model, you'll see that on the boder the mesh gets 3 layers. I guess the program assumes there to be some gradient.
The contour3d
function can set line_width
, but I don't see any sense of using it for binary data.
Yet, mayavi is very quick, at least compared with voxelfuse. Maybe some post-processing is needed to solve the size problem.
Doc
This function also enables setting color and opacity, etc.
See Plotting functions - contour3d:
contour3d
mayavi.mlab.contour3d(*args, **kwargs)
Plots iso-surfaces for a 3D volume of data supplied as arguments.
Function signatures:
contour3d(scalars, ...) contour3d(x, y, z, scalars, ...)
scalars is a 3D numpy arrays giving the data on a grid.
If 4 arrays, (x, y, z, scalars) are passed, the 3 first arrays give
the position, and the last the scalar value. The x, y and z arrays are
then supposed to have been generated by numpy.mgrid, in other words,
they are 3D arrays, with positions lying on a 3D orthogonal and
regularly spaced grid with nearest neighbor in space matching nearest
neighbor in the array. The function builds a scalar field assuming the
points are regularly spaced.
Keyword arguments:
color
the color of the vtk object. Overides the colormap, if any, when specified. This is specified as a triplet of float ranging from 0
to 1, eg (1, 1, 1) for white.
colormap
type of colormap to use.
contours
Integer/list specifying number/list of contours. Specifying a list of values will only give the requested contours
asked for.
extent
[xmin, xmax, ymin, ymax, zmin, zmax] Default is the x, y, z arrays extent. Use this to change the extent of the object created.
figure
Figure to populate.
line_width
The width of the lines, if any used. Must be a float. Default: 2.0
name
the name of the vtk object created.
opacity
The overall opacity of the vtk object. Must be a float. Default: 1.0
reset_zoom
Reset the zoom to accomodate the data newly added to the scene. Defaults to True.
transparent
make the opacity of the actor depend on the scalar.
vmax
vmax is used to scale the colormap. If None, the max of the data will be used
vmin
vmin is used to scale the colormap. If None, the min of the data will be used
Example (run in ipython --gui=qt
, or in the mayavi2 interactive shell, see Running mlab scripts for more info):
def test_contour3d():
x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalars = x * x * 0.5 + y * y + z * z * 2.0
obj = contour3d(scalars, contours=4, transparent=True)
return obj ```