function type = tjsr_estimatenorm( type, VV);
%Diese Funktion gehoert zu tjsr

incounter=0;
outcounter=0;

%check if we test old vertices
if(type.opt.testoldvertex==0);
    selectoldvertexflag=0;
elseif(type.opt.testoldvertex==1)
    %if number of vertices increases much
    REESTIMATE=2; %by what factor the number of vertices of the polytope must grow, until we reestimate all vertices
    val=log2(sum(cumsum(vertcat(type.cyclictree.L{:}),2),1))/log2(REESTIMATE);
    [~,~,val]=unique(ceil(val));
    val=diff(val');
    val=val(end);
    MINNUM=1000;
    if(val && sum([type.cyclictree.L{:}])>MINNUM); selectoldvertexflag=1; else; selectoldvertexflag=0; end;
elseif(type.opt.testoldvertex==2)
    %if option is given 
    selectoldvertexflag=1;
else
    error('Wrong value for ''testoldvertex''.'); 
end    



if(selectoldvertexflag)
    type.info.infotext=vprintf('Estimate old vertices. ','imp',[2,type.opt.verbose],'str',type.info.infotext); 
end
maxlevel=size(type.cyclictree.L{1},2);
nestimate=0;
for i=1:type.counter.numordering
    %select vertices which we want to estimate
    
    %select all new vertices
    idx_all = type.cyclictree.level{i}==maxlevel;
    
    %select old vertices if there are enough new vertices in the polytope
    if(selectoldvertexflag)
        idx = type.cyclictree.level{i}<type.cyclictree.level{i}(end)-5 & (isnan(type.cyclictree.norm{i}) | type.cyclictree.norm{i}>1-type.opt.epsequal); %XX also test dead vertices, since they may be inside now
        idx_all = idx_all | idx; %5 is not chosen for a specific reason. I hope it is good.
    end
    
    pts=type.cyclictree.V{i}(:,idx_all);
    nestimate=nestimate+size(pts,2);
    type.info.infotext=vprintf('Estimate %i vertices for tree %i.\n',size(pts,2),i, 'imp',[4,type.opt.verbose],'str',type.info.infotext); 
    [norm_est,region]=estimatepolytopenorm(pts,VV,[],type.info.algorithm);

    
    type.cyclictree.norm_est{i}(idx_all)=norm_est;
    idx_all=find(idx_all);
    
    
    if(type.opt.fastnorm>=1)
        idx=norm_est<region(1)-type.opt.epsequal;
        idx=idx_all(idx);
        type.cyclictree.norm{i}(idx)=TJSR_INSIDE;
        incounter=incounter+length(idx);
    end

    if(type.opt.fastnorm>=2); 
        %only set to TJSR_OUTSIDE if there is no value yet for the norm XX
        idx=norm_est>region(2)+type.opt.epsequal;
        idx=idx_all(idx);
        if(~isempty(idx))
            2;
        end
        type.cyclictree.norm{i}(idx)=min(type.cyclictree.norm{i}(idx),TJSR_OUTSIDE); 
        outcounter=outcounter+length(idx);
    end;
    
end;
if(nestimate);
    type.info.infotext=vprintf('Estimate %i vertices.\n',nestimate, 'imp',[3,type.opt.verbose],'str',type.info.infotext); 
end

if(incounter);  type.info.infotext=vprintf('In: %i, ',incounter ,'imp',[2,type.opt.verbose],'str',type.info.infotext); end;
if(outcounter); type.info.infotext=vprintf('Out: %i, ',outcounter,'imp',[2,type.opt.verbose],'str',type.info.infotext); end;
   
end

function val = TJSR_INSIDE;                         val=.5; end %
function val = TJSR_OUTSIDE;                        val=inf;  end %

function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing.   