diff scripts/plot/draw/private/__quiver__.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 7dcb6b4a4218
line wrap: on
line diff
--- a/scripts/plot/draw/private/__quiver__.m	Wed May 03 20:45:33 2023 +0200
+++ b/scripts/plot/draw/private/__quiver__.m	Wed May 03 22:52:33 2023 -0400
@@ -28,8 +28,7 @@
 ## Undocumented internal function.
 ## @end deftypefn
 
-function hg = __quiver__ (varargin)
-
+function [hax, hg]= __quiver__ (varargin)
   hax = varargin{1};
   is3d = varargin{2};
 
@@ -40,92 +39,120 @@
   ## in order to get equivalent visual results while keeping equivalent
   ## property values.
   arrowsize = 0.20;
-  firstnonnumeric = find (cellfun ("ischar", varargin(3:nargin)), 1);
-  if (isempty (firstnonnumeric))
-    firstnonnumeric = Inf;
+  lastnumeric = find (cellfun ("ischar", varargin(3:nargin)), 1) - 1;
 
+  if (isempty (lastnumeric))
+    lastnumeric = nargin;
     ## Recast non-float inputs as doubles to avoid erroneous plots.
     varargin(3:end) = cellfun ('double', varargin(3:end), ...
                                   "UniformOutput", false);
   else
-    firstnonnumeric += 2;
 
-    ## Recast non-float inputs as doubles.
-    varargin(3:firstnonnumeric-1) = cellfun ('double',
-                    varargin(3:firstnonnumeric-1), "UniformOutput", false);
+    lastnumeric += 2;
+    ## Recast non-float inputs as doubles to avoid erroneous plots.
+    varargin(3:lastnumeric) = cellfun ('double',
+                    varargin(3:lastnumeric), "UniformOutput", false);
 
     ## Check for scaling factor "off" and set it to 0.
-    if (strcmpi (varargin{firstnonnumeric}, "off"))
-      varargin(firstnonnumeric) = 0;
+    if ((nargin > lastnumeric) && strcmpi (varargin{lastnumeric+1}, "off"))
+      varargin(++lastnumeric) = 0;
+    endif
+  endif
+
+  if (is3d)
+    ## quiver3 3D input validation.
+    switch (lastnumeric)
+      case {6,7}
+        [z, u, v, w] = deal (varargin{3:6});
+        if (isvector (z) && ! isvector (u))
+          if (! size_equal (u, v, w))
+            error ("quiver3: U, V, and W must be the same size");
+          elseif (numel(z) != size (u, 3))
+            error (["quiver3: Z vector length must equal size of ", ...
+                        "U, V, and W in dim 3"]);
+          endif
+          [x, y, z] = meshgrid (1 : columns (u), 1 : rows (u), z);
+        else
+          if (! size_equal (z, u, v, w))
+            error ("quiver3: Z, U, V, and W must be the same size");
+          endif
+          [x, y] = meshgrid (1 : columns (u), 1 : rows (u), 1:size (u, 3));
+        endif
+
+      case {8,9}
+        [x, y, z, u, v, w] = deal (varargin{3:8});
+        if (isvector (x) && isvector (y) && isvector (z) && ! isvector (u))
+          if (! size_equal (u, v, w))
+            error ("quiver3: U, V, and W must be the same size");
+          elseif (numel(x) != columns (u))
+            error (["quiver3: X vector length must equal number of ", ...
+                        "columns in U, V, and W"]);
+          elseif (numel(y) != rows (u))
+            error (["quiver3: Y vector length must equal number of ", ...
+                        "rows in U, V, and W"]);
+          elseif (numel(z) != size (u, 3))
+            error (["quiver3: Z vector length must equal size of ", ...
+                        "U, V, and W in dim 3"]);
+          endif
+          [x, y, z] = meshgrid (x, y, z);
 
-      if ((firstnonnumeric) == nargin)
-        firstnonnumeric = Inf;
+        elseif (! size_equal (x, y, z, u, v, w))
+          error ("quiver3: X, Y, Z, U, V, and W must be the same size");
+        endif
+      otherwise
+        ## too few or too many numeric inputs before first style input
+        print_usage ("quiver3");
+    endswitch
+
+  else
+    ## quiver 2D input validation.
+    switch (lastnumeric)
+      case {4,5}
+        [u, v] = deal (varargin{3:4});
+        if (! size_equal (u, v))
+          error ("quiver: U and V must be the same size");
+        endif
+        [x, y] = meshgrid (1:columns (u), 1:rows (u));
+
+      case {6,7} #
+        [x, y, u, v] = deal (varargin{3:6});
+
+        if (isvector (x) && isvector (y) && ...
+                (! isvector (u) || ! isvector (v) ))
+           if (! size_equal (u, v))
+              error ("quiver: U and V must be the same size");
+           elseif (numel (x) != columns (u))
+              error (["quiver: X vector length must equal number of ", ...
+                        "columns in U and V"]);
+           elseif (numel (y) != rows (u))
+              error (["quiver: Y vector length must equal number of ", ...
+                        "rows in U and V"]);
+           endif
+          [x, y] = meshgrid (x, y);
+        elseif (! size_equal (x, y, u, v))
+          error ("quiver: X, Y, U, and V must be the same size");
+        endif
+      otherwise
+        ## too few or too many numeric inputs before first style input
+        print_usage ("quiver");
+    endswitch
+  endif
+
+  if (rem (lastnumeric, 2))
+    autoscale = varargin{lastnumeric}; # Last odd input is scale factor.
+
+    if (autoscale < 0 || ! isscalar (autoscale))
+      if (is3d)
+        error (["quiver3: scaling factor must be a non-negative scalar ", ...
+                 "or 'off'"]);
       else
-        firstnonnumeric++;
+        error (["quiver: scaling factor must be a non-negative scalar ", ...
+                 "or 'off'"]);
       endif
     endif
   endif
 
-  ioff = 3;
-  if (nargin < (6 + is3d) || firstnonnumeric < (6 + is3d))
-    if (is3d)
-      z = varargin{ioff++};
-    endif
-    u = varargin{ioff++};
-    v = varargin{ioff++};
-    if (is3d)
-      w = varargin{ioff++};
-    endif
-    if (is3d)
-      if (! size_equal (z, u, v, w))
-        error ("quiver3: Z, U, V, and W must be the same size");
-      endif
-    else
-      if (! size_equal (u, v))
-        error ("quiver: U and V must be the same size");
-      endif
-    endif
-    [x, y] = meshgrid (1:columns (u), 1:rows (u));
-
-    if (nargin >= ioff && isnumeric (varargin{ioff})
-        && isscalar (varargin{ioff}))
-      autoscale = varargin{ioff++};
-    endif
-  else
-    x = varargin{ioff++};
-    y = varargin{ioff++};
-    if (is3d)
-      z = varargin{ioff++};
-    endif
-    u = varargin{ioff++};
-    v = varargin{ioff++};
-    if (is3d)
-      w = varargin{ioff++};
-      if (isvector (x) && isvector (y) && isvector (z)
-          && (! isvector (u) || ! isvector (v) || ! isvector (w)))
-        [x, y, z] = meshgrid (x, y, z);
-      endif
-    else
-      if (isvector (x) && isvector (y) && (! isvector (u) || ! isvector (v)))
-        [x, y] = meshgrid (x, y);
-      endif
-    endif
-    if (is3d)
-      if (! size_equal (x, y, z, u, v, w))
-        error ("quiver3: X, Y, Z, U, V, and W must be the same size");
-      endif
-    else
-      if (! size_equal (x, y, u, v))
-        error ("quiver: X, Y, U, and V must be the same size");
-      endif
-    endif
-
-    if (nargin >= ioff && isnumeric (varargin{ioff})
-        && isscalar (varargin{ioff}))
-      autoscale = varargin{ioff++};
-    endif
-  endif
-
+  ioff = lastnumeric + 1;
   have_filled = false;
   have_line_spec = false;
   args = {};
@@ -193,6 +220,7 @@
     endif
   endif
 
+  hax = newplot (hax);
   hstate = get (hax, "nextplot");
   unwind_protect
     if (have_line_spec)
@@ -352,6 +380,7 @@
     if (! isempty (args))
       set (hg, args{:});
     endif
+
   unwind_protect_cleanup
     set (hax, "nextplot", hstate);
   end_unwind_protect