Skip to content

Commit 7db178b

Browse files
committed
Working implementation of FA2. Tweaks to come
1 parent 02f4600 commit 7db178b

File tree

3 files changed

+181
-20
lines changed

3 files changed

+181
-20
lines changed

examples/transferables.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
var i,
7171
s,
7272
o,
73-
N = 1775,
74-
E = 5232,
73+
N = 1000,
74+
E = 5000,
7575
C = 5,
7676
d = 0.5,
7777
cs = [],
@@ -117,7 +117,7 @@
117117
});
118118
}
119119
}
120-
120+
sigma.renderers.def = sigma.renderers.canvas;
121121
s = new sigma({
122122
graph: g,
123123
container: 'graph-container',

plugins/sigma.layout.forceAtlas2/supervisor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363

6464
// Send data back to worker and loop
6565
_this.sendByteArrayToWorker();
66+
67+
// Rendering graph
68+
_this.sigInst.refresh();
6669
});
6770

6871
// Filling byteArrays
@@ -146,9 +149,6 @@
146149
nodes[j].y = this.nodesByteArray[i + 1];
147150
j++;
148151
}
149-
150-
// Refreshing
151-
this.sigInst.refresh();
152152
};
153153

154154
Supervisor.prototype.sendByteArrayToWorker = function(action) {

plugins/sigma.layout.forceAtlas2/worker.js

Lines changed: 175 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ppe: 3,
3232
maxForce: 10,
3333
iterations: 0,
34+
converged: false,
3435
settings: {
3536
linLogMode: false,
3637
outboundAttractionDistribution: false,
@@ -40,14 +41,13 @@
4041
strongGravityMode: false,
4142
gravity: 1,
4243
barnesHutOptimize: false,
44+
45+
// Are those settings
4346
barnesHutTheta: 1.2,
4447
outboundAttCompensation: 1,
4548
totalSwinging: 0,
4649
totalEffectiveTraction: 0,
4750
speedEfficiency: 1,
48-
complexIntervals: 500,
49-
simpleIntervals: 1000,
50-
converged: false
5151
}
5252
};
5353

@@ -206,12 +206,14 @@
206206

207207
// MATH: get distances stuff and power 2 issues
208208
function pass() {
209-
var n, n1, n2, e, s, t, w;
209+
var n, n1, n2, e, w, g;
210210

211211
var rootRegion,
212212
outboundAttCompensation,
213+
coefficient,
213214
xDist,
214215
yDist,
216+
ewc,
215217
distance,
216218
factor;
217219

@@ -236,11 +238,11 @@
236238
// If outbound attraction distribution, compensate
237239
if (W.settings.outboundAttractionDistribution) {
238240
outboundAttCompensation = 0;
239-
for (n = 0, l = W.nodes.length; n < l; n++) {
241+
for (n = 0; n < W.nodesLength; n += W.ppn) {
240242
outboundAttCompensation += W.nodeMatrix[np(n, 'mass')];
241243
}
242244

243-
outboundAttCompensation /= l;
245+
outboundAttCompensation /= W.nodesLength;
244246
}
245247

246248

@@ -291,8 +293,8 @@
291293
W.nodeMatrix[np(n1, 'dx')] += xDist * factor;
292294
W.nodeMatrix[np(n1, 'dy')] += yDist * factor;
293295

294-
W.nodeMatrix[np(n2, 'dx')] += xDist * factor;
295-
W.nodeMatrix[np(n2, 'dy')] += yDist * factor;
296+
W.nodeMatrix[np(n2, 'dx')] -= xDist * factor;
297+
W.nodeMatrix[np(n2, 'dy')] -= yDist * factor;
296298
}
297299
}
298300
else {
@@ -310,8 +312,8 @@
310312
W.nodeMatrix[np(n1, 'dx')] += xDist * factor;
311313
W.nodeMatrix[np(n1, 'dy')] += yDist * factor;
312314

313-
W.nodeMatrix[np(n2, 'dx')] += xDist * factor;
314-
W.nodeMatrix[np(n2, 'dy')] += yDist * factor;
315+
W.nodeMatrix[np(n2, 'dx')] -= xDist * factor;
316+
W.nodeMatrix[np(n2, 'dy')] -= yDist * factor;
315317
}
316318
}
317319
}
@@ -321,22 +323,181 @@
321323

322324
// 3) Gravity
323325
//------------
326+
g = W.settings.gravity / W.settings.scalingRatio;
327+
coefficient = W.settings.scalingRatio;
324328
for (n = 0; n < W.nodesLength; n += W.ppn) {
325329

326-
// TODO: apply gravity
330+
// Common to both methods
331+
xDist = W.nodeMatrix[np(n, 'x')];
332+
yDist = W.nodeMatrix[np(n, 'y')];
333+
distance = Math.sqrt(
334+
Math.pow(xDist, 2) + Math.pow(yDist, 2)
335+
);
336+
337+
if (W.settings.strongGravityMode) {
338+
339+
//-- Strong gravity
340+
if (distance > 0)
341+
factor = coefficient * W.nodeMatrix[np(n, 'mass')] * g;
342+
}
343+
else {
344+
345+
//-- Linear Anti-collision Repulsion n
346+
if (distance > 0)
347+
factor = coefficient * W.nodeMatrix[np(n, 'mass')] * g / distance;
348+
}
327349
}
328350

351+
// Updating node's dx and dy
352+
W.nodeMatrix[np(n, 'dx')] -= xDist * factor;
353+
W.nodeMatrix[np(n, 'dy')] -= yDist * factor;
354+
329355

330356
// 4) Attraction
331357
//---------------
358+
coefficient = 1 *
359+
(W.settings.outboundAttractionDistribution ?
360+
outboundAttCompensation :
361+
1);
362+
363+
// TODO: simplify distance
364+
// TODO: coefficient is always used as -c --> optimize?
332365
for (e = 0; e < W.edgesLength; e += W.ppe) {
333-
s = W.edgeMatrix[ep(e, 'source')];
334-
t = W.edgeMatrix[ep(e, 'target')];
366+
n1 = W.edgeMatrix[ep(e, 'source')];
367+
n2 = W.edgeMatrix[ep(e, 'target')];
335368
w = W.edgeMatrix[ep(e, 'weight')];
336369

337-
// TODO: apply attraction
370+
// Edge weight influence
371+
if (W.settings.edgeWeightInfluence === 0)
372+
ewc = 1
373+
else if (W.settings.edgeWeightInfluence === 1)
374+
ewc = w;
375+
else
376+
ewc = Math.pow(w, W.settings.edgeWeightInfluence);
377+
378+
// Common measures
379+
xDist = W.nodeMatrix[np(n1, 'x')] - W.nodeMatrix[np(n2, 'x')];
380+
yDist = W.nodeMatrix[np(n1, 'y')] - W.nodeMatrix[np(n2, 'y')];
381+
382+
// Applying attraction to nodes
383+
if (W.settings.adjustSizes) {
384+
if (W.settings.linLogMode) {
385+
if (W.settings.outboundAttractionDistribution) {
386+
387+
//-- LinLog Degree Distributed Anti-collision Attraction
388+
distance = Math.sqrt(
389+
(Math.pow(xDist, 2) + Math.pow(yDist, 2)) -
390+
W.nodeMatrix[np(n1, 'size')] -
391+
W.nodeMatrix[np(n2, 'size')]
392+
);
393+
394+
if (distance > 0) {
395+
factor = -coefficient * ewc * Math.log(1 + distance) /
396+
distance /
397+
W.nodeMatrix[np(n1, 'mass')];
398+
}
399+
}
400+
else {
401+
402+
//-- LinLog Anti-collision Attraction
403+
distance = Math.sqrt(
404+
(Math.pow(xDist, 2) + Math.pow(yDist, 2)) -
405+
W.nodeMatrix[np(n1, 'size')] -
406+
W.nodeMatrix[np(n2, 'size')]
407+
);
408+
409+
if (distance > 0) {
410+
factor = -coefficient * ewc * Math.log(1 + distance) / distance;
411+
}
412+
}
413+
}
414+
else {
415+
if (W.settings.outboundAttractionDistribution) {
416+
417+
//-- Linear Degree Distributed Anti-collision Attraction
418+
distance = Math.sqrt(
419+
(Math.pow(xDist, 2) + Math.pow(yDist, 2)) -
420+
W.nodeMatrix[np(n1, 'size')] -
421+
W.nodeMatrix[np(n2, 'size')]
422+
);
423+
424+
if (distance > 0) {
425+
factor = -coefficient * ewc / W.nodeMatrix[np(n1, 'mass')];
426+
}
427+
}
428+
else {
429+
430+
//-- Linear Anti-collision Attraction
431+
distance = Math.sqrt(
432+
(Math.pow(xDist, 2) + Math.pow(yDist, 2)) -
433+
W.nodeMatrix[np(n1, 'size')] -
434+
W.nodeMatrix[np(n2, 'size')]
435+
);
436+
437+
if (distance > 0) {
438+
factor = -coefficient * ewc;
439+
}
440+
}
441+
}
442+
}
443+
else {
444+
if (W.settings.linLogMode) {
445+
if (W.settings.outboundAttractionDistribution) {
446+
447+
//-- LinLog Degree Distributed Attraction
448+
distance = Math.sqrt(
449+
Math.pow(xDist, 2) + Math.pow(yDist, 2)
450+
);
451+
452+
if (distance > 0) {
453+
factor = -coefficient * ewc * Math.log(1 + distance) /
454+
distance /
455+
W.nodeMatrix[np(n1, 'mass')];
456+
}
457+
}
458+
else {
459+
460+
//-- LinLog Attraction
461+
distance = Math.sqrt(
462+
Math.pow(xDist, 2) + Math.pow(yDist, 2)
463+
);
464+
465+
if (distance > 0)
466+
factor = -coefficient * ewc * Math.log(1 + distance) / distance;
467+
}
468+
}
469+
else {
470+
if (W.settings.outboundAttractionDistribution) {
471+
472+
//-- Linear Attraction Mass Distributed
473+
// NOTE: Distance is set to 1 to override condition
474+
distance = 1;
475+
factor = -coefficient * ewc / W.nodeMatrix[np(n1, 'mass')];
476+
}
477+
else {
478+
479+
//-- Linear Attraction
480+
// NOTE: Distance is set to 1 to override condition
481+
distance = 1;
482+
factor = -coefficient * ewc;
483+
}
484+
}
485+
}
486+
487+
// Updating nodes' dx and dy
488+
// TODO: if condition or factor = 1?
489+
if (distance > 0) {
490+
491+
// Updating nodes' dx and dy
492+
W.nodeMatrix[np(n1, 'dx')] += xDist * factor;
493+
W.nodeMatrix[np(n1, 'dy')] += yDist * factor;
494+
495+
W.nodeMatrix[np(n2, 'dx')] -= xDist * factor;
496+
W.nodeMatrix[np(n2, 'dy')] -= yDist * factor;
497+
}
338498
}
339499

500+
340501
// 5) Apply Forces
341502
//-----------------
342503
var force,

0 commit comments

Comments
 (0)