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 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 node1
  48. * @param node2
  49. * @param 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.sqrt(dx * dx + dy * dy);
  56. distance = distance === 0 ? 0.01 : distance;
  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;