File: joinPolygons.m

package info (click to toggle)
octave-geometry 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 720 kB
  • sloc: cpp: 5,358; python: 379; objc: 328; makefile: 25
file content (61 lines) | stat: -rw-r--r-- 2,172 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
## Copyright (C) 2016 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{outpol} =} joinPolygons (@var{inpol})
## Convert a cell style set of polygons into an array of subpolygons
## separated by NaN rows.
##
## @var{inpol} is expected to be an Nx1 (column) cell array with each cell
## containing either a matrix of Mx1 (X), Mx2 (X,Y), or Mx3 (X,Y,Z) coordinates.
##
## @var{outpol} is a numeric Px1, Px2 or Px3 array os subpolygons each
## separated by a row of NaN values.
##
## @seealso{splitPolygons}
## @end deftypefn

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

function [polys] = joinPolygons (poly)

  if (! iscell (poly))
    error ('Octave:invalid-input-arg', 'joinPolygons: cell array expected');
  elseif (isempty (poly))
    polys = [];
    return
  elseif (isvector (poly) && any (cellfun (@(p)isrow(p) && numel(p) != 1, poly)))
    error ('Octave:invalid-input-arg', 'joinPolygons: column vectors expected');
  endif

  XY(1:2:2*size (poly, 1), :) = [{poly{:}}'];
  XY(2:2:2*size (poly, 1) - 1, :) = NaN (1, size (poly{1}, 2));
  polys = cell2mat (XY);

endfunction

%!test
%! assert (joinPolygons ({1,2}), [1 2]);

%!test
%! assert (joinPolygons ({}), []);

%!test
%! XY = joinPolygons ({[1 6; 2 5; 3 4]; [4 3; 5 2; 6 1]});
%! assert (XY, [1 6; 2 5; 3 4; NaN NaN; 4 3; 5 2; 6 1]);

%!error <joinPolygons: function called with too many inputs> joinPolygons ([1 2 NaN 3 4], [56 NaN 78])
%!error <joinPolygons: column vectors expected>  joinPolygons ({[1,0], [0,2]});