function [error_arc,error_cur]=Compute_reparam_criteria(x,iflag)
%function [error_arc,error_cur]=Compute_reparam_criteria(Aux,iflag)
global Invariant ini_Val 

%Initialization
nn=ini_Val.ncontrol;
N=(nn-1)*ini_Val.samp_reparam;
gaussp=linspace(0,1,N);
u=gaussp;
Invariant_sample_bis = precal_generic(gaussp); 
[C,C1,C2] = interpolate(Invariant_sample_bis,x);
au = sqrt(C1(:,1).^2 + C1(:,2).^2);

%to compute span length
temp = ini_Val.U;
temp(1:ini_Val.p) = [];
temp(end-ini_Val.p+1:end)=[];
ini_Val_ = ini_Val;
ini_Val_.p = 1;
ini_Val_.U = [zeros(1,ini_Val_.p) temp ones(1,ini_Val_.p)];
ini_Val_.ncontrol=(length(ini_Val_.U)-(ini_Val_.p+1));
Invariant_sample_tool = precal_generic_(gaussp,ini_Val_); 
span_size = 1./abs(Invariant_sample_tool.dNsh(:,1));

arclength=zeros(size(gaussp));
arclength(1)=0;
for i=2:length(au)
    arclength(i)=arclength(i-1) + (au(i-1)+au(i))/2*Invariant_sample_bis.gweight;
end
k= ini_Val.cc*(abs(C1(:,1).*C2(:,2)-C2(:,1).*C1(:,2))./(C1(:,1).^2 + C1(:,2).^2)).^ini_Val.ce+1;
k = smooth(k,ini_Val.smoo_reparam);

curvature_indicator(1)=0;
for i=2:length(au)
    curvature_indicator(i)=curvature_indicator(i-1)+(k(i-1)*span_size(i-1)+k(i)*span_size(i))/2*Invariant_sample_bis.gweight;
end



% k=abs(Aux.bu./Aux.au.^2);
% u(1)=0;
% arclength(1)=0;
% curvature_indicator(1)=0;
% for i=1:length(k)/ini_Val.gaussno
%     ind=ini_Val.gaussno*(i-1)+1:ini_Val.gaussno*i;
%     u(i+1) = u(i)+sum(Invariant.gweight(ind)); 
%     arclength(i+1)=arclength(i)+sum(Invariant.gweight(ind).*Aux.au(ind)); 
%     curvature_indicator(i+1)=curvature_indicator(i)+sum(Invariant.gweight(ind).*  ...
%         (ini_Val.cc*k(ind).^ini_Val.ce + 1)); 
% end

arclength=arclength/arclength(end);
arclength_test=u;
curv_test=0:curvature_indicator(end)/(length(curvature_indicator)-1):curvature_indicator(end);

error_arc=norm(arclength-arclength_test);
error_cur=norm(curvature_indicator-curv_test);


if iflag==1
 figure
 plot(u,arclength,'ko-')
 hold on
 plot(u,arclength_test,'gx')
 figure
 plot(curvature_indicator,'ro-')
 hold on
 plot(curv_test,'gx')

%  figure
%  set(gca,'FontName','times')
% set(gca,'FontSize',16)
% set(gca,'LineWidth',2)
% 
%  axis square
% axis tight
% set(gcf,'paperPositionMode','auto')
% %title('Oblate vesicle','FontSize',22,'interpreter','LaTeX')
% axis normal
% scalefactor = .9;
% g = get(gca,'Position');
% g(1:2) = g(1:2) + (1-scalefactor)/2*g(3:4);
% g(3:4) = scalefactor*g(3:4);
% set(gca,'Position',g);
% 
%  
%  
%  plot(u,0*u,'k+','MarkerSize',15,'MarkerFaceColor','k','LineWidth',2)
%  hold on
%  xx=linspace(0,1,1000);
%  plot(xx,spline(arclength,curv,xx),'k-','LineWidth',2)
%  ylabel('$\kappa$','FontSize',22,'interpreter','LaTeX')
% xlabel('$u$','FontSize',22,'interpreter','LaTeX')

 
 
end


