|
29 | 29 | var W = {
|
30 | 30 | ppn: 10,
|
31 | 31 | ppe: 3,
|
32 |
| - maxRise: 0.5, |
| 32 | + maxForce: 10, |
33 | 33 | iterations: 0,
|
34 | 34 | settings: {
|
35 | 35 | linLogMode: false,
|
|
39 | 39 | scalingRatio: 1,
|
40 | 40 | strongGravityMode: false,
|
41 | 41 | gravity: 1,
|
42 |
| - jitterTolerance: 1, |
43 | 42 | barnesHutOptimize: false,
|
44 | 43 | barnesHutTheta: 1.2,
|
45 |
| - speed: 1, |
46 | 44 | outboundAttCompensation: 1,
|
47 | 45 | totalSwinging: 0,
|
48 | 46 | totalEffectiveTraction: 0,
|
|
197 | 195 | W.nodeMatrix = nodes;
|
198 | 196 | W.edgeMatrix = edges;
|
199 | 197 |
|
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; |
209 | 201 | }
|
210 | 202 |
|
211 | 203 | /**
|
|
214 | 206 |
|
215 | 207 | // MATH: get distances stuff and power 2 issues
|
216 | 208 | function pass() {
|
217 |
| - var l, m, n, n1, n2, e, s, t, w; |
| 209 | + var n, n1, n2, e, s, t, w; |
218 | 210 |
|
219 | 211 | var rootRegion,
|
220 | 212 | outboundAttCompensation;
|
|
224 | 216 | //-----------------------------
|
225 | 217 |
|
226 | 218 | // Resetting positions
|
227 |
| - for (n = 0, l = W.nodes.length; n < l; n++) { |
| 219 | + for (n = 0; n < W.nodesLength; n += W.ppn) { |
228 | 220 | W.nodeMatrix[np(n, 'old_dx')] = W.nodeMatrix[np(n, 'dx')];
|
229 | 221 | W.nodeMatrix[np(n, 'old_dy')] = W.nodeMatrix[np(n, 'dy')];
|
230 | 222 | W.nodeMatrix[np(n, 'dx')] = 0;
|
|
258 | 250 | else {
|
259 | 251 |
|
260 | 252 | // 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) { |
263 | 255 |
|
264 | 256 | // TODO: apply repulsion
|
265 | 257 | }
|
|
269 | 261 |
|
270 | 262 | // 3) Gravity
|
271 | 263 | //------------
|
272 |
| - for (n = 0, l = W.nodes.length; n < l; n++) { |
| 264 | + for (n = 0; n < W.nodesLength; n += W.ppn) { |
273 | 265 |
|
274 | 266 | // TODO: apply gravity
|
275 | 267 | }
|
276 | 268 |
|
277 | 269 |
|
278 | 270 | // 4) Attraction
|
279 | 271 | //---------------
|
280 |
| - for (e = 0, l = W.edges.length; e < l; e++) { |
| 272 | + for (e = 0; e < W.edgesLength; e += W.ppe) { |
281 | 273 | s = W.edgeMatrix[ep(e, 'source')];
|
282 | 274 | t = W.edgeMatrix[ep(e, 'target')];
|
283 | 275 | w = W.edgeMatrix[ep(e, 'weight')];
|
284 | 276 |
|
285 | 277 | // TODO: apply attraction
|
286 | 278 | }
|
287 | 279 |
|
288 |
| - |
289 |
| - // 5) Adjust Speed |
| 280 | + // 5) Apply Forces |
290 | 281 | //-----------------
|
291 |
| - var totalSwinging, totalEffectiveTraction = 0, |
| 282 | + var force, |
292 | 283 | 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; |
327 | 286 |
|
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 |
330 | 288 | if (W.settings.adjustSizes) {
|
331 | 289 |
|
332 |
| - for (n = 0, l = W.nodes.length; n < l; n++) { |
333 |
| - |
| 290 | + for (n = 0; n < W.nodesLength; n += W.ppn) { |
334 | 291 | 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( |
345 | 293 | Math.pow(W.nodeMatrix[np(n, 'dx')], 2) +
|
346 | 294 | Math.pow(W.nodeMatrix[np(n, 'dy')], 2)
|
347 | 295 | );
|
348 | 296 |
|
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; |
354 | 327 | }
|
355 | 328 | }
|
356 | 329 | }
|
357 | 330 | else {
|
358 | 331 |
|
359 |
| - for (n = 0, l = W.nodes.length; n < l; n++) { |
360 |
| - |
| 332 | + for (n = 0; n < W.nodesLength; n += W.ppn) { |
361 | 333 | if (!W.nodeMatrix[np(n, 'fixed')]) {
|
362 | 334 |
|
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; |
375 | 367 | }
|
376 | 368 | }
|
377 | 369 | }
|
378 | 370 |
|
| 371 | + // Counting one more iteration |
379 | 372 | W.iterations++;
|
380 | 373 | }
|
381 | 374 |
|
|
0 commit comments