Mercurial > octave
annotate scripts/plot/util/rotate.m @ 21581:6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
* testfun.txi: Document Octave conventions for use of semicolon in BIST tests.
* cellfun.cc, ellipj.cc, file-io.cc, gcd.cc, hash.cc, nproc.cc,
utils.cc, fftw.cc, ov-usr-fcn.cc, oct-parse.in.yy, md5sum.m, wavread.m, wavwrite.m,
cplxpair.m, rat.m, im2double.m, rref.m, stem3.m, rotate.m, pchip.m, blackman.m,
hamming.m, hanning.m, spectral_adf.m, spectral_xdf.m, assert.m, fail.m,
etime.m, build-sparse-tests.sh, classes.tst, ctor-vs-method.tst, index.tst,
io.tst, struct.tst: Follow Octave conventions for use of semicolon in BIST
tests.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 01 Apr 2016 18:16:01 -0700 |
parents | 516bb87ea72e |
children | b002b4331a12 |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19272
diff
changeset
|
1 ## Copyright (C) 2014-2015 John W. Eaton |
19081 | 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 -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
20 ## @deftypefn {} {} rotate (@var{h}, @var{dir}, @var{alpha}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
21 ## @deftypefnx {} {} rotate (@dots{}, @var{origin}) |
20173
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
22 ## Rotate the plot object @var{h} through @var{alpha} degrees around the line |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
23 ## with direction @var{dir} and origin @var{origin}. |
19081 | 24 ## |
20173
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
25 ## The default value of @var{origin} is the center of the axes object that is |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
26 ## the parent of @var{h}. |
19081 | 27 ## |
20173
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## If @var{h} is a vector of handles, they must all have the same parent axes |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## object. |
19081 | 30 ## |
20173
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## Graphics objects that may be rotated are lines, surfaces, patches, and |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## images. |
19081 | 33 ## @end deftypefn |
34 | |
35 function rotate (h, direction, alpha, origin) | |
36 | |
37 ## Note in doc string about compatibility issues with calculation of | |
38 ## default origin due to possible differences in the auto-scaling | |
39 ## algorithm between Octave and Matlab. | |
40 | |
41 if (nargin < 3 || nargin > 4) | |
19272 | 42 print_usage (); |
19081 | 43 endif |
44 | |
45 is_h = ishandle (h); | |
46 if (is_h) | |
47 ax_list = get (h, "parent"); | |
48 if (iscell (ax_list)) | |
49 ax_list = cell2mat (ax_list); | |
50 endif | |
51 if (ax_list == ax_list(1)) | |
52 ax = ax_list(1); | |
53 else | |
54 error ("rotate: all handles must be children of the same axes object"); | |
55 endif | |
56 else | |
57 error ("rotate: H must be an array of one or more graphics handles"); | |
58 endif | |
59 | |
60 if (! (isnumeric (direction) && numel (direction) == 3)) | |
61 error ("rotate: invalid direction"); | |
62 endif | |
63 | |
64 if (! (isnumeric (alpha) && isscalar (alpha))) | |
65 error ("rotate: invalid rotation angle"); | |
66 endif | |
67 | |
68 t = get (h, "type"); | |
69 | |
70 is_image = strcmp (t, "image"); | |
71 is_line = strcmp (t, "line"); | |
72 is_patch = strcmp (t, "patch"); | |
73 is_surface = strcmp (t, "surface"); | |
74 | |
75 if (! all (is_image | is_line | is_patch | is_surface)) | |
76 error ("rotate: expecting image, line, patch, or surface objects"); | |
77 endif | |
78 | |
79 if (nargin == 4) | |
80 if (! (isnumeric (origin) && numel (origin) == 3)) | |
81 error ("rotate: invalid origin"); | |
82 endif | |
83 else | |
84 ## Should Z limit be considered when computing origin? | |
85 | |
86 use_zlim = any (is_patch | is_surface); | |
87 | |
88 if (! use_zlim && any (is_line)) | |
89 idx = find (is_line)'; | |
90 for i = idx | |
91 if (! isempty (get (h(i), "zdata"))) | |
92 use_zlim = true; | |
93 break; | |
94 endif | |
95 endfor | |
96 endif | |
97 | |
98 xlim = get (ax, "xlim"); | |
99 ylim = get (ax, "ylim"); | |
100 | |
101 a = (xlim(1) + xlim(2)) / 2; | |
102 b = (ylim(1) + ylim(2)) / 2; | |
103 | |
104 if (use_zlim) | |
105 zlim = get (ax, "zlim"); | |
106 c = (zlim(1) + zlim(2)) / 2; | |
107 else | |
108 c = 0; | |
109 endif | |
110 | |
111 origin = [a, b, c]; | |
112 endif | |
113 | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20173
diff
changeset
|
114 direction /= norm (direction); |
19081 | 115 |
116 u = direction(1); | |
117 v = direction(2); | |
118 w = direction(3); | |
119 | |
120 a = origin(1); | |
121 b = origin(2); | |
122 c = origin(3); | |
123 | |
124 sa = sind (alpha); | |
125 ca = cosd (alpha); | |
126 | |
127 for i = 1:numel (h) | |
128 x = get (h(i), "xdata"); | |
129 y = get (h(i), "ydata"); | |
130 | |
131 if (is_image(i)) | |
132 z = zeros (size (x)); | |
133 else | |
134 z = get (h(i), "zdata"); | |
135 if (isempty (z)) | |
136 z = zeros (size (x)); | |
137 elseif (isvector (x) && isvector (y) && ! isvector (z)) | |
138 [x, y] = meshgrid (x, y); | |
139 endif | |
140 endif | |
141 | |
142 if (a == 0 && b == 0 && c == 0) | |
143 tmp = (u*x + v*y + w*z) * (1 - ca); | |
144 | |
145 xr = u*tmp + x*ca + (-w*y + v*z)*sa; | |
146 yr = v*tmp + y*ca + (w*x - u*z)*sa; | |
147 zr = w*tmp + z*ca + (-v*x + u*y)*sa; | |
148 else | |
149 one_m_ca = 1 - ca; | |
150 tmp = u*x + v*y + w*z; | |
151 | |
152 xr = ((a*(v**2 + w**2) - u*(b*v + c*w - tmp))*one_m_ca | |
153 + x*ca + (-c*v + b*w - w*y + v*z)*sa); | |
154 yr = ((b*(u**2 + w**2) - v*(a*u + c*w - tmp))*one_m_ca | |
155 + y*ca + (c*u - a*w + w*x - u*z)*sa); | |
156 zr = ((c*(u**2 + v**2) - w*(a*u + b*v - tmp))*one_m_ca | |
157 + z*ca + (-b*u + a*v - v*x + u*y)*sa); | |
158 endif | |
159 | |
160 set (h(i), "xdata", xr, "ydata", yr); | |
161 | |
162 if (! is_image(i)) | |
163 set (h(i), "zdata", zr); | |
164 endif | |
165 endfor | |
166 | |
167 endfunction | |
168 | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
169 ## Test input validation |
19081 | 170 %!shared h1, h2, o1, o2, o3 |
171 %! h1 = figure ("visible", "off"); | |
172 %! o1 = line (); | |
173 %! h2 = figure ("visible", "off"); | |
174 %! o2 = line (); | |
175 %! o3 = text (0, 0, "foobar"); | |
176 %!error rotate () | |
177 %!error rotate (o1) | |
21581
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
178 %!error rotate (o1, [0,0,0]) |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
179 %!error <all handles must be children of the same axes object> rotate ([o1, o2], [0,0,0], 90) |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
180 %!error <invalid direction> rotate (o1, "foo", 90) |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
181 %!error <invalid rotation angle> rotate (o1, [0,0,0], "foo") |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
182 %!error <invalid origin> rotate (o1, [0,0,0], 90, "foo") |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
183 %!error rotate (o1, [0,0,0], 90, [0,0,0], 1) |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
184 %!error <H must be an array of one or more graphics handles> rotate (NaN, [0,0,0], 90) |
6fab85c1538f
maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
185 %!error <expecting image, line, patch, or surface objects> rotate (o3, [0,0,0], 90) |
19081 | 186 %!test |
187 %! close (h1); | |
188 %! close (h2); |