function [xy, es]=loadToy(pth,plotFlag,widthFactor)
% Loads a graph and plots it. 
% pth is the path for the *.toy file.
% if plotFlag==true then the function also plots the graph (default=true)
% widthFactor is the factor to multiply the width for plooting (default=2)
% For simple usage, simply run
%   loadToy('25.toy')
% (for example, to plot the 25th simulation)
%
% returns: 
%   xy   A matrix of the coordinates of the vertices. xy(1,i) is
%        the x coordinate of the i-th vertx, and xy(2,i) is its y coordinate.
%   es   A matrix describing the edges.  The i-th column of es corresponds
%        to the i-th edge. The entries of the column correspond to
%          1. The index of the first vertex in xy
%          2. The x offset of the 1st vertex (w.r.t the periodic unit cell)
%          3. The y offset of the 1st vertex (w.r.t the periodic unit cell)
%          4. The index of the second vertex in xy
%          5. The x offset of the 2nd vertex (w.r.t the periodic unit cell)
%          6. The y offset of the 2nd vertex (w.r.t the periodic unit cell)
%          7. The rest-length of the edge
%          8. The width of the edge


if nargin<3
    widthFactor=2;
end
if nargin<2
    plotFlag=true;
end

dat=csvread(pth);       % read file
W=dat(1);               % the width of the unit cell
H=dat(2);               % the height of the unit cell
nv=dat(3);              % number of vertices
ne=dat(4);              % number of edges
nc=dat(5);              % number od cells (not used)
dat=dat(6:end);         % crop 

xy=dat(1:2*nv);         
xy=reshape(xy,[2,nv]);
dat=dat(2*nv+1:end);    % crop again

es=reshape(dat(1:10*ne),[10,ne]);  % build the es matrix 
es=es(3:end,:);                    % discard the first two rows

if ~plotFlag % quit if plotting is not needed.
    return
end

figure;
hold on;
plot(2*[-W W],[H H],'r')        % plot the unit cell
plot(2*[-W W],[0 0],'r')
plot([W W],2*[-H H],'r')
plot([0 0],2*[-H H],'r')
plot(xy(1,:),xy(2,:),'o')       % plot the points
plot(xy(1,:)+W,xy(2,:),'o')     % plot 8 more copies of each point
plot(xy(1,:),xy(2,:)+H,'o')
plot(xy(1,:)+W,xy(2,:)+H,'o')
plot(xy(1,:)-W,xy(2,:),'o')
plot(xy(1,:),xy(2,:)-H,'o')
plot(xy(1,:)-W,xy(2,:)-H,'o')
plot(xy(1,:)+W,xy(2,:)-H,'o')
plot(xy(1,:)-W,xy(2,:)+H,'o')
ylim([-H/2,3*H/2])              % set the limits of the figure
xlim([-W/2,3*W/2])

for i=1:ne
    e=es(:,i);              % choose the i-th edge
    x1=xy(1,e(1)+1)+W*e(2); % the x-coordinate 1st vertex (note the offset)
    y1=xy(2,e(1)+1)+H*e(3); % the y-coordinate 1st vertex (note the offset)
    x2=xy(1,e(4)+1)+W*e(5); % the y-coordinate 2nd vertex (note the offset)
    y2=xy(2,e(4)+1)+H*e(6); % the y-coordinate 2nd vertex (note the offset)
    
    plot([x1 x2],[y1 y2],'k','linewidth',widthFactor*e(8))      % plot the edge
    plot([x1 x2]+W,[y1 y2],'k','linewidth',widthFactor*e(8))    % plot 8 more copies
    plot([x1 x2]-W,[y1 y2],'k','linewidth',widthFactor*e(8))
    plot([x1 x2],[y1 y2]+H,'k','linewidth',widthFactor*e(8))
    plot([x1 x2],[y1 y2]-H,'k','linewidth',widthFactor*e(8))
    plot([x1 x2]+W,[y1 y2]+H,'k','linewidth',widthFactor*e(8))
    plot([x1 x2]-W,[y1 y2]-H,'k','linewidth',widthFactor*e(8))
    plot([x1 x2]-W,[y1 y2]+H,'k','linewidth',widthFactor*e(8))
    plot([x1 x2]+W,[y1 y2]-H,'k','linewidth',widthFactor*e(8))
end