annotate toolboxes/graph_visualisation/graphViz4Matlab/layouts/Gridlayout.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 classdef Gridlayout < Abstractlayout
wolffd@0 2 % Provides a simple, (naive) grid layout for Graphlayout.
wolffd@0 3 % An uninitialized instance can be created to pass to the GraphLayout
wolffd@0 4 % constructor by just calling Gridlayout without any parameters, e.g.
wolffd@0 5 % Graphlayout('-adjMat',[0 1; 0 0], '-layout',Gridlayout);
wolffd@0 6 %
wolffd@0 7 % Matthew Dunham
wolffd@0 8 % University of British Columbia
wolffd@0 9 % http://www.cs.ubc.ca/~mdunham/
wolffd@0 10
wolffd@0 11 properties
wolffd@0 12 xmin; % The left most point on the graph axis in data units
wolffd@0 13 xmax; % The right most point on the graph axis in data units
wolffd@0 14 ymin; % The bottom most point on the graph axis in data units
wolffd@0 15 ymax; % The top most point on the graph axis in data units
wolffd@0 16 adjMatrix; % The adjacency matrix
wolffd@0 17 maxNodeSize; % The maximum diameter of a node in data units
wolffd@0 18 image; % An image for the button that will lanuch this layout
wolffd@0 19 name; % A unique name for instances of this class
wolffd@0 20 shortDescription; % A description for use in the tooltips
wolffd@0 21 nodeSize; % The calculated node size, call dolayout() before accessing
wolffd@0 22 centers; % The calculated node centers in an n-by-2 matrix
wolffd@0 23 end
wolffd@0 24
wolffd@0 25 methods
wolffd@0 26 function obj = Gridlayout(name)
wolffd@0 27
wolffd@0 28 % constructor
wolffd@0 29 if(nargin < 1)
wolffd@0 30 obj.name = 'Gridlayout';
wolffd@0 31 else
wolffd@0 32 obj.name = name;
wolffd@0 33 end
wolffd@0 34 load glicons;
wolffd@0 35 obj.image = icons.grid;
wolffd@0 36 obj.shortDescription = 'Grid Layout';
wolffd@0 37 end
wolffd@0 38
wolffd@0 39 end
wolffd@0 40
wolffd@0 41
wolffd@0 42 methods(Access = 'protected')
wolffd@0 43
wolffd@0 44 function calcLayout(obj)
wolffd@0 45
wolffd@0 46 nnodes = size(obj.adjMatrix,1);
wolffd@0 47 obj.centers = zeros(nnodes,2);
wolffd@0 48 xspacePerNode = (obj.xmax - obj.xmin)/ceil(sqrt(nnodes));
wolffd@0 49 yspacePerNode = (obj.ymax - obj.ymin)/ceil(sqrt(nnodes));
wolffd@0 50 obj.nodeSize = min(min([xspacePerNode,yspacePerNode]./2),obj.maxNodeSize);
wolffd@0 51 xstart = obj.xmin + (xspacePerNode)/2;
wolffd@0 52 ystart = obj.ymin + (yspacePerNode)/2;
wolffd@0 53 counter = 1;
wolffd@0 54 for ypos=1:ceil(sqrt(nnodes))
wolffd@0 55 if(counter > nnodes),break,end
wolffd@0 56 for xpos=1:ceil(sqrt(nnodes))
wolffd@0 57 obj.centers(counter,1) = xstart + (xpos-1)*xspacePerNode;
wolffd@0 58 obj.centers(counter,2) = ystart + (ypos-1)*yspacePerNode;
wolffd@0 59 positions(ypos,xpos) = counter; %#ok
wolffd@0 60 counter = counter + 1;
wolffd@0 61 end
wolffd@0 62 end
wolffd@0 63
wolffd@0 64 %swap out the center node for the node with the most edges,
wolffd@0 65 %(total of in and out).
wolffd@0 66 edgeCounts = sum(obj.adjMatrix,1)' + sum(obj.adjMatrix,2);
wolffd@0 67 [vals ndx] = sort(edgeCounts,'descend');
wolffd@0 68 c = positions(ceil(size(positions,1)/2),ceil(size(positions,2)/2));
wolffd@0 69 if(c ~= 0)
wolffd@0 70 store = obj.centers(c,:);
wolffd@0 71 obj.centers(c,:) = obj.centers(ndx(1),:);
wolffd@0 72 obj.centers(ndx(1),:) = store;
wolffd@0 73 end
wolffd@0 74
wolffd@0 75 end
wolffd@0 76
wolffd@0 77
wolffd@0 78 end
wolffd@0 79
wolffd@0 80 end