Mercurial > hg > octave-nkf
comparison 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 |
comparison
equal
deleted
inserted
replaced
7019:4270ded9ddc6 | 7020:e31f12bb9194 |
---|---|
22 ## Create patch object from x and y with color c and parent p. | 22 ## Create patch object from x and y with color c and parent p. |
23 ## Return handle to patch object. | 23 ## Return handle to patch object. |
24 | 24 |
25 ## Author: Kai Habel | 25 ## Author: Kai Habel |
26 | 26 |
27 function h = __patch__ (p, varargin) | 27 function [h, fail] = __patch__ (p, varargin) |
28 | 28 fail = false; |
29 if (nargin < 3) | 29 if (nargin < 3) |
30 print_usage (); | 30 fail = true; |
31 return; | |
31 endif | 32 endif |
32 | 33 |
33 iarg = 1; | 34 iarg = 1; |
34 have_x = have_z = have_c = false; | 35 have_x = have_z = have_c = have_faces = false; |
35 if (isnumeric (varargin{1})) | 36 if (isnumeric (varargin{1})) |
36 if (! isnumeric (varargin{2})) | 37 if (! isnumeric (varargin{2})) |
37 print_usage (); | 38 fail = true; |
39 return; | |
38 endif | 40 endif |
39 | 41 |
40 x = varargin{1}; | 42 x = varargin{1}; |
41 y = varargin{2}; | 43 y = varargin{2}; |
42 have_x = true; | 44 have_x = true; |
43 iarg += 2; | 45 iarg += 2; |
44 | 46 |
45 if (nargin > 3 && ndims (varargin{3}) == 2 && ndims (x) == 2 | 47 if (nargin > 3 && ndims (varargin{3}) == 2 && ndims (x) == 2 |
46 && size (varargin{3}) == size (x)) | 48 && isequal (size (varargin{3}), size (x))) |
47 z = varargin {3}; | 49 z = varargin {3}; |
48 have_z = true; | 50 have_z = true; |
49 iarg++; | 51 iarg++; |
50 endif | 52 endif |
53 elseif (ischar (varargin{1}) && (strcmp (tolower (varargin{1}), "faces") || | |
54 strcmp (tolower (varargin{1}), "vertices"))) | |
55 if (! isnumeric (varargin{2})) | |
56 fail = true; | |
57 return; | |
58 endif | |
59 | |
60 if (strcmp (tolower (varargin{1}), "faces")) | |
61 faces = varargin{2}; | |
62 if (strcmp (tolower (varargin{3}), "vertices")) | |
63 vert = varargin{4}; | |
64 have_faces = true; | |
65 endif | |
66 elseif (strcmp (tolower (varargin{3}), "vertices")) | |
67 vert = varargin{2}; | |
68 if (strcmp (tolower (varargin{3}), "faces")) | |
69 faces = varargin{4}; | |
70 have_faces = true; | |
71 endif | |
72 endif | |
73 if (!have_faces) | |
74 fail = true; | |
75 return; | |
76 else | |
77 iarg += 4; | |
78 endif | |
51 endif | 79 endif |
52 | 80 |
53 if (have_x && nargin > iarg) | 81 if ((have_x || have_faces) && nargin > iarg) |
54 if (isnumeric (varargin{iarg})) | 82 if (isnumeric (varargin{iarg})) |
55 c = varargin{iarg}; | 83 c = varargin{iarg}; |
56 have_c = true; | 84 have_c = true; |
57 iarg++; | 85 iarg++; |
58 | 86 |
66 iarg++; | 94 iarg++; |
67 endif | 95 endif |
68 endif | 96 endif |
69 | 97 |
70 if (rem (nargin - iarg, 2) != 0) | 98 if (rem (nargin - iarg, 2) != 0) |
71 print_usage (); | 99 fail = true; |
100 return; | |
72 endif | 101 endif |
73 | 102 |
74 if (have_x) | 103 if (have_x) |
75 if (isvector (x)) | 104 if (isvector (x)) |
76 x = x(:); | 105 x = x(:); |
77 y = y(:); | 106 y = y(:); |
78 if (have_z) | 107 if (have_z) |
79 z = z(:); | 108 z = z(:); |
80 endif | 109 endif |
81 endif | 110 endif |
82 | |
83 [nr, nc] = size (x); | 111 [nr, nc] = size (x); |
84 | 112 if (have_z) |
85 for i = 1 : nc | 113 vert = [x(:), y(:), z(:)]; |
86 h = __go_patch__ (p); | 114 else |
87 ax = get (h, "parent"); | 115 vert = [x(:), y(:)]; |
88 if (have_x) | 116 endif |
89 set (h, "xdata", x (:, i), "ydata", y (:, i)); | 117 faces = reshape (1:numel(x), size(x,2), size(x,1)); |
90 if (have_z) | 118 elseif (have_faces) |
91 set (h, "zdata", z (:, i)); | 119 nr = size (faces, 2); |
92 endif | 120 nc = size (faces, 1); |
93 endif | 121 idx = faces .'; |
94 | 122 for i = 1: nc |
95 if (have_c) | 123 t1 = isnan (idx (:,i)); |
96 if (ndims (c) == 2 && ((nr > 3 && size (c, 2) == nc) | 124 if (any (t1)) |
97 || (size (c, 1) > 1 && size (c, 2) == nc))) | 125 t2 = find (t1(1:end-1) != t1(2:end))(1); |
98 c2 = c (:, i); | 126 idx(t1,i) = idx(t2,i); |
99 elseif (ndims (c) == 3) | |
100 c2 = permute (c(:,i,:), [1, 3, 2]); | |
101 else | |
102 c2 = c; | |
103 endif | |
104 | |
105 if (ischar (c2)) | |
106 set (h, "facecolor", c2); | |
107 elseif (numel (c2) == 1) | |
108 if (isnan (c)) | |
109 set (h, "facecolor", [1, 1, 1]); | |
110 set (h, "cdata", c2); | |
111 elseif (isnumeric (c2)) | |
112 ## Have color index. | |
113 set (h, "facecolor", "flat"); | |
114 set (h, "cdata", c2); | |
115 clim = get(ax, "clim"); | |
116 if (c2 < clim(1)) | |
117 set (ax, "clim", [c2, clim(2)]) | |
118 endif | |
119 if (c2 > clim(2)) | |
120 set (ax, "clim", [clim(1), c2]) | |
121 endif | |
122 else | |
123 ## Unknown color value. | |
124 error ("patch: color value not valid"); | |
125 endif | |
126 elseif (numel (c2) == 3) | |
127 ## Have rgb/rgba value. | |
128 set (h, "facecolor", c2); | |
129 else | |
130 ## Color vector. | |
131 if (length (c2) != length (x) || length (c2) != length (y)) | |
132 error ("patch: size of x, y, and c must be equal") | |
133 else | |
134 set (h, "facecolor", "interp"); | |
135 set(h, "cdata", c2); | |
136 if (abs(max(c2) - min(c2)) < eps) | |
137 set (ax, "clim", [c2(1)-1, c2(1)+1]) | |
138 else | |
139 set (ax, "clim", [min(c2), max(c2)]); | |
140 endif | |
141 endif | |
142 endif | |
143 else | |
144 set (h, "facecolor", [0, 1, 0]); | |
145 endif | |
146 | |
147 if (nargin > iarg + 1) | |
148 set (h, varargin{iarg:end}); | |
149 endif | 127 endif |
150 endfor | 128 endfor |
129 x = vert(:,1)(idx); | |
130 y = vert(:,2)(idx); | |
131 if (size(vert,2) > 2) | |
132 have_z = true; | |
133 z = vert(:,3)(idx); | |
134 endif | |
151 else | 135 else |
152 error ("patch: not supported"); | 136 error ("patch: not supported"); |
153 endif | 137 endif |
154 | 138 |
139 h = __go_patch__ (p); | |
140 ax = get (h, "parent"); | |
141 | |
142 cargs = {}; | |
143 if (have_c) | |
144 if (ischar (c)) | |
145 cargs{1} = "facecolor"; | |
146 cargs{2} = c; | |
147 elseif (isvector(c) && numel(c) == nc) | |
148 if (isnan (c)) | |
149 cargs{1} = "facecolor"; | |
150 cargs{2} = [1, 1, 1]; | |
151 cargs{3} = "cdata"; | |
152 cargs{4} = c; | |
153 elseif (isnumeric (c)) | |
154 cargs{1} = "facecolor"; | |
155 cargs{2} = "flat"; | |
156 cargs{3} = "cdata"; | |
157 cargs{4} = c; | |
158 clim = get(ax, "clim"); | |
159 if (c(1) < clim(1)) | |
160 set (ax, "clim", [c(1), clim(2)]) | |
161 endif | |
162 if (c(1) > clim(2)) | |
163 set (ax, "clim", [clim(1), c(1)]) | |
164 endif | |
165 else | |
166 error ("patch: color value not valid"); | |
167 endif | |
168 elseif (size(c, ndims(c)) == 3) | |
169 cargs{1} = "facecolor"; | |
170 cargs{2} = "flat"; | |
171 cargs{3} = "cdata"; | |
172 cargs{4} = c; | |
173 else | |
174 ## Color Vectors | |
175 | |
176 if (rows (c2) != rows (x) || rows (c2) != length (y)) | |
177 error ("patch: size of x, y, and c must be equal") | |
178 else | |
179 cargs{1} = "facecolor"; | |
180 cargs{2} = "interp"; | |
181 if (abs(max(c2(:)) - min(c2(:))) < eps) | |
182 set (ax, "clim", [c2(1)-1, c2(1)+1]) | |
183 else | |
184 set (ax, "clim", [min(c2(:)), max(c2(:))]); | |
185 endif | |
186 endif | |
187 endif | |
188 else | |
189 cargs{1} = "facecolor"; | |
190 cargs{2} = [0, 1, 0]; | |
191 endif | |
192 | |
193 set (h, "xdata", x, "ydata", y, "faces", faces, "vertices", vert, ... | |
194 cargs{:}, varargin{iarg:end}); | |
195 if (have_z) | |
196 set (h, "zdata", z); | |
197 endif | |
198 | |
155 endfunction | 199 endfunction |