1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn{Function File} {[@var{xs}, @var{ys}] =} adresamp2 (@var{x}, @var{y}, @var{n}, @var{eps})
18 ## Perform an adaptive resampling of a planar curve.
19 ## The arrays @var{x} and @var{y} specify x and y coordinates of the points of the curve.
20 ## On return, the same curve is approximated by @var{xs}, @var{ys} that have length @var{n}
21 ## and the angles between successive segments are approximately equal.
24 ## Author : Jaroslav Hajek <highegg@gmail.com>
26 function [xs, ys] = adresamp2 (x, y, n, eps)
27 if (! isvector (x) || ! size_equal (x, y) || ! isscalar (n) \
40 dx = diff (x); dy = diff (y);
47 d2x = deriv2 (dx, ds);
48 d2y = deriv2 (dy, ds);
50 k = abs (d2x .* dy - d2y .* dx);
53 k = max (k, eps*max (k));
55 # cumulative integrals
57 t = cumsum ([0; ds .* k]);
58 # generate sample points
59 i = linspace (0, t(end), n);
64 xs = interp1 (t, x, i);
65 ys = interp1 (t, y, i);
68 # calculates second derivatives from first (non-uniform intervals), using local
69 # quadratic approximations.
70 function d2x = deriv2 (dx, dt)
73 d2x = diff (dx) ./ (dt(1:n-1) + dt(2:n));
74 d2x = [2*d2x(1); d2x(1:n-2) + d2x(2:n-1); 2*d2x(n-1)];
81 %! R = 2; r = 3; d = 1.5;
82 %! th = linspace (0, 2*pi, 1000);
83 %! x = (R-r) * cos (th) + d*sin ((R-r)/r * th);
84 %! y = (R-r) * sin (th) + d*cos ((R-r)/r * th);
85 %! x += 0.3*exp (-(th-0.8*pi).^2);
86 %! y += 0.4*exp (-(th-0.9*pi).^2);
88 %! [xs, ys] = adresamp2 (x, y, 40);
89 %! plot (x, y, "-", xs, ys, "*");
90 %! title ("adaptive resampling")