# Area and Volume Calculations

equation int #8729 volume area set flux part closed

# Motivation

In recent months, the indie game programming scene has witnessed the rising popularity of a pressure volume model described in a paper written by Maciej Matyka and Mark Ollila. The paper can be

found here: http://panoramix.ift.uni.wroc.pl/~maq/eng/index.php. In the model's first presentation the authors used an axis aligned

bounding box to approximate the volume of a body. Such a rough approximation is easily computed but highly inaccurate. A second paper followed sometime later describing a two dimensional

implementation that employed Gauss's Theorem to calculate the exact area of a simulated body. The author mentioned the possibility of a three dimensional version using the same technique but

restricted his presentation to the two dimensional case. It would seem that a three dimensional volume calculator should have surfaced shortly afterward; however, no such algorithm has been detailed

by Matyka or Ollila at the time of this writing. The goal of this article is to fill in the details left out of Matyka's second paper.

# Problem Space

The two dimensional case: Given a set of edges that form a closed loop calculate the enclosed area.

The three dimensional case: Given a set of triangles that form a closed surface calculate the enclosed volume.

# Derivation (2D)

Gauss's Theorem relates the divergence of a vector field within an area to the flux of a vector field through a closed path by the following:

**∫∫**_{a} **dell ∙F** da = ∫_{l} **F ∙ dn**

where the path *l* encloses the area *a*. The **dell** vector is defined by:

**dell** = (∂/∂x, ∂/∂y)

When **F** is set to:

**F**(x, y) = (x, 0)

the innards of the double integral in equation 1.1 can be reduced to one. This reduction lets us write:

**∫∫**_{a} 1 da = ∫_{l} **F ∙ dn**

which equates to the area of *a* because:

**dell ∙ F** = x **∙** ∂/∂x + 0 **∙** ∂/∂y = ∂x/∂x = 1

Equation 1.4 tells us that the flux of the vector field **F** through the closed path *l* is equal to the area enclosed by *l*. When *l* is defined by a set of edges the enclosed

area is equal to the sum of the flux of **F** through every edge. A straight forward parameterization of the edge {**v _{1}**,

**v**} is given by:

_{2}**e** = **v _{2}** –

**v**

_{1}**l**(t) = **v _{1}** + t

**e**

where the evaluation of **l**(t) yields any point on the edge for t on [0, 1]. Under this parameterization **dn** from equation 2.4 can be written as:

**dn** = cross **dl** = cross **e** dt = (**-e**_{y}, **e**_{x})dt

The flux of **F** through the edge {**v _{1}**,

**v**} now follows:

_{2}Φ = ∫ **F**(**l**(t)) **∙** (**-e**_{y}, **e**_{x})dt

Φ = ∫ -**e**_{y}(**v _{1}**

_{x}+ t

**e**

_{x})dt

where the integral is evaluated through t on [0, 1]. The analytical solution to equation 1.10 can be written as:

Φ = ½ ∙ (**v _{1}**

_{y}–

**v**

_{2}_{y}) ∙ (

**v**

_{1}_{x}+

**v**

_{2}_{x})

The right hand side of equation 1.11 yields the flux of **F** through the edge {**v _{1}**,

**v**}. Given a closed and clockwise wound edge set {

_{2}**v**,

_{i1}**v**} for i = {0, 1, 2,…,n} the enclosed area is computed by evaluating the following sum:

_{i2}area = ½ ∑ (**v _{i1}**

_{y}–

**v**

_{i2}_{y}) ∙ (

**v**

_{i1}_{x}+

**v**

_{i2}_{x})

# C++ Implementation (2D)

<span class="codecomment">// v: A pointer to the array of vertices // i: A pointer to the array of indices // n: The number of indices (multiple of 2) // This function uses Gauss's Theorem to calculate the area (or field) // of a body enclosed by a set of edges. The edge set must form a // closed path in R2 and be outward facing. An outward facing edge set // wraps clockwise around the body where the x-axis points left and the // y-axis points up.</span> <span class="codekeyword">float</span> CalculateArea(<span class="codekeyword">const</span> VECTOR2 *v, <span class="codekeyword">const unsigned int</span> *i, <span class="codekeyword">const unsigned int</span> n) { <span class="codekeyword">unsigned int</span> j; VECTOR2 v1; VECTOR2 v2; <span class="codekeyword">float</span> area = 0.0f; <span class="codekeyword">for</span>(j = 0; j < n; j+=2) { v1 = v[i[j ]]; v2 = v[i[j+1]]; area += (v1.y-v2.y) * (v1.x+v2.x); } <span class="codekeyword">return</span> area / 2.0f; }

# Derivation (3D)

Gauss's Theorem relates the divergence of a vector field within a volume to the flux of a vector field through a closed surface by the following:

**∫∫∫**_{v} **dell ∙ F** dv = ∫∫_{s} **F ∙ da**

where the surface *s* encloses the volume *v*. The **dell** vector is defined by:

**dell** = (∂/∂x, ∂/∂y, ∂/∂z)

When **F** is set to:

**F**(x, y, z) = (x, 0, 0)

the innards of the triple integral in equation 1.1 can be reduced to one. This reduction lets us write:

**∫∫∫**_{v} 1 dv = ∫∫_{s} **F ∙ da**

which equates to the volume of *v* because:

**dell** **∙ F** = x **∙** ∂/∂x + 0 **∙** ∂/∂y + 0 **∙** ∂/∂z = ∂x/∂x = 1

Equation 2.4 tells us that the flux of the vector field **F** through the closed surface *s* is equal to the volume enclosed by *s*. When *s* is defined by a set of triangles the

enclosed volume is equal to the sum of the flux of **F** through every triangle. A straightforward parameterization of the triangle {**v _{1}**,

**v**,

_{2}**v**} is given by:

_{3}**e _{1}** =

**v**–

_{2}**v**

_{1}**e _{2}** =

**v**–

_{3}**v**

_{1}**s**(u, v)= **v _{1}** + u

**e**+ v

_{1}**e**

_{2}where the evaluation of **s**(u, v) yields any point on the triangle for u on [0, 1] and v on [0, 1-u]. Under this parameterization **da** from equation 2.4 can be written as:

**dn** = (**e _{1}** cross

**e**)dvdu

_{2}The flux of **F** through the triangle {**v _{1}**,

**v**,

_{2}**v**} now follows:

_{3}Φ = ∫∫**F**(**s**(u, v)) **∙** (**e _{1}** cross

**e**)dvdu

_{2}Φ = ∫∫ (**v _{1x}** + u

**e**+ v

_{1x}**e**)(

_{2x}**e**–

_{1y}e_{2z}**e**)dvdu

_{1z}e_{2y}where the integral is evaluated through u on [0, 1] and v on [0, 1-u]. The analytical solution to equation 2.11 can be written as:

Φ = 1/6 ((**v _{2}**

_{y}–

**v**

_{1}_{y})(

**v**

_{3}_{z}–

**v**

_{1}_{z}) –

(

**v**

_{2}_{z}–

**v**

_{1}_{z})(

**v**

_{3}_{y}–

**v**

_{1}_{y}))(

**v**

_{1}_{x}+

**v**

_{2}_{x}+

**v**

_{3}_{x})

The right hand side of equation 2.12 yields the flux of **F** through the triangle {**v _{1}**,

**v**,

_{2}**v**}. Given a closed and clockwise wound

_{3}triangle set {

**v**,

_{i1}**v**,

_{i2}**v**} for i = {0, 1, 2,…,n} the enclosed volume is computed by evaluating the following sum:

_{i3}volume = 1/6 ∑ ((**v _{i2}**

_{y}–

**v**

_{i1}_{y})(

**v**

_{i3}_{z}–

**v**

_{i1}_{z}) –

(

**v**

_{i2}_{z}–

**v**

_{i1}_{z})(

**v**

_{i3}_{y}–

**v**

_{i1}_{y}))(

**v**

_{i1}_{x}+

**v**

_{i2}_{x}+

**v**

_{i3}_{x})

# C++ Implementation (3D)

<span class="codecomment">// v: A pointer to the array of vertices // i: A pointer to the array of indices // n: The number of indices (multiple of 3) // This function uses Gauss's Theorem to calculate the volume of a body // enclosed by a set of triangles. The triangle set must form a closed // surface in R3 and be outward facing. Outward facing triangles index // their vertices in a counterclockwise order where the x-axis points // left, the y-axis point up and the z-axis points toward you (rhs).</span> <span class="codekeyword">float</span> CalculateVolume(<span class="codekeyword">const</span> VECTOR3 *v, <span class="codekeyword">const unsigned int</span> *i, <span class="codekeyword">const unsigned int</span> n) { <span class="codekeyword">unsigned int</span> j; VECTOR3 v1; VECTOR3 v2; VECTOR3 v3; <span class="codekeyword">float</span> volume = 0.0f; <span class="codekeyword">for</span>(j = 0; j < n; j+=3) { v1 = v[i[j ]]; v2 = v[i[j+1]]; v3 = v[i[j+2]]; volume += ((v2.y-v1.y)*(v3.z-v1.z)– (v2.z-v1.z)*(v3.y-v1.y) )*(v1.x+v2.x+v3.x); } <span class="codekeyword">return</span> volume / 6.0f; }

# Conclusion

And there you have it, a fast and precise way to find the area or volume of a body given its edge or triangle set. I have tested the code on a variety of basic primitives and am 99% certain it is

bug free. You can contact me with question, comments and bug reports at kwoxrf@umr.edu.

## 0 Comments

Note: GameDev.net moderates article comments.