use of csapps.layout.Profile 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);
}
}
}
Aggregations