Hilariously fast volume computation with the divergence theorem (2018)

258 points · 66 comments on HN · read original →

Points and comments are a snapshot, not live.

Deriving a fast O(n) volume algorithm for triangulated 3D meshes using the divergence theorem.

The divergence theorem converts a volume integral into a simpler surface integral over the mesh's triangles. Choosing F = ⟨x, 0, 0⟩ simplifies the calculation because only the cross product's x-component and the sum of triangle vertices' x-coordinates matter. The resulting formula sums over triangles: V = (1/6) Σ ((Δ₁×Δ₂)ₓ)(T₀ₓ + T₁ₓ + T₂ₓ). This requires 8n - 1 additions and 3n + 1 multiplications for n triangles, enabling real-time volume computation of roughly 30 million triangles per frame on a Raspberry Pi at 60 fps.

What commenters are saying

Several commenters note this is a known technique and a 3D analog of the shoelace formula or the surveyor's formula. One references Algorithm 550 (1980) for solid polyhedron measures. Another describes the geometric intuition: summing signed volumes of triangular prisms parallel to axes. Some debate the 'naive' alternatives the author mentions (voxelization, GPU rendering). The thread treats the derivation as a satisfying but unsurprising result for those with undergraduate mathematics background.