Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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;
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 
147 
150 
153 
155  double m_bboxMin[Dim];
156 
158  double m_bboxMax[Dim];
159 };
160 
162 template<int Dim>
163 DTreeWSPD<Dim>::DTreeWSPD(int numPoints) : m_numPoints(numPoints)
164 {
165  // initial value for the wspd predicate
167 
168  // get the memory
169  allocate();
170 }
171 
173 template<int Dim>
175 {
176  // free the mem
177  deallocate();
178 }
179 
180 template<int Dim>
182 {
183  m_pTree = new Tree(m_numPoints);
186 }
187 
188 template<int Dim>
190 {
191  delete m_pTree;
192  delete[] m_nodeData;
193  delete[] m_pointData;
194 }
195 
196 template<int Dim>
198 {
199  // update the bounding box of the point set
201 
202  // update grid points inside the quadtree
204 
205  // rebuild the tree
206  m_pTree->build();
207 
208  // compute center, radius and bbox for each node
210 }
211 
212 template<int Dim>
214 {
215  m_pIWSPD = pIWSPD;
216 
217  // update this cached value for the well-sep test
218  const double s = (m_wspdSeparationFactor + 2.0);
219 
220  // precompute this
222 
223  // go ahead with the decomposition
225 }
226 
227 
228 // the unary recursive function generating the binary calls
229 template<int Dim>
231 {
232  // iterate over all ordered pairs of children
233  for (int i = 0; i < m_pTree->numChilds(curr); i++) {
234  // the first child index
235  const int first_child = m_pTree->child(curr, i);
236 
237  // the second loop for the pair
238  for (int j = i+1; j < m_pTree->numChilds(curr); j++) {
239  // the second child index
240  const int second_child = m_pTree->child(curr, j);
241 
242  // call for each ordered pair the binary function
243  wspdRecursive(first_child, second_child);
244  }
245 
246  // now do all this for every child
247  wspdRecursive(first_child);
248  }
249 }
250 
251 template<int Dim>
253 {
254  if (areWellSeparated(a, b)) {
255  // far enough away => approx
256  if (m_pIWSPD)
258  } else {
259  // two cells are too close
260  int small_node = a;
261  int large_node = b;
262 
263  // make sure the small one is not the bigger one
264  if (node(small_node).radius_sq > node(large_node).radius_sq) {
265  std::swap(small_node, large_node);
266  }
267 
268  // split the bigger one
269  for (int i = 0; i < tree().numChilds(large_node); ++i) {
270  // recurse on the child
271  wspdRecursive(small_node, tree().child(large_node, i));
272  }
273  }
274 }
275 
276 template<int Dim>
277 bool DTreeWSPD<Dim>::areWellSeparated(int a, int b) const
278 {
279  // take the bigger radius
280  double r_max_sq = std::max(node(a).radius_sq, node(b).radius_sq);
281 
282  // compute the square distance
283  double dist_sq = 0.0;
284 
285  // the d^2 loop
286  for (int d = 0; d < Dim; d++) {
287  double dx = node(a).x[d] - node(b).x[d];
288  dist_sq += dx * dx;
289  }
290 
291  // two circles are well separated iff d - 2r > sr
292  // where d is the distance between the two centers
293  // (not the distance of circles!)
294  // this ws <=> d > (s + 2)r
295  // more efficient: d^2 > (s + 2)^2 r^2
296  // d_sq > (s^2 + 4s + 4) * r_max
297 #if 0
298 # if 0
299  const double s = (m_wspdSeparationFactor + 2.0);
300  return dist_sq > (m_wspdSeparationFactor * m_wspdSeparationFactor + 4.0 * m_wspdSeparationFactor + 4) * r_max * r_max;
301 # else
302  return dist_sq > s * s * r_max_sq;
303 # endif
304 #else
305  return dist_sq > m_wspdSeparationFactorPlus2Squared_cached * r_max_sq;
306 #endif
307 }
308 
309 template<>
310 bool DTreeWSPD<2>::areWellSeparated(int a, int b) const
311 {
312  // take the bigger radius
313  double r_max_sq = std::max(node(a).radius_sq, node(b).radius_sq);
314 
315  // compute the square distance
316  double dx_0 = node(a).x[0] - node(b).x[0];
317  double dx_1 = node(a).x[1] - node(b).x[1];
318  double dist_sq = dx_0 * dx_0 + dx_1 * dx_1;
319 
320  // compare the sq distance
321  return dist_sq > m_wspdSeparationFactorPlus2Squared_cached * r_max_sq;
322 }
323 
324 template<>
325 bool DTreeWSPD<3>::areWellSeparated(int a, int b) const
326 {
327  // take the bigger radius
328  double r_max_sq = std::max(node(a).radius_sq, node(b).radius_sq);
329 
330  // compute the square distance
331  double dx_0 = node(a).x[0] - node(b).x[0];
332  double dx_1 = node(a).x[1] - node(b).x[1];
333  double dx_2 = node(a).x[2] - node(b).x[2];
334  double dist_sq = dx_0 * dx_0 + dx_1 * dx_1 + dx_2 * dx_2;
335 
336  // compare the sq distance
337  return dist_sq > m_wspdSeparationFactorPlus2Squared_cached * r_max_sq;
338 }
339 
340 
342 template<int Dim>
343 void DTreeWSPD<Dim>::setPoint(int i, int d, double coord)
344 {
345  m_pointData[i].x[d] = coord;
346 }
347 
348 template<int Dim>
350 {
351  if (!m_numPoints)
352  return;
353 
354  // initial values
355  for (int d = 0; d < Dim; d++) {
356  m_bboxMin[d] = m_bboxMax[d] = point(0).x[d];
357  }
358 
359  // no magic here
360  for (int i = 1; i < m_numPoints; i++) {
361  for (int d = 0; d < Dim; d++) {
362  m_bboxMin[d] = std::min(m_bboxMin[d], point(i).x[d]);
363  m_bboxMax[d] = std::max(m_bboxMax[d], point(i).x[d]);
364  }
365  }
366 }
367 
368 // updates the integer grid points in the quadtree
369 template<int Dim>
371 {
372  for (int d = 0; d < Dim; d++) {
373  double noise_max = (m_bboxMax[d] - m_bboxMin[d]) * 0.25;
374  m_bboxMax[d] += randomDouble(0.0, noise_max);
375  m_bboxMin[d] -= randomDouble(0.0, noise_max);
376  }
377  // lets assume the bbox is updated. find the longest side
378  double quad_size = m_bboxMax[0] - m_bboxMin[0];
379  for (int d = 1; d < Dim; d++) {
380  quad_size = std::max(quad_size, m_bboxMax[d] - m_bboxMin[d]);
381  }
382 
383  const double factor = (double)(0xffffffff);
384 
385  double scale = factor / quad_size;
386  // iterate over all points
387  for (int i = 0; i < m_numPoints; i++) {
388  for (int d = 0; d < Dim; d++) {
389  // put it in the bounding square
390 #if 0
391  double nx = ((point(i).x[d] - m_bboxMin[d] + 0.01) / quad_size + 0.02);
392 #endif
393  double nx = ((point(i).x[d] - m_bboxMin[d]) * scale);
394 
395  // dirty put on grid here
396  unsigned int ix = static_cast<unsigned int>(nx); //nx * (double)(unsigned int)(0x1fffffff);
397 
398  // set the point coord
399  m_pTree->setPoint(i, d, ix);
400  }
401  }
402 }
403 
404 template<int Dim>
406 {
408 }
409 
410 // updates the geometry of the quadtree nodes
411 template<int Dim>
413 {
414  // if this is an inner node
415  if (m_pTree->numChilds(curr)) {
416  // init with the bbox of the first child
417 
418  // iterate over the rest of the children
419  for (int i = 0; i < m_pTree->numChilds(curr); i++) {
420  // the child index
421  const int child = m_pTree->child(curr, i);
422 
423  // compute the size of the subtree
424  updateTreeNodeGeometry(child);
425 
426  // lazy set the first
427  if (!i) {
428  for (int d = 0; d < Dim; d++) {
429  node(curr).min_x[d] = node(child).min_x[d];
430  node(curr).max_x[d] = node(child).max_x[d];
431  }
432  } else {
433  for (int d = 0; d < Dim; d++) {
434  node(curr).min_x[d] = std::min(node(curr).min_x[d], node(child).min_x[d]);
435  node(curr).max_x[d] = std::max(node(curr).max_x[d], node(child).max_x[d]);
436  }
437  }
438  };
439  } else { // else it is a leaf
440  // init from first point
441  for (int d = 0; d < Dim; d++) {
442  node(curr).min_x[d] = node(curr).max_x[d] = point(tree().point(curr,0)).x[d];
443  }
444 
445  // and the remaining points
446  for (int i = 1; i < tree().numPoints(curr); ++i) {
447  for (int d = 0; d < Dim; d++) {
448  node(curr).min_x[d] = std::min(node(curr).min_x[d], point(tree().point(curr,i)).x[d]);
449  node(curr).max_x[d] = std::max(node(curr).max_x[d], point(tree().point(curr,i)).x[d]);
450  }
451  }
452  }
453 
454  // init the radius
455  node(curr).radius_sq = 0.0;
456 
457  // compute the center and radius of the rect
458  for (int d = 0; d < Dim; d++) {
459  node(curr).x[d] = (node(curr).min_x[d] + node(curr).max_x[d]) * 0.5;
460  double s_x = (node(curr).max_x[d] - node(curr).min_x[d]);
461  node(curr).radius_sq += s_x * s_x;
462  }
463 
464  // and the smallest enclosing circle radius squared
465  node(curr).radius_sq *= 0.25; // sqrt(node(curr).radius) * 0.5 squared;
466 }
467 
468 }}}
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:110
void allocate()
allocate mem
Definition: DTreeWSPD.h:181
void updateBoundingBox()
updates the bounding box by iterating over all points
Definition: DTreeWSPD.h:349
void updateTreeNodeGeometry()
updates the geometry of the quadtree nodes
Definition: DTreeWSPD.h:405
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:370
void wspdRecursive(int a)
the unary recursive function generating the binary calls
Definition: DTreeWSPD.h:230
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:107
void setPoint(int i, int d, IntType value)
sets the point to the given grid coords
Definition: DTree.h:197
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:104
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:371
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:197
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:343
int rootIndex() const
returns the index of the root node
Definition: DTree.h:158
double max_x[Dim]
bounding box min coord
Definition: DTreeWSPD.h:67
void computeWSPD(IWSPD *m_pIWSPD)
Definition: DTreeWSPD.h:213
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:122
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:277
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