File: hgtransform.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 (255 lines) | stat: -rw-r--r-- 7,343 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
########################################################################
##
## Copyright (C) 2017-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  {} {@var{h} =} hgtransform ()
## @deftypefnx {} {@var{h} =} hgtransform (@var{property}, @var{value}, @dots{})
## @deftypefnx {} {@var{h} =} hgtransform (@var{hax}, @dots{})
##
## Create a graphics transform object.
##
## FIXME: Need to write documentation.
## FIXME: Add <makehgtform> to seealso list when it is implemented.
## @seealso{hggroup}
## @end deftypefn

## FIXME: hgtransform should be a C++ graphics object, not an m-file.
##        For the moment (3/7/17), it is quicker to implement something in
##        an m-file.  But, this approach requires double the memory (original
##        and transformed data), and a system of listeners and callbacks.
##        In OpenGL toolkits it should be possible to simply insert a transform
##        somewhere in gl-render.cc to have this done on the fly.

function h = hgtransform (varargin)

  [hax, varargin] = __plt_get_axis_arg__ ("hgtransform", varargin{:});

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

  htmp = hggroup (hax);

  addproperty ("matrix", htmp, "data", eye (4));
  addproperty ("__orig_data__", htmp, "any", struct ("h", {}));
  if (! isempty (varargin))
    set (htmp, varargin{:});
  endif
  addlistener (htmp, "matrix", @matrix_cb);
  addlistener (htmp, "children", @children_cb);

  if (nargout > 0)
    h = htmp;
  endif

endfunction

function matrix_cb (hgt, ~)

  M = get (hgt, "matrix");
  ## FIXME: Need better input validation on transform matrix M.
  ##        Disallow shear, perspective transforms.
  if (! isreal (M) || ! ismatrix (M) || rows (M) != 4 || columns (M) != 4)
    error ("hgtransform: transform must be 4x4 real matrix");
  endif

  hkids = get (hgt, "children");
  xform_data (hgt, hkids);

endfunction

function xform_data (hgt, hlist)

  M = get (hgt, "matrix");
  orig_data = get (hgt, "__orig_data__");

  for hk = hlist.'

    ## FIXME: Add support for light and rectangle objects
    type = get (hk, "type");
    if (! any (strcmp (type, {"line", "patch", "surface", "image", "text"})))
      continue;  # Unsupported type.  Silently skip.
    endif

    idx = find (hk == [orig_data.h]);
    if (! idx)
      warning ("hgtransform: original data not found for %f", hk);
      continue;
    endif

    if (strcmp (type, "text"))
      pos = orig_data(idx).position;
      xd = pos(1);
      yd = pos(2);
      zd = pos(3);
    else
      ## FIXME: Rotations for "image" objects are not handled correctly.
      xd = double (orig_data(idx).xdata);
      xsz = size (xd);

      yd = double (orig_data(idx).ydata);
      ysz = size (yd);

      if (strcmp (type, "image"))
        zd = [];
      else
        zd = double (orig_data(idx).zdata);
        zsz = size (zd);
      endif
      z_empty = isempty (zd);

      if (z_empty)
        ## Common case of 2-D data.
        zd = zeros (numel (xd), 1);
      elseif (isvector (xd) && isvector (yd))
        ## Handle surface data which may be a vector/matrix combination
        if (isvector (zd))
          ## Do nothing.  All data will be forced to row vectors below
        elseif (length (xd) == rows (zd) && length (yd) == columns (zd))
          [xd, yd] = meshgrid (xd, yd);
          xsz = size (xd);
          ysz = size (yd);
        endif
      endif

      ## Force row vectors for later concatenation
      xd = xd(:).';
      yd = yd(:).';
      zd = zd(:).';

    endif

    ## FIXME: To minimize memory, better to construct data matrix in-place?
    data = [xd; yd; zd; ones(1, columns(xd))];
    tol = 2 * max (eps (data(1:3,:)));
    data = M * data;
    ## Need to trim or rotations which produce values near 0 will be strange.
    data(abs (data) < tol) = 0;

    if (strcmp (type, "text"))
      set (hk, "position", [data(1), data(2), data(3)]);
    else
      set (hk, "xdata", reshape (data(1,:), xsz),
               "ydata", reshape (data(2,:), ysz));
      if (! z_empty)
        set (hk, "zdata", reshape (data(3,:), zsz));
      endif
    endif

  endfor

endfunction

function children_cb (hgt, ~)

  hkids = get (hgt, "children");
  orig_data = get (hgt, "__orig_data__");
  hlist = [orig_data.h];

  ## Delete any children that have been removed
  hdel = setdiff (hlist, hkids);
  if (! isempty (hdel))
    for hk = hdel.'
      idx = find (hk == hlist);
      if (ishghandle (hk))
        ## child was re-parented to something else, restore data
        switch (get (hk, "type"))

          case {"line", "patch", "surface"}
            set (hk, "xdata", orig_data(idx).xdata,
                     "ydata", orig_data(idx).ydata,
                     "zdata", orig_data(idx).zdata);

          case "image"
            set (hk, "xdata", orig_data(idx).xdata,
                     "ydata", orig_data(idx).ydata);

          case "text"
            set (hk, "position", orig_data(idx).position);

          otherwise
            ## Unsupported type.  No data was saved.

        endswitch
      endif
    endfor
    orig_data = orig_data(hlist != hdel);
    hlist = hlist(hlist != hdel);
  endif

  ## Add new children
  hnew = setdiff (hkids, hlist);
  for hk = hnew.'

    orig_data(end+1).h = hk;
    type = get (hk, "type");
    orig_data(end).type = type;

    switch (type)

      case {"line", "patch", "surface"}
        orig_data(end).xdata = get (hk, "xdata");
        orig_data(end).ydata = get (hk, "ydata");
        orig_data(end).zdata = get (hk, "zdata");

      case "image"
        orig_data(end).xdata = get (hk, "xdata");
        orig_data(end).ydata = get (hk, "ydata");

      case "text"
        orig_data(end).position = get (hk, "position");

      case "light"
        warning ("hgtransform: %s objects are not yet supported", type);

      case "hggroup"
        try
          get (hk, "curvature");
          is_rectangle = true;
        catch
          is_rectangle = false;
        end_try_catch
        if (is_rectangle)
          warning ("hgtransform: rectangle objects are not yet supported");
        else
          error ("hgtransform: hggroup cannot be a child of an hgtransform");
        endif

      otherwise
        error ("hgtransform: %s cannot be a child of an hgtransform", type);

    endswitch

  endfor

  set (hgt, "__orig_data__", orig_data);

  ## Update data of new children only
  xform_data (hgt, hnew);

endfunction


## Need BIST tests here