clc
imstr=img_style;

[stereos1,stereos2]=textread([write_folder 'task.txt'],'%s %s'); 


angt=pi/18;
%cross ratio
ecrosst=0.5;
%Mapping error
dist=2;
%Nearest neighbors
neighbours=40;
%epipolar error
disppt=dist;
%Epipolar error
projpt=dist;
crossdis=3;

gridsize=20;

for i=1:size(stereos1,1)
    tic
    name1=stereos1{i,:};
    name2=stereos2{i,:};
  
    union_name=[name1,'-',name2];
    savename=[write_folder union_name];
    
    
    P1=load([folder 'cameras\' name1 '.txt']);
    P2=load([folder 'cameras\' name2 '.txt']);
    
    lines1=load([write_folder name1 '.lines']);
    lines2=load([write_folder name2 '.lines']);
    lines1=lines1(:,1:4);
    lines2=lines2(:,1:4);
    
    cross1=load([write_folder name1 '.cross']);
    cross2=load([write_folder name2 '.cross']);
    
    points1=load([write_folder name1 '.points']);
    points2=load([write_folder name2 '.points']);
    
    F = computeF(P1, P2);
    
    e2 = Epipole(F');
    e2=[e2(1),e2(2),1]';
    
    %surf candidates
    getPointcandidates(points1',size(points1,1),...
        points2',size(points2,1),F',0.5);
    point_candi_E=load('point_candidate.txt');

    
    callMP(lines1',lines2',cross1',cross2',...
        F',e2',...
        size(lines1,1),size(lines2,1),...
        size(cross1,1),size(cross2,1),dist,angt,ecrosst,crossdis);
    cross_match=load('crossLineMatch.txt');

    callNeighbour(lines1',size(lines1,1),...
       cross_match(:,1:2)',size(cross_match,1),neighbours,'nearneibour');
    Line_hom_N=load('nearneibour.nb');
    
    im2=imread([folder 'images\' name2,imstr]);
    
    r1=0;
    r2=size(im2,1);
    c1=0;
    c2=size(im2,2);
    
    callAM_BOOST(lines1',lines2',...
        cross_match',F',...
        size(lines1,1),...
        size(lines2,1),size(cross_match,1),...
        dist,angt,ecrosst,...
        Line_hom_N',size(Line_hom_N,2),...
        [write_folder name2 '.grid_l'],...
        gridsize,r1,r2,c1,c2);
    
    add_match=load('Addcandidates.txt');
    
    
    
    %Obtain all candidate
    all_match=[[cross_match(:,1) cross_match(:,3)];...
        [cross_match(:,2) cross_match(:,4)];...
        add_match(:,1:2)];
    
    [all_match,coreidx]=uniqueDense(all_match);
    [C1, ~, ic1] = unique(all_match(:,1));
    [C2, ~, ic2] = unique(all_match(:,2));
    C1=C1+1;
    C2=C2+1;
    
    save([savename '-linecandi.txt'],'all_match','-ascii');
    
    %triangulation network
    callLT(lines1(C1,:)',length(C1),neighbours,1);
    callLT(lines2(C2,:)',length(C2),neighbours,2);
    
    T1=loadTrang('nearestNeighbours1.txt',C1,size(lines1,1));
    T2=loadTrang('nearestNeighbours2.txt',C2,size(lines2,1));
    
    %distance from point to line segments
    callPLD(lines1(C1,:)',length(C1),points1',size(points1,1),neighbours,1);
    callPLD(lines2(C2,:)',length(C2),points2',size(points2,1),neighbours,2);
    %load pt line trang
    PLD1=loadPtLine('Pt_L_Neighbours1.txt',size(points1,1),size(lines1,1),C1);
    PLD2=loadPtLine('Pt_L_Neighbours2.txt',size(points2,1),size(lines2,1),C2);
    
    %Obtain line and point pairs
    callPM_PT_pre(lines1',lines2',...
        size(lines1,1),size(lines2,1),F',e2',all_match',...
        size(all_match,1),T1',T2',neighbours,...
        points1',size(points1,1),PLD1,...
        points2',size(points2,1),PLD2,dist,disppt,projpt,...
        point_candi_E',size(point_candi_E,1));
    pairwise_score=load('pairwiseM.txt');
    save([savename '-pairwise_line.txt'],'pairwise_score','-ascii');
 
    [pt_candi,pair_score_pt]=loadPtMatch();
   
    save([savename '-pairwise_pt.txt'],'pair_score_pt','-ascii');
    save([savename '-ptcandi.txt'],'pt_candi','-ascii');
    t=toc;
   [num2str(i) ' ' num2str(t)]
   
end

%% Triplet validation
Triplets_Validation





