Calculate Volume of any Tetrahedron given 4 points

26,470

Solution 1

Here is the code, in PHP, that calculates the Volume of any Tetrahedron given 4 points:

class Math{
public static function determinant(array $vals){
    $s = sizeof($vals);
    $sum = 0.0;
    for( $i=0; $i < $s ; $i++ ){
        $mult = 1.0;
        for($j=0; $j < $s ; $j++ ){
            $mult *= $vals[$j][ ($i+$j)%$s ];
        }
        $sum += $mult;
    }
    for( $i=0; $i < $s ; $i++ ){
        $mult = 1;
        for($j=0; $j < $s ; $j++ ){
            $mult *= $vals[$j][ ($i-$j < 0? $s - ($j-$i) :($i-$j)) ];
        }
        $sum -= $mult;
    }
    return $sum;
}

public static function subtract(array $a, array $b){
    for($i = 0; $i < sizeof($a); $i++)
        $a[$i] -= $b[$i];

    return $a;
}
}
// TEST CASE
$a = array(0,0,0);
$d = array(2,0,0);
$c = array(0,2,0);
$b = array(0,0,2);

echo abs(Math::determinant(array(
Math::subtract($a, $b),
Math::subtract($b, $c),
Math::subtract($c, $d),
)))/6;

Solution 2

Say if you have 4 vertices a,b,c,d (3-D vectors).

enter image description here

Now, the problem comes down to writing code which solves cross product and dot product of vectors. If you are from python, you can use NumPy or else you can write code on your own.

The Wikipedia link should definitely help you. LINK

Solution 3

One way to compute this volume is this:

     1      [ax bx cx dx]
V = --- det [ay by cy dy]
     6      [az bz cz dz]
            [ 1  1  1  1]

This involves the evaluation of a 4×4 determinant. It generalizes nicely to simplices of higher dimensions, with the 6 being a special case of n!, the factorial of the dimension. The resulting volume will be oriented, i.e. may be negative depending on the order of points. If you don't want that, take the absolute value of the result.

If you have a math library at hand, the above formulation might be among the easiest to write down, and the software can take it from there. If not, you might simplify things first by subtracting the d coordinates from a through c. This will not change the volume but turn the rightmost column into (0, 0, 0, 1). As a result, you can compute the value of the matrix simply as the determinant of the upper left 3×3 submatrix. And using the equation

det(a, b, c) = a · (b × c)

you end up with the formula from Surya's answer.

In case you don't have coordinates for the points, but just distances between them, look at Tartaglia's Formula which is essentually a squared version of the above, although it's not as straight-forward as it would seem at first glance.

Solution 4

Ivan Seidel's example, in Python (answer is 1.3333...)

def determinant_3x3(m):
    return (m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) -
            m[1][0] * (m[0][1] * m[2][2] - m[0][2] * m[2][1]) +
            m[2][0] * (m[0][1] * m[1][2] - m[0][2] * m[1][1]))


def subtract(a, b):
    return (a[0] - b[0],
            a[1] - b[1],
            a[2] - b[2])

def tetrahedron_calc_volume(a, b, c, d):
    return (abs(determinant_3x3((subtract(a, b),
                                 subtract(b, c),
                                 subtract(c, d),
                                 ))) / 6.0)

a = [0.0, 0.0, 0.0]
d = [2.0, 0.0, 0.0]
c = [0.0, 2.0, 0.0]
b = [0.0, 0.0, 2.0]

print(tetrahedron_calc_volume(a, b, c, d))
Share:
26,470
Ivan Seidel
Author by

Ivan Seidel

Updated on July 31, 2022

Comments

  • Ivan Seidel
    Ivan Seidel almost 2 years

    I need to calculate the volume of a tetrahedron given the coordinates of its four corner points.

  • Ivan Seidel
    Ivan Seidel about 12 years
    (a-b) would be: (ax-bx, ay-by, az-bz), and where do i have to do "Determinant"? thanks!
  • Surya
    Surya about 12 years
    Look at it clearly, its not determinant. Its mod. The dot product of two vectors will give rise to a number. so, as its volume, we can't assign negative numbers.. Thus mod was kept.
  • Surya
    Surya about 12 years
    If you are talking about cross product. Say you have 2 vectors, A & B. Create 2 arrays each having 3 numbers. Thus, the cross product should be directly written as <br/> iDir = (vectA[0] * ( (vectA[1]*vectB[2]) - (vectA[2]*vectB[1]) ) ) jDir = - ( vectA[1] * ( (vectA[0]*vectB[2]) - (vectA[2]*vectB[0]) ) ) kDir = (vectA[2] * ( (vectA[0]*vectB[1]) - (vectA[1]*vectB[0]) ) ) productAB = [iDir, jDir, kDir]
  • Surya
    Surya about 12 years
    you could always go for a better algorithm but this one is a basic one to go for.
  • Ivan Seidel
    Ivan Seidel about 12 years
    Thanks for your help! i did it yesterday, but i couldn't post the code because i had to wait 8 hours, as i'm not a frequent member...
  • ajfbiw.s
    ajfbiw.s over 7 years
    Is there a proof for the volume formula that you can find?
  • MvG
    MvG over 7 years
    @ajfbiw.s: I don't have a reference at hand just now. The Wikipedia section on Parallelotopes gives no reference either. If I had to prove this myself now, I'd start by observing that the value is invariant under translations and rotations, and then demonstrate that in the special case where the upper left 3×3 submatrix is triangular, the result is what you get if for each vector you just take the component orthogonal to all the previous vectors. Thanks to the mentioned invariance, that already coveres the general case as well.
  • micsthepick
    micsthepick almost 6 years
    @Surya but keep in mind the scalar triple product is also equivalent to taking the determinant with those vectors as row vectors
  • The XGood
    The XGood over 3 years
    Why rewrite it into python?
  • ideasman42
    ideasman42 over 3 years
    @the-xgood because some details aren't obvious (see the replies to that answer).