Statistiques
| Révision :

root / bin / Init / loadToy.m @ 1

Historique | Voir | Annoter | Télécharger (3,26 ko)

1 1 akiss
function [xy, es]=loadToy(pth,plotFlag,widthFactor)
2 1 akiss
% Loads a graph and plots it.
3 1 akiss
% pth is the path for the *.toy file.
4 1 akiss
% if plotFlag==true then the function also plots the graph (default=true)
5 1 akiss
% widthFactor is the factor to multiply the width for plooting (default=2)
6 1 akiss
% For simple usage, simply run
7 1 akiss
%   loadToy('25.toy')
8 1 akiss
% (for example, to plot the 25th simulation)
9 1 akiss
%
10 1 akiss
% returns:
11 1 akiss
%   xy   A matrix of the coordinates of the vertices. xy(1,i) is
12 1 akiss
%        the x coordinate of the i-th vertx, and xy(2,i) is its y coordinate.
13 1 akiss
%   es   A matrix describing the edges.  The i-th column of es corresponds
14 1 akiss
%        to the i-th edge. The entries of the column correspond to
15 1 akiss
%          1. The index of the first vertex in xy
16 1 akiss
%          2. The x offset of the 1st vertex (w.r.t the periodic unit cell)
17 1 akiss
%          3. The y offset of the 1st vertex (w.r.t the periodic unit cell)
18 1 akiss
%          4. The index of the second vertex in xy
19 1 akiss
%          5. The x offset of the 2nd vertex (w.r.t the periodic unit cell)
20 1 akiss
%          6. The y offset of the 2nd vertex (w.r.t the periodic unit cell)
21 1 akiss
%          7. The rest-length of the edge
22 1 akiss
%          8. The width of the edge
23 1 akiss
24 1 akiss
25 1 akiss
if nargin<3
26 1 akiss
    widthFactor=2;
27 1 akiss
end
28 1 akiss
if nargin<2
29 1 akiss
    plotFlag=true;
30 1 akiss
end
31 1 akiss
32 1 akiss
dat=csvread(pth);       % read file
33 1 akiss
W=dat(1);               % the width of the unit cell
34 1 akiss
H=dat(2);               % the height of the unit cell
35 1 akiss
nv=dat(3);              % number of vertices
36 1 akiss
ne=dat(4);              % number of edges
37 1 akiss
nc=dat(5);              % number od cells (not used)
38 1 akiss
dat=dat(6:end);         % crop
39 1 akiss
40 1 akiss
xy=dat(1:2*nv);
41 1 akiss
xy=reshape(xy,[2,nv]);
42 1 akiss
dat=dat(2*nv+1:end);    % crop again
43 1 akiss
44 1 akiss
es=reshape(dat(1:10*ne),[10,ne]);  % build the es matrix
45 1 akiss
es=es(3:end,:);                    % discard the first two rows
46 1 akiss
47 1 akiss
if ~plotFlag % quit if plotting is not needed.
48 1 akiss
    return
49 1 akiss
end
50 1 akiss
51 1 akiss
figure;
52 1 akiss
hold on;
53 1 akiss
plot(2*[-W W],[H H],'r')        % plot the unit cell
54 1 akiss
plot(2*[-W W],[0 0],'r')
55 1 akiss
plot([W W],2*[-H H],'r')
56 1 akiss
plot([0 0],2*[-H H],'r')
57 1 akiss
plot(xy(1,:),xy(2,:),'o')       % plot the points
58 1 akiss
plot(xy(1,:)+W,xy(2,:),'o')     % plot 8 more copies of each point
59 1 akiss
plot(xy(1,:),xy(2,:)+H,'o')
60 1 akiss
plot(xy(1,:)+W,xy(2,:)+H,'o')
61 1 akiss
plot(xy(1,:)-W,xy(2,:),'o')
62 1 akiss
plot(xy(1,:),xy(2,:)-H,'o')
63 1 akiss
plot(xy(1,:)-W,xy(2,:)-H,'o')
64 1 akiss
plot(xy(1,:)+W,xy(2,:)-H,'o')
65 1 akiss
plot(xy(1,:)-W,xy(2,:)+H,'o')
66 1 akiss
ylim([-H/2,3*H/2])              % set the limits of the figure
67 1 akiss
xlim([-W/2,3*W/2])
68 1 akiss
69 1 akiss
for i=1:ne
70 1 akiss
    e=es(:,i);              % choose the i-th edge
71 1 akiss
    x1=xy(1,e(1)+1)+W*e(2); % the x-coordinate 1st vertex (note the offset)
72 1 akiss
    y1=xy(2,e(1)+1)+H*e(3); % the y-coordinate 1st vertex (note the offset)
73 1 akiss
    x2=xy(1,e(4)+1)+W*e(5); % the y-coordinate 2nd vertex (note the offset)
74 1 akiss
    y2=xy(2,e(4)+1)+H*e(6); % the y-coordinate 2nd vertex (note the offset)
75 1 akiss
76 1 akiss
    plot([x1 x2],[y1 y2],'k','linewidth',widthFactor*e(8))      % plot the edge
77 1 akiss
    plot([x1 x2]+W,[y1 y2],'k','linewidth',widthFactor*e(8))    % plot 8 more copies
78 1 akiss
    plot([x1 x2]-W,[y1 y2],'k','linewidth',widthFactor*e(8))
79 1 akiss
    plot([x1 x2],[y1 y2]+H,'k','linewidth',widthFactor*e(8))
80 1 akiss
    plot([x1 x2],[y1 y2]-H,'k','linewidth',widthFactor*e(8))
81 1 akiss
    plot([x1 x2]+W,[y1 y2]+H,'k','linewidth',widthFactor*e(8))
82 1 akiss
    plot([x1 x2]-W,[y1 y2]-H,'k','linewidth',widthFactor*e(8))
83 1 akiss
    plot([x1 x2]-W,[y1 y2]+H,'k','linewidth',widthFactor*e(8))
84 1 akiss
    plot([x1 x2]+W,[y1 y2]-H,'k','linewidth',widthFactor*e(8))
85 1 akiss
end