]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/polygons2d/parametrize.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / polygons2d / parametrize.m
1 %% Copyright (C) 2003-2011 David Legland <david.legland@grignon.inra.fr>
2 %% Copyright (C) 2012 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 %% All rights reserved.
4 %%
5 %% Redistribution and use in source and binary forms, with or without
6 %% modification, are permitted provided that the following conditions are met:
7 %%
8 %%     1 Redistributions of source code must retain the above copyright notice,
9 %%       this list of conditions and the following disclaimer.
10 %%     2 Redistributions in binary form must reproduce the above copyright
11 %%       notice, this list of conditions and the following disclaimer in the
12 %%       documentation and/or other materials provided with the distribution.
13 %%
14 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
15 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 %% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 %% ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 %% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 %% SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 %% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 %% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 %% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 %%
25 %% The views and conclusions contained in the software and documentation are
26 %% those of the authors and should not be interpreted as representing official
27 %% policies, either expressed or implied, of the copyright holders.
28
29 %% -*- texinfo -*-
30 %% @deftypefn {Function File} {@var{par} = } parametrize (@var{poly})
31 %% @deftypefnx {Function File} {@var{par} = } parametrize (@var{px},@var{py})
32 %% @deftypefnx {Function File} {@var{par} = } parametrize (@dots{},@var{normalize})
33 %%
34 %%  Parametrization of a curve, based on edges length
35 %%
36 %%  Returns a parametrization of the curve defined by the serie of points,
37 %% based on euclidean distance between two consecutive points.
38 %% POLY is a N-by-2 array, representing coordinates of vertices. The
39 %% result PAR is N-by-1, and contains the cumulative length of edges until
40 %% corresponding vertex. The function also accepts the polygon as two inputs
41 %% @var{px} and @var{py} representinx coordinates x and y respectively.
42 %%  When optional argument @var{normalize} is non-empty @var{par} is rescaled
43 %% such that the last element equals 1, i.e. @code{@var{par}(end)==1}.
44 %%
45 %%  Example
46 %% @example
47 %%     % Parametrize a circle approximation
48 %%     poly = circleToPolygon([0 0 1], 200);
49 %%     p = parametrize(poly);
50 %%     p(end)
51 %%     ans =
52 %%         6.2829
53 %%     p = parametrize(poly,'norm');
54 %%     p(end)
55 %%     ans =
56 %%         1
57 %%     p = parametrize(poly,true);
58 %%     p(end)
59 %%     ans =
60 %%         1
61 %% @end example
62 %%
63 %%  @seealso{polygons2d, polylineLength}
64 %% @end deftypefn
65 function par = parametrize(varargin)
66   %% Process inputs
67
68   % extract vertex coordinates
69   if size(varargin{1}, 2) > 1
70       % vertices in a single array
71       pts = varargin{1};
72       normalize = numel(varargin) > 1;
73   elseif size(varargin{1}, 2) == 1 && numel(varargin) >= 2
74       % points as separate arrays
75       pts = [varargin{1} varargin{2}];
76       normalize = numel(varargin) > 2;
77   end
78
79   %% Parametrize polyline
80
81   % compute cumulative sum of euclidean distances between consecutive
82   % vertices, setting distance of first vertex to 0.
83   if size(pts, 2) == 2
84       % process points in 2D
85       par = [0 ; cumsum(hypot(diff(pts(:,1)), diff(pts(:,2))))];
86   else
87       % process points in arbitrary dimension
88       par = [0 ; cumsum(sqrt(sum(diff(pts).^2, 2)))];
89   end
90
91   % eventually rescale between 0 and 1
92   if normalize
93       par = par / par(end);
94   end
95
96 endfunction