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