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.

94 lines
3.0 KiB

  1. /**
  2. * @class SpringSolver
  3. */
  4. class SpringSolver {
  5. /**
  6. * @param {Object} body
  7. * @param {{physicsNodeIndices: Array, physicsEdgeIndices: Array, forces: {}, velocities: {}}} physicsBody
  8. * @param {Object} options
  9. * @constructor SpringSolver
  10. */
  11. constructor(body, physicsBody, options) {
  12. this.body = body;
  13. this.physicsBody = physicsBody;
  14. this.setOptions(options);
  15. }
  16. /**
  17. *
  18. * @param {Object} options
  19. */
  20. setOptions(options) {
  21. this.options = options;
  22. }
  23. /**
  24. * This function calculates the springforces on the nodes, accounting for the support nodes.
  25. *
  26. * @private
  27. */
  28. solve() {
  29. let edgeLength, edge;
  30. let edgeIndices = this.physicsBody.physicsEdgeIndices;
  31. let edges = this.body.edges;
  32. let node1, node2, node3;
  33. // forces caused by the edges, modelled as springs
  34. for (let i = 0; i < edgeIndices.length; i++) {
  35. edge = edges[edgeIndices[i]];
  36. if (edge.connected === true && edge.toId !== edge.fromId) {
  37. // only calculate forces if nodes are in the same sector
  38. if (this.body.nodes[edge.toId] !== undefined && this.body.nodes[edge.fromId] !== undefined) {
  39. if (edge.edgeType.via !== undefined) {
  40. edgeLength = edge.options.length === undefined ? this.options.springLength : edge.options.length;
  41. node1 = edge.to;
  42. node2 = edge.edgeType.via;
  43. node3 = edge.from;
  44. this._calculateSpringForce(node1, node2, 0.5 * edgeLength);
  45. this._calculateSpringForce(node2, node3, 0.5 * edgeLength);
  46. }
  47. else {
  48. // the * 1.5 is here so the edge looks as large as a smooth edge. It does not initially because the smooth edges use
  49. // the support nodes which exert a repulsive force on the to and from nodes, making the edge appear larger.
  50. edgeLength = edge.options.length === undefined ? this.options.springLength * 1.5: edge.options.length;
  51. this._calculateSpringForce(edge.from, edge.to, edgeLength);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * This is the code actually performing the calculation for the function above.
  59. *
  60. * @param {vis.Node} node1
  61. * @param {vis.Node} node2
  62. * @param {number} edgeLength
  63. * @private
  64. */
  65. _calculateSpringForce(node1, node2, edgeLength) {
  66. let dx = (node1.x - node2.x);
  67. let dy = (node1.y - node2.y);
  68. let distance = Math.max(Math.sqrt(dx * dx + dy * dy),0.01);
  69. // the 1/distance is so the fx and fy can be calculated without sine or cosine.
  70. let springForce = this.options.springConstant * (edgeLength - distance) / distance;
  71. let fx = dx * springForce;
  72. let fy = dy * springForce;
  73. // handle the case where one node is not part of the physcis
  74. if (this.physicsBody.forces[node1.id] !== undefined) {
  75. this.physicsBody.forces[node1.id].x += fx;
  76. this.physicsBody.forces[node1.id].y += fy;
  77. }
  78. if (this.physicsBody.forces[node2.id] !== undefined) {
  79. this.physicsBody.forces[node2.id].x -= fx;
  80. this.physicsBody.forces[node2.id].y -= fy;
  81. }
  82. }
  83. }
  84. export default SpringSolver;