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