Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

DTreeForce.h
Go to the documentation of this file.
1 
29 #pragma once
30 
33 
34 namespace ogdf {
35 namespace energybased {
36 namespace dtree {
37 
38 template<int Dim, typename ForceFunc, bool UseForcePrime>
40 
41 template<int Dim>
43 {
44 public:
45  template<int _Dim, typename ForceFunc, bool UseForcePrime>
46  friend class DTreeWSPDCallback;
47 
49  using Tree = typename WSPD::Tree;
50 
52  explicit DTreeForce(int numPoints);
53 
55  virtual ~DTreeForce();
56 
58  double position(int i , int d) const;
59 
61  void setPosition(int i, int d, double c);
62 
64  double mass(int i) const;
65 
67  void setMass(int i, double m);
68 
70  double force(int i , int d) const;
71 
73  double force_prime(int i ) const;
74 
76  template<typename ForceFunc, bool UseForcePrime>
77  void computeForces(ForceFunc forceFunc);
78 
80  int numPoints() const { return m_numPoints; };
81 
83  const WSPD& wspd() const;
84 
86  const Tree& tree() const;
87 private:
89  struct PointData {
91  double mass;
92 
94  double force[Dim];
95 
96  double force_prime;
97  };
98 
100  struct NodeData {
102  double mass;
103 
105  double centerOfMass[Dim];
106 
108  double force[Dim];
109 
111  double force_prime;
112  };
113 
115  WSPD& wspd();
116 
118  void allocate();
119 
121  void deallocate();
122 
124  void bottomUpPhase(int curr);
125 
127  void topDownPhase(int curr);
128 
130  void resetPointForces();
131 
133  void resetTreeNodes();
134 
137 
140 
143 
146 };
147 
148 template<int Dim, typename ForceFunc, bool UseForcePrime>
149 class DTreeWSPDCallback : public IWSPD
150 {
151 public:
152  DTreeWSPDCallback(DTreeForce<Dim>& treeForce, ForceFunc forceFunc)
153  : m_treeForce(treeForce), m_forceFunc(forceFunc)
154  {
155  }
156 
158  virtual void onWellSeparatedPair(int a, int b)
159  {
160  // force vector
161  double delta[Dim];
162  double force;
163  double force_prime;
164 
165  double dist = computeDeltaAndDistance<Dim>(m_treeForce.m_nodeData[a].centerOfMass,
166  m_treeForce.m_nodeData[b].centerOfMass,
167  delta);
168  m_forceFunc(dist, force, force_prime);
169 #if 0
170  m_forceFunc(m_treeForce.m_nodeData[a].centerOfMass, m_treeForce.m_nodeData[b].centerOfMass, force, force_prime);
171 #endif
172 
173  // compute the force vector for each dim
174  for (int d = 0; d < Dim; d++) {
175  m_treeForce.m_nodeData[a].force[d] += force * delta[d] / dist * m_treeForce.m_nodeData[b].mass;
176  m_treeForce.m_nodeData[b].force[d] -= force * delta[d] / dist * m_treeForce.m_nodeData[a].mass;
177  };
178 
179  if (UseForcePrime) {
180  m_treeForce.m_nodeData[a].force_prime += force_prime * m_treeForce.m_nodeData[b].mass;
181  m_treeForce.m_nodeData[b].force_prime += force_prime * m_treeForce.m_nodeData[a].mass;
182  }
183 
184 
185  }
186 
188  ForceFunc m_forceFunc;
189 };
190 
191 template<int Dim>
193 {
194  // get the memory
195  allocate();
196 
198 }
199 
200 template<int Dim>
202 {
203  // free the mem
204  deallocate();
205 }
206 
207 template<int Dim>
209 {
210  m_pWSPD = new WSPD(m_numPoints);
211  m_nodeData = new NodeData[tree().maxNumNodes()];
213 
214  // initial mass setting for a point
215  for (int i = 0; i < m_numPoints; i++) {
216  m_pointData[i].mass = 1.0;
217  }
218 }
219 
220 template<int Dim>
222 {
223  delete m_pWSPD;
224  delete[] m_nodeData;
225  delete[] m_pointData;
226 }
227 
228 template<int Dim>
230 {
231  return *m_pWSPD;
232 }
233 
234 template<int Dim>
236 {
237  return *m_pWSPD;
238 }
239 
240 template<int Dim>
242 {
243  return wspd().tree();
244 }
245 
246 template<int Dim>
247 double DTreeForce<Dim>::position(int i , int d) const
248 {
249  return wspd().point(i).x[d];
250 }
251 
252 template<int Dim>
253 void DTreeForce<Dim>::setPosition(int i, int d, double c)
254 {
255  wspd().setPoint(i, d, c);
256 }
257 
258 template<int Dim>
259 double DTreeForce<Dim>::mass(int i) const
260 {
261  return m_pointData[i].mass;
262 }
263 
264 template<int Dim>
265 void DTreeForce<Dim>::setMass(int i, double m)
266 {
267  m_pointData[i].mass = m;
268 }
269 
270 template<int Dim>
271 double DTreeForce<Dim>::force(int i , int d) const
272 {
273  return m_pointData[i].force[d];
274 }
275 
276 template<int Dim>
277 double DTreeForce<Dim>::force_prime(int i) const
278 {
279  return m_pointData[i].force_prime;
280 }
281 
282 template<int Dim>
283 template<typename ForceFunc, bool UseForcePrime>
284 void DTreeForce<Dim>::computeForces(ForceFunc forceFunc)
285 {
286  // reset all point forces
288 
289  if (numPoints() <= 1)
290  return;
291 
292  // first let the WSPD instance update the quadtre
293  wspd().update();
294 
295  // now do the bottom up phase
296  bottomUpPhase(tree().rootIndex());
297 
298  DTreeWSPDCallback<Dim, ForceFunc, UseForcePrime> wspdCallBack(*this, forceFunc);
299 
300  // run the WSPD cell cell interaction
301  wspd().computeWSPD(&wspdCallBack);
302 
303  // finally, push down the forces
304  topDownPhase(tree().rootIndex());
305 }
306 
307 
308 template<int Dim>
310 {
311  // reset the force and the center of mass
312  for (int d = 0; d < Dim; d++) {
313  m_nodeData[curr].force[d] = 0.0;
314  m_nodeData[curr].force_prime = 0.0;
315  m_nodeData[curr].centerOfMass[d] = 0.0;
316  }
317 
318  // reset the mass
319  m_nodeData[curr].mass = 0.0;
320 
321  // if this is an inner node
322  if (tree().numChilds(curr)) {
323  // iterate over the children
324  for (int i = 0; i < tree().numChilds(curr); i++) {
325  // the child index
326  const int child = tree().child(curr, i);
327 
328  // compute the size of the subtree
329  bottomUpPhase(child);
330 
331  // node curr
332  for (int d = 0; d < Dim; d++) {
333  // sum up the center of mass coordinates
334  m_nodeData[curr].centerOfMass[d] += m_nodeData[child].centerOfMass[d] * m_nodeData[child].mass;
335  }
336 
337  // add the mass
338  m_nodeData[curr].mass += m_nodeData[child].mass;
339  };
340  } // else it is a leaf
341  else {
342  // and the remaining points
343  for (int i = 0; i < tree().numPoints(curr); ++i) {
344  int pointIndex = tree().point(curr, i);
345  // node curr
346  for (int d = 0; d < Dim; d++) {
347  m_nodeData[curr].centerOfMass[d] += position(pointIndex, d) * mass(pointIndex);
348  }
349 
350  m_nodeData[curr].mass += mass(pointIndex);
351  }
352  }
353  // node curr
354  for (int d = 0; d < Dim; d++) {
355  m_nodeData[curr].centerOfMass[d] /= m_nodeData[curr].mass;
356  }
357 }
358 
359 template<int Dim>
361 {
362  // if this is an inner node
363  if (tree().numChilds(curr)) {
364  // iterate over the children
365  for (int i = 0; i < tree().numChilds(curr); i++) {
366  // the child index
367  const int child = tree().child(curr, i);
368 
369  // node curr
370  for (int d = 0; d < Dim; d++) {
371  // push the force down to the child
372  m_nodeData[child].force[d] += m_nodeData[curr].force[d];
373  }
374 
376 
377  // compute the size of the subtree
378  topDownPhase(child);
379  };
380  } // else it is a leaf
381  else {
382  // and the remaining points
383  for (int i = 0; i < tree().numPoints(curr); ++i) {
384  // node curr
385  int pointIndex = tree().point(curr, i);
386 
387  // node curr
388  for (int d = 0; d < Dim; d++) {
389  // push the force down to the child
390  m_pointData[pointIndex].force[d] = m_nodeData[curr].force[d] * mass(pointIndex);
391  }
392 
393  m_pointData[pointIndex].force_prime = m_nodeData[curr].force_prime * mass(pointIndex);
394  }
395  }
396 }
397 
398 template<int Dim>
400 {
401  for (int i = 0; i < m_numPoints; i++) {
402  for (int d = 0; d < Dim; d++) {
403  m_pointData[i].force[d] = 0.0;
404  }
405  m_pointData[i].force_prime = 0.0;
406  }
407 }
408 
409 }}}
virtual void onWellSeparatedPair(int a, int b)
called by the WSPD for well separated pair
Definition: DTreeForce.h:158
void computeForces(ForceFunc forceFunc)
main call
Definition: DTreeForce.h:284
WSPD * m_pWSPD
the WSPD instance
Definition: DTreeForce.h:145
const Tree & tree() const
returns the corresponding Dtree
Definition: DTreeWSPD.h:83
double position(int i, int d) const
returns d-th coordinate of the i-th point
Definition: DTreeForce.h:247
double force_prime(int i) const
returns derivation of the d-th coordinate of the i-th force vector
Definition: DTreeForce.h:277
DTreeWSPDCallback(DTreeForce< Dim > &treeForce, ForceFunc forceFunc)
Definition: DTreeForce.h:152
void bottomUpPhase(int curr)
internal function for computing the mass and center of mass of quadtree nodes
Definition: DTreeForce.h:309
const WSPD & wspd() const
returns a const ref to the wspd
Definition: DTreeForce.h:229
void resetPointForces()
reset the point forces
Definition: DTreeForce.h:399
double force_prime
the first derivation of the distance based force function
Definition: DTreeForce.h:111
PointData * m_pointData
array for the point related data
Definition: DTreeForce.h:136
virtual ~DTreeForce()
destructor
Definition: DTreeForce.h:201
int numPoints() const
returns the number of points
Definition: DTreeForce.h:80
double mass(int i) const
returns the mass of the i-th point
Definition: DTreeForce.h:259
void resetTreeNodes()
reset the tree nodes
DTreeForce(int numPoints)
constructs a new WSPD (well-separated pair decomposition) for numPoints
Definition: DTreeForce.h:192
void setSeparationFactor(double s)
sets the parameter s of the WSPD (default is 1.0)
Definition: DTreeWSPD.h:101
void setPosition(int i, int d, double c)
sets the d-th coordinate of the i-th point
Definition: DTreeForce.h:253
void update()
call this when the point set has been updated.
Definition: DTreeWSPD.h:202
void allocate()
internal for allocating mem
Definition: DTreeForce.h:208
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
void computeWSPD(IWSPD *m_pIWSPD)
Definition: DTreeWSPD.h:218
void deallocate()
internal for allocating mem
Definition: DTreeForce.h:221
const Tree & tree() const
returns a const reference to the tree
Definition: DTreeForce.h:241
NodeData * m_nodeData
array for the node related data
Definition: DTreeForce.h:139
double centerOfMass[Dim]
center of mass of the subtree
Definition: DTreeForce.h:105
The namespace for all OGDF objects.
void setMass(int i, double m)
sets the mass of the i-th point
Definition: DTreeForce.h:265
double force(int i, int d) const
returns d-th coordinate of the i-th force vector
Definition: DTreeForce.h:271
void topDownPhase(int curr)
internal function for accumulating forces in the leaves
Definition: DTreeForce.h:360
const PointData & point(int i) const
return ith point
Definition: DTreeWSPD.h:92
int m_numPoints
the number of points
Definition: DTreeForce.h:142