summaryrefslogtreecommitdiff
path: root/inst/isShapeMultipart.m
blob: 3db52d3dea03f2265168f1cb035ed9537b6a712e (plain)
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
## Copyright (C) 2016-2020 Philip Nienhuis
##
## This program 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.
##
## This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {} {@var{mp} =} isShapeMultipart (@var{x}, @var{y})
## Checks if a polygon or polyline consists of multiple parts separated by
## NaN rows.
##
## @var{x} and @var{y} must be vectors with the same orientation (either
## row vectors of column vectors).
##
## Output argument @var{mp} is zero (false) if the shape contains no NaN
## rows, otherwise it equals the number of polygon/polyline shape parts.
##
## @seealso{}
## @end deftypefn

## Author: Philip Nienhuis <prnienhuis@users.sf.net>
## Created: 2016-05-22

function mp = isShapeMultipart (x, y)

  if (nargin  < 2)
    print_usage ();
  endif
  if (isrow (x) != isrow (y))
    error ("isShapeMultipart: x and y must be both row vectors or both column vectors")
  endif
  if (numel (x) != numel (y))
    error ("isShapeMultipart: incompatible input vectors");
  endif

  mp = 0;
  mp_x = find (isnan (x));
  mp_y = find (isnan (y));
  if (! isempty (mp_x) && ! isempty (mp_y) && numel (mp_x) == numel (mp_y))
    if (any (mp_x - mp_y))
      error ("isShapeMultipart: NaN positions don't match");
    else
      mp = numel (mp_x) + 1;
    endif
  endif

endfunction


%!test
%! assert (isShapeMultipart ([0 1 0], [1 0 0]), 0);

%!test
%! h = [0 0 1 NaN 2 2 NaN 3 3];
%! k = [0 1 0 NaN 2 3 NaN 3 2];
%! assert (isShapeMultipart (h, k), 3);

%!error <x and y must be both> isShapeMultipart ([0 0 1 NaN 2 2 NaN 3 3], ...
%!                                               [0 1 0 NaN 2 3 NaN 3 2]')
%!error <NaN positions don't match> isShapeMultipart ([0 1 NaN 2 3 NaN 4], ...
%!                                                    [0 1 NaN 2 NaN 3 4])
%!error <incompatible input> isShapeMultipart ([0 0 1 NaN 2 2 NaN 3 3], ...
%!                                       [0 1 0 NaN 2 3 NaN 3])