]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/adresamp2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / adresamp2.m
1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
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.
22 ## @end deftypefn
23
24 ## Author : Jaroslav Hajek <highegg@gmail.com>
25
26 function [xs, ys] = adresamp2 (x, y, n, eps)
27   if (! isvector (x) || ! size_equal (x, y) || ! isscalar (n) \
28       || ! isscalar (eps))
29     print_usage ();
30   endif
31
32   if (rows (x) == 1)
33     rowvec = true;
34     x = x.'; y = y.';
35   else
36     rowvec = false;
37   endif
38
39   # first differences
40   dx = diff (x); dy = diff (y);
41   # arc lengths
42   ds = hypot (dx, dy);
43   # derivatives
44   dx = dx ./ ds;
45   dy = dy ./ ds;
46   # second derivatives
47   d2x = deriv2 (dx, ds);
48   d2y = deriv2 (dy, ds);
49   # curvature
50   k = abs (d2x .* dy - d2y .* dx);
51   # curvature cut-off
52   if (eps > 0)
53     k = max (k, eps*max (k));
54   endif
55   # cumulative integrals
56   s = cumsum ([0; ds]);
57   t = cumsum ([0; ds .* k]);
58   # generate sample points
59   i = linspace (0, t(end), n);
60   if (! rowvec)
61     i = i.';
62   endif
63   # and resample
64   xs = interp1 (t, x, i);
65   ys = interp1 (t, y, i);
66 endfunction
67
68 # calculates second derivatives from first (non-uniform intervals), using local
69 # quadratic approximations.
70 function d2x = deriv2 (dx, dt)
71   n = length (dt);
72   if (n >= 2)
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)];
75   else
76     d2x = zeros (n, 1);
77   endif
78 endfunction
79
80 %!demo
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); 
87 %! 
88 %! [xs, ys] = adresamp2 (x, y, 40);
89 %! plot (x, y, "-", xs, ys, "*");
90 %! title ("adaptive resampling")