%-------------------------------------------------------------------------%
% Part of array visualizer for CoF applications as part of my thesis 
% project titled 'High-density interconnect technology optimised for 
% flexible implants'.
% Full documentation can be found in my thesis report.
%
% Author:   T.B. Hosman
% E-mail:   timhosman@posteo.net
% Version:  1.1
% Created:  07/08/2019
% Modified: 22/08/2019
%-------------------------------------------------------------------------%

function [visualizerArrayScaled] = plot_visual(visualizerArray, ...
    PAD_SIZE, eePitch, scale)
%PLOTVISUAL This function is used in combination with the optimizer script,
% it plots a visualizerArray generated by the generate_visual function and
% scales to save calculation time if necessary (for very large arrays).
%   visualizerArray     = Unscaled array containing '1' for connections and
%                           '0' for opens.
%   PAD_SIZE            = dimensions of a single pad
%   eePitch             = edge-edge distance between pads
%   scale               = compression factor, used to downsize large arrays

%% Determine array size and initialize
totalOuterPads = sum(visualizerArray(:,1) == 1);
totalPadLength = totalOuterPads * PAD_SIZE;
totalPitchLength = (totalOuterPads - 1) * eePitch;
arraySize = round((totalPadLength + totalPitchLength) * scale,0);
visualizerArrayScaled = int8(zeros(arraySize));
pitchPadLength = round(scale * (PAD_SIZE + eePitch),0);

%% Fill array
scaledPad = ones(round(scale * PAD_SIZE,0));
unscaledSize = size(visualizerArray,1);
for i=1:unscaledSize
    for j=1:unscaledSize
        if (visualizerArray(i,j) == 1)
            xLoc = 1 + (i-1) / 2 * pitchPadLength;
            yLoc = 1 + (j-1) / 2 * pitchPadLength;
            visualizerArrayScaled( ...
                xLoc:xLoc + round(scale * PAD_SIZE - 1,0), ...
                yLoc:yLoc + round(scale * PAD_SIZE - 1,0)) ...
                = scaledPad;
        end
    end
end

%% Plot visuals
% credit to https://stackoverflow.com/questions/3280705/
% how-can-i-display-a-2d-binary-matrix-as-a-black-white-plot

TICK_SCALE = 2;

[r, c] = size(visualizerArrayScaled);            % Get the matrix size
imagesc((1:c)+0.5, (1:r)+0.5, visualizerArrayScaled); % Plot the image
colormap(gray);                            % Use a gray colormap
axis equal                                 % Make axes grid sizes equal
set(gca, 'XTick', 0:TICK_SCALE*pitchPadLength:(c+1),... % Change some axis properties
         'YTick', 0:TICK_SCALE*pitchPadLength:(r+1), ...
         'XTickLabel', 0:TICK_SCALE*pitchPadLength/scale:(c+1)/scale, ...
         'YTickLabel', 0:TICK_SCALE*pitchPadLength/scale:(r+1)/scale, ...
         'XLim', [1 c+1], ...
         'YLim', [1 r+1], ...
         'GridLineStyle', '-', ...
         'XGrid', 'on', ...
         'YGrid', 'on');
 xlabel('distance [um]');
 ylabel('distance [um]');

%end

end

