Home > matGeom > meshes3d > removeMeshVertices.m

removeMeshVertices

PURPOSE ^

REMOVEMESHVERTICES Remove vertices and associated faces from a mesh.

SYNOPSIS ^

function varargout = removeMeshVertices(vertices, faces, indsToRemove)

DESCRIPTION ^

REMOVEMESHVERTICES Remove vertices and associated faces from a mesh.

   [V2, F2] = removeMeshVertices(VERTS, FACES, VERTINDS)
   Removes the vertices specified by the vertex indices VERTINDS, and
   remove the faces containing one of the removed vertices.

   Example
     % remove some vertices from a soccerball polyhedron
     [v, f] = createSoccerBall; 
     plane = createPlane([.6 0 0], [1 0 0]);
     indAbove = find(~isBelowPlane(v, plane));
     [v2, f2] = removeMeshVertices(v, f, indAbove);
     drawMesh(v2, f2);
     axis equal; hold on;

   See also
     meshes3d, trimMesh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = removeMeshVertices(vertices, faces, indsToRemove)
0002 %REMOVEMESHVERTICES Remove vertices and associated faces from a mesh.
0003 %
0004 %   [V2, F2] = removeMeshVertices(VERTS, FACES, VERTINDS)
0005 %   Removes the vertices specified by the vertex indices VERTINDS, and
0006 %   remove the faces containing one of the removed vertices.
0007 %
0008 %   Example
0009 %     % remove some vertices from a soccerball polyhedron
0010 %     [v, f] = createSoccerBall;
0011 %     plane = createPlane([.6 0 0], [1 0 0]);
0012 %     indAbove = find(~isBelowPlane(v, plane));
0013 %     [v2, f2] = removeMeshVertices(v, f, indAbove);
0014 %     drawMesh(v2, f2);
0015 %     axis equal; hold on;
0016 %
0017 %   See also
0018 %     meshes3d, trimMesh
0019  
0020 % ------
0021 % Author: David Legland
0022 % e-mail: david.legland@nantes.inra.fr
0023 % Created: 2016-02-03,    using Matlab 8.6.0.267246 (R2015b)
0024 % Copyright 2016 INRA - Cepia Software Platform.
0025 
0026 % parse inputs
0027 if nargin == 2
0028     indsToRemove = faces;
0029     [vertices, faces] = parseMeshData(vertices);
0030 end
0031 
0032 % create array of indices to keep
0033 nVertices = size(vertices, 1);
0034 newInds = (1:nVertices)';
0035 newInds(indsToRemove) = [];
0036 
0037 % create new vertex array
0038 vertices2 = vertices(newInds, :);
0039 
0040 % compute map from old indices to new indices
0041 oldNewMap = zeros(nVertices, 1);
0042 for iIndex = 1:size(newInds, 1)
0043    oldNewMap(newInds(iIndex)) = iIndex; 
0044 end
0045 
0046 % change labels of vertices referenced by faces
0047 if isnumeric(faces)
0048     faces2 = oldNewMap(faces);
0049     if size(faces2,2)==1; faces2=faces2'; end
0050     % keep only faces with valid vertices
0051     faces2 = faces2(sum(faces2 == 0, 2) == 0, :);
0052 elseif iscell(faces)
0053     faces2 = cell(1, length(faces));
0054     for iFace = 1:length(faces)
0055          newFace = oldNewMap(faces{iFace});
0056          % add the new face only if all vertices are valid
0057          if ~any(newFace == 0)
0058              faces2{iFace} = newFace;
0059          end
0060     end
0061     
0062     % remove empty faces
0063     faces2 = faces2(~cellfun(@isempty, faces2));
0064 end
0065 
0066 % format output arguments
0067 varargout = formatMeshOutput(nargout, vertices2, faces2);

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019