function varargout = plotSID2D(sid, figno, tt)
% plotSID2D       Plot 2D Shift Invariant (Flexible Structure) Dictionary 
%
% use:   plotSID2D(sid);
%        h = plotSID2D(sid, figno);
%        h = plotSID2D(sid, tt, figno);
% -------------------------------------------------------------------------
%   sid    The 2D SI-FSD, a struct as in initSID2D
%   tt     text added to title, i.e. above figure (may be omitted)
%   figno  Figure number to make plot in, if omitted a new figure is made.
% -------------------------------------------------------------------------
% ex:   plotSID2D(sid, 1);

%--------------------------------------------------------------------------
% Made by Karl Skretting, University of Stavanger (UiS), 
% Department of Electrical Engineering and Computer Science (IDE)
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.uis.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  28.06.2015  KS: made file 
%--------------------------------------------------------------------------

if ~isstruct(sid) || ~isfield(sid,'Type') || ~strcmp(sid.Type, 'SID2D')
    disp([mfilename,': First argument not a 2D SI-FSD structure.']);
    h = [];
    
else
    if (nargin < 3); tt = ''; end;
    if (nargin < 2); figno = []; end;
    if ischar(figno) && (isnumeric(tt) || isempty(tt))     % swap figno and tt
        a3 = tt;
        tt = figno;
        figno = a3;
    end
    if isempty(figno) || (figno <= 0)
        figno = figure;
    else
        figure(figno);
    end
    clf;
    
    % vMin = min(sid.freeVar);
    vMax = max(sid.freeVar);
    % val = uint8(floor((sid.freeVar-vMin)*255.99/(vMax-vMin)));
    val = sid.freeVar;
    qvMax = max(sid.Qv);    
    qhMax = max(sid.Qh);    
    Nv = qvMax+4;
    Nh = qhMax+4;
    M = sid.M;
    if M <= 9
        Mv = ceil(M/3);
        Mh = 3;
        if (M==4); Mv = 2; Mh = 2; end;
        if (M==8); Mv = 2; Mh = 4; end;
    elseif M <= 16
        Mv = ceil(M/4);
        Mh = 4;
        if (M==15); Mv = 3; Mh = 5; end;
    else
        Mv = ceil(M/6);
        Mh = 6;
    end
    % disp('Nv, Mv, Nh, Mh, Nv*Mv, Nh*Mh');
    % disp([Nv,Mv,Nh,Mh,Nv*Mv,Nh*Mh]);
    A = vMax*ones(Nv*Mv,Nh*Mh);   % uint8(255*ones(Nv*Mv,Nh*Mh));
    for m=1:M
        mv = floor((m-1)/Mh);
        mh = rem(m-1,Mh);
        nv = (1:sid.Qv(m)) + (2 + mv*Nv + floor((qvMax-sid.Qv(m))/2));
        nh = (1:sid.Qh(m)) + (2 + mh*Nh + floor((qhMax-sid.Qh(m))/2));
        % disp(' '); disp([m, mv, mh]); disp(nv); disp(nh);
        A(nv,nh) = reshape(val(sid.i1(m):sid.i2(m)),sid.Qv(m),sid.Qh(m));
    end
    imagesc(A); colormap(gray(256)); 
    colorbar; axis equal; axis off;
    if ischar(tt) && ~isempty(tt)
        title(['Atoms in 2D SI-FSD,  M = ',int2str(sid.M),'. ',tt]);
    else
        title(['Atoms in 2D SI-FSD,  M = ',int2str(sid.M)]);
    end
    for m = 1:M
        mv = floor((m-1)/Mh)+1;
        mh = rem(m-1,Mh)+1;
        text(mh*Nh-((Nh-1)/2), mv*Nv-0.5, int2str(m), 'HorizontalAlignment','center');
    end
    % locate figure in upper right part of screen
    a = get(0,'ScreenSize'); sw = a(3); sh = a(4);
    w = 200 + ceil(500*size(A,2)/size(A,1));  
    h = 600; 
    set(gcf, 'Position',[sw-w-10,sh-h-70-20*min(figno,12),w,h]);
    % saveas(gcf, ['plotSIDfig',int2str(figno),'.png'], 'png');
end
    
if nargout >= 1
    varargout{1} = h;
end

end

