annotate scripts/plot/draw/surfnorm.m @ 20227:9f484edd8767

surfnorm.m: Return unnormalized vectors for Matlab compatibility. * NEWS: Announce change. surfnorm.m: Return unnormalized vectors if output arguments requested. If plotting immediately, new algorithm for determining normals takes into account the data aspect ratio of the plot.
author Rik <rik@octave.org>
date Mon, 25 May 2015 16:48:47 -0700
parents 9fc020886ae9
children 5b7643257978
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19597
diff changeset
1 ## Copyright (C) 2007-2015 David Bateman
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
2 ##
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
3 ## This file is part of Octave.
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
4 ##
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
8 ## your option) any later version.
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
9 ##
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
13 ## General Public License for more details.
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
14 ##
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
18
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
19 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 10634
diff changeset
20 ## @deftypefn {Function File} {} surfnorm (@var{x}, @var{y}, @var{z})
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
21 ## @deftypefnx {Function File} {} surfnorm (@var{z})
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
22 ## @deftypefnx {Function File} {} surfnorm (@dots{}, @var{prop}, @var{val}, @dots{})
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
23 ## @deftypefnx {Function File} {} surfnorm (@var{hax}, @dots{})
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
24 ## @deftypefnx {Function File} {[@var{nx}, @var{ny}, @var{nz}] =} surfnorm (@dots{})
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
25 ## Find the vectors normal to a meshgridded surface.
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
26 ##
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
27 ## If @var{x} and @var{y} are vectors, then a typical vertex is
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
28 ## (@var{x}(j), @var{y}(i), @var{z}(i,j)). Thus, columns of @var{z} correspond
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
29 ## to different @var{x} values and rows of @var{z} correspond to different
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
30 ## @var{y} values. If only a single input @var{z} is given then @var{x} is
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
31 ## taken to be @code{1:rows (@var{z})} and @var{y} is
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
32 ## @code{1:columns (@var{z})}.
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
33 ##
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
34 ## If no return arguments are requested, a surface plot with the normal
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
35 ## vectors to the surface is plotted.
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
36 ##
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
37 ## Any property/value input pairs are assigned to the surface object.
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19596
diff changeset
38 ##
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
39 ## If the first argument @var{hax} is an axes handle, then plot into this axis,
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
40 ## rather than the current axes returned by @code{gca}.
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
41 ##
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
42 ## If output arguments are requested then the components of the normal
19038
9e3c35d934ab doc: Periodic spell check of documentation.
Rik <rik@octave.org>
parents: 18947
diff changeset
43 ## vectors are returned in @var{nx}, @var{ny}, and @var{nz} and no plot is
20227
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ## made. The normal vectors are unnormalized (magnitude != 1). To normalize,
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## use
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ##
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ## @example
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
48 ## mag = sqrt (nx.^2 + ny.^2 + nz.^2);
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
49 ## nx ./= len; ny ./= len; nz ./= len;
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
50 ## @end example
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
51 ##
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
52 ## An example of the use of @code{surfnorm} is
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
53 ##
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
54 ## @example
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
55 ## surfnorm (peaks (25));
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
56 ## @end example
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
57 ##
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
58 ## Algorithm: The normal vectors are calculated by taking the cross product
20227
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
59 ## of the diagonals of each of the quadrilateral faces in the meshgrid to find
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
60 ## the normal vectors at the center of each face. Next, for each meshgrid
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
61 ## point the four nearest normal vectors are averaged to obtain the final
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
62 ## normal to the surface at the meshgrid point.
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
63 ##
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
64 ## For surface objects, the @qcode{"VertexNormals"} property contains
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
65 ## equivalent information, except possibly near the boundary of the surface
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
66 ## where different interpolation schemes may yield slightly different values.
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
67 ##
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
68 ## @seealso{isonormals, quiver3, surf, meshgrid}
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
69 ## @end deftypefn
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
70
7215
dd88d61d443f [project @ 2007-11-29 16:44:45 by jwe]
jwe
parents: 7208
diff changeset
71 function [Nx, Ny, Nz] = surfnorm (varargin)
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
72
17052
1118d566bcd4 surf.m, surfc.m, surfl.m, surfnorm.m: Update to use new __plt_get_axis_arg__.
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 14872
diff changeset
73 [hax, varargin, nargin] = __plt_get_axis_arg__ ("surfnorm", varargin{:});
7215
dd88d61d443f [project @ 2007-11-29 16:44:45 by jwe]
jwe
parents: 7208
diff changeset
74
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
75 if (nargin == 0 || nargin == 2)
7215
dd88d61d443f [project @ 2007-11-29 16:44:45 by jwe]
jwe
parents: 7208
diff changeset
76 print_usage ();
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
77 endif
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
78
7215
dd88d61d443f [project @ 2007-11-29 16:44:45 by jwe]
jwe
parents: 7208
diff changeset
79 if (nargin == 1)
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
80 z = varargin{1};
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
81 [x, y] = meshgrid (1:columns (z), 1:rows (z));
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
82 ioff = 2;
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
83 else
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
84 x = varargin{1};
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
85 y = varargin{2};
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
86 z = varargin{3};
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
87 ioff = 4;
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
88 endif
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
89
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
90 if (iscomplex (z) || iscomplex (x) || iscomplex (y))
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
91 error ("surfnorm: X, Y, and Z must be 2-D real matrices");
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
92 endif
10634
60542efcfa2c Check input arguments for size and type (bug #29861).
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
93 if (! size_equal (x, y, z))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
94 error ("surfnorm: X, Y, and Z must have the same dimensions");
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
95 endif
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
96
20227
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
97 ## FIXME: Matlab uses a bicubic interpolation, not linear, along the boundary.
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
98 ## Do a linear extrapolation for mesh points on the boundary so that the mesh
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
99 ## is increased by 1 on each side. This allows each original meshgrid point
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
100 ## to be surrounded by four quadrilaterals and the same calculation can be
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
101 ## used for interior and boundary points. The extrapolation works badly for
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
102 ## closed surfaces like spheres.
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
103 xx = [2 * x(:,1) - x(:,2), x, 2 * x(:,end) - x(:,end-1)];
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
104 xx = [2 * xx(1,:) - xx(2,:); xx; 2 * xx(end,:) - xx(end-1,:)];
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
105 yy = [2 * y(:,1) - y(:,2), y, 2 * y(:,end) - y(:,end-1)];
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
106 yy = [2 * yy(1,:) - yy(2,:); yy; 2 * yy(end,:) - yy(end-1,:)];
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
107 zz = [2 * z(:,1) - z(:,2), z, 2 * z(:,end) - z(:,end-1)];
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
108 zz = [2 * zz(1,:) - zz(2,:); zz; 2 * zz(end,:) - zz(end-1,:)];
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
109
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
110 u.x = xx(1:end-1,1:end-1) - xx(2:end,2:end);
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
111 u.y = yy(1:end-1,1:end-1) - yy(2:end,2:end);
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
112 u.z = zz(1:end-1,1:end-1) - zz(2:end,2:end);
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
113 v.x = xx(1:end-1,2:end) - xx(2:end,1:end-1);
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
114 v.y = yy(1:end-1,2:end) - yy(2:end,1:end-1);
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
115 v.z = zz(1:end-1,2:end) - zz(2:end,1:end-1);
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
116
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
117 c = cross ([u.x(:), u.y(:), u.z(:)], [v.x(:), v.y(:), v.z(:)]);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14335
diff changeset
118 w.x = reshape (c(:,1), size (u.x));
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14335
diff changeset
119 w.y = reshape (c(:,2), size (u.y));
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14335
diff changeset
120 w.z = reshape (c(:,3), size (u.z));
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
121
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
122 ## Create normal vectors as mesh vectices from normals at mesh centers
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
123 nx = (w.x(1:end-1,1:end-1) + w.x(1:end-1,2:end) +
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
124 w.x(2:end,1:end-1) + w.x(2:end,2:end)) / 4;
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
125 ny = (w.y(1:end-1,1:end-1) + w.y(1:end-1,2:end) +
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
126 w.y(2:end,1:end-1) + w.y(2:end,2:end)) / 4;
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
127 nz = (w.z(1:end-1,1:end-1) + w.z(1:end-1,2:end) +
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
128 w.z(2:end,1:end-1) + w.z(2:end,2:end)) / 4;
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
129
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
130 if (nargout == 0)
17476
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
131 oldfig = [];
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
132 if (! isempty (hax))
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
133 oldfig = get (0, "currentfigure");
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
134 endif
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
135 unwind_protect
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
136 hax = newplot (hax);
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
137
17476
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
138 surf (x, y, z, varargin{ioff:end});
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
139 old_hold_state = get (hax, "nextplot");
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
140 unwind_protect
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
141 set (hax, "nextplot", "add");
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
142
20227
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
143 ## Normalize the normal vectors
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
144 nmag = sqrt (nx.^2 + ny.^2 + nz.^2);
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
145
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
146 ## And correct for the aspect ratio of the display
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
147 daratio = daspect (hax);
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
148 damag = sqrt (sumsq (daratio));
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
149
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
150 ## FIXME: May also want to normalize the vectors relative to the size
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
151 ## of the diagonal.
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
152
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
153 nx ./= nmag / (daratio(1)^2 / damag);
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
154 ny ./= nmag / (daratio(2)^2 / damag);
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
155 nz ./= nmag / (daratio(3)^2 / damag);
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
156
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
157 plot3 ([x(:).'; x(:).' + nx(:).' ; NaN(size(x(:).'))](:),
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
158 [y(:).'; y(:).' + ny(:).' ; NaN(size(y(:).'))](:),
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
159 [z(:).'; z(:).' + nz(:).' ; NaN(size(z(:).'))](:),
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
160 "r");
17476
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
161 unwind_protect_cleanup
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
162 set (hax, "nextplot", old_hold_state);
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
163 end_unwind_protect
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
164
17476
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
165 unwind_protect_cleanup
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
166 if (! isempty (oldfig))
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
167 set (0, "currentfigure", oldfig);
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
168 endif
f0f4b524b6d0 maint: Correct indentation for several plot routines.
Rik <rik@octave.org>
parents: 17301
diff changeset
169 end_unwind_protect
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
170 else
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
171 Nx = nx;
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
172 Ny = ny;
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
173 Nz = nz;
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
174 endif
7216
5389a52df87b [project @ 2007-11-29 19:07:29 by jwe]
jwe
parents: 7215
diff changeset
175
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
176 endfunction
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
177
14223
ba7a26030214 Use Octave spacing convention in %!test blocks of surface plot functions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
178
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
179 %!demo
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14223
diff changeset
180 %! clf;
14247
c4fa5e0b6193 test: Make surface demos reproducible by setting colormap to default at start of demo.
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
181 %! colormap ('default');
20227
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
182 %! surfnorm (peaks (19));
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
183 %! shading faceted;
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
184 %! title ({'surfnorm() shows surface and normals at each vertex', ...
20227
9f484edd8767 surfnorm.m: Return unnormalized vectors for Matlab compatibility.
Rik <rik@octave.org>
parents: 19833
diff changeset
185 %! 'peaks() function with 19 faces'});
7189
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
186
e8d953d03f6a [project @ 2007-11-26 20:42:09 by dbateman]
dbateman
parents:
diff changeset
187 %!demo
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14223
diff changeset
188 %! clf;
14247
c4fa5e0b6193 test: Make surface demos reproducible by setting colormap to default at start of demo.
Rik <octave@nomad.inbox5.com>
parents: 14237
diff changeset
189 %! colormap ('default');
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
190 %! [x, y, z] = sombrero (10);
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
191 %! surfnorm (x, y, z);
8790
a013ff655ca4 Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents: 8241
diff changeset
192
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
193 ## Test input validation
18947
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
194 %!error surfnorm ()
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
195 %!error surfnorm (1,2)
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
196 %!error <X, Y, and Z must be 2-D real matrices> surfnorm (i)
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
197 %!error <X, Y, and Z must be 2-D real matrices> surfnorm (i, 1, 1)
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
198 %!error <X, Y, and Z must be 2-D real matrices> surfnorm (1, i, 1)
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
199 %!error <X, Y, and Z must be 2-D real matrices> surfnorm (1, 1, i)
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
200 %!error <X, Y, and Z must have the same dimensions> surfnorm ([1 2], 1, 1)
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
201 %!error <X, Y, and Z must have the same dimensions> surfnorm (1, [1 2], 1)
91f626902d17 surfnorm.m: Overhaul for Matlab compatibility.
Rik <rik@octave.org>
parents: 17744
diff changeset
202 %!error <X, Y, and Z must have the same dimensions> surfnorm (1, 1, [1 2])
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14223
diff changeset
203