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.

81 lines
2.8 KiB

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