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