Search in sources :

Example 1 with LayoutEdge

use of org.cytoscape.view.layout.LayoutEdge in project cytoscape-impl by cytoscape.

the class ForceDirectedLayoutTask method layoutPartition.

public void layoutPartition(LayoutPartition part) {
    LayoutPoint initialLocation = null;
    // System.out.println("layoutPartion: "+part.getEdgeList().size()+" edges");
    // Calculate our edge weights
    part.calculateEdgeWeights();
    // System.out.println("layoutPartion: "+part.getEdgeList().size()+" edges after calculateEdgeWeights");
    // m_fsim.setIntegrator(integrator.getNewIntegrator());
    // m_fsim.clear();
    m_fsim = new ForceSimulator();
    m_fsim.addForce(new NBodyForce());
    m_fsim.addForce(new SpringForce());
    m_fsim.addForce(new DragForce());
    forceItems.clear();
    List<LayoutNode> nodeList = part.getNodeList();
    List<LayoutEdge> edgeList = part.getEdgeList();
    if (context.isDeterministic) {
        Collections.sort(nodeList);
        Collections.sort(edgeList);
    }
    // initialize nodes
    for (LayoutNode ln : nodeList) {
        ForceItem fitem = forceItems.get(ln);
        if (fitem == null) {
            fitem = new ForceItem();
            forceItems.put(ln, fitem);
        }
        fitem.mass = getMassValue(ln);
        fitem.location[0] = 0f;
        fitem.location[1] = 0f;
        m_fsim.addItem(fitem);
    }
    // initialize edges
    for (LayoutEdge e : edgeList) {
        LayoutNode n1 = e.getSource();
        ForceItem f1 = forceItems.get(n1);
        LayoutNode n2 = e.getTarget();
        ForceItem f2 = forceItems.get(n2);
        if (f1 == null || f2 == null)
            continue;
        m_fsim.addSpring(f1, f2, getSpringCoefficient(e), getSpringLength(e));
    }
    // setTaskStatus(5); // This is a rough approximation, but probably good enough
    if (taskMonitor != null) {
        taskMonitor.setStatusMessage("Initializing partition " + part.getPartitionNumber());
    }
    // Figure out our starting point
    initialLocation = part.getAverageLocation();
    // perform layout
    long timestep = 1000L;
    for (int i = 0; i < context.numIterations && !cancelled; i++) {
        timestep *= (1.0 - i / (double) context.numIterations);
        long step = timestep + 50;
        m_fsim.runSimulator(step);
        setTaskStatus((int) (((double) i / (double) context.numIterations) * 90. + 5));
    }
    // update positions
    // reset the nodes so we get the new average location
    part.resetNodes();
    for (LayoutNode ln : part.getNodeList()) {
        if (!ln.isLocked()) {
            ForceItem fitem = forceItems.get(ln);
            ln.setX(fitem.location[0]);
            ln.setY(fitem.location[1]);
            part.moveNodeToLocation(ln);
        }
    }
}
Also used : LayoutNode(org.cytoscape.view.layout.LayoutNode) NBodyForce(prefuse.util.force.NBodyForce) SpringForce(prefuse.util.force.SpringForce) DragForce(prefuse.util.force.DragForce) LayoutPoint(org.cytoscape.view.layout.LayoutPoint) ForceSimulator(prefuse.util.force.ForceSimulator) LayoutPoint(org.cytoscape.view.layout.LayoutPoint) LayoutEdge(org.cytoscape.view.layout.LayoutEdge) ForceItem(prefuse.util.force.ForceItem)

Example 2 with LayoutEdge

use of org.cytoscape.view.layout.LayoutEdge in project cytoscape-impl by cytoscape.

the class BioLayoutKKAlgorithmTask method calculateSpringData.

private void calculateSpringData(int[][] nodeDistances) {
    // Set all springs to the default
    for (int node_i = 0; node_i < m_nodeCount; node_i++) {
        Arrays.fill(m_nodeDistanceSpringRestLengths[node_i], m_disconnectedNodeDistanceSpringRestLength);
        Arrays.fill(m_nodeDistanceSpringStrengths[node_i], m_disconnectedNodeDistanceSpringStrength);
    }
    // Calculate rest lengths and strengths based on node distance data.
    for (LayoutEdge edge : partition.getEdgeList()) {
        int node_i = edge.getSource().getIndex();
        int node_j = edge.getTarget().getIndex();
        double weight = context.unweighted ? edgeWeighter.defaultEdgeWeight : edge.getWeight();
        // System.out.println(edge);
        if (nodeDistances[node_i][node_j] != Integer.MAX_VALUE) {
            // Compute spring rest lengths.
            m_nodeDistanceSpringRestLengths[node_i][node_j] = (m_nodeDistanceRestLengthConstant * nodeDistances[node_i][node_j]) / (weight);
            m_nodeDistanceSpringRestLengths[node_j][node_i] = m_nodeDistanceSpringRestLengths[node_i][node_j];
            // System.out.println("Setting spring ("+node_i+","+node_j+") ["+weight+"] length to "+m_nodeDistanceSpringRestLengths[node_j][node_i]);
            // Compute spring strengths.
            m_nodeDistanceSpringStrengths[node_i][node_j] = m_nodeDistanceStrengthConstant / (nodeDistances[node_i][node_j] * nodeDistances[node_i][node_j]);
            m_nodeDistanceSpringStrengths[node_j][node_i] = m_nodeDistanceSpringStrengths[node_i][node_j];
        // System.out.println("Setting spring ("+node_i+","+node_j+") strength to "+m_nodeDistanceSpringStrengths[node_j][node_i]);
        }
    }
}
Also used : LayoutPoint(org.cytoscape.view.layout.LayoutPoint) LayoutEdge(org.cytoscape.view.layout.LayoutEdge)

Example 3 with LayoutEdge

use of org.cytoscape.view.layout.LayoutEdge 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)

Example 4 with LayoutEdge

use of org.cytoscape.view.layout.LayoutEdge in project cytoscape-impl by cytoscape.

the class BioLayoutFRAlgorithmTask method doOneIteration.

/**
 * This executes a single iteration of the FR algorithm.
 *
 * @param iteration The current interation.
 * @param temp The current temperature factor.
 * @return an updated temperature factor.
 */
public double doOneIteration(int iteration, double temp) {
    double xAverage = 0;
    double yAverage = 0;
    double zAverage = 0;
    // Calculate repulsive forces
    for (LayoutNode v : partition.getNodeList()) {
        if (!v.isLocked()) {
            xAverage += v.getX() / partition.nodeCount();
            yAverage += v.getY() / partition.nodeCount();
            zAverage += v.getZ() / partition.nodeCount();
        }
    }
    for (LayoutNode v : partition.getNodeList()) {
        if (!v.isLocked()) {
            calculateRepulsion(v);
            if (gravity_constant != 0)
                calculateGravity(v, xAverage, yAverage, zAverage);
        }
    }
    // / for e in E do begin
    for (LayoutEdge e : partition.getEdgeList()) {
        calculateAttraction(e);
    }
    // / end
    // attractProfile.checkpoint();
    // Dump the current displacements
    // print_disp();
    // Dampen & update
    double xDispTotal = 0;
    double yDispTotal = 0;
    // / for v in V do begin
    for (LayoutNode v : partition.getNodeList()) {
        if (v.isLocked())
            continue;
        calculatePosition(v, temp);
        xDispTotal += Math.abs(v.getXDisp());
        yDispTotal += Math.abs(v.getYDisp());
    }
    // hit our completion criteria
    if (complete(xDispTotal, yDispTotal))
        return 0;
    // t := cool(t)
    return cool(temp, iteration);
}
Also used : LayoutNode(org.cytoscape.view.layout.LayoutNode) LayoutEdge(org.cytoscape.view.layout.LayoutEdge)

Example 5 with LayoutEdge

use of org.cytoscape.view.layout.LayoutEdge in project cytoscape-impl by cytoscape.

the class CoSELayoutAlgorithmTask method layoutPartition.

@Override
public void layoutPartition(final LayoutPartition partition) {
    if (cancelled)
        return;
    final CyGroupManager groupManager = serviceRegistrar.getService(CyGroupManager.class);
    final CyNetwork network = networkView.getModel();
    // Create the CoSE model
    // (see http://www.cs.bilkent.edu.tr/~ivis/chilay/ChiLay-2.0-PG.pdf)
    cose = new CoSELayout();
    cose.addProgressListener(new ProgressListener() {

        @Override
        public void update(double value) {
            taskMonitor.setProgress(value);
        }
    });
    final LGraphManager gm = cose.getGraphManager();
    final LGraph root = gm.addRoot();
    // Index all LayoutNodes by CyNode for future reference
    final Map<CyNode, LayoutNode> layoutNodeMap = new HashMap<>();
    for (LayoutNode n : partition.getNodeList()) layoutNodeMap.put(n.getNode(), n);
    // Create all CoSE nodes
    final Map<CyNode, LNode> lNodeMap = new HashMap<>();
    for (LayoutNode n : partition.getNodeList()) {
        // If this node does not belong to a CyGroup, let's traverse its potential compound node tree.
        if (groupManager.getGroupsForNode(n.getNode(), network).isEmpty())
            traverseLNodeTree(n, root, cose, lNodeMap, layoutNodeMap, groupManager);
    }
    if (cancelled)
        return;
    // Create all CoSE edges
    final Map<CyEdge, LEdge> lEdgeMap = new HashMap<>();
    final Iterator<LayoutEdge> edgeIter = partition.edgeIterator();
    while (edgeIter.hasNext() && !cancelled) {
        final LayoutEdge e = edgeIter.next();
        createLEdge(e, cose, lNodeMap, lEdgeMap);
    }
    if (cancelled)
        return;
    // Run the layout
    try {
        cose.runLayout();
    } catch (Exception e) {
        logger.error("Error running CoSE Layout", e);
        return;
    }
    if (cancelled)
        return;
    // Move all Node Views to the new positions
    for (LayoutNode n : partition.getNodeList()) partition.moveNodeToLocation(n);
}
Also used : LayoutNode(org.cytoscape.view.layout.LayoutNode) HashMap(java.util.HashMap) LEdge(org.ivis.layout.LEdge) CyNetwork(org.cytoscape.model.CyNetwork) LGraphManager(org.ivis.layout.LGraphManager) LGraph(org.ivis.layout.LGraph) CyEdge(org.cytoscape.model.CyEdge) LayoutEdge(org.cytoscape.view.layout.LayoutEdge) ProgressListener(org.ivis.layout.ProgressListener) CyNode(org.cytoscape.model.CyNode) LNode(org.ivis.layout.LNode) CoSELayout(org.ivis.layout.cose.CoSELayout) CyGroupManager(org.cytoscape.group.CyGroupManager)

Aggregations

LayoutEdge (org.cytoscape.view.layout.LayoutEdge)5 LayoutNode (org.cytoscape.view.layout.LayoutNode)4 HashMap (java.util.HashMap)2 CyNode (org.cytoscape.model.CyNode)2 LayoutPoint (org.cytoscape.view.layout.LayoutPoint)2 Edge (csapps.layout.algorithms.hierarchicalLayout.Edge)1 Graph (csapps.layout.algorithms.hierarchicalLayout.Graph)1 LinkedList (java.util.LinkedList)1 CyGroupManager (org.cytoscape.group.CyGroupManager)1 CyEdge (org.cytoscape.model.CyEdge)1 CyNetwork (org.cytoscape.model.CyNetwork)1 CyNetworkView (org.cytoscape.view.model.CyNetworkView)1 View (org.cytoscape.view.model.View)1 LEdge (org.ivis.layout.LEdge)1 LGraph (org.ivis.layout.LGraph)1 LGraphManager (org.ivis.layout.LGraphManager)1 LNode (org.ivis.layout.LNode)1 ProgressListener (org.ivis.layout.ProgressListener)1 CoSELayout (org.ivis.layout.cose.CoSELayout)1 DragForce (prefuse.util.force.DragForce)1