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];
40 void graph_layout_prepare() {
44 breadth_first_search(v1, sqdistances[v1]);
47 beta= -log(10)/log(alpha);
48 beta_prime= (1-beta)/2;
49 printf("alpha=%g beta=%g beta'=%g\n", alpha,beta,beta_prime);
53 double graph_layout_cost(const Vertices v, const double vertex_areas[N]) {
54 /* For each (vi,vj) computes shortest path s_ij = |vi..vj|
55 * along edges, and actual distance d_ij = |vi-vj|.
57 * We will also use the `vertex areas': for each vertex vi the
58 * vertex area a_vi is the mean area of the incident triangles.
59 * This is computed elsewhere.
61 * Energy contribution is proportional to
64 * a a . d . [ (s/d) - 1 ]
67 * (In practice we compute d^2+epsilon and use it for the
68 * divisions, to avoid division by zero.)
70 //static const double d2_epsilon= 1e-6;
73 int v1,v2,e, nedges=0;
74 double totaledgelength=0, meanedgelength, meanedgelength2;
77 totaledgelength += hypotD(v[v1], v[v2]);
81 meanedgelength= totaledgelength / nedges;
82 meanedgelength2= meanedgelength * meanedgelength;
83 // printf("mean=%g mean^2=%g\n", meanedgelength, meanedgelength2);
87 if (v1 == v2) continue;
89 double d2= hypotD2(v[v1],v[v2]);
91 int dist2= sqdistances[v1][v2];
94 double s2= dist2 * meanedgelength2;
96 /* energy = (d/s)^(1-beta) where beta is -log\_{alpha}(10)
97 * energy = ((d/s)^2) ^ (1-beta)/2
98 * let beta' = (1-beta)/2
101 double cost= pow(d2/s2, beta_prime);
103 //printf("layout %03x..%03x dist^2=%d s^2=%g d^2=%g "
104 //" cost+=%g\n", v1,v2, dist2,