Search code examples
python3dmeshcgalmarching-cubes

Find volume of object given a triangular mesh


I have an object defined by vertices and triangular faces.

verts = [[0.1, 1.,  1. ]  [1.,  1.,  0.1]  [1.,  0.1, 1. ]  [1.,  1.,  1.9]  [1.,  1.9, 1. ]
 [1.9, 1.,  1. ] ]

And triangular faces, which define the vertices composing each faces:

faces = [[ 2,  1,  0]  [ 0,  3,  2]  [ 1,  4,  0]  [ 0,  4,  3]  [ 5,  1,  2]  [ 3,  5,  2]
 [ 5,  4,  1]  [ 4,  5,  3]]

I can create a mesh using Poly3DCollection

from mpl_toolkits.mplot3d.art3d import Poly3DCollection
mesh = Poly3DCollection(verts[faces])

I can use mesh to plot the object in 3D. Is there a way in python to then find the volume of this object? Papers (here, here, and here) indicate it is possible. If not in python, are there methods in other languages that make this possible (i.e. that you know of). Thanks in advance!


Solution

  • I solved this using meshplex

    import meshplex
    import numpy as np
    
    verts = [[0.1, 1.,  1. ],  [1.,  1.,  0.1],  [1.,  0.1, 1. ],  [1.,  1.,  1.9],  [1.,  1.9, 1. ], [1.9, 1.,  1. ]]
    faces = [[ 2,  1,  0],  [ 0,  3,  2],  [ 1,  4,  0],  [ 0,  4,  3],  [ 5,  1,  2],  [ 3,  5,  2], [ 5,  4,  1],  [ 4,  5,  3]]
    
    mesh = meshplex.MeshTri(np.array(points), np.array(faces))
    
    V = np.sum(mesh.cell_volumes)
    
    print("Volume =", V)
    # Volume = 5.6118446