% Plot reconstructed conductivity with or without numbers
%
% Inputs:
% n            size of the grid is nxn
% condmatH     conductivity values between horizontal neighbors, nx(n-1) matrix
% condmatV     conductivity values between vertical neighbors, (n-1)x n matrix
% fignum       figure window number
% fsize_in     font size
% dec_acc      how many decimals to show in numbers
% x_displmt    displacement of text in x direction
% y_displmt    displacement of text in y direction
%
% Samuli Siltanen May 2025

function EIT_FD_demo2D_recon_numbers_plot(n,condmatH,condmatV,fignum,fsize_in,dec_acc,x_displmt,y_displmt)

% Record and check the size of the conductivity matrices
[cHrow,cHcol] = size(condmatH);
if ~(cHrow==(cHcol+1))
    error('Conductivity matrix condmatH should be nx(n-1) size')
end
if ~(cHrow==n)
    error('Conductivity matrix condmatH has wrong size')
end
[cVrow,cVcol] = size(condmatV);
if ~(cVrow==(cVcol-1))
    error('Conductivity matrix condmatV should be (n-1)x n size')
end
if ~(cVcol==n)
    error('Conductivity matrix condmatV has wrong size')
end


% Set display parameters
geometry_set;
fsize = fsize_in;
str_format = ['%0.',num2str(dec_acc),'f'];

% Create plot window
figure(fignum)
clf

% Graphical parameters
lwidth_grid = .5;
color_whitish = .97*ones(1,3);
color_blackish = .05*ones(1,3);

% Left (Western) boundary has conductivity = 1
for rrr=1:n
    p = patch([0 Dx/2 0],(n-rrr)+[0 Dx/2 Dx],color_whitish);
    set(p,'edgecolor',color_blackish,'linewidth',lwidth_grid)
    hold on
end
% Right (Eastern) boundary has conductivity = 1
for rrr=1:n
    p = patch(n*Dx-[0 Dx/2 0],(n-rrr)+[0 Dx/2 Dx],color_whitish);
    set(p,'edgecolor',color_blackish,'linewidth',lwidth_grid)
    hold on
end
% Top (northern) boundary has conductivity = 1
for ccc=1:n
    p = patch((ccc-1)*Dx+[0 Dx/2 Dx],n*Dx-[0 Dx/2 0],color_whitish);
    set(p,'edgecolor',color_blackish,'linewidth',lwidth_grid)
    hold on
end
% Bottom (Southern) boundary has conductivity = 1
for ccc=1:n
    p = patch((ccc-1)*Dx+[0 Dx/2 Dx],[0 Dx/2 0],color_whitish);
    set(p,'edgecolor',color_blackish,'linewidth',lwidth_grid)
    hold on
end

% Conductivities inside the grid (not at the boundary
star_x = [-Dx/2 0    Dx/2  0   ];
star_y = [ 0    Dx/2 0    -Dx/2];
% First: conductivities between horizontal neighbors
for rrr = 1:n
    for ccc = 1:(n-1)
        pc = patch(ccc*Dx+star_x,(n-rrr+.5)*Dx+star_y,color_whitish);
        set(pc,'edgecolor',color_blackish,'linewidth',lwidth_grid);
        text((ccc-0)*Dx+x_displmt,(n-rrr+.5)*Dx+y_displmt,...
            num2str(condmatH(rrr,ccc),str_format),'fontsize',fsize);
    end
end
% Then: conductivities between vertical neighbors
for rrr = 1:(n-1)
    for ccc = 1:n
        pc = patch((ccc-.5)*Dx+star_x,(n-rrr)*Dx+star_y,color_whitish);
        set(pc,'edgecolor',color_blackish,'linewidth',lwidth_grid);
        text((ccc-.5)*Dx+x_displmt,(n-rrr)*Dx+y_displmt,...
            num2str(condmatV(rrr,ccc),str_format),'fontsize',fsize);
    end
end



% Axis settings
axis([-3*Dx (n+3)*Dx -3*Dx (n+3)*Dx])
axis equal
axis off





%
% % Horizontal neighbors
%
% % Create plot window
% figure(1)
% clf
%
% % Conductivities inside the grid (not at the boundary
% star_x = [-Dx/2 0    Dx/2  0   ];
% star_y = [ 0    Dx/2 0    -Dx/2];
% % Conductivities between horizontal neighbors
% for rrr = 1:n
%     for ccc = 1:(n-1)
%         curr_cond_color = colorscale_cond_scaled(condmatH(rrr,ccc),condmax);
%         pc = patch(ccc*Dx+star_x,(n-rrr+.5)*Dx+star_y,curr_cond_color);
%         set(pc,'edgecolor',curr_cond_color);
%         index = (ccc-1)*n+rrr;
%         text(ccc*Dx-.35*Dx,(n-rrr+.5)*Dx,num2str(index),'fontsize',fsize);
%         hold on
%     end
% end
%
% % Plot grid
% p1 = plot([0 n],[0 0],'k','linewidth',lwidth_grid);
% set(p1,'color',color_gridline)
% hold on
% p2 = plot([0 0],[0 n],'k','linewidth',lwidth_grid);
% set(p2,'color',color_gridline)
% for iii = 1:n
%     for jjj = 1:n
%         p3 = plot(jjj*[Dx Dx],[(iii-1)*Dx iii*Dx],'k','linewidth',lwidth_grid);
%         set(p3,'color',color_gridline)
%         p3 = plot([jjj*Dx (jjj-1)*Dx],iii*[Dx Dx],'k','linewidth',lwidth_grid);
%         set(p3,'color',color_gridline)
%     end
% end
%
% % Axis settings
% axis([-3*Dx (n+3)*Dx -3*Dx (n+3)*Dx])
% axis equal
% axis off
%
% % Write image to file
% eval(['print -dpng -r500 images/conductanceindicesH_',num2str(n),'grid.png'])
%
% % Horizontal neighbors
%
% % Create plot window
% figure(2)
% clf
%
% % Conductivities inside the grid (not at the boundary
% star_x = [-Dx/2 0    Dx/2  0   ];
% star_y = [ 0    Dx/2 0    -Dx/2];
% % Conductivities between vertical neighbors
% for rrr = 1:(n-1)
%     for ccc = 1:n
%         curr_cond_color = colorscale_cond_scaled(condmatV(rrr,ccc),condmax);
%         pc = patch((ccc-.5)*Dx+star_x,(n-rrr)*Dx+star_y,curr_cond_color);
%         set(pc,'edgecolor',curr_cond_color);
%         index = n*(n-1)+(ccc-1)*(n-1)+rrr;
%         text((ccc-.6)*Dx,(n-rrr+.2)*Dx,num2str(index),'fontsize',fsize);
%         hold on
%     end
% end
%
% % Plot grid
% p1 = plot([0 n],[0 0],'k','linewidth',lwidth_grid);
% set(p1,'color',color_gridline)
% hold on
% p2 = plot([0 0],[0 n],'k','linewidth',lwidth_grid);
% set(p2,'color',color_gridline)
% for iii = 1:n
%     for jjj = 1:n
%         p3 = plot(jjj*[Dx Dx],[(iii-1)*Dx iii*Dx],'k','linewidth',lwidth_grid);
%         set(p3,'color',color_gridline)
%         p3 = plot([jjj*Dx (jjj-1)*Dx],iii*[Dx Dx],'k','linewidth',lwidth_grid);
%         set(p3,'color',color_gridline)
%     end
% end
%
% % Axis settings
% axis([-3*Dx (n+3)*Dx -3*Dx (n+3)*Dx])
% axis equal
% axis off
%
% % Write image to file
% eval(['print -dpng -r500 images/conductanceindicesV_',num2str(n),'grid.png'])
