Hilariously Fast Volume Computation with the Divergence Theorem (2018)
Hilariously Fast Volume Computation with the Divergence Theorem (2018)
Author: Rosenzweig
Date: February 16, 2018
Warning: This article contains high-level humor.Correction: No, there actually won't be any jokes.
This paper outlines a highly efficient algorithm designed to calculate the volume of a simple, closed, 3D mesh composed of triangles. While the current focus is on triangulated meshes, the logic could potentially be extended to other mesh types in the future.
1. Mathematical Foundation
The starting point is the standard definition of volume as a triple integral over a region where the integrand is a constant one:
To simplify this, we introduce a vector field in such that its divergence is exactly one. For this specific implementation, we define:
F(x, y, z) = (x, 0, 0)
We can verify the divergence as follows:
By substituting this back into our volume equation, we get:
Applying the Divergence Theorem, we can transform this volume integral into a surface integral over the boundary :
Decomposing the Mesh
Since the 3D mesh is a collection of piecewise triangles, the total volume is simply the sum of the integrals over each individual triangle :
2. Triangle Parametrization
To solve the integral for a single triangle , we define its vertices as and . We then establish two edge vectors:
The surface of the triangle can be parametrized by :
From this, we derive the partial derivatives:
The cross product represents the normal vector, which remains constant across the triangle's surface.
Simplifying the Surface Integral
Substituting our parametrization and the definition of into the integral:
Because only has an -component, only the -component of the cross product contributes to the result. The formula simplifies to:
3. Solving the Integral of
We now evaluate the remaining integral using the parametrization:
By treating the vertex coordinates as constants and integrating, we find:
Substituting and :
Final Volume Formula
To optimize performance and avoid division inside the loop, we factor out :
4. Implementation and Performance
Workflow Visualization
Complexity Analysis
Unlike naive sampling methods (which involve rendering the mesh and sampling pixels—a very costly process), this algorithm is strictly linear.
| Metric | Value |
|---|---|
| Time Complexity | where is the number of triangles |
| Additions per Triangle | 7 |
| Multiplications per Triangle | 3 |
| Total FLOPs | (approx) |
Hardware Example: On a budget-friendly $35 Raspberry Pi (CPU only), this algorithm could process roughly 30 million triangles per frame while maintaining a 60 FPS target.
Implementation Checklist
- Ensure mesh is closed (watertight).
- Compute and for every triangle.
- Extract the -component of the cross product.
- Sum the -coordinates of the three vertices.
- Apply the final scalar.
5. Conclusion & Motivation
Why was this written?
"The vector calculus exam is soon, and I need to study."
Further Reading: After publication, it was noted that similar logic is described in the paper "Efficient Feature Extraction for 2D/3D Objects in Mesh Representation" by Cha Zheng and Tsuhan Chen.
Summary Code Snippet
double calculateVolume(Mesh mesh) {
double totalSum = 0;
for (auto& tri : mesh.triangles) {
Vector3 d1 = tri.v1 - tri.v0;
Vector3 d2 = tri.v2 - tri.v0;
double crossX = (d1.y * d2.z) - (d1.z * d2.y);
double sumX = tri.v0.x + tri.v1.x + tri.v2.x;
totalSum += crossX * sumX;
}
return totalSum / 6.0;
}
Figure 1: Conceptual representation of converting a volume integral to a surface sum.