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.

83 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. var edgeLength, edge;
  17. var edgeIndices = this.physicsBody.physicsEdgeIndices;
  18. var edges = this.body.edges;
  19. // forces caused by the edges, modelled as springs
  20. for (let i = 0; i < edgeIndices.length; i++) {
  21. edge = edges[edgeIndices[i]];
  22. if (edge.connected === true && edge.toId !== edge.fromId) {
  23. // only calculate forces if nodes are in the same sector
  24. if (this.body.nodes[edge.toId] !== undefined && this.body.nodes[edge.fromId] !== undefined) {
  25. if (edge.edgeType.via !== undefined) {
  26. edgeLength = edge.options.length === undefined ? this.options.springLength : edge.options.length;
  27. var node1 = edge.to;
  28. var node2 = edge.edgeType.via;
  29. var node3 = edge.from;
  30. this._calculateSpringForce(node1, node2, 0.5 * edgeLength);
  31. this._calculateSpringForce(node2, node3, 0.5 * edgeLength);
  32. }
  33. else {
  34. // the * 1.5 is here so the edge looks as large as a smooth edge. It does not initially because the smooth edges use
  35. // the support nodes which exert a repulsive force on the to and from nodes, making the edge appear larger.
  36. edgeLength = edge.options.length === undefined ? this.options.springLength * 1.5: edge.options.length;
  37. this._calculateSpringForce(edge.from, edge.to, edgeLength);
  38. }
  39. }
  40. }
  41. }
  42. }
  43. /**
  44. * This is the code actually performing the calculation for the function above.
  45. *
  46. * @param node1
  47. * @param node2
  48. * @param edgeLength
  49. * @private
  50. */
  51. _calculateSpringForce(node1, node2, edgeLength) {
  52. var dx, dy, fx, fy, springForce, distance;
  53. dx = (node1.x - node2.x);
  54. dy = (node1.y - node2.y);
  55. 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. springForce = this.options.springConstant * (edgeLength - distance) / distance;
  59. fx = dx * springForce;
  60. 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;