% function [T Ti x X]=LoadData(str,n,DO_OPT)
% str - string form variable data
% n - number of transformations to load
% DO_OPT - if is do use optimization or not

function [T Ti Xout K projERR]=LoadData(str,n,DO_OPT,ID)

eval([ 'load ''' str ''';' ])
n_aux=n_ima;
if exist('n','var');
 if ~isempty(n)
  n_ima=n;
 end
end
if ~exist('DO_OPT','var');DO_OPT=0;end

N=min(sum(active_images),n_ima);

T=NaN(4,4,N);
Ti=NaN(4,4,N);
projERR(N)=struct();
Xout(N)=struct();
i=1;j=1;
ind=[];
while (j<=n_aux) && (i<=n_ima)
 if active_images(j)
  eval([ 'T(:,:,i)=Rt2T(Rc_' num2str(j) ' ,Tc_' num2str(j) ' ); ' ]);
  Ti(:,:,i)=T(:,:,i)*inv(T(:,:,1));
  if DO_OPT
   eval(['Xout(i).x_px=x_' num2str(j) ';']);
   eval(['Xout(i).X=X_' num2str(j) ';']);
   Xout(i).ind_image=j;
   Xout(i).sizePoints=size(Xout(i).X,2);
   projERR(i).err=ProjectPoints(Xout(i).X,T(:,:,i),fc,cc,kc,alpha_c)-Xout(i).x_px;
   projERR(i).derr=sqrt(sum([projERR(i).err].^2));
  end
  ind=[ind i];
  i=i+1;
 end
  j=j+1;
end

K.fc=fc;K.cc=cc;K.kc=kc;K.alpha=alpha_c;

% if DO_OPT
%  if nargin<4
%   ID='';
%  else
%   ID=['_' ID];
%  end
%  eval([ 'clear global X_g' ID ' fc1' ID ' cc1' ID ' kc1' ID ' alpha_c1' ID ' store' ID ';']);
%  eval([' global fc1' ID ' cc1' ID ' kc1' ID ' alpha_c1' ID ' store' ID ';' ])
%  eval([' fc1' ID '=fc;cc1' ID '=cc;kc1' ID '=kc;alpha_c1' ID '=alpha_c;']);
%  K.fc=fc;K.cc=cc;K.kc=kc;K.alpha=alpha_c;
%  for i=1:numel(ind)
%   eval(['store' ID '(i).x_px=x_' num2str(ind(i)) ';' ]);
%   eval(['store' ID '(i).X=X_' num2str(ind(i)) ';'])
%  end
%  eval(['Xout=store' ID ';'])
% end



