9 static int sqdistances[N][N];
11 static double alpha, beta, beta_prime;
13 static void breadth_first_search(int start, int sqdistances_r[N]) {
14 int d[N], buffer[N], *buf_pop=buffer, *buf_push=buffer;
15 int v,e, current, future, dfuture;
17 buf_push= buf_pop= buffer;
18 FOR_VERTEX(v,INNER) d[v]= -1;
23 while (buf_pop < buf_push) {
25 dfuture= d[current] + 1;
26 FOR_VEDGE(current,e,future) {
27 if (d[future] >= 0) continue; /* already found this one */
32 assert(buf_pop==buf_push);
33 assert(buf_push <= buffer+sizeof(buffer)/sizeof(buffer[0]));
37 sqdistances_r[v]= d[v] * d[v];
44 void graph_layout_prepare(void) {
48 breadth_first_search(v1, sqdistances[v1]);
51 beta= log(10)/log(alpha);
52 beta_prime= (1-beta)/2;
53 printf("alpha=%g beta=%g beta'=%g\n", alpha,beta,beta_prime);
57 double graph_layout_cost(const Vertices v, int section) {
58 /* For each (vi,vj) computes shortest path s_ij = |vi..vj|
59 * along edges, and actual distance d_ij = |vi-vj|.
61 * We will also use the `vertex areas': for each vertex vi the
62 * vertex area a_vi is the mean area of the incident triangles.
63 * This is computed elsewhere.
65 * Energy contribution is proportional to
68 * a a . d . [ (s/d) - 1 ]
71 * (In practice we compute d^2+epsilon and use it for the
72 * divisions, to avoid division by zero.)
74 //static const double d2_epsilon= 1e-6;
77 int v1,v2,e, nedges=0;
78 double totaledgelength=0, meanedgelength, meanedgelength2;
80 FOR_EDGE(v1,e,v2,INNER) {
81 totaledgelength += hypotD(v[v1], v[v2]);
85 meanedgelength= totaledgelength / nedges;
86 meanedgelength2= meanedgelength * meanedgelength;
87 // printf("mean=%g mean^2=%g\n", meanedgelength, meanedgelength2);
89 FOR_VERTEX(v1,OUTER) {
90 FOR_VERTEX(v2,INNER) {
91 if (v1 == v2) continue;
93 double d2= hypotD2(v[v1],v[v2]);
95 int dist2= sqdistances[v1][v2];
98 double s2= dist2 * meanedgelength2;
100 /* energy = (d/s)^(1-beta) where beta is log\_{alpha}(10)
101 * energy = ((d/s)^2) ^ (1-beta)/2
102 * let beta' = (1-beta)/2
105 double cost= (vertex_mean_edge_lengths[v1]) * pow(d2/s2, beta_prime);
107 //printf("layout %03x..%03x dist^2=%d s^2=%g d^2=%g "
108 //" cost+=%g\n", v1,v2, dist2,
113 return total_cost/meanedgelength;