function [Spr,Edgs, shape_springs]=reinit_regions(Spr,Edgs2,Rb, REGS, control_pts, shape, opt, shape_springs)
%function [Spr,Edgs]=reinitreg(Spr,Edgs2,Rb,opt,rgmore);
%
% function that reinitialises regions as growth/ungrowth proceeds
% enables to recalculate distances of one region towrds an other and reset values in springs matrix
% possible to save region layers as are calculated here. not saved as not required so far
% opt: =1: reinit. all
%      =0: only recenters centroids
%      =2: reinit but no recenter centroids
% rolland apr 2003
opt = 1;
T=size(Spr,1);
Edgs = Edgs2;
%trial:recentre vertices before ungrowth (for regions where some vertices were taken out and are now triangles)
xy=Spr(:,2*T+(1:2));
xyEdg = Edgs(:,2*T+(1:2));
NEdg = Edgs(:,1:T);
RR=zeros(size(Rb,1),size(Rb,2));
R2{1}=zeros(size(Rb,1),size(Rb,2));
for k=2:length(REGS)
    ctour = [shape(REGS{k}.edge,:); control_pts(REGS{k}.vertex,:)];
    %ctour=[];
    %indx=find(NEdg(:,k)); %indx that belong to region k
    %ctour=[xyEdg(indx,1) xyEdg(indx,2)];
    ctour = order_xy_pts(ctour);
    ctx = ctour(:,1); cty = ctour(:,2);
    R2{k}=roipoly(RR,ctx,cty);
    R2{1}=R2{1}|R2{k};
    if opt==0,
        Spr(k-1,end-1:end)=round(img_centroid(R2{k})); %puts region centroid at centroid of Edgs
    elseif opt==1,
        Spr(k-1,end-1:end)=round(img_centroid(R2{k})); %for trial
    end;
end

for i=1:length(shape_springs)
   spr = shape_springs(i);
   p1 = xyEdg(spr.indx(1),:);
   p2 = xyEdg(spr.indx(2),:);
   d = sqrt(sum((p1-p2).^2,2));
   spr.dist = d;
   shape_springs(i) = spr;
end


return
%if opt==0|opt==1,
%Spr(1,2*T+(1:2))=round(img_centroid(R2{1}));  %put overall organ centroid at centroid of Edgs
%end;
%%%%compute new distances between points
% if opt==1|opt==2,
%     N=Spr(:,1:T);
%     D=Spr(:,T+1:2*T);
%     for k=2:T,
%         rgk=R2{k};
% 
%         ck=Spr(k,2*T+(1:2));
%         for j=find(N(k,:)),
%             rgj=R2{j};
%             %cj=round(img_centroid(rgj));
%             cj=Spr(j,2*T+(1:2));  %for trial
%             %line([ck(1) cj(1)],[ck(2) cj(2)]);
%             % Distance between regions i and j
%             d=floor(distance(ck,cj));
%             % Find width of region k towards region j.
%             prof=getproftc(rgk,ck,cj,d);
%             if min(prof)==1,
%                 width=0;
%             else
%                 % width=min(find(prof==0));
%                 width=sum(prof)-1.5; % changed april 03: see getproftcfunction: added 1 pix before, 1 after
%             end
%             D(k,j)=width;
%         end
%     end;
%     Spr(:,T+1:2*T)=D;
% end;
if opt==1|opt==2,
    N=Spr(:,1:T);
    D = zeros(size(N));
    for i=1:size(R,3)-1
        r = R(:,:,i+1);
        ci = Spr(i,2*T+(1:2));
        for j=find(N(i,:))
            r = R(:,:,j+1);
            cj=Spr(j,2*T+(1:2));  %for trial

            d = matdistance(ci', cj');
            % this function gets the profile of the region and its neighbours. As
            % far as I can tell the result will be the same as N. Proof, since
            % the images are binary, if the minimum value in prof is 1, then the
            % two centroids belong to the same region and so they cannot be
            % neighbours.
            prof = getproftc(r, ci, cj, round(d(:))');
            if min(prof)==1,
                width=0;
            else
                width=max(find(prof==0));
            end
            D(i,j)=width;
        end
    end
    S = [N D xy];
end
return;

%%%%%%%%%%%%%added: compute new distances edges-centroids
%%%%compute new distances between points
Edgs=Edgs2;

if opt==1|opt==2,
    xy=Spr(:,2*T+(1:2));
    NEdg = Edgs(:,1:T);
    DEdg = Edgs(:,T+(1:T));

    for k=1:T,

        ck=Spr(k,2*T+(1:2));

        %plot(xy(i,1),xy(i,2),'*'); inactiv 24/01/00
        for l=find(NEdg(:,k)),
            el=Edgs(l,2*T+(1:2));
            %distance between ci and edges%ag
            %dis=round(distance(ck,el));
            dis=matdistance(ck',el');
            DEdg(l,k)=dis';
        end
        %else De{i}=[];
    end;
    Edgs(:,T+1:2*T)=DEdg;

end;
