Mercurial > octave
annotate scripts/plot/draw/lightangle.m @ 29359:7854d5752dd2
maint: merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2021 10:10:40 -0500 |
parents | 41c54377d67f 0a5b15007766 |
children | 796f54d4ddbf |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
28172
diff
changeset
|
3 ## Copyright (C) 2019-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26995
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
26545 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
10 ## Octave is free software: you can redistribute it and/or modify it | |
11 ## under the terms of the GNU General Public License as published by | |
12 ## the Free Software Foundation, either version 3 of the License, or | |
13 ## (at your option) any later version. | |
14 ## | |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ## GNU General Public License for more details. | |
19 ## | |
20 ## You should have received a copy of the GNU General Public License | |
21 ## along with Octave; see the file COPYING. If not, see | |
22 ## <https://www.gnu.org/licenses/>. | |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
26545 | 25 |
26 ## -*- texinfo -*- | |
27 ## @deftypefn {} {} lightangle (@var{az}, @var{el}) | |
28 ## @deftypefnx {} {} lightangle (@var{hax}, @var{az}, @var{el}) | |
29 ## @deftypefnx {} {} lightangle (@var{hl}, @var{az}, @var{el}) | |
30 ## @deftypefnx {} {@var{hl} =} lightangle (@dots{}) | |
31 ## @deftypefnx {} {[@var{az}, @var{el}] =} lightangle (@var{hl}) | |
32 ## Add a light object to the current axes using spherical coordinates. | |
33 ## | |
34 ## The light position is specified by an azimuthal rotation @var{az} and an | |
35 ## elevation angle @var{el}, both in degrees. | |
36 ## | |
37 ## If the first argument @var{hax} is an axes handle, then create a new light | |
38 ## object in this axes, rather than the current axes returned by @code{gca}. | |
39 ## | |
40 ## If the first argument @var{hl} is a handle to a light object, then act on | |
41 ## this light object rather than creating a new object. | |
42 ## | |
43 ## The optional return value @var{hl} is a graphics handle to the light object. | |
44 ## | |
45 ## Example: | |
46 ## | |
47 ## Add a light object to a plot | |
48 ## | |
49 ## @example | |
50 ## @group | |
51 ## @c doctest: +SKIP | |
52 ## clf; | |
53 ## sphere (36); | |
54 ## lightangle (45, 30); | |
55 ## @end group | |
56 ## @end example | |
57 ## | |
58 ## @seealso{light, view, camlight} | |
59 ## @end deftypefn | |
60 | |
61 function varargout = lightangle (varargin) | |
62 | |
63 if (nargin == 0 || nargin > 3 || nargout > 2 || (nargin > 1 && nargout > 1)) | |
64 print_usage (); | |
65 endif | |
66 | |
67 hl = hax = az = el = []; | |
68 | |
69 if (nargin == 1) | |
70 hl = varargin{1}; | |
71 if (! isscalar (hl) || ! isgraphics (hl, "light")) | |
72 error ("lightangle: HL must be a handle to a light object"); | |
73 endif | |
74 elseif (nargin == 2) | |
75 az = varargin{1}; | |
76 el = varargin{2}; | |
77 elseif (nargin == 3) | |
78 h = varargin{1}; | |
79 if (isscalar (h) && isaxes (h)) | |
80 hax = h; | |
81 elseif (isscalar (h) && isgraphics (h, "light")) | |
82 hl = h; | |
83 else | |
84 error ("lightangle: H must be a handle to an axes or light object"); | |
85 endif | |
86 az = varargin{2}; | |
87 el = varargin{3}; | |
88 endif | |
89 | |
26591
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
90 if (nargin == 1) |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
91 pos = get (hl, "Position"); |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
92 [az, el] = cart2sph (pos(1), pos(2), pos(3)); |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
93 az = rad2deg (az) + 90; # see view.m |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
94 el = rad2deg (el); |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
95 varargout = { az, el }; |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
96 return; |
26995
ab5bfee22d18
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
26591
diff
changeset
|
97 endif |
26591
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
98 |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
99 if (! isscalar (az) || ! isnumeric (az) |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
100 || ! isscalar (el) || ! isnumeric (el)) |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
101 error ("lightangle: AZ and EL must be numeric scalars"); |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
102 endif |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
103 |
26545 | 104 if (! isempty (hl)) |
105 hax = ancestor (hl, "axes"); | |
106 endif | |
107 | |
108 if (isempty (hax)) | |
109 hax = gca (); | |
110 endif | |
111 | |
112 if (isempty (hl)) | |
113 hl = light (hax); | |
114 endif | |
115 | |
116 pos = get (hl, "Position"); | |
117 | |
118 az = deg2rad (az - 90); | |
119 el = deg2rad (el); | |
120 | |
121 if (strcmp (get (hl, "Style"), "local")) | |
122 pos -= get (hax, "CameraTarget"); | |
26995
ab5bfee22d18
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
26591
diff
changeset
|
123 endif |
26545 | 124 |
28171
a23da76e0693
Matlab compatibility fixes for coordinate transform functions (bug #57794).
Nicholas R. Jankowski <jankowskin@asme.org>
parents:
27923
diff
changeset
|
125 [pos(1), pos(2), pos(3)] = sph2cart (az, el, norm (pos)); |
26545 | 126 |
127 if (strcmp (get (hl, "Style"), "local")) | |
128 pos += get (hax, "CameraTarget"); | |
129 endif | |
130 | |
131 set (hl, "Position", pos); | |
132 | |
133 if (nargout == 1) | |
134 varargout = { hl }; | |
135 endif | |
136 | |
137 endfunction | |
138 | |
139 | |
140 %!demo | |
141 %! clf; | |
142 %! sphere (36); | |
143 %! lightangle (45, 30); | |
28172
65ddd575a5f5
lightangle.m: Add title to demo figure.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
144 %! title ("lightangle() demo #1"); |
26545 | 145 |
146 %!test | |
147 %! hf = figure ("visible", "off"); | |
148 %! unwind_protect | |
149 %! sphere (24); | |
150 %! hl = lightangle (45, 20); | |
151 %! assert (isgraphics (hl, "light")); | |
152 %! [az, el] = lightangle (hl); | |
153 %! assert ([45, 20], [az, el], -20*eps); | |
154 %! lightangle (hl, 90, 45); | |
155 %! [az, el] = lightangle (hl); | |
156 %! assert ([90, 45], [az, el], -20*eps); | |
157 %! pos = get (hl, "Position"); | |
158 %! assert ([1, 0, 1], pos, -20*eps); | |
159 %! hl = lightangle (gca (), 45, 20); | |
160 %! assert (isgraphics (hl, "light")); | |
161 %! unwind_protect_cleanup | |
162 %! close (hf); | |
163 %! end_unwind_protect | |
164 | |
165 ## Test input validation | |
26591
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
166 %!error <Invalid call> lightangle () |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
167 %!error <Invalid call> lightangle (1, 2, 3, 4) |
28895
41c54377d67f
maint: remove unnecessary checks on nargout in m-files.
Rik <rik@octave.org>
parents:
28176
diff
changeset
|
168 %!error <Invalid call> [a, b, c] = lightangle (45, 30) |
26591
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
169 %!error <Invalid call> [a, b] = lightangle (45, 30) |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
170 %!error <HL must be a handle to a light object> lightangle (0) |
26a3861a34b6
lightangle.m: review new function (bug #55446).
Rik <rik@octave.org>
parents:
26545
diff
changeset
|
171 %!error <H must be a handle to an axes or light object> lightangle (0, 90, 45) |
26995
ab5bfee22d18
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
26591
diff
changeset
|
172 %!error <AZ and EL must be numeric scalars> lightangle ([1 2], 0) |
ab5bfee22d18
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
26591
diff
changeset
|
173 %!error <AZ and EL must be numeric scalars> lightangle ({1}, 0) |
ab5bfee22d18
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
26591
diff
changeset
|
174 %!error <AZ and EL must be numeric scalars> lightangle (0, [1 2]) |
ab5bfee22d18
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
26591
diff
changeset
|
175 %!error <AZ and EL must be numeric scalars> lightangle (0, {1}) |