]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/surface.m
update packages
[CreaPhase.git] / octave_packages / m / plot / surface.m
1 ## Copyright (C) 1993-2012 John W. Eaton
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} {} surface (@var{x}, @var{y}, @var{z}, @var{c})
21 ## @deftypefnx {Function File} {} surface (@var{x}, @var{y}, @var{z})
22 ## @deftypefnx {Function File} {} surface (@var{z}, @var{c})
23 ## @deftypefnx {Function File} {} surface (@var{z})
24 ## @deftypefnx {Function File} {} surface (@dots{}, @var{prop}, @var{val})
25 ## @deftypefnx {Function File} {} surface (@var{h}, @dots{})
26 ## @deftypefnx {Function File} {@var{h} =} surface (@dots{})
27 ## Plot a surface graphic object given matrices @var{x}, and @var{y} from
28 ## @code{meshgrid} and a matrix @var{z} corresponding to the @var{x} and
29 ## @var{y} coordinates of the surface.  If @var{x} and @var{y} are vectors,
30 ## then a typical vertex is (@var{x}(j), @var{y}(i), @var{z}(i,j)).  Thus,
31 ## columns of @var{z} correspond to different @var{x} values and rows of
32 ## @var{z} correspond to different @var{y} values.  If @var{x} and @var{y}
33 ## are missing, they are constructed from size of the matrix @var{z}.
34 ##
35 ## Any additional properties passed are assigned to the surface.
36 ## 
37 ## The optional return value @var{h} is a graphics handle to the created
38 ## surface object.
39 ## @seealso{surf, mesh, patch, line}
40 ## @end deftypefn
41
42 ## Author: jwe
43
44 function retval = surface (varargin)
45
46   [h, varargin] = __plt_get_axis_arg__ ("surface", varargin{:});
47
48   oldh = gca ();
49   unwind_protect
50     axes (h);
51     [tmp, bad_usage] = __surface__ (h, varargin{:});
52   unwind_protect_cleanup
53     axes (oldh);
54   end_unwind_protect
55
56   if (bad_usage)
57     print_usage ();
58   endif
59
60   if (nargout > 0)
61     retval = tmp;
62   endif
63
64 endfunction
65
66 function [h, bad_usage] = __surface__ (ax, varargin)
67
68   bad_usage = false;
69   h = 0;
70   firststring = nargin;
71   for i = 2 : nargin
72     if (ischar (varargin{i - 1}))
73       firststring = i - 1;
74       break;
75     endif
76   endfor
77
78   if (firststring > 5)
79     bad_usage = true;
80   elseif (firststring == 5)
81     x = varargin{1};
82     y = varargin{2};
83     z = varargin{3};
84     c = varargin{4};
85
86     [z_nr, z_nc] = size (z);
87     [c_nr, c_nc, c_np] = size (c);
88     if (! (z_nr == c_nr && z_nc == c_nc && (c_np == 1 || c_np == 3)))
89       error ("surface: Z and C must have the same size");
90     endif
91
92     if (isvector (x) && isvector (y) && ismatrix (z))
93       if (rows (z) == length (y) && columns (z) == length (x))
94         x = x(:)';
95         y = y(:);
96       else
97         error ("surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)");
98       endif
99     elseif (ismatrix (x) && ismatrix (y) && ismatrix (z))
100       if (! size_equal (x, y, z))
101         error ("surface: X, Y, and Z must have the same dimensions");
102       endif
103     else
104       error ("surface: X and Y must be vectors and Z must be a matrix");
105     endif
106   elseif (firststring == 4)
107     x = varargin{1};
108     y = varargin{2};
109     z = varargin{3};
110     c = z;
111     if (isvector (x) && isvector (y) && ismatrix (z))
112       if (rows (z) == length (y) && columns (z) == length (x))
113         x = x(:)';
114         y = y(:);
115       else
116         error ("surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)");
117       endif
118     elseif (ismatrix (x) && ismatrix (y) && ismatrix (z))
119       if (! size_equal (x, y, z))
120         error ("surface: X, Y, and Z must have the same dimensions");
121       endif
122     else
123       error ("surface: X and Y must be vectors and Z must be a matrix");
124     endif
125   elseif (firststring == 3)
126     z = varargin{1};
127     c = varargin{2};
128     if (ismatrix (z) && !isvector (z) && !isscalar (z))
129       [nr, nc] = size (z);
130       x = 1:nc;
131       y = (1:nr)';
132     else
133       error ("surface: Z argument must be a matrix");
134     endif
135   elseif (firststring == 2)
136     z = varargin{1};
137     c = z;
138     if (ismatrix (z) && !isvector (z) && !isscalar (z))
139       [nr, nc] = size (z);
140       x = 1:nc;
141       y = (1:nr)';
142     else
143       error ("surface: Z argument must be a matrix");
144     endif
145   elseif (firststring == 1)
146     x = 1:3;
147     y = (x).';
148     c = z = eye(3);
149   else
150     bad_usage = true;
151   endif
152
153   if (! bad_usage)
154     ## Make a default surface object.
155     other_args = {};
156     if (firststring < nargin)
157       other_args = varargin(firststring:end);
158     endif
159     h = __go_surface__ (ax, "xdata", x, "ydata", y, "zdata", z, "cdata", c,
160                         other_args{:});
161
162     if (! ishold ())
163       set (ax, "view", [0, 90], "box", "off");
164     endif
165   endif
166
167 endfunction
168
169 ## Functional tests for surface() are in surf.m, surfc.m, surfl.m, and pcolor.m
170
171 %!test
172 %! hf = figure ("visible", "off");
173 %! unwind_protect
174 %!   h = surface;
175 %!   assert (findobj (hf, "type", "surface"), h);
176 %!   assert (get (h, "xdata"), 1:3, eps);
177 %!   assert (get (h, "ydata"), (1:3)', eps);
178 %!   assert (get (h, "zdata"), eye(3));
179 %!   assert (get (h, "cdata"), eye(3));
180 %!   assert (get (h, "type"), "surface");
181 %!   assert (get (h, "linestyle"), get (0, "defaultsurfacelinestyle"));
182 %!   assert (get (h, "linewidth"), get (0, "defaultsurfacelinewidth"), eps);
183 %!   assert (get (h, "marker"), get (0, "defaultsurfacemarker"));
184 %!   assert (get (h, "markersize"), get (0, "defaultsurfacemarkersize"));
185 %! unwind_protect_cleanup
186 %!   close (hf);
187 %! end_unwind_protect
188