Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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:
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:
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* pLevelBegin = new GalaxyLevel(graph);
153 
154  // this is the coarsest level with at most m_levelMaxNumNodes
155  GalaxyLevel* pLevelEnd = pLevelBegin->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 = pLevelBegin; pCurrLevel; 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* pEmbedder = new 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  pEmbedder->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  pEmbedder->setPosition(v, d, offset + randomDouble(-1.0, 1.0));
204  }
205  }
206  } // we have some proper initial coordinates for the nodes
207 
209  // we cannot init from the parent level, do it random
210  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
211  pEmbedder->setMass(v, pCurrLevel->weight(v));
212  }
213 
214  for (edge e = pCurrLevel->graph().firstEdge(); e; e = e->succ()) {
215  pEmbedder->setEdgeWeight(e, pCurrLevel->edgeWeight(e));
216  }
217  }
218 
219  const int numIterationsMaybe = pCurrLevel->isCoarsestLevel() ? m_numIterationsCoarsestLevel : currNumIterations;
220  const int numIterations = std::min(std::max(m_minIterationsPerLevel, numIterationsMaybe), m_maxIterationsPerLevel);
221 
222  pEmbedder->scaleNodes(3.0);
223  pEmbedder->doIterationsNewton(numIterations, currThreshold, RepForceFunctionNewton<Dim, 1>, AttrForceFunctionPow<Dim, 2>);
224  pEmbedder->scaleNodes(1.0 / 3.0);
225  pEmbedder->doIterationsNewton(numIterations, currThreshold, RepForceFunctionNewton<Dim, 2>, AttrForceFunctionPow<Dim, 2>);
226  // run the layout
227 
228  // we now have to backup the positions before getting rid of the embedder
229  parentPosition.init(pCurrLevel->graph());
230 
231  // iterate over all nodes
232  for (node v = pCurrLevel->graph().firstNode(); v; v = v->succ()) {
233  // for all coords
234  for (int d = 0; d < Dim; d++) {
235  parentPosition[v].coords[d] = pEmbedder->position(v, d);
236  }
237  }
238 
239  currLevelIndex--;
240  // now we can free the embedder instance
241  delete pEmbedder;
242  }
243 
244  // we are done with the layout. It is saved now in the parentposition nodearray.
245  // which is a reference to the result array
246 }
247 
248 }
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:472
static const int threeD
Corresponds to node attribute z(node). Note that all methods work on 2D coordinates only...
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:292
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:598
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:216
double y(node v) const
Returns the y-coordinate of node v.
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
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:291
double z(node v) const
Returns the z-coordinate of node v.
double x(node v) const
Returns the x-coordinate of node v.
Declaration of interface for layout algorithms (class LayoutModule)
void call(GraphAttributes &GA)
Computes a layout of graph GA.
Class for the representation of nodes.
Definition: Graph_d.h:167
const Graph & constGraph() const
Returns a reference to the associated graph.
Interface of general layout algorithms.
Definition: LayoutModule.h:44
void call(GraphAttributes &GA)
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:340
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