# How can I calculate the volume of a mesh?

**URL:** <https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450>\
**Category:** General Usage\
**Tags:** question, mesh\
**Created:** [May 22, 2022, 6:21am UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450 "2022-05-22T06:21:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![thenry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thenry/32/9967_2.png) [@thenry](https://discourse.julialang.org/u/thenry)\
**Post date:** [May 22, 2022, 6:21am UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/1 "2022-05-22T06:21:35Z")

</div>

How can I calculate the volume of a mesh?

The mesh is a closed triangles mesh, with 3-D coordinates; it is a surface mesh.

I have tried the following, but I am getting an error:

using GLMakie

using FileIO

using MeshIO

file\_path = “my\_file.stl”

my\_mesh = load(assetpath(file\_path))

volume(my\_mesh)

ERROR: `Makie.convert_arguments` for the plot type Volume{Tuple{GeometryBasics.Mesh{3, Float32, GeometryBasics.TriangleP{3, Float32, GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}}, GeometryBasics.FaceView{GeometryBasics.TriangleP{3, Float32, GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}}, GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}, GeometryBasics.NgonFace{3, GeometryBasics.OffsetInteger{-1, UInt32}}, StructArrays.StructVector{GeometryBasics.PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}, NamedTuple{(:position, :normals), Tuple{Vector{Point{3, Float32}}, Vector{Vec{3, Float32}}}}, Int64}, Vector{GeometryBasics.NgonFace{3, GeometryBasics.OffsetInteger{-1, UInt32}}}}}}} and its conversion trait VolumeLike() was unsuccessful.

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [May 22, 2022, 7:00am UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/2 "2022-05-22T07:00:20Z")

</div>

I don’t know whether there is a Julia package for it; others might.

Anyhow, is the mesh guaranteed to be convex (no “indents”)?

If yes, a simple way would be to calculate the sum of the volumes of all triangular pyramids with their apex being a (the same) point inside the mesh. If the mesh is not convex, things are more complex.

---

<div class="post-metadata">

**Author:** ![thenry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thenry/32/9967_2.png) [@thenry](https://discourse.julialang.org/u/thenry)\
**Post date:** [May 22, 2022, 7:32am UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/3 "2022-05-22T07:32:57Z")

</div>

The mesh is not necessarily convex.  
I thought about creating a 3D-mesh (a mesh of tetrahedron) from the 2-D mesh (mesh of triangles), and then calculating the sum of the tetrahedron volumes. But I also do not know how to get the 3D-mesh from the triangle-mesh.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 22, 2022, 2:32pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/4 "2022-05-22T14:32:22Z")

</div>

Isn’t there some theorem that proves the volume can be calculated by taking an arbitrary center, computing the signed volumes of tetrahedra constructed by connecting the center with each triangular facet, and adding them together? The facets must represent a consistent cover of the boundary.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [May 22, 2022, 3:01pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/5 "2022-05-22T15:01:44Z")

</div>

Do you need convexity for this? Suppose the region is two spheres intersecting only at one boundayr point, does “singed volume” fix this up?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 22, 2022, 3:17pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/6 "2022-05-22T15:17:18Z")

</div>

In this case the boundary is empty, isn’t it?

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [May 22, 2022, 4:57pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/7 "2022-05-22T16:57:25Z")

</div>

I’m thinking of two separate spheres, so the boundary is the union of the two boundaries.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 22, 2022, 5:15pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/8 "2022-05-22T17:15:57Z")

</div>

In that case, wouldn’t that be a union of two convex shapes sharing a single commong point. Two convex shapes period, really?

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [May 22, 2022, 5:24pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/9 "2022-05-22T17:24:08Z")

</div>

Yes. Does the volume computation work for such a geometry?

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [May 22, 2022, 5:24pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/10 "2022-05-22T17:24:52Z")

</div>

Or let them overlap a bit and call it a peanut.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 22, 2022, 10:06pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/11 "2022-05-22T22:06:52Z")

</div>

Solution described [here](https://n-e-r-v-o-u-s.com/blog/?p=4415) seems simple to implement

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 22, 2022, 11:27pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/12 "2022-05-22T23:27:15Z")

</div>

I still think it would work. I believe the principle is the same as that of the polar planimeter.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 22, 2022, 11:34pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/13 "2022-05-22T23:34:37Z")

</div>

> **[A symbolic method for calculating the integral properties of arbitrary...](https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=6429334&casa_token=ugSTW4nXNBwAAAAA:AaXl8wP10VCouCkevgp9lhB_9oJeLJDd3wxu-zSWGk5jCqsAL-9H8kQrRwoXiiH8WTHEKx6YWrI&tag=1)**
>
> A simple and systematic method is described for calculating the integral of a polynomial function over an arbitrary nonconvex polyhedron. First a general formula is presented for direct evaluation of the integral of a polynomial over a 3-D simplex....

It appears to be a variant of the Green’s theorem.

---

<div class="post-metadata">

**Author:** ![thenry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thenry/32/9967_2.png) [@thenry](https://discourse.julialang.org/u/thenry)\
**Post date:** [May 23, 2022, 1:04pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/14 "2022-05-23T13:04:36Z")

</div>

I found a Matlab implementation:

> **[Volume, Surface Area, and Centroid of Polyhedron](https://ch.mathworks.com/matlabcentral/fileexchange/93875-volume-surface-area-and-centroid-of-polyhedron)**
>
> Finds volume, surface area, and centroid of a polyhedron

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.

````julia
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
```
````

---

<div class="post-metadata">

**Author:** ![cesquerre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cesquerre/32/205867_2.png) [@cesquerre](https://discourse.julialang.org/u/cesquerre)\
**Post date:** [March 10, 2025, 3:47pm UTC](https://discourse.julialang.org/t/how-can-i-calculate-the-volume-of-a-mesh/81450/15 "2025-03-10T15:47:50Z")

</div>

[GeometryBasics.jl](https://juliageometry.github.io/GeometryBasics.jl/stable/api/#GeometryBasics.volume-Tuple%7BMesh%7D) includes functions for the volume and area of ​​a mesh.

```julia
using GeometryBasics: area, volume

```
