Search in sources :

Example 1 with Graph

use of csapps.layout.algorithms.hierarchicalLayout.Graph in project cytoscape-impl by cytoscape.

the class CircularLayoutAlgorithmTask method layoutPartition.

@Override
public void layoutPartition(LayoutPartition partition) {
    if (cancelled)
        return;
    final int numNodes = partition.nodeCount();
    if (numNodes == 1) {
        // We were asked to do a circular layout of a single node -- done!
        return;
    }
    nodeViews = new HashMap<Integer, View<CyNode>>(numNodes);
    Map<CyNode, Integer> nodeIdexMap = new HashMap<CyNode, Integer>();
    int nodeIndex = 0;
    Iterator<LayoutNode> nodeIter = partition.getNodeList().iterator();
    while (nodeIter.hasNext() && !cancelled) {
        // final View<CyNode> nv = nodeIter.next().getNodeView();
        LayoutNode ln = nodeIter.next();
        if (ln.isLocked())
            continue;
        final View<CyNode> nv = ln.getNodeView();
        nodeViews.put(nodeIndex, nv);
        nodeIdexMap.put(nv.getModel(), nodeIndex);
        nodeIndex++;
    }
    if (cancelled)
        return;
    /* create edge list from edges between selected nodes */
    final List<Edge> edges = new LinkedList<Edge>();
    final Iterator<LayoutEdge> edgeIter = partition.edgeIterator();
    while (edgeIter.hasNext() && !cancelled) {
        final LayoutEdge ev = edgeIter.next();
        final Integer edgeFrom = nodeIdexMap.get(ev.getEdge().getSource());
        final Integer edgeTo = nodeIdexMap.get(ev.getEdge().getTarget());
        if ((edgeFrom == null) || (edgeTo == null))
            continue;
        edges.add(new Edge(edgeFrom, edgeTo));
        edges.add(new Edge(edgeTo, edgeFrom));
    }
    nodeIdexMap.clear();
    nodeIdexMap = null;
    if (cancelled)
        return;
    /* find horizontal and vertical coordinates of each node */
    final Edge[] edge = new Edge[edges.size()];
    edges.toArray(edge);
    final Graph graph = new Graph(numNodes, edge);
    if (cancelled)
        return;
    // all false
    posSet = new boolean[nodeViews.size()];
    // all false
    depthPosSet = new boolean[nodeViews.size()];
    bc = graph.biconnectedComponents();
    int maxSize = -1;
    int maxIndex = -1;
    for (int i = 0; i < bc.length; i++) if (bc[i].length > maxSize) {
        maxSize = bc[i].length;
        maxIndex = i;
    }
    if (maxIndex == -1)
        return;
    if (cancelled)
        return;
    drawnBiComps = new boolean[bc.length];
    node2BiComp = new HashMap<Integer, Integer>();
    for (int i = 0; i < bc.length; i++) if (bc[i].length > 3) {
        for (int j = 0; j < bc[i].length; j++) {
            node2BiComp.put(bc[i][j], i);
        }
    }
    final double radius = (48 * maxSize) / (2 * Math.PI);
    final double deltaAngle = (2 * Math.PI) / maxSize;
    double angle = 0;
    int startX = (int) radius;
    int startY = (int) radius;
    edgesFrom = graph.GetEdgesFrom();
    // sorting nodes on inner circle
    bc[maxIndex] = SortInnerCircle(bc[maxIndex]);
    // setting nodes on inner circle
    for (int i = 0; i < bc[maxIndex].length; i++) {
        setOffset(nodeViews.get(bc[maxIndex][i]), startX + (Math.cos(angle) * radius), startY - (Math.sin(angle) * radius));
        posSet[bc[maxIndex][i]] = true;
        angle += deltaAngle;
    }
    drawnBiComps[maxIndex] = true;
    nodeHeights = new HashMap<Integer, Integer>();
    SetOuterCircle(maxIndex, radius, startX, startY, -1);
    if (cancelled)
        return;
    nodeIter = partition.nodeIterator();
    while (nodeIter.hasNext() && !cancelled) {
        final LayoutNode ln = nodeIter.next();
        final View<CyNode> nv = ln.getNodeView();
        ln.setX(nv.getVisualProperty(BasicVisualLexicon.NODE_X_LOCATION));
        ln.setY(nv.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION));
        partition.moveNodeToLocation(ln);
    }
}
Also used : LayoutNode(org.cytoscape.view.layout.LayoutNode) HashMap(java.util.HashMap) View(org.cytoscape.view.model.View) CyNetworkView(org.cytoscape.view.model.CyNetworkView) LinkedList(java.util.LinkedList) LayoutEdge(org.cytoscape.view.layout.LayoutEdge) Graph(csapps.layout.algorithms.hierarchicalLayout.Graph) CyNode(org.cytoscape.model.CyNode) LayoutEdge(org.cytoscape.view.layout.LayoutEdge) Edge(csapps.layout.algorithms.hierarchicalLayout.Edge)

Aggregations

Edge (csapps.layout.algorithms.hierarchicalLayout.Edge)1 Graph (csapps.layout.algorithms.hierarchicalLayout.Graph)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 CyNode (org.cytoscape.model.CyNode)1 LayoutEdge (org.cytoscape.view.layout.LayoutEdge)1 LayoutNode (org.cytoscape.view.layout.LayoutNode)1 CyNetworkView (org.cytoscape.view.model.CyNetworkView)1 View (org.cytoscape.view.model.View)1