vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.6 KiB

  1. /**
  2. * Calculate the forces the nodes apply on eachother based on a repulsion field.
  3. * This field is linearly approximated.
  4. *
  5. * @private
  6. */
  7. exports._calculateNodeForces = function () {
  8. var dx, dy, distance, fx, fy, combinedClusterSize,
  9. repulsingForce, node1, node2, i, j;
  10. var nodes = this.calculationNodes;
  11. var nodeIndices = this.calculationNodeIndices;
  12. // approximation constants
  13. var b = 5;
  14. var a_base = 0.5 * -b;
  15. // repulsing forces between nodes
  16. var nodeDistance = this.constants.physics.hierarchicalRepulsion.nodeDistance;
  17. var minimumDistance = nodeDistance;
  18. var a = a_base / minimumDistance;
  19. // we loop from i over all but the last entree in the array
  20. // j loops from i+1 to the last. This way we do not double count any of the indices, nor i == j
  21. for (i = 0; i < nodeIndices.length - 1; i++) {
  22. node1 = nodes[nodeIndices[i]];
  23. for (j = i + 1; j < nodeIndices.length; j++) {
  24. node2 = nodes[nodeIndices[j]];
  25. if (node1.level == node2.level) {
  26. dx = node2.x - node1.x;
  27. dy = node2.y - node1.y;
  28. distance = Math.sqrt(dx * dx + dy * dy);
  29. if (distance < 2 * minimumDistance) {
  30. repulsingForce = a * distance + b;
  31. var c = 0.05;
  32. var d = 2 * minimumDistance * 2 * c;
  33. repulsingForce = c * Math.pow(distance,2) - d * distance + d*d/(4*c);
  34. // normalize force with
  35. if (distance == 0) {
  36. distance = 0.01;
  37. }
  38. else {
  39. repulsingForce = repulsingForce / distance;
  40. }
  41. fx = dx * repulsingForce;
  42. fy = dy * repulsingForce;
  43. node1.fx -= fx;
  44. node1.fy -= fy;
  45. node2.fx += fx;
  46. node2.fy += fy;
  47. }
  48. }
  49. }
  50. }
  51. };
  52. /**
  53. * this function calculates the effects of the springs in the case of unsmooth curves.
  54. *
  55. * @private
  56. */
  57. exports._calculateHierarchicalSpringForces = function () {
  58. var edgeLength, edge, edgeId;
  59. var dx, dy, fx, fy, springForce, distance;
  60. var edges = this.edges;
  61. // forces caused by the edges, modelled as springs
  62. for (edgeId in edges) {
  63. if (edges.hasOwnProperty(edgeId)) {
  64. edge = edges[edgeId];
  65. if (edge.connected) {
  66. // only calculate forces if nodes are in the same sector
  67. if (this.nodes.hasOwnProperty(edge.toId) && this.nodes.hasOwnProperty(edge.fromId)) {
  68. edgeLength = edge.customLength ? edge.length : this.constants.physics.springLength;
  69. // this implies that the edges between big clusters are longer
  70. edgeLength += (edge.to.clusterSize + edge.from.clusterSize - 2) * this.constants.clustering.edgeGrowth;
  71. dx = (edge.from.x - edge.to.x);
  72. dy = (edge.from.y - edge.to.y);
  73. distance = Math.sqrt(dx * dx + dy * dy);
  74. if (distance == 0) {
  75. distance = 0.01;
  76. }
  77. distance = Math.max(0.8*edgeLength,Math.min(5*edgeLength, distance));
  78. // the 1/distance is so the fx and fy can be calculated without sine or cosine.
  79. springForce = this.constants.physics.springConstant * (edgeLength - distance) / distance;
  80. fx = dx * springForce;
  81. fy = dy * springForce;
  82. edge.to.fx -= fx;
  83. edge.to.fy -= fy;
  84. edge.from.fx += fx;
  85. edge.from.fy += fy;
  86. var factor = 5;
  87. if (distance > edgeLength) {
  88. factor = 25;
  89. }
  90. if (edge.from.level > edge.to.level) {
  91. edge.to.fx -= factor*fx;
  92. edge.to.fy -= factor*fy;
  93. }
  94. else if (edge.from.level < edge.to.level) {
  95. edge.from.fx += factor*fx;
  96. edge.from.fy += factor*fy;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. };