comparison toolboxes/graph_visualisation/graphViz4Matlab/layouts/Randlayout.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 classdef Randlayout < Abstractlayout
2 % A greedy, random layout - gives a different layout each time its called.
3 % Matthew Dunham
4 % University of British Columbia
5 % http://www.cs.ubc.ca/~mdunham/
6
7 properties
8 xmin; % The left most point on the graph axis in data units
9 xmax; % The right most point on the graph axis in data units
10 ymin; % The bottom most point on the graph axis in data units
11 ymax; % The top most point on the graph axis in data units
12 adjMatrix; % The adjacency matrix
13 maxNodeSize; % The maximum diameter of a node in data units
14 image; % An image for the button that will lanuch this layout
15 name; % A unique name for instances of this class
16 shortDescription; % A description for use in the tooltips
17 nodeSize; % The calculated node size, call dolayout() before accessing
18 centers; % The calculated node centers in an n-by-2 matrix
19 seed; % The seed used to generate the last layout.
20 end
21
22
23
24 methods
25
26 function obj = Randlayout(name,seed)
27 % constructor - seed is optional.
28 if(nargin < 1)
29 obj.name = 'Randlayout';
30 else
31 obj.name = name;
32 end
33 if(nargin < 2)
34 seed = 31;
35 end
36 obj.seed = seed;
37 load glicons
38 obj.image = icons.random;
39 obj.shortDescription = 'Random Greedy Layout';
40
41 end
42 end
43
44 methods(Access = 'protected')
45
46 function calcLayout(obj)
47 rand('twister',obj.seed); randn('state',obj.seed);
48 obj.seed = obj.seed + 1;
49 nnodes = size(obj.adjMatrix,1);
50 obj.nodeSize = min(obj.maxNodeSize,2*(obj.xmax-obj.xmin)/nnodes);
51 npoints = 25;
52 locdist = ones(npoints,npoints);
53 c = floor(0.4*npoints):ceil(0.6*npoints);
54 locdist(c,c) = 1.5; % slightly favour the center
55 locdist = locdist./sum(locdist(:)); % discrete distribution over locations
56 locations = zeros(nnodes,2); % holds indices into locdist
57 nsize = ceil(npoints*(obj.nodeSize/2)/(obj.xmax - obj.xmin+obj.nodeSize));
58 for i=1:nnodes
59 while(true)
60 [xcandidate,ycandidate] = obj.sample(locdist);
61 [valid,locdist] = obj.isfree(xcandidate,ycandidate,locdist,nsize);
62 if(valid)
63 locations(i,:) = [xcandidate,ycandidate];
64 locdist = obj.radiate(locdist,locations,npoints);
65 break;
66 end
67 end
68 end
69 nedges = sum(obj.adjMatrix,1) + sum(obj.adjMatrix,2)';
70 [val,perm] = sort(nedges,'descend');
71 locations = locations(perm,:);
72 obj.mapLocationsToAxes(locations,npoints);
73 end
74
75 function mapLocationsToAxes(obj,locations,npoints)
76 % Map sampled locations on a grid to actual points on the graph
77 % axes.
78 xndx = locations(:,1); yndx = locations(:,2);
79 xmin = obj.xmin + obj.nodeSize/2;
80 xmax = obj.xmax - obj.nodeSize/2;
81 ymin = obj.ymin + obj.nodeSize/2;
82 ymax = obj.ymax - obj.nodeSize/2;
83 xstep = (xmax - xmin) /npoints;
84 ystep = (ymax - ymin) /npoints;
85 xpos = xmin+xstep:xstep:xmax;
86 ypos = ymin+ystep:ystep:ymax;
87 obj.centers = [xpos(xndx)',ypos(yndx)'];
88 end
89
90 function [valid,locdist2] = isfree(obj,xcandidate,ycandidate,locdist,nsize)
91 % Check if the sampled location is valid. Regardless, zero out the
92 % spot so that we don't sample from here again.
93 xndx = max(1,xcandidate-nsize):min(xcandidate+nsize,size(locdist,2));
94 yndx = max(1,ycandidate-nsize):min(ycandidate+nsize,size(locdist,1));
95 nodeArea = locdist(xndx,yndx);
96 valid = all(nodeArea(:));
97 locdist2 = locdist;
98 locdist2(xndx,yndx) = 0;
99 locdist2 = locdist2 ./sum(locdist(:));
100 end
101
102 function locdist = radiate(obj,locdist,locations,npoints)
103 % Update the location probability distribution.
104 n = nnz(locations(:,1));
105 loczeros = locdist == 0;
106 for i=1:n
107 [x y] = meshgrid(1:npoints,1:npoints);
108 x = x(:); y = y(:);
109 dist = sqrt((x-locations(i,1)).^2 + (y-locations(i,2)).^2);
110 locdist = locdist + reshape(dist,npoints,npoints)';
111 end
112 locdist(loczeros) = 0;
113 locdist = locdist ./sum(locdist(:));
114 end
115
116 function [xndx,yndx] = sample(obj,dist)
117 % Sample a single value from the non-uniform discrete distribution.
118 if(isnan(dist(1)))
119 perm = randperm(size(dist,1));
120 xndx = perm(1);
121 yndx = perm(2);
122 return;
123 end
124 r = rand;
125 cumprob = cumsum(dist(:));
126 s = sum(r > cumprob(1:end-1)) + 1;
127 [xndx,yndx] = ind2sub(size(dist),s);
128 end
129
130 end
131
132
133
134 end