chiark / gitweb /
do not actually compute vertex areas
[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;
76 //  int k;
77
78   FOR_VERTEX(v0) {
79     double total= 0.0, edges_total=0;
80     int count= 0;
81
82     FOR_VEDGE(v0,e1,v1) {
83       e2= (e1+1) % V6;
84       v2= EDGE_END2(v0,e2);
85       if (v2<0) continue;
86
87       edges_total += edge_lengths[v0][e1];
88
89 //      double e1v[D3], e2v[D3], av[D3];
90 //      K {
91 //      e1v[k]= vertices[v1][k] - vertices[v0][k];
92 //      e2v[k]= vertices[v2][k] - vertices[v0][k];
93 //      }
94 //      xprod(av, e1v, e2v);
95 //      total += magnD(av);
96       
97       count++;
98     }
99     vertex_areas[v0]= total / count;
100     vertex_mean_edge_lengths[v0]= edges_total / count;
101   }
102 }
103
104 /*---------- Edgewise vertex displacement ----------*/
105
106   /*
107    *
108    *
109    *
110    *                Q `-_
111    *              / |    `-_
112    *             /  |       `-.
113    *            /   M - - - - - S
114    *           /  ' |      _,-'
115    *          /  '  |  _,-'
116    *         / '  , P '
117    *        / ',-'
118    *       /,-'
119    *      /'
120    *     R
121    *
122    *  Let delta =  180deg - angle RMS
123    *
124    *  Let  l = |PQ|
125    *       d = |RS|
126    *
127    *  Giving energy contribution:
128    *
129    *                                   3
130    *                            l delta
131    *    E             =  F   .  --------
132    *     vd, edge PQ      vd       d
133    *
134    *
135    *  (The dimensions of this are those of F_vd.)
136    *
137    *  We calculate delta as  atan2(|AxB|, A.B)
138    *  where A = PQ, B = QR
139    *
140    *  In practice to avoid division by zero we'll add epsilon to d and
141    *  |AxB| and the huge energy ought then to be sufficient for the
142    *  model to avoid being close to R=S.
143    */
144
145 double edgewise_vertex_displacement_cost(const Vertices vertices) {
146   static const double axb_epsilon= 1e-6;
147
148   int pi,e,qi,ri, k; //,si
149   double  a[D3], b[D3], axb[D3]; //m[D3],
150   double total_cost= 0;
151
152   FOR_EDGE(qi,e,ri) {
153     pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
154
155 //    K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5;
156     K a[k]= -vertices[pi][k] + vertices[qi][k];
157     K b[k]= -vertices[qi][k] + vertices[ri][k];
158
159     xprod(axb,a,b);
160     
161     double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
162     double cost= pow(delta,3);
163
164     if (!e && !(qi & YMASK))
165       cost *= 10;
166
167     total_cost += cost;
168   }
169   return total_cost;
170 }
171
172 /*---------- edge length variation ----------*/
173
174 double edge_length_variation_cost(const Vertices vertices) {
175   double diff, cost= 0;
176   int v0, efwd,vfwd, eback;
177
178   FOR_EDGE(v0,efwd,vfwd) {
179     eback= edge_reverse(v0,efwd);
180     diff= edge_lengths[v0][efwd] - edge_lengths[v0][eback];
181     cost += diff*diff;
182   }
183   return cost;
184 }    
185
186 /*---------- rim proximity cost ----------*/
187
188 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
189   /* By symmetry, nearest point on circle is the one with
190    * the same angle subtended at the z axis. */
191   oncircle[0]= p[0];
192   oncircle[1]= p[1];
193   oncircle[2]= 0;
194   double mult= 1.0/ magnD(oncircle);
195   oncircle[0] *= mult;
196   oncircle[1] *= mult;
197 }
198
199 double rim_proximity_cost(const Vertices vertices) {
200   double oncircle[3], cost=0;
201   int v;
202
203   FOR_VERTEX(v) {
204     int y= v >> YSHIFT;
205     int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
206     if (nominal_edge_distance==0) continue;
207
208     find_nearest_oncircle(oncircle, vertices[v]);
209
210     cost +=
211       vertex_mean_edge_lengths[v] *
212       (nominal_edge_distance*nominal_edge_distance) /
213       (hypotD2(vertices[v], oncircle) + 1e-6);
214   }
215   return cost;
216 }
217
218 /*---------- noncircular rim cost ----------*/
219
220 double noncircular_rim_cost(const Vertices vertices) {
221   int vy,vx,v;
222   double cost= 0.0;
223   double oncircle[3];
224
225   FOR_RIM_VERTEX(vy,vx,v) {
226     find_nearest_oncircle(oncircle, vertices[v]);
227     
228     double d2= hypotD2(vertices[v], oncircle);
229     cost += d2*d2;
230   }
231   return cost;
232 }