Created
August 2, 2022 08:34
-
-
Save maxrohleder/010d257e752dfffbab7bbd16846321a7 to your computer and use it in GitHub Desktop.
snippet to convert a binary volume to an stl surface mesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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