RESAMPLEPOLYLINEBYLENGTH Resample a polyline with a fixed sampling step. RES = resamplePolyline(POLY, STEP) Resample the input polyline POLY by distributing new vertices on the original polyline such that the (curvilinear) distance between the new vertices is approximately equal to STEP. Example poly = [0 10;0 0;10 0; 10 10; 20 10;20 0]; figure; drawPolyline(poly, 'k'); poly2 = resamplePolylineByLength(poly, 4); hold on; drawPolyline(poly2, 'm'); drawPoint(poly2, 'mo'); axis equal; axis([-10 30 -10 20]); legend('Original polyline', 'Resampled polyline'); See also polygons2d, drawPolyline, resamplePolygon
0001 function poly2 = resamplePolylineByLength(poly, step) 0002 %RESAMPLEPOLYLINEBYLENGTH Resample a polyline with a fixed sampling step. 0003 % 0004 % RES = resamplePolyline(POLY, STEP) 0005 % Resample the input polyline POLY by distributing new vertices on the 0006 % original polyline such that the (curvilinear) distance between the new 0007 % vertices is approximately equal to STEP. 0008 % 0009 % Example 0010 % poly = [0 10;0 0;10 0; 10 10; 20 10;20 0]; 0011 % figure; drawPolyline(poly, 'k'); 0012 % poly2 = resamplePolylineByLength(poly, 4); 0013 % hold on; 0014 % drawPolyline(poly2, 'm'); 0015 % drawPoint(poly2, 'mo'); 0016 % axis equal; axis([-10 30 -10 20]); 0017 % legend('Original polyline', 'Resampled polyline'); 0018 % 0019 % See also 0020 % polygons2d, drawPolyline, resamplePolygon 0021 % 0022 0023 % ------ 0024 % Author: David Legland 0025 % e-mail: david.legland@inra.fr 0026 % Created: 2011-12-09, using Matlab 7.9.0.529 (R2009b) 0027 % Copyrightf 2011 INRA - Cepia Software Platform. 0028 0029 % parametrisation of the curve 0030 s = parametrize(poly); 0031 0032 % compute the number of points for sampling the polygon 0033 % (equal to the number of segments plus one) 0034 Lmax = s(end); 0035 n = round(Lmax / step) + 1; 0036 0037 % distribute N points equally spaced 0038 pos = linspace(0, Lmax, n); 0039 0040 poly2 = zeros(n, size(poly, 2)); 0041 for i = 1:n 0042 % index of surrounding vertices before and after 0043 ind0 = find(s <= pos(i), 1, 'last'); 0044 ind1 = find(s >= pos(i), 1, 'first'); 0045 0046 if ind0 == ind1 0047 % get position of a vertex in input polyline 0048 poly2(i, :) = poly(ind0, :); 0049 continue; 0050 end 0051 0052 % position of surrounding vertices 0053 pt0 = poly(ind0, :); 0054 pt1 = poly(ind1, :); 0055 0056 % weights associated to each neighbor 0057 l0 = pos(i) - s(ind0); 0058 l1 = s(ind1) - pos(i); 0059 0060 % linear interpolation of neighbor positions 0061 if (l0 + l1) > Lmax * 1e-12 0062 poly2(i, :) = (pt0 * l1 + pt1 * l0) / (l0 + l1); 0063 else 0064 % if neighbors are too close, do not use interpolation 0065 poly2(i, :) = pt0; 0066 end 0067 end