7189
|
1 ## Copyright (C) 2007 David Bateman |
|
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 ## Undocumented internal function |
|
20 |
|
21 function hlist = __quiver__ (varargin) |
|
22 h = varargin {1}; |
|
23 is3d = varargin {2}; |
|
24 |
|
25 s = 1; |
|
26 arrowsize = 0.33; |
|
27 |
|
28 firstnonnumeric = Inf; |
|
29 for i = 3:nargin |
|
30 if (! isnumeric (varargin {i})) |
|
31 firstnonnumeric = i; |
|
32 break; |
|
33 endif |
|
34 endfor |
|
35 |
|
36 ioff = 3; |
|
37 if (nargin < (6 + is3d) || firstnonnumeric < (6 + is3d)) |
|
38 u = varargin{ioff++}; |
|
39 v = varargin{ioff++}; |
|
40 if (is3d) |
|
41 w = varargin{ioff++} |
|
42 [x, y, z] = meshgrid (1:size(u,1), 1:size(u,2), 1:max(size(w))); |
|
43 else |
|
44 [x, y] = meshgrid (1:size(u,1), 1:size(u,2)); |
|
45 endif |
|
46 if (nargin >= ioff && isnumeric (varargin{ioff}) && |
|
47 isscalar (varargin{ioff})) |
|
48 s = varargin{ioff++}; |
|
49 endif |
|
50 else |
|
51 x = varargin{ioff++}; |
|
52 y = varargin{ioff++}; |
|
53 if (is3d) |
|
54 z = varargin{ioff++}; |
|
55 endif |
|
56 u = varargin{ioff++}; |
|
57 v = varargin{ioff++}; |
|
58 if (is3d) |
|
59 w = varargin{ioff++}; |
|
60 if (isvector(x) && isvector(y) && isvector(z) && |
|
61 (!isvector (u) || !isvector (v) || !isvector(w))) |
|
62 [x, y, z] = meshgrid (x, y, z); |
|
63 endif |
|
64 else |
|
65 if (isvector(x) && isvector(y) && (!isvector (u) || !isvector (v))) |
|
66 [x, y] = meshgrid (x, y); |
|
67 endif |
|
68 endif |
|
69 if (nargin >= ioff && isnumeric (varargin{ioff}) && |
|
70 isscalar (varargin{ioff})) |
|
71 s = varargin{ioff++}; |
|
72 endif |
|
73 endif |
|
74 |
|
75 have_filled = false; |
|
76 have_line_spec = false; |
|
77 while (ioff <= nargin) |
|
78 arg = varargin {ioff++}; |
|
79 if (ischar (arg) && strncmp (tolower (arg), "filled", 6)) |
|
80 have_filled = true; |
|
81 elseif ((isstr (arg) || iscell (arg)) |
|
82 && ! have_line_spec) |
|
83 [linespec, valid] = __pltopt__ ("quiver", arg, false); |
|
84 if (valid) |
|
85 have_line_spec = true; |
|
86 if (strncmp (linespec.linestyle, "none", 4)) |
|
87 linespec.linestyle = "-"; |
|
88 endif |
|
89 else |
|
90 error ("quiver: invalid linespec"); |
|
91 endif |
|
92 else |
|
93 error ("quiver: unrecognized argument"); |
|
94 endif |
|
95 endwhile |
|
96 |
|
97 if (s) |
|
98 ## Scale the arrows to fit in the grid |
|
99 dx = (max(x(:)) - min(x(:))) ./ size (x, 2); |
|
100 dy = (max(y(:)) - min(y(:))) ./ size (y, 1); |
|
101 if (is3d) |
|
102 ## What should this be divided by? The below seems right |
|
103 dz = (max(z(:)) - min(z(:))) ./ max (size (z)); |
|
104 len = max (sqrt (u(:).^2 + dy(:).^2) + dz(:).^2); |
|
105 else |
|
106 len = max (sqrt (u(:).^2 + dy(:).^2)); |
|
107 dz = 0; |
|
108 endif |
|
109 if (len > 0) |
|
110 s = s / sqrt (2) * sqrt (dx.^2 + dy.^2 + dz.^2) / len; |
|
111 u = s * u; |
|
112 v = s * v; |
|
113 if (is3d) |
|
114 w = s*w; |
|
115 endif |
|
116 endif |
|
117 endif |
|
118 |
|
119 x = x(:); |
|
120 y = y(:); |
|
121 xend = x + u(:); |
|
122 yend = y + v(:); |
|
123 if (is3d) |
|
124 z = z(:); |
|
125 zend = z + w(:); |
|
126 endif |
|
127 |
|
128 hstate = get (h, "nextplot"); |
|
129 unwind_protect |
|
130 if (have_line_spec) |
|
131 if (is3d) |
|
132 h1 = plot3 ([x.'; xend.'; NaN(1, length (x))](:), |
|
133 [y.'; yend.'; NaN(1, length (y))](:), |
|
134 [z.'; zend.'; NaN(1, length (z))](:), |
|
135 "linestyle", linespec.linestyle); |
|
136 else |
|
137 h1 = plot ([x.'; xend.'; NaN(1, length (x))](:), |
|
138 [y.'; yend.'; NaN(1, length (y))](:), |
|
139 "linestyle", linespec.linestyle); |
|
140 endif |
|
141 else |
|
142 if (is3d) |
|
143 h1 = plot3 ([x.'; xend.'; NaN(1, length (x))](:), |
|
144 [y.'; yend.'; NaN(1, length (y))](:), |
|
145 [z.'; zend.'; NaN(1, length (z))](:)); |
|
146 else |
|
147 h1 = plot ([x.'; xend.'; NaN(1, length (x))](:), |
|
148 [y.'; yend.'; NaN(1, length (y))](:)); |
|
149 endif |
|
150 endif |
|
151 hold on; |
|
152 |
|
153 xtmp = x + u(:) .* (1 - arrowsize); |
|
154 ytmp = y + v(:) .* (1 - arrowsize); |
|
155 xarrw1 = xtmp + (y - yend) * arrowsize / 3; |
|
156 xarrw2 = xtmp - (y - yend) * arrowsize / 3; |
|
157 yarrw1 = ytmp - (x - xend) * arrowsize / 3; |
|
158 yarrw2 = ytmp + (x - xend) * arrowsize / 3; |
|
159 if (is3d) |
|
160 zarrw1 = zarrw2 = zend - w(:) * arrowsize / 3; |
|
161 endif |
|
162 |
|
163 if (have_line_spec) |
|
164 if (isfield (linespec, "marker") && |
|
165 ! strncmp (linespec.marker, "none", 4)) |
|
166 if (is3d) |
|
167 h2 = plot3 ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
168 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:), |
|
169 [zarrw1.'; zend.'; zarrw2.'; NaN(1, length (z))](:), |
|
170 "linestyle", "none"); |
|
171 else |
|
172 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
173 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:), |
|
174 "linestyle", "none"); |
|
175 endif |
|
176 else |
|
177 if (is3d) |
|
178 h2 = plot3 ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
179 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:), |
|
180 [zarrw1.'; zend.'; zarrw2.'; NaN(1, length (z))](:), |
|
181 "linestyle", linespec.linestyle); |
|
182 else |
|
183 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
184 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:), |
|
185 "linestyle", linespec.linestyle); |
|
186 endif |
|
187 endif |
|
188 elseif (is3d) |
|
189 h2 = plot3 ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
190 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:), |
|
191 [zarrw1.'; zend.'; zarrw2.'; NaN(1, length (z))](:)); |
|
192 else |
|
193 h2 = plot ([xarrw1.'; xend.'; xarrw2.'; NaN(1, length (x))](:), |
|
194 [yarrw1.'; yend.'; yarrw2.'; NaN(1, length (y))](:)); |
|
195 endif |
|
196 |
|
197 if (! have_line_spec || (isfield (linespec, "marker") && |
|
198 strncmp (linespec.marker, "none", 4))) |
|
199 if (is3d) |
|
200 h3 = plot3 (x, y, z, "linestyle", "none", "marker", "none"); |
|
201 else |
|
202 h3 = plot (x, y, "linestyle", "none", "marker", "none"); |
|
203 endif |
|
204 else |
|
205 if (is3d) |
|
206 h3 = plot3 (x, y, z, "linestyle", "none", "marker", linespec.marker); |
|
207 else |
|
208 |
|
209 h3 = plot (x, y, "linestyle", "none", "marker", linespec.marker); |
|
210 endif |
|
211 endif |
|
212 if (have_filled) |
|
213 ## FIXME gnuplot doesn't respect the markerfacecolor field |
|
214 set(h3, "markerfacecolor", get (h1, "color")); |
|
215 endif |
|
216 unwind_protect_cleanup |
|
217 set (h, "nextplot", hstate); |
|
218 end_unwind_protect |
|
219 |
|
220 hlist = [h1; h2; h3]; |
|
221 |
|
222 endfunction |