(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, meshComplement
0001 function vol = meshVolume(varargin) 0002 % (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, meshComplement 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 INRA - Cepia Software Platform. 0030 0031 % HISTORY 0032 % 2013-08-16 speed improvement by Sven Holcombe 0033 0034 % parse input 0035 [vertices, faces] = parseMeshData(varargin{:}); 0036 0037 % ensure mesh has triangle faces 0038 faces = triangulateFaces(faces); 0039 0040 % initialize an array of volume 0041 nFaces = size(faces, 1); 0042 vols = zeros(nFaces, 1); 0043 0044 % Shift all vertices to the mesh centroid 0045 vertices = bsxfun(@minus, vertices, mean(vertices,1)); 0046 0047 % compute volume of each tetraedron 0048 for i = 1:nFaces 0049 % consider the tetrahedron formed by face and mesh centroid 0050 tetra = vertices(faces(i, :), :); 0051 0052 % volume of current tetrahedron 0053 vols(i) = det(tetra) / 6; 0054 end 0055 0056 vol = sum(vols);