ERR_tr = [];   
ERR_te = [];   

SQ = 'Ballet';

ctr_cells = loadOff(['.\' SQ '\template\CVT5000_250\visu_color4matching.off'], 1);
cellsadjacency = load(['.\' SQ '\template\CVT5000_250\CVT\cells_adjacency.txt']);
edges = cellsadjacency + 1;
avgEdgeL = mean(sqrt(sum((ctr_cells.coords(edges(:,1), :) - ctr_cells.coords(edges(:,2), :)).^2, 2)));

%%
folderName = ['./' SQ '/corres_matchingExperiment/seq1(Training_fullCVT_Tree20)'];
filename = dir(folderName);
endIdx = numel(filename);
 
for f = 3:endIdx
    tmp = load([folderName '\' filename(f).name]);
    corrs = tmp(:,3);
    predicted = ctr_cells.coords(corrs+1,:);
    error = sqrt(sum((predicted - ctr_cells.coords).^2, 2));
    ERR_tr = [ERR_tr; error];
end
%%
folderName = ['./' SQ '/corres_matchingExperiment/seq2(Testing_fullCVT_Tree20)'];
filename = dir(folderName);
endIdx = numel(filename);
for f = 3:endIdx
    tmp = load([folderName '\' filename(f).name]);
    corrs = tmp(:,3);
    predicted = ctr_cells.coords(corrs+1,:);
    error = sqrt(sum((predicted - ctr_cells.coords).^2, 2));
    ERR_te = [ERR_te; error];

end
%%
% mean(ERR(:))
% std(ERR(:))
%%
figure;
maxAllowedD = 0:avgEdgeL:50*avgEdgeL;
cur_tr = zeros(length(maxAllowedD), 1);         cur_te = zeros(length(maxAllowedD), 1);
for th = maxAllowedD
   cur_tr(th == maxAllowedD) = sum(ERR_tr <= th)/length(ERR_tr);
   cur_te(th == maxAllowedD) = sum(ERR_te  <= th)/length(ERR_te );   
end

plot(maxAllowedD, cur_tr,'b-'); hold on;    
plot(maxAllowedD, cur_te,'r-');     
