6807
|
1 ## Copyright (C) 2007 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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6807
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6807
|
18 |
6895
|
19 ## Undocumented internal function. |
|
20 |
|
21 ## __patch__ (p, x, y, c) |
|
22 ## Create patch object from x and y with color c and parent p. |
6807
|
23 ## Return handle to patch object. |
|
24 |
|
25 ## Author: Kai Habel |
|
26 |
7020
|
27 function [h, fail] = __patch__ (p, varargin) |
|
28 fail = false; |
6885
|
29 if (nargin < 3) |
7020
|
30 fail = true; |
|
31 return; |
6885
|
32 endif |
|
33 |
|
34 iarg = 1; |
7020
|
35 have_x = have_z = have_c = have_faces = false; |
6886
|
36 if (isnumeric (varargin{1})) |
|
37 if (! isnumeric (varargin{2})) |
7020
|
38 fail = true; |
|
39 return; |
6885
|
40 endif |
|
41 |
6886
|
42 x = varargin{1}; |
|
43 y = varargin{2}; |
6885
|
44 have_x = true; |
|
45 iarg += 2; |
|
46 |
6886
|
47 if (nargin > 3 && ndims (varargin{3}) == 2 && ndims (x) == 2 |
7020
|
48 && isequal (size (varargin{3}), size (x))) |
6885
|
49 z = varargin {3}; |
|
50 have_z = true; |
6886
|
51 iarg++; |
6885
|
52 endif |
7020
|
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 |
6885
|
79 endif |
|
80 |
7020
|
81 if ((have_x || have_faces) && nargin > iarg) |
6925
|
82 if (isnumeric (varargin{iarg})) |
|
83 c = varargin{iarg}; |
|
84 have_c = true; |
|
85 iarg++; |
6885
|
86 |
6925
|
87 if (ndims (c) == 3 && size (c, 2) == 1) |
|
88 c = permute (c, [1, 3, 2]); |
|
89 endif |
|
90 elseif (ischar (varargin{iarg}) && rem (nargin - iarg, 2) != 0) |
|
91 ## Assume that any additional argument over an even number is color string |
|
92 c = tolower (varargin{iarg}); |
|
93 have_c = true; |
|
94 iarg++; |
6885
|
95 endif |
|
96 endif |
|
97 |
|
98 if (rem (nargin - iarg, 2) != 0) |
7020
|
99 fail = true; |
|
100 return; |
6807
|
101 endif |
|
102 |
6885
|
103 if (have_x) |
|
104 if (isvector (x)) |
|
105 x = x(:); |
|
106 y = y(:); |
|
107 if (have_z) |
|
108 z = z(:); |
|
109 endif |
|
110 endif |
|
111 [nr, nc] = size (x); |
7020
|
112 if (have_z) |
|
113 vert = [x(:), y(:), z(:)]; |
|
114 else |
|
115 vert = [x(:), y(:)]; |
|
116 endif |
7160
|
117 faces = reshape (1:numel(x), rows (x), columns (x)); |
|
118 faces = faces'; |
7020
|
119 elseif (have_faces) |
|
120 nr = size (faces, 2); |
|
121 nc = size (faces, 1); |
|
122 idx = faces .'; |
|
123 for i = 1: nc |
|
124 t1 = isnan (idx (:,i)); |
|
125 if (any (t1)) |
|
126 t2 = find (t1(1:end-1) != t1(2:end))(1); |
|
127 idx(t1,i) = idx(t2,i); |
6885
|
128 endif |
|
129 endfor |
7189
|
130 x = reshape (vert(:,1)(idx), size (idx)); |
|
131 y = reshape (vert(:,2)(idx), size (idx)); |
7020
|
132 if (size(vert,2) > 2) |
|
133 have_z = true; |
7189
|
134 z = reshape (vert(:,3)(idx), size (idx)); |
7020
|
135 endif |
6807
|
136 else |
6886
|
137 error ("patch: not supported"); |
6807
|
138 endif |
6886
|
139 |
7020
|
140 h = __go_patch__ (p); |
|
141 ax = get (h, "parent"); |
|
142 |
|
143 cargs = {}; |
|
144 if (have_c) |
|
145 if (ischar (c)) |
|
146 cargs{1} = "facecolor"; |
|
147 cargs{2} = c; |
|
148 elseif (isvector(c) && numel(c) == nc) |
|
149 if (isnan (c)) |
|
150 cargs{1} = "facecolor"; |
|
151 cargs{2} = [1, 1, 1]; |
|
152 cargs{3} = "cdata"; |
|
153 cargs{4} = c; |
|
154 elseif (isnumeric (c)) |
|
155 cargs{1} = "facecolor"; |
|
156 cargs{2} = "flat"; |
|
157 cargs{3} = "cdata"; |
|
158 cargs{4} = c; |
|
159 clim = get(ax, "clim"); |
|
160 if (c(1) < clim(1)) |
|
161 set (ax, "clim", [c(1), clim(2)]) |
7189
|
162 clim(1) = c(1); |
7020
|
163 endif |
|
164 if (c(1) > clim(2)) |
|
165 set (ax, "clim", [clim(1), c(1)]) |
|
166 endif |
|
167 else |
|
168 error ("patch: color value not valid"); |
|
169 endif |
|
170 elseif (size(c, ndims(c)) == 3) |
|
171 cargs{1} = "facecolor"; |
|
172 cargs{2} = "flat"; |
|
173 cargs{3} = "cdata"; |
|
174 cargs{4} = c; |
|
175 else |
|
176 ## Color Vectors |
|
177 |
|
178 if (rows (c2) != rows (x) || rows (c2) != length (y)) |
|
179 error ("patch: size of x, y, and c must be equal") |
|
180 else |
|
181 cargs{1} = "facecolor"; |
|
182 cargs{2} = "interp"; |
|
183 if (abs(max(c2(:)) - min(c2(:))) < eps) |
|
184 set (ax, "clim", [c2(1)-1, c2(1)+1]) |
|
185 else |
|
186 set (ax, "clim", [min(c2(:)), max(c2(:))]); |
|
187 endif |
|
188 endif |
|
189 endif |
|
190 else |
|
191 cargs{1} = "facecolor"; |
|
192 cargs{2} = [0, 1, 0]; |
|
193 endif |
|
194 |
|
195 set (h, "xdata", x, "ydata", y, "faces", faces, "vertices", vert, ... |
|
196 cargs{:}, varargin{iarg:end}); |
|
197 if (have_z) |
|
198 set (h, "zdata", z); |
|
199 endif |
|
200 |
6807
|
201 endfunction |