TRIMMESH Reduce memory footprint of a polygonal mesh. [V2, F2] = trimMesh(V, F) Unreferenced vertices are removed. Following functions are implemented only for numeric faces: Duplicate vertices are removed. Duplicate faces are removed. Example [V, F] = createIcosahedron; F(13:20, :) = []; [V2, F2] = trimMesh(V, F); figure; drawMesh(V2, F2) view(3); axis equal; axis([-1 1 -1 1 0 2]) See also meshes3d, clipMeshVertices
0001 function varargout = trimMesh(varargin) 0002 %TRIMMESH Reduce memory footprint of a polygonal mesh. 0003 % 0004 % [V2, F2] = trimMesh(V, F) 0005 % Unreferenced vertices are removed. 0006 % Following functions are implemented only for numeric faces: 0007 % Duplicate vertices are removed. 0008 % Duplicate faces are removed. 0009 % 0010 % Example 0011 % [V, F] = createIcosahedron; 0012 % F(13:20, :) = []; 0013 % [V2, F2] = trimMesh(V, F); 0014 % figure; drawMesh(V2, F2) 0015 % view(3); axis equal; 0016 % axis([-1 1 -1 1 0 2]) 0017 % 0018 % See also 0019 % meshes3d, clipMeshVertices 0020 0021 % ------ 0022 % Author: David Legland, oqilipo 0023 % E-mail: david.legland@inrae.fr 0024 % Created: 2014-08-01, using Matlab 8.3.0.532 (R2014a) 0025 % Copyright 2014-2024 INRA - Cepia Software Platform 0026 0027 % parse input data 0028 [vertices, faces] = parseMeshData(varargin{:}); 0029 0030 if isnumeric(faces) 0031 % Process meshes with faces given as Nf-by-3 or Nf-by-4 numeric arrays 0032 0033 % Delete duplicate vertices 0034 [tempVertices, tempFaces] = removeDuplicateVertices(vertices, faces); 0035 % Delete unindexed/unreferenced vertices 0036 [tempVertices2, tempFaces2] = removeUnreferencedVertices(tempVertices, tempFaces); 0037 % Delete duplicate faces 0038 [~, uniqueFaceIdx, ~] = unique(tempFaces2, 'rows'); 0039 duplicateFaceIdx = ~ismember(1:size(tempFaces2,1), uniqueFaceIdx); 0040 [vertices2, faces2] = removeMeshFaces(tempVertices2, tempFaces2, duplicateFaceIdx); 0041 0042 elseif iscell(faces) 0043 % Process meshes with faces given as cell array of row vectors 0044 0045 nVertices = size(vertices, 1); 0046 nFaces = length(faces); 0047 0048 % identify vertices referenced by at least one face 0049 vertexUsed = false(nVertices, 1); 0050 for iFace = 1:nFaces 0051 face = faces{iFace}; 0052 vertexUsed(face) = true; 0053 end 0054 vertices2 = vertices(vertexUsed, :); 0055 0056 % compute map from old index to new index 0057 inds = find(vertexUsed); 0058 newInds = zeros(nVertices, 1); 0059 for iIndex = 1:length(inds) 0060 newInds(inds(iIndex)) = iIndex; 0061 end 0062 0063 % change labels of vertices referenced by faces 0064 faces2 = cell(1, nFaces); 0065 for iFace = 1:nFaces 0066 faces2{iFace} = newInds(faces{iFace})'; 0067 end 0068 0069 else 0070 error('matGeom:trimMesh', ... 0071 'Unsupported representation for mesh faces'); 0072 end 0073 0074 % format output arguments 0075 varargout = formatMeshOutput(nargout, vertices2, faces2);