chiark / gitweb /
allow pausing updates
[moebius2.git] / graph.c
1 /*
2  * graph layout energy
3  */
4
5 #include "mgraph.h"
6 #include "minimise.h"
7
8 static int sqdistances[N][N];
9
10 static double alpha, beta, beta_prime;
11
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;
15
16   buf_push= buf_pop= buffer;
17   FOR_VERTEX(v) d[v]= -1;
18
19   d[start]= 0;
20   *buf_push++= start;
21
22   while (buf_pop < buf_push) {
23     current= *buf_pop++;
24     dfuture= d[current] + 1;
25     FOR_VEDGE(current,e,future) {
26       if (d[future] >= 0) continue; /* already found this one */
27       d[future]= dfuture;
28       *buf_push++= future;
29     }
30   }
31   assert(buf_pop==buf_push);
32   assert(buf_push <= buffer+sizeof(buffer)/sizeof(buffer[0]));
33
34   FOR_VERTEX(v) {
35     assert(d[v] >= 0);
36     sqdistances_r[v]= d[v] * d[v];
37   }
38 }
39
40 void graph_layout_prepare() {
41   int v1;
42   
43   FOR_VERTEX(v1)
44     breadth_first_search(v1, sqdistances[v1]);
45
46   alpha= 2;
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);
50 }
51
52
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|.
56    *
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.
60    *
61    * Energy contribution is proportional to
62    *
63    *               -4          2
64    *    a  a   .  d   . [ (s/d)  - 1 ]
65    *     vi vj
66    *
67    * (In practice we compute d^2+epsilon and use it for the
68    *  divisions, to avoid division by zero.)
69    */
70   //static const double d2_epsilon= 1e-6;
71
72   double total_cost=0;
73   int v1,v2,e, nedges=0;
74   double totaledgelength=0, meanedgelength, meanedgelength2;
75
76   FOR_EDGE(v1,e,v2) {
77     totaledgelength += hypotD(v[v1], v[v2]);
78     nedges++;
79   }
80
81   meanedgelength= totaledgelength / nedges;
82   meanedgelength2= meanedgelength * meanedgelength;
83 //  printf("mean=%g mean^2=%g\n", meanedgelength, meanedgelength2);
84     
85   FOR_VERTEX(v1) {
86     FOR_VERTEX(v2) {
87       if (v1 == v2) continue;
88
89       double d2= hypotD2(v[v1],v[v2]);
90       
91       int dist2= sqdistances[v1][v2];
92       assert(dist2>0);
93
94       double s2= dist2 * meanedgelength2;
95
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
99        */
100
101       double cost= pow(d2/s2, beta_prime);
102       
103       //printf("layout %03x..%03x dist^2=%d s^2=%g d^2=%g "
104                //" cost+=%g\n", v1,v2, dist2,
105                //            s2,d2, cost);
106       total_cost += cost;
107     }
108   }
109   return total_cost;
110 }