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