% function [errRMS errMEAN errSTD]=ProjectError(GRIDpoints,T,PI,Ti,intrinsic)
%
%   Receives 'GRIDpoints'. A structure containing .x_px and .X (2D image
%   points and 3D struture points)
%   Recevies 'Tplane' (plane pose), 'PI' (mirror plane pose) and 'intrinsic' to re-project 'X' into 'x_px' and compute the
%   projection error (RMS,mean,std)
%   - intrinsic is a structure with fields fc,cc,kc,alpha
%

function [errRMS errMEAN errSTD err]=ProjectError(GRIDpoints,Tplane,PI,Ti,intrinsic)

n=size(PI,2);

err=[];
for i=1:n
 T=Ti(:,:,i)*ReflectTransf(PI(1:3,i),-PI(4,i))*inv(Ti(:,:,i))*Tplane;
 x_est=ProjectPoints(GRIDpoints(i).X,T,intrinsic.fc,intrinsic.cc,intrinsic.kc,intrinsic.alpha);
 d_error=sqrt(sum((GRIDpoints(i).x_px-x_est).^2));
 err=[err d_error];
 
 
%  figure(1),clf,
%  plot(x_est(1,:),x_est(2,:),'or');
%  hold on
%  plot(GRIDpoints(i).x_px(1,:),GRIDpoints(i).x_px(2,:),'+g');
%  pause
end

errRMS=RMS(err);
if nargout>1
 errMEAN=mean(err);
end
if nargout>2
 errSTD=std(err);
end