-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh_volume.m
31 lines (26 loc) · 874 Bytes
/
mesh_volume.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function volume = mesh_volume(V, F)
% MESH_VOLUME computes the volume of a 3D mesh
%
% Inputs:
% V - #V by 3 matrix of vertex coordinates
% F - #F by 3 matrix of face indices into V
%
% Output:
% volume - scalar volume of the mesh
% Initialize volume
volume = 0;
% Loop over all faces
for i = 1:size(F, 1)
% Get vertices of the current face
v1 = V(F(i, 1), :);
v2 = V(F(i, 2), :);
v3 = V(F(i, 3), :);
% Compute the volume of the tetrahedron formed by v1, v2, v3, and the origin
% This is done using the scalar triple product
tet_volume = dot(v1, cross(v2, v3)) / 6.0;
% Accumulate the volume
volume = volume + tet_volume;
end
% Take absolute value of the volume (since it can be negative)
volume = abs(volume);
end