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