Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

SpringEmbedderBase.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Array.h>
35 #include <ogdf/basic/Graph.h>
37 #include <ogdf/basic/GraphCopy.h>
38 #include <ogdf/basic/GraphList.h>
41 #include <ogdf/basic/List.h>
42 #include <ogdf/basic/System.h>
43 #include <ogdf/basic/geometry.h>
47 
48 #include <cmath>
49 
50 namespace ogdf {
51 namespace spring_embedder {
52 
55 public:
57  enum class Scaling {
58  input,
62  };
63 
66  // default parameters
67  m_iterations = 400;
68  m_iterationsImprove = 200;
69  m_coolDownFactor = 0.999;
70  m_forceLimitStep = 0.25;
71 
72  m_boundingBox = DRect(0, 0, 250, 250);
73  m_noise = true;
74 
77 
80 
82  m_scaleFactor = 4.0;
83 
84  m_userBoundingBox = DRect(0, 0, 100, 100);
85 
87  m_pageRatio = 1.0;
88 
90 
91  double def_nw = LayoutStandards::defaultNodeWidth();
92  double def_nh = LayoutStandards::defaultNodeHeight();
94  LayoutStandards::defaultNodeSeparation() + sqrt(def_nw * def_nw + def_nh * def_nh);
95  }
96 
97  virtual void call(GraphAttributes& GA) override {
98  const Graph& G = GA.constGraph();
99  if (G.empty()) {
100  return;
101  }
102 
103  // all edges straight-line
104  GA.clearAllBends();
105 
106  GraphCopy GC;
107  GC.setOriginalGraph(G);
108 
109  // compute connected component of G
110  NodeArray<int> component(G);
111  int numCC = connectedComponents(G, component);
112 
113  // intialize the array of lists of nodes contained in a CC
114  Array<List<node>> nodesInCC(numCC);
115 
116  for (node v : G.nodes) {
117  nodesInCC[component[v]].pushBack(v);
118  }
119 
120  NodeArray<node> nodeCopy(G);
121  EdgeArray<edge> auxCopy(G);
122  Array<DPoint> boundingBox(numCC);
123 
124  for (int i = 0; i < numCC; ++i) {
125  nodeCopy.init(G);
126  auxCopy.init(G);
127  GC.clear();
128  GC.insert(nodesInCC[i].begin(), nodesInCC[i].end(), filter_any_edge, nodeCopy, auxCopy);
130 
131  const int n = GC.numberOfNodes();
132 
133  // special case: just one node
134  if (n == 1) {
135  node vOrig = GC.original(GC.firstNode());
136  GA.x(vOrig) = GA.y(vOrig) = 0;
137  boundingBox[i] = DPoint(0, 0);
138  continue;
139  }
140 
141  callMaster(GC, GA, boundingBox[i]);
142  //std::cout << "avg. force: " << master.avgDisplacement() << std::endl;
143  //std::cout << "max. force: " << master.maxDisplacement() << std::endl;
144  }
145 
146  Array<DPoint> offset(numCC);
147  TileToRowsCCPacker packer;
148  packer.call(boundingBox, offset, m_pageRatio);
149 
150  // The arrangement is given by offset to the origin of the coordinate
151  // system. We still have to shift each node and edge by the offset
152  // of its connected component.
153 
154  for (int i = 0; i < numCC; ++i) {
155  const List<node>& nodes = nodesInCC[i];
156 
157  const double dx = offset[i].m_x;
158  const double dy = offset[i].m_y;
159 
160  // iterate over all nodes in ith CC
162  for (node v : nodes) {
163  GA.x(v) += dx;
164  GA.y(v) += dy;
165  }
166  }
167  }
168 
171 
174 
177 
180 
182 
187  double avgConvergenceFactor() const { return m_avgConvergenceFactor; }
188 
190  void avgConvergenceFactor(double f) {
191  if (f >= 0) {
193  }
194  }
195 
197 
202  double maxConvergenceFactor() const { return m_maxConvergenceFactor; }
203 
205  void maxConvergenceFactor(double f) {
206  if (f >= 0) {
208  }
209  }
210 
212 
216  int iterations() const { return m_iterations; }
217 
219  void iterations(int i) {
220  if (i >= 0) {
221  m_iterations = i;
222  }
223  }
224 
226  int iterationsImprove() const { return m_iterationsImprove; }
227 
229  void iterationsImprove(int i) {
230  if (i >= 0) {
232  }
233  }
234 
235  double coolDownFactor() const { return m_coolDownFactor; }
236 
237  double forceLimitStep() const { return m_forceLimitStep; }
238 
240  double idealEdgeLength() const { return m_idealEdgeLength; }
241 
243 
247  void idealEdgeLength(double len) { m_idealEdgeLength = len; }
248 
250  bool noise() const { return m_noise; }
251 
253  void noise(bool on) { m_noise = on; }
254 
256  double minDistCC() const { return m_minDistCC; }
257 
259  void minDistCC(double x) { m_minDistCC = x; }
260 
262  double pageRatio() { return m_pageRatio; }
263 
265  void pageRatio(double x) { m_pageRatio = x; }
266 
268  Scaling scaling() const { return m_scaling; }
269 
271  void scaling(Scaling sc) { m_scaling = sc; }
272 
274  double scaleFunctionFactor() const { return m_scaleFactor; }
275 
277  void scaleFunctionFactor(double f) { m_scaleFactor = f; }
278 
280  void userBoundingBox(double xmin, double ymin, double xmax, double ymax) {
281  m_userBoundingBox = DRect(xmin, ymin, xmax, ymax);
282  }
283 
286 
288  unsigned int maxThreads() const { return m_maxThreads; }
289 
291  void maxThreads(unsigned int n) { m_maxThreads = n; }
292 
293 protected:
294  virtual void callMaster(const GraphCopy& copy, GraphAttributes& attr, DPoint& box) = 0;
295 
301 
303 
306  bool m_noise;
307 
309  double m_scaleFactor;
310 
312 
313  double m_minDistCC;
314  double m_pageRatio;
315 
318 
319  unsigned int m_maxThreads;
320 };
321 
322 }
323 }
ogdf::spring_embedder::SpringEmbedderBase::coolDownFactor
double coolDownFactor() const
Definition: SpringEmbedderBase.h:235
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::GraphAttributes
Stores additional attributes of a graph (like layout information).
Definition: GraphAttributes.h:72
ogdf::spring_embedder::SpringEmbedderBase::maxConvergenceFactor
void maxConvergenceFactor(double f)
Sets the maximum convergence factor to f.
Definition: SpringEmbedderBase.h:205
GraphAttributes.h
Declaration of class GraphAttributes which extends a Graph by additional attributes.
ogdf::spring_embedder::SpringEmbedderBase::m_userBoundingBox
DRect m_userBoundingBox
Definition: SpringEmbedderBase.h:311
ogdf::spring_embedder::SpringEmbedderBase::noise
bool noise() const
Returns the current setting of noise.
Definition: SpringEmbedderBase.h:250
ogdf::spring_embedder::SpringEmbedderBase::idealEdgeLength
void idealEdgeLength(double len)
Sets the ideal edge length to len.
Definition: SpringEmbedderBase.h:247
SpringForceModel.h
Declaration of SpringForceModel enumeration.
ogdf::spring_embedder::SpringEmbedderBase::Scaling::scaleFunction
@ scaleFunction
automatic scaling is used with parameter set by scaleFunctionFactor() (larger factor,...
Graph.h
Includes declaration of graph class.
ogdf::GenericPoint< double >
ogdf::connectedComponents
int connectedComponents(const Graph &G, NodeArray< int > &component, List< node > *isolated=nullptr, ArrayBuffer< node > *reprs=nullptr)
Computes the connected components of G and optionally generates a list of isolated nodes.
geometry.h
Declaration of classes GenericPoint, GenericPolyline, GenericLine, GenericSegment,...
ogdf::GraphCopy::setOriginalGraph
void setOriginalGraph(const Graph *G) override
Associates the graph copy with G, but does not create any nodes or edges.
System.h
Decalration of System class which provides unified access to system information.
ogdf::begin
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
ogdf::spring_embedder::SpringEmbedderBase::maxConvergenceFactor
double maxConvergenceFactor() const
Returns the currently used maximum convergence factor.
Definition: SpringEmbedderBase.h:202
ogdf::GraphAttributes::x
double x(node v) const
Returns the x-coordinate of node v.
Definition: GraphAttributes.h:247
ogdf::spring_embedder::SpringEmbedderBase::SpringEmbedderBase
SpringEmbedderBase()
Constructor.
Definition: SpringEmbedderBase.h:65
ogdf::filter_any_edge
bool filter_any_edge(edge e)
std::function<bool(edge)> that returns true for any edge e
ogdf::spring_embedder::SpringEmbedderBase::forceModelImprove
SpringForceModel forceModelImprove() const
Returns the currently used force model for the improvement step.
Definition: SpringEmbedderBase.h:176
ogdf::GraphCopy
Copies of graphs supporting edge splitting.
Definition: GraphCopy.h:391
ogdf::spring_embedder::SpringEmbedderBase::maxThreads
unsigned int maxThreads() const
Returns the maximal number of used threads.
Definition: SpringEmbedderBase.h:288
LayoutModule.h
Declaration of interface for layout algorithms (class LayoutModule)
ogdf::LayoutStandards::defaultNodeHeight
static double defaultNodeHeight()
Returns the global default height for nodes.
Definition: LayoutStandards.h:81
ogdf::GraphCopy::clear
void clear() override
Removes all nodes and edges from this copy but does not break the link with the original graph.
ogdf::makeSimpleUndirected
void makeSimpleUndirected(Graph &G)
Removes all self-loops and all but one edge of each bundle of undirected parallel edges.
Definition: simple_graph_alg.h:433
ogdf::spring_embedder::SpringEmbedderBase::m_avgConvergenceFactor
double m_avgConvergenceFactor
convergence if avg.
Definition: SpringEmbedderBase.h:316
ogdf::spring_embedder::SpringEmbedderBase::maxThreads
void maxThreads(unsigned int n)
Sets the maximal number of used threads to n.
Definition: SpringEmbedderBase.h:291
ogdf::GraphAttributes::constGraph
const Graph & constGraph() const
Returns a reference to the associated graph.
Definition: GraphAttributes.h:223
ogdf::spring_embedder::SpringEmbedderBase::iterationsImprove
void iterationsImprove(int i)
Sets the number of iterations for the improvement phase to i.
Definition: SpringEmbedderBase.h:229
ogdf::spring_embedder::SpringEmbedderBase::m_iterationsImprove
int m_iterationsImprove
The number of iterations for the improvement phase.
Definition: SpringEmbedderBase.h:297
ogdf::spring_embedder::SpringEmbedderBase
Common base class for ogdf::SpringEmbedderBase and ogdf::SpringEmbedderGridVariant.
Definition: SpringEmbedderBase.h:54
ogdf::spring_embedder::SpringEmbedderBase::userBoundingBox
DRect userBoundingBox() const
Gets the user bounding box.
Definition: SpringEmbedderBase.h:285
ogdf::spring_embedder::SpringEmbedderBase::m_noise
bool m_noise
The used force model for the improvement phase.
Definition: SpringEmbedderBase.h:306
ogdf::spring_embedder::SpringEmbedderBase::iterations
void iterations(int i)
Sets the number of iterations to i.
Definition: SpringEmbedderBase.h:219
ogdf::spring_embedder::SpringEmbedderBase::avgConvergenceFactor
double avgConvergenceFactor() const
Returns the currently used average convergence factor.
Definition: SpringEmbedderBase.h:187
ogdf::spring_embedder::SpringEmbedderBase::scaleFunctionFactor
void scaleFunctionFactor(double f)
Sets the scale function factor to f.
Definition: SpringEmbedderBase.h:277
ogdf::spring_embedder::SpringEmbedderBase::Scaling::useIdealEdgeLength
@ useIdealEdgeLength
use the given ideal edge length to scale the layout suitably.
ogdf::GraphAttributes::y
double y(node v) const
Returns the y-coordinate of node v.
Definition: GraphAttributes.h:265
Minisat::Internal::copy
static void copy(const T &from, T &to)
Definition: Alg.h:61
ogdf::spring_embedder::SpringEmbedderBase::m_iterations
int m_iterations
The number of iterations.
Definition: SpringEmbedderBase.h:296
ogdf::Graph::numberOfNodes
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:982
ogdf::LayoutStandards::defaultCCSeparation
static double defaultCCSeparation()
Returns the global default separation between connected components.
Definition: LayoutStandards.h:194
ogdf::spring_embedder::SpringEmbedderBase::Scaling
Scaling
The scaling method used by the algorithm.
Definition: SpringEmbedderBase.h:57
ogdf::spring_embedder::SpringEmbedderBase::m_coolDownFactor
double m_coolDownFactor
Definition: SpringEmbedderBase.h:299
LayoutStandards.h
Declares class LayoutStandards which specifies default / standard values used in graph layouts.
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:219
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::DRect
Rectangles with real coordinates.
Definition: geometry.h:798
ogdf::spring_embedder::SpringEmbedderBase::forceModel
void forceModel(SpringForceModel fm)
Sets the used force model to fm.
Definition: SpringEmbedderBase.h:173
ogdf::Graph::firstNode
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:997
ogdf::spring_embedder::SpringEmbedderBase::m_scaleFactor
double m_scaleFactor
The factor used if scaling type is scScaleFunction.
Definition: SpringEmbedderBase.h:309
ogdf::TileToRowsCCPacker::call
virtual void call(Array< DPoint > &box, Array< DPoint > &offset, double pageRatio=1.0) override
Arranges the rectangles given by box.
ogdf::spring_embedder::SpringEmbedderBase::call
virtual void call(GraphAttributes &GA) override
Computes a layout of graph GA.
Definition: SpringEmbedderBase.h:97
ogdf::spring_embedder::SpringEmbedderBase::m_maxThreads
unsigned int m_maxThreads
The maximal number of used threads.
Definition: SpringEmbedderBase.h:319
GraphCopy.h
Declaration of graph copy classes.
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::spring_embedder::SpringEmbedderBase::userBoundingBox
void userBoundingBox(double xmin, double ymin, double xmax, double ymax)
Sets the user bounding box (used if scaling method is scUserBoundingBox).
Definition: SpringEmbedderBase.h:280
ogdf::GraphAttributes::clearAllBends
void clearAllBends()
Removes all edge bends.
ogdf::spring_embedder::SpringEmbedderBase::forceModelImprove
void forceModelImprove(SpringForceModel fm)
Sets the used force model for the improvement step to fm.
Definition: SpringEmbedderBase.h:179
ogdf::spring_embedder::SpringEmbedderBase::m_minDistCC
double m_minDistCC
The minimal distance between connected components.
Definition: SpringEmbedderBase.h:313
ogdf::spring_embedder::SpringEmbedderBase::callMaster
virtual void callMaster(const GraphCopy &copy, GraphAttributes &attr, DPoint &box)=0
ogdf::spring_embedder::SpringEmbedderBase::iterationsImprove
int iterationsImprove() const
Returns the current setting of iterations for the improvement phase.
Definition: SpringEmbedderBase.h:226
ogdf::DPoint
GenericPoint< double > DPoint
Representing two-dimensional point with real coordinates.
Definition: geometry.h:244
ogdf::spring_embedder::SpringEmbedderBase::forceModel
SpringForceModel forceModel() const
Returns the currently used force model.
Definition: SpringEmbedderBase.h:170
TileToRowsCCPacker.h
Declaration of class TileToRowsCCPacker.
ogdf::spring_embedder::SpringEmbedderBase::pageRatio
void pageRatio(double x)
Sets the page ration to x.
Definition: SpringEmbedderBase.h:265
ogdf::spring_embedder::SpringEmbedderBase::minDistCC
void minDistCC(double x)
Sets the minimum distance between connected components to x.
Definition: SpringEmbedderBase.h:259
ogdf::spring_embedder::SpringEmbedderBase::m_forceLimitStep
double m_forceLimitStep
Definition: SpringEmbedderBase.h:300
ogdf::Graph::insert
std::pair< int, int > insert(const NI &nodesBegin, const NI &nodesEnd, const EI &edgesBegin, const EI &edgesEnd, NodeArray< node > &nodeMap, EdgeArray< edge > &edgeMap)
Inserts a copy of a given subgraph into this graph.
Definition: InducedSubgraph.h:102
ogdf::spring_embedder::SpringEmbedderBase::scaling
void scaling(Scaling sc)
Sets the method for scaling the inital layout to sc.
Definition: SpringEmbedderBase.h:271
ogdf::spring_embedder::SpringEmbedderBase::scaling
Scaling scaling() const
Returns the current scaling method.
Definition: SpringEmbedderBase.h:268
ogdf::spring_embedder::SpringEmbedderBase::m_boundingBox
DRect m_boundingBox
Definition: SpringEmbedderBase.h:302
ogdf::spring_embedder::SpringEmbedderBase::m_scaling
Scaling m_scaling
The scaling method.
Definition: SpringEmbedderBase.h:308
ogdf::SpringForceModel
SpringForceModel
The force model used for computing forces on nodes.
Definition: SpringForceModel.h:38
ogdf::end
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
ogdf::spring_embedder::SpringEmbedderBase::idealEdgeLength
double idealEdgeLength() const
Returns the current setting of ideal edge length.
Definition: SpringEmbedderBase.h:240
ogdf::spring_embedder::SpringEmbedderBase::m_forceModel
SpringForceModel m_forceModel
Definition: SpringEmbedderBase.h:304
ogdf::spring_embedder::SpringEmbedderBase::Scaling::userBoundingBox
@ userBoundingBox
bounding box set by userBoundingBox() is used.
ogdf::spring_embedder::SpringEmbedderBase::pageRatio
double pageRatio()
Returns the page ratio.
Definition: SpringEmbedderBase.h:262
Array.h
Declaration and implementation of Array class and Array algorithms.
ogdf::spring_embedder::SpringEmbedderBase::Scaling::input
@ input
bounding box of input is used.
ogdf::spring_embedder::SpringEmbedderBase::m_maxConvergenceFactor
double m_maxConvergenceFactor
convergence if max.
Definition: SpringEmbedderBase.h:317
ogdf::SpringForceModel::FruchtermanReingoldModRep
@ FruchtermanReingoldModRep
List.h
Declaration of doubly linked lists and iterators.
ogdf::spring_embedder::SpringEmbedderBase::m_idealEdgeLength
double m_idealEdgeLength
The ideal edge length.
Definition: SpringEmbedderBase.h:298
ogdf::RegisteredArray< GraphRegistry< Key >, Value, WithDefault, Graph >::init
void init(const Graph *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
Definition: RegisteredArray.h:858
ogdf::spring_embedder::SpringEmbedderBase::forceLimitStep
double forceLimitStep() const
Definition: SpringEmbedderBase.h:237
ogdf::SpringForceModel::FruchtermanReingold
@ FruchtermanReingold
the force model proposed by Fruchterman and Reingold.
ogdf::spring_embedder::SpringEmbedderBase::noise
void noise(bool on)
Sets the parameter noise to on.
Definition: SpringEmbedderBase.h:253
ogdf::spring_embedder::SpringEmbedderBase::avgConvergenceFactor
void avgConvergenceFactor(double f)
Sets the average convergence factor to f.
Definition: SpringEmbedderBase.h:190
ogdf::ListIteratorBase
Encapsulates a pointer to a list element.
Definition: List.h:51
ogdf::TileToRowsCCPacker
The tile-to-rows algorithm for packing drawings of connected components.
Definition: TileToRowsCCPacker.h:43
ogdf::LayoutStandards::defaultNodeWidth
static double defaultNodeWidth()
Returns the global default width for nodes.
Definition: LayoutStandards.h:68
ogdf::spring_embedder::SpringEmbedderBase::iterations
int iterations() const
Returns the current setting of iterations.
Definition: SpringEmbedderBase.h:216
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::spring_embedder::SpringEmbedderBase::scaleFunctionFactor
double scaleFunctionFactor() const
Returns the current scale function factor.
Definition: SpringEmbedderBase.h:274
simple_graph_alg.h
Declaration of simple graph algorithms.
ogdf::LayoutStandards::defaultNodeSeparation
static double defaultNodeSeparation()
Returns the global default node separation.
Definition: LayoutStandards.h:181
ogdf::System::numberOfProcessors
static int numberOfProcessors()
Returns the number of processors (cores) available on the current system.
Definition: System.h:275
ogdf::spring_embedder::SpringEmbedderBase::m_pageRatio
double m_pageRatio
The page ratio.
Definition: SpringEmbedderBase.h:314
ogdf::spring_embedder::SpringEmbedderBase::m_forceModelImprove
SpringForceModel m_forceModelImprove
The used force model.
Definition: SpringEmbedderBase.h:305
ogdf::GraphCopyBase::original
const Graph & original() const
Returns a reference to the original graph.
Definition: GraphCopy.h:105
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::LayoutModule
Interface of general layout algorithms.
Definition: LayoutModule.h:45
ogdf::spring_embedder::SpringEmbedderBase::minDistCC
double minDistCC() const
Returns the minimum distance between connected components.
Definition: SpringEmbedderBase.h:256