Daniel@0: classdef Gridlayout < Abstractlayout Daniel@0: % Provides a simple, (naive) grid layout for Graphlayout. Daniel@0: % An uninitialized instance can be created to pass to the GraphLayout Daniel@0: % constructor by just calling Gridlayout without any parameters, e.g. Daniel@0: % Graphlayout('-adjMat',[0 1; 0 0], '-layout',Gridlayout); Daniel@0: % Daniel@0: % Matthew Dunham Daniel@0: % University of British Columbia Daniel@0: % http://www.cs.ubc.ca/~mdunham/ Daniel@0: Daniel@0: properties Daniel@0: xmin; % The left most point on the graph axis in data units Daniel@0: xmax; % The right most point on the graph axis in data units Daniel@0: ymin; % The bottom most point on the graph axis in data units Daniel@0: ymax; % The top most point on the graph axis in data units Daniel@0: adjMatrix; % The adjacency matrix Daniel@0: maxNodeSize; % The maximum diameter of a node in data units Daniel@0: image; % An image for the button that will lanuch this layout Daniel@0: name; % A unique name for instances of this class Daniel@0: shortDescription; % A description for use in the tooltips Daniel@0: nodeSize; % The calculated node size, call dolayout() before accessing Daniel@0: centers; % The calculated node centers in an n-by-2 matrix Daniel@0: end Daniel@0: Daniel@0: methods Daniel@0: function obj = Gridlayout(name) Daniel@0: Daniel@0: % constructor Daniel@0: if(nargin < 1) Daniel@0: obj.name = 'Gridlayout'; Daniel@0: else Daniel@0: obj.name = name; Daniel@0: end Daniel@0: load glicons; Daniel@0: obj.image = icons.grid; Daniel@0: obj.shortDescription = 'Grid Layout'; Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: Daniel@0: Daniel@0: methods(Access = 'protected') Daniel@0: Daniel@0: function calcLayout(obj) Daniel@0: Daniel@0: nnodes = size(obj.adjMatrix,1); Daniel@0: obj.centers = zeros(nnodes,2); Daniel@0: xspacePerNode = (obj.xmax - obj.xmin)/ceil(sqrt(nnodes)); Daniel@0: yspacePerNode = (obj.ymax - obj.ymin)/ceil(sqrt(nnodes)); Daniel@0: obj.nodeSize = min(min([xspacePerNode,yspacePerNode]./2),obj.maxNodeSize); Daniel@0: xstart = obj.xmin + (xspacePerNode)/2; Daniel@0: ystart = obj.ymin + (yspacePerNode)/2; Daniel@0: counter = 1; Daniel@0: for ypos=1:ceil(sqrt(nnodes)) Daniel@0: if(counter > nnodes),break,end Daniel@0: for xpos=1:ceil(sqrt(nnodes)) Daniel@0: obj.centers(counter,1) = xstart + (xpos-1)*xspacePerNode; Daniel@0: obj.centers(counter,2) = ystart + (ypos-1)*yspacePerNode; Daniel@0: positions(ypos,xpos) = counter; %#ok Daniel@0: counter = counter + 1; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %swap out the center node for the node with the most edges, Daniel@0: %(total of in and out). Daniel@0: edgeCounts = sum(obj.adjMatrix,1)' + sum(obj.adjMatrix,2); Daniel@0: [vals ndx] = sort(edgeCounts,'descend'); Daniel@0: c = positions(ceil(size(positions,1)/2),ceil(size(positions,2)/2)); Daniel@0: if(c ~= 0) Daniel@0: store = obj.centers(c,:); Daniel@0: obj.centers(c,:) = obj.centers(ndx(1),:); Daniel@0: obj.centers(ndx(1),:) = store; Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: Daniel@0: Daniel@0: end Daniel@0: Daniel@0: end