chiark / gitweb /
best so far perhaps
[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    if (XBITS==3) {
36      COST(  3e3,   line_bending_cost(vs->a));
37      COST(  3e3,   edge_length_variation_cost(vs->a));
38      COST( 0.4e3,  rim_proximity_cost(vs->a));
39      COST(  1e6,   edge_angle_cost(vs->a, 0.5/1.7));
40  //    COST(  1e1,   small_triangles_cost(vs->a));
41      COST(  1e12,   noncircular_rim_cost(vs->a));
42      stop_epsilon= 1e-6;
43    } else if (XBITS==4) {
44      COST(  3e5,   line_bending_cost(vs->a));
45      COST( 10e2,   edge_length_variation_cost(vs->a));
46      COST( 9.0e1,  rim_proximity_cost(vs->a)); // 5e1 is too much
47                                                   // 2.5e1 is too little
48      // 0.2e1 grows compared to previous ?
49      // 0.6e0 shrinks compared to previous ?
50      COST(  1e12,   edge_angle_cost(vs->a, 0.5/1.3));
51      COST(  1e18,   noncircular_rim_cost(vs->a));
52      stop_epsilon= 1e-6;
53    } else {
54      abort();
55    }
56
57    if (printing) printf("| total %# e |", energy);
58
59    if (energy < best_energy) {
60      FILE *best_f;
61      int r;
62
63      if (printing) {
64        printf(" BEST");
65        if (bests_unprinted) printf(" [%4d]",bests_unprinted);
66        bests_unprinted= 0;
67      } else {
68        bests_unprinted++;
69      }
70
71      best_f= fopen(best_file_tmp,"wb");  if (!best_f) diee("fopen new out");
72      r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
73      if (fclose(best_f)) diee("fclose new best");
74      if (rename(best_file_tmp,best_file)) diee("rename install new best");
75
76      best_energy= energy;
77    }
78    if (printing) {
79      putchar('\n');
80      flushoutput();
81    }
82
83    evaluations++;
84    return energy;
85  }
86
87  static void addcost(double *energy, double tweight, double tcost, int pr) {
88    double tenergy= tweight * tcost;
89    if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy);
90    *energy += tenergy;
91  }
92
93  /*---------- Precomputations ----------*/
94
95  void compute_edge_lengths(const Vertices vertices) {
96    int v1,e,v2;
97
98    FOR_EDGE(v1,e,v2)
99      edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
100  }
101
102  void compute_vertex_areas(const Vertices vertices) {
103    int v0,v1,v2, e1,e2;
104  //  int k;
105
106    FOR_VERTEX(v0) {
107      double total= 0.0, edges_total=0;
108      int count= 0;
109
110      FOR_VEDGE(v0,e1,v1) {
111        e2= (e1+1) % V6;
112        v2= EDGE_END2(v0,e2);
113        if (v2<0) continue;
114
115        edges_total += edge_lengths[v0][e1];
116
117  //      double e1v[D3], e2v[D3], av[D3];
118  //      K {
119  //     e1v[k]= vertices[v1][k] - vertices[v0][k];
120  //     e2v[k]= vertices[v2][k] - vertices[v0][k];
121  //      }
122  //      xprod(av, e1v, e2v);
123  //      total += magnD(av);
124
125        count++;
126      }
127      vertex_areas[v0]= total / count;
128      vertex_mean_edge_lengths[v0]= edges_total / count;
129    }
130  }
131
132  /*---------- Edgewise vertex displacement ----------*/
133
134    /*
135     * Definition:
136     *
137     *    At each vertex Q, in each direction e:
138     *
139     *                                         e
140     *                           Q ----->----- R
141     *                     _,-'\__/
142     *                         _,-'       delta
143     *              P '
144     *
145     *                      r
146     *       cost    = delta          (we use r=3)
147     *           Q,e
148     *
149     *
150     * Calculation:
151     *
152     *      Let vector A = PQ
153     *                 B = QR
154     *
155     *                          -1   A . B
156     *      delta =  tan     -------
157     *                     | A x B |
158     *
159     *      which is always in the range 0..pi because the denominator
160     *      is nonnegative.  We add epsilon to |AxB| to avoid division
161     *      by zero.
162     *
163     *                     r
164     *      cost    = delta
165     *          Q,e
166     */
167
168  double line_bending_cost(const Vertices vertices) {
169    static const double axb_epsilon= 1e-6;
170    static const double exponent_r= 4;
171
172    int pi,e,qi,ri, k;
173    double  a[D3], b[D3], axb[D3];
174    double total_cost= 0;
175
176    FOR_EDGE(qi,e,ri) {
177      pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
178
179 //if (!(qi&XMASK)) fprintf(stderr,"%02x-%02x-%02x (%d)\n",pi,qi,ri,e);
180
181     K a[k]= -vertices[pi][k] + vertices[qi][k];
182     K b[k]= -vertices[qi][k] + vertices[ri][k];
183
184     xprod(axb,a,b);
185
186     double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
187     double cost= pow(delta,exponent_r);
188
189     total_cost += cost;
190   }
191   return total_cost;
192 }
193
194 /*---------- edge length variation ----------*/
195
196   /*
197    * Definition:
198    *
199    *    See the diagram above.
200    *                                r
201    *       cost    = ( |PQ| - |QR| )
202    *           Q,e
203    */
204
205 double edge_length_variation_cost(const Vertices vertices) {
206   double diff, cost= 0, exponent_r= 2;
207   int q, e,r, eback;
208
209   FOR_EDGE(q,e,r) {
210     eback= edge_reverse(q,e);
211     diff= edge_lengths[q][e] - edge_lengths[q][eback];
212     cost += pow(diff,exponent_r);
213   }
214   return cost;
215 }
216
217 /*---------- rim proximity cost ----------*/
218
219 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
220   /* By symmetry, nearest point on circle is the one with
221    * the same angle subtended at the z axis. */
222   oncircle[0]= p[0];
223   oncircle[1]= p[1];
224   oncircle[2]= 0;
225   double mult= 1.0/ magnD(oncircle);
226   oncircle[0] *= mult;
227   oncircle[1] *= mult;
228 }
229
230 double rim_proximity_cost(const Vertices vertices) {
231   double oncircle[3], cost=0;
232   int v;
233
234   FOR_VERTEX(v) {
235     int y= v >> YSHIFT;
236     int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
237     if (nominal_edge_distance==0) continue;
238
239     find_nearest_oncircle(oncircle, vertices[v]);
240
241     cost +=
242       vertex_mean_edge_lengths[v] *
243       (nominal_edge_distance*nominal_edge_distance) /
244       (hypotD2(vertices[v], oncircle) + 1e-6);
245   }
246   return cost;
247 }
248
249 /*---------- noncircular rim cost ----------*/
250
251 double noncircular_rim_cost(const Vertices vertices) {
252   int vy,vx,v;
253   double cost= 0.0;
254   double oncircle[3];
255
256   FOR_RIM_VERTEX(vy,vx,v) {
257     find_nearest_oncircle(oncircle, vertices[v]);
258
259     double d2= hypotD2(vertices[v], oncircle);
260     cost += d2*d2;
261   }
262   return cost;
263 }
264
265 /*---------- overly sharp edge cost ----------*/
266
267   /*
268    *
269    *                Q `-_
270    *              / |    `-_                           P'Q' ------ S'
271    *             /  |       `-.                      _,' `.       .
272    *            /   |           S                 _,'     :      .
273    *           /    |      _,-'                _,'        :r    .r
274    *          /     |  _,-'                R' '           `.   .
275    *         /    , P '                        ` .   r     :  .
276    *        /  ,-'                                  `  .   : 
277    *       /,-'                                          ` C'
278    *      /'
279    *     R
280    *
281    *
282    *
283    *  Let delta =  angle between two triangles' normals
284    *
285    *  Giving energy contribution:
286    *
287    *                                   2
288    *    E             =  F   .  delta
289    *     vd, edge PQ      vd
290    */
291
292 double edge_angle_cost(const Vertices vertices, double circcircrat) {
293   double pq1[D3], rp[D3], ps[D3], rp_2d[D3], ps_2d[D3], rs_2d[D3];
294   double a,b,c,s,r;
295   const double minradius_base= 0.2;
296
297   int pi,e,qi,ri,si, k;
298 //  double our_epsilon=1e-6;
299   double total_cost= 0;
300
301   FOR_EDGE(pi,e,qi) {
302 //    if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
303
304     si= EDGE_END2(pi,(e+V6-1)%V6);  if (si<0) continue;
305     ri= EDGE_END2(pi,(e   +1)%V6);  if (ri<0) continue;
306
307     K {
308       pq1[k]= -vertices[pi][k] + vertices[qi][k];
309       rp[k]=  -vertices[ri][k] + vertices[pi][k];
310       ps[k]=  -vertices[pi][k] + vertices[si][k];
311     }
312
313     normalise(pq1,1,1e-6);
314     xprod(rp_2d, rp,pq1); /* projects RP into plane normal to PQ */
315     xprod(ps_2d, ps,pq1); /* likewise PS */
316     K rs_2d[k]= rp_2d[k] + ps_2d[k];
317     /* radius of circumcircle of R'P'S' from Wikipedia
318      * `Circumscribed circle' */
319     a= magnD(rp_2d);
320     b= magnD(ps_2d);
321     c= magnD(rs_2d);
322     s= 0.5*(a+b+c);
323     r= a*b*c / sqrt((a+b+c)*(a-b+c)*(b-c+a)*(c-a+b) + 1e-6);
324
325     double minradius= minradius_base + circcircrat*(a+b);
326     double deficit= minradius - r;
327     if (deficit < 0) continue;
328     double cost= deficit*deficit;
329
330     total_cost += cost;
331   }
332
333   return total_cost;
334 }
335
336 /*---------- small triangles cost ----------*/
337
338   /*
339    *
340    *                Q `-_
341    *              / |    `-_
342    *             /  |       `-.
343    *            /   |           S
344    *           /    |      _,-'
345    *          /     |  _,-'
346    *         /    , P '
347    *        /  ,-'
348    *       /,-'
349    *      /'
350    *     R
351    *
352    *  Let delta =  angle between two triangles' normals
353    *
354    *  Giving energy contribution:
355    *
356    *                                   2
357    *    E             =  F   .  delta
358    *     vd, edge PQ      vd
359    */
360
361 double small_triangles_cost(const Vertices vertices) {
362   double pq[D3], ps[D3];
363   double x[D3];
364   int pi,e,qi,si, k;
365 //  double our_epsilon=1e-6;
366   double total_cost= 0;
367
368   FOR_EDGE(pi,e,qi) {
369 //    if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
370
371     si= EDGE_END2(pi,(e+V6-1)%V6);  if (si<0) continue;
372
373     K {
374       pq[k]= vertices[qi][k] - vertices[pi][k];
375       ps[k]= vertices[si][k] - vertices[pi][k];
376     }
377     xprod(x, pq,ps);
378
379     double cost= 1/(magnD2(x) + 0.01);
380
381 //double cost= pow(magnD(spqxpqr), 3);
382 //assert(dot>=-1 && dot <=1);
383 //double cost= 1-dot;
384     total_cost += cost;
385   }
386
387   return total_cost;
388 }