Optimize setTension() CPU usage

This commit is contained in:
Ian Gulliver
2019-07-03 20:00:05 +00:00
parent b066001423
commit 27411817b1
5 changed files with 14 additions and 9 deletions

View File

@@ -77,7 +77,7 @@ class Architype {
}
onResize(e) {
this.fixSizes(this.graph_.nodes);
this.fixSizes();
}
exportGraphviz() {

View File

@@ -50,19 +50,19 @@ class GraphNode {
// Keep one space between subgraphs
if (this.subgraph != node.subgraph) {
this.addAffinity(node, d => d < 1.5 ? -INF : 0);
this.addAffinity(node, d => d <= 2 ? -INF : 0);
}
// Keep one space around groups
if (this.groups.size && !intersects(this.groups, node.groups)) {
node.addAffinity(this, d => d < 1.5 ? -INF : 0);
node.addAffinity(this, d => d <= 2 ? -INF : 0);
}
}
for (let to of this.links) {
// Stronger affinity for links
// Prefer to move toward the target instance
this.addAffinity(to, d => d < 1.5 ? -INF : d * 11);
to.addAffinity(this, d => d < 1.5 ? -INF : d * 9);
this.addAffinity(to, d => d <= 2 ? -INF : d * 11);
to.addAffinity(this, d => d <= 2 ? -INF : d * 9);
}
for (let group of this.groups.values()) {
for (let node of group.nodes) {

View File

@@ -67,9 +67,14 @@ class Layout {
iterate() {
let objects = Array.from(this.nodes_);
objects.push(...this.groups_);
this.setTension(objects);
this.sortByMostTension(objects);
for (let group of this.groups_) {
// Groups go in the list after nodes, and nodes must have tension set
// properly first.
group.setTension();
objects.push(group);
}
let newOffset = null;
let newTension = this.getTotalTension(objects);

View File

@@ -9,7 +9,6 @@ class LayoutGroup {
// tension
this.vec = [0, 0];
for (let node of this.nodes.values()) {
node.setTension();
for (let i of [0, 1]) {
this.vec[i] += node.vec[i];
};

View File

@@ -26,8 +26,9 @@ class LayoutNode {
vec[i] = aff.node.pos[i] - this.pos[i];
vecsum += Math.abs(vec[i]);
};
let distance = Math.sqrt(Math.pow(vec[0], 2) + Math.pow(vec[1], 2));
let weight = aff.distanceToWeight(distance);
// Avoid calling sqrt(), since the results are used relatively
let distanceSquared = vec[0] * vec[0] + vec[1] * vec[1];
let weight = aff.distanceToWeight(distanceSquared);
for (let i of [0, 1]) {
this.vec[i] += (weight * vec[i]) / vecsum;
}