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.

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