How can I calculate the volume of a mesh?

I found a Matlab implementation:

It seems to be the same idea as the link you mention.
I have ported the code to Julia, and it gives me the same volume as Meshlab for the two meshes I tested on.

using LinearAlgebra

# ------------------------------------------------
# original code in Matlab:
# https://ch.mathworks.com/matlabcentral/fileexchange/93875-volume-surface-area-and-centroid-of-polyhedron
# ------------------------------------------------
# This function find the volume, surface area, and centroid of a polyhedron. 
# The polyhedron faces should be divided into triangles. The numbering of all triangles faces should be consistent. 
# Either all of them in clock wise or counter clock wise direction as seen from the outside.
# 
# Input:
# V: (Nv,3) vertices matrix
# F: (Nf,3) faces matrix
# Output:
# Vol: volume
# A: surface area
# Cx, Cy, and Cz: x,y, and z coordinates of centroid, respectively.
# 
# ------------------------------------------------
function Polyhedron_VAC(V,F)
  Vol=0;
  A=0;
  Mx=0;
  My=0;
  Mz=0;
  
  for i=1:1:size(F,1)
      
    x1=V[F[i,1],1];
    y1=V[F[i,1],2];
    z1=V[F[i,1],3];

    x2=V[F[i,2],1];
    y2=V[F[i,2],2];
    z2=V[F[i,2],3];
      
    x3=V[F[i,3],1];
    y3=V[F[i,3],2];
    z3=V[F[i,3],3];
     
    vi=1/6*det([x1 x2 x3 0; y1 y2 y3 0; z1 z2 z3 0; 1 1 1 1])
    
    # ai=1/2*norm( cross(V(F(i,2),:)-V(F(i,1),:),V(F(i,3),:)-V(F(i,1),:)));
    ai=1/2*norm( cross(V[F[i,2],:] - V[F[i,1],:], V[F[i,3],:]-V[F[i,1],:]) )
    
    xi=1/4*(x1+x2+x3);
    yi=1/4*(y1+y2+y3);
    zi=1/4*(z1+z2+z3);
    Vol=Vol+vi;
    A=A+ai;
    Mx=Mx+xi*vi;
    My=My+yi*vi;
    Mz=Mz+zi*vi;
  end
  
  Cx=Mx/Vol
  Cy=My/Vol
  Cz=Mz/Vol
  S=sign(Vol)
  Vol=Vol*S
end
```
2 Likes