ForceAtlas2.js
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import Texture2D from 'claygl/src/Texture2D';
import Texture from 'claygl/src/Texture';
import workerFunc from './forceAtlas2Worker.js';
var workerUrl = workerFunc.toString();
workerUrl = workerUrl.slice(workerUrl.indexOf('{') + 1, workerUrl.lastIndexOf('}'));
var defaultConfigs = {
barnesHutOptimize: true,
barnesHutTheta: 1.5,
repulsionByDegree: true,
linLogMode: false,
strongGravityMode: false,
gravity: 1.0,
scaling: 1.0,
edgeWeightInfluence: 1.0,
jitterTolerence: 0.1,
preventOverlap: false,
dissuadeHubs: false,
gravityCenter: null
};
var ForceAtlas2 = function (options) {
for (var name in defaultConfigs) {
this[name] = defaultConfigs[name];
}
if (options) {
for (var name in options) {
this[name] = options[name];
}
}
this._nodes = [];
this._edges = [];
this._disposed = false;
this._positionTex = new Texture2D({
type: Texture.FLOAT,
flipY: false,
minFilter: Texture.NEAREST,
magFilter: Texture.NEAREST
});
};
ForceAtlas2.prototype.initData = function (nodes, edges) {
var bb = new Blob([workerUrl]);
var blobURL = window.URL.createObjectURL(bb);
this._worker = new Worker(blobURL);
this._worker.onmessage = this._$onupdate.bind(this);
this._nodes = nodes;
this._edges = edges;
this._frame = 0;
var nNodes = nodes.length;
var nEdges = edges.length;
var positionArr = new Float32Array(nNodes * 2);
var massArr = new Float32Array(nNodes);
var sizeArr = new Float32Array(nNodes);
var edgeArr = new Float32Array(nEdges * 2);
var edgeWeightArr = new Float32Array(nEdges);
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
positionArr[i * 2] = node.x;
positionArr[i * 2 + 1] = node.y;
massArr[i] = node.mass == null ? 1 : node.mass;
sizeArr[i] = node.size == null ? 1 : node.size;
}
for (var i = 0; i < edges.length; i++) {
var edge = edges[i];
var source = edge.node1;
var target = edge.node2;
edgeArr[i * 2] = source;
edgeArr[i * 2 + 1] = target;
edgeWeightArr[i] = edge.weight == null ? 1 : edge.weight;
}
var textureWidth = Math.ceil(Math.sqrt(nodes.length));
var textureHeight = textureWidth;
var pixels = new Float32Array(textureWidth * textureHeight * 4);
var positionTex = this._positionTex;
positionTex.width = textureWidth;
positionTex.height = textureHeight;
positionTex.pixels = pixels;
this._worker.postMessage({
cmd: 'init',
nodesPosition: positionArr,
nodesMass: massArr,
nodesSize: sizeArr,
edges: edgeArr,
edgesWeight: edgeWeightArr
});
this._globalSpeed = Infinity;
};
ForceAtlas2.prototype.updateOption = function (options) {
var config = {};
// Default config
for (var name in defaultConfigs) {
config[name] = defaultConfigs[name];
}
var nodes = this._nodes;
var edges = this._edges;
// Config according to data scale
var nNodes = nodes.length;
if (nNodes > 50000) {
config.jitterTolerence = 10;
}
else if (nNodes > 5000) {
config.jitterTolerence = 1;
}
else {
config.jitterTolerence = 0.1;
}
if (nNodes > 100) {
config.scaling = 2.0;
}
else {
config.scaling = 10.0;
}
if (nNodes > 1000) {
config.barnesHutOptimize = true;
}
else {
config.barnesHutOptimize = false;
}
if (options) {
for (var name in defaultConfigs) {
if (options[name] != null) {
config[name] = options[name];
}
}
}
if (!config.gravityCenter) {
var min = [Infinity, Infinity];
var max = [-Infinity, -Infinity];
for (var i = 0; i < nodes.length; i++) {
min[0] = Math.min(nodes[i].x, min[0]);
min[1] = Math.min(nodes[i].y, min[1]);
max[0] = Math.max(nodes[i].x, max[0]);
max[1] = Math.max(nodes[i].y, max[1]);
}
config.gravityCenter = [(min[0] + max[0]) * 0.5, (min[1] + max[1]) * 0.5];
}
// Update inDegree, outDegree
for (var i = 0; i < edges.length; i++) {
var node1 = edges[i].node1;
var node2 = edges[i].node2;
nodes[node1].degree = (nodes[node1].degree || 0) + 1;
nodes[node2].degree = (nodes[node2].degree || 0) + 1;
}
if (this._worker) {
this._worker.postMessage({
cmd: 'updateConfig',
config: config
});
}
};
// Steps per call, to keep sync with rendering
ForceAtlas2.prototype.update = function (renderer, steps, cb) {
if (steps == null) {
steps = 1;
}
steps = Math.max(steps, 1);
this._frame += steps;
this._onupdate = cb;
if (this._worker) {
this._worker.postMessage({
cmd: 'update',
steps: Math.round(steps)
});
}
};
ForceAtlas2.prototype._$onupdate = function (e) {
// Incase the worker keep postMessage of last frame after it is disposed
if (this._disposed) {
return;
}
var positionArr = new Float32Array(e.data.buffer);
this._globalSpeed = e.data.globalSpeed;
this._positionArr = positionArr;
this._updateTexture(positionArr);
this._onupdate && this._onupdate();
};
ForceAtlas2.prototype.getNodePositionTexture = function () {
return this._positionTex;
};
ForceAtlas2.prototype.getNodeUV = function (nodeIndex, uv) {
uv = uv || [];
var textureWidth = this._positionTex.width;
var textureHeight = this._positionTex.height;
uv[0] = (nodeIndex % textureWidth) / (textureWidth - 1);
uv[1] = Math.floor(nodeIndex / textureWidth) / (textureHeight - 1);
return uv;
};
ForceAtlas2.prototype.getNodes = function () {
return this._nodes;
};
ForceAtlas2.prototype.getEdges = function () {
return this._edges;
};
ForceAtlas2.prototype.isFinished = function (maxSteps) {
return this._frame > maxSteps;
};
ForceAtlas2.prototype.getNodePosition = function (renderer, out) {
if (!out) {
out = new Float32Array(this._nodes.length * 2);
}
if (this._positionArr) {
for (var i = 0; i < this._positionArr.length; i++) {
out[i] = this._positionArr[i];
}
}
return out;
};
ForceAtlas2.prototype._updateTexture = function (positionArr) {
var pixels = this._positionTex.pixels;
var offset = 0;
for (var i = 0; i < positionArr.length;){
pixels[offset++] = positionArr[i++];
pixels[offset++] = positionArr[i++];
pixels[offset++] = 1;
pixels[offset++] = 1;
}
this._positionTex.dirty();
};
ForceAtlas2.prototype.dispose = function (renderer) {
this._disposed = true;
this._worker = null;
};
export default ForceAtlas2;