Optimize setTension() CPU usage
This commit is contained in:
@@ -77,7 +77,7 @@ class Architype {
|
||||
}
|
||||
|
||||
onResize(e) {
|
||||
this.fixSizes(this.graph_.nodes);
|
||||
this.fixSizes();
|
||||
}
|
||||
|
||||
exportGraphviz() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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];
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user