Calculate the volume of any Tetrahedron based on 4 points

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

+8
source share
4 answers

Here is the PHP code that calculates the volume of any Tetrahedron with 4 points in mind:

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; 
+1
source

Let's 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 that solves the cross-product and point product of vectors. If you use python, you can use NumPy, otherwise you can write code yourself.

The Wikipedia link is sure to help you. LINK

+16
source

One way to calculate this volume is:

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

This is connected with the estimation of the determinant 4 Γ— 4. It generalizes well to simplexes of higher dimensions, and 6 is a special case of n !, a factorial of dimension. The resulting volume will be oriented, that is, it may be negative depending on the order of the points. If you do not want this, take the absolute value of the result.

If you have a math library on hand, the above wording may be the easiest to write, and the software can take it from there. If not, you can simplify things first by subtracting the d-coordinates from c to c. This will not change the volume, but rotate the rightmost column to (0, 0, 0, 1) . As a result, you can calculate the value of the matrix simply as a determinant of the upper left 3 Γ— 3 submatrix. And using the equation

det (a, b, c) = a

You will receive a Surya answer formula.

If you do not have coordinates for the points, but only the distances between them, look at the Tartaglia Formula , which is basically the square version above, although this is not as straightforward as it would seem at first glance.

+9
source

An example of Ivan Seidel, in Python (answer 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)) 
+5
source

Source: https://habr.com/ru/post/911641/


All Articles