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.

98 lines
3.0 KiB

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