PARAMETRIZE Parametrization of a polyline, based on edges lengths. PAR = parametrize(POLY); Returns a parametrization of the curve defined by the serie of points, based on euclidean distance between two consecutive points. POLY is a N-by-2 array, representing coordinates of vertices. The result PAR is N-by-1, and contains the cumulative length of edges until corresponding vertex. PAR = parametrize(PX, PY); is the same, but specify points coordinates in separate column vectors. PAR = parametrize(..., 'normalize', 1); PAR = parametrize(..., 'normalize', true); Rescales the result such that the last element of PAR is 1. Example % Parametrize a circle approximation poly = circleToPolygon([0 0 1], 200); p = parametrize(poly); p(end) ans = 6.2829 See also: polygons2d, polylineLength --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 06/04/2003.
0001 function par = parametrize(varargin) 0002 %PARAMETRIZE Parametrization of a polyline, based on edges lengths. 0003 % 0004 % PAR = parametrize(POLY); 0005 % Returns a parametrization of the curve defined by the serie of points, 0006 % based on euclidean distance between two consecutive points. 0007 % POLY is a N-by-2 array, representing coordinates of vertices. The 0008 % result PAR is N-by-1, and contains the cumulative length of edges until 0009 % corresponding vertex. 0010 % 0011 % PAR = parametrize(PX, PY); 0012 % is the same, but specify points coordinates in separate column vectors. 0013 % 0014 % PAR = parametrize(..., 'normalize', 1); 0015 % PAR = parametrize(..., 'normalize', true); 0016 % Rescales the result such that the last element of PAR is 1. 0017 % 0018 % Example 0019 % % Parametrize a circle approximation 0020 % poly = circleToPolygon([0 0 1], 200); 0021 % p = parametrize(poly); 0022 % p(end) 0023 % ans = 0024 % 6.2829 0025 % 0026 % See also: 0027 % polygons2d, polylineLength 0028 % 0029 % --------- 0030 % author : David Legland 0031 % INRA - TPV URPOI - BIA IMASTE 0032 % created the 06/04/2003. 0033 % 0034 0035 0036 %% Process inputs 0037 0038 % extract vertex coordinates 0039 if size(varargin{1}, 2) > 1 0040 % vertices in a single array 0041 pts = varargin{1}; 0042 varargin(1) = []; 0043 0044 elseif length(varargin) == 2 0045 % points as separate arrays 0046 pts = [varargin{1} varargin{2}]; 0047 varargin(1:2) = []; 0048 0049 end 0050 0051 % by default, do not normalize 0052 normalize = false; 0053 0054 % extract options 0055 while length(varargin) > 1 0056 param = varargin{1}; 0057 switch lower(param) 0058 case 'normalize' 0059 normalize = varargin{2}; 0060 otherwise 0061 error('Unknown parameter name: %s', param); 0062 end 0063 varargin(1:2) = []; 0064 end 0065 0066 0067 %% Parametrize polyline 0068 0069 % compute cumulative sum of euclidean distances between consecutive 0070 % vertices, setting distance of first vertex to 0. 0071 if size(pts, 2) == 2 0072 % process points in 2D 0073 par = [0 ; cumsum(hypot(diff(pts(:,1)), diff(pts(:,2))))]; 0074 else 0075 % process points in arbitrary dimension 0076 par = [0 ; cumsum(sqrt(sum(diff(pts).^2, 2)))]; 0077 end 0078 0079 % eventually rescale between 0 and 1 0080 if normalize 0081 par = par / par(end); 0082 end 0083