Mercurial > hg > octave-nkf
annotate scripts/plot/surfnorm.m @ 12118:973f585cfdf2 release-3-2-x
include PTHREAD_CFLAGS in LINK_DEPS for liboctave
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 22 Jan 2010 10:21:33 +0100 |
parents | 1bf0ce0930be |
children | 95c3e38098bf |
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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9040
diff
changeset
|
29 ## @group |
7189 | 30 ## [@var{x}, @var{y}] = meshgrid (1:size(@var{z}, 1), |
31 ## 1:size(@var{z}, 2)); | |
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 ## | |
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 ## | |
40 ## 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
|
41 ## 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
|
42 ## normal vectors of the centers of these quadrilaterals. The four nearest |
7189 | 43 ## normal vectors to the meshgrid points are then averaged to obtain the |
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 |
7215 | 56 [h, varargin, nargin] = __plt_get_axis_arg__ ((nargout != 0), "surfnorm", |
57 varargin{:}); | |
58 | |
59 if (nargin != 1 && nargin != 3) | |
60 print_usage (); | |
7189 | 61 endif |
62 | |
7215 | 63 if (nargin == 1) |
7189 | 64 z = varargin{1}; |
65 [x, y] = meshgrid (1:size(z,1), 1:size(z,2)); | |
66 ioff = 2; | |
67 else | |
68 x = varargin{1}; | |
69 y = varargin{2}; | |
70 z = varargin{3}; | |
71 ioff = 4; | |
72 endif | |
73 | |
74 if (nargout == 0) | |
75 endif | |
76 | |
77 ## Make life easier, and avoid having to do the extrapolation later, do | |
78 ## a simpler linear extrapolation here. This is approximative, and works | |
79 ## badly for closed surfaces like spheres. | |
80 xx = [2 .* x(:,1) - x(:,2), x, 2 .* x(:,end) - x(:,end-1)]; | |
81 xx = [2 .* xx(1,:) - xx(2,:); xx; 2 .* xx(end,:) - xx(end-1,:)]; | |
82 yy = [2 .* y(:,1) - y(:,2), y, 2 .* y(:,end) - y(:,end-1)]; | |
83 yy = [2 .* yy(1,:) - yy(2,:); yy; 2 .* yy(end,:) - yy(end-1,:)]; | |
84 zz = [2 .* z(:,1) - z(:,2), z, 2 .* z(:,end) - z(:,end-1)]; | |
85 zz = [2 .* zz(1,:) - zz(2,:); zz; 2 .* zz(end,:) - zz(end-1,:)]; | |
86 | |
87 u.x = xx(1:end-1,1:end-1) - xx(2:end,2:end); | |
88 u.y = yy(1:end-1,1:end-1) - yy(2:end,2:end); | |
89 u.z = zz(1:end-1,1:end-1) - zz(2:end,2:end); | |
90 v.x = xx(1:end-1,2:end) - xx(2:end,1:end-1); | |
91 v.y = yy(1:end-1,2:end) - yy(2:end,1:end-1); | |
92 v.z = zz(1:end-1,2:end) - zz(2:end,1:end-1); | |
93 | |
94 c = cross ([u.x(:), u.y(:), u.z(:)], [v.x(:), v.y(:), v.z(:)]); | |
95 w.x = reshape (c(:,1), size(u.x)); | |
96 w.y = reshape (c(:,2), size(u.y)); | |
97 w.z = reshape (c(:,3), size(u.z)); | |
98 | |
99 ## Create normal vectors as mesh vectices from normals at mesh centers | |
100 nx = (w.x(1:end-1,1:end-1) + w.x(1:end-1,2:end) + | |
101 w.x(2:end,1:end-1) + w.x(2:end,2:end)) ./ 4; | |
102 ny = (w.y(1:end-1,1:end-1) + w.y(1:end-1,2:end) + | |
103 w.y(2:end,1:end-1) + w.y(2:end,2:end)) ./ 4; | |
104 nz = (w.z(1:end-1,1:end-1) + w.z(1:end-1,2:end) + | |
105 w.z(2:end,1:end-1) + w.z(2:end,2:end)) ./ 4; | |
106 | |
107 ## Normalize the normal vectors | |
108 len = sqrt (nx.^2 + ny.^2 + nz.^2); | |
109 nx = nx ./ len; | |
110 ny = ny ./ len; | |
111 nz = nz ./ len; | |
112 | |
113 if (nargout == 0) | |
7215 | 114 oldh = gca (); |
115 unwind_protect | |
116 axes (h); | |
117 newplot (); | |
118 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
|
119 old_hold_state = get (h, "nextplot"); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
120 unwind_protect |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
121 set (h, "nextplot", "add"); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
122 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
|
123 [y(:)'; y(:).' + ny(:).' ; NaN(size(y(:).'))](:), |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
124 [z(:)'; z(:).' + nz(:).' ; NaN(size(z(:).'))](:), |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
125 varargin{ioff:end}); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
126 unwind_protect_cleanup |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
127 set (h, "nextplot", old_hold_state); |
1e1e88bcc733
surfnorm.m: save and restore hold state
John W. Eaton <jwe@octave.org>
parents:
7216
diff
changeset
|
128 end_unwind_protect |
7215 | 129 unwind_protect_cleanup |
130 axes (oldh); | |
131 end_unwind_protect | |
7189 | 132 else |
133 Nx = nx; | |
134 Ny = ny; | |
135 Nz = nz; | |
136 endif | |
7216 | 137 |
7189 | 138 endfunction |
139 | |
140 %!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
|
141 %! colormap (jet (64)) |
7189 | 142 %! [x, y, z] = peaks(10); |
143 %! surfnorm (x, y, z); | |
144 | |
145 %!demo | |
146 %! 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
|
147 |
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 %!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
|
149 %! 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
|
150 %! shading interp |