% Draw the result of a finite difference demo for electrical impedance
% tomography. This routine is for 2D demos
%
% Inputs:
% potmat       matrix of electric voltage potentials, NxN matrix
% fignum       figure window number
% bdata        voltage at the electrodes, vector of length 4*(N-1)
% plot_x       value 1 means plotting variable names instead of values
% show_inside  if zero, then plot voltage and/or current only at boundary
% font_size    [optional] font size
% digitn       [optional] how many digits to show in the potential values
%
% Samuli Siltanen March 2025

function EIT_FD_demo2D_text_plot(potmat,fignum,bdata,plot_x,show_inside,font_size,digitn)

% Record and check the size of the solution matrix
[row,col] = size(potmat);
if min(row,col)<2
    error('Potential matrix should not be a vector')
end
if ~(row==col)
    error('Potential matrix should be square')
end
N = row;

% Record number of electrodes
Ne = 4*(N-1);

% Check voltage potential vector sizes
if min(size(bdata))>1
    error('Boundary data bdata should be a vector')
end
if ~(length(bdata)==Ne)
    error('Boundary data bdata_W has wrong size')
end

% Set geometrical parameters
geometry_set;
if nargin>5 % If user defined fontsize in the optional argument
    fsize = font_size;
end
if nargin>6 % If user defined digit accuracy in the optional argument
    formatstr = ['%0.',num2str(digitn),'f'];
else 
    formatstr = '%0.1f';
end


% Create plot window
figure(fignum)
clf
hold on

% If the user wants, show variable names
if plot_x
    % Voltage variables at electrodes, denoted by v
    for b_ind = 1:Ne
        pix_text = ['v_{',num2str(b_ind),'}'];
        [row,col,side] = bind2gind(N,b_ind);
        switch side
            case 'E'
                if row==1 % Corner
                    text((N+.3)*Dx,(N-row+1.5)*Dx,pix_text,'fontsize',fsize)
                    hold on
                else % Not corner
                    text((N+.3)*Dx,(N-row+.5)*Dx,pix_text,'fontsize',fsize)
                    hold on
                end
            case 'N'
                if col==1 % Corner
                    text((col-1.7)*Dx,(N+.5)*Dx,pix_text,'fontsize',fsize)
                else % Not corner
                    text((col-.7)*Dx,(N+.5)*Dx,pix_text,'fontsize',fsize)
                end
            case 'W'
                if row==N % Corner
                    text((-.7)*Dx,(N-row-.5)*Dx,pix_text,'fontsize',fsize)
                else % Not corner
                    text((-.7)*Dx,(N-row+.5)*Dx,pix_text,'fontsize',fsize)
                end
            case 'S'
                if col==N % Corner
                    text((col-.7+1)*Dx,(-.5)*Dx,pix_text,'fontsize',fsize)
                else % not corner
                    text((col-.7)*Dx,(-.5)*Dx,pix_text,'fontsize',fsize)
                end
        end
        if show_inside % Voltage variables in the grid, denoted by u
            for rrr = 1:N
                for ccc = 1:N
                    g_ind = sub2ind([N N],rrr,ccc);
                    pix_text = ['u_{',num2str(g_ind),'}'];
                    text((ccc-.7)*Dx,(N-rrr+.5)*Dx,pix_text,'fontsize',fsize)
                end
            end
        end
    end
else % If not variable names, then voltage potential values
    % Voltages at electrodes
    for b_ind = 1:Ne
        pix_text = num2str(bdata(b_ind),formatstr);
        [row,col,side] = bind2gind(N,b_ind);
        switch side
            case 'E'
                if row==1 % Corner
                    text((N+.3)*Dx,(N-row+1.5)*Dx,pix_text,'fontsize',fsize)
                    hold on
                else % Not corner
                    text((N+.3)*Dx,(N-row+.5)*Dx,pix_text,'fontsize',fsize)
                    hold on
                end
            case 'N'
                if col==1 % Corner
                    text((col-1.8)*Dx,(N+.5)*Dx,pix_text,'fontsize',fsize)
                else % Not corner
                    text((col-.8)*Dx,(N+.5)*Dx,pix_text,'fontsize',fsize)
                end
            case 'W'
                if row==N % Corner
                    text((-.8)*Dx,(N-row-.5)*Dx,pix_text,'fontsize',fsize)
                else % Not corner
                    text((-.8)*Dx,(N-row+.5)*Dx,pix_text,'fontsize',fsize)
                end
            case 'S'
                if col==N % Corner
                    text((col-.8+1)*Dx,(-.5)*Dx,pix_text,'fontsize',fsize)
                else % not corner
                    text((col-.8)*Dx,(-.5)*Dx,pix_text,'fontsize',fsize)
                end
        end
        if show_inside % Voltages in the grid
            for rrr = 1:N
                for ccc = 1:N
                    pix_text = num2str(potmat(rrr,ccc),formatstr);
                    text((ccc-.8)*Dx,(N-rrr+.5)*Dx,pix_text,'fontsize',fsize)
                end
            end
        end
    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([-2*Dx (N+2)*Dx -2*Dx (N+2)*Dx])
axis equal
axis off