2 * We try to find an optimal triangle grid
9 double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6];
11 static double best_energy= DBL_MAX;
13 static void addcost(double *energy, double tweight, double tcost, int pr);
14 #define COST(weight, compute) addcost(&energy, (weight), (compute), printing)
16 void energy_init(void) {
19 /*---------- main energy computation and subroutines ----------*/
21 double compute_energy(const struct Vertices *vs) {
22 static int bests_unprinted;
27 compute_edge_lengths(vs->a);
28 compute_vertex_areas(vs->a);
31 printing= printing_check(pr_cost,0);
33 if (printing) printf("%15lld c>e |", evaluations);
35 COST(3e2, line_bending_cost(vs->a));
36 COST(1e3, edge_length_variation_cost(vs->a));
37 COST(0.2e3, rim_proximity_cost(vs->a));
38 // COST(1e2, graph_layout_cost(vs->a));
39 COST(1e8, noncircular_rim_cost(vs->a));
41 if (printing) printf("| total %# e |", energy);
43 if (energy < best_energy) {
49 if (bests_unprinted) printf(" [%4d]",bests_unprinted);
55 best_f= fopen(best_file_tmp,"wb"); if (!best_f) diee("fopen new out");
56 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
57 if (fclose(best_f)) diee("fclose new best");
58 if (rename(best_file_tmp,best_file)) diee("rename install new best");
71 static void addcost(double *energy, double tweight, double tcost, int pr) {
72 double tenergy= tweight * tcost;
73 if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy);
77 /*---------- Precomputations ----------*/
79 void compute_edge_lengths(const Vertices vertices) {
83 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
86 void compute_vertex_areas(const Vertices vertices) {
91 double total= 0.0, edges_total=0;
99 edges_total += edge_lengths[v0][e1];
101 // double e1v[D3], e2v[D3], av[D3];
103 // e1v[k]= vertices[v1][k] - vertices[v0][k];
104 // e2v[k]= vertices[v2][k] - vertices[v0][k];
106 // xprod(av, e1v, e2v);
107 // total += magnD(av);
111 vertex_areas[v0]= total / count;
112 vertex_mean_edge_lengths[v0]= edges_total / count;
116 /*---------- Edgewise vertex displacement ----------*/
121 * At each vertex Q, in each direction e:
130 * cost = delta (we use r=3)
140 * delta = tan -------
143 * which is always in the range 0..pi because the denominator
144 * is nonnegative. We add epsilon to |AxB| to avoid division
152 double line_bending_cost(const Vertices vertices) {
153 static const double axb_epsilon= 1e-6;
154 static const double exponent_r= 3;
157 double a[D3], b[D3], axb[D3];
158 double total_cost= 0;
161 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
163 K a[k]= -vertices[pi][k] + vertices[qi][k];
164 K b[k]= -vertices[qi][k] + vertices[ri][k];
168 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
169 double cost= pow(delta,exponent_r);
171 if (!e && !(qi & YMASK))
179 /*---------- edge length variation ----------*/
184 * See the diagram above.
186 * cost = ( |PQ| - |QR| )
190 double edge_length_variation_cost(const Vertices vertices) {
191 double diff, cost= 0, exponent_r= 2;
195 eback= edge_reverse(q,e);
196 diff= edge_lengths[q][e] - edge_lengths[q][eback];
197 cost += pow(diff,exponent_r);
202 /*---------- rim proximity cost ----------*/
204 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
205 /* By symmetry, nearest point on circle is the one with
206 * the same angle subtended at the z axis. */
210 double mult= 1.0/ magnD(oncircle);
215 double rim_proximity_cost(const Vertices vertices) {
216 double oncircle[3], cost=0;
221 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
222 if (nominal_edge_distance==0) continue;
224 find_nearest_oncircle(oncircle, vertices[v]);
227 vertex_mean_edge_lengths[v] *
228 (nominal_edge_distance*nominal_edge_distance) /
229 (hypotD2(vertices[v], oncircle) + 1e-6);
234 /*---------- noncircular rim cost ----------*/
236 double noncircular_rim_cost(const Vertices vertices) {
241 FOR_RIM_VERTEX(vy,vx,v) {
242 find_nearest_oncircle(oncircle, vertices[v]);
244 double d2= hypotD2(vertices[v], oncircle);