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.

48 lines
1.5 KiB

  1. import BarnesHutSolver from "./BarnesHutSolver"
  2. /**
  3. * @extends BarnesHutSolver
  4. */
  5. class ForceAtlas2BasedRepulsionSolver extends BarnesHutSolver {
  6. /**
  7. * @param {Object} body
  8. * @param {{physicsNodeIndices: Array, physicsEdgeIndices: Array, forces: {}, velocities: {}}} physicsBody
  9. * @param {Object} options
  10. */
  11. constructor(body, physicsBody, options) {
  12. super(body, physicsBody, options);
  13. }
  14. /**
  15. * Calculate the forces based on the distance.
  16. *
  17. * @param {number} distance
  18. * @param {number} dx
  19. * @param {number} dy
  20. * @param {Node} node
  21. * @param {Object} parentBranch
  22. * @private
  23. */
  24. _calculateForces(distance, dx, dy, node, parentBranch) {
  25. if (distance === 0) {
  26. distance = 0.1 * Math.random();
  27. dx = distance;
  28. }
  29. if (this.overlapAvoidanceFactor < 1 && node.shape.radius) {
  30. distance = Math.max(0.1 + (this.overlapAvoidanceFactor * node.shape.radius), distance - node.shape.radius);
  31. }
  32. let degree = (node.edges.length + 1);
  33. // the dividing by the distance cubed instead of squared allows us to get the fx and fy components without sines and cosines
  34. // it is shorthand for gravityforce with distance squared and fx = dx/distance * gravityForce
  35. let gravityForce = this.options.gravitationalConstant * parentBranch.mass * node.options.mass * degree / Math.pow(distance,2);
  36. let fx = dx * gravityForce;
  37. let fy = dy * gravityForce;
  38. this.physicsBody.forces[node.id].x += fx;
  39. this.physicsBody.forces[node.id].y += fy;
  40. }
  41. }
  42. export default ForceAtlas2BasedRepulsionSolver;