Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

DTree.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <algorithm>
33 
34 namespace ogdf {
35 namespace energybased {
36 namespace dtree {
37 
39 template<typename IntType, int Dim>
40 class DTree
41 {
42 public:
43 
45  static constexpr int MaxNumChildrenPerNode = (1 << Dim);
46 
48  explicit DTree(int numPoints)
49  {
50  m_maxLevel = (sizeof(IntType) << 3) + 1;
51  allocate(numPoints);
52  }
53 
56  {
57  deallocate();
58  }
59 
61  struct Point
62  {
63  IntType x[Dim];
64  };
65 
67  struct MortonEntry
68  {
69  IntType mortonNr[Dim];
70  int ref;
71 
73  bool operator<(const MortonEntry& other) const
74  {
75  return mortonComparerLess<IntType, Dim>(mortonNr, other.mortonNr);
76  }
77 
79  bool operator==(const MortonEntry& other) const
80  {
81  return mortonComparerEqual<IntType, Dim>(mortonNr, other.mortonNr);
82  }
83  };
84 
86  struct Node
87  {
88  // quadtree related stuff
89  int level;
90  int next;
92  int numChilds;
93  int firstPoint;
94  int numPoints;
95  };
96 
98  inline const Node& node(int i) const { return m_nodes[i]; };
99 
101  inline Node& node(int i) { return m_nodes[i]; };
102 
104  inline int numChilds(int i) const { return m_nodes[i].numChilds; };
105 
107  inline int child(int i, int j) const { return m_nodes[i].child[j]; };
108 
110  inline int numPoints(int i) const { return m_nodes[i].numPoints; };
111 
113  inline int point(int i, int j) const { return m_mortonOrder[ m_nodes[i].firstPoint + j ].ref; };
114 
116  void setPoint(int i, int d, IntType value);
117 
119  inline int numPoints() const { return m_numPoints; };
120 
122  inline int maxNumNodes() const { return m_numPoints * 2; };
123 
125  const Point& point(int i) const { return m_points[i]; }
126 
128  void prepareMortonOrder();
129 
131  void sortMortonNumbers();
132 
134  void prepareNodeLayer();
135 
137  inline void mergeWithNext(int curr);
138 
140  inline void adjustPointInfo(int curr);
141 
143  int linkNodes(int curr, int maxLevel);
144 
146  void linkNodes();
147 
149  void build();
150 
152  int countPoints(int curr) const;
153 
155  int countPoints() const { return countPoints(m_rootIndex); };
156 
158  int rootIndex() const { return m_rootIndex; };
159 
160 private:
161 
163  void allocate(int n);
164 
166  void deallocate();
167 
174 };
175 
177 template<typename IntType, int Dim>
179 {
180  m_numPoints = n;
181  m_points = new Point[m_numPoints];
183  m_nodes = new Node[m_numPoints * 2];
184 };
185 
186 
188 template<typename IntType, int Dim>
190 {
191  delete[] m_points;
192  delete[] m_mortonOrder;
193  delete[] m_nodes;
194 };
195 
196 template<typename IntType, int Dim>
197 void DTree<IntType, Dim>::setPoint(int i, int d, IntType value)
198 {
199  // set i-th point d coord to value
200  m_points[i].x[d] = value;
201 }
202 
204 template<typename IntType, int Dim>
206 {
207  // loop over the point order
208  for (int i=0; i<m_numPoints; i++) {
209  // set i's ref to i
210  m_mortonOrder[i].ref = i;
211 
212  // generate the morton number by interleaving the bits
213  interleaveBits<IntType, Dim>(m_points[i].x, m_mortonOrder[i].mortonNr);
214  }
215 }
216 
218 template<typename IntType, int Dim>
220 {
221  // just sort them
223 }
224 
225 
227 template<typename IntType, int Dim>
229 {
230  Node* leafLayer = m_nodes;
231  Node* innerLayer = m_nodes + m_numPoints;
232 
233 #if 0
234  int last = 0;
235 #endif
236  for (int i = 0; i<m_numPoints;) {
237  Node& leaf = leafLayer[i];
238  Node& innerNode = innerLayer[i];
239  // i represents the current node on both layers
240  int j = i+1;
241 
242  // find the next morton number that differs or stop when j is equal to m_numPoints
243  while ((j < m_numPoints) &&
244  (m_mortonOrder[i] == m_mortonOrder[j]))
245  j++;
246  // j is the index of the next cell (node)
247 
248  // init the node on the leaf layer
249  leaf.firstPoint = i; //< node sits above the first point of the cell
250  leaf.numPoints = j-i; //< number of points with equal morton numbers (number of points in grid cell)
251  leaf.numChilds = 0; //< it's a leaf
252  leaf.level = 0; //< it's a leaf
253  leaf.next = j; //< this leaf hasnt been created yet but we use indices so its ok
254 
255  if (j < m_numPoints) {
256  // Note: the n-th inner node is not needed because we only need n-1 inner nodes to cover n leaves
257  // if we reach the n-1-th inner node, the variable last is set for the last time
258 #if 0
259  last = i;
260 #endif
261  // init the node on the inner node layer
262  innerNode.child[0] = i; //< node sits above the first leaf
263  innerNode.child[1] = j; //< this leaf hasnt been created yet but we use indices so its ok
264  innerNode.numChilds = 2; //< every inner node covers two leaves in the beginning
265  innerNode.level = lowestCommonAncestorLevel<IntType, Dim>(m_mortonOrder[i].mortonNr, m_mortonOrder[j].mortonNr);
266  innerNode.next = m_numPoints + j; // the inner node layer is shifted by n
267  } else {
268  // no next for the last
269  innerLayer[i].next = 0;
270  // this is important to make the recursion stop
271  innerLayer[i].level = m_maxLevel + 1;
272  }
273 
274  // advance to the next cell
275  i = j;
276  };
277  // here we set the successor of the n-1-th inner node to zero to avoid dealing with the n-th inner node
278 #if 0
279  innerLayer[last].next = 0;
280 #endif
281 };
282 
284 template<typename IntType, int Dim>
286 {
287  int next = node(curr).next;
288  // Cool: since we never touched node(next) before
289  // it is still linked to only two leaves,
290  node(curr).child[node(curr).numChilds++] = node(next).child[1];
291 
292  // thus we don't need this ugly loop:
293  // for (int i=1; i<node(next).numChilds; i++)
294  // node(curr).child[node(curr).numChilds++] = node(next).child[i];
295  node(curr).next = node(next).next;
296 };
297 
298 template<typename IntType, int Dim>
300 {
301  // adjust the first such that it matched the first child
302  node(curr).firstPoint = node(node(curr).child[0]).firstPoint;
303 
304  // index of the last child
305  const int lastChild = node(curr).child[node(curr).numChilds-1];
306 
307  // numPoints is lastPoint + 1 - firstPoint
308  node(curr).numPoints = node(lastChild).firstPoint + node(lastChild).numPoints - node(curr).firstPoint;
309 }
310 
312 template<typename IntType, int Dim>
313 int DTree<IntType, Dim>::linkNodes(int curr, int maxLevel)
314 {
315  // while the subtree is smaller than maxLevel
316  while (node(curr).next && (node(node(curr).next).level < maxLevel)) {
317  // get next node in the chain
318  int next = node(curr).next;
319  // First case: same level => merge, discard next
320  if (node(curr).level == node(next).level) {
321  mergeWithNext(curr);
322  } else // Second case: next is higher => become first child
323  if (node(curr).level < node(next).level) {
324  // set the first child of next to the current node
325  node(next).child[0] = curr;
326 
327  // adjust the point info of the curr
328  adjustPointInfo(curr);
329 
330  // this is the only case where we advance curr
331  curr = next;
332  } else { // Third case: next is smaller => construct a maximal subtree starting with next
333  int r = linkNodes(next, node(curr).level);
334  node(curr).child[node(curr).numChilds-1] = r;
335  node(curr).next = node(r).next;
336  };
337  };
338  // adjust the point info of the curr
339  adjustPointInfo(curr);
340 
341  // we are done with this subtree, return the root
342  return curr;
343 };
344 
345 
347 template<typename IntType, int Dim>
349 {
351 };
352 
354 template<typename IntType, int Dim>
356 {
357  if (m_nodes[curr].numChilds) {
358  int sum = 0;
359  for (int i=0; i<m_nodes[curr].numChilds; i++) {
360  sum += countPoints(m_nodes[curr].child[i]);
361  };
362 
363  return sum;
364  }
365  else
366  return m_nodes[curr].numPoints;
367 };
368 
370 template<typename IntType, int Dim>
372 {
373  // prepare the array with the morton numbers
375 
376  // sort the morton numbers
378 
379  // prepare the node layer
381 
383  linkNodes();
384 };
385 
386 }}}
MortonEntry * m_mortonOrder
The order to be sorted.
Definition: DTree.h:171
int child[MaxNumChildrenPerNode]
index of the children
Definition: DTree.h:91
int countPoints() const
Just for fun: traverse the tree and count the points in the leaves.
Definition: DTree.h:155
void prepareMortonOrder()
Prepares the morton numbers for sorting.
Definition: DTree.h:205
int numPoints(int i) const
returns the number of points covered by this subtree
Definition: DTree.h:110
int level
the level of the node in a complete quadtree
Definition: DTree.h:89
void adjustPointInfo(int curr)
used to update the first and numPoints of inner nodes by linkNodes
Definition: DTree.h:299
Implentation of the reduced quadtree for Dim dimensions.
Definition: DTree.h:40
int ref
index in the original point order
Definition: DTree.h:70
Point * m_points
The input set.
Definition: DTree.h:169
void allocate(int n)
Allocates memory for n points.
Definition: DTree.h:178
int numChilds
number of children
Definition: DTree.h:92
int numPoints() const
returns the number of points the quadtree contains
Definition: DTree.h:119
void linkNodes()
The Recursive Bottom-Up Construction (recursion start)
Definition: DTree.h:348
The entry in the sorted order for a point.
Definition: DTree.h:67
DTree(int numPoints)
constructor
Definition: DTree.h:48
Node * m_nodes
Memory for all nodes.
Definition: DTree.h:172
int child(int i, int j) const
returns the index of the j th child of node i
Definition: DTree.h:107
void setPoint(int i, int d, IntType value)
sets the point to the given grid coords
Definition: DTree.h:197
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
Node & node(int i)
Just to access the nodes a little bit easier.
Definition: DTree.h:101
int numChilds(int i) const
returns the number of children of node i
Definition: DTree.h:104
void deallocate()
Releases memory.
Definition: DTree.h:189
void mergeWithNext(int curr)
Merges curr with next node in the chain (used by linkNodes)
Definition: DTree.h:285
void build()
Does all required steps except the allocate, deallocate, randomPoints.
Definition: DTree.h:371
void prepareNodeLayer()
Prepares both the leaf and inner node layer.
Definition: DTree.h:228
int point(int i, int j) const
returns the index of the jth point covered by i&#39;s subtree.
Definition: DTree.h:113
int firstPoint
the first point in the sorted order covered by this subtree
Definition: DTree.h:93
int rootIndex() const
returns the index of the root node
Definition: DTree.h:158
int numPoints
the number of points covered by this subtree
Definition: DTree.h:94
int maxNumNodes() const
returns the maximum number of nodes (and the max index of a node)
Definition: DTree.h:122
const Point & point(int i) const
returns the ith point in the input sequence
Definition: DTree.h:125
int m_numPoints
Total number of points.
Definition: DTree.h:170
int next
the next node on the same layer (leaf or inner node layer)
Definition: DTree.h:90
bool operator==(const MortonEntry &other) const
equal comparer for the construction algorithm
Definition: DTree.h:79
void sortMortonNumbers()
Sorts the points by morton number.
Definition: DTree.h:219
The namespace for all OGDF objects.
bool operator<(const MortonEntry &other) const
less comparator for sort
Definition: DTree.h:73
static constexpr int MaxNumChildrenPerNode
the maximum number of children per node = 2^d
Definition: DTree.h:45
int m_rootIndex
The index of the root node.
Definition: DTree.h:173
const Node & node(int i) const
Just to access the nodes a little bit easier.
Definition: DTree.h:98
IntType mortonNr[Dim]
the morton number of the point
Definition: DTree.h:69
The point class with integer coords.
Definition: DTree.h:61