8 static int sqdistances[N][N];
10 static double alpha, beta, beta_prime;
12 static void breadth_first_search(int start, int sqdistances_r[N]) {
13 int d[N], buffer[N], *buf_pop=buffer, *buf_push=buffer;
14 int v,e, current, future, dfuture;
16 buf_push= buf_pop= buffer;
17 FOR_VERTEX(v) d[v]= -1;
22 while (buf_pop < buf_push) {
24 dfuture= d[current] + 1;
25 FOR_VEDGE(current,e,future) {
26 if (d[future] >= 0) continue; /* already found this one */
31 assert(buf_pop==buf_push);
32 assert(buf_push <= buffer+sizeof(buffer)/sizeof(buffer[0]));
36 sqdistances_r[v]= d[v] * d[v];
43 void graph_layout_prepare() {
47 breadth_first_search(v1, sqdistances[v1]);
50 beta= log(10)/log(alpha);
51 beta_prime= (1-beta)/2;
52 printf("alpha=%g beta=%g beta'=%g\n", alpha,beta,beta_prime);
56 double graph_layout_cost(const Vertices v) {
57 /* For each (vi,vj) computes shortest path s_ij = |vi..vj|
58 * along edges, and actual distance d_ij = |vi-vj|.
60 * We will also use the `vertex areas': for each vertex vi the
61 * vertex area a_vi is the mean area of the incident triangles.
62 * This is computed elsewhere.
64 * Energy contribution is proportional to
67 * a a . d . [ (s/d) - 1 ]
70 * (In practice we compute d^2+epsilon and use it for the
71 * divisions, to avoid division by zero.)
73 //static const double d2_epsilon= 1e-6;
76 int v1,v2,e, nedges=0;
77 double totaledgelength=0, meanedgelength, meanedgelength2;
80 totaledgelength += hypotD(v[v1], v[v2]);
84 meanedgelength= totaledgelength / nedges;
85 meanedgelength2= meanedgelength * meanedgelength;
86 // printf("mean=%g mean^2=%g\n", meanedgelength, meanedgelength2);
90 if (v1 == v2) continue;
92 double d2= hypotD2(v[v1],v[v2]);
94 int dist2= sqdistances[v1][v2];
97 double s2= dist2 * meanedgelength2;
99 /* energy = (d/s)^(1-beta) where beta is log\_{alpha}(10)
100 * energy = ((d/s)^2) ^ (1-beta)/2
101 * let beta' = (1-beta)/2
104 double cost= (vertex_mean_edge_lengths[v1]) * pow(d2/s2, beta_prime);
106 //printf("layout %03x..%03x dist^2=%d s^2=%g d^2=%g "
107 //" cost+=%g\n", v1,v2, dist2,
112 return total_cost/meanedgelength;