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 |
|
52 function varargout = surfnorm (varargin) |
|
53 |
|
54 if (nargout > 0) |
|
55 varargout = cell (nargout, 1); |
|
56 else |
|
57 varargout = cell (0, 0); |
|
58 endif |
|
59 if (isscalar (varargin{1}) && ishandle (varargin{1})) |
|
60 h = varargin {1}; |
|
61 if (! strcmp (get (h, "type"), "axes")) |
|
62 error ("surfnorm: expecting first argument to be an axes object"); |
|
63 endif |
|
64 if (nargin != 2 && nargin != 4) |
|
65 print_usage (); |
|
66 endif |
|
67 oldh = gca (); |
|
68 unwind_protect |
|
69 axes (h); |
|
70 [varargout{:}] = __surfnorm__ (h, varargin{2:end}); |
|
71 unwind_protect_cleanup |
|
72 axes (oldh); |
|
73 end_unwind_protect |
|
74 else |
|
75 if (nargin != 1 && nargin != 3) |
|
76 print_usage (); |
|
77 endif |
|
78 [varargout{:}] = __surfnorm__ (gca (), varargin{:}); |
|
79 endif |
|
80 |
|
81 endfunction |
|
82 |
|
83 function [Nx, Ny, Nz] = __surfnorm__ (h, varargin) |
|
84 |
|
85 if (nargin == 2) |
|
86 z = varargin{1}; |
|
87 [x, y] = meshgrid (1:size(z,1), 1:size(z,2)); |
|
88 ioff = 2; |
|
89 else |
|
90 x = varargin{1}; |
|
91 y = varargin{2}; |
|
92 z = varargin{3}; |
|
93 ioff = 4; |
|
94 endif |
|
95 |
|
96 if (nargout == 0) |
|
97 newplot(); |
|
98 surf (x, y, z, varargin{ioff:end}); |
|
99 hold on; |
|
100 endif |
|
101 |
|
102 ## Make life easier, and avoid having to do the extrapolation later, do |
|
103 ## a simpler linear extrapolation here. This is approximative, and works |
|
104 ## badly for closed surfaces like spheres. |
|
105 xx = [2 .* x(:,1) - x(:,2), x, 2 .* x(:,end) - x(:,end-1)]; |
|
106 xx = [2 .* xx(1,:) - xx(2,:); xx; 2 .* xx(end,:) - xx(end-1,:)]; |
|
107 yy = [2 .* y(:,1) - y(:,2), y, 2 .* y(:,end) - y(:,end-1)]; |
|
108 yy = [2 .* yy(1,:) - yy(2,:); yy; 2 .* yy(end,:) - yy(end-1,:)]; |
|
109 zz = [2 .* z(:,1) - z(:,2), z, 2 .* z(:,end) - z(:,end-1)]; |
|
110 zz = [2 .* zz(1,:) - zz(2,:); zz; 2 .* zz(end,:) - zz(end-1,:)]; |
|
111 |
|
112 u.x = xx(1:end-1,1:end-1) - xx(2:end,2:end); |
|
113 u.y = yy(1:end-1,1:end-1) - yy(2:end,2:end); |
|
114 u.z = zz(1:end-1,1:end-1) - zz(2:end,2:end); |
|
115 v.x = xx(1:end-1,2:end) - xx(2:end,1:end-1); |
|
116 v.y = yy(1:end-1,2:end) - yy(2:end,1:end-1); |
|
117 v.z = zz(1:end-1,2:end) - zz(2:end,1:end-1); |
|
118 |
|
119 c = cross ([u.x(:), u.y(:), u.z(:)], [v.x(:), v.y(:), v.z(:)]); |
|
120 w.x = reshape (c(:,1), size(u.x)); |
|
121 w.y = reshape (c(:,2), size(u.y)); |
|
122 w.z = reshape (c(:,3), size(u.z)); |
|
123 |
|
124 ## Create normal vectors as mesh vectices from normals at mesh centers |
|
125 nx = (w.x(1:end-1,1:end-1) + w.x(1:end-1,2:end) + |
|
126 w.x(2:end,1:end-1) + w.x(2:end,2:end)) ./ 4; |
|
127 ny = (w.y(1:end-1,1:end-1) + w.y(1:end-1,2:end) + |
|
128 w.y(2:end,1:end-1) + w.y(2:end,2:end)) ./ 4; |
|
129 nz = (w.z(1:end-1,1:end-1) + w.z(1:end-1,2:end) + |
|
130 w.z(2:end,1:end-1) + w.z(2:end,2:end)) ./ 4; |
|
131 |
|
132 ## Normalize the normal vectors |
|
133 len = sqrt (nx.^2 + ny.^2 + nz.^2); |
|
134 nx = nx ./ len; |
|
135 ny = ny ./ len; |
|
136 nz = nz ./ len; |
|
137 |
|
138 if (nargout == 0) |
|
139 plot3 ([x(:)'; x(:).' + nx(:).' ; NaN(size(x(:).'))](:), |
|
140 [y(:)'; y(:).' + ny(:).' ; NaN(size(y(:).'))](:), |
|
141 [z(:)'; z(:).' + nz(:).' ; NaN(size(z(:).'))](:), |
|
142 varargin{ioff:end}); |
|
143 else |
|
144 Nx = nx; |
|
145 Ny = ny; |
|
146 Nz = nz; |
|
147 endif |
|
148 endfunction |
|
149 |
|
150 %!demo |
|
151 %! [x, y, z] = peaks(10); |
|
152 %! surfnorm (x, y, z); |
|
153 |
|
154 %!demo |
|
155 %! surfnorm (peaks(10)); |