Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

DTreeWSPD.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <ogdf/basic/basic.h>
33 
34 namespace ogdf {
35 namespace energybased {
36 namespace dtree {
37 
38 class IWSPD
39 {
40 public:
42  virtual void onWellSeparatedPair(int aIndex, int bIndex) = 0;
43 };
44 
45 template<int Dim>
46 class DTreeWSPD
47 {
48 public:
49  using IntType = unsigned int;
51 
53  explicit DTreeWSPD(int numPoints);
54 
56  ~DTreeWSPD();
57 
59  struct NodeData {
61  double x[Dim];
62 
64  double min_x[Dim];
65 
67  double max_x[Dim];
68 
70  double radius_sq;
71  };
72 
74  struct PointData {
76  double x[Dim];
77  };
78 
80  void update();
81 
83  const Tree& tree() const { return *m_pTree; };
84 
86  const NodeData& node(int i) const { return m_nodeData[i]; };
87 
89  void setPoint(int i, int d, double coord);
90 
92  const PointData& point(int i) const { return m_pointData[i]; };
93 
94  // temp function for
95  void computeWSPD(IWSPD* m_pIWSPD);
96 
98  double separationFactor() const { return m_wspdSeparationFactor; };
99 
101  void setSeparationFactor(double s) { m_wspdSeparationFactor = s; };
102 
103 protected:
104  IWSPD* m_pIWSPD = nullptr;
105 
107  NodeData& node(int i) { return m_nodeData[i]; };
108 
110  void updateBoundingBox();
111 
113  void updateTreeGridPoints();
114 
116  void updateTreeNodeGeometry();
117 
119  void updateTreeNodeGeometry(int curr);
120 
122  void wspdRecursive(int a);
123 
125  void wspdRecursive(int a, int b);
126 
128  bool areWellSeparated(int a, int b) const;
129 
131  void allocate();
132 
134  void deallocate();
135 
138 
141 
144 
146  Tree* m_pTree = nullptr;
147 
149  NodeData* m_nodeData = nullptr;
150 
152  PointData* m_pointData = nullptr;
153 
155  double m_bboxMin[Dim];
156 
158  double m_bboxMax[Dim];
159 };
160 
162 template<int Dim>
164 : m_numPoints(numPoints)
165 , m_wspdSeparationFactor(1.0)
166 , m_wspdSeparationFactorPlus2Squared_cached(9.0)
167 {
168  // get the memory
169  allocate();
170 
171  // init arrays
172  for (int i = 0; i < Dim; i++) {
173  m_bboxMin[i] = m_bboxMax[i] = 0.0;
174  }
175 }
176 
178 template<int Dim>
180 {
181  // free the mem
182  deallocate();
183 }
184 
185 template<int Dim>
187 {
188  m_pTree = new Tree(m_numPoints);
191 }
192 
193 template<int Dim>
195 {
196  delete m_pTree;
197  delete[] m_nodeData;
198  delete[] m_pointData;
199 }
200 
201 template<int Dim>
203 {
204  // update the bounding box of the point set
206 
207  // update grid points inside the quadtree
209 
210  // rebuild the tree
211  m_pTree->build();
212 
213  // compute center, radius and bbox for each node
215 }
216 
217 template<int Dim>
219 {
220  m_pIWSPD = pIWSPD;
221 
222  // update this cached value for the well-sep test
223  const double s = (m_wspdSeparationFactor + 2.0);
224 
225  // precompute this
227 
228  // go ahead with the decomposition
230 }
231 
232 
233 // the unary recursive function generating the binary calls
234 template<int Dim>
236 {
237  // iterate over all ordered pairs of children
238  for (int i = 0; i < m_pTree->numChilds(curr); i++) {
239  // the first child index
240  const int first_child = m_pTree->child(curr, i);
241 
242  // the second loop for the pair
243  for (int j = i+1; j < m_pTree->numChilds(curr); j++) {
244  // the second child index
245  const int second_child = m_pTree->child(curr, j);
246 
247  // call for each ordered pair the binary function
248  wspdRecursive(first_child, second_child);
249  }
250 
251  // now do all this for every child
252  wspdRecursive(first_child);
253  }
254 }
255 
256 template<int Dim>
258 {
259  if (areWellSeparated(a, b)) {
260  // far enough away => approx
261  if (m_pIWSPD)
263  } else {
264  // two cells are too close
265  int small_node = a;
266  int large_node = b;
267 
268  // make sure the small one is not the bigger one
269  if (node(small_node).radius_sq > node(large_node).radius_sq) {
270  std::swap(small_node, large_node);
271  }
272 
273  // split the bigger one
274  for (int i = 0; i < tree().numChilds(large_node); ++i) {
275  // recurse on the child
276  wspdRecursive(small_node, tree().child(large_node, i));
277  }
278  }
279 }
280 
281 template<int Dim>
282 bool DTreeWSPD<Dim>::areWellSeparated(int a, int b) const
283 {
284  // take the bigger radius
285  double r_max_sq = std::max(node(a).radius_sq, node(b).radius_sq);
286 
287  // compute the square distance
288  double dist_sq = 0.0;
289 
290  // the d^2 loop
291  for (int d = 0; d < Dim; d++) {
292  double dx = node(a).x[d] - node(b).x[d];
293  dist_sq += dx * dx;
294  }
295 
296  // two circles are well separated iff d - 2r > sr
297  // where d is the distance between the two centers
298  // (not the distance of circles!)
299  // this ws <=> d > (s + 2)r
300  // more efficient: d^2 > (s + 2)^2 r^2
301  // d_sq > (s^2 + 4s + 4) * r_max
302 #if 0
303 # if 0
304  const double s = (m_wspdSeparationFactor + 2.0);
305  return dist_sq > (m_wspdSeparationFactor * m_wspdSeparationFactor + 4.0 * m_wspdSeparationFactor + 4) * r_max * r_max;
306 # else
307  return dist_sq > s * s * r_max_sq;
308 # endif
309 #else
310  return dist_sq > m_wspdSeparationFactorPlus2Squared_cached * r_max_sq;
311 #endif
312 }
313 
314 template<>
315 bool DTreeWSPD<2>::areWellSeparated(int a, int b) const
316 {
317  // take the bigger radius
318  double r_max_sq = std::max(node(a).radius_sq, node(b).radius_sq);
319 
320  // compute the square distance
321  double dx_0 = node(a).x[0] - node(b).x[0];
322  double dx_1 = node(a).x[1] - node(b).x[1];
323  double dist_sq = dx_0 * dx_0 + dx_1 * dx_1;
324 
325  // compare the sq distance
326  return dist_sq > m_wspdSeparationFactorPlus2Squared_cached * r_max_sq;
327 }
328 
329 template<>
330 bool DTreeWSPD<3>::areWellSeparated(int a, int b) const
331 {
332  // take the bigger radius
333  double r_max_sq = std::max(node(a).radius_sq, node(b).radius_sq);
334 
335  // compute the square distance
336  double dx_0 = node(a).x[0] - node(b).x[0];
337  double dx_1 = node(a).x[1] - node(b).x[1];
338  double dx_2 = node(a).x[2] - node(b).x[2];
339  double dist_sq = dx_0 * dx_0 + dx_1 * dx_1 + dx_2 * dx_2;
340 
341  // compare the sq distance
342  return dist_sq > m_wspdSeparationFactorPlus2Squared_cached * r_max_sq;
343 }
344 
345 
347 template<int Dim>
348 void DTreeWSPD<Dim>::setPoint(int i, int d, double coord)
349 {
350  m_pointData[i].x[d] = coord;
351 }
352 
353 template<int Dim>
355 {
356  if (!m_numPoints)
357  return;
358 
359  // initial values
360  for (int d = 0; d < Dim; d++) {
361  m_bboxMin[d] = m_bboxMax[d] = point(0).x[d];
362  }
363 
364  // no magic here
365  for (int i = 1; i < m_numPoints; i++) {
366  for (int d = 0; d < Dim; d++) {
367  m_bboxMin[d] = std::min(m_bboxMin[d], point(i).x[d]);
368  m_bboxMax[d] = std::max(m_bboxMax[d], point(i).x[d]);
369  }
370  }
371 }
372 
373 // updates the integer grid points in the quadtree
374 template<int Dim>
376 {
377  for (int d = 0; d < Dim; d++) {
378  double noise_max = (m_bboxMax[d] - m_bboxMin[d]) * 0.25;
379  m_bboxMax[d] += randomDouble(0.0, noise_max);
380  m_bboxMin[d] -= randomDouble(0.0, noise_max);
381  }
382  // lets assume the bbox is updated. find the longest side
383  double quad_size = m_bboxMax[0] - m_bboxMin[0];
384  for (int d = 1; d < Dim; d++) {
385  quad_size = std::max(quad_size, m_bboxMax[d] - m_bboxMin[d]);
386  }
387 
388  const double factor = (double)(0xffffffff);
389 
390  double scale = factor / quad_size;
391  // iterate over all points
392  for (int i = 0; i < m_numPoints; i++) {
393  for (int d = 0; d < Dim; d++) {
394  // put it in the bounding square
395 #if 0
396  double nx = ((point(i).x[d] - m_bboxMin[d] + 0.01) / quad_size + 0.02);
397 #endif
398  double nx = ((point(i).x[d] - m_bboxMin[d]) * scale);
399 
400  // dirty put on grid here
401  unsigned int ix = static_cast<unsigned int>(nx); //nx * (double)(unsigned int)(0x1fffffff);
402 
403  // set the point coord
404  m_pTree->setPoint(i, d, ix);
405  }
406  }
407 }
408 
409 template<int Dim>
411 {
413 }
414 
415 // updates the geometry of the quadtree nodes
416 template<int Dim>
418 {
419  // if this is an inner node
420  if (m_pTree->numChilds(curr)) {
421  // init with the bbox of the first child
422 
423  // iterate over the rest of the children
424  for (int i = 0; i < m_pTree->numChilds(curr); i++) {
425  // the child index
426  const int child = m_pTree->child(curr, i);
427 
428  // compute the size of the subtree
429  updateTreeNodeGeometry(child);
430 
431  // lazy set the first
432  if (!i) {
433  for (int d = 0; d < Dim; d++) {
434  node(curr).min_x[d] = node(child).min_x[d];
435  node(curr).max_x[d] = node(child).max_x[d];
436  }
437  } else {
438  for (int d = 0; d < Dim; d++) {
439  node(curr).min_x[d] = std::min(node(curr).min_x[d], node(child).min_x[d]);
440  node(curr).max_x[d] = std::max(node(curr).max_x[d], node(child).max_x[d]);
441  }
442  }
443  };
444  } else { // else it is a leaf
445  // init from first point
446  for (int d = 0; d < Dim; d++) {
447  node(curr).min_x[d] = node(curr).max_x[d] = point(tree().point(curr,0)).x[d];
448  }
449 
450  // and the remaining points
451  for (int i = 1; i < tree().numPoints(curr); ++i) {
452  for (int d = 0; d < Dim; d++) {
453  node(curr).min_x[d] = std::min(node(curr).min_x[d], point(tree().point(curr,i)).x[d]);
454  node(curr).max_x[d] = std::max(node(curr).max_x[d], point(tree().point(curr,i)).x[d]);
455  }
456  }
457  }
458 
459  // init the radius
460  node(curr).radius_sq = 0.0;
461 
462  // compute the center and radius of the rect
463  for (int d = 0; d < Dim; d++) {
464  node(curr).x[d] = (node(curr).min_x[d] + node(curr).max_x[d]) * 0.5;
465  double s_x = (node(curr).max_x[d] - node(curr).min_x[d]);
466  node(curr).radius_sq += s_x * s_x;
467  }
468 
469  // and the smallest enclosing circle radius squared
470  node(curr).radius_sq *= 0.25; // sqrt(node(curr).radius) * 0.5 squared;
471 }
472 
473 }}}
const Tree & tree() const
returns the corresponding Dtree
Definition: DTreeWSPD.h:83
int numPoints(int i) const
returns the number of points covered by this subtree
Definition: DTree.h:112
void allocate()
allocate mem
Definition: DTreeWSPD.h:186
void updateBoundingBox()
updates the bounding box by iterating over all points
Definition: DTreeWSPD.h:354
void updateTreeNodeGeometry()
updates the geometry of the quadtree nodes
Definition: DTreeWSPD.h:410
Implentation of the reduced quadtree for Dim dimensions.
Definition: DTree.h:40
double x[Dim]
center of cell circle
Definition: DTreeWSPD.h:61
void updateTreeGridPoints()
updates the integer grid points in the quadtree
Definition: DTreeWSPD.h:375
void wspdRecursive(int a)
the unary recursive function generating the binary calls
Definition: DTreeWSPD.h:235
int m_numPoints
number of points
Definition: DTreeWSPD.h:137
double x[Dim]
coords of the point
Definition: DTreeWSPD.h:76
void setSeparationFactor(double s)
sets the parameter s of the WSPD (default is 1.0)
Definition: DTreeWSPD.h:101
NodeData * m_nodeData
geometry for the quadtree nodes
Definition: DTreeWSPD.h:149
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
world coordinates of the points
Definition: DTreeWSPD.h:74
int numChilds(int i) const
returns the number of children of node i
Definition: DTree.h:106
double m_bboxMin[Dim]
the bounding box min coord of the point set
Definition: DTreeWSPD.h:155
PointData * m_pointData
point data
Definition: DTreeWSPD.h:152
void build()
Does all required steps except the allocate, deallocate, randomPoints.
Definition: DTree.h:373
const NodeData & node(int i) const
returns the data for a quadtree
Definition: DTreeWSPD.h:86
void update()
call this when the point set has been updated.
Definition: DTreeWSPD.h:202
double min_x[Dim]
bounding box min coord
Definition: DTreeWSPD.h:64
double m_wspdSeparationFactor
the separation factor for the ws predicate
Definition: DTreeWSPD.h:140
double m_wspdSeparationFactorPlus2Squared_cached
a cached value for the ws test
Definition: DTreeWSPD.h:143
double randomDouble(double low, double high)
Returns a random double value from the interval [low, high).
double m_bboxMax[Dim]
the bounding box max coord of the point set
Definition: DTreeWSPD.h:158
DTree< IntType, Dim > Tree
Definition: DTreeWSPD.h:50
void setPoint(int i, int d, double coord)
sets the point to the given coords
Definition: DTreeWSPD.h:348
int rootIndex() const
returns the index of the root node
Definition: DTree.h:160
double max_x[Dim]
bounding box min coord
Definition: DTreeWSPD.h:67
void computeWSPD(IWSPD *m_pIWSPD)
Definition: DTreeWSPD.h:218
double radius_sq
radius of the cell
Definition: DTreeWSPD.h:70
int maxNumNodes() const
returns the maximum number of nodes (and the max index of a node)
Definition: DTree.h:124
double separationFactor() const
returns the parameter s of the WSPD (default is 1.0)
Definition: DTreeWSPD.h:98
Basic declarations, included by all source files.
geometry for the quadtree nodes
Definition: DTreeWSPD.h:59
bool areWellSeparated(int a, int b) const
predicate for determining if cells are well-separated
Definition: DTreeWSPD.h:282
The namespace for all OGDF objects.
NodeData & node(int i)
returns the data for a quadtree
Definition: DTreeWSPD.h:107
virtual void onWellSeparatedPair(int aIndex, int bIndex)=0
called by the WSPD for a pair that is well separated
Tree * m_pTree
the quadtree this wspd is working on
Definition: DTreeWSPD.h:146
const PointData & point(int i) const
return ith point
Definition: DTreeWSPD.h:92
DTreeWSPD(int numPoints)
constructs a new WSPD for numPoints
Definition: DTreeWSPD.h:163