pmneila/PyMCubes

Broken Mesh

YoruCathy opened this issue · 3 comments

Hello, I am trying to covert voxel to mesh with marching cubes.
My code is as the following

vertices, triangles = mcubes.marching_cubes(voxel, 0.5)  
mcubes.export_mesh(vertices, triangles, "gorira.obj", "gorira")  
print("Plotting mesh...")  
mlab.triangular_mesh(
    vertices[:, 0], vertices[:, 1], vertices[:, 2], triangles)
print("Done.")  
mlab.show()

The voxel is visualized as
image
However, after marching cubes algorithm, it becomes broken, like this

image

Why does this happen? Does anyone have some ideas about this?

Hi @YoruCathy,

Without having access to the voxel array and without knowing its internal structure, it is hard to answer your question. My first suggestion is to chage the value of the extracted isosurface to 0:

vertices, triangles = mcubes.marching_cubes(voxel, 0)

If that does not solve your problem, could you send me an example voxel array?

Thanks to your suggestion, I tried this solution but it still doesn't work.
The voxel file is uploaded on Google Drive here

Hi,

I think I understand your problem now. This broken mesh is expected behavior, since the 0.5 isosurface touches the boder of the array. If you want that your mesh is closed, you can simply pad your array with zeros in all dimensions. Something like this should work:

voxel = np.pad(voxel, 1)
vertices, triangles = mcubes.marching_cubes(voxel, 0.5)  
mcubes.export_mesh(vertices, triangles, "gorira.obj", "gorira")  

Note that, since the version 0.1 of PyMCubes it is possible to smooth the embedding array to produce better results. For example

voxel = np.pad(voxel, 1)
voxel = mcubes.smooth(voxel)
vertices, triangles = mcubes.marching_cubes(voxel, 0)  # If you smooth the array you need to extract the 0-isosurface
mcubes.export_mesh(vertices, triangles, "gorira.obj", "gorira")  

Hope that helps.