(No, there won’t be jokes.)
The following presents a fast algorithm for volume computation of a simple, closed, triangulated 3D mesh. This assumption is a consequence of the divergence theorem. Further extensions may generalise to other meshes as well, although that is presently out of scope.
We begin with the definition of volume as the triple integral over a region of the constant one:
Let be a function in such that its divergence is equal to one. For the purposes of this paper, we choose:
It can easily be verified that
By the Divergence Theorem, this is equal to the surface integral:
This surface integral, defined over the surface S of the 3D mesh, is equal to the sum of its piecewise triangle parts. Let denote the surface of the ’th triangle in the mesh. Then,
Let represent the ’th vertex of the ’th triangle. Let equal the vector difference between and , and likewise equal to . Each individual triangle may thus be parametrised as:
Then, simple differentiation yields:
Thus, the surface integral can be rewritten in terms of this parametrisation, substituting in the definition of as needed:
This cross product is constant throughout the triangle and easy to calculate from the vertex data. Only the X component of the cross product should be calculated; the others are equal to zero due to the dot product with the zero components of . can be thus be rewritten as:
We now focus on the surface integral . Expanding with the parametrisation yields:
This integral can be directly evaluated, treating vertex data as constants:
Substituting into the original sum and pulling out a constant factor of to avoid the inner loop division, this yields the following compact formula for the volume:
The final algorithm contains no numerical integration nor differentiation. In contrast to common naive algorithms for volume, which are equivalent to rendering the mesh and then sampling the render, an expensive operation, there is only a single loop in this algorithm, over the triangles. Thus, this algorithm for volume computation is O(n) to the number of the triangles. Furthermore, the per-triangle calculation is similarly efficient: given the natural expansion of the cross product, the inner part contains seven additions and three multiplications. On the outside of the loop is only a single multiplication. Thus, for a mesh of triangles, the algorithm requires additions and multiplications, or floating point operations. This is very fast.
For a ballpark number, if volume needs to be calculated every frame in a high-performance 60 frames per second application, without the aid of a GPU, only using the CPU capabilities of a $35 Raspberry Pi, around 30 million triangles could be measured every frame.
The vector calculus exam is soon, and I need to study. Plus, who doesn’t love 3D graphics?!
I would be (pleasantly) surprised if the algorithm is novel. Further research after posting reveals the paper Efficient Feature Extraction for 2D/3D Objects in Mesh Representation by Cha Zheng and Tsuhan Chen, which appears to describe the same algorithm, although the derivation is different. It was fun while it lasted!
Back to home