Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2007-2013, JGraph Ltd
*/
package com.mxgraph.layout;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;
/**
* An implementation of a simulated annealing layout, based on "Drawing Graphs
* Nicely Using Simulated Annealing" by Davidson and Harel (1996). This
* paper describes these criteria as being favourable in a graph layout: (1)
* distributing nodes evenly, (2) making edge-lengths uniform, (3)
* minimizing cross-crossings, and (4) keeping nodes from coming too close
* to edges. These criteria are translated into energy cost functions in the
* layout. Nodes or edges breaking these criteria create a larger cost function
* , the total cost they contribute related to the extent that they break it.
* The idea of the algorithm is to minimise the total system energy. Factors
* are assigned to each of the criteria describing how important that
* criteria is. Higher factors mean that those criteria are deemed to be
* relatively preferable in the final layout. Most of the criteria conflict
* with the others to some extent and so the setting of the factors determines
* the general look of the resulting graph.
*
* In addition to the four aesthetic criteria the concept of a border line
* which induces an energy cost to nodes in proximity to the graph bounds is
* introduced to attempt to restrain the graph. All of the 5 factors can be
* switched on or off using the isOptimize... variables.
*
* Simulated Annealing is a force-directed layout and is one of the more
* expensive, but generally effective layouts of this type. Layouts like
* the spring layout only really factor in edge length and inter-node
* distance being the lowest CPU intensive for the most aesthetic gain. The
* additional factors are more expensive but can have very attractive results.
*
* The main loop of the algorithm consist of processing the nodes in a
* deterministic order. During the processing of each node a circle of radius
* moveRadius is made around the node and split into
* triesPerCell equal segments. Each point between neighbour
* segments is determined and the new energy of the system if the node were
* moved to that position calculated. Only the necessary nodes and edges are
* processed new energy values resulting in quadratic performance, O(VE),
* whereas calculating the total system energy would be cubic. The default
* implementation only checks 8 points around the radius of the circle, as
* opposed to the suggested 30 in the paper. Doubling the number of points
* double the CPU load and 8 works almost as well as 30.
*
* The moveRadius replaces the temperature as the influencing
* factor in the way the graph settles in later iterations. If the user does
* not set the initial move radius it is set to half the maximum dimension
* of the graph. Thus, in 2 iterations a node may traverse the entire graph,
* and it is more sensible to find minima this way that uphill moves, which
* are little more than an expensive 'tilt' method. The factor by which
* the radius is multiplied by after each iteration is important, lowering
* it improves performance but raising it towards 1.0 can improve the
* resulting graph aesthetics. When the radius hits the minimum move radius
* defined, the layout terminates. The minimum move radius should be set
* a value where the move distance is too minor to be of interest.
*
* Also, the idea of a fine tuning phase is used, as described in the paper.
* This involves only calculating the edge to node distance energy cost
* at the end of the algorithm since it is an expensive calculation and
* it really an 'optimizating' function. fineTuningRadius
* defines the radius value that, when reached, causes the edge to node
* distance to be calculated.
*
* There are other special cases that are processed after each iteration.
* unchangedEnergyRoundTermination defines the number of
* iterations, after which the layout terminates. If nothing is being moved
* it is assumed a good layout has been found. In addition to this if
* no nodes are moved during an iteration the move radius is halved, presuming
* that a finer granularity is required.
*
*/
public class mxOrganicLayout extends mxGraphLayout
{
/**
* Whether or not the distance between edge and nodes will be calculated
* as an energy cost function. This function is CPU intensive and is best
* only used in the fine tuning phase.
*/
protected boolean isOptimizeEdgeDistance = true;
/**
* Whether or not edges crosses will be calculated as an energy cost
* function. This function is CPU intensive, though if some iterations
* without it are required, it is best to have a few cycles at the start
* of the algorithm using it, then use it intermittantly through the rest
* of the layout.
*/
protected boolean isOptimizeEdgeCrossing = true;
/**
* Whether or not edge lengths will be calculated as an energy cost
* function. This function not CPU intensive.
*/
protected boolean isOptimizeEdgeLength = true;
/**
* Whether or not nodes will contribute an energy cost as they approach
* the bound of the graph. The cost increases to a limit close to the
* border and stays constant outside the bounds of the graph. This function
* is not CPU intensive
*/
protected boolean isOptimizeBorderLine = true;
/**
* Whether or not node distribute will contribute an energy cost where
* nodes are close together. The function is moderately CPU intensive.
*/
protected boolean isOptimizeNodeDistribution = true;
/**
* when {@link #moveRadius}reaches this value, the algorithm is terminated
*/
protected double minMoveRadius = 2.0;
/**
* The current radius around each node where the next position energy
* values will be calculated for a possible move
*/
protected double moveRadius;
/**
* The initial value of moveRadius. If this is set to zero
* the layout will automatically determine a suitable value.
*/
protected double initialMoveRadius = 0.0;
/**
* The factor by which the moveRadius is multiplied by after
* every iteration. A value of 0.75 is a good balance between performance
* and aesthetics. Increasing the value provides more chances to find
* minimum energy positions and decreasing it causes the minimum radius
* termination condition to occur more quickly.
*/
protected double radiusScaleFactor = 0.75;
/**
* The average amount of area allocated per node. If bounds
* is not set this value mutiplied by the number of nodes to find
* the total graph area. The graph is assumed square.
*/
protected double averageNodeArea = 160000;
/**
* The radius below which fine tuning of the layout should start
* This involves allowing the distance between nodes and edges to be
* taken into account in the total energy calculation. If this is set to
* zero, the layout will automatically determine a suitable value
*/
protected double fineTuningRadius = 40.0;
/**
* Limit to the number of iterations that may take place. This is only
* reached if one of the termination conditions does not occur first.
*/
protected int maxIterations = 1000;
/**
* Cost factor applied to energy calculations involving the distance
* nodes and edges. Increasing this value tends to cause nodes to move away
* from edges, at the partial cost of other graph aesthetics.
* isOptimizeEdgeDistance must be true for edge to nodes
* distances to be taken into account.
*/
protected double edgeDistanceCostFactor = 3000;
/**
* Cost factor applied to energy calculations involving edges that cross
* over one another. Increasing this value tends to result in fewer edge
* crossings, at the partial cost of other graph aesthetics.
* isOptimizeEdgeCrossing must be true for edge crossings
* to be taken into account.
*/
protected double edgeCrossingCostFactor = 6000;
/**
* Cost factor applied to energy calculations involving the general node
* distribution of the graph. Increasing this value tends to result in
* a better distribution of nodes across the available space, at the
* partial cost of other graph aesthetics.
* isOptimizeNodeDistribution must be true for this general
* distribution to be applied.
*/
protected double nodeDistributionCostFactor = 30000;
/**
* Cost factor applied to energy calculations for node promixity to the
* notional border of the graph. Increasing this value results in
* nodes tending towards the centre of the drawing space, at the
* partial cost of other graph aesthetics.
* isOptimizeBorderLine must be true for border
* repulsion to be applied.
*/
protected double borderLineCostFactor = 5;
/**
* Cost factor applied to energy calculations for the edge lengths.
* Increasing this value results in the layout attempting to shorten all
* edges to the minimum edge length, at the partial cost of other graph
* aesthetics.
* isOptimizeEdgeLength must be true for edge length
* shortening to be applied.
*/
protected double edgeLengthCostFactor = 0.02;
/**
* The x coordinate of the final graph
*/
protected double boundsX = 0.0;
/**
* The y coordinate of the final graph
*/
protected double boundsY = 0.0;
/**
* The width coordinate of the final graph
*/
protected double boundsWidth = 0.0;
/**
* The height coordinate of the final graph
*/
protected double boundsHeight = 0.0;
/**
* current iteration number of the layout
*/
protected int iteration;
/**
* determines, in how many segments the circle around cells is divided, to
* find a new position for the cell. Doubling this value doubles the CPU
* load. Increasing it beyond 16 might mean a change to the
* performRound method might further improve accuracy for a
* small performance hit. The change is described in the method comment.
*/
protected int triesPerCell = 8;
/**
* prevents from dividing with zero and from creating excessive energy
* values
*/
protected double minDistanceLimit = 2;
/**
* cached version of minDistanceLimit squared
*/
protected double minDistanceLimitSquared;
/**
* distance limit beyond which energy costs due to object repulsive is
* not calculated as it would be too insignificant
*/
protected double maxDistanceLimit = 100;
/**
* cached version of maxDistanceLimit squared
*/
protected double maxDistanceLimitSquared;
/**
* Keeps track of how many consecutive round have passed without any energy
* changes
*/
protected int unchangedEnergyRoundCount;
/**
* The number of round of no node moves taking placed that the layout
* terminates
*/
protected int unchangedEnergyRoundTermination = 5;
/**
* Whether or not to use approximate node dimensions or not. Set to true
* the radius squared of the smaller dimension is used. Set to false the
* radiusSquared variable of the CellWrapper contains the width squared
* and heightSquared is used in the obvious manner.
*/
protected boolean approxNodeDimensions = true;
/**
* Internal models collection of nodes ( vertices ) to be laid out
*/
protected CellWrapper[] v;
/**
* Internal models collection of edges to be laid out
*/
protected CellWrapper[] e;
/**
* Array of the x portion of the normalised test vectors that
* are tested for a lower energy around each vertex. The vector
* of the combined x and y normals are multipled by the current
* radius to obtain test points for each vector in the array.
*/
protected double[] xNormTry;
/**
* Array of the y portion of the normalised test vectors that
* are tested for a lower energy around each vertex. The vector
* of the combined x and y normals are multipled by the current
* radius to obtain test points for each vector in the array.
*/
protected double[] yNormTry;
/**
* Whether or not fine tuning is on. The determines whether or not
* node to edge distances are calculated in the total system energy.
* This cost function , besides detecting line intersection, is a
* performance intensive component of this algorithm and best left
* to optimization phase. isFineTuning is switched to
* true if and when the fineTuningRadius
* radius is reached. Switching this variable to true
* before the algorithm runs mean the node to edge cost function
* is always calculated.
*/
protected boolean isFineTuning = true;
/**
* Specifies if the STYLE_NOEDGESTYLE flag should be set on edges that are
* modified by the result. Default is true.
*/
protected boolean disableEdgeStyle = true;
/**
* Specifies if all edge points of traversed edges should be removed.
* Default is true.
*/
protected boolean resetEdges = false;
/**
* Constructor for mxOrganicLayout.
*/
public mxOrganicLayout(mxGraph graph)
{
super(graph);
}
/**
* Constructor for mxOrganicLayout.
*/
public mxOrganicLayout(mxGraph graph, Rectangle2D bounds)
{
super(graph);
boundsX = bounds.getX();
boundsY = bounds.getY();
boundsWidth = bounds.getWidth();
boundsHeight = bounds.getHeight();
}
/**
* Returns true if the given vertex has no connected edges.
*
* @param vertex Object that represents the vertex to be tested.
* @return Returns true if the vertex should be ignored.
*/
public boolean isVertexIgnored(Object vertex)
{
return false;
}
/**
* Implements .
*/
public void execute(Object parent)
{
mxIGraphModel model = graph.getModel();
mxGraphView view = graph.getView();
Object[] vertices = graph.getChildVertices(parent);
HashSet