Compute the volume of a tetrahedron, given 4 points which are 1-D arrays Since we know that the determinant is to be evaluated of a 3x3 matrix, we write the expression itself
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=wp), | intent(in), | dimension(:) | :: | p1 | ||
real(kind=wp), | intent(in), | dimension(:) | :: | p2 | ||
real(kind=wp), | intent(in), | dimension(:) | :: | p3 | ||
real(kind=wp), | intent(in), | dimension(:) | :: | p4 |
function vol_tetrahedron(p1, p2, p3, p4)
!< Compute the volume of a tetrahedron, given 4 points which
!< are 1-D arrays
!< Since we know that the determinant is to be evaluated of
!< a 3x3 matrix, we write the expression itself
!-----------------------------------------------------------
implicit none
real(wp), dimension(:), intent(in):: p1, p2, p3, p4
real(wp), dimension(1:3,1:3) :: A
real(wp) :: vol_tetrahedron
A(:, 1) = p1 - p4
A(:, 2) = p2 - p4
A(:, 3) = p3 - p4
!vol_tetrahedron = A(1,1) * (A(2,2)*A(3,3) - A(2,3)*A(3,2)) + &
! A(1,2) * (A(2,3)*A(3,1) - A(2,1)*A(3,3)) + &
! A(1,3) * (A(2,1)*A(3,2) - A(2,2)*A(3,1))
vol_tetrahedron = (p4(1) - p1(1))*((p2(2)-p1(2))*(p3(3)-p1(3)) - (p2(3)-p1(3))*(p3(2)-p1(2))) &
+ (p4(2) - p1(2))*((p2(3)-p1(3))*(p3(1)-p1(1)) - (p2(1)-p1(1))*(p3(3)-p1(3))) &
+ (p4(3) - p1(3))*((p2(1)-p1(1))*(p3(2)-p1(2)) - (p2(2)-p1(2))*(p3(1)-p1(1)))
vol_tetrahedron = -vol_tetrahedron / 6.
end function vol_tetrahedron