MESHVOLUME (Signed) volume of the space enclosed by a polygonal mesh. V = meshVolume(VERTS, FACES) Computes the volume of the space enclosed by the polygonal mesh represented by vertices VERTS (as a Nv-by-3 array of cooridnates) and the array of faces FACES (either as a Nf-by-3 array of vertex indices, or as a cell array of arrays of vertex indices). The volume is computed as the sum of the signed volumes of tetrahedra formed by triangular faces and the centroid of the mesh. Faces need to be oriented such that normal points outwards the mesh. See: http://stackoverflow.com/questions/1838401/general-formula-to-calculate-polyhedron-volume Example % computes the volume of a unit cube (should be equal to 1...) [v f] = createCube; meshVolume(v, f) ans = 1 See also meshes3d, meshSurfaceArea, tetrahedronVolume, meshCentroid
0001 function vol = meshVolume(varargin) 0002 %MESHVOLUME (Signed) volume of the space enclosed by a polygonal mesh. 0003 % 0004 % V = meshVolume(VERTS, FACES) 0005 % Computes the volume of the space enclosed by the polygonal mesh 0006 % represented by vertices VERTS (as a Nv-by-3 array of cooridnates) and 0007 % the array of faces FACES (either as a Nf-by-3 array of vertex indices, 0008 % or as a cell array of arrays of vertex indices). 0009 % 0010 % The volume is computed as the sum of the signed volumes of tetrahedra 0011 % formed by triangular faces and the centroid of the mesh. Faces need to 0012 % be oriented such that normal points outwards the mesh. See: 0013 % http://stackoverflow.com/questions/1838401/general-formula-to-calculate-polyhedron-volume 0014 % 0015 % Example 0016 % % computes the volume of a unit cube (should be equal to 1...) 0017 % [v f] = createCube; 0018 % meshVolume(v, f) 0019 % ans = 0020 % 1 0021 % 0022 % See also 0023 % meshes3d, meshSurfaceArea, tetrahedronVolume, meshCentroid 0024 0025 % ------ 0026 % Author: David Legland 0027 % E-mail: david.legland@inrae.fr 0028 % Created: 2012-10-01, using Matlab 7.9.0.529 (R2009b) 0029 % Copyright 2012-2024 INRA - Cepia Software Platform 0030 0031 % parse input 0032 [vertices, faces] = parseMeshData(varargin{:}); 0033 0034 % ensure mesh has triangle faces 0035 faces = triangulateFaces(faces); 0036 0037 % initialize an array of volume 0038 nFaces = size(faces, 1); 0039 vols = zeros(nFaces, 1); 0040 0041 % Shift all vertices to the mesh centroid 0042 vertices = bsxfun(@minus, vertices, mean(vertices,1)); 0043 0044 % compute volume of each tetraedron 0045 for i = 1:nFaces 0046 % consider the tetrahedron formed by face and mesh centroid 0047 tetra = vertices(faces(i, :), :); 0048 0049 % volume of current tetrahedron 0050 vols(i) = det(tetra) / 6; 0051 end 0052 0053 vol = sum(vols);