File: rotate.m

package info (click to toggle)
octave 7.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,464 kB
  • sloc: cpp: 332,823; ansic: 71,320; fortran: 20,963; objc: 8,562; sh: 8,115; yacc: 4,882; lex: 4,438; perl: 1,554; java: 1,366; awk: 1,257; makefile: 652; xml: 173
file content (197 lines) | stat: -rw-r--r-- 5,593 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
########################################################################
##
## Copyright (C) 2014-2022 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  {} {} rotate (@var{h}, @var{direction}, @var{alpha})
## @deftypefnx {} {} rotate (@dots{}, @var{origin})
## Rotate the plot object @var{h} through @var{alpha} degrees around the line
## with direction @var{direction} and origin @var{origin}.
##
## The default value of @var{origin} is the center of the axes object that is
## the parent of @var{h}.
##
## If @var{h} is a vector of handles, they must all have the same parent axes
## object.
##
## Graphics objects that may be rotated are lines, surfaces, patches, and
## images.
## @end deftypefn

function rotate (h, direction, alpha, origin)

  ## Note in doc string about compatibility issues with calculation of
  ## default origin due to possible differences in the auto-scaling
  ## algorithm between Octave and Matlab.

  if (nargin < 3)
    print_usage ();
  endif

  is_h = ishghandle (h);
  if (is_h)
    ax_list = get (h, "parent");
    if (iscell (ax_list))
      ax_list = cell2mat (ax_list);
    endif
    if (ax_list == ax_list(1))
      ax = ax_list(1);
    else
       error ("rotate: all handles must be children of the same axes object");
    endif
  else
    error ("rotate: H must be an array of one or more graphics handles");
  endif

  if (! (isnumeric (direction) && numel (direction) == 3))
    error ("rotate: invalid DIRECTION");
  endif

  if (! (isnumeric (alpha) && isscalar (alpha)))
    error ("rotate: invalid rotation angle");
  endif

  t = get (h, "type");

  is_image = strcmp (t, "image");
  is_line = strcmp (t, "line");
  is_patch = strcmp (t, "patch");
  is_surface = strcmp (t, "surface");

  if (! all (is_image | is_line | is_patch | is_surface))
    error ("rotate: expecting image, line, patch, or surface objects");
  endif

  if (nargin == 4)
    if (! (isnumeric (origin) && numel (origin) == 3))
       error ("rotate: invalid ORIGIN");
    endif
  else
    ## Should Z limit be considered when computing origin?

    use_zlim = any (is_patch | is_surface);

    if (! use_zlim && any (is_line))
      idx = find (is_line)';
      for i = idx
        if (! isempty (get (h(i), "zdata")))
          use_zlim = true;
          break;
        endif
      endfor
    endif

    xlim = get (ax, "xlim");
    ylim = get (ax, "ylim");

    a = (xlim(1) + xlim(2)) / 2;
    b = (ylim(1) + ylim(2)) / 2;

    if (use_zlim)
      zlim = get (ax, "zlim");
      c = (zlim(1) + zlim(2)) / 2;
    else
      c = 0;
    endif

    origin = [a, b, c];
  endif

  direction /= norm (direction);

  u = direction(1);
  v = direction(2);
  w = direction(3);

  a = origin(1);
  b = origin(2);
  c = origin(3);

  sa = sind (alpha);
  ca = cosd (alpha);

  for i = 1:numel (h)
    x = get (h(i), "xdata");
    y = get (h(i), "ydata");

    if (is_image(i))
      z = zeros (size (x));
    else
      z = get (h(i), "zdata");
      if (isempty (z))
        z = zeros (size (x));
      elseif (isvector (x) && isvector (y) && ! isvector (z))
        [x, y] = meshgrid (x, y);
      endif
    endif

    if (a == 0 && b == 0 && c == 0)
      tmp = (u*x + v*y + w*z) * (1 - ca);

      xr = u*tmp + x*ca + (-w*y + v*z)*sa;
      yr = v*tmp + y*ca + (w*x - u*z)*sa;
      zr = w*tmp + z*ca + (-v*x + u*y)*sa;
    else
      one_m_ca = 1 - ca;
      tmp = u*x + v*y + w*z;

      xr = ((a*(v^2 + w^2) - u*(b*v + c*w - tmp))*one_m_ca
            + x*ca + (-c*v + b*w - w*y + v*z)*sa);
      yr = ((b*(u^2 + w^2) - v*(a*u + c*w - tmp))*one_m_ca
            + y*ca + (c*u - a*w + w*x - u*z)*sa);
      zr = ((c*(u^2 + v^2) - w*(a*u + b*v - tmp))*one_m_ca
            + z*ca + (-b*u + a*v - v*x + u*y)*sa);
    endif

    set (h(i), "xdata", xr, "ydata", yr);

    if (! is_image(i))
      set (h(i), "zdata", zr);
    endif
  endfor

endfunction


## Test input validation
%!shared h1, h2, o1, o2, o3
%! h1 = figure ("visible", "off");
%! o1 = line ();
%! h2 = figure ("visible", "off");
%! o2 = line ();
%! o3 = text (0, 0, "foobar");

%!error <Invalid call> rotate ()
%!error <Invalid call> rotate (o1)
%!error <Invalid call> rotate (o1, [0,0,0])
%!error <all handles must be children of the same axes object> rotate ([o1, o2], [0,0,0], 90)
%!error <invalid DIRECTION> rotate (o1, "foo", 90)
%!error <invalid rotation angle> rotate (o1, [0,0,0], "foo")
%!error <invalid ORIGIN> rotate (o1, [0,0,0], 90, "foo")
%!error rotate (o1, [0,0,0], 90, [0,0,0], 1)
%!error <H must be an array of one or more graphics handles> rotate (NaN, [0,0,0], 90)
%!error <expecting image, line, patch, or surface objects> rotate (o3, [0,0,0], 90)
%!test
%! close (h1);
%! close (h2);