1## Copyright (C) 2012 Roberto Metere <roberto@metere.it>
2## Copyright (C) 2012 Carnë Draug <roberto@metere.it>
3##
4## This program is free software; you can redistribute it and/or modify it under
5## the terms of the GNU General Public License as published by the Free Software
6## Foundation; either version 3 of the License, or (at your option) any later
7## version.
8##
9## This program is distributed in the hope that it will be useful, but WITHOUT
10## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12## details.
13##
14## You should have received a copy of the GNU General Public License along with
15## this program; if not, see <http://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @deftypefn {Function File} {@var{se2} =} reflect (@var{se})
19## Reflect structuring element of strel object.
20##
21## Returns another strel object with all its elements reflected.  If @var{se} is
22## a sequence of strel objects, reflects each one of them.  If @var{se} is a
23## non-flat structuring element, its height is reflected accordingly.
24##
25## Reflection is a rotation of 180 degrees around the center, including for
26## N-dimensional matrices.
27##
28## @seealso{strel, @@strel/getheight, @@strel/getsequence, @@strel/translate}
29## @end deftypefn
30
31function se = reflect (se)
32
33  ## FIXME this should be done in a smarter way for non-arbitrary shapes (but
34  ##       then we may need to also change some of the options values...)
35  if (isempty (se.seq))
36    se = rotate_strel (se);
37  else
38    for idx = 1:numel (se.seq)
39      se.seq{idx} = rotate_strel (se.seq{idx});
40    endfor
41  endif
42
43endfunction
44
45function se = rotate_strel (se)
46  nhood  = getnhood (se);
47  height = getheight (se);
48  if (se.flat)
49    se = strel ("arbitrary", rotate (nhood));
50  else
51    se = strel ("arbitrary", rotate (nhood), rotate (height));
52  endif
53endfunction
54
55function rot = rotate (ori)
56  rot = reshape (ori(end:-1:1), size (ori));
57
58  ## For Matlab compatibility:
59  ## Check if any of the sides has an even size. If so, we create a larger
60  ## matrix, all even sized, and place the rotated matrix on the top left
61  ## corner (and whatever top and left means for N dimensions)
62  if (any (mod (size (ori)+1, 2)))
63    ## get subcript indices of the elements that matter
64    ori_ind = find (ori);
65    [subs{1:ndims (ori)}] = ind2sub (size (ori), ori_ind);
66    rot = zeros ((floor (size (ori) /2) *2) +1, class (ori));
67    rot_ind = sub2ind (size (rot), subs{:});
68    rot(rot_ind) = ori(ori_ind);
69  endif
70endfunction
71