REMOVEMULTIPLEVERTICES Remove multiple vertices of a polygon or polyline. POLY2 = removeMultipleVertices(POLY, EPS) Remove adjacent vertices that are closer than the distance EPS to each other and merge them to a unique vertex. POLY2 = removeMultipleVertices(POLY, EPS, CLOSED) If CLOSED is true, also check if first and last vertices need to be merged. If not specified, CLOSED is false. Example poly = [10 10; 20 10;20 10;20 20;10 20; 10 10]; poly2 = removeMultipleVertices(poly, true); size(poly2, 1) ans = 4 See also polygons2d, mergeClosePoints
0001 function poly = removeMultipleVertices(poly, varargin) 0002 %REMOVEMULTIPLEVERTICES Remove multiple vertices of a polygon or polyline. 0003 % 0004 % POLY2 = removeMultipleVertices(POLY, EPS) 0005 % Remove adjacent vertices that are closer than the distance EPS to each 0006 % other and merge them to a unique vertex. 0007 % 0008 % POLY2 = removeMultipleVertices(POLY, EPS, CLOSED) 0009 % If CLOSED is true, also check if first and last vertices need to be 0010 % merged. If not specified, CLOSED is false. 0011 % 0012 % Example 0013 % poly = [10 10; 20 10;20 10;20 20;10 20; 10 10]; 0014 % poly2 = removeMultipleVertices(poly, true); 0015 % size(poly2, 1) 0016 % ans = 0017 % 4 0018 % 0019 % See also 0020 % polygons2d, mergeClosePoints 0021 0022 % ------ 0023 % Author: David Legland 0024 % e-mail: david.legland@grignon.inra.fr 0025 % Created: 2013-10-04, using Matlab 7.9.0.529 (R2009b) 0026 % Copyright 2013 INRA - Cepia Software Platform. 0027 0028 % default values 0029 eps = 1e-14; 0030 closed = false; 0031 0032 % process input options 0033 while ~isempty(varargin) 0034 var = varargin{1}; 0035 if islogical(var) 0036 closed = var; 0037 elseif isnumeric(var) 0038 eps = var; 0039 else 0040 error('MatGeom:removeMultipleVertices:IllegalArgument',... 0041 'Can not interpret optional argument'); 0042 end 0043 varargin(1) = []; 0044 end 0045 0046 % distance between adjacent vertices 0047 dist = sqrt(sum((poly(2:end,:) - poly(1:end-1,:)).^2, 2)); 0048 multi = dist < eps; 0049 0050 % process extremities 0051 if closed 0052 dist = sqrt(sum((poly(end,:) - poly(1,:)).^2, 2)); 0053 multi = [multi ; dist < eps]; 0054 else 0055 multi = [multi ; false]; 0056 end 0057 0058 % remove multiple vertices 0059 poly(multi, :) = []; 0060