Home > matGeom > polygons2d > splitPolygons.m

splitPolygons

PURPOSE ^

SPLITPOLYGONS Convert a NaN separated polygon list to a cell array of polygons.

SYNOPSIS ^

function polygons = splitPolygons(polygon)

DESCRIPTION ^

SPLITPOLYGONS Convert a NaN separated polygon list to a cell array of polygons.

   POLYGONS = splitPolygons(POLYGON);
   POLYGON is a N-by-2 array of points, possibly with pairs of NaN values.
   The functions separates each component separated by NaN values, and
   returns a cell array of polygons.

   See also:
   polygons2d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function polygons = splitPolygons(polygon)
0002 %SPLITPOLYGONS Convert a NaN separated polygon list to a cell array of polygons.
0003 %
0004 %   POLYGONS = splitPolygons(POLYGON);
0005 %   POLYGON is a N-by-2 array of points, possibly with pairs of NaN values.
0006 %   The functions separates each component separated by NaN values, and
0007 %   returns a cell array of polygons.
0008 %
0009 %   See also:
0010 %   polygons2d
0011 %
0012 
0013 % ------
0014 % Author: David Legland
0015 % e-mail: david.legland@inra.fr
0016 % Created: 2007-10-12,    using Matlab 7.4.0.287 (R2007a)
0017 % Copyright 2007 INRA - BIA PV Nantes - MIAJ Jouy-en-Josas.
0018 
0019 if iscell(polygon)
0020     % case of a cell array
0021     polygons = polygon;
0022     
0023 elseif sum(isnan(polygon(:))) == 0
0024     % single polygon -> no break
0025     polygons = {polygon};
0026     
0027 else
0028     % find indices of NaN couples
0029     inds = find(sum(isnan(polygon), 2) > 0);
0030     
0031     % number of polygons
0032     N = length(inds) + 1;
0033     polygons = cell(N, 1);
0034 
0035     % iterate over NaN-separated regions to create new polygon
0036     inds = [0; inds; size(polygon, 1)+1];
0037     for i = 1:N
0038         polygons{i} = polygon((inds(i)+1):(inds(i+1)-1), :);    
0039     end
0040 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019