Mercurial > octave-nkf
comparison scripts/plot/private/__patch__.m @ 9896:1aeb39118764
convert some plot functions to subfunctions or make some them private
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 01 Dec 2009 15:59:43 -0500 |
parents | scripts/plot/__patch__.m@308311b642b2 |
children | 1c6ff93c025a |
comparison
equal
deleted
inserted
replaced
9895:5a4f8d52bb0e | 9896:1aeb39118764 |
---|---|
1 ## Copyright (C) 2007, 2008, 2009 John W. Eaton, Shai Ayal, Kai Habel | |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {[@var{h}, @var{fail}] =} __patch__ (@var{p}, @dots{}) | |
21 ## Undocumented internal function. | |
22 ## @end deftypefn | |
23 | |
24 ## __patch__ (p, x, y, c) | |
25 ## Create patch object from x and y with color c and parent p. | |
26 ## Return handle to patch object. | |
27 | |
28 ## Author: Kai Habel | |
29 | |
30 function [h, failed] = __patch__ (p, varargin) | |
31 | |
32 failed = false; | |
33 | |
34 if (isstruct (varargin{1})) | |
35 if (isfield (varargin{1}, "vertices") && isfield (varargin{1}, "faces")) | |
36 args{1} = "faces"; | |
37 args{2} = getfield(varargin{1}, "faces"); | |
38 args{3} = "vertices"; | |
39 args{4} = getfield(varargin{1}, "vertices"); | |
40 args{5} = "facevertexcdata"; | |
41 if (isfield (varargin{1}, "facevertexcdata")) | |
42 args{6} = getfield(varargin{1}, "facevertexcdata"); | |
43 else | |
44 args{6} = []; | |
45 endif | |
46 args = [args; varargin(2:end)]; | |
47 args = setdata (args); | |
48 else | |
49 failed = true; | |
50 endif | |
51 elseif (isnumeric (varargin{1})) | |
52 if (nargin < 3 || ! isnumeric (varargin{2})) | |
53 failed = true; | |
54 else | |
55 x = varargin{1}; | |
56 y = varargin{2}; | |
57 iarg = 3; | |
58 | |
59 if (nargin > 3 && ndims (varargin{3}) == 2 && ndims (x) == 2 | |
60 && size_equal(x, varargin{3}) && !ischar(varargin{3})) | |
61 z = varargin{3}; | |
62 iarg++; | |
63 else | |
64 z = []; | |
65 endif | |
66 | |
67 if (isvector (x)) | |
68 x = x(:); | |
69 y = y(:); | |
70 z = z(:); | |
71 endif | |
72 args{1} = "xdata"; | |
73 args{2} = x; | |
74 args{3} = "ydata"; | |
75 args{4} = y; | |
76 args{5} = "zdata"; | |
77 args{6} = z; | |
78 | |
79 if (isnumeric (varargin{iarg})) | |
80 c = varargin{iarg}; | |
81 iarg++; | |
82 | |
83 if (ndims (c) == 3 && size (c, 2) == 1) | |
84 c = permute (c, [1, 3, 2]); | |
85 endif | |
86 | |
87 if (isvector (c) && numel (c) == columns (x)) | |
88 if (isnan (c)) | |
89 args{7} = "facecolor"; | |
90 args{8} = [1, 1, 1]; | |
91 args{9} = "cdata"; | |
92 args{10} = c; | |
93 elseif (isnumeric (c)) | |
94 args{7} = "facecolor"; | |
95 args{8} = "flat"; | |
96 args{9} = "cdata"; | |
97 args{10} = c; | |
98 else | |
99 error ("patch: color value not valid"); | |
100 endif | |
101 elseif (size (c, ndims (c)) == 3) | |
102 args{7} = "facecolor"; | |
103 args{8} = "flat"; | |
104 args{9} = "cdata"; | |
105 args{10} = c; | |
106 else | |
107 ## Color Vectors | |
108 if (rows (c) != rows (x) || rows (c) != length (y)) | |
109 error ("patch: size of x, y, and c must be equal") | |
110 else | |
111 args{7} = "facecolor"; | |
112 args{8} = "interp"; | |
113 args{9} = "cdata"; | |
114 args{10} = []; | |
115 endif | |
116 endif | |
117 elseif (ischar (varargin{iarg}) && rem (nargin - iarg, 2) != 0) | |
118 ## Assume that any additional argument over an even number is | |
119 ## color string. | |
120 args{7} = "facecolor"; | |
121 args{8} = tolower (varargin{iarg}); | |
122 args{9} = "cdata"; | |
123 args{10} = []; | |
124 iarg++; | |
125 else | |
126 args{7} = "facecolor"; | |
127 args{8} = [0, 1, 0]; | |
128 args{9} = "cdata"; | |
129 args{10} = []; | |
130 endif | |
131 | |
132 args = [args, varargin(iarg:end)]; | |
133 args = setvertexdata (args); | |
134 endif | |
135 else | |
136 args = varargin; | |
137 if (any(cellfun (@(x) strcmpi(x,"faces") || strcmpi(x, "vertices"), args))) | |
138 args = setdata (args); | |
139 else | |
140 args = setvertexdata (args); | |
141 endif | |
142 endif | |
143 | |
144 if (!failed) | |
145 h = __go_patch__ (p, args {:}); | |
146 | |
147 ## Setup listener functions | |
148 addlistener (h, "xdata", @update_data); | |
149 addlistener (h, "ydata", @update_data); | |
150 addlistener (h, "zdata", @update_data); | |
151 addlistener (h, "cdata", @update_data); | |
152 | |
153 addlistener (h, "faces", @update_fvc); | |
154 addlistener (h, "vertices", @update_fvc); | |
155 addlistener (h, "facevertexcdata", @update_fvc); | |
156 endif | |
157 endfunction | |
158 | |
159 function args = delfields(args, flds) | |
160 idx = cellfun (@(x) any (strcmpi (x, flds)), args); | |
161 idx = idx | [false, idx(1:end-1)]; | |
162 args (idx) = []; | |
163 endfunction | |
164 | |
165 function args = setdata (args) | |
166 args = delfields (args, {"xdata", "ydata", "zdata", "cdata"}); | |
167 nargs = length (args); | |
168 idx = find (cellfun (@(x) strcmpi (x, "faces"), args)) + 1; | |
169 if (idx > nargs) | |
170 faces = []; | |
171 else | |
172 faces = args {idx}; | |
173 endif | |
174 idx = find (cellfun (@(x) strcmpi (x, "vertices"), args)) + 1; | |
175 if (idx > nargs) | |
176 vert = []; | |
177 else | |
178 vert = args {idx}; | |
179 endif | |
180 idx = find (cellfun (@(x) strcmpi (x, "facevertexcdata"), args)) + 1; | |
181 if (isempty(idx) || idx > nargs) | |
182 fvc = []; | |
183 else | |
184 fvc = args {idx}; | |
185 endif | |
186 idx = find (cellfun (@(x) strcmpi (x, "facecolor"), args)) + 1; | |
187 if (isempty(idx) || idx > nargs) | |
188 if (!isempty (fvc)) | |
189 fc = "flat"; | |
190 else | |
191 fc = [0, 1, 0]; | |
192 endif | |
193 args = {"facecolor", fc, args{:}}; | |
194 else | |
195 fc = args {idx}; | |
196 endif | |
197 | |
198 nr = size (faces, 2); | |
199 nc = size (faces, 1); | |
200 idx = faces .'; | |
201 t1 = isnan (idx); | |
202 if (any (t1(:))) | |
203 t2 = find (t1 != t1([2:end,end],:)); | |
204 idx (t1) = idx (t2 (cell2mat (cellfun (@(x) x(1)*ones(1,x(2)), | |
205 mat2cell ([1 : nc; sum(t1)], 2, ones(1,nc)), | |
206 "UniformOutput", false)))); | |
207 endif | |
208 x = reshape (vert(:,1)(idx), size (idx)); | |
209 y = reshape (vert(:,2)(idx), size (idx)); | |
210 if (size(vert,2) > 2) | |
211 z = reshape (vert(:,3)(idx), size (idx)); | |
212 else | |
213 z = []; | |
214 endif | |
215 | |
216 if (ischar (fc) && (strcmpi (fc, "flat") || strcmpi (fc, "interp"))) | |
217 if (size(fvc, 1) == nc || size (fvc, 1) == 1) | |
218 c = reshape (fvc, [1, size(fvc)]); | |
219 else | |
220 if (size(fvc, 2) == 3) | |
221 c = cat(3, reshape (fvc(idx, 1), size(idx)), | |
222 reshape (fvc(idx, 2), size(idx)), | |
223 reshape (fvc(idx, 3), size(idx))); | |
224 else | |
225 c = reshape (fvc(idx), size(idx)); | |
226 endif | |
227 endif | |
228 else | |
229 c = []; | |
230 endif | |
231 args = {"xdata", x, "ydata", y, "zdata", z, "cdata", c, args{:}}; | |
232 endfunction | |
233 | |
234 function args = setvertexdata (args) | |
235 args = delfields (args, {"vertices", "faces", "facevertexcdata"}); | |
236 nargs = length (args); | |
237 idx = find (cellfun (@(x) strcmpi (x, "xdata"), args)) + 1; | |
238 if (idx > nargs) | |
239 x = []; | |
240 else | |
241 x = args {idx}; | |
242 endif | |
243 idx = find (cellfun (@(x) strcmpi (x, "ydata"), args)) + 1; | |
244 if (idx > nargs) | |
245 y = []; | |
246 else | |
247 y = args {idx}; | |
248 endif | |
249 idx = find (cellfun (@(x) strcmpi (x, "zdata"), args)) + 1; | |
250 if (isempty(idx) || idx > nargs) | |
251 z = []; | |
252 else | |
253 z = args {idx}; | |
254 endif | |
255 idx = find (cellfun (@(x) strcmpi (x, "cdata"), args)) + 1; | |
256 if (isempty(idx) || idx > nargs) | |
257 c = []; | |
258 else | |
259 c = args {idx}; | |
260 endif | |
261 idx = find (cellfun (@(x) strcmpi (x, "facecolor"), args)) + 1; | |
262 if (isempty(idx) || idx > nargs) | |
263 if (!isempty (c)) | |
264 fc = "flat"; | |
265 else | |
266 fc = [0, 1, 0]; | |
267 endif | |
268 args = {"facecolor", fc, args{:}}; | |
269 else | |
270 fc = args {idx}; | |
271 endif | |
272 | |
273 [nr, nc] = size (x); | |
274 if (!isempty (z)) | |
275 vert = [x(:), y(:), z(:)]; | |
276 else | |
277 vert = [x(:), y(:)]; | |
278 endif | |
279 faces = reshape (1:numel(x), rows (x), columns (x)); | |
280 faces = faces'; | |
281 | |
282 if (ischar (fc) && (strcmpi (fc, "flat") || strcmpi (fc, "interp"))) | |
283 if (ndims (c) == 3) | |
284 fvc = reshape (c, size (c, 1) * size (c, 2), size(c, 3)); | |
285 else | |
286 fvc = c(:); | |
287 endif | |
288 else | |
289 fvc = []; | |
290 endif | |
291 | |
292 args = {"faces", faces, "vertices", vert, "facevertexcdata", fvc, args{:}}; | |
293 endfunction | |
294 | |
295 function update_data (h, d) | |
296 update_handle (h, false); | |
297 endfunction | |
298 | |
299 function update_fvc (h, d) | |
300 update_handle (h, true); | |
301 endfunction | |
302 | |
303 function update_handle (h, isfv) | |
304 persistent recursive = false; | |
305 | |
306 if (! recursive) | |
307 recursive = true; | |
308 f = get (h); | |
309 if (isfvc) | |
310 set (h, setvertexdata ([fieldnames(f), struct2cell(f)].'(:)){:}); | |
311 else | |
312 set (h, setdata ([fieldnames(f), struct2cell(f)].'(:)){:}); | |
313 endif | |
314 recursive = false; | |
315 endif | |
316 endfunction |