ncollide3d/utils/
tetrahedron.rs

1use num;
2
3use na::{self, Matrix3, RealField};
4
5use crate::math::Point;
6use crate::utils;
7
8/// Computes the volume of a tetrahedron.
9#[inline]
10pub fn tetrahedron_volume<N: RealField + Copy>(
11    p1: &Point<N>,
12    p2: &Point<N>,
13    p3: &Point<N>,
14    p4: &Point<N>,
15) -> N {
16    num::abs(tetrahedron_signed_volume(p1, p2, p3, p4))
17}
18
19/// Computes the signed volume of a tetrahedron.
20///
21/// If it is positive, `p4` is on the half-space pointed by the normal of the oriented triangle
22/// `(p1, p2, p3)`.
23#[inline]
24pub fn tetrahedron_signed_volume<N: RealField + Copy>(
25    p1: &Point<N>,
26    p2: &Point<N>,
27    p3: &Point<N>,
28    p4: &Point<N>,
29) -> N {
30    let p1p2 = *p2 - *p1;
31    let p1p3 = *p3 - *p1;
32    let p1p4 = *p4 - *p1;
33
34    let mat = Matrix3::new(
35        p1p2[0], p1p3[0], p1p4[0], p1p2[1], p1p3[1], p1p4[1], p1p2[2], p1p3[2], p1p4[2],
36    );
37
38    mat.determinant() / na::convert(6.0f64)
39}
40
41/// Computes the center of a tetrahedron.
42#[inline]
43pub fn tetrahedron_center<N: RealField + Copy>(
44    p1: &Point<N>,
45    p2: &Point<N>,
46    p3: &Point<N>,
47    p4: &Point<N>,
48) -> Point<N> {
49    utils::center(&[*p1, *p2, *p3, *p4])
50}