comparison toolboxes/FullBNT-1.0.7/GraphViz/draw_graph.m @ 0:e9a9cd732c1e tip

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