~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to scripts/plot/surface.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2004,
 
2
##               2005, 2006, 2007 John W. Eaton
 
3
##
 
4
## This file is part of Octave.
 
5
##
 
6
## Octave is free software; you can redistribute it and/or modify it
 
7
## under the terms of the GNU General Public License as published by
 
8
## the Free Software Foundation; either version 3 of the License, or (at
 
9
## your option) any later version.
 
10
##
 
11
## Octave is distributed in the hope that it will be useful, but
 
12
## WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
## General Public License for more details.
 
15
##
 
16
## You should have received a copy of the GNU General Public License
 
17
## along with Octave; see the file COPYING.  If not, see
 
18
## <http://www.gnu.org/licenses/>.
 
19
 
 
20
## -*- texinfo -*-
 
21
## @deftypefn {Function File} {} surface (@var{x}, @var{y}, @var{z}, @var{c})
 
22
## @deftypefnx {Function File} {} surface (@var{x}, @var{y}, @var{z})
 
23
## @deftypefnx {Function File} {} surface (@var{z}, @var{c})
 
24
## @deftypefnx {Function File} {} surface (@var{z})
 
25
## @deftypefnx {Function File} {} surface (@dots{}, @var{prop}, @var{val})
 
26
## @deftypefnx {Function File} {} surface (@var{h}, @dots{})
 
27
## @deftypefnx {Function File} {@var{h} = } surface (@dots{})
 
28
## Plot a surface graphic object given matrices @var{x}, and @var{y} from 
 
29
## @code{meshgrid} and a matrix @var{z} corresponding to the @var{x} and 
 
30
## @var{y} coordinates of the surface.  If @var{x} and @var{y} are vectors,
 
31
## then a typical vertex  is (@var{x}(j), @var{y}(i), @var{z}(i,j)).  Thus, 
 
32
## columns of @var{z} correspond to different @var{x} values and rows of 
 
33
## @var{z} correspond to different @var{y} values. If @var{x} and @var{y}
 
34
## are missing, they are constructed from size of the matrix @var{z}.
 
35
##
 
36
## Any additional properties passed are assigned the the surface..
 
37
## @seealso{surf, mesh, patch, line}
 
38
## @end deftypefn
 
39
 
 
40
## Author: jwe
 
41
 
 
42
function retval = surface (varargin)
 
43
 
 
44
  [h, varargin] = __plt_get_axis_arg__ ("surface", varargin{:});
 
45
 
 
46
  oldh = gca ();
 
47
  unwind_protect
 
48
    axes (h);
 
49
    [tmp, bad_usage] = __surface__ (h, varargin{:});
 
50
  unwind_protect_cleanup
 
51
    axes (oldh);
 
52
  end_unwind_protect
 
53
 
 
54
  if (bad_usage)
 
55
    print_usage ();
 
56
  endif
 
57
 
 
58
  if (nargout > 0)
 
59
    retval = tmp;
 
60
  endif
 
61
 
 
62
endfunction
 
63
 
 
64
function [h, bad_usage] = __surface__ (ax, varargin)
 
65
 
 
66
  bad_usage = false;
 
67
  h = 0;
 
68
  firststring = nargin;
 
69
  for i = 2 : nargin
 
70
    if (ischar (varargin{i - 1}))
 
71
      firststring = i - 1;
 
72
      break;
 
73
    endif
 
74
  endfor
 
75
 
 
76
  if (firststring > 5)
 
77
    bad_usage = true;
 
78
  elseif (firststring == 5)
 
79
    x = varargin{1};
 
80
    y = varargin{2};
 
81
    z = varargin{3};
 
82
    c = varargin{4};
 
83
 
 
84
    if (! size_equal (z, c))
 
85
      error ("surface: z and c must have same size");
 
86
    endif
 
87
    if (isvector (x) && isvector (y) && ismatrix (z))
 
88
      if (rows (z) == length (y) && columns (z) == length (x))
 
89
        x = x(:)';
 
90
        y = y(:);
 
91
      else
 
92
        error ("surface: rows (z) must be the same as length (y) and columns (z) must be the same as length (x)");
 
93
      endif
 
94
    elseif (ismatrix (x) && ismatrix (y) && ismatrix (z))
 
95
      if (! size_equal (x, y, z))
 
96
        error ("surface: x, y, and z must have same dimensions");
 
97
      endif
 
98
    else
 
99
      error ("surface: x and y must be vectors and z must be a matrix");
 
100
    endif
 
101
  elseif (firststring == 4)
 
102
    x = varargin{1};
 
103
    y = varargin{2};
 
104
    z = varargin{3};
 
105
    c = z;
 
106
    if (isvector (x) && isvector (y) && ismatrix (z))
 
107
      if (rows (z) == length (y) && columns (z) == length (x))
 
108
        x = x(:)';
 
109
        y = y(:);
 
110
      else
 
111
        error ("surface: rows (z) must be the same as length (y) and columns (z) must be the same as length (x)");
 
112
      endif
 
113
    elseif (ismatrix (x) && ismatrix (y) && ismatrix (z))
 
114
      if (! size_equal (x, y, z))
 
115
        error ("surface: x, y, and z must have same dimensions");
 
116
      endif
 
117
    else
 
118
      error ("surface: x and y must be vectors and z must be a matrix");
 
119
    endif
 
120
  elseif (firststring == 3)    
 
121
    z = varargin{1};
 
122
    c = varargin{2};
 
123
    if (ismatrix (z))
 
124
      [nr, nc] = size (z);
 
125
      x = 1:nc;
 
126
      y = (1:nr)';
 
127
    else
 
128
      error ("surface: argument must be a matrix");
 
129
    endif
 
130
  elseif (firststring == 2)    
 
131
    z = varargin{1};
 
132
    c = z;
 
133
    if (ismatrix (z))
 
134
      [nr, nc] = size (z);
 
135
      x = 1:nc;
 
136
      y = (1:nr)';
 
137
    else
 
138
      error ("surface: argument must be a matrix");
 
139
    endif
 
140
  else
 
141
    bad_usage = true;
 
142
  endif
 
143
 
 
144
  if (! bad_usage)
 
145
    ## Make a default surface object.
 
146
    other_args = {};
 
147
    if (firststring < nargin)
 
148
      other_args = varargin(firststring:end);
 
149
    endif
 
150
    h = __go_surface__ (ax, "xdata", x, "ydata", y, "zdata", z, "cdata", c,
 
151
                        other_args{:});
 
152
 
 
153
    if (! ishold ())
 
154
      set (ax, "view", [0, 90], "box", "off");
 
155
    endif
 
156
  endif
 
157
 
 
158
endfunction