Skip to content

Commit e61ce60

Browse files
committed
Fixing iteration
1 parent ed87ed2 commit e61ce60

File tree

1 file changed

+81
-88
lines changed

1 file changed

+81
-88
lines changed

plugins/sigma.layout.forceAtlas2/worker.js

Lines changed: 81 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
var W = {
3030
ppn: 10,
3131
ppe: 3,
32-
maxRise: 0.5,
32+
maxForce: 10,
3333
iterations: 0,
3434
settings: {
3535
linLogMode: false,
@@ -39,10 +39,8 @@
3939
scalingRatio: 1,
4040
strongGravityMode: false,
4141
gravity: 1,
42-
jitterTolerance: 1,
4342
barnesHutOptimize: false,
4443
barnesHutTheta: 1.2,
45-
speed: 1,
4644
outboundAttCompensation: 1,
4745
totalSwinging: 0,
4846
totalEffectiveTraction: 0,
@@ -197,15 +195,9 @@
197195
W.nodeMatrix = nodes;
198196
W.edgeMatrix = edges;
199197

200-
// Helper arrays
201-
W.nodes = [];
202-
W.edges = [];
203-
204-
for (i = 0, l = W.nodeMatrix.length; i < l; i += W.ppn)
205-
W.nodes.push(i);
206-
207-
for (i = 0, l = W.edgeMatrix.length; i < l; i += W.ppe)
208-
W.edges.push(i);
198+
// Length
199+
W.nodesLength = W.nodeMatrix.length;
200+
W.edgesLength = W.edgeMatrix.length;
209201
}
210202

211203
/**
@@ -214,7 +206,7 @@
214206

215207
// MATH: get distances stuff and power 2 issues
216208
function pass() {
217-
var l, m, n, n1, n2, e, s, t, w;
209+
var n, n1, n2, e, s, t, w;
218210

219211
var rootRegion,
220212
outboundAttCompensation;
@@ -224,7 +216,7 @@
224216
//-----------------------------
225217

226218
// Resetting positions
227-
for (n = 0, l = W.nodes.length; n < l; n++) {
219+
for (n = 0; n < W.nodesLength; n += W.ppn) {
228220
W.nodeMatrix[np(n, 'old_dx')] = W.nodeMatrix[np(n, 'dx')];
229221
W.nodeMatrix[np(n, 'old_dy')] = W.nodeMatrix[np(n, 'dy')];
230222
W.nodeMatrix[np(n, 'dx')] = 0;
@@ -258,8 +250,8 @@
258250
else {
259251

260252
// Square iteration
261-
for (n1 = 0, l = W.nodes.length; n1 < l; n1++) {
262-
for (n2 = 0, m = W.nodes.length; n2 < m; n2++) {
253+
for (n1 = 0; n1 < W.nodesLength; n1 += W.ppn) {
254+
for (n2 = 0; n2 < W.nodesLength; n2 += W.ppn) {
263255

264256
// TODO: apply repulsion
265257
}
@@ -269,113 +261,114 @@
269261

270262
// 3) Gravity
271263
//------------
272-
for (n = 0, l = W.nodes.length; n < l; n++) {
264+
for (n = 0; n < W.nodesLength; n += W.ppn) {
273265

274266
// TODO: apply gravity
275267
}
276268

277269

278270
// 4) Attraction
279271
//---------------
280-
for (e = 0, l = W.edges.length; e < l; e++) {
272+
for (e = 0; e < W.edgesLength; e += W.ppe) {
281273
s = W.edgeMatrix[ep(e, 'source')];
282274
t = W.edgeMatrix[ep(e, 'target')];
283275
w = W.edgeMatrix[ep(e, 'weight')];
284276

285277
// TODO: apply attraction
286278
}
287279

288-
289-
// 5) Adjust Speed
280+
// 5) Apply Forces
290281
//-----------------
291-
var totalSwinging, totalEffectiveTraction = 0,
282+
var force,
292283
swinging,
293-
targetSpeed,
294-
speed;
295-
296-
// !!!TODO!!!: speed is NaN
297-
298-
// MATH: clean this up
299-
for (n = 0, l = W.nodes.length; n < l; n++) {
300-
if (!W.nodeMatrix[np(n, 'fixed')]) {
301-
swinging = Math.sqrt(
302-
Math.pow(
303-
W.nodeMatrix[np(n, 'old_dx')] - W.nodeMatrix[np(n, 'dx')], 2) +
304-
Math.pow(
305-
W.nodeMatrix[np(n, 'old_dy')] - W.nodeMatrix[np(n, 'dy')], 2)
306-
);
307-
308-
totalSwinging += W.nodeMatrix[np(n, 'mass')] * swinging;
309-
totalEffectiveTraction += W.nodeMatrix[np(n, 'mass')] * 0.5 *
310-
Math.pow(
311-
W.nodeMatrix[np(n, 'old_dx')] + W.nodeMatrix[np(n, 'dx')], 2) +
312-
Math.pow(
313-
W.nodeMatrix[np(n, 'old_dy')] + W.nodeMatrix[np(n, 'dy')], 2);
314-
}
315-
}
316-
317-
targetSpeed = Math.pow(W.settings.jitterTolerance, 2) *
318-
totalEffectiveTraction / totalSwinging;
319-
320-
speed = speed + Math.min(targetSpeed - speed, W.maxRise * speed);
321-
322-
323-
// 6) Apply Forces
324-
//-----------------
325-
var factor,
326-
df;
284+
traction,
285+
nodespeed;
327286

328-
// MATH: some code is repeated from previous step
329-
// TODO: possible to drop following condition to make this more concise
287+
// MATH: sqrt and square distances
330288
if (W.settings.adjustSizes) {
331289

332-
for (n = 0, l = W.nodes.length; n < l; n++) {
333-
290+
for (n = 0; n < W.nodesLength; n += W.ppn) {
334291
if (!W.nodeMatrix[np(n, 'fixed')]) {
335-
swinging = Math.sqrt(
336-
Math.pow(
337-
W.nodeMatrix[np(n, 'old_dx')] - W.nodeMatrix[np(n, 'dx')], 2) +
338-
Math.pow(
339-
W.nodeMatrix[np(n, 'old_dy')] - W.nodeMatrix[np(n, 'dy')], 2)
340-
);
341-
342-
factor = 0.1 * speed / (1 + speed * Math.sqrt(swinging));
343-
344-
df = Math.sqrt(
292+
force = Math.sqrt(
345293
Math.pow(W.nodeMatrix[np(n, 'dx')], 2) +
346294
Math.pow(W.nodeMatrix[np(n, 'dy')], 2)
347295
);
348296

349-
factor = Math.min(factor * df, 10) / df;
350-
351-
// Updating node's X and Y
352-
W.nodeMatrix[np(n, 'x')] = W.nodeMatrix[np(n, 'dx')] * factor;
353-
W.nodeMatrix[np(n, 'y')] = W.nodeMatrix[np(n, 'dy')] * factor;
297+
if (force > W.maxForce) {
298+
W.nodeMatrix[np(n, 'dx')] =
299+
W.nodeMatrix[np(n, 'dx')] * maxForce / force;
300+
W.nodeMatrix[np(n, 'dy')] =
301+
W.nodeMatrix[np(n, 'dy')] * maxForce / force;
302+
}
303+
304+
swinging = W.nodeMatrix[np(n, 'mass')] *
305+
Math.sqrt(
306+
(W.nodeMatrix[np(n, 'old_dx')] - W.nodeMatrix[np(n, 'dx')]) *
307+
(W.nodeMatrix[np(n, 'old_dx')] - W.nodeMatrix[np(n, 'dx')]) +
308+
(W.nodeMatrix[np(n, 'old_dy')] - W.nodeMatrix[np(n, 'dy')]) *
309+
(W.nodeMatrix[np(n, 'old_dy')] - W.nodeMatrix[np(n, 'dy')])
310+
);
311+
312+
traction = Math.sqrt(
313+
(W.nodeMatrix[np(n, 'old_dx')] + W.nodeMatrix[np(n, 'dx')]) *
314+
(W.nodeMatrix[np(n, 'old_dx')] + W.nodeMatrix[np(n, 'dx')]) +
315+
(W.nodeMatrix[np(n, 'old_dy')] + W.nodeMatrix[np(n, 'dy')]) *
316+
(W.nodeMatrix[np(n, 'old_dy')] + W.nodeMatrix[np(n, 'dy')])
317+
) / 2;
318+
319+
nodespeed =
320+
0.1 * Math.log(1 + traction) / (1 + Math.sqrt(swinging));
321+
322+
// Updating node's positon
323+
W.nodeMatrix[np(n, 'x')] =
324+
W.nodeMatrix[np(n, 'x')] + W.nodeMatrix[np(n, 'dx')] * nodespeed;
325+
W.nodeMatrix[np(n, 'y')] =
326+
W.nodeMatrix[np(n, 'y')] + W.nodeMatrix[np(n, 'dy')] * nodespeed;
354327
}
355328
}
356329
}
357330
else {
358331

359-
for (n = 0, l = W.nodes.length; n < l; n++) {
360-
332+
for (n = 0; n < W.nodesLength; n += W.ppn) {
361333
if (!W.nodeMatrix[np(n, 'fixed')]) {
362334

363-
swinging = Math.sqrt(
364-
Math.pow(
365-
W.nodeMatrix[np(n, 'old_dx')] - W.nodeMatrix[np(n, 'dx')], 2) +
366-
Math.pow(
367-
W.nodeMatrix[np(n, 'old_dy')] - W.nodeMatrix[np(n, 'dy')], 2)
368-
);
369-
370-
factor = speed / (1 + speed * Math.sqrt(swinging));
371-
372-
// Updating node's X and Y
373-
W.nodeMatrix[np(n, 'x')] = W.nodeMatrix[np(n, 'dx')] * factor;
374-
W.nodeMatrix[np(n, 'y')] = W.nodeMatrix[np(n, 'dy')] * factor;
335+
swinging = W.nodeMatrix[np(n, 'mass')] *
336+
Math.sqrt(
337+
(W.nodeMatrix[np(n, 'old_dx')] - W.nodeMatrix[np(n, 'dx')]) *
338+
(W.nodeMatrix[np(n, 'old_dx')] - W.nodeMatrix[np(n, 'dx')]) +
339+
(W.nodeMatrix[np(n, 'old_dy')] - W.nodeMatrix[np(n, 'dy')]) *
340+
(W.nodeMatrix[np(n, 'old_dy')] - W.nodeMatrix[np(n, 'dy')])
341+
);
342+
343+
traction = Math.sqrt(
344+
(W.nodeMatrix[np(n, 'old_dx')] + W.nodeMatrix[np(n, 'dx')]) *
345+
(W.nodeMatrix[np(n, 'old_dx')] + W.nodeMatrix[np(n, 'dx')]) +
346+
(W.nodeMatrix[np(n, 'old_dy')] + W.nodeMatrix[np(n, 'dy')]) *
347+
(W.nodeMatrix[np(n, 'old_dy')] + W.nodeMatrix[np(n, 'dy')])
348+
) / 2;
349+
350+
nodespeed = W.nodeMatrix[np(n, 'convergence')] *
351+
Math.log(1 + traction) / (1 + Math.sqrt(swinging));
352+
353+
// Updating node convergence
354+
W.nodeMatrix[np(n, 'convergence')] =
355+
Math.min(1, Math.sqrt(
356+
nodespeed *
357+
(Math.pow(W.nodeMatrix[np(n, 'dx')], 2) +
358+
Math.pow(W.nodeMatrix[np(n, 'dy')], 2)) /
359+
(1 + Math.sqrt(swinging))
360+
));
361+
362+
// Updating node's positon
363+
W.nodeMatrix[np(n, 'x')] =
364+
W.nodeMatrix[np(n, 'x')] + W.nodeMatrix[np(n, 'dx')] * nodespeed;
365+
W.nodeMatrix[np(n, 'y')] =
366+
W.nodeMatrix[np(n, 'y')] + W.nodeMatrix[np(n, 'dy')] * nodespeed;
375367
}
376368
}
377369
}
378370

371+
// Counting one more iteration
379372
W.iterations++;
380373
}
381374

0 commit comments

Comments
 (0)