function [xyreg, order]=concavitycheck(Spr, Edgs, E);
%
%D  : dir to check
%
%

fig_handle = figure;                 
nx=size(Edgs,2)-1;
ny=size(Edgs,2);
if (ceil(size(Edgs,2)/2)-floor(size(Edgs,2)/2))>0,
   nx=nx-1;
   ny=ny-1;   
end;
%axis ij;
axes=[min(Edgs(:,nx)) max(Edgs(:,ny)) min(Edgs(:,nx)) max(Edgs(:,ny))]
axis ij;
for k=1:size(Edgs,1), 
   h=plot(Edgs(k,nx),Edgs(k,ny),'.');
   h1=text(Edgs(k,nx),Edgs(k,ny),num2str(k));
   set(h1,'Fontsize',[10]);
   hold on,
end;
T=size(Spr,1);
NEdg=Edgs(:,1:T);
col=[1 0 0;0 1 0;0 0 1;1 0 1;0 1 1;1 1 0;0.5 0 1; 1 0.5 0; 0 1 0.5];
colc=[];
cptr=0;
axis ij;
for i=1:T,
    a=[];
   cptr=cptr+1;
   hold on,
   rg=text(Spr(i,2*T+2),Spr(i,2*T+1),num2str(i));
   set(rg,'Color',[1 0 0]);
   if cptr<=9,
      colc(i,:)=[0 0 0];
   else
      cptr=1;
      colc(i,:)=[0 0 0];
   end,
   ctour=[];
   
   indx=find(NEdg(:,i));
   ctour=[Edgs(indx,nx) Edgs(indx,ny)];
   ctx=ctour(:,1); 
   cty=ctour(:,2);
   h=plot(ctx,cty,'.'); %ag , inactive 12/01/00
   set (h,'color',[1 0 0]);
   hold on,
   title(['click clockwise on points of region' num2str(i) ', then right click to end']);   
   button=1;
   distc=zeros(size(Edgs,1),1);
   axis image xy;
   while button==1,
       [x,y,button]=ginput(1);
       if button==3,break;end;
       %hold on, h=plot(x,y,'.');
       for cpt=1:size(Edgs,1),
         distc(cpt)=norm([x y]-[Edgs(cpt,nx) Edgs(cpt,ny)]);
       end;
       [val,ind]=min(distc);
       a=cat(2,a,ind);
       hold on, h=plot(Edgs(ind,nx),Edgs(ind,ny),'.');set(h,'Color',[0 0 0]);
   end;  
   
   ctx1=Edgs(a,nx);
   cty1=Edgs(a,ny);
   ctx=ctx1;cty=cty1;
   hold on;
   h=line(cat(1,ctx,ctx(1)),cat(1,cty,cty(1)));
   set (h,'color',colc(i,:),'LineWidth',[2]);
   xyreg{i}=[ctx cty];
   order{i}=a;
end;
axis image xy,
close(fig_handle);
