function mask=readalp(alp_file, grid_size)
[fid, message] = fopen(alp_file);
if(-1 == fid)
input(message);
exit;
end
polynum = 1;
while ~feof(fid)
line = fgetl(fid);
llength = size(line);
x=[];y=[];
if line(1,1) == 'P'
x=[]; y=[];
if line(1, llength(1,2)) == ';'
leave = 1;
line(1, llength(1,2)) = ',';
else
leave = 0;
end
line = strrep(line, ',', ' ');
linecell = strsplit(line, ' ');
pointnum=str2num(linecell{2});
[tmp index]=size(linecell);
for i=3:2:index
if linecell{i} == '*'
tmpx=size(x);
x =[x x(1, tmpx(1,2))];
else
x=[x str2num(linecell{i})];
end
if linecell{i+1} == '*'
tmpy=size(y);
y= [y y(1, tmpy(1,2))];
else
y=[y str2num(linecell{i+1})];
end
end
while 1
if leave == 1
break;
end
line='';
line=fgetl(fid);
llength=size(line);
if line(1, llength(1,2)) == ';'
leave = 1;
line(1, llength(1,2)) = ',';
else
leave = 0;
end
line=strrep(line, ',', ' ');
linecell=strsplit(line, ' ');
[tmp index]=size(linecell);
for i =1:2:index
if linecell{i} == '*'
tmpx=size(x);
x=[x x(1, tmpx(1,2))];
else
x=[x str2num(linecell{i})];
end
if linecell{i+1} == '*'
tmpy=size(y);
y=[y y(1, tmpy(1,2))];
else
y=[y str2num(linecell{i+1})];
end
end
end
polyx{polynum}=x;
polyy{polynum}=y;
polynum = polynum +1;
end
end
fclose(fid);
minx=min(polyx{1});
miny=min(polyy{1});
maxx=max(polyx{1});
maxy=max(polyy{1});
for i = 2:polynum-1
if minx > min(polyx{i})
minx = min(polyx{i});
end
if maxx < max(polyx{i})
maxx = max(polyx{i});
end
if miny > min(polyy{i})
miny = min(polyy{i});
end
if maxy < max(polyy{i})
maxy = max(polyy{i});
end
end
for i = 1:polynum-1
polyx{i}=ceil((polyx{i}-minx)./grid_size);
polyy{i}=ceil((polyy{i}-miny)./grid_size);
end
M=ceil((maxx-minx)/grid_size);
N=ceil((maxy-miny)/grid_size);
pm = zeros(N,M);
for i=1:polynum-1
pm =pm+poly2mask(polyx{i}, polyy{i}, N, M);
end
mask = pm;
size(mask)
imshow(mask)
end
|