Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

DTreeMultilevelEmbedder.h
Go to the documentation of this file.
1 
29 #pragma once
30 
35 
36 namespace ogdf {
37 
38 template<int Dim>
40 {
41 public:
42  struct NodeCoords {
43  double coords[Dim];
44  };
45 
49  m_levelMaxNumNodes = 10;
50 
52  m_scaleFactorPerLevel = 1.71;
53  } else {
54  m_scaleFactorPerLevel = 3.71;//3.71;//3.71;
55  }
56 
59 #if 0
61 #endif
64 
65  m_thresholdFinestLevel = 0.0002;
67 
70  }
71 
73  void call(const Graph& graph, NodeArray<NodeCoords>& coords);
74 
75 private:
85 
88 };
89 
91 {
92 public:
93  void call(GraphAttributes& GA) override
94  {
95  // the graph
96  const Graph& G = GA.constGraph();
97 
98  // the input for the general d dim class
100 
101  // call it
103 
104  // copy the coords back to graph attributes
105  for (node v = G.firstNode(); v; v = v->succ()) {
106  GA.x(v) = coords[v].coords[0];
107  GA.y(v) = coords[v].coords[1];
108  }
109  }
110 };
111 
113 {
114 public:
115  void call(GraphAttributes& GA) override
116  {
117  // assert 3d
119 
120  // the graph
121  const Graph& G = GA.constGraph();
122 
123  // the input for the general d dim class
125 
126  // call it
128 
129  // copy the coords back to graph attributes
130  for (node v = G.firstNode(); v; v = v->succ()) {
131  GA.x(v) = coords[v].coords[0];
132  GA.y(v) = coords[v].coords[1];
133  GA.z(v) = coords[v].coords[2];
134  }
135  }
136 };
137 
138 
139 template<int Dim>
141 {
142  using namespace energybased::dtree;
143  using Embedder = DTreeEmbedder<Dim>;
144 
145  // we call this just in case
146  resultCoords.init(graph);
147 
148  // make sure the graph is connected
149  OGDF_ASSERT(isConnected(graph));
150 
151  // setup the multilevel step
152  GalaxyLevel levelBegin(graph);
153 
154  // this is the coarsest level with at most m_levelMaxNumNodes
155  GalaxyLevel* pLevelEnd = levelBegin.buildLevelsUntil(m_levelMaxNumNodes);
156 
157  // this array will hold the layout information of the parent node in the coarser level.
158  // furthermore this will keep the final result.
159  NodeArray<NodeCoords>& parentPosition = resultCoords;
160 
161  double currNumIterations = m_numIterationsFinestLevel;
162  double currThreshold = m_thresholdFinestLevel;
163 
164  int numLevels = 0;
165  // loop from the coarsest to the finest level
166  for (GalaxyLevel* pCurrLevel = &levelBegin; pCurrLevel != nullptr; pCurrLevel = pCurrLevel->nextCoarser()) {
167  numLevels++;
168  currNumIterations *= m_numIterationsFactorPerLevel;
169  currThreshold *= m_thresholdFactorPerLevel;
170  }
171 
172  int currLevelIndex = numLevels - 1;
173  // now we loop from the coarsest to the finest level
174  for (GalaxyLevel* pCurrLevel = pLevelEnd; pCurrLevel; pCurrLevel = pCurrLevel->nextFiner()) {
175  currNumIterations /= m_numIterationsFactorPerLevel;
176  currThreshold /= m_thresholdFactorPerLevel;
177 
178  // new embedder instance for the current level
179  Embedder embedder(pCurrLevel->graph());
180 
181  // if this is coarsest one
182  if (pCurrLevel->isCoarsestLevel()) {
183  // we cannot init from the parent level, do it random
184  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
185  // for all dims
186  for (int d = 0; d < Dim; d++) {
187  // set the position to some random value
188  embedder.setPosition(v, d, randomDouble(-1.0, 1.0));
189  }
190  }
191  } else { // if we have a parent level
192  // iterate over all nodes
193  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
194  // this is a node on the coarser level we already processed
195  node v_parent = pCurrLevel->parent(v);
196 
197  // now init the position from the parent.
198  for (int d = 0; d < Dim; d++) {
199  // get the position of the parent
200  double offset = parentPosition[v_parent].coords[d] * m_scaleFactorPerLevel;
201 
202  // set v's position to the parents pos with some random
203  embedder.setPosition(v, d, offset + randomDouble(-1.0, 1.0));
204  }
205  }
206  }
207  // we have some proper initial coordinates for the nodes
208 
210  // we cannot init from the parent level, do it random
211  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
212  embedder.setMass(v, pCurrLevel->weight(v));
213  }
214 
215  for (edge e = pCurrLevel->graph().firstEdge(); e; e = e->succ()) {
216  embedder.setEdgeWeight(e, pCurrLevel->edgeWeight(e));
217  }
218  }
219 
220  const int numIterationsMaybe = pCurrLevel->isCoarsestLevel() ? m_numIterationsCoarsestLevel : currNumIterations;
221  const int numIterations = std::min(std::max(m_minIterationsPerLevel, numIterationsMaybe), m_maxIterationsPerLevel);
222 
223  embedder.scaleNodes(3.0);
224  embedder.doIterationsNewton(numIterations, currThreshold, RepForceFunctionNewton<Dim, 1>, AttrForceFunctionPow<Dim, 2>);
225  embedder.scaleNodes(1.0 / 3.0);
226  embedder.doIterationsNewton(numIterations, currThreshold, RepForceFunctionNewton<Dim, 2>, AttrForceFunctionPow<Dim, 2>);
227  // run the layout
228 
229  // we now have to backup the positions before getting rid of the embedder
230  parentPosition.init(pCurrLevel->graph());
231 
232  // iterate over all nodes
233  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
234  // for all coords
235  for (int d = 0; d < Dim; d++) {
236  parentPosition[v].coords[d] = embedder.position(v, d);
237  }
238  }
239 
240  currLevelIndex--;
241  }
242 
243  // we are done with the layout. It is saved now in the parentposition nodearray.
244  // which is a reference to the result array
245 }
246 
247 }
void call(const Graph &graph, NodeArray< NodeCoords > &coords)
call the multilevel embedder layout for graph, the result is stored in coords
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
bool has(long attr) const
Returns true iff all attributes in attr are available.
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:618
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:218
void call(GraphAttributes &GA) override
Computes a layout of graph GA.
double y(node v) const
Returns the y-coordinate of node v.
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:438
Stores additional attributes of a graph (like layout information).
double randomDouble(double low, double high)
Returns a random double value from the interval [low, high).
Class for the representation of edges.
Definition: Graph_d.h:292
double z(node v) const
Returns the z-coordinate of node v.
static const long threeD
Corresponds to node attribute z(node). Note that all methods work on 2D coordinates only...
double x(node v) const
Returns the x-coordinate of node v.
Declaration of interface for layout algorithms (class LayoutModule)
Class for the representation of nodes.
Definition: Graph_d.h:169
const Graph & constGraph() const
Returns a reference to the associated graph.
Interface of general layout algorithms.
Definition: LayoutModule.h:44
void call(GraphAttributes &GA) override
Computes a layout of graph GA.
DTreeMultilevelEmbedder()
constructor with a given graph, allocates memory and does initialization
edge succ() const
Returns the successor in the list of all edges.
Definition: Graph_d.h:353
Declaration of simple graph algorithms.
The namespace for all OGDF objects.
bool isConnected(const Graph &G)
Returns true iff G is connected.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41