Mercurial > hg > octave-lyh
annotate scripts/plot/surfnorm.m @ 9042:97aa01a85ea4
Merge documentation cleanup changes to main branch.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Wed, 25 Mar 2009 18:13:08 -0700 |
parents | dbd0c77e575e |
children | 1bf0ce0930be |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008, 2009 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 -*- | |
20 ## @deftypefn {Function File} {} surfnorm (@var{x}, @var{y}, @var{z}) | |
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{}) | |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## Find the vectors normal to a meshgridded surface. The meshed gridded |
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
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 | |
29 ## [@var{x}, @var{y}] = meshgrid (1:size(@var{z}, 1), | |
30 ## 1:size(@var{z}, 2)); | |
31 ## @end example | |
32 ## | |
33 ## 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
|
34 ## vectors to the surface is plotted. Otherwise the components of the normal |
7189 | 35 ## vectors at the mesh gridded points are returned in @var{nx}, @var{ny}, |
36 ## and @var{nz}. | |
37 ## | |
38 ## The normal vectors are calculated by taking the cross product of the | |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
39 ## diagonals of each of the quadrilaterals in the meshgrid to find the |
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## normal vectors of the centers of these quadrilaterals. The four nearest |
7189 | 41 ## normal vectors to the meshgrid points are then averaged to obtain the |
42 ## normal to the surface at the meshgridded points. | |
43 ## | |
44 ## An example of the use of @code{surfnorm} is | |
45 ## | |
46 ## @example | |
47 ## surfnorm (peaks (25)); | |
48 ## @end example | |
49 ## @seealso{surf, quiver3} | |
50 ## @end deftypefn | |
51 | |
7215 | 52 function [Nx, Ny, Nz] = surfnorm (varargin) |
7189 | 53 |
7215 | 54 [h, varargin, nargin] = __plt_get_axis_arg__ ((nargout != 0), "surfnorm", |
55 varargin{:}); | |
56 | |
57 if (nargin != 1 && nargin != 3) | |
58 print_usage (); | |
7189 | 59 endif |
60 | |
7215 | 61 if (nargin == 1) |
7189 | 62 z = varargin{1}; |
63 [x, y] = meshgrid (1:size(z,1), 1:size(z,2)); | |
64 ioff = 2; | |
65 else | |
66 x = varargin{1}; | |
67 y = varargin{2}; | |
68 z = varargin{3}; | |
69 ioff = 4; | |
70 endif | |
71 | |
72 if (nargout == 0) | |
73 endif | |
74 | |
75 ## Make life easier, and avoid having to do the extrapolation later, do | |
76 ## a simpler linear extrapolation here. This is approximative, and works | |
77 ## badly for closed surfaces like spheres. | |
78 xx = [2 .* x(:,1) - x(:,2), x, 2 .* x(:,end) - x(:,end-1)]; | |
79 xx = [2 .* xx(1,:) - xx(2,:); xx; 2 .* xx(end,:) - xx(end-1,:)]; | |
80 yy = [2 .* y(:,1) - y(:,2), y, 2 .* y(:,end) - y(:,end-1)]; | |
81 yy = [2 .* yy(1,:) - yy(2,:); yy; 2 .* yy(end,:) - yy(end-1,:)]; | |
82 zz = [2 .* z(:,1) - z(:,2), z, 2 .* z(:,end) - z(:,end-1)]; | |
83 zz = [2 .* zz(1,:) - zz(2,:); zz; 2 .* zz(end,:) - zz(end-1,:)]; | |
84 | |
85 u.x = xx(1:end-1,1:end-1) - xx(2:end,2:end); | |
86 u.y = yy(1:end-1,1:end-1) - yy(2:end,2:end); | |
87 u.z = zz(1:end-1,1:end-1) - zz(2:end,2:end); | |
88 v.x = xx(1:end-1,2:end) - xx(2:end,1:end-1); | |
89 v.y = yy(1:end-1,2:end) - yy(2:end,1:end-1); | |
90 v.z = zz(1:end-1,2:end) - zz(2:end,1:end-1); | |
91 | |
92 c = cross ([u.x(:), u.y(:), u.z(:)], [v.x(:), v.y(:), v.z(:)]); | |
93 w.x = reshape (c(:,1), size(u.x)); | |
94 w.y = reshape (c(:,2), size(u.y)); | |
95 w.z = reshape (c(:,3), size(u.z)); | |
96 | |
97 ## Create normal vectors as mesh vectices from normals at mesh centers | |
98 nx = (w.x(1:end-1,1:end-1) + w.x(1:end-1,2:end) + | |
99 w.x(2:end,1:end-1) + w.x(2:end,2:end)) ./ 4; | |
100 ny = (w.y(1:end-1,1:end-1) + w.y(1:end-1,2:end) + | |
101 w.y(2:end,1:end-1) + w.y(2:end,2:end)) ./ 4; | |
102 nz = (w.z(1:end-1,1:end-1) + w.z(1:end-1,2:end) + | |
103 w.z(2:end,1:end-1) + w.z(2:end,2:end)) ./ 4; | |
104 | |
105 ## Normalize the normal vectors | |
106 len = sqrt (nx.^2 + ny.^2 + nz.^2); | |
107 nx = nx ./ len; | |
108 ny = ny ./ len; | |
109 nz = nz ./ len; | |
110 | |
111 if (nargout == 0) | |
7215 | 112 oldh = gca (); |
113 unwind_protect | |
114 axes (h); | |
115 newplot (); | |
116 surf (x, y, z, varargin{ioff:end}); | |
8241
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
117 old_hold_state = get (h, "nextplot"); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
118 unwind_protect |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
119 set (h, "nextplot", "add"); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
120 plot3 ([x(:)'; x(:).' + nx(:).' ; NaN(size(x(:).'))](:), |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
121 [y(:)'; y(:).' + ny(:).' ; NaN(size(y(:).'))](:), |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
122 [z(:)'; z(:).' + nz(:).' ; NaN(size(z(:).'))](:), |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
123 varargin{ioff:end}); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
124 unwind_protect_cleanup |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
125 set (h, "nextplot", old_hold_state); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
126 end_unwind_protect |
7215 | 127 unwind_protect_cleanup |
128 axes (oldh); | |
129 end_unwind_protect | |
7189 | 130 else |
131 Nx = nx; | |
132 Ny = ny; | |
133 Nz = nz; | |
134 endif | |
7216 | 135 |
7189 | 136 endfunction |
137 | |
138 %!demo | |
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
|
139 %! colormap (jet (64)) |
7189 | 140 %! [x, y, z] = peaks(10); |
141 %! surfnorm (x, y, z); | |
142 | |
143 %!demo | |
144 %! 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
|
145 |
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8241
diff
changeset
|
146 %!demo |
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8241
diff
changeset
|
147 %! surfnorm (peaks(32)); |
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8241
diff
changeset
|
148 %! shading interp |