diff scripts/plot/__patch__.m @ 7020:e31f12bb9194

[project @ 2007-10-13 05:13:28 by dbateman]
author dbateman
date Sat, 13 Oct 2007 05:13:29 +0000
parents 93c65f2a5668
children db85cf23875e
line wrap: on
line diff
--- a/scripts/plot/__patch__.m
+++ b/scripts/plot/__patch__.m
@@ -24,17 +24,19 @@
 
 ## Author: Kai Habel
 
-function h = __patch__ (p, varargin)
-
+function [h, fail] = __patch__ (p, varargin)
+  fail = false;
   if (nargin < 3)
-    print_usage ();
+    fail = true;
+    return;
   endif
 
   iarg = 1;
-  have_x = have_z = have_c = false;
+  have_x = have_z = have_c = have_faces = false;
   if (isnumeric (varargin{1}))
     if (! isnumeric (varargin{2}))
-      print_usage ();
+      fail = true;
+      return;
     endif
 
     x = varargin{1};
@@ -43,14 +45,40 @@
     iarg += 2;
 
     if (nargin > 3 && ndims (varargin{3}) == 2 && ndims (x) == 2
-	&& size (varargin{3}) == size (x))
+	&& isequal (size (varargin{3}), size (x)))
       z = varargin {3};
       have_z = true;
       iarg++;
     endif
+  elseif (ischar (varargin{1}) && (strcmp (tolower (varargin{1}), "faces") || 
+				strcmp (tolower (varargin{1}), "vertices")))
+    if (! isnumeric (varargin{2}))
+      fail = true;
+      return;
+    endif
+    
+    if (strcmp (tolower (varargin{1}), "faces"))
+      faces = varargin{2};
+      if (strcmp (tolower (varargin{3}), "vertices"))
+	vert = varargin{4};
+	have_faces = true;
+      endif
+    elseif (strcmp (tolower (varargin{3}), "vertices"))
+      vert = varargin{2};
+      if (strcmp (tolower (varargin{3}), "faces"))
+	faces = varargin{4};
+	have_faces = true;
+      endif
+    endif
+    if (!have_faces)
+      fail = true;
+      return;
+    else
+      iarg += 4;
+    endif
   endif
 
-  if (have_x && nargin > iarg)
+  if ((have_x || have_faces) && nargin > iarg)
     if (isnumeric (varargin{iarg}))
       c = varargin{iarg};
       have_c = true;
@@ -68,7 +96,8 @@
   endif
 
   if (rem (nargin - iarg, 2) != 0)
-    print_usage ();
+    fail = true;
+    return;
   endif
 
   if (have_x)
@@ -79,77 +108,92 @@
 	z = z(:);
       endif
     endif
-
     [nr, nc] = size (x);
-
-    for i = 1 : nc
-      h = __go_patch__ (p);
-      ax = get (h, "parent");
-      if (have_x)
-	set (h, "xdata", x (:, i), "ydata", y (:, i));
-	if (have_z)
-	  set (h, "zdata", z (:, i));
-	endif
-      endif
-
-      if (have_c)
-	if (ndims (c) == 2 && ((nr > 3 && size (c, 2) == nc)
-			       || (size (c, 1) > 1 && size (c, 2) == nc)))
-	  c2 = c (:, i);
-	elseif (ndims (c) == 3)
-	  c2 = permute (c(:,i,:), [1, 3, 2]);
-	else
-	  c2 = c;
-	endif
-
-	if (ischar (c2))
-	  set (h, "facecolor", c2);
-	elseif (numel (c2) == 1)
-	  if (isnan (c))
-	    set (h, "facecolor", [1, 1, 1]);
-	    set (h, "cdata", c2);
-	  elseif (isnumeric (c2))
-	    ## Have color index.
-	    set (h, "facecolor", "flat");
-	    set (h, "cdata", c2);
-	    clim = get(ax, "clim");
-	    if (c2 < clim(1))
-              set (ax, "clim", [c2, clim(2)])
-	    endif
-	    if (c2 > clim(2))
-              set (ax, "clim", [clim(1), c2])
-	    endif
-	  else
-	    ## Unknown color value.
-	    error ("patch: color value not valid");
-	  endif
-	elseif (numel (c2) == 3)
-	  ## Have rgb/rgba value.
-	  set (h, "facecolor", c2);
-	else
-	  ## Color vector.
-	  if (length (c2) != length (x) || length (c2) != length (y))
-	    error ("patch: size of x, y, and c must be equal")
-	  else
-	    set (h, "facecolor", "interp");
-	    set(h, "cdata", c2);
-	    if (abs(max(c2) - min(c2)) < eps)
-              set (ax, "clim", [c2(1)-1, c2(1)+1])
-	    else
-              set (ax, "clim", [min(c2), max(c2)]);
-	    endif
-	  endif
-	endif
-      else
-	set (h, "facecolor", [0, 1, 0]);
-      endif
-
-      if (nargin > iarg + 1)
-	set (h, varargin{iarg:end});
+    if (have_z)
+      vert = [x(:), y(:), z(:)];
+    else
+      vert = [x(:), y(:)];
+    endif
+    faces = reshape (1:numel(x), size(x,2), size(x,1));
+  elseif (have_faces)
+    nr = size (faces, 2);
+    nc = size (faces, 1);
+    idx = faces .';
+    for i = 1: nc
+      t1 = isnan (idx (:,i));
+      if (any (t1))
+	t2 = find (t1(1:end-1) != t1(2:end))(1);
+        idx(t1,i) = idx(t2,i);
       endif
     endfor
+    x = vert(:,1)(idx);
+    y = vert(:,2)(idx);
+    if (size(vert,2) > 2)
+      have_z = true;
+      z = vert(:,3)(idx);
+    endif
   else
     error ("patch: not supported");
   endif
 
+  h = __go_patch__ (p);
+  ax = get (h, "parent");
+
+  cargs = {};
+  if (have_c)
+    if (ischar (c))
+      cargs{1} = "facecolor";
+      cargs{2} = c;
+    elseif (isvector(c) && numel(c) == nc)
+      if (isnan (c))
+	cargs{1} = "facecolor";
+	cargs{2} = [1, 1, 1];
+	cargs{3} = "cdata";
+	cargs{4} = c;
+      elseif (isnumeric (c))
+	cargs{1} = "facecolor";
+	cargs{2} = "flat";
+	cargs{3} = "cdata";
+	cargs{4} = c;
+	clim = get(ax, "clim");
+	if (c(1) < clim(1))
+          set (ax, "clim", [c(1), clim(2)])
+	endif
+	if (c(1) > clim(2))
+          set (ax, "clim", [clim(1), c(1)])
+	endif
+      else
+	error ("patch: color value not valid");
+      endif
+    elseif (size(c, ndims(c)) == 3)
+      cargs{1} = "facecolor";
+      cargs{2} = "flat";
+      cargs{3} = "cdata";
+      cargs{4} = c;
+    else
+      ## Color Vectors
+
+      if (rows (c2) != rows (x) || rows (c2) != length (y))
+	error ("patch: size of x, y, and c must be equal")
+      else
+	cargs{1} = "facecolor";
+	cargs{2} = "interp";
+	if (abs(max(c2(:)) - min(c2(:))) < eps)
+          set (ax, "clim", [c2(1)-1, c2(1)+1])
+	else
+          set (ax, "clim", [min(c2(:)), max(c2(:))]);
+	endif
+      endif
+    endif
+  else
+    cargs{1} = "facecolor";
+    cargs{2} = [0, 1, 0];
+  endif
+
+  set (h, "xdata", x, "ydata", y, "faces", faces, "vertices", vert, ...
+       cargs{:}, varargin{iarg:end});
+  if (have_z)
+    set (h, "zdata", z);
+  endif
+ 
 endfunction