From e5d7f65124f490354506cf9325358c161f2afc37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Balc=C4=B1?= Date: Wed, 16 Oct 2019 14:06:12 +0300 Subject: [PATCH 1/2] Prevent spring force calculation if edge length is zero --- layout-base.js | 122 +++++++++++++++++++++++---------------------- src/fd/FDLayout.js | 5 +- 2 files changed, 66 insertions(+), 61 deletions(-) diff --git a/layout-base.js b/layout-base.js index 97d3b6a..8c9f565 100644 --- a/layout-base.js +++ b/layout-base.js @@ -85,13 +85,13 @@ return /******/ (function(modules) { // webpackBootstrap function LayoutConstants() {} -/** - * Layout Quality: 0:draft, 1:default, 2:proof +/** + * Layout Quality: 0:draft, 1:default, 2:proof */ LayoutConstants.QUALITY = 1; -/** - * Default parameters +/** + * Default parameters */ LayoutConstants.DEFAULT_CREATE_BENDS_AS_NEEDED = false; LayoutConstants.DEFAULT_INCREMENTAL = false; @@ -103,50 +103,50 @@ LayoutConstants.DEFAULT_UNIFORM_LEAF_NODE_SIZES = false; // ----------------------------------------------------------------------------- // Section: General other constants // ----------------------------------------------------------------------------- -/* - * Margins of a graph to be applied on bouding rectangle of its contents. We - * assume margins on all four sides to be uniform. +/* + * Margins of a graph to be applied on bouding rectangle of its contents. We + * assume margins on all four sides to be uniform. */ LayoutConstants.DEFAULT_GRAPH_MARGIN = 15; -/* - * Whether to consider labels in node dimensions or not +/* + * Whether to consider labels in node dimensions or not */ LayoutConstants.NODE_DIMENSIONS_INCLUDE_LABELS = false; -/* - * Default dimension of a non-compound node. +/* + * Default dimension of a non-compound node. */ LayoutConstants.SIMPLE_NODE_SIZE = 40; -/* - * Default dimension of a non-compound node. +/* + * Default dimension of a non-compound node. */ LayoutConstants.SIMPLE_NODE_HALF_SIZE = LayoutConstants.SIMPLE_NODE_SIZE / 2; -/* - * Empty compound node size. When a compound node is empty, its both - * dimensions should be of this value. +/* + * Empty compound node size. When a compound node is empty, its both + * dimensions should be of this value. */ LayoutConstants.EMPTY_COMPOUND_NODE_SIZE = 40; -/* - * Minimum length that an edge should take during layout +/* + * Minimum length that an edge should take during layout */ LayoutConstants.MIN_EDGE_LENGTH = 1; -/* - * World boundaries that layout operates on +/* + * World boundaries that layout operates on */ LayoutConstants.WORLD_BOUNDARY = 1000000; -/* - * World boundaries that random positioning can be performed with +/* + * World boundaries that random positioning can be performed with */ LayoutConstants.INITIAL_WORLD_BOUNDARY = LayoutConstants.WORLD_BOUNDARY / 1000; -/* - * Coordinates of the world center +/* + * Coordinates of the world center */ LayoutConstants.WORLD_CENTER_X = 1200; LayoutConstants.WORLD_CENTER_Y = 900; @@ -2314,8 +2314,8 @@ module.exports = LinkedList; "use strict"; -/* - *This class is the javascript implementation of the Point.java class in jdk +/* + *This class is the javascript implementation of the Point.java class in jdk */ function Point(x, y, p) { this.x = null; @@ -2569,16 +2569,16 @@ function Layout(isRemoteUse) { this.animationDuringLayout = LayoutConstants.DEFAULT_ANIMATION_DURING_LAYOUT; //Number iterations that should be done between two successive animations this.animationPeriod = LayoutConstants.DEFAULT_ANIMATION_PERIOD; - /** - * Whether or not leaf nodes (non-compound nodes) are of uniform sizes. When - * they are, both spring and repulsion forces between two leaf nodes can be - * calculated without the expensive clipping point calculations, resulting - * in major speed-up. + /** + * Whether or not leaf nodes (non-compound nodes) are of uniform sizes. When + * they are, both spring and repulsion forces between two leaf nodes can be + * calculated without the expensive clipping point calculations, resulting + * in major speed-up. */ this.uniformLeafNodeSizes = LayoutConstants.DEFAULT_UNIFORM_LEAF_NODE_SIZES; - /** - * This is used for creation of bendpoints by using dummy nodes and edges. - * Maps an LEdge to its dummy bendpoint path. + /** + * This is used for creation of bendpoints by using dummy nodes and edges. + * Maps an LEdge to its dummy bendpoint path. */ this.edgeToDummyNodes = new Map(); this.graphManager = new LGraphManager(this); @@ -2670,8 +2670,8 @@ Layout.prototype.runLayout = function () { return isLayoutSuccessfull; }; -/** - * This method performs the operations required after layout. +/** + * This method performs the operations required after layout. */ Layout.prototype.doPostLayout = function () { //assert !isSubLayout : "Should not be called on sub-layout!"; @@ -2682,9 +2682,9 @@ Layout.prototype.doPostLayout = function () { this.update(); }; -/** - * This method updates the geometry of the target graph according to - * calculated layout. +/** + * This method updates the geometry of the target graph according to + * calculated layout. */ Layout.prototype.update2 = function () { // update bend points @@ -2771,9 +2771,9 @@ Layout.prototype.update = function (obj) { } }; -/** - * This method is used to set all layout parameters to default values - * determined at compile time. +/** + * This method is used to set all layout parameters to default values + * determined at compile time. */ Layout.prototype.initParameters = function () { if (!this.isSubLayout) { @@ -2848,11 +2848,11 @@ Layout.prototype.positionNodesRandomly = function (graph) { } }; -/** - * This method returns a list of trees where each tree is represented as a - * list of l-nodes. The method returns a list of size 0 when: - * - The graph is not flat or - * - One of the component(s) of the graph is not a tree. +/** + * This method returns a list of trees where each tree is represented as a + * list of l-nodes. The method returns a list of size 0 when: + * - The graph is not flat or + * - One of the component(s) of the graph is not a tree. */ Layout.prototype.getFlatForest = function () { var flatForest = []; @@ -2953,10 +2953,10 @@ Layout.prototype.getFlatForest = function () { return flatForest; }; -/** - * This method creates dummy nodes (an l-level node with minimal dimensions) - * for the given edge (one per bendpoint). The existing l-level structure - * is updated accordingly. +/** + * This method creates dummy nodes (an l-level node with minimal dimensions) + * for the given edge (one per bendpoint). The existing l-level structure + * is updated accordingly. */ Layout.prototype.createDummyNodesForBendpoints = function (edge) { var dummyNodes = []; @@ -2996,9 +2996,9 @@ Layout.prototype.createDummyNodesForBendpoints = function (edge) { return dummyNodes; }; -/** - * This method creates bendpoints for edges from the dummy nodes - * at l-level. +/** + * This method creates bendpoints for edges from the dummy nodes + * at l-level. */ Layout.prototype.createBendpointsFromDummyNodes = function () { var edges = []; @@ -3059,9 +3059,9 @@ Layout.transform = function (sliderValue, defaultValue, minDiv, maxMul) { } }; -/** - * This method finds and returns the center of the given nodes, assuming - * that the given nodes form a tree in themselves. +/** + * This method finds and returns the center of the given nodes, assuming + * that the given nodes form a tree in themselves. */ Layout.findCenterOfTree = function (nodes) { var list = []; @@ -3130,9 +3130,9 @@ Layout.findCenterOfTree = function (nodes) { return centerNode; }; -/** - * During the coarsening process, this layout may be referenced by two graph managers - * this setter function grants access to change the currently being used graph manager +/** + * During the coarsening process, this layout may be referenced by two graph managers + * this setter function grants access to change the currently being used graph manager */ Layout.prototype.setGraphManager = function (gm) { this.graphManager = gm; @@ -3491,6 +3491,8 @@ FDLayout.prototype.calcSpringForce = function (edge, idealLength) { length = edge.getLength(); + if (length == 0) return; + // Calculate spring forces springForce = this.springConstant * (length - idealLength); diff --git a/src/fd/FDLayout.js b/src/fd/FDLayout.js index 8149d7e..9fd986e 100644 --- a/src/fd/FDLayout.js +++ b/src/fd/FDLayout.js @@ -215,7 +215,10 @@ FDLayout.prototype.calcSpringForce = function (edge, idealLength) { } length = edge.getLength(); - + + if(length == 0) + return; + // Calculate spring forces springForce = this.springConstant * (length - idealLength); From 5b236a45e17f610ba38cac8571b0cbdb73b818f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Balc=C4=B1?= Date: Tue, 22 Oct 2019 10:36:02 +0300 Subject: [PATCH 2/2] Add reference to random seed code --- layout-base.js | 1 + src/util/RandomSeed.js | 1 + 2 files changed, 2 insertions(+) diff --git a/layout-base.js b/layout-base.js index 8c9f565..20e3d63 100644 --- a/layout-base.js +++ b/layout-base.js @@ -3148,6 +3148,7 @@ module.exports = Layout; function RandomSeed() {} +// adapted from: https://stackoverflow.com/a/19303725 RandomSeed.seed = 1; RandomSeed.x = 0; diff --git a/src/util/RandomSeed.js b/src/util/RandomSeed.js index 149889c..f71f40f 100644 --- a/src/util/RandomSeed.js +++ b/src/util/RandomSeed.js @@ -1,5 +1,6 @@ function RandomSeed() { } +// adapted from: https://stackoverflow.com/a/19303725 RandomSeed.seed = 1; RandomSeed.x = 0;