Home > matGeom > polygons2d > readPolygonSet.m

readPolygonSet

PURPOSE ^

READPOLYGONSET Read a set of simple polygons stored in a file.

SYNOPSIS ^

function polys = readPolygonSet(filename)

DESCRIPTION ^

READPOLYGONSET Read a set of simple polygons stored in a file.
   
   POLY = readPolygonSet(FILENAME);
   Returns the polygon stored in the file FILENAME.
   Polygons are assumed to be stored in text files, without headers, with
   x and y coordinates packed in two separate lines:
     X11 X12 X13 ... X1N
     Y11 Y12 Y13 ... Y1N
     X21 X22 X23 ... X2N
     Y21 Y22 Y23 ... Y2N

   Each polygon may have a different number of vertices. The result is a
   cell array of polygon, each cell containing a N-by-2 array representing
   the vertex coordinates.

   See also:
   polygons2d

   ---------
   author : David Legland
   INRA - TPV URPOI - BIA IMASTE
   created the 11/04/2004.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function polys = readPolygonSet(filename)
0002 %READPOLYGONSET Read a set of simple polygons stored in a file.
0003 %
0004 %   POLY = readPolygonSet(FILENAME);
0005 %   Returns the polygon stored in the file FILENAME.
0006 %   Polygons are assumed to be stored in text files, without headers, with
0007 %   x and y coordinates packed in two separate lines:
0008 %     X11 X12 X13 ... X1N
0009 %     Y11 Y12 Y13 ... Y1N
0010 %     X21 X22 X23 ... X2N
0011 %     Y21 Y22 Y23 ... Y2N
0012 %
0013 %   Each polygon may have a different number of vertices. The result is a
0014 %   cell array of polygon, each cell containing a N-by-2 array representing
0015 %   the vertex coordinates.
0016 %
0017 %   See also:
0018 %   polygons2d
0019 %
0020 %   ---------
0021 %   author : David Legland
0022 %   INRA - TPV URPOI - BIA IMASTE
0023 %   created the 11/04/2004.
0024 %
0025 
0026 % the set of polygons (no pre-allocation, as we do not know how many
0027 % polygons are stored)
0028 polys = {};
0029 
0030 % index of polygon
0031 p = 0;
0032 
0033 % open file for reading
0034 fid = fopen(filename, 'rt');
0035 
0036 % use an infinite loop, terminated in case of EOF
0037 while true
0038     % set of X, and Y coordinates
0039     line1 = fgetl(fid);
0040     line2 = fgetl(fid);
0041     
0042     % break loop if end of file is reached
0043     if line1 == -1
0044         break;
0045     end
0046    
0047     % create a new polygon by concatenating vertex coordinates
0048     p = p + 1;
0049     polys{p} = [str2num(line1)' str2num(line2)']; %#ok<AGROW,ST2NM>
0050 end    
0051 
0052 % close file
0053 fclose(fid);

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