% this script can be runned after the processing to plot figure 5 of the
% publication 
%% plot figure 3 of the manuscript
figure(3);
close(3)
fig3=figure(3);
set(fig3,'Units','centimeters');
set(fig3,'Position',[10,5,14.3,12.6])

%define the indices of the plotted frames and the x,y and z limits
Yindex = 248; 
Zindex1 = 566; %PSF planes
Zindex2 = 461;
zlims = [0.25,0.65]; 
xlims_bscan = [0,0.225]; 
xlims_enface1 = [0.121,0.221]; 
ylims_enface1 = [0.05,0.15]; 
xlims_enface2 = [0.1,0.20]; 
ylims_enface2 = [0.05,0.15]; 
scalebar_length = 50e-3;
scalebar_length2 = 20e-3;
methods = {'DFT','RFIAA','DFT + ISAM','MIAA + ISAM'};
c_map=morgenstemning(256);
%plot all the images in a loop
for i=1:4
    image = images{i};
    subplot(3,4,i)
    imagesc(Xroi,Zroi,squeeze(image(:,:,Yindex)))
    caxis([-80,0])
    colormap(c_map)
    axis equal tight
    ylim(zlims)
    xlim(xlims_bscan)
    hold on
    plot(xlims_enface1,[1,1]*z_isam(Zindex1),'-b','LineWidth',1.5)
    plot(xlims_enface2,[1,1]*z_isam(Zindex2),'-r','LineWidth',1.5)
    plot(xlims_bscan(1)+(xlims_bscan(2)-xlims_bscan(1))/15+[0,scalebar_length],(zlims(2)-(zlims(2)-zlims(1))/40)*[1,1],'-w','LineWidth',3)
    if i==1
        text(xlims_bscan(1)+(xlims_bscan(2)-xlims_bscan(1))/15+scalebar_length/2,(zlims(2)-3*(zlims(2)-zlims(1))/40),string(round(scalebar_length*1e3))+' '+char(181)+'m','Color','w','HorizontalAlignment','Center')
    end
    title(methods{i})
    if i==4
        c(1) = colorbar();
    end
    subplot(3,4,4+i)
    imagesc(Xroi,Xroi,squeeze(image(Zindex1,:,:))')
    caxis([-70,0])
    axis equal tight
    xlim(xlims_enface1)
    ylim(ylims_enface1)
    hold on
    plot(xlims_enface1(1)+(xlims_enface1(2)-xlims_enface1(1))/12+[0,scalebar_length2],(ylims_enface1(2)-(ylims_enface1(2)-ylims_enface1(1))/20)*[1,1],'-w','LineWidth',3)
    if i==1
        text(xlims_enface1(1)+(xlims_enface1(2)-xlims_enface1(1))/12+scalebar_length2/2,(ylims_enface1(2)-3*(ylims_enface1(2)-ylims_enface1(1))/20),string(round(scalebar_length2*1e3))+' '+char(181)+'m','Color','w','HorizontalAlignment','Center')
    end
    
    if i==4
        c(2) = colorbar();
    end
    
    subplot(3,4,8+i)
    imagesc(Xroi,Xroi,squeeze(image(Zindex2,:,:))')
    caxis([-80,0])
    axis equal tight
    xlim(xlims_enface2)
    ylim(ylims_enface2)
    hold on
    plot(xlims_enface2(1)+(xlims_enface2(2)-xlims_enface2(1))/12+[0,scalebar_length2],(ylims_enface2(2)-(ylims_enface2(2)-ylims_enface2(1))/20)*[1,1],'-w','LineWidth',3)
    if i==1
        text(xlims_enface2(1)+(xlims_enface2(2)-xlims_enface2(1))/12+scalebar_length2/2,(ylims_enface2(2)-3*(ylims_enface2(2)-ylims_enface2(1))/20),string(round(scalebar_length2*1e3))+' '+char(181)+'m','Color','w','HorizontalAlignment','Center')
    end
    if i==4
        c(3) = colorbar();
    end
    
end
% adapt the layout of the plot and add labels
figratio = fig3.Position(3)/fig3.Position(4);
ratio_Bscan = (xlims_bscan(2)-xlims_bscan(1))/(zlims(2)-zlims(1))*figratio;
ratio_enface1 = (xlims_enface1(2)-xlims_enface1(1))/(ylims_enface1(2)-ylims_enface1(1))*figratio;
ratio_enface2 = (xlims_enface2(2)-xlims_enface2(1))/(ylims_enface2(2)-ylims_enface2(1))*figratio;

fig.xpos = (0:4)*0.2225+0.035;
fig.ypos = [0.54,0.275,0.01];
fig.width = 0.2145;

fig.ratio = [0,0,0];
fig.ratio(1) = (xlims_bscan(2)-xlims_bscan(1))/(zlims(2)-zlims(1))/figratio;
fig.ratio(2) = (xlims_enface1(2)-xlims_enface1(1))/(ylims_enface1(2)-ylims_enface1(1))/figratio;
fig.ratio(3) = (xlims_enface2(2)-xlims_enface2(1))/(ylims_enface2(2)-ylims_enface2(1))/figratio;

pos = zeros(4,1);
for i=1:4
    for j=1:3
        pos(1) = fig.xpos(i);
        pos(2) = fig.ypos(j);
        pos(3) = fig.width;
        pos(4) = fig.width/fig.ratio(j);
        ch_no = 16-(3*(i-1)+j)-(i==4)*(j-1);
        fig3.Children(ch_no).Position=pos;
        if j==1
            fig3.Children(ch_no).Title.Position(2) = fig3.Children(ch_no).Title.Position(2)+0.005;
        end
    end
    ch_no1 = 16-3*i-(i==4)*2;
    fig3.Children(ch_no1).YAxis.LineWidth = 1.5;
    fig3.Children(ch_no1).XAxis.LineWidth = 1.5;
    fig3.Children(ch_no1).YColor = 'r';
    fig3.Children(ch_no1).XColor = 'r';
    ch_no2 = 16-(3*(i-1)+2)-(i==4)*1;
    fig3.Children(ch_no2).YAxis.LineWidth = 1.5;
    fig3.Children(ch_no2).XAxis.LineWidth = 1.5;
    fig3.Children(ch_no2).YColor = 'b';
    fig3.Children(ch_no2).XColor = 'b';
end

for i = 7:15
    fig3.Children(i).XTick = [];
    fig3.Children(i).YTick = [];
end

for j=1:3
    fig3.Children(2*j).XTick = [];
    fig3.Children(2*j).YTick = [];
    pos(1) = fig.xpos(5)-0.002;
    pos(2) = fig.ypos(j);
    pos(3) = 0.012;
    pos(4) = fig.width/fig.ratio(j);
    fig3.Children(7-2*j).Position = pos;
    c(j).Label.String = 'Intensity (dB)';
    c(j).Label.Position(1) = 4.0;
end

fig3.Children(14).YLabel.String = 'in focus';
fig3.Children(14).YLabel.Color = 'k';
fig3.Children(13).YLabel.String = 'out of focus';
fig3.Children(13).YLabel.Color = 'k';
% add labels
labels = 'abcd';
fig.a = [];
i=1;
    for j=1:3
        pos(1) = -0.012;%fig.xpos(1)-0.008;
        pos(2) = fig.ypos(j)+fig.width/fig.ratio(j)+0.008+0.012;
        pos(3) = 0;%fig.width;
        pos(4) = 0;%fig.width/fig.ratio(j);
        fig.a(3*(i-1)+j) = annotation('textbox','string', ['(',labels(j),')'],'FontWeight','bold','color','k','Position',pos','EdgeColor','none');
    end

% add anotations
t(1) = text(0.1245,0.108,'\leftarrow','Rotation',-30,'Color','w','parent',fig3.Children(2),'FontSize',14,'FontWeight','Bold');
t(2) = text(0.1245,0.108,'\leftarrow','Rotation',-30,'Color','w','parent',fig3.Children(7),'FontSize',14,'FontWeight','Bold');
t(3) = text(0.1985,0.0975,'\rightarrow','Rotation',-30,'Color','w','parent',fig3.Children(4),'FontSize',14,'FontWeight','Bold');
t(4) = text(0.1985,0.0975,'\rightarrow','Rotation',-30,'Color','w','parent',fig3.Children(8),'FontSize',14,'FontWeight','Bold');
t(5) = text(0.1985,0.0975,'\rightarrow','Rotation',-30,'Color','w','parent',fig3.Children(11),'FontSize',14,'FontWeight','Bold');
t(6) = text(0.1985,0.0975,'\rightarrow','Rotation',-30,'Color','w','parent',fig3.Children(14),'FontSize',14,'FontWeight','Bold');
%% add axes
t(7) = text(xlims_bscan(1)+0.005,zlims(1)+0.01,'\rightarrowx','Color','w','parent',fig3.Children(15),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','left');
t(8) = text(xlims_bscan(1)+0.01,zlims(1)+0.004,'\downarrow','Color','w','parent',fig3.Children(15),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','center','VerticalAlignment','top');
t(9) = text(xlims_bscan(1)+0.01,zlims(1)+0.03,'z','Color','w','parent',fig3.Children(15),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','center','VerticalAlignment','top');

t(10) = text(xlims_enface1(1)+0.0035,ylims_enface1(1)+0.004,'\rightarrowx','Color','w','parent',fig3.Children(14),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','left');
t(11) = text(xlims_enface1(1)+0.005,ylims_enface1(1)+0.002,'\downarrow','Color','w','parent',fig3.Children(14),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','center','VerticalAlignment','top');
t(12) = text(xlims_enface1(1)+0.005,ylims_enface1(1)+0.015,'y','Color','w','parent',fig3.Children(14),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','center','VerticalAlignment','top');

t(13) = text(xlims_enface2(1)+0.0035,ylims_enface2(1)+0.004,'\rightarrowx','Color','w','parent',fig3.Children(13),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','left');
t(14) = text(xlims_enface2(1)+0.005,ylims_enface2(1)+0.002,'\downarrow','Color','w','parent',fig3.Children(13),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','center','VerticalAlignment','top');
t(15) = text(xlims_enface2(1)+0.005,ylims_enface2(1)+0.015,'y','Color','w','parent',fig3.Children(13),'FontSize',12,'FontWeight','Bold','HorizontalAlignment','center','VerticalAlignment','top');
return
%% save figure
fig.name = 'figure3.pdf';
fig.pos = get(fig3,'Position');
set(fig3,'PaperPositionMode','Auto','PaperUnits','centimeters','PaperSize',[fig.pos(3), fig.pos(4)])
print(fig3, '-dpdf', fig.name);
print(fig3, '-dpng','-r500', [fig.name(1:end-4),'.png']);