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