Search in sources :

Example 1 with LayoutPoint

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

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

the class BioLayoutKKAlgorithmTask method layoutPartition.

/**
 * Perform a layout
 */
public void layoutPartition(LayoutPartition partition) {
    LayoutPoint initialLocation = null;
    this.partition = partition;
    // Initialize all of our values.  This will create
    // our internal objects and initialize them
    // local_initialize();
    m_nodeCount = partition.nodeCount();
    // Set defaults -- this is done here insted of in the constructor
    // to allow users to change m_numLayoutPasses
    m_nodeDistanceSpringScalars = new double[m_numLayoutPasses];
    for (int i = 0; i < m_numLayoutPasses; i++) m_nodeDistanceSpringScalars[i] = 1.0;
    m_anticollisionSpringScalars = new double[m_numLayoutPasses];
    m_anticollisionSpringScalars[0] = 0.0;
    for (int i = 1; i < m_numLayoutPasses; i++) m_anticollisionSpringScalars[i] = 1.0;
    // System.out.println("BioLayoutKK Algorithm.  Laying out " + m_nodeCount + " nodes and "
    // + partition.edgeCount() + " edges: ");
    /*
		        for (Iterator diter = partition.nodeIterator(); diter.hasNext(); ) {
		            System.out.println("\t"+(LayoutNode)diter.next());
		        }
		        for (Iterator diter = partition.edgeIterator(); diter.hasNext(); ) {
		            System.out.println("\t"+(LayoutEdge)diter.next());
		        }
		*/
    // Calculate a distance threshold
    double euclideanDistanceThreshold = (m_nodeCount + partition.edgeCount()) / 10;
    int numIterations = (int) ((m_nodeCount * m_averageIterationsPerNode) / m_numLayoutPasses);
    List<PartialDerivatives> partialsList = new ArrayList<PartialDerivatives>();
    double[] potentialEnergy = new double[1];
    if (potentialEnergy[0] != 0.0)
        throw new RuntimeException();
    PartialDerivatives partials;
    PartialDerivatives furthestNodePartials = null;
    m_nodeDistanceSpringRestLengths = new double[m_nodeCount][m_nodeCount];
    m_nodeDistanceSpringStrengths = new double[m_nodeCount][m_nodeCount];
    // Figure out our starting point
    initialLocation = partition.getAverageLocation();
    // outside of our bounds
    if (context.randomize)
        partition.randomizeLocations();
    // Calculate our edge weights
    partition.calculateEdgeWeights();
    // Compute our distances
    if (cancelled)
        return;
    taskMonitor.setProgress(0.02);
    taskMonitor.setStatusMessage("Calculating node distances");
    int[][] nodeDistances = calculateNodeDistances();
    if (cancelled)
        return;
    taskMonitor.setProgress(0.04);
    taskMonitor.setStatusMessage("Calculating spring constants");
    calculateSpringData(nodeDistances);
    final double percentCompletedBeforePasses = 5.0d;
    final double percentCompletedAfterPass1 = 60.0d;
    final double percentCompletedAfterFinalPass = 95.0d;
    double currentProgress = percentCompletedBeforePasses;
    // Compute our optimal lengths
    for (m_layoutPass = 0; m_layoutPass < m_numLayoutPasses; m_layoutPass++) {
        final double percentProgressPerIter;
        Profile passTimer = new Profile();
        passTimer.start();
        if (m_layoutPass == 0) {
            percentProgressPerIter = (percentCompletedAfterPass1 - percentCompletedBeforePasses) / (double) (m_nodeCount + numIterations);
        } else {
            percentProgressPerIter = (percentCompletedAfterFinalPass - percentCompletedAfterPass1) / (double) ((m_nodeCount + numIterations) * (m_numLayoutPasses - 1));
        }
        // Initialize this layout pass.
        potentialEnergy[0] = 0.0;
        partialsList.clear();
        furthestNodePartials = null;
        taskMonitor.setStatusMessage("Calculating partial derivatives -- pass " + (m_layoutPass + 1) + " of " + m_numLayoutPasses);
        // Calculate all node distances.  Keep track of the furthest.
        for (LayoutNode v : partition.getNodeList()) {
            if (cancelled)
                return;
            taskMonitor.setProgress(currentProgress / 100.0);
            if (v.isLocked())
                continue;
            partials = new PartialDerivatives(v);
            calculatePartials(partials, null, potentialEnergy, false);
            // System.out.println(partials.printPartial()+" potentialEnergy = "+potentialEnergy[0]);
            partialsList.add(partials);
            if ((furthestNodePartials == null) || (partials.euclideanDistance > furthestNodePartials.euclideanDistance)) {
                furthestNodePartials = partials;
            }
            currentProgress += percentProgressPerIter;
        }
        // partialProfile.done("Partial time for pass "+(m_layoutPass+1)+" is ");
        taskMonitor.setStatusMessage("Executing spring logic -- pass " + (m_layoutPass + 1) + " of " + m_numLayoutPasses);
        // springProfile.start();
        for (int iterations_i = 0; (iterations_i < numIterations) && (furthestNodePartials.euclideanDistance >= euclideanDistanceThreshold); iterations_i++) {
            if (cancelled)
                return;
            taskMonitor.setProgress(currentProgress / 100.0);
            furthestNodePartials = moveNode(furthestNodePartials, partialsList, potentialEnergy);
            // System.out.println(furthestNodePartials.printPartial()+" (furthest) potentialEnergy = "+potentialEnergy[0]);
            currentProgress += percentProgressPerIter;
        }
    // springProfile.done("Spring time for pass "+(m_layoutPass+1)+" is ");
    }
    taskMonitor.setProgress(percentCompletedAfterFinalPass / 100.0);
    taskMonitor.setStatusMessage("Updating display");
    // Actually move the pieces around
    // Note that we reset our min/max values before we start this
    // so we can get an accurate min/max for paritioning
    partition.resetNodes();
    for (LayoutNode v : partition.getNodeList()) {
        partition.moveNodeToLocation(v);
    }
    // Not quite done, yet.  If we're only laying out selected nodes, we need
    // to migrate the selected nodes back to their starting position
    double xDelta = 0.0;
    double yDelta = 0.0;
    final LayoutPoint finalLocation = partition.getAverageLocation();
    xDelta = finalLocation.getX() - initialLocation.getX();
    yDelta = finalLocation.getY() - initialLocation.getY();
    partition.resetNodes();
    for (LayoutNode v : partition.getNodeList()) {
        if (!v.isLocked()) {
            v.decrement(xDelta, yDelta);
            partition.moveNodeToLocation(v);
        }
    }
}
Also used : LayoutNode(org.cytoscape.view.layout.LayoutNode) ArrayList(java.util.ArrayList) LayoutPoint(org.cytoscape.view.layout.LayoutPoint) LayoutPoint(org.cytoscape.view.layout.LayoutPoint) Profile(csapps.layout.Profile)

Example 3 with LayoutPoint

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

the class BioLayoutFRAlgorithmTask method layoutPartition.

/**
 * Perform a layout
 */
public void layoutPartition(LayoutPartition partition) {
    this.partition = partition;
    LayoutPoint initialLocation = null;
    /* Get all of our profiles */
    /*
		        initProfile = new Profile();
		        iterProfile = new Profile();
		        repulseProfile = new Profile();
		        attractProfile = new Profile();
		        updateProfile = new Profile();

		        initProfile.start();
		*/
    // Calculate a bounded rectangle for our
    // layout.  This is roughly the area of all
    // nodes * 2
    calculateSize();
    // System.out.println("BioLayoutFR Algorithm.  Laying out " + partition.nodeCount()
    // + " nodes and " + partition.edgeCount() + " edges: ");
    // Initialize our temperature
    double temp;
    if (context.temperature == 0) {
        temp = Math.sqrt(this.width * this.height) / 2;
    } else {
        temp = Math.sqrt(this.width * this.height) * this.context.temperature / 100;
    }
    // Figure out our starting point
    initialLocation = partition.getAverageLocation();
    // outside of our bounds
    if (context.randomize)
        partition.randomizeLocations(context.layout3D);
    // Calculate our force constant
    calculateForces();
    // Calculate our edge weights
    partition.calculateEdgeWeights();
    // initProfile.done("Initialization completed in ");
    taskMonitor.setStatusMessage("Calculating new node positions");
    taskMonitor.setProgress(0.01);
    // Main algorithm
    // iterProfile.start();
    int iteration = 0;
    for (iteration = 0; (iteration < context.nIterations) && !cancelled; iteration++) {
        if ((temp = doOneIteration(iteration, temp)) == 0)
            break;
        if (debug || ((context.update_iterations > 0) && ((iteration % context.update_iterations) == 0))) {
            if (iteration > 0) {
                // Actually move the pieces around
                for (LayoutNode v : partition.getNodeList()) {
                    // if this is locked, the move just resets X and Y
                    if (context.layout3D)
                        v.moveToLocation3D();
                    else
                        v.moveToLocation();
                }
                // This fires events to presentation layer.
                networkView.updateView();
            }
            if (debug) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
        }
        taskMonitor.setStatusMessage("Calculating new node positions - " + iteration);
        taskMonitor.setProgress(iteration / context.nIterations);
    }
    // iterProfile.done("Iterations complete in ");
    // System.out.println("Attraction calculation portion of iterations took "+attractProfile.getTotalTime()+"ms");
    // System.out.println("Repulsion calculation portion of iterations took "+repulseProfile.getTotalTime()+"ms");
    // System.out.println("Update portion of iterations took "+updateProfile.getTotalTime()+"ms");
    taskMonitor.setStatusMessage("Updating display");
    // Actually move the pieces around
    // Note that we reset our min/max values before we start this
    // so we can get an accurate min/max for paritioning
    partition.resetNodes();
    for (LayoutNode v : partition.getNodeList()) {
        if (context.layout3D)
            partition.moveNodeToLocation3D(v);
        else
            partition.moveNodeToLocation(v);
    }
    // Not quite done, yet.  If we're only laying out selected nodes, we need
    // to migrate the selected nodes back to their starting position
    double xDelta = 0.0;
    double yDelta = 0.0;
    double zDelta = 0.0;
    final LayoutPoint finalLocation = partition.getAverageLocation();
    xDelta = finalLocation.getX() - initialLocation.getX();
    yDelta = finalLocation.getY() - initialLocation.getY();
    if (context.layout3D)
        zDelta = finalLocation.getZ() - initialLocation.getZ();
    partition.resetNodes();
    for (LayoutNode v : partition.getNodeList()) {
        if (!v.isLocked()) {
            v.decrement(xDelta, yDelta, zDelta);
            if (context.layout3D)
                partition.moveNodeToLocation3D(v);
            else
                partition.moveNodeToLocation(v);
        }
    }
// System.out.println("Layout complete after " + iteration + " iterations");
}
Also used : LayoutNode(org.cytoscape.view.layout.LayoutNode) LayoutPoint(org.cytoscape.view.layout.LayoutPoint) LayoutPoint(org.cytoscape.view.layout.LayoutPoint)

Aggregations

LayoutNode (org.cytoscape.view.layout.LayoutNode)3 LayoutPoint (org.cytoscape.view.layout.LayoutPoint)3 Profile (csapps.layout.Profile)1 ArrayList (java.util.ArrayList)1 LayoutEdge (org.cytoscape.view.layout.LayoutEdge)1 DragForce (prefuse.util.force.DragForce)1 ForceItem (prefuse.util.force.ForceItem)1 ForceSimulator (prefuse.util.force.ForceSimulator)1 NBodyForce (prefuse.util.force.NBodyForce)1 SpringForce (prefuse.util.force.SpringForce)1