OpenGraph DrawingFramework

v. 2023.09 (Elderberry)

Searching...
No Matches
DTreeForceTypes.h
Go to the documentation of this file.
1
29#pragma once
30
31#include <ogdf/basic/basic.h>
32
33#include <type_traits>
34
35namespace ogdf {
36namespace energybased {
37namespace dtree {
38
39template<int Dim>
40inline typename std::enable_if<Dim != 2, double>::type computeDeltaAndDistance(const double a[Dim],
41 const double b[Dim], double delta[Dim]) {
42 // distance var
43 double dist = 0;
44
45 // for all dim
46 for (int d = 0; d < Dim; d++) {
47 // delta in d dim
48 delta[d] = a[d] - b[d];
49
50 // squared distance sum
51 dist += delta[d] * delta[d];
52 }
53
54 // distance square root
55 return sqrt(dist);
56}
57
58template<int Dim>
59inline typename std::enable_if<Dim == 2, double>::type computeDeltaAndDistance(const double a[Dim],
60 const double b[Dim], double delta[Dim]) {
61 // delta in d dim
62 delta[0] = a[0] - b[0];
63 delta[1] = a[1] - b[1];
64
65 // distance square root
66 return sqrt(delta[0] * delta[0] + delta[1] * delta[1]);
67}
68
69template<int Dim, int K>
70inline typename std::enable_if<Dim != 2 || (K != 1 && K != 2), void>::type RepForceFunctionNewton(
71 double dist, double& force, double& force_prime) {
72 // distance square root
73 dist = dist + 0.1;
74
75 double t = dist;
76 for (int i = 1; i < K; i++) {
77 t *= dist;
78 }
79
80 force = 1.0 / (t);
81 force_prime = (double)(K) / (t * dist);
82}
83
84template<int Dim, int K>
85inline typename std::enable_if<Dim == 2 && K == 2, void>::type RepForceFunctionNewton(double dist,
86 double& force, double& force_prime) {
87 // distance square root
88 dist = dist + 0.1;
89 force_prime = 2.0 / (dist * dist * dist);
90 force = force_prime * dist * 0.5;
91}
92
93template<int Dim, int K>
94inline typename std::enable_if<Dim == 2 && K == 1, void>::type RepForceFunctionNewton(double dist,
95 double& force, double& force_prime) {
96 // distance square root
97 dist = dist + 0.1;
98 force_prime = 1.0 / (dist * dist);
99 force = 1.0 / (dist);
100}
101
102template<int Dim>
103inline void AttrForceFunctionLog(double dist, double& force, double& force_prime) {
104 // distance square root
105 force = log(dist / 1.0);
106 force_prime = 1.0 / dist;
107}
108
109template<int Dim, int K>
110inline typename std::enable_if<Dim != 2 || (K != 1 && K != 2), void>::type AttrForceFunctionPow(
111 double dist, double& force, double& force_prime) {
112 // distance square root
113 dist = dist + 0.1;
114
115 double t = dist;
116 for (int i = 1; i < K; i++) {
117 t *= dist;
118 }
119
120 force = t;
121 force_prime = (double)(K) * (t / dist);
122}
123
124template<int Dim, int K>
125inline typename std::enable_if<Dim == 2 && K == 2, void>::type AttrForceFunctionPow(double dist,
126 double& force, double& force_prime) {
127 // distance square root
128 dist = dist + 0.1;
129
130 force = dist * dist;
131 force_prime = 2.0 * dist;
132}
133
134template<int Dim, int K>
135inline typename std::enable_if<Dim == 2 && K == 1, void>::type AttrForceFunctionPow(double dist,
136 double& force, double& force_prime) {
137 // distance square root
138 dist = dist + 0.1;
139
140 force = dist;
141 force_prime = 1.0;
142}
143
144#if 0
145template<int Dim>
146inline void RepForceFunctionInvGauss(const double a[Dim], const double b[Dim], double result[Dim])
147{
148 // the range of the repulsive force
149 const double force_range = 10.0;
150
151 // the amount of repulsive force
152 const double force_amount = 20.0;
153
154 // vector from b to a
155 double delta[Dim];
156
157 // distance var
158 double dist = 0;
159
160 // for all dim
161 for (int d = 0; d < Dim; d++) {
162 // delta in d dim
163 delta[d] = a[d] - b[d];
164
165 // squared distance sum
166 dist += delta[d] * delta[d];
167 }
168
169 // distance square root
170 dist = sqrt(dist);
171
172 // force function
173 double f = (exp(- (dist * dist) / force_range ) * force_amount);
174# if 0
175 double f = exp(-dist * dist * 0.01) * 10.0) / dist;
176# endif
177
178
179 // compute the force vector for each dim
180 for (int d = 0; d < Dim; d++) {
181 result[d] = delta[d] * f/dist;
182 };
183}
184#endif
185
186}
187}
188}
Basic declarations, included by all source files.