logo
Kurashizu Blogwhere ideas flow
  • Home
  • News
  • Blog
  • About
  • Home
  • News
  • Blog
  • About

© 2026 Kurashizu. All rights reserved.

Admin·New Post·Service Status
Powered by Cloudflare·31.10.26
← Back to news

Hilariously Fast Volume Computation with the Divergence Theorem (2018)

Original article
alyssarosenzweig.ca|204 points|51 comments|by luu|Aug 28, 2026

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 VVV as a triple integral over a region RRR where the integrand is a constant one:

V=∭R1dVV = \iiint_R 1 \mathrm{d}VV=∭R​1dV

To simplify this, we introduce a vector field F\mathbf{F}F in R3\mathbb{R}^3R3 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 divF\text{div} \mathbf{F}divF as follows:

divF=∂Fx∂x+∂Fy∂y+∂Fz∂z=1+0+0=1\mathrm{div} \mathbf{F} = \frac{\partial F_x}{\partial x} + \frac{\partial F_y}{\partial y} + \frac{\partial F_z}{\partial z} = 1 + 0 + 0 = 1divF=∂x∂Fx​​+∂y∂Fy​​+∂z∂Fz​​=1+0+0=1

By substituting this back into our volume equation, we get:

V=∭RdivF(x,y,z)dVV = \iiint_R \mathrm{div} \mathbf{F}(x, y, z) \mathrm{d}VV=∭R​divF(x,y,z)dV

Applying the Divergence Theorem, we can transform this volume integral into a surface integral over the boundary SSS:

V=∬SF(x,y,z)⋅dSV = \iint_S \mathbf{F}(x, y, z) \cdot \mathrm{d}\mathbf{S}V=∬S​F(x,y,z)⋅dS

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 TiT_iTi​:

V=∑i=0∬TiF(x,y,z)⋅dSV = \sum_{i = 0} \iint_{T_i} \mathbf{F}(x, y, z) \cdot \mathrm{d}\mathbf{S}V=∑i=0​∬Ti​​F(x,y,z)⋅dS


2. Triangle Parametrization

To solve the integral for a single triangle TiT_iTi​, we define its vertices as Ti0,Ti1,T_{i0}, T_{i1},Ti0​,Ti1​, and Ti2T_{i2}Ti2​. We then establish two edge vectors:

  • Δ1=Ti1−Ti0\Delta_1 = T_{i1} - T_{i0}Δ1​=Ti1​−Ti0​
  • Δ2=Ti2−Ti0\Delta_2 = T_{i2} - T_{i0}Δ2​=Ti2​−Ti0​

The surface of the triangle can be parametrized by r(u,v)\mathbf{r}(u, v)r(u,v):

r(u,v)=Ti0+uΔ1+vΔ2\mathbf{r}(u, v) = T_{i0} + u\Delta_1 + v\Delta_2r(u,v)=Ti0​+uΔ1​+vΔ2​

From this, we derive the partial derivatives:

  • ru=Δ1\mathbf{r}_u = \Delta_1ru​=Δ1​
  • rv=Δ2\mathbf{r}_v = \Delta_2rv​=Δ2​

The cross product ru×rv=Δ1×Δ2\mathbf{r}_u \times \mathbf{r}_v = \Delta_1 \times \Delta_2ru​×rv​=Δ1​×Δ2​ represents the normal vector, which remains constant across the triangle's surface.

Simplifying the Surface Integral

Substituting our parametrization and the definition of F\mathbf{F}F into the integral:

V=∑i=0∬Ti(x,0,0)⋅(Δi1×Δi2)dAV = \sum_{i = 0} \iint_{T_i} (x, 0, 0) \cdot (\Delta_{i1} \times \Delta_{i2}) dAV=∑i=0​∬Ti​​(x,0,0)⋅(Δi1​×Δi2​)dA

Because F\mathbf{F}F only has an xxx-component, only the xxx-component of the cross product (Δi1×Δi2)x(\Delta_{i1} \times \Delta_{i2})_x(Δi1​×Δi2​)x​ contributes to the result. The formula simplifies to:

V=∑i=0(Δi1×Δi2)x∬TixdAV = \sum_{i = 0} (\Delta_{i1} \times \Delta_{i2})_x \iint_{T_i} x dAV=∑i=0​(Δi1​×Δi2​)x​∬Ti​​xdA


3. Solving the Integral of xxx

We now evaluate the remaining integral ∬TixdA\iint_{T_i} x dA∬Ti​​xdA using the parametrization:

∬TixdA=∫01∫01−u(Ti0x+uΔi1x+vΔi2x)dvdu\iint_{T_i} x dA = \int_{0}^{1} \int_{0}^{1-u} (T_{i0x} + u \Delta_{i1x} + v \Delta_{i2x}) dv du∬Ti​​xdA=∫01​∫01−u​(Ti0x​+uΔi1x​+vΔi2x​)dvdu

By treating the vertex coordinates as constants and integrating, we find:

=Ti0x(12)+Δi1x(16)+Δi2x(16)= T_{i0x} \left(\frac{1}{2}\right) + \Delta_{i1x} \left(\frac{1}{6}\right) + \Delta_{i2x} \left(\frac{1}{6}\right)=Ti0x​(21​)+Δi1x​(61​)+Δi2x​(61​)

Substituting Δi1x=(Ti1x−Ti0x)\Delta_{i1x} = (T_{i1x} - T_{i0x})Δi1x​=(Ti1x​−Ti0x​) and Δi2x=(Ti2x−Ti0x)\Delta_{i2x} = (T_{i2x} - T_{i0x})Δi2x​=(Ti2x​−Ti0x​):

=Ti0x(12)+(Ti1x−Ti0x)(16)+(Ti2x−Ti0x)(16)= T_{i0x} \left(\frac{1}{2}\right) + (T_{i1x} - T_{i0x})\left(\frac{1}{6}\right) + (T_{i2x} - T_{i0x})\left(\frac{1}{6}\right)=Ti0x​(21​)+(Ti1x​−Ti0x​)(61​)+(Ti2x​−Ti0x​)(61​) =16(Ti0x+Ti1x+Ti2x)= \frac{1}{6}(T_{i0x} + T_{i1x} + T_{i2x})=61​(Ti0x​+Ti1x​+Ti2x​)

Final Volume Formula

To optimize performance and avoid division inside the loop, we factor out 1/61/61/6:

V=16∑i=0(Δi1×Δi2)x(Ti0x+Ti1x+Ti2x)V = \frac{1}{6} \sum_{i = 0} (\Delta_{i1} \times \Delta_{i2})_x (T_{i0x} + T_{i1x} + T_{i2x})V=61​∑i=0​(Δi1​×Δi2​)x​(Ti0x​+Ti1x​+Ti2x​)


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.

MetricValue
Time ComplexityO(n)O(n)O(n) where nnn is the number of triangles
Additions per Triangle7
Multiplications per Triangle3
Total FLOPs11n11n11n (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 Δi1\Delta_{i1}Δi1​ and Δi2\Delta_{i2}Δi2​ for every triangle.
  • Extract the xxx-component of the cross product.
  • Sum the xxx-coordinates of the three vertices.
  • Apply the final 1/61/61/6 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;
}

Volume Integration Concept Figure 1: Conceptual representation of converting a volume integral to a surface sum.