Link: optimize so that no link {} is used, less than half the ram used

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-30 20:10:46 +00:00
parent 7ca8b2bbc3
commit 409777bc14
2 changed files with 29 additions and 22 deletions

View File

@@ -8,10 +8,15 @@ function startLinkSimulation() {
manualStop = true;
simulation.stop();
p1 = performance.now();
let links = [];
let links = [], force;
// Initialize link array.
nodes = simulation.nodes();
if (tweakedVerOfLink) {
force = d3.forceLinkTweaked()
.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm);
});
}
else {
for (i = nodes.length-1; i >= 1; i--) {
for (j = i-1; j >= 0; j--) {
links.push({
@@ -20,6 +25,12 @@ function startLinkSimulation() {
});
}
}
force = d3.forceLink()
.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm);
})
.links(links);
}
/* Add force
* Please add the distance function before feeding the force
@@ -33,11 +44,6 @@ function startLinkSimulation() {
* The full pre-calculation will then occur once when the force is being
* initialized by the simulation.
*/
let force = tweakedVerOfLink ? d3.forceLinkTweaked() : d3.forceLink();
force.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm);
})
.links(links);
simulation
.alphaDecay(0)

View File

@@ -18,17 +18,16 @@ export default function(links) {
function force(alpha) {
// Each iteration in a tick
for (var k = 0, n = links.length; k < iterations; ++k) {
for (var k = 0, n = nodes.length, source, target, i, j, x, y, l; k < iterations; ++k) {
// For each link
for (var i = 0, link, source, target, x, y, l, b; i < n; ++i) {
link = links[i];
for (i = 1; i < n; i++) for (j = 0; j < i; j++) {
// jiggle so it wont divide / multiply by zero after this
source = link.source;
target = link.target;
source = nodes[i];
target = nodes[j];
x = target.x + target.vx - source.x - source.vx || jiggle();
y = target.y + target.vy - source.y - source.vy || jiggle();
l = Math.sqrt(x * x + y * y);
l = (l - distances[i]) / l * dataSizeFactor * alpha;
l = (l - distances[i*(i-1)/2+j]) / l * dataSizeFactor * alpha;
x *= l, y *= l;
target.vx -= x;
target.vy -= y;
@@ -46,8 +45,10 @@ export default function(links) {
function initializeDistance() {
if (!nodes) return;
for (var i = 0, n = links.length; i < n; ++i) {
distances[i] = +distance(links[i], i, links);
for (let i = 1, n = nodes.length; i < n; i++) {
for (let j = 0; j < i; j++) {
distances.push(distanceFunction(nodes[i], nodes[j], props, norm));
}
}
}