changeset 17439:000be929e835

trimesh.m: Overhaul function to support color matrix argument. * scripts/plot/trimesh.m: Add documentation about C matrix argument. Also return handle H for 2-D trimesh (equivalent to triplot). Add code to check for 4th argument being a color matrix. Set FaceColor to white in patch which corresponds to "hidden on". Turn off axis plot box for visual compatibility with Matlab. Add %!error tests for input validation.
author Rik <rik@octave.org>
date Thu, 19 Sep 2013 09:33:55 -0700
parents 791c117eb2cf
children 0fa126e9944e
files scripts/plot/trimesh.m
diffstat 1 files changed, 39 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/trimesh.m	Wed Sep 18 20:29:19 2013 -0700
+++ b/scripts/plot/trimesh.m	Thu Sep 19 09:33:55 2013 -0700
@@ -39,7 +39,9 @@
 ## change the colormap to control the appearance.
 ##
 ## Optionally, the color of the mesh can be specified independently of @var{z}
-## by supplying a color matrix, @var{c}.
+## by supplying a color matrix, @var{c}.  If @var{z} has N elements, then
+## @var{c} should be an Nx1 vector for colormap data or an Nx3 matrix for
+## RGB data.
 ##
 ## Any property/value pairs are passed directly to the underlying patch object.
 ##
@@ -55,21 +57,39 @@
   endif
 
   if (nargin == 3)
-    triplot (tri, x, y);
+    htmp = triplot (tri, x, y);
   elseif (ischar (z))
-    triplot (tri, x, y, z, varargin{:});
+    htmp = triplot (tri, x, y, z, varargin{:});
   else
+    ## Process color argument
+    if (nargin > 4 && isnumeric (varargin{1}))
+      c = varargin{1};
+      varargin(1) = [];
+      if (isvector (c))
+        if (numel (c) != numel (z))
+          error ("trimesh: C must have 'numel (Z)' elements");
+        endif
+        c = c(:);
+      elseif (rows (c) != numel (z) || columns (c) != 3)
+        error ("trimesh: TrueColor C matrix must be 'numel (Z)' rows by 3 columns");
+      endif
+    else
+      c = z(:);
+    endif
+
     hax = newplot ();
-    handle = patch ("Vertices", [x(:), y(:), z(:)], "Faces", tri,
-                    "FaceColor", "none", "EdgeColor", __next_line_color__ (),
-                    varargin{:});
+
+    htmp = patch ("Vertices", [x(:), y(:), z(:)], "Faces", tri,
+                  "FaceVertexCdata", c, "EdgeColor", "flat", "FaceColor", "w",
+                  varargin{:});
     if (! ishold ())
-      set (hax, "view", [-37.5, 30],
+      set (hax, "view", [-37.5, 30], "box", "off",
                 "xgrid", "on", "ygrid", "on", "zgrid", "on");
     endif
-    if (nargout > 0)
-      h = handle;
-    endif
+  endif
+
+  if (nargout > 0)
+    h = htmp;
   endif
 
 endfunction
@@ -87,3 +107,12 @@
 %! tri = delaunay (x(:), y(:));
 %! trimesh (tri, x(:), y(:), z(:));
 
+%% Test input validation
+%!error trimesh ()
+%!error trimesh (1)
+%!error trimesh (1,2)
+%!error <C must have 'numel \(Z\)' elements> trimesh (1,2,3,4,[5 6])
+%!error <C must have 'numel \(Z\)' elements> trimesh (1,2,3,4,[5 6]')
+%!error <TrueColor C matrix must> trimesh ([1;1],[2;2],[3;3],[4;4],zeros(3,3))
+%!error <TrueColor C matrix must> trimesh ([1;1],[2;2],[3;3],[4;4],zeros(2,2))
+