I want to calculate the volume of a 3D mesh object having a surface made up triangles.
4 Answers
Reading this paper, it is actually a pretty simple calculation.
The trick is to calculate the signed volume of a tetrahedron - based on your triangle and topped off at the origin. The sign of the volume comes from whether your triangle is pointing in the direction of the origin. (The normal of the triangle is itself dependent upon the order of your vertices, which is why you don't see it explicitly referenced below.)
This all boils down to the following simple function:
public float SignedVolumeOfTriangle(Vector p1, Vector p2, Vector p3) {
var v321 = p3.X*p2.Y*p1.Z;
var v231 = p2.X*p3.Y*p1.Z;
var v312 = p3.X*p1.Y*p2.Z;
var v132 = p1.X*p3.Y*p2.Z;
var v213 = p2.X*p1.Y*p3.Z;
var v123 = p1.X*p2.Y*p3.Z;
return (1.0f/6.0f)*(-v321 + v231 + v312 - v132 - v213 + v123);
}
and then a driver to calculate the volume of the mesh:
public float VolumeOfMesh(Mesh mesh) {
var vols = from t in mesh.Triangles
select SignedVolumeOfTriangle(t.P1, t.P2, t.P3);
return Math.Abs(vols.Sum());
}
-
3
-
I’m wondering why this wasn’t discovered before 2001. Or it was, but had no relevance? Sep 16, 2013 at 22:47
-
2Oct 1984, the paper "A symbolic method for calculating the integral properties of arbitrary nonconvex polyhedra" was published and describe this method to compute the volume. It's also a more or less a trivial method, so you need much more than just this information to publish a paper.– R.FalqueSep 24, 2013 at 5:53
-
3"topped off at the origin" is not mandatory, you may choose any fixed point. And if the object is quite far from the origin, this will lead to numerical instability. Better choose an arbitrary point from the mesh Sep 7, 2014 at 15:55
-
1@KaranAhuja In 2D, this is a simple computer application of Green's theorem (see e.g. planimeters): you can calculate the area enclosed by a curve by a line integral over the curve. In 3D it's an application of the divergence theorem (Gauss' theorem): you can calculate the volume enclosed by a boundary surface by a flux integral over that boundary (for a vector field whose divergence is 1). The boundaries do not need to be convex, but they need to be closed. Sep 29, 2022 at 6:13
Yip Frank Kruegers answer works well +1 for that. If you have vector functions available to you you could use this too:
public static float SignedVolumeOfTriangle(Vector p1, Vector p2, Vector p3)
{
return p1.Dot(p2.Cross(p3)) / 6.0f;
}
edit .. added impl. for Dot() and Cross() if you are unsure. Most Math libs will have these. If you are using WPF they are implemented as static methods of the Vector3D class.
public class Vector
{
...
public float Dot(Vector a)
{
return this.X * a.X + this.Y * a.Y + this.Z * a.Z;
}
public Vector Cross(Vector a)
{
return new Vector(
this.Y * a.Z - this.Z * a.Y,
this.Z * a.X - this.X * a.Z,
this.X * a.Y - this.Y * a.X
);
}
...
}
-
perhaps post code for Dot() AND Cross()? (both trivial to implement, but for completeness). BTW, @Frank Kruegers answer is what you get if you simplify p1.Dot(p2.Cross(p3)) / 6.0f Jan 3, 2013 at 23:18
The GNU Triangulated Surface Library can do this for you. Keep in mind that the surface must be closed. That is not going to be the case for quite a few 3D models.
If you want to implement it yourself, you could start by taking a look at their code.
-
8If you do reimplement this, please be careful - the GTS library is LGPL, so any derivative work must be LGPL or GPL.– CascabelSep 10, 2009 at 16:00
The method above is correct for "simple" objects (no intersecting/overlapping triangles) like spheres tetrahedras and so on. For more complex shapes, a good idea could be to segment the mesh (close it) and calculate the volume of each segment separately. Hope this helps.
-
4
-
2Wrong answer. The method described is correct for arbitrarily complex closed objects with no intersecting/overlapping triangles, and there is no requirement on position of the center point. That's because the tetrahedron "volumes" are signed and add algebraically. Mar 10, 2016 at 22:02
-
@galinette Yes you are right. By "simple" objects i meant objects with no intersecting/overlapping triangles. Also the center of the mesh can be outside.– AnoroahJul 22, 2016 at 15:26
-