%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% refine multi camera calibration 
addpath(genpath([pwd '/../core/']));

%% input parameters
calib_dir       = 'L:\array\tcf\6C_A\calib\';
calib_pat       = '%s/camera-model.mat';

font_size       = 20;

%% load setup
calib_file      = sprintf(calib_pat, calib_dir);
setup           = load(calib_file);
n_cameras       = setup.n_cameras;
n_views         = setup.n_views;

%% reconstruct points from different calibration targets
% define reconstruction options
ropts          = struct();
ropts.R_tolerance_img = 10;
ropts.R_tolerance_obj = 10;
ropts.re_lim          = [-100 -300 -1000; 700 300 1000];
ropts.adjacency= setup.adjacency;

for v = 1 : n_views
    fprintf('reconstructing markers in view %d\n', v);
    %% load image coordinates of detected points
    im_mark     = cell(1,n_cameras);
    for c = 1 : n_cameras
        im_mark{c} = setup.camera(c).view(v).im_markers;
        fprintf('cam%d: %d markers visible\n', c, size(im_mark{c},2));
    end
    
    %% triangulate
    re_matched  = match3d3c(setup.camera, im_mark, ropts);
    fprintf('reconstructed %d markers\n', size(re_matched,2));
end