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.

103 lines
3.0 KiB

  1. /**
  2. * Created by Alex on 2/25/2015.
  3. */
  4. class HierarchicalSpringSolver {
  5. constructor(body, physicsBody, options) {
  6. this.body = body;
  7. this.physicsBody = physicsBody;
  8. this.setOptions(options);
  9. }
  10. setOptions(options) {
  11. this.options = options;
  12. }
  13. /**
  14. * This function calculates the springforces on the nodes, accounting for the support nodes.
  15. *
  16. * @private
  17. */
  18. solve() {
  19. var edgeLength, edge, edgeId;
  20. var dx, dy, fx, fy, springForce, distance;
  21. var edges = this.body.edges;
  22. var nodeIndices = this.physicsBody.calculationNodeIndices;
  23. var forces = this.physicsBody.forces;
  24. // initialize the spring force counters
  25. for (let i = 0; i < nodeIndices.length; i++) {
  26. let nodeId = nodeIndices[i];
  27. forces[nodeId].springFx = 0;
  28. forces[nodeId].springFy = 0;
  29. }
  30. // forces caused by the edges, modelled as springs
  31. for (edgeId in edges) {
  32. if (edges.hasOwnProperty(edgeId)) {
  33. edge = edges[edgeId];
  34. if (edge.connected === true) {
  35. edgeLength = edge.properties.length === undefined ? this.options.springLength : edge.properties.length;
  36. dx = (edge.from.x - edge.to.x);
  37. dy = (edge.from.y - edge.to.y);
  38. distance = Math.sqrt(dx * dx + dy * dy);
  39. distance = distance == 0 ? 0.01 : distance;
  40. // the 1/distance is so the fx and fy can be calculated without sine or cosine.
  41. springForce = this.options.springConstant * (edgeLength - distance) / distance;
  42. fx = dx * springForce;
  43. fy = dy * springForce;
  44. if (edge.to.level != edge.from.level) {
  45. forces[edge.toId].springFx -= fx;
  46. forces[edge.toId].springFy -= fy;
  47. forces[edge.fromId].springFx += fx;
  48. forces[edge.fromId].springFy += fy;
  49. }
  50. else {
  51. let factor = 0.5;
  52. forces[edge.toId].x -= factor*fx;
  53. forces[edge.toId].y -= factor*fy;
  54. forces[edge.fromId].x += factor*fx;
  55. forces[edge.fromId].y += factor*fy;
  56. }
  57. }
  58. }
  59. }
  60. // normalize spring forces
  61. var springForce = 1;
  62. var springFx, springFy;
  63. for (let i = 0; i < nodeIndices.length; i++) {
  64. let nodeId = nodeIndices[i];
  65. springFx = Math.min(springForce,Math.max(-springForce,forces[nodeId].springFx));
  66. springFy = Math.min(springForce,Math.max(-springForce,forces[nodeId].springFy));
  67. forces[nodeId].x += springFx;
  68. forces[nodeId].y += springFy;
  69. }
  70. // retain energy balance
  71. var totalFx = 0;
  72. var totalFy = 0;
  73. for (let i = 0; i < nodeIndices.length; i++) {
  74. let nodeId = nodeIndices[i];
  75. totalFx += forces[nodeId].x;
  76. totalFy += forces[nodeId].y;
  77. }
  78. var correctionFx = totalFx / nodeIndices.length;
  79. var correctionFy = totalFy / nodeIndices.length;
  80. for (let i = 0; i < nodeIndices.length; i++) {
  81. let nodeId = nodeIndices[i];
  82. forces[nodeId].x -= correctionFx;
  83. forces[nodeId].y -= correctionFy;
  84. }
  85. }
  86. }
  87. export {HierarchicalSpringSolver};