Mercurial > hg > octave-lyh
annotate scripts/plot/surfnorm.m @ 17475:fd01c78caa0b
Added tag ss-3-7-7 for changeset cc13924a4266
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 23 Sep 2013 13:31:11 -0400 |
parents | 68bcac3c043a |
children | f0f4b524b6d0 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14092
diff
changeset
|
1 ## Copyright (C) 2007-2012 David Bateman |
7189 | 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 -*- | |
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 | 21 ## @deftypefnx {Function File} {} surfnorm (@var{z}) |
22 ## @deftypefnx {Function File} {[@var{nx}, @var{ny}, @var{nz}] =} surfnorm (@dots{}) | |
23 ## @deftypefnx {Function File} {} surfnorm (@var{h}, @dots{}) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
24 ## Find the vectors normal to a meshgridded surface. The meshed gridded |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
25 ## surface is defined by @var{x}, @var{y}, and @var{z}. If @var{x} and |
7189 | 26 ## @var{y} are not defined, then it is assumed that they are given by |
27 ## | |
28 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9040
diff
changeset
|
29 ## @group |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
30 ## [@var{x}, @var{y}] = meshgrid (1:rows (@var{z}), |
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
31 ## 1:columns (@var{z})); |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9040
diff
changeset
|
32 ## @end group |
7189 | 33 ## @end example |
34 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
35 ## If no return arguments are requested, a surface plot with the normal |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
36 ## vectors to the surface is plotted. Otherwise the components of the normal |
7189 | 37 ## vectors at the mesh gridded points are returned in @var{nx}, @var{ny}, |
38 ## and @var{nz}. | |
39 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
40 ## The normal vectors are calculated by taking the cross product of the |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
41 ## diagonals of each of the quadrilaterals in the meshgrid to find the |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
42 ## normal vectors of the centers of these quadrilaterals. The four nearest |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
43 ## normal vectors to the meshgrid points are then averaged to obtain the |
7189 | 44 ## normal to the surface at the meshgridded points. |
45 ## | |
46 ## An example of the use of @code{surfnorm} is | |
47 ## | |
48 ## @example | |
49 ## surfnorm (peaks (25)); | |
50 ## @end example | |
51 ## @seealso{surf, quiver3} | |
52 ## @end deftypefn | |
53 | |
7215 | 54 function [Nx, Ny, Nz] = surfnorm (varargin) |
7189 | 55 |
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
|
56 [hax, varargin, nargin] = __plt_get_axis_arg__ ("surfnorm", varargin{:}); |
7215 | 57 |
58 if (nargin != 1 && nargin != 3) | |
59 print_usage (); | |
7189 | 60 endif |
61 | |
7215 | 62 if (nargin == 1) |
7189 | 63 z = varargin{1}; |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
64 [x, y] = meshgrid (1:rows (z), 1:columns (z)); |
7189 | 65 ioff = 2; |
66 else | |
67 x = varargin{1}; | |
68 y = varargin{2}; | |
69 z = varargin{3}; | |
70 ioff = 4; | |
71 endif | |
72 | |
10634
60542efcfa2c
Check input arguments for size and type (bug #29861).
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
73 if (!ismatrix (z) || isvector (z) || isscalar (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
|
74 error ("surfnorm: Z argument must be a matrix"); |
7189 | 75 endif |
10634
60542efcfa2c
Check input arguments for size and type (bug #29861).
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
76 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
|
77 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
|
78 endif |
7189 | 79 |
80 ## Make life easier, and avoid having to do the extrapolation later, do | |
81 ## a simpler linear extrapolation here. This is approximative, and works | |
82 ## badly for closed surfaces like spheres. | |
83 xx = [2 .* x(:,1) - x(:,2), x, 2 .* x(:,end) - x(:,end-1)]; | |
84 xx = [2 .* xx(1,:) - xx(2,:); xx; 2 .* xx(end,:) - xx(end-1,:)]; | |
85 yy = [2 .* y(:,1) - y(:,2), y, 2 .* y(:,end) - y(:,end-1)]; | |
86 yy = [2 .* yy(1,:) - yy(2,:); yy; 2 .* yy(end,:) - yy(end-1,:)]; | |
87 zz = [2 .* z(:,1) - z(:,2), z, 2 .* z(:,end) - z(:,end-1)]; | |
88 zz = [2 .* zz(1,:) - zz(2,:); zz; 2 .* zz(end,:) - zz(end-1,:)]; | |
89 | |
90 u.x = xx(1:end-1,1:end-1) - xx(2:end,2:end); | |
91 u.y = yy(1:end-1,1:end-1) - yy(2:end,2:end); | |
92 u.z = zz(1:end-1,1:end-1) - zz(2:end,2:end); | |
93 v.x = xx(1:end-1,2:end) - xx(2:end,1:end-1); | |
94 v.y = yy(1:end-1,2:end) - yy(2:end,1:end-1); | |
95 v.z = zz(1:end-1,2:end) - zz(2:end,1:end-1); | |
96 | |
97 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
|
98 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
|
99 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
|
100 w.z = reshape (c(:,3), size (u.z)); |
7189 | 101 |
102 ## Create normal vectors as mesh vectices from normals at mesh centers | |
103 nx = (w.x(1:end-1,1:end-1) + w.x(1:end-1,2:end) + | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
104 w.x(2:end,1:end-1) + w.x(2:end,2:end)) ./ 4; |
7189 | 105 ny = (w.y(1:end-1,1:end-1) + w.y(1:end-1,2:end) + |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
106 w.y(2:end,1:end-1) + w.y(2:end,2:end)) ./ 4; |
7189 | 107 nz = (w.z(1:end-1,1:end-1) + w.z(1:end-1,2:end) + |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
108 w.z(2:end,1:end-1) + w.z(2:end,2:end)) ./ 4; |
7189 | 109 |
110 ## Normalize the normal vectors | |
111 len = sqrt (nx.^2 + ny.^2 + nz.^2); | |
112 nx = nx ./ len; | |
113 ny = ny ./ len; | |
114 nz = nz ./ len; | |
115 | |
116 if (nargout == 0) | |
17219
87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
Rik <rik@octave.org>
parents:
17130
diff
changeset
|
117 oldfig = []; |
17309
68bcac3c043a
Correct inversion accidentally introduced in cset 87ba70043bfc.
Rik <rik@octave.org>
parents:
17219
diff
changeset
|
118 if (! isempty (hax)) |
17219
87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
Rik <rik@octave.org>
parents:
17130
diff
changeset
|
119 oldfig = get (0, "currentfigure"); |
87ba70043bfc
Don't use ifelse in plot fcns to avoid unnecessary fcn evaluations.
Rik <rik@octave.org>
parents:
17130
diff
changeset
|
120 endif |
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
|
121 unwind_protect |
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
|
122 hax = newplot (hax); |
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
|
123 |
17130
26589abbc78d
Don't pass axis handle unnecessarily from high level to low level plot functions.
Rik <rik@octave.org>
parents:
17052
diff
changeset
|
124 surf (x, y, z, varargin{ioff:end}); |
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
|
125 old_hold_state = get (hax, "nextplot"); |
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
|
126 unwind_protect |
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
|
127 set (hax, "nextplot", "add"); |
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
|
128 plot3 ([x(:)'; x(:).' + nx(:).' ; NaN(size(x(:).'))](:), |
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
|
129 [y(:)'; y(:).' + ny(:).' ; NaN(size(y(:).'))](:), |
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
|
130 [z(:)'; z(:).' + nz(:).' ; NaN(size(z(:).'))](:), |
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
|
131 varargin{ioff:end}); |
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
|
132 unwind_protect_cleanup |
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
|
133 set (hax, "nextplot", old_hold_state); |
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
|
134 end_unwind_protect |
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
|
135 |
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
|
136 unwind_protect_cleanup |
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
|
137 if (! isempty (oldfig)) |
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
|
138 set (0, "currentfigure", oldfig); |
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
|
139 endif |
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
|
140 end_unwind_protect |
7189 | 141 else |
142 Nx = nx; | |
143 Ny = ny; | |
144 Nz = nz; | |
145 endif | |
7216 | 146 |
7189 | 147 endfunction |
148 | |
14223
ba7a26030214
Use Octave spacing convention in %!test blocks of surface plot functions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
149 |
7189 | 150 %!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
|
151 %! 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
|
152 %! colormap ('default'); |
14223
ba7a26030214
Use Octave spacing convention in %!test blocks of surface plot functions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
153 %! [x, y, z] = peaks (10); |
7189 | 154 %! surfnorm (x, y, z); |
155 | |
156 %!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
|
157 %! 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
|
158 %! colormap ('default'); |
14223
ba7a26030214
Use Octave spacing convention in %!test blocks of surface plot functions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 %! surfnorm (peaks (10)); |
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
|
160 |
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8241
diff
changeset
|
161 %!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
|
162 %! 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
|
163 %! colormap ('default'); |
14223
ba7a26030214
Use Octave spacing convention in %!test blocks of surface plot functions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
164 %! surfnorm (peaks (32)); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14223
diff
changeset
|
165 %! shading interp; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14223
diff
changeset
|
166 |