Browse Source

small refactoring of force calculation

css_transitions
AlexDM0 10 years ago
parent
commit
107d14a29a
3 changed files with 61 additions and 13 deletions
  1. +31
    -7
      dist/vis.js
  2. +29
    -5
      src/graph/Graph.js
  3. +1
    -1
      src/graph/SectorsMixin.js

+ 31
- 7
dist/vis.js View File

@ -5,7 +5,7 @@
* A dynamic, browser-based visualization library.
*
* @version 0.4.0-SNAPSHOT
* @date 2014-01-21
* @date 2014-01-22
*
* @license
* Copyright (C) 2011-2014 Almende B.V, http://almende.com
@ -10893,7 +10893,7 @@ var SectorMixin = {
/**
* This runs a function in all active sectors. This is used in _redraw() and the _calculateForces().
* This runs a function in all active sectors. This is used in _redraw() and the _initializeForceCalculation().
*
* @param {String} runFunction | This is the NAME of a function we want to call in all active sectors
* | we dont pass the function itself because then the "this" is the window object
@ -12143,7 +12143,7 @@ function Graph (container, data, options) {
this._loadClusterSystem();
// call the sector constructor
this._loadSectorSystem(); // would be fantastic if multiple in heritance just worked!
this._loadSectorSystem();
var graph = this;
this.freezeSimulation = false;// freeze the simulation
@ -13705,7 +13705,7 @@ Graph.prototype._doStabilize = function() {
var vmin = this.constants.minVelocity;
var stable = false;
while (!stable && count < this.constants.maxIterations) {
this._calculateForces();
this._initializeForceCalculation();
this._discreteStepNodes();
stable = !this._isMoving(vmin);
count++;
@ -13716,6 +13716,30 @@ Graph.prototype._doStabilize = function() {
// console.log('Stabilized in ' + (end-start) + ' ms, ' + count + ' iterations' ); // TODO: cleanup
};
/**
* Before calculating the forces, we check if we need to cluster to keep up performance and we check
* if there is more than one node. If it is just one node, we dont calculate anything.
*
* @private
*/
Graph.prototype._initializeForceCalculation = function() {
// stop calculation if there is only one node
if (this.nodeIndices.length == 1) {
this.nodes[this.nodeIndices[0]]._setForce(0,0);
}
else {
// if there are too many nodes on screen, we cluster without repositioning
if (this.nodeIndices.length > this.constants.clustering.absoluteMaxNumberOfNodes && this.constants.clustering.enabled == true) {
this.clusterToFit(this.constants.clustering.reduceToMaxNumberOfNodes, false);
}
// we now start the force calculation
this._calculateForces();
}
};
/**
* Calculate the external forces acting on the nodes
* Forces are caused by: edges, repulsing forces between nodes, gravity
@ -13729,7 +13753,7 @@ Graph.prototype._calculateForces = function() {
// if there are too many nodes on screen, we cluster without repositioning
else if (this.nodeIndices.length > this.constants.clustering.absoluteMaxNumberOfNodes && this.constants.clustering.enabled == true) {
this.clusterToFit(this.constants.clustering.reduceToMaxNumberOfNodes, false);
this._calculateForces();
this._initializeForceCalculation();
}
else {
this.canvasTopLeft = {"x": (0-this.translation.x)/this.scale,
@ -13971,7 +13995,7 @@ Graph.prototype._discreteStepNodes = function() {
Graph.prototype.start = function() {
if (!this.freezeSimulation) {
if (this.moving) {
this._doInAllActiveSectors("_calculateForces");
this._doInAllActiveSectors("_initializeForceCalculation");
this._doInAllActiveSectors("_discreteStepNodes");
}
@ -14003,7 +14027,7 @@ Graph.prototype.start = function() {
Graph.prototype.singleStep = function() {
if (this.moving) {
this._calculateForces();
this._initializeForceCalculation();
this._discreteStepNodes();
var vmin = this.constants.minVelocity;

+ 29
- 5
src/graph/Graph.js View File

@ -91,7 +91,7 @@ function Graph (container, data, options) {
this._loadClusterSystem();
// call the sector constructor
this._loadSectorSystem(); // would be fantastic if multiple in heritance just worked!
this._loadSectorSystem();
var graph = this;
this.freezeSimulation = false;// freeze the simulation
@ -1653,7 +1653,7 @@ Graph.prototype._doStabilize = function() {
var vmin = this.constants.minVelocity;
var stable = false;
while (!stable && count < this.constants.maxIterations) {
this._calculateForces();
this._initializeForceCalculation();
this._discreteStepNodes();
stable = !this._isMoving(vmin);
count++;
@ -1664,6 +1664,30 @@ Graph.prototype._doStabilize = function() {
// console.log('Stabilized in ' + (end-start) + ' ms, ' + count + ' iterations' ); // TODO: cleanup
};
/**
* Before calculating the forces, we check if we need to cluster to keep up performance and we check
* if there is more than one node. If it is just one node, we dont calculate anything.
*
* @private
*/
Graph.prototype._initializeForceCalculation = function() {
// stop calculation if there is only one node
if (this.nodeIndices.length == 1) {
this.nodes[this.nodeIndices[0]]._setForce(0,0);
}
else {
// if there are too many nodes on screen, we cluster without repositioning
if (this.nodeIndices.length > this.constants.clustering.absoluteMaxNumberOfNodes && this.constants.clustering.enabled == true) {
this.clusterToFit(this.constants.clustering.reduceToMaxNumberOfNodes, false);
}
// we now start the force calculation
this._calculateForces();
}
};
/**
* Calculate the external forces acting on the nodes
* Forces are caused by: edges, repulsing forces between nodes, gravity
@ -1677,7 +1701,7 @@ Graph.prototype._calculateForces = function() {
// if there are too many nodes on screen, we cluster without repositioning
else if (this.nodeIndices.length > this.constants.clustering.absoluteMaxNumberOfNodes && this.constants.clustering.enabled == true) {
this.clusterToFit(this.constants.clustering.reduceToMaxNumberOfNodes, false);
this._calculateForces();
this._initializeForceCalculation();
}
else {
this.canvasTopLeft = {"x": (0-this.translation.x)/this.scale,
@ -1919,7 +1943,7 @@ Graph.prototype._discreteStepNodes = function() {
Graph.prototype.start = function() {
if (!this.freezeSimulation) {
if (this.moving) {
this._doInAllActiveSectors("_calculateForces");
this._doInAllActiveSectors("_initializeForceCalculation");
this._doInAllActiveSectors("_discreteStepNodes");
}
@ -1951,7 +1975,7 @@ Graph.prototype.start = function() {
Graph.prototype.singleStep = function() {
if (this.moving) {
this._calculateForces();
this._initializeForceCalculation();
this._discreteStepNodes();
var vmin = this.constants.minVelocity;

+ 1
- 1
src/graph/SectorsMixin.js View File

@ -342,7 +342,7 @@ var SectorMixin = {
/**
* This runs a function in all active sectors. This is used in _redraw() and the _calculateForces().
* This runs a function in all active sectors. This is used in _redraw() and the _initializeForceCalculation().
*
* @param {String} runFunction | This is the NAME of a function we want to call in all active sectors
* | we dont pass the function itself because then the "this" is the window object

Loading…
Cancel
Save