]> Creatis software - CreaPhase.git/blob - octave_packages/m/geometry/convhull.m
a63db86956fdb3477d2223c733046c09b9572ea9
[CreaPhase.git] / octave_packages / m / geometry / convhull.m
1 ## Copyright (C) 2000-2012 Kai Habel
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {@var{H} =} convhull (@var{x}, @var{y})
21 ## @deftypefnx {Function File} {@var{H} =} convhull (@var{x}, @var{y}, @var{options})
22 ## Compute the convex hull of the set of points defined by the
23 ## vectors @var{x} and @var{y}.  The hull @var{H} is an index vector into
24 ## the set of points and specifies which points form the enclosing hull.
25 ##
26 ## An optional third argument, which must be a string or cell array of strings,
27 ## contains options passed to the underlying qhull command.
28 ## See the documentation for the Qhull library for details
29 ## @url{http://www.qhull.org/html/qh-quick.htm#options}.
30 ## The default option is @code{@{"Qt"@}}.
31 ##
32 ## If @var{options} is not present or @code{[]} then the default arguments are
33 ## used.  Otherwise, @var{options} replaces the default argument list. 
34 ## To append user options to the defaults it is necessary to repeat the 
35 ## default arguments in @var{options}.  Use a null string to pass no arguments.
36 ##
37 ## @seealso{convhulln, delaunay, voronoi}
38 ## @end deftypefn
39
40 ## Author: Kai Habel <kai.habel@gmx.de>
41
42 function H = convhull (x, y, options)
43
44   if (nargin != 2 && nargin != 3)
45     print_usage ();
46   endif
47
48   if (! (isvector (x) && isvector (y) && length (x) == length (y))
49       && ! size_equal (x, y))
50     error ("convhull: X and Y must be the same size");
51   elseif (nargin == 3 && ! (ischar (options) || iscellstr (options)))
52     error ("convhull: OPTIONS must be a string or cell array of strings");
53   endif
54
55   if (nargin == 2)
56     i = convhulln ([x(:), y(:)]);
57   else
58     i = convhulln ([x(:), y(:)], options);
59   endif
60
61   n = rows (i);
62   i = i'(:);
63   H = zeros (n + 1, 1);
64
65   H(1) = i(1);
66   next_i = i(2);
67   i(2) = 0;
68   for k = 2:n
69     next_idx = find (i == next_i);
70
71     if (rem (next_idx, 2) == 0)
72       H(k) = i(next_idx);
73       next_i = i(next_idx - 1);
74       i(next_idx - 1) = 0;
75     else
76       H(k) = i(next_idx);
77       next_i = i(next_idx + 1);
78       i(next_idx + 1) = 0;
79     endif
80   endfor
81
82   H(n + 1) = H(1);
83
84 endfunction
85
86
87 %!demo
88 %! x = -3:0.05:3;
89 %! y = abs (sin (x));
90 %! k = convhull (x, y);
91 %! plot (x(k),y(k),"r-;convex hull;", x,y,"b+;points;");
92 %! axis ([-3.05, 3.05, -0.05, 1.05]);
93
94 %!testif HAVE_QHULL
95 %! x = -3:0.5:3;
96 %! y = abs (sin (x));
97 %! assert (convhull (x, y), [1;7;13;12;11;10;4;3;2;1])
98
99 %% FIXME: Need input validation tests
100