Skip to content

Instantly share code, notes, and snippets.

@maxrohleder
Created August 2, 2022 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxrohleder/010d257e752dfffbab7bbd16846321a7 to your computer and use it in GitHub Desktop.
Save maxrohleder/010d257e752dfffbab7bbd16846321a7 to your computer and use it in GitHub Desktop.
snippet to convert a binary volume to an stl surface mesh
from skimage import measure
from stl import mesh
def to_stl(fname, vol):
assert np.alltrue(np.isin(vol, [1, 0])), "volume must be binary"
verts, faces, _, _ = measure.marching_cubes_lewiner(vol)
m = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
m.vectors[i][j] = verts[f[j], :]
m.save(fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment