comparison scripts/plot/slice.m @ 17077:f8b485d09ac6

ribbon.m, slice.m: Overhaul to use __plt_get_axis_arg__. * scripts/plot/ribbon.m, scripts/plot/slice.m: Overhaul to use __plt_get_axis_arg__. Redo docstring.
author Rik <rik@octave.org>
date Wed, 24 Jul 2013 23:12:46 -0700
parents 6239f5806c26
children eaab03308c0b
comparison
equal deleted inserted replaced
17076:0de31fe43c4d 17077:f8b485d09ac6
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} slice (@var{x}, @var{y}, @var{z}, @var{v}, @var{sx}, @var{sy}, @var{sz}) 20 ## @deftypefn {Function File} {} slice (@var{x}, @var{y}, @var{z}, @var{v}, @var{sx}, @var{sy}, @var{sz})
21 ## @deftypefnx {Function File} {} slice (@var{x}, @var{y}, @var{z}, @var{v}, @var{xi}, @var{yi}, @var{zi}) 21 ## @deftypefnx {Function File} {} slice (@var{x}, @var{y}, @var{z}, @var{v}, @var{xi}, @var{yi}, @var{zi})
22 ## @deftypefnx {Function File} {} slice (@var{v}, @var{sx}, @var{sy}, @var{sz}) 22 ## @deftypefnx {Function File} {} slice (@var{v}, @var{sx}, @var{sy}, @var{sz})
23 ## @deftypefnx {Function File} {} slice (@var{v}, @var{xi}, @var{yi}, @var{zi}) 23 ## @deftypefnx {Function File} {} slice (@var{v}, @var{xi}, @var{yi}, @var{zi})
24 ## @deftypefnx {Function File} {} slice (@dots{}, @var{method})
25 ## @deftypefnx {Function File} {} slice (@var{hax}, @dots{})
24 ## @deftypefnx {Function File} {@var{h} =} slice (@dots{}) 26 ## @deftypefnx {Function File} {@var{h} =} slice (@dots{})
25 ## @deftypefnx {Function File} {@var{h} =} slice (@dots{}, @var{method}) 27 ## Plot slices of 3-D data/scalar fields.
26 ## Plot slices of 3-D data/scalar fields. Each element of the 3-dimensional 28 ##
29 ## Each element of the 3-dimensional
27 ## array @var{v} represents a scalar value at a location given by the 30 ## array @var{v} represents a scalar value at a location given by the
28 ## parameters @var{x}, @var{y}, and @var{z}. The parameters @var{x}, 31 ## parameters @var{x}, @var{y}, and @var{z}. The parameters @var{x},
29 ## @var{x}, and @var{z} are either 3-dimensional arrays of the same size 32 ## @var{x}, and @var{z} are either 3-dimensional arrays of the same size
30 ## as the array @var{v} in the "meshgrid" format or vectors. The 33 ## as the array @var{v} in the "meshgrid" format or vectors. The
31 ## parameters @var{xi}, etc. respect a similar format to @var{x}, etc., 34 ## parameters @var{xi}, etc. respect a similar format to @var{x}, etc.,
35 ## 38 ##
36 ## If @var{x}, @var{y}, @var{z} are omitted, they are assumed to be 39 ## If @var{x}, @var{y}, @var{z} are omitted, they are assumed to be
37 ## @code{x = 1:size (@var{v}, 2)}, @code{y = 1:size (@var{v}, 1)} and 40 ## @code{x = 1:size (@var{v}, 2)}, @code{y = 1:size (@var{v}, 1)} and
38 ## @code{z = 1:size (@var{v}, 3)}. 41 ## @code{z = 1:size (@var{v}, 3)}.
39 ## 42 ##
40 ## @var{Method} is one of: 43 ## @var{method} is one of:
41 ## 44 ##
42 ## @table @asis 45 ## @table @asis
43 ## @item "nearest" 46 ## @item "nearest"
44 ## Return the nearest neighbor. 47 ## Return the nearest neighbor.
45 ## 48 ##
52 ## @item "spline" 55 ## @item "spline"
53 ## Cubic spline interpolation---smooth first and second derivatives 56 ## Cubic spline interpolation---smooth first and second derivatives
54 ## throughout the curve. 57 ## throughout the curve.
55 ## @end table 58 ## @end table
56 ## 59 ##
57 ## The default method is @code{"linear"}. 60 ## The default method is "linear".
61 ##
62 ## If the first argument @var{hax} is an axis handle, then plot into this axis,
63 ## rather than the current axis handle returned by @code{gca}.
58 ## 64 ##
59 ## The optional return value @var{h} is a graphics handle to the created 65 ## The optional return value @var{h} is a graphics handle to the created
60 ## surface object. 66 ## surface object.
61 ## 67 ##
62 ## Examples: 68 ## Examples:
64 ## @example 70 ## @example
65 ## @group 71 ## @group
66 ## [x, y, z] = meshgrid (linspace (-8, 8, 32)); 72 ## [x, y, z] = meshgrid (linspace (-8, 8, 32));
67 ## v = sin (sqrt (x.^2 + y.^2 + z.^2)) ./ (sqrt (x.^2 + y.^2 + z.^2)); 73 ## v = sin (sqrt (x.^2 + y.^2 + z.^2)) ./ (sqrt (x.^2 + y.^2 + z.^2));
68 ## slice (x, y, z, v, [], 0, []); 74 ## slice (x, y, z, v, [], 0, []);
75 ##
69 ## [xi, yi] = meshgrid (linspace (-7, 7)); 76 ## [xi, yi] = meshgrid (linspace (-7, 7));
70 ## zi = xi + yi; 77 ## zi = xi + yi;
71 ## slice (x, y, z, v, xi, yi, zi); 78 ## slice (x, y, z, v, xi, yi, zi);
72 ## @end group 79 ## @end group
73 ## @end example 80 ## @end example
76 83
77 ## Author: Kai Habel <kai.habel@gmx.de> 84 ## Author: Kai Habel <kai.habel@gmx.de>
78 85
79 function h = slice (varargin) 86 function h = slice (varargin)
80 87
88 [hax, varargin, nargs] = __plt_get_axis_arg__ ("slice", varargin{:});
89
81 method = "linear"; 90 method = "linear";
82 nargs = nargin;
83 91
84 if (ischar (varargin{end})) 92 if (ischar (varargin{end}))
85 method = varargin{end}; 93 method = varargin{end};
86 nargs -= 1; 94 nargs -= 1;
87 endif 95 endif
88 96
89 if (nargs == 4) 97 if (nargs == 4)
90 v = varargin{1}; 98 v = varargin{1};
91 if (ndims (v) != 3) 99 if (ndims (v) != 3)
92 error ("slice: expect 3-dimensional array of values"); 100 error ("slice: V must be a 3-dimensional array of values");
93 endif 101 endif
94 [nx, ny, nz] = size (v); 102 [nx, ny, nz] = size (v);
95 [x, y, z] = meshgrid (1:nx, 1:ny, 1:nz); 103 [x, y, z] = meshgrid (1:nx, 1:ny, 1:nz);
96 sx = varargin{2}; 104 sx = varargin{2};
97 sy = varargin{3}; 105 sy = varargin{3};
98 sz = varargin{4}; 106 sz = varargin{4};
99 elseif (nargs == 7) 107 elseif (nargs == 7)
100 v = varargin{4}; 108 v = varargin{4};
101 if (ndims (v) != 3) 109 if (ndims (v) != 3)
102 error ("slice: expect 3-dimensional array of values"); 110 error ("slice: V must be a 3-dimensional array of values");
103 endif 111 endif
104 x = varargin{1}; 112 x = varargin{1};
105 y = varargin{2}; 113 y = varargin{2};
106 z = varargin{3}; 114 z = varargin{3};
107 if (isvector (x) && isvector (y) && isvector (z)) 115 if (isvector (x) && isvector (y) && isvector (z))
124 have_sval = false; 132 have_sval = false;
125 else 133 else
126 error ("slice: dimensional mismatch for (XI, YI, ZI) or (SX, SY, SZ)"); 134 error ("slice: dimensional mismatch for (XI, YI, ZI) or (SX, SY, SZ)");
127 endif 135 endif
128 136
129 newplot (); 137 oldfig = ifelse (isempty (hax), [], get (0, "currentfigure"));
130 ax = gca (); 138 unwind_protect
131 sidx = 1; 139 hax = newplot (hax);
132 maxv = max (v(:)); 140
133 minv = min (v(:)); 141 sidx = 1;
134 set (ax, "clim", [minv, maxv]); 142 minv = min (v(:));
135 143 maxv = max (v(:));
136 if (have_sval) 144 set (hax, "clim", [minv, maxv]);
137 ns = length (sx) + length (sy) + length (sz); 145
138 hs = zeros (ns,1); 146 if (have_sval)
139 [ny, nx, nz] = size (v); 147 ns = length (sx) + length (sy) + length (sz);
140 if (length (sz) > 0) 148 hs = zeros (ns,1);
141 for i = 1:length (sz) 149 [ny, nx, nz] = size (v);
142 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)), 150 if (length (sz) > 0)
143 squeeze (y(:,1,1)), sz(i)); 151 for i = 1:length (sz)
144 vz = squeeze (interp3 (x, y, z, v, xi, yi, zi, method)); 152 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)),
145 tmp(sidx++) = surface (xi, yi, sz(i) * ones (size (yi)), vz); 153 squeeze (y(:,1,1)), sz(i));
146 endfor 154 vz = squeeze (interp3 (x, y, z, v, xi, yi, zi, method));
147 endif 155 htmp(sidx++) = surface (xi, yi, sz(i) * ones (size (yi)), vz);
148 156 endfor
149 if (length (sy) > 0) 157 endif
150 for i = length (sy):-1:1 158
151 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)), sy(i), squeeze (z(1,1,:))); 159 if (length (sy) > 0)
152 vy = squeeze (interp3 (x, y, z, v, xi, yi, zi, method)); 160 for i = length (sy):-1:1
153 tmp(sidx++) = surface (squeeze (xi), 161 [xi, yi, zi] = meshgrid (squeeze (x(1,:,1)), sy(i), squeeze (z(1,1,:)));
154 squeeze (sy(i) * ones (size (zi))), 162 vy = squeeze (interp3 (x, y, z, v, xi, yi, zi, method));
155 squeeze (zi), vy); 163 htmp(sidx++) = surface (squeeze (xi),
156 endfor 164 squeeze (sy(i) * ones (size (zi))),
157 endif 165 squeeze (zi), vy);
158 166 endfor
159 if (length (sx) > 0) 167 endif
160 for i = length (sx):-1:1 168
161 [xi, yi, zi] = meshgrid (sx(i), squeeze (y(:,1,1)), squeeze (z(1,1,:))); 169 if (length (sx) > 0)
162 vx = squeeze (interp3 (x, y, z, v, xi, yi, zi, method)); 170 for i = length (sx):-1:1
163 tmp(sidx++) = surface (squeeze (sx(i) * ones (size (zi))), 171 [xi, yi, zi] = meshgrid (sx(i), squeeze (y(:,1,1)), squeeze (z(1,1,:)));
164 squeeze (yi), squeeze(zi), vx); 172 vx = squeeze (interp3 (x, y, z, v, xi, yi, zi, method));
165 endfor 173 htmp(sidx++) = surface (squeeze (sx(i) * ones (size (zi))),
166 endif 174 squeeze (yi), squeeze(zi), vx);
167 else 175 endfor
168 vi = interp3 (x, y, z, v, sx, sy, sz); 176 endif
169 tmp = surface (sx, sy, sz, vi); 177 else
170 endif 178 vi = interp3 (x, y, z, v, sx, sy, sz);
171 179 htmp = surface (sx, sy, sz, vi);
172 if (! ishold ()) 180 endif
173 set (ax, "view", [-37.5, 30.0], "box", "off", "xgrid", "on", 181
174 "ygrid", "on", "zgrid", "on"); 182 if (! ishold ())
175 endif 183 set (hax, "view", [-37.5, 30.0], "box", "off",
184 "xgrid", "on", "ygrid", "on", "zgrid", "on");
185 endif
186
187 unwind_protect_cleanup
188 if (! isempty (oldfig))
189 set (0, "currentfigure", oldfig);
190 endif
191 end_unwind_protect
176 192
177 if (nargout > 0) 193 if (nargout > 0)
178 h = tmp; 194 h = htmp;
179 endif 195 endif
180 196
181 endfunction 197 endfunction
182 198
183 199