]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/contourc.m
update packages
[CreaPhase.git] / octave_packages / m / plot / contourc.m
1 ## Copyright (C) 2003-2012 Shai Ayal
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{c}, @var{lev}] =} contourc (@var{x}, @var{y}, @var{z}, @var{vn})
21 ## Compute isolines (contour lines) of the matrix @var{z}.
22 ## Parameters @var{x}, @var{y} and @var{vn} are optional.
23 ##
24 ## The return value @var{lev} is a vector of the contour levels.
25 ## The return value @var{c} is a 2 by @var{n} matrix containing the
26 ## contour lines in the following format
27 ##
28 ## @example
29 ## @group
30 ## @var{c} = [lev1, x1, x2, @dots{}, levn, x1, x2, ...
31 ##      len1, y1, y2, @dots{}, lenn, y1, y2, @dots{}]
32 ## @end group
33 ## @end example
34 ##
35 ## @noindent
36 ## in which contour line @var{n} has a level (height) of @var{levn} and
37 ## length of @var{lenn}.
38 ##
39 ## If @var{x} and @var{y} are omitted they are taken as the row/column
40 ## index of @var{z}.  @var{vn} is either a scalar denoting the number of lines
41 ## to compute or a vector containing the values of the lines.  If only one
42 ## value is wanted, set @code{@var{vn} = [val, val]};
43 ## If @var{vn} is omitted it defaults to 10.
44 ##
45 ## For example:
46 ##
47 ## @example
48 ## @group
49 ## x = 0:2;
50 ## y = x;
51 ## z = x' * y;
52 ## contourc (x, y, z, 2:3)
53 ##    @result{}   2.0000   2.0000   1.0000   3.0000   1.5000   2.0000
54 ##         2.0000   1.0000   2.0000   2.0000   2.0000   1.5000
55 ## @end group
56 ## @end example
57 ## @seealso{contour}
58 ## @end deftypefn
59
60 ## Author: Shai Ayal <shaiay@users.sourceforge.net>
61
62 function [cout, lev] = contourc (varargin)
63
64   if (nargin == 1)
65     vn = 10;
66     z = varargin{1};
67     [nr, nc] = size (z);
68     x = 1:nc;
69     y = 1:nr;
70   elseif (nargin == 2)
71     vn = varargin{2};
72     z = varargin{1};
73     [nr, nc] = size (z);
74     x = 1:nc;
75     y = 1:nr;
76   elseif (nargin == 3)
77     vn = 10;
78     x = varargin{1};
79     y = varargin{2};
80     z = varargin{3};
81   elseif (nargin == 4)
82     vn = varargin{4};
83     x = varargin{1};
84     y = varargin{2};
85     z = varargin{3};
86   else
87     print_usage ();
88   endif
89
90   if (!ismatrix (z) || isvector (z) || isscalar (z))
91     error ("contourc: Z argument must be a matrix");
92   endif
93
94   if (isscalar (vn))
95     vv = linspace (min (z(:)), max (z(:)), vn+2)(2:end-1);
96   else
97     vv = unique (sort (vn));
98   endif
99
100   if (isvector (x) && isvector (y))
101     c = __contourc__ (x(:)', y(:)', z, vv);
102   else
103     ## Indexes x,y for the purpose of __contourc__.
104     ii = 1:size (z,2);
105     jj = 1:size (z,1);
106
107     ## Now call __contourc__ for the real work...
108     c = __contourc__ (ii, jj, z, vv);
109
110     ## Map the contour lines from index space (i,j) back
111     ## to the original grid (x,y)
112     i = 1;
113
114     while (i < size (c,2))
115       clen = c(2, i);
116       ind = i + [1 : clen];
117
118       ci = c(1, ind);
119       cj = c(2,ind);
120
121       ## due to rounding errors some elements of ci and cj
122       ## can fall out of the range of ii and jj and interp2 would
123       ## return NA for those values.
124       ## The permitted range is enforced here:
125
126       ci = max (ci, 1); ci = min (ci, size (z, 2));
127       cj = max (cj, 1); cj = min (cj, size (z, 1));
128
129       c(1, ind) = interp2 (ii, jj, x, ci, cj);
130       c(2, ind) = interp2 (ii, jj, y, ci, cj);
131
132       i = i + clen + 1;
133     endwhile
134   endif
135
136   if (nargout > 0)
137     cout = c;
138     lev = vv;
139   endif
140
141 endfunction
142
143 %!test
144 %! x = 0:2;
145 %! y = x;
146 %! z = x' * y;
147 %! [c_actual, lev_actual]= contourc (x, y, z, 2:3);
148 %! c_expected = [2, 1, 1, 2, 2, 3, 1.5, 2; 4, 2, 2, 1, 1, 2, 2, 1.5];
149 %! lev_expected = [2 3];
150 %! assert (c_actual, c_expected, eps)
151 %! assert (lev_actual, lev_expected, eps)
152
153