File: lightangle.m

package info (click to toggle)
octave 10.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,484 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (175 lines) | stat: -rw-r--r-- 5,080 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
########################################################################
##
## Copyright (C) 2019-2025 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {} lightangle (@var{az}, @var{el})
## @deftypefnx {} {} lightangle (@var{hax}, @var{az}, @var{el})
## @deftypefnx {} {} lightangle (@var{hl}, @var{az}, @var{el})
## @deftypefnx {} {@var{hl} =} lightangle (@dots{})
## @deftypefnx {} {[@var{az}, @var{el}] =} lightangle (@var{hl})
## Add a light object to the current axes using spherical coordinates.
##
## The light position is specified by an azimuthal rotation @var{az} and an
## elevation angle @var{el}, both in degrees.
##
## If the first argument @var{hax} is an axes handle, then create a new light
## object in this axes, rather than the current axes returned by @code{gca}.
##
## If the first argument @var{hl} is a handle to a light object, then act on
## this light object rather than creating a new object.
##
## The optional return value @var{hl} is a graphics handle to the light object.
##
## Example:
##
## Add a light object to a plot
##
## @example
## @group
## @c doctest: +SKIP
## clf;
## sphere (36);
## lightangle (45, 30);
## @end group
## @end example
##
## @seealso{light, view, camlight}
## @end deftypefn

function varargout = lightangle (varargin)

  if (nargin == 0 || nargin > 3 || nargout > 2 || (nargin > 1 && nargout > 1))
    print_usage ();
  endif

  hl = hax = az = el = [];

  if (nargin == 1)
    hl = varargin{1};
    if (! isscalar (hl) || ! isgraphics (hl, "light"))
      error ("lightangle: HL must be a handle to a light object");
    endif
  elseif (nargin == 2)
    az = varargin{1};
    el = varargin{2};
  elseif (nargin == 3)
    h = varargin{1};
    if (isscalar (h) && isaxes (h))
      hax = h;
    elseif (isscalar (h) && isgraphics (h, "light"))
      hl = h;
    else
      error ("lightangle: H must be a handle to an axes or light object");
    endif
    az = varargin{2};
    el = varargin{3};
  endif

  if (nargin == 1)
    pos = get (hl, "Position");
    [az, el] = cart2sph (pos(1), pos(2), pos(3));
    az = rad2deg (az) + 90;  # see view.m
    el = rad2deg (el);
    varargout = { az, el };
    return;
  endif

  if (! isscalar (az) || ! isnumeric (az)
      || ! isscalar (el) || ! isnumeric (el))
    error ("lightangle: AZ and EL must be numeric scalars");
  endif

  if (! isempty (hl))
    hax = ancestor (hl, "axes");
  endif

  if (isempty (hax))
    hax = gca ();
  endif

  if (isempty (hl))
    hl = light (hax);
  endif

  pos = get (hl, "Position");

  az = deg2rad (az - 90);
  el = deg2rad (el);

  if (strcmp (get (hl, "Style"), "local"))
    pos -= get (hax, "CameraTarget");
  endif

  [pos(1), pos(2), pos(3)] = sph2cart (az, el, norm (pos));

  if (strcmp (get (hl, "Style"), "local"))
    pos += get (hax, "CameraTarget");
  endif

  set (hl, "Position", pos);

  if (nargout == 1)
    varargout = { hl };
  endif

endfunction


%!demo
%! clf;
%! sphere (36);
%! lightangle (45, 30);
%! title ("lightangle() demo #1");

%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%!   sphere (24);
%!   hl = lightangle (45, 20);
%!   assert (isgraphics (hl, "light"));
%!   [az, el] = lightangle (hl);
%!   assert ([45, 20], [az, el], -20*eps);
%!   lightangle (hl, 90, 45);
%!   [az, el] = lightangle (hl);
%!   assert ([90, 45], [az, el], -20*eps);
%!   pos = get (hl, "Position");
%!   assert ([1, 0, 1], pos, -20*eps);
%!   hl = lightangle (gca (), 45, 20);
%!   assert (isgraphics (hl, "light"));
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect

## Test input validation
%!error <Invalid call> lightangle ()
%!error <Invalid call> lightangle (1, 2, 3, 4)
%!error <Invalid call> [a, b, c] = lightangle (45, 30)
%!error <Invalid call> [a, b] = lightangle (45, 30)
%!error <HL must be a handle to a light object> lightangle (0)
%!error <H must be a handle to an axes or light object> lightangle (0, 90, 45)
%!error <AZ and EL must be numeric scalars> lightangle ([1 2], 0)
%!error <AZ and EL must be numeric scalars> lightangle ({1}, 0)
%!error <AZ and EL must be numeric scalars> lightangle (0, [1 2])
%!error <AZ and EL must be numeric scalars> lightangle (0, {1})