Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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  : m_maxLevel((sizeof(IntType) << 3) + 1)
50  , m_numPoints(0)
51  , m_rootIndex(-1)
52  {
53  allocate(numPoints);
54  }
55 
58  {
59  deallocate();
60  }
61 
63  struct Point
64  {
65  IntType x[Dim];
66  };
67 
69  struct MortonEntry
70  {
71  IntType mortonNr[Dim];
72  int ref;
73 
75  bool operator<(const MortonEntry& other) const
76  {
77  return mortonComparerLess<IntType, Dim>(mortonNr, other.mortonNr);
78  }
79 
81  bool operator==(const MortonEntry& other) const
82  {
83  return mortonComparerEqual<IntType, Dim>(mortonNr, other.mortonNr);
84  }
85  };
86 
88  struct Node
89  {
90  // quadtree related stuff
91  int level;
92  int next;
94  int numChilds;
95  int firstPoint;
96  int numPoints;
97  };
98 
100  inline const Node& node(int i) const { return m_nodes[i]; };
101 
103  inline Node& node(int i) { return m_nodes[i]; };
104 
106  inline int numChilds(int i) const { return m_nodes[i].numChilds; };
107 
109  inline int child(int i, int j) const { return m_nodes[i].child[j]; };
110 
112  inline int numPoints(int i) const { return m_nodes[i].numPoints; };
113 
115  inline int point(int i, int j) const { return m_mortonOrder[ m_nodes[i].firstPoint + j ].ref; };
116 
118  void setPoint(int i, int d, IntType value);
119 
121  inline int numPoints() const { return m_numPoints; };
122 
124  inline int maxNumNodes() const { return m_numPoints * 2; };
125 
127  const Point& point(int i) const { return m_points[i]; }
128 
130  void prepareMortonOrder();
131 
133  void sortMortonNumbers();
134 
136  void prepareNodeLayer();
137 
139  inline void mergeWithNext(int curr);
140 
142  inline void adjustPointInfo(int curr);
143 
145  int linkNodes(int curr, int maxLevel);
146 
148  void linkNodes();
149 
151  void build();
152 
154  int countPoints(int curr) const;
155 
157  int countPoints() const { return countPoints(m_rootIndex); };
158 
160  int rootIndex() const { return m_rootIndex; };
161 
162 private:
163 
165  void allocate(int n);
166 
168  void deallocate();
169 
171  Point* m_points = nullptr;
174  Node* m_nodes = nullptr;
176 };
177 
179 template<typename IntType, int Dim>
181 {
182  m_numPoints = n;
183  m_points = new Point[m_numPoints];
185  m_nodes = new Node[m_numPoints * 2];
186 };
187 
188 
190 template<typename IntType, int Dim>
192 {
193  delete[] m_points;
194  delete[] m_mortonOrder;
195  delete[] m_nodes;
196 };
197 
198 template<typename IntType, int Dim>
199 void DTree<IntType, Dim>::setPoint(int i, int d, IntType value)
200 {
201  // set i-th point d coord to value
202  m_points[i].x[d] = value;
203 }
204 
206 template<typename IntType, int Dim>
208 {
209  // loop over the point order
210  for (int i=0; i<m_numPoints; i++) {
211  // set i's ref to i
212  m_mortonOrder[i].ref = i;
213 
214  // generate the morton number by interleaving the bits
215  interleaveBits<IntType, Dim>(m_points[i].x, m_mortonOrder[i].mortonNr);
216  }
217 }
218 
220 template<typename IntType, int Dim>
222 {
223  // just sort them
225 }
226 
227 
229 template<typename IntType, int Dim>
231 {
232  Node* leafLayer = m_nodes;
233  Node* innerLayer = m_nodes + m_numPoints;
234 
235 #if 0
236  int last = 0;
237 #endif
238  for (int i = 0; i<m_numPoints;) {
239  Node& leaf = leafLayer[i];
240  Node& innerNode = innerLayer[i];
241  // i represents the current node on both layers
242  int j = i+1;
243 
244  // find the next morton number that differs or stop when j is equal to m_numPoints
245  while ((j < m_numPoints) &&
246  (m_mortonOrder[i] == m_mortonOrder[j]))
247  j++;
248  // j is the index of the next cell (node)
249 
250  // init the node on the leaf layer
251  leaf.firstPoint = i; //< node sits above the first point of the cell
252  leaf.numPoints = j-i; //< number of points with equal morton numbers (number of points in grid cell)
253  leaf.numChilds = 0; //< it's a leaf
254  leaf.level = 0; //< it's a leaf
255  leaf.next = j; //< this leaf hasnt been created yet but we use indices so its ok
256 
257  if (j < m_numPoints) {
258  // Note: the n-th inner node is not needed because we only need n-1 inner nodes to cover n leaves
259  // if we reach the n-1-th inner node, the variable last is set for the last time
260 #if 0
261  last = i;
262 #endif
263  // init the node on the inner node layer
264  innerNode.child[0] = i; //< node sits above the first leaf
265  innerNode.child[1] = j; //< this leaf hasnt been created yet but we use indices so its ok
266  innerNode.numChilds = 2; //< every inner node covers two leaves in the beginning
267  innerNode.level = lowestCommonAncestorLevel<IntType, Dim>(m_mortonOrder[i].mortonNr, m_mortonOrder[j].mortonNr);
268  innerNode.next = m_numPoints + j; // the inner node layer is shifted by n
269  } else {
270  // no next for the last
271  innerLayer[i].next = 0;
272  // this is important to make the recursion stop
273  innerLayer[i].level = m_maxLevel + 1;
274  }
275 
276  // advance to the next cell
277  i = j;
278  };
279  // here we set the successor of the n-1-th inner node to zero to avoid dealing with the n-th inner node
280 #if 0
281  innerLayer[last].next = 0;
282 #endif
283 };
284 
286 template<typename IntType, int Dim>
288 {
289  int next = node(curr).next;
290  // Cool: since we never touched node(next) before
291  // it is still linked to only two leaves,
292  node(curr).child[node(curr).numChilds++] = node(next).child[1];
293 
294  // thus we don't need this ugly loop:
295  // for (int i=1; i<node(next).numChilds; i++)
296  // node(curr).child[node(curr).numChilds++] = node(next).child[i];
297  node(curr).next = node(next).next;
298 };
299 
300 template<typename IntType, int Dim>
302 {
303  // adjust the first such that it matched the first child
304  node(curr).firstPoint = node(node(curr).child[0]).firstPoint;
305 
306  // index of the last child
307  const int lastChild = node(curr).child[node(curr).numChilds-1];
308 
309  // numPoints is lastPoint + 1 - firstPoint
310  node(curr).numPoints = node(lastChild).firstPoint + node(lastChild).numPoints - node(curr).firstPoint;
311 }
312 
314 template<typename IntType, int Dim>
315 int DTree<IntType, Dim>::linkNodes(int curr, int maxLevel)
316 {
317  // while the subtree is smaller than maxLevel
318  while (node(curr).next && node(node(curr).next).level < maxLevel) {
319  // get next node in the chain
320  int next = node(curr).next;
321  // First case: same level => merge, discard next
322  if (node(curr).level == node(next).level) {
323  mergeWithNext(curr);
324  } else // Second case: next is higher => become first child
325  if (node(curr).level < node(next).level) {
326  // set the first child of next to the current node
327  node(next).child[0] = curr;
328 
329  // adjust the point info of the curr
330  adjustPointInfo(curr);
331 
332  // this is the only case where we advance curr
333  curr = next;
334  } else { // Third case: next is smaller => construct a maximal subtree starting with next
335  int r = linkNodes(next, node(curr).level);
336  node(curr).child[node(curr).numChilds-1] = r;
337  node(curr).next = node(r).next;
338  };
339  };
340  // adjust the point info of the curr
341  adjustPointInfo(curr);
342 
343  // we are done with this subtree, return the root
344  return curr;
345 };
346 
347 
349 template<typename IntType, int Dim>
351 {
353 };
354 
356 template<typename IntType, int Dim>
358 {
359  if (m_nodes[curr].numChilds) {
360  int sum = 0;
361  for (int i=0; i<m_nodes[curr].numChilds; i++) {
362  sum += countPoints(m_nodes[curr].child[i]);
363  };
364 
365  return sum;
366  }
367  else
368  return m_nodes[curr].numPoints;
369 };
370 
372 template<typename IntType, int Dim>
374 {
375  // prepare the array with the morton numbers
377 
378  // sort the morton numbers
380 
381  // prepare the node layer
383 
385  linkNodes();
386 };
387 
388 }}}
MortonEntry * m_mortonOrder
The order to be sorted.
Definition: DTree.h:173
int child[MaxNumChildrenPerNode]
index of the children
Definition: DTree.h:93
int countPoints() const
Just for fun: traverse the tree and count the points in the leaves.
Definition: DTree.h:157
void prepareMortonOrder()
Prepares the morton numbers for sorting.
Definition: DTree.h:207
int numPoints(int i) const
returns the number of points covered by this subtree
Definition: DTree.h:112
int level
the level of the node in a complete quadtree
Definition: DTree.h:91
void adjustPointInfo(int curr)
used to update the first and numPoints of inner nodes by linkNodes
Definition: DTree.h:301
Implentation of the reduced quadtree for Dim dimensions.
Definition: DTree.h:40
int ref
index in the original point order
Definition: DTree.h:72
Point * m_points
The input set.
Definition: DTree.h:171
void allocate(int n)
Allocates memory for n points.
Definition: DTree.h:180
int numChilds
number of children
Definition: DTree.h:94
int numPoints() const
returns the number of points the quadtree contains
Definition: DTree.h:121
void linkNodes()
The Recursive Bottom-Up Construction (recursion start)
Definition: DTree.h:350
The entry in the sorted order for a point.
Definition: DTree.h:69
DTree(int numPoints)
constructor
Definition: DTree.h:48
Node * m_nodes
Memory for all nodes.
Definition: DTree.h:174
int child(int i, int j) const
returns the index of the j th child of node i
Definition: DTree.h:109
void setPoint(int i, int d, IntType value)
sets the point to the given grid coords
Definition: DTree.h:199
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:103
int numChilds(int i) const
returns the number of children of node i
Definition: DTree.h:106
void deallocate()
Releases memory.
Definition: DTree.h:191
void mergeWithNext(int curr)
Merges curr with next node in the chain (used by linkNodes)
Definition: DTree.h:287
void build()
Does all required steps except the allocate, deallocate, randomPoints.
Definition: DTree.h:373
void prepareNodeLayer()
Prepares both the leaf and inner node layer.
Definition: DTree.h:230
int point(int i, int j) const
returns the index of the jth point covered by i&#39;s subtree.
Definition: DTree.h:115
int firstPoint
the first point in the sorted order covered by this subtree
Definition: DTree.h:95
int rootIndex() const
returns the index of the root node
Definition: DTree.h:160
int numPoints
the number of points covered by this subtree
Definition: DTree.h:96
int maxNumNodes() const
returns the maximum number of nodes (and the max index of a node)
Definition: DTree.h:124
const Point & point(int i) const
returns the ith point in the input sequence
Definition: DTree.h:127
int m_numPoints
Total number of points.
Definition: DTree.h:172
int next
the next node on the same layer (leaf or inner node layer)
Definition: DTree.h:92
bool operator==(const MortonEntry &other) const
equal comparer for the construction algorithm
Definition: DTree.h:81
void sortMortonNumbers()
Sorts the points by morton number.
Definition: DTree.h:221
The namespace for all OGDF objects.
bool operator<(const MortonEntry &other) const
less comparator for sort
Definition: DTree.h:75
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:175
Container::value_type sum(const Container &values)
Returns the sum of an iterable container of given values.
Definition: Math.h:245
const Node & node(int i) const
Just to access the nodes a little bit easier.
Definition: DTree.h:100
IntType mortonNr[Dim]
the morton number of the point
Definition: DTree.h:71
The point class with integer coords.
Definition: DTree.h:63