chiark / gitweb /
tidy up and normalise line bending cost
[moebius2.git] / energy.c
1 /*
2  * We try to find an optimal triangle grid
3  */
4
5 #include "common.h"
6 #include "minimise.h"
7 #include "mgraph.h"
8
9 double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6];
10
11 static double best_energy= DBL_MAX;
12
13 static void addcost(double *energy, double tweight, double tcost, int pr);
14 #define COST(weight, compute) addcost(&energy, (weight), (compute), printing)
15
16 double density;
17
18 void energy_init(void) {
19   density= sqrt(N);
20 }
21
22 /*---------- main energy computation and subroutines ----------*/
23
24 double compute_energy(const struct Vertices *vs) {
25   double energy;
26   int printing;
27
28   compute_edge_lengths(vs->a);
29   compute_vertex_areas(vs->a);
30   energy= 0;
31
32   printing= printing_check(pr_cost,0);
33
34   if (printing) printf("cost > energy |");
35
36   COST(2.25e3, line_bending_adjcost(vs->a));
37   COST(1e3, edge_length_variation_cost(vs->a));
38   COST(0.2e3, rim_proximity_cost(vs->a));
39 //  COST(1e2, graph_layout_cost(vs->a));
40   COST(1e8, noncircular_rim_cost(vs->a));
41
42   if (printing) printf("| total %# e |", energy);
43
44   if (energy < best_energy) {
45     FILE *best_f;
46     int r;
47
48     if (printing) printf(" BEST");
49
50     best_f= fopen(output_file_tmp,"wb");  if (!best_f) diee("fopen new out");
51     r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
52     if (fclose(best_f)) diee("fclose new best");
53     if (rename(output_file_tmp,output_file)) diee("rename install new best");
54
55     best_energy= energy;
56   }
57   if (printing) {
58     putchar('\n');
59     flushoutput();
60   }
61
62   return energy;
63 }
64
65 static void addcost(double *energy, double tweight, double tcost, int pr) {
66   double tenergy= tweight * tcost;
67   if (pr) printf(" %# e x %# e > %# e* |", tcost, tweight, tenergy);
68   *energy += tenergy;
69 }
70
71 /*---------- Precomputations ----------*/
72
73 void compute_edge_lengths(const Vertices vertices) {
74   int v1,e,v2;
75
76   FOR_EDGE(v1,e,v2)
77     edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
78 }
79
80 void compute_vertex_areas(const Vertices vertices) {
81   int v0,v1,v2, e1,e2;
82 //  int k;
83
84   FOR_VERTEX(v0) {
85     double total= 0.0, edges_total=0;
86     int count= 0;
87
88     FOR_VEDGE(v0,e1,v1) {
89       e2= (e1+1) % V6;
90       v2= EDGE_END2(v0,e2);
91       if (v2<0) continue;
92
93       edges_total += edge_lengths[v0][e1];
94
95 //      double e1v[D3], e2v[D3], av[D3];
96 //      K {
97 //      e1v[k]= vertices[v1][k] - vertices[v0][k];
98 //      e2v[k]= vertices[v2][k] - vertices[v0][k];
99 //      }
100 //      xprod(av, e1v, e2v);
101 //      total += magnD(av);
102
103       count++;
104     }
105     vertex_areas[v0]= total / count;
106     vertex_mean_edge_lengths[v0]= edges_total / count;
107   }
108 }
109
110 /*---------- Edgewise vertex displacement ----------*/
111
112   /*
113    * Definition:
114    *
115    *    At each vertex Q, in each direction e:
116    *
117    *                                  e
118    *                           Q ----->----- R
119    *                      _,-'\__/
120    *                  _,-'       delta
121    *               P '
122    *
123    *                      r
124    *       cost    = delta          (we use r=3)
125    *           Q,e
126    *
127    *
128    * Calculation:
129    *
130    *      Let vector A = PQ
131    *                 B = QR
132    *
133    *                   -1   A . B
134    *      delta =  tan     -------
135    *                      | A x B |
136    *
137    *      which is always in the range 0..pi because the denominator
138    *      is nonnegative.  We add epsilon to |AxB| to avoid division
139    *      by zero.
140    *
141    * Normalisation:
142    *
143    *    We want the answer to remain unchanged when the vertices lie
144    *    on circles.  Interposing M and N so that we have P-M-Q-N-R
145    *    generates half as much delta for each vertex.  So
146    *
147    *           ,       -1
148    *       cost    =  D   . cost
149    *           Q,e              Q,e
150    *
151    *                           where D is the linear density.
152    *
153    *                ,              -1   
154    *     Sigma  cost      =   N . D  .  Sigma  cost
155    *      Q,e       Q,e                  Q,e       Q,e
156    *
157    *
158    */
159
160 double line_bending_adjcost(const Vertices vertices) {
161   static const double axb_epsilon= 1e-6;
162   static const double exponent_r= 3;
163
164   int pi,e,qi,ri, k;
165   double  a[D3], b[D3], axb[D3];
166   double total_cost= 0;
167
168   FOR_EDGE(qi,e,ri) {
169     pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
170
171     K a[k]= -vertices[pi][k] + vertices[qi][k];
172     K b[k]= -vertices[qi][k] + vertices[ri][k];
173
174     xprod(axb,a,b);
175
176     double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
177     double cost= pow(delta,exponent_r);
178
179     if (!e && !(qi & YMASK))
180       cost *= 10;
181
182     total_cost += cost;
183   }
184   return total_cost / (N / density);
185 }
186
187 /*---------- edge length variation ----------*/
188
189 double edge_length_variation_cost(const Vertices vertices) {
190   double diff, cost= 0;
191   int v0, efwd,vfwd, eback;
192
193   FOR_EDGE(v0,efwd,vfwd) {
194     eback= edge_reverse(v0,efwd);
195     diff= edge_lengths[v0][efwd] - edge_lengths[v0][eback];
196     cost += diff*diff;
197   }
198   return cost;
199 }
200
201 /*---------- rim proximity cost ----------*/
202
203 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
204   /* By symmetry, nearest point on circle is the one with
205    * the same angle subtended at the z axis. */
206   oncircle[0]= p[0];
207   oncircle[1]= p[1];
208   oncircle[2]= 0;
209   double mult= 1.0/ magnD(oncircle);
210   oncircle[0] *= mult;
211   oncircle[1] *= mult;
212 }
213
214 double rim_proximity_cost(const Vertices vertices) {
215   double oncircle[3], cost=0;
216   int v;
217
218   FOR_VERTEX(v) {
219     int y= v >> YSHIFT;
220     int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
221     if (nominal_edge_distance==0) continue;
222
223     find_nearest_oncircle(oncircle, vertices[v]);
224
225     cost +=
226       vertex_mean_edge_lengths[v] *
227       (nominal_edge_distance*nominal_edge_distance) /
228       (hypotD2(vertices[v], oncircle) + 1e-6);
229   }
230   return cost;
231 }
232
233 /*---------- noncircular rim cost ----------*/
234
235 double noncircular_rim_cost(const Vertices vertices) {
236   int vy,vx,v;
237   double cost= 0.0;
238   double oncircle[3];
239
240   FOR_RIM_VERTEX(vy,vx,v) {
241     find_nearest_oncircle(oncircle, vertices[v]);
242
243     double d2= hypotD2(vertices[v], oncircle);
244     cost += d2*d2;
245   }
246   return cost;
247 }