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