annotate toolboxes/FullBNT-1.0.7/GraphViz/draw_graph.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [x, y, h] = draw_graph(adj, labels, node_t, x, y, varargin)
Daniel@0 2 % DRAW_LAYOUT Draws a layout for a graph
Daniel@0 3 %
Daniel@0 4 % [X, Y, H] = DRAW_LAYOUT(ADJ, <LABELS, ISBOX, X, Y>)
Daniel@0 5 %
Daniel@0 6 % Inputs :
Daniel@0 7 % ADJ : Adjacency matrix (source, sink)
Daniel@0 8 % LABELS : Cell array containing labels <Default : '1':'N'>
Daniel@0 9 % ISBOX : 1 if node is a box, 0 if oval <Default : zeros>
Daniel@0 10 % X, Y, : Coordinates of nodes on the unit square <Default : calls make_layout>
Daniel@0 11 %
Daniel@0 12 % Outputs :
Daniel@0 13 % X, Y : Coordinates of nodes on the unit square
Daniel@0 14 % H : Object handles
Daniel@0 15 %
Daniel@0 16 % Usage Example : [x, y] = draw_layout([0 1;0 0], {'Hidden','Visible'}, [1 0]');
Daniel@0 17 %
Daniel@0 18 % h(i,1) is the text handle - color
Daniel@0 19 % h(i,2) is the circle handle - facecolor
Daniel@0 20 %
Daniel@0 21 % See also MAKE_LAYOUT
Daniel@0 22
Daniel@0 23 % Change History :
Daniel@0 24 % Date Time Prog Note
Daniel@0 25 % 13-Apr-2000 9:06 PM ATC Created under MATLAB 5.3.1.29215a (R11.1)
Daniel@0 26 %
Daniel@0 27 % ATC = Ali Taylan Cemgil,
Daniel@0 28 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
Daniel@0 29 % e-mail : cemgil@mbfys.kun.nl
Daniel@0 30
Daniel@0 31 adj = double(adj);
Daniel@0 32 N = size(adj,1);
Daniel@0 33 if nargin<2,
Daniel@0 34 labels = cellstr(int2str((1:N)'));
Daniel@0 35 end
Daniel@0 36
Daniel@0 37 if nargin<3,
Daniel@0 38 node_t = zeros(N,1);
Daniel@0 39 else
Daniel@0 40 node_t = node_t(:);
Daniel@0 41 end;
Daniel@0 42
Daniel@0 43 axis([0 1 0 1]);
Daniel@0 44 set(gca,'XTick',[],'YTick',[],'box','on');
Daniel@0 45 % axis('square');
Daniel@0 46 %colormap(flipud(gray));
Daniel@0 47
Daniel@0 48 if nargin<4,
Daniel@0 49 [x y] = make_layout(adj);
Daniel@0 50 end;
Daniel@0 51
Daniel@0 52 idx1 = find(node_t==0); h1 = []; wd1=[];
Daniel@0 53 if ~isempty(idx1)
Daniel@0 54 [h1 wd1] = textoval(x(idx1), y(idx1), labels(idx1), varargin{:});
Daniel@0 55 end;
Daniel@0 56
Daniel@0 57 idx2 = find(node_t~=0); h2 = []; wd2 = [];
Daniel@0 58 if ~isempty(idx2)
Daniel@0 59 [h2 wd2] = textbox(x(idx2), y(idx2), labels(idx2), varargin{:});
Daniel@0 60 end;
Daniel@0 61
Daniel@0 62 wd = zeros(size(wd1,1)+size(wd2,1),2);
Daniel@0 63 if ~isempty(idx1), wd(idx1, :) = wd1; end;
Daniel@0 64 if ~isempty(idx2), wd(idx2, :) = wd2; end;
Daniel@0 65
Daniel@0 66 % bug: this code assumes [x y] is the center of each box and oval, which
Daniel@0 67 % isn't exactly true.
Daniel@0 68 h_edge = [];
Daniel@0 69 for i=1:N,
Daniel@0 70 j = find(adj(i,:)==1);
Daniel@0 71 for k=j,
Daniel@0 72 if x(k)-x(i)==0,
Daniel@0 73 sign = 1;
Daniel@0 74 if y(i)>y(k), alpha = -pi/2; else alpha = pi/2; end;
Daniel@0 75 else
Daniel@0 76 alpha = atan((y(k)-y(i))/(x(k)-x(i)));
Daniel@0 77 if x(i)<x(k), sign = 1; else sign = -1; end;
Daniel@0 78 end;
Daniel@0 79 dy1 = sign.*wd(i,2).*sin(alpha); dx1 = sign.*wd(i,1).*cos(alpha);
Daniel@0 80 dy2 = sign.*wd(k,2).*sin(alpha); dx2 = sign.*wd(k,1).*cos(alpha);
Daniel@0 81 if adj(k,i)==0, % if directed edge
Daniel@0 82 h = arrow([x(i)+dx1 y(i)+dy1],[x(k)-dx2 y(k)-dy2],'BaseAngle',30);
Daniel@0 83 else
Daniel@0 84 h = line([x(i)+dx1 x(k)-dx2],[y(i)+dy1 y(k)-dy2]);
Daniel@0 85 adj(k,i)=-1; % Prevent drawing lines twice
Daniel@0 86 end;
Daniel@0 87 h_edge = [h_edge h];
Daniel@0 88 end;
Daniel@0 89 end;
Daniel@0 90
Daniel@0 91 color.box = 'black';
Daniel@0 92 color.text = color.box;
Daniel@0 93 color.edge = [1 1 1]*3/4;
Daniel@0 94 %color.edge = 'green';
Daniel@0 95 if ~isempty(idx1)
Daniel@0 96 set(h1(:,1),'Color',color.text)
Daniel@0 97 set(h1(:,2),'EdgeColor',color.box)
Daniel@0 98 end
Daniel@0 99 if ~isempty(idx2)
Daniel@0 100 set(h2(:,1),'Color',color.text)
Daniel@0 101 set(h2(:,2),'EdgeColor',color.box)
Daniel@0 102 end
Daniel@0 103 set(h_edge,'Color',color.edge)
Daniel@0 104
Daniel@0 105 if nargout>2,
Daniel@0 106 h = zeros(length(wd),2);
Daniel@0 107 if ~isempty(idx1),
Daniel@0 108 h(idx1,:) = h1;
Daniel@0 109 end;
Daniel@0 110 if ~isempty(idx2),
Daniel@0 111 h(idx2,:) = h2;
Daniel@0 112 end;
Daniel@0 113 end;
Daniel@0 114
Daniel@0 115 %%%%%
Daniel@0 116
Daniel@0 117 function [t, wd] = textoval(x, y, str, varargin)
Daniel@0 118 % TEXTOVAL Draws an oval around text objects
Daniel@0 119 %
Daniel@0 120 % [T, WIDTH] = TEXTOVAL(X, Y, STR)
Daniel@0 121 % [..] = TEXTOVAL(STR) % Interactive
Daniel@0 122 %
Daniel@0 123 % Inputs :
Daniel@0 124 % X, Y : Coordinates
Daniel@0 125 % TXT : Strings
Daniel@0 126 %
Daniel@0 127 % Outputs :
Daniel@0 128 % T : Object Handles
Daniel@0 129 % WIDTH : x and y Width of ovals
Daniel@0 130 %
Daniel@0 131 % Usage Example : [t] = textoval('Visit to Asia?');
Daniel@0 132 %
Daniel@0 133 %
Daniel@0 134 % Note :
Daniel@0 135 % See also TEXTBOX
Daniel@0 136
Daniel@0 137 % Uses :
Daniel@0 138
Daniel@0 139 % Change History :
Daniel@0 140 % Date Time Prog Note
Daniel@0 141 % 15-Jun-1998 10:36 AM ATC Created under MATLAB 5.1.0.421
Daniel@0 142 % 12-Mar-2004 10:00 AM minka Changed placement/sizing.
Daniel@0 143 %
Daniel@0 144 % ATC = Ali Taylan Cemgil,
Daniel@0 145 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
Daniel@0 146 % e-mail : cemgil@mbfys.kun.nl
Daniel@0 147
Daniel@0 148 temp = [];
Daniel@0 149 textProperties = {'BackgroundColor','Color','FontAngle','FontName','FontSize','FontUnits','FontWeight','Rotation'};
Daniel@0 150 varargin = argfilter(varargin,textProperties);
Daniel@0 151
Daniel@0 152 if nargin == 1
Daniel@0 153 str = x;
Daniel@0 154 end
Daniel@0 155 if ~isa(str,'cell') str=cellstr(str); end;
Daniel@0 156 N = length(str);
Daniel@0 157 wd = zeros(N,2);
Daniel@0 158 for i=1:N,
Daniel@0 159 if nargin == 1
Daniel@0 160 [x, y] = ginput(1);
Daniel@0 161 end
Daniel@0 162 tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
Daniel@0 163 % minka
Daniel@0 164 [ptc wx wy] = draw_oval(tx);
Daniel@0 165 wd(i,:) = [wx wy];
Daniel@0 166 % draw_oval will paint over the text, so need to redraw it
Daniel@0 167 delete(tx);
Daniel@0 168 tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
Daniel@0 169 temp = [temp; tx ptc];
Daniel@0 170 end
Daniel@0 171 if nargout>0, t = temp; end;
Daniel@0 172
Daniel@0 173 %%%%%%%%%
Daniel@0 174
Daniel@0 175
Daniel@0 176 function [ptc, wx, wy] = draw_oval(tx, x, y)
Daniel@0 177 % Draws an oval box around a tex object
Daniel@0 178 sz = get(tx,'Extent');
Daniel@0 179 % minka
Daniel@0 180 wy = 2/3*sz(4);
Daniel@0 181 wx = 2/3*sz(3);
Daniel@0 182 x = sz(1)+sz(3)/2;
Daniel@0 183 y = sz(2)+sz(4)/2;
Daniel@0 184 ptc = ellipse(x, y, wx, wy);
Daniel@0 185 set(ptc, 'FaceColor','w');
Daniel@0 186
Daniel@0 187
Daniel@0 188 %%%%%%%%%%%%%
Daniel@0 189
Daniel@0 190 function [p] = ellipse(x, y, rx, ry, c)
Daniel@0 191 % ELLIPSE Draws Ellipse shaped patch objects
Daniel@0 192 %
Daniel@0 193 % [<P>] = ELLIPSE(X, Y, Rx, Ry, C)
Daniel@0 194 %
Daniel@0 195 % Inputs :
Daniel@0 196 % X : N x 1 vector of x coordinates
Daniel@0 197 % Y : N x 1 vector of y coordinates
Daniel@0 198 % Rx, Ry : Radii
Daniel@0 199 % C : Color index
Daniel@0 200 %
Daniel@0 201 %
Daniel@0 202 % Outputs :
Daniel@0 203 % P = Handles of Ellipse shaped path objects
Daniel@0 204 %
Daniel@0 205 % Usage Example : [] = ellipse();
Daniel@0 206 %
Daniel@0 207 %
Daniel@0 208 % Note :
Daniel@0 209 % See also
Daniel@0 210
Daniel@0 211 % Uses :
Daniel@0 212
Daniel@0 213 % Change History :
Daniel@0 214 % Date Time Prog Note
Daniel@0 215 % 27-May-1998 9:55 AM ATC Created under MATLAB 5.1.0.421
Daniel@0 216
Daniel@0 217 % ATC = Ali Taylan Cemgil,
Daniel@0 218 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
Daniel@0 219 % e-mail : cemgil@mbfys.kun.nl
Daniel@0 220
Daniel@0 221 if (nargin < 2) error('Usage Example : e = ellipse([0 1],[0 -1],[1 0.5],[2 0.5]); '); end;
Daniel@0 222 if (nargin < 3) rx = 0.1; end;
Daniel@0 223 if (nargin < 4) ry = rx; end;
Daniel@0 224 if (nargin < 5) c = 1; end;
Daniel@0 225
Daniel@0 226 if length(c)==1, c = ones(size(x)).*c; end;
Daniel@0 227 if length(rx)==1, rx = ones(size(x)).*rx; end;
Daniel@0 228 if length(ry)==1, ry = ones(size(x)).*ry; end;
Daniel@0 229
Daniel@0 230 n = length(x);
Daniel@0 231 p = zeros(size(x));
Daniel@0 232 t = 0:pi/30:2*pi;
Daniel@0 233 for i=1:n,
Daniel@0 234 px = rx(i)*cos(t)+x(i);
Daniel@0 235 py = ry(i)*sin(t)+y(i);
Daniel@0 236 p(i) = patch(px,py,c(i));
Daniel@0 237 end;
Daniel@0 238
Daniel@0 239 if nargout>0, pp = p; end;
Daniel@0 240
Daniel@0 241 %%%%%
Daniel@0 242
Daniel@0 243 function [t, wd] = textbox(x,y,str,varargin)
Daniel@0 244 % TEXTBOX Draws A Box around the text
Daniel@0 245 %
Daniel@0 246 % [T, WIDTH] = TEXTBOX(X, Y, STR)
Daniel@0 247 % [..] = TEXTBOX(STR)
Daniel@0 248 %
Daniel@0 249 % Inputs :
Daniel@0 250 % X, Y : Coordinates
Daniel@0 251 % TXT : Strings
Daniel@0 252 %
Daniel@0 253 % Outputs :
Daniel@0 254 % T : Object Handles
Daniel@0 255 % WIDTH : x and y Width of boxes
Daniel@0 256 %%
Daniel@0 257 % Usage Example : t = textbox({'Ali','Veli','49','50'});
Daniel@0 258 %
Daniel@0 259 %
Daniel@0 260 % Note :
Daniel@0 261 % See also TEXTOVAL
Daniel@0 262
Daniel@0 263 % Uses :
Daniel@0 264
Daniel@0 265 % Change History :
Daniel@0 266 % Date Time Prog Note
Daniel@0 267 % 09-Jun-1998 11:43 AM ATC Created under MATLAB 5.1.0.421
Daniel@0 268 % 12-Mar-2004 10:00 AM minka Changed placement/sizing.
Daniel@0 269 %
Daniel@0 270 % ATC = Ali Taylan Cemgil,
Daniel@0 271 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
Daniel@0 272 % e-mail : cemgil@mbfys.kun.nl
Daniel@0 273
Daniel@0 274 temp = [];
Daniel@0 275 textProperties = {'BackgroundColor','Color','FontAngle','FontName','FontSize','FontUnits','FontWeight','Rotation'};
Daniel@0 276 varargin = argfilter(varargin,textProperties);
Daniel@0 277
Daniel@0 278 if nargin == 1
Daniel@0 279 str = x;
Daniel@0 280 end
Daniel@0 281 if ~isa(str,'cell') str=cellstr(str); end;
Daniel@0 282 N = length(str);
Daniel@0 283 wd = zeros(N,2);
Daniel@0 284 for i=1:N,
Daniel@0 285 if nargin == 1
Daniel@0 286 [x, y] = ginput(1);
Daniel@0 287 end
Daniel@0 288 tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
Daniel@0 289 % minka
Daniel@0 290 [ptc wx wy] = draw_box(tx);
Daniel@0 291 wd(i,:) = [wx wy];
Daniel@0 292 % draw_box will paint over the text, so need to redraw it
Daniel@0 293 delete(tx);
Daniel@0 294 tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
Daniel@0 295 temp = [temp; tx ptc];
Daniel@0 296 end;
Daniel@0 297
Daniel@0 298 if nargout>0, t = temp; end;
Daniel@0 299
Daniel@0 300
Daniel@0 301 function [ptc, wx, wy] = draw_box(tx)
Daniel@0 302 % Draws a box around a text object
Daniel@0 303 sz = get(tx,'Extent');
Daniel@0 304 % minka
Daniel@0 305 wy = 1/2*sz(4);
Daniel@0 306 wx = 1/2*sz(3);
Daniel@0 307 x = sz(1)+sz(3)/2;
Daniel@0 308 y = sz(2)+sz(4)/2;
Daniel@0 309 ptc = patch([x-wx x+wx x+wx x-wx], [y+wy y+wy y-wy y-wy],'w');
Daniel@0 310 set(ptc, 'FaceColor','w');
Daniel@0 311
Daniel@0 312
Daniel@0 313
Daniel@0 314 function args = argfilter(args,keep)
Daniel@0 315 %ARGFILTER Remove unwanted arguments.
Daniel@0 316 % ARGFILTER(ARGS,KEEP), where ARGS = {'arg1',value1,'arg2',value2,...},
Daniel@0 317 % returns a new argument list where only the arguments named in KEEP are
Daniel@0 318 % retained. KEEP is a character array or cell array of strings.
Daniel@0 319
Daniel@0 320 % Written by Tom Minka
Daniel@0 321
Daniel@0 322 if ischar(keep)
Daniel@0 323 keep = cellstr(keep);
Daniel@0 324 end
Daniel@0 325 i = 1;
Daniel@0 326 while i < length(args)
Daniel@0 327 if ~ismember(args{i},keep)
Daniel@0 328 args = args(setdiff(1:length(args),[i i+1]));
Daniel@0 329 else
Daniel@0 330 i = i + 2;
Daniel@0 331 end
Daniel@0 332 end