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.

105 lines
3.4 KiB

  1. /**
  2. * Created by Alex on 2/23/2015.
  3. */
  4. import {BarnesHutSolver} from "./components/physics/BarnesHutSolver";
  5. // TODO Create
  6. //import {Repulsion} from "./components/physics/Repulsion";
  7. //import {HierarchicalRepulsion} from "./components/physics/HierarchicalRepulsion";
  8. import {SpringSolver} from "./components/physics/SpringSolver";
  9. // TODO Create
  10. //import {HierarchicalSpringSolver} from "./components/physics/HierarchicalSpringSolver";
  11. import {CentralGravitySolver} from "./components/physics/CentralGravitySolver";
  12. class PhysicsEngine {
  13. constructor(body, options) {
  14. this.body = body;
  15. this.physicsBody = {calculationNodes: {}, calculationNodeIndices:[]};
  16. this.setOptions(options);
  17. }
  18. setOptions(options) {
  19. if (options !== undefined) {
  20. this.options = options;
  21. this.init();
  22. }
  23. }
  24. init() {
  25. var options;
  26. if (this.options.model == "repulsion") {
  27. options = this.options.repulsion;
  28. // TODO uncomment when created
  29. //this.nodesSolver = new Repulsion(this.body, this.physicsBody, options);
  30. //this.edgesSolver = new SpringSolver(this.body, options);
  31. }
  32. else if (this.options.model == "hierarchicalRepulsion") {
  33. options = this.options.hierarchicalRepulsion;
  34. // TODO uncomment when created
  35. //this.nodesSolver = new HierarchicalRepulsion(this.body, this.physicsBody, options);
  36. //this.edgesSolver = new HierarchicalSpringSolver(this.body, options);
  37. }
  38. else { // barnesHut
  39. options = this.options.barnesHut;
  40. this.nodesSolver = new BarnesHutSolver(this.body, this.physicsBody, options);
  41. this.edgesSolver = new SpringSolver(this.body, options);
  42. }
  43. this.gravitySolver = new CentralGravitySolver(this.body, this.physicsBody, options);
  44. }
  45. /**
  46. * Smooth curves are created by adding invisible nodes in the center of the edges. These nodes are also
  47. * handled in the calculateForces function. We then use a quadratic curve with the center node as control.
  48. * This function joins the datanodes and invisible (called support) nodes into one object.
  49. * We do this so we do not contaminate this.body.nodes with the support nodes.
  50. *
  51. * @private
  52. */
  53. _updateCalculationNodes() {
  54. this.physicsBody.calculationNodes = {};
  55. this.physicsBody.calculationNodeIndices = [];
  56. for (let i = 0; i < this.body.nodeIndices.length; i++) {
  57. let nodeId = this.body.nodeIndices[i];
  58. this.physicsBody.calculationNodes[nodeId] = this.body.nodes[nodeId];
  59. }
  60. // if support nodes are used, we have them here
  61. var supportNodes = this.body.supportNodes;
  62. for (let i = 0; i < this.body.supportNodeIndices.length; i++) {
  63. let supportNodeId = this.body.supportNodeIndices[i];
  64. if (this.body.edges[supportNodes[supportNodeId].parentEdgeId] !== undefined) {
  65. this.physicsBody.calculationNodes[supportNodeId] = supportNodes[supportNodeId];
  66. }
  67. else {
  68. console.error("Support node detected that does not have an edge!")
  69. }
  70. }
  71. console.log('here', this.body)
  72. this.physicsBody.calculationNodeIndices = Object.keys(this.physicsBody.calculationNodes);
  73. }
  74. calculateField() {
  75. this.nodesSolver.solve();
  76. }
  77. calculateSprings() {
  78. this.edgesSolver.solve();
  79. }
  80. calculateCentralGravity() {
  81. this.gravitySolver.solve();
  82. }
  83. step() {
  84. this.calculateCentralGravity();
  85. this.calculateField();
  86. this.calculateSprings();
  87. }
  88. }
  89. export {PhysicsEngine};