7189
|
1 ## Copyright (C) 2007 David Bateman |
|
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{}) |
|
24 ## Find the vectors normal to a meshgridded surface. The meshed gridded |
|
25 ## surface is defined by @var{x}, @var{y}, and @var{z}. If @var{x} and |
|
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 |
|
34 ## vectors to the surface is plotted. Otherwise the componets of the normal |
|
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 |
|
39 ## diagonals of eash of teh quadrilaterals in the meshgrid to find the |
|
40 ## normal vectors of the centers of these quadrilaterals. The four nearest |
|
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}); |
|
117 hold on; |
|
118 plot3 ([x(:)'; x(:).' + nx(:).' ; NaN(size(x(:).'))](:), |
|
119 [y(:)'; y(:).' + ny(:).' ; NaN(size(y(:).'))](:), |
|
120 [z(:)'; z(:).' + nz(:).' ; NaN(size(z(:).'))](:), |
|
121 varargin{ioff:end}); |
|
122 unwind_protect_cleanup |
|
123 axes (oldh); |
|
124 end_unwind_protect |
7189
|
125 else |
|
126 Nx = nx; |
|
127 Ny = ny; |
|
128 Nz = nz; |
|
129 endif |
|
130 endfunction |
|
131 |
|
132 %!demo |
|
133 %! [x, y, z] = peaks(10); |
|
134 %! surfnorm (x, y, z); |
|
135 |
|
136 %!demo |
|
137 %! surfnorm (peaks(10)); |