comparison toolboxes/graph_visualisation/graphViz4Matlab/layouts/Gvizlayout.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 Gvizlayout < Abstractlayout
2 % Use the graphVIZ package to determine the optimal layout for a graph.
3 %
4 % Matthew Dunham
5 % University of British Columbia
6 % http://www.cs.ubc.ca/~mdunham/
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 short descriptions used in tooltips.
17 nodeSize; % The calculated node size, call dolayout() before accessing
18 centers; % The calculated node centers in an n-by-2 matrix
19 end
20
21 properties(GetAccess = 'protected', SetAccess = 'protected')
22 layoutFile = 'layout.dot';
23 adjFile = 'adjmat.dot';
24 end
25
26 methods
27
28 function obj = Gvizlayout(name)
29 % constructor
30 if(nargin < 1)
31 obj.name = 'Gvizlayout';
32 else
33 obj.name = name;
34 end
35 load glicons;
36 obj.image = icons.energy;
37 obj.shortDescription = 'Minimum Energy Layout (GraphViz)';
38 end
39
40 function available = isavailable(obj)
41 % Make sure graphViz is available.
42 available = Gvizlayout.queryGviz('neato');
43 if(not(available))
44 fprintf('Please install or upgrade graphViz\n');
45 end
46 end
47
48
49 end
50
51
52 methods(Access = 'protected')
53
54 function calcLayout(obj)
55 % Have graphViz calculate the layout
56 obj.writeDOTfile();
57 obj.callGraphViz();
58 obj.readLayout();
59 obj.cleanup();
60 end
61
62 function writeDOTfile(obj)
63 % Write the adjacency matrix into a dot file that graphViz can
64 % understand.
65 fid = fopen('adjmat.dot','w');
66 fprintf(fid,'digraph G {\ncenter = 1;\nsize="10,10";\n');
67 n = size(obj.adjMatrix,1);
68 for i=1:n, fprintf(fid,'%d;\n',i); end
69 edgetxt = ' -> ';
70 for i=1:n
71 for j=1:n
72 if(obj.adjMatrix(i,j))
73 fprintf(fid,'%d%s%d;\n',i,edgetxt,j);
74 end
75 end
76 end
77 fprintf(fid,'}');
78 fclose(fid);
79 end
80
81 function callGraphViz(obj)
82 % Call GraphViz to determine an optimal layout. Write this layout in
83 % layout.dot for later parsing.
84 err = system(['neato -Tdot -Gmaxiter=5000 -Gstart=7 -o ',obj.layoutFile,' ',obj.adjFile]);
85 if(err),error('Sorry, unknown GraphViz failure, try another layout'); end
86 end
87
88 function readLayout(obj)
89 % Parse the layout.dot file for the graphViz node locations and
90 % dimensions.
91 fid = fopen(obj.layoutFile,'r');
92 text = textscan(fid,'%s','delimiter','\n');
93 fclose(fid);
94 text = text{:};
95 [start,dims] = strtok(text{cellfun(@(x)~isempty(x),strfind(text,'graph [bb="'))},'"');
96 dims = textscan(strtok(dims,'"'),'%n','delimiter',',');
97 dims = dims{:}';
98 text(cellfun(@(x)~isempty(x),strfind(text,' -> ')))=[]; % delete edge info, we don't need it
99 text(cellfun(@(x)isempty(x),strfind(text,'pos')))=[]; % keep only positions
100 [start,remaining] = strtok(text,'"');
101 [locations,remaining] = strtok(remaining,'"');
102 locations = cellfun(@(str)textscan(str,'%n','delimiter',','),locations,'UniformOutput',false);
103 locations = [locations{:}];
104 locations = [locations{:}]';
105 obj.scaleLocations(locations,dims);
106 end
107
108 function scaleLocations(obj,locations,graphVizDims)
109 % Scale the graphViz node locations to the smartgraph dimensions and
110 % set the node size.
111 dims = graphVizDims; loc = locations;
112 loc(:,1) = (loc(:,1)-dims(1)) ./ (dims(3)-dims(1))*(obj.xmax - obj.xmin) + obj.xmin;
113 loc(:,2) = (loc(:,2)-dims(2)) ./ (dims(4)-dims(2))*(obj.ymax - obj.ymin) + obj.ymin;
114 obj.centers = loc;
115
116 a = min(abs(loc(:,1) - obj.xmin));
117 b = min(abs(loc(:,1) - obj.xmax));
118 c = min(abs(loc(:,2) - obj.ymin));
119 d = min(abs(loc(:,2) - obj.ymax));
120 obj.nodeSize = min(2*min([a,b,c,d]),obj.maxNodeSize);
121
122 end
123
124 function cleanup(obj)
125 % delete the temporary files.
126 delete(obj.adjFile);
127 delete(obj.layoutFile);
128 end
129
130 end
131
132 methods(Access = 'protected',Static = true)
133
134 function available = queryGviz(name)
135 err = system([name,' -V']);
136 available = ~err;
137 end
138
139 end
140
141
142 end % end of graphVIZlayout class