comparison scripts/plot/draw/quiver3.m @ 32074:03fe0b635d2e

quiver/quiver3: Overhaul input processing, validation, and add BISTs. * scripts/plot/draw/private/__quiver__.m: Overhaul numeric input validation. Simplify input classification using numeric input count switch statements and avoid quiver3 miscount due to scale factor. Add error messages for all valid numeric input combinations including vector x,y,z and scale factor. Move newplot command from quiver/quiver3 into __quiver__ after numeric input validation. Add hax as an output argument to return any changes back to calling function. * scripts/plot/draw/quiver.m: Remove newplot call. Update __quiver__ call to include hax as a return variable. Update docstring with note that line style and name-value pairs can both be provided but linstyle must appear first. Add BISTs to check standard inputs with single and multiple arrows, arrowhead shape, vector and array inputs, proper treatment of scaling factor "off", some simple input styles, and input validation BISTs to cover all numeric input errors. Added known failing BIST for linestyle+pair arrowhead showing when it should stay off (bug #64143). * scripts/plot/draw/quiver3.m: Remove newplot call. Update __quiver__ call to include hax as a return variable. Update docstring with note that line style and name-value pairs can both be provided but linstyle must appear first. Add BISTs to check standard inputs with single and multiple arrows, vector and array inputs, and input validation BISTs to cover all numeric input errors. * etc/NEWS.9.md: Update quiver/quiver3 improvement description under General Improvements.
author Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
date Wed, 03 May 2023 22:52:33 -0400
parents ada96a467a28
children 998cba4600e9
comparison
equal deleted inserted replaced
32073:24752aa8be11 32074:03fe0b635d2e
34 ## 34 ##
35 ## Plot a 3-D vector field with arrows. 35 ## Plot a 3-D vector field with arrows.
36 ## 36 ##
37 ## Plot the (@var{u}, @var{v}, @var{w}) components of a vector field at the 37 ## Plot the (@var{u}, @var{v}, @var{w}) components of a vector field at the
38 ## grid points defined by (@var{x}, @var{y}, @var{z}). If the grid is uniform 38 ## grid points defined by (@var{x}, @var{y}, @var{z}). If the grid is uniform
39 ## then @var{x}, @var{y}, and @var{z} can be specified as vectors and 39 ## then @var{x}, @var{y}, and @var{z} can be specified as grid vectors and
40 ## @code{meshgrid} is used to create the 3-D grid. 40 ## @code{meshgrid} is used to create the 3-D grid.
41 ## 41 ##
42 ## If @var{x} and @var{y} are not given they are assumed to be 42 ## If @var{x} and @var{y} are not given they are assumed to be
43 ## @code{(1:@var{m}, 1:@var{n})} where 43 ## @code{(1:@var{m}, 1:@var{n})} where
44 ## @code{[@var{m}, @var{n}] = size (@var{u})}. 44 ## @code{[@var{m}, @var{n}] = size (@var{u})}.
51 ## The style to use for the plot can be defined with a line style @var{style} 51 ## The style to use for the plot can be defined with a line style @var{style}
52 ## of the same format as the @code{plot} command. If a marker is specified 52 ## of the same format as the @code{plot} command. If a marker is specified
53 ## then the markers are drawn at the origin of the vectors (which are the grid 53 ## then the markers are drawn at the origin of the vectors (which are the grid
54 ## points defined by @var{x}, @var{y}, @var{z}). When a marker is specified, 54 ## points defined by @var{x}, @var{y}, @var{z}). When a marker is specified,
55 ## the arrowhead is not drawn. If the argument @qcode{"filled"} is given then 55 ## the arrowhead is not drawn. If the argument @qcode{"filled"} is given then
56 ## the markers are filled. 56 ## the markers are filled. If name-value plot style properties are used, they
57 ## must appear in pairs and follow any other plot style arguments.
57 ## 58 ##
58 ## If the first argument @var{hax} is an axes handle, then plot into this axes, 59 ## If the first argument @var{hax} is an axes handle, then plot into this axes,
59 ## rather than the current axes returned by @code{gca}. 60 ## rather than the current axes returned by @code{gca}.
60 ## 61 ##
61 ## The optional return value @var{h} is a graphics handle to a quiver object. 62 ## The optional return value @var{h} is a graphics handle to a quiver object.
87 oldfig = []; 88 oldfig = [];
88 if (! isempty (hax)) 89 if (! isempty (hax))
89 oldfig = get (0, "currentfigure"); 90 oldfig = get (0, "currentfigure");
90 endif 91 endif
91 unwind_protect 92 unwind_protect
92 hax = newplot (hax); 93 [hax, htmp] = __quiver__ (hax, true, varargin{:});
93 htmp = __quiver__ (hax, true, varargin{:});
94 94
95 if (! ishold ()) 95 if (! ishold ())
96 set (hax, "view", [-37.5, 30], 96 set (hax, "view", [-37.5, 30],
97 "xgrid", "on", "ygrid", "on", "zgrid", "on"); 97 "xgrid", "on", "ygrid", "on", "zgrid", "on");
98 endif 98 endif
105 if (nargout > 0) 105 if (nargout > 0)
106 h = htmp; 106 h = htmp;
107 endif 107 endif
108 108
109 endfunction 109 endfunction
110
111 110
112 %!demo 111 %!demo
113 %! clf; 112 %! clf;
114 %! colormap ("default"); 113 %! colormap ("default");
115 %! [x, y, z] = peaks (25); 114 %! [x, y, z] = peaks (25);
133 %! hold off; 132 %! hold off;
134 %! shading interp; 133 %! shading interp;
135 %! title ({"quiver3() of surface normals to peaks() function"; ... 134 %! title ({"quiver3() of surface normals to peaks() function"; ...
136 %! 'shading "interp"'}); 135 %! 'shading "interp"'});
137 136
137 ## Check standard inputs, single arrow.
138 %!test
139 %! hf = figure ("visible", "off");
140 %! hax = gca ();
141 %! unwind_protect
142 %!
143 %! h = quiver3 (hax, 0, 1, 2, 3);
144 %! children = get (h, "children");
145 %! childxdata = get (children, "xdata");
146 %! childydata = get (children, "ydata");
147 %! childzdata = get (children, "zdata");
148 %! stemchild = find (cellfun (@numel, childxdata) == 3);
149 %! arrowheadchild = find (cellfun (@numel, childxdata) == 4);
150 %! assert (childxdata{stemchild}(1), 1, eps);
151 %! assert (childxdata{stemchild}(2), 1 + 1*0.9, eps);
152 %! assert (isnan (childxdata{stemchild}(3)));
153 %! assert (childxdata{arrowheadchild}(2), 1 + 1*0.9, eps);
154 %! assert (isnan (childxdata{arrowheadchild}(4)));
155 %! assert (childydata{stemchild}(1), 1, eps);
156 %! assert (childydata{stemchild}(2), 1 + 2*0.9, eps);
157 %! assert (isnan (childydata{stemchild}(3)));
158 %! assert (childydata{arrowheadchild}(2), 1 + 2*0.9, eps);
159 %! assert (isnan (childydata{arrowheadchild}(4)));
160 %! assert (childzdata{stemchild}(1), 0, eps);
161 %! assert (childzdata{stemchild}(2), 0 + 3*0.9, eps);
162 %! assert (isnan (childzdata{stemchild}(3)));
163 %! assert (childzdata{arrowheadchild}(2), 0 + 3*0.9, eps);
164 %! assert (isnan (childzdata{arrowheadchild}(4)));
165 %!
166 %! h = quiver3 (hax, 1, 1, 0, 1, 2, 3);
167 %! children = get (h, "children");
168 %! childxdata = get (children, "xdata");
169 %! childydata = get (children, "ydata");
170 %! childzdata = get (children, "zdata");
171 %! stemchild = find (cellfun (@numel, childxdata) == 3);
172 %! arrowheadchild = find (cellfun (@numel, childxdata) == 4);
173 %! assert (childxdata{stemchild}(1), 1, eps);
174 %! assert (childxdata{stemchild}(2), 1 + 1*0.9, eps);
175 %! assert (isnan (childxdata{stemchild}(3)));
176 %! assert (childxdata{arrowheadchild}(2), 1 + 1*0.9, eps);
177 %! assert (isnan (childxdata{arrowheadchild}(4)));
178 %! assert (childydata{stemchild}(1), 1, eps);
179 %! assert (childydata{stemchild}(2), 1 + 2*0.9, eps);
180 %! assert (isnan (childydata{stemchild}(3)));
181 %! assert (childydata{arrowheadchild}(2), 1 + 2*0.9, eps);
182 %! assert (isnan (childydata{arrowheadchild}(4)));
183 %! assert (childzdata{stemchild}(1), 0, eps);
184 %! assert (childzdata{stemchild}(2), 0 + 3*0.9, eps);
185 %! assert (isnan (childzdata{stemchild}(3)));
186 %! assert (childzdata{arrowheadchild}(2), 0 + 3*0.9, eps);
187 %! assert (isnan (childzdata{arrowheadchild}(4)));
188 %! unwind_protect_cleanup
189 %! close (hf);
190 %! end_unwind_protect
191
192 ## Check standard inputs, multiple arrows.
193 %!test
194 %! hf = figure ("visible", "off");
195 %! hax = gca ();
196 %! unwind_protect
197 %!
198 %! a = reshape(1:12,2,3,2);
199 %! x = 1:3; y = 1:2; z = 1:2;
200 %! [xx,yy,zz] = meshgrid (x,y,z);
201 %! numpts = 12;
202 %! sf= sqrt(sumsq([1/3 1/2 11/6])/432); # Actual internal scale factor, z=a.
203 %! sf2= sqrt(sumsq([1/3 1/2 1/6])/432); # z vector internal scale factor.
204 %!
205 %! h = quiver3 (hax, a, a, a, a, 1); # No x,y input.
206 %! children = get (h, "children");
207 %! childxdata = get (children, "xdata");
208 %! childydata = get (children, "ydata");
209 %! childzdata = get (children, "zdata");
210 %! basechild = find (cellfun (@numel, childxdata) == numpts);
211 %! stemchild = find (cellfun (@numel, childxdata) == numpts*3);
212 %! arrowheadchild = find (cellfun (@numel, childxdata) == numpts*4);
213 %! ## Check all bases.
214 %! assert (childxdata{basechild}, [1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3]);
215 %! assert (childydata{basechild}, [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
216 %! assert (childzdata{basechild}, [1:12]);
217 %! ## Check first arrow.
218 %! assert (childxdata{stemchild}(1), 1, eps);
219 %! assert (childxdata{stemchild}(2), 1 + 1*sf, eps);
220 %! assert (isnan (childxdata{stemchild}(3)));
221 %! assert (childxdata{arrowheadchild}(2), 1 + 1*sf, eps);
222 %! assert (isnan (childxdata{arrowheadchild}(4)));
223 %! assert (childydata{stemchild}(1), 1, eps);
224 %! assert (childydata{stemchild}(2), 1 + 1*sf, eps);
225 %! assert (isnan (childydata{stemchild}(3)));
226 %! assert (childydata{arrowheadchild}(2), 1 + 1*sf, eps);
227 %! assert (isnan (childydata{arrowheadchild}(4)));
228 %! assert (childzdata{stemchild}(1), 1, eps);
229 %! assert (childzdata{stemchild}(2), 1 + 1*sf, eps);
230 %! assert (isnan (childzdata{stemchild}(3)));
231 %! assert (childzdata{arrowheadchild}(2), 1 + 1*sf, eps);
232 %! assert (isnan (childzdata{arrowheadchild}(4)));
233 %! ## Check last arrow.
234 %! assert (childxdata{stemchild}(numpts*3-2), 3, eps);
235 %! assert (childxdata{stemchild}(numpts*3-1), 3 + 12*sf, eps);
236 %! assert (isnan (childxdata{stemchild}(end)));
237 %! assert (childxdata{arrowheadchild}(numpts*4-2), 3 + 12*sf, eps);
238 %! assert (isnan (childxdata{arrowheadchild}(end)));
239 %! assert (childydata{stemchild}(numpts*3-2), 2, eps);
240 %! assert (childydata{stemchild}(numpts*3-1), 2 + 12*sf, eps);
241 %! assert (isnan (childydata{stemchild}(end)));
242 %! assert (childydata{arrowheadchild}(numpts*4-2), 2 + 12*sf, eps);
243 %! assert (isnan (childydata{arrowheadchild}(end)));
244 %! assert (childzdata{stemchild}(numpts*3-2), 12, eps);
245 %! assert (childzdata{stemchild}(numpts*3-1), 12 + 12*sf, eps);
246 %! assert (isnan (childzdata{stemchild}(end)));
247 %! assert (childzdata{arrowheadchild}(numpts*4-2), 12 + 12*sf, eps);
248 %! assert (isnan (childzdata{arrowheadchild}(end)));
249 %!
250 %! h = quiver3 (hax, xx, yy, a, a, a, a, 1); # x,y input as matrices.
251 %! children = get (h, "children");
252 %! childxdata = get (children, "xdata");
253 %! childydata = get (children, "ydata");
254 %! childzdata = get (children, "zdata");
255 %! basechild = find (cellfun (@numel, childxdata) == numpts);
256 %! stemchild = find (cellfun (@numel, childxdata) == numpts*3);
257 %! arrowheadchild = find (cellfun (@numel, childxdata) == numpts*4);
258 %! ## Check all bases.
259 %! assert (childxdata{basechild}, [1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3]);
260 %! assert (childydata{basechild}, [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
261 %! assert (childzdata{basechild}, [1:12]);
262 %! ## Check first arrow.
263 %! assert (childxdata{stemchild}(1), 1, eps);
264 %! assert (childxdata{stemchild}(2), 1 + 1*sf, eps);
265 %! assert (isnan (childxdata{stemchild}(3)));
266 %! assert (childxdata{arrowheadchild}(2), 1 + 1*sf, eps);
267 %! assert (isnan (childxdata{arrowheadchild}(4)));
268 %! assert (childydata{stemchild}(1), 1, eps);
269 %! assert (childydata{stemchild}(2), 1 + 1*sf, eps);
270 %! assert (isnan (childydata{stemchild}(3)));
271 %! assert (childydata{arrowheadchild}(2), 1 + 1*sf, eps);
272 %! assert (isnan (childydata{arrowheadchild}(4)));
273 %! assert (childzdata{stemchild}(1), 1, eps);
274 %! assert (childzdata{stemchild}(2), 1 + 1*sf, eps);
275 %! assert (isnan (childzdata{stemchild}(3)));
276 %! assert (childzdata{arrowheadchild}(2), 1 + 1*sf, eps);
277 %! assert (isnan (childzdata{arrowheadchild}(4)));
278 %! ## Check last arrow.
279 %! assert (childxdata{stemchild}(numpts*3-2), 3, eps);
280 %! assert (childxdata{stemchild}(numpts*3-1), 3 + 12*sf, eps);
281 %! assert (isnan (childxdata{stemchild}(end)));
282 %! assert (childxdata{arrowheadchild}(numpts*4-2), 3 + 12*sf, eps);
283 %! assert (isnan (childxdata{arrowheadchild}(end)));
284 %! assert (childydata{stemchild}(numpts*3-2), 2, eps);
285 %! assert (childydata{stemchild}(numpts*3-1), 2 + 12*sf, eps);
286 %! assert (isnan (childydata{stemchild}(end)));
287 %! assert (childydata{arrowheadchild}(numpts*4-2), 2 + 12*sf, eps);
288 %! assert (isnan (childydata{arrowheadchild}(end)));
289 %! assert (childzdata{stemchild}(numpts*3-2), 12, eps);
290 %! assert (childzdata{stemchild}(numpts*3-1), 12 + 12*sf, eps);
291 %! assert (isnan (childzdata{stemchild}(end)));
292 %! assert (childzdata{arrowheadchild}(numpts*4-2), 12 + 12*sf, eps);
293 %! assert (isnan (childzdata{arrowheadchild}(end)));
294 %!
295 %! h = quiver3 (hax, x, y, z, a, a, a, 1); # x,y z input as vectors.
296 %! children = get (h, "children");
297 %! childxdata = get (children, "xdata");
298 %! childydata = get (children, "ydata");
299 %! childzdata = get (children, "zdata");
300 %! basechild = find (cellfun (@numel, childxdata) == numpts);
301 %! stemchild = find (cellfun (@numel, childxdata) == numpts*3);
302 %! arrowheadchild = find (cellfun (@numel, childxdata) == numpts*4);
303 %! ## Check all bases.
304 %! assert (childxdata{basechild}, [1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3]);
305 %! assert (childydata{basechild}, [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
306 %! assert (childzdata{basechild}, [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]);
307 %! ## Check first arrow.
308 %! assert (childxdata{stemchild}(1), 1, eps);
309 %! assert (childxdata{stemchild}(2), 1 + 1*sf2, eps);
310 %! assert (isnan (childxdata{stemchild}(3)));
311 %! assert (childxdata{arrowheadchild}(2), 1 + 1*sf2, eps);
312 %! assert (isnan (childxdata{arrowheadchild}(4)));
313 %! assert (childydata{stemchild}(1), 1, eps);
314 %! assert (childydata{stemchild}(2), 1 + 1*sf2, eps);
315 %! assert (isnan (childydata{stemchild}(3)));
316 %! assert (childydata{arrowheadchild}(2), 1 + 1*sf2, eps);
317 %! assert (isnan (childydata{arrowheadchild}(4)));
318 %! assert (childzdata{stemchild}(1), 1, eps);
319 %! assert (childzdata{stemchild}(2), 1 + 1*sf2, eps);
320 %! assert (isnan (childzdata{stemchild}(3)));
321 %! assert (childzdata{arrowheadchild}(2), 1 + 1*sf2, eps);
322 %! assert (isnan (childzdata{arrowheadchild}(4)));
323 %! ## Check last arrow.
324 %! assert (childxdata{stemchild}(numpts*3-2), 3, eps);
325 %! assert (childxdata{stemchild}(numpts*3-1), 3 + 12*sf2, eps);
326 %! assert (isnan (childxdata{stemchild}(end)));
327 %! assert (childxdata{arrowheadchild}(numpts*4-2), 3 + 12*sf2, eps);
328 %! assert (isnan (childxdata{arrowheadchild}(end)));
329 %! assert (childydata{stemchild}(numpts*3-2), 2, eps);
330 %! assert (childydata{stemchild}(numpts*3-1), 2 + 12*sf2, eps);
331 %! assert (isnan (childydata{stemchild}(end)));
332 %! assert (childydata{arrowheadchild}(numpts*4-2), 2 + 12*sf2, eps);
333 %! assert (isnan (childydata{arrowheadchild}(end)));
334 %! assert (childzdata{stemchild}(numpts*3-2), 2, eps);
335 %! assert (childzdata{stemchild}(numpts*3-1), 2 + 12*sf2, eps);
336 %! assert (isnan (childzdata{stemchild}(end)));
337 %! assert (childzdata{arrowheadchild}(numpts*4-2), 2 + 12*sf2, eps);
338 %! assert (isnan (childzdata{arrowheadchild}(end)));
339 %! unwind_protect_cleanup
340 %! close (hf);
341 %! end_unwind_protect
342
138 ##Test input validation 343 ##Test input validation
139 %!error <Invalid call> quiver3 () 344 %!error <Invalid call> quiver3 ()
140 %!error <Invalid call> quiver3 (1.1) 345 %!error <Invalid call> quiver3 (1.1)
141 %!error <Invalid call> quiver3 (1.1, 2) 346 %!error <Invalid call> quiver3 (1.1, 2)
142 %!error <Invalid call> quiver3 (1.1, 2, 3) 347 %!error <Invalid call> quiver3 (1.1, 2, 3)
143 348 %!error <Invalid call> quiver3 (1.1, 2, 3, "foo")
349 %!error <Invalid call> quiver3 (1.1, 2, 3, 4, 5, 6, 7, 8, "foo")
350 %!error <U, V, and W must be the same> quiver3 (30, [40 50], 60, 70)
351 %!error <Z vector length must equal size of> quiver3 ([30 40], eye(3), eye(3), eye(3))
352 %!error <Z, U, V, and W must be the same> quiver3 ([30 40], 50, 60, 70)
353 %!error <Z, U, V, and W must be the same> quiver3 (eye(2), eye(3), eye(2), eye(2))
354 %!error <Z, U, V, and W must be the same> quiver3 (eye(2), eye(2), eye(3), eye(2))
355 %!error <Z, U, V, and W must be the same> quiver3 (eye(2), eye(2), eye(2), eye(3))
356 %!error <U, V, and W must be the same size> quiver3 ([1:2], [1:2], 1, eye(3), eye(2), eye(2))
357 %!error <U, V, and W must be the same size> quiver3 ([1:2], [1:2], 1, eye(2), eye(3), eye(2))
358 %!error <U, V, and W must be the same size> quiver3 ([1:2], [1:2], 1, eye(2), eye(2), eye(3))
359 %!error <X vector length must equal number of> quiver3 ([1:3], [1:2], 1, eye(2), eye(2), eye(2))
360 %!error <Y vector length must equal number of> quiver3 ([1:2], [1:3], 1, eye(2), eye(2), eye(2))
361 %!error <Z vector length must equal size of> quiver3 ([1:2], [1:2], [1:2], eye(2), eye(2), eye(2))
362 %!error <X, Y, Z, U, V, and W must be the same size> quiver3 (eye(3), eye(2), eye(2), eye(2), eye(2), eye(2))
363 %!error <X, Y, Z, U, V, and W must be the same size> quiver3 (eye(2), eye(3), eye(2), eye(2), eye(2), eye(2))
364 %!error <X, Y, Z, U, V, and W must be the same size> quiver3 (eye(2), eye(2), eye(3), eye(2), eye(2), eye(2))
365 %!error <X, Y, Z, U, V, and W must be the same size> quiver3 (eye(2), eye(2), eye(2), eye(3), eye(2), eye(2))
366 %!error <X, Y, Z, U, V, and W must be the same size> quiver3 (eye(2), eye(2), eye(2), eye(2), eye(3), eye(2))
367 %!error <X, Y, Z, U, V, and W must be the same size> quiver3 (eye(2), eye(2), eye(2), eye(2), eye(2), eye(3))
368 %!error <scaling factor must be> quiver3 (10, 20, 30, 40, -5)
369 %!error <scaling factor must be> quiver3 (10, 20, 30, 40, [1 2])
370 %!error <scaling factor must be> quiver3 (10, 20, 30, 40, 50, 60, -5)
371 %!error <scaling factor must be> quiver3 (10, 20, 30, 40, 50, 60, [1 2])