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);
36 COST( 3e2, line_bending_cost(vs->a));
37 COST( 1e3, edge_length_variation_cost(vs->a));
38 COST( 0.4e3, rim_proximity_cost(vs->a));
39 COST( 1e6, edge_angle_cost(vs->a));
40 // COST( 1e1, small_triangles_cost(vs->a));
41 COST( 1e12, noncircular_rim_cost(vs->a));
43 } else if (XBITS==4) {
44 COST( 3e2, line_bending_cost(vs->a));
45 COST( 3e3, edge_length_variation_cost(vs->a));
46 COST( 9.0e1, rim_proximity_cost(vs->a)); // 5e1 is too much
47 // 2.5e1 is too little
48 // 0.2e1 grows compared to previous ?
49 // 0.6e0 shrinks compared to previous ?
50 COST( 1e12, edge_angle_cost(vs->a));
51 COST( 1e12, noncircular_rim_cost(vs->a));
57 if (printing) printf("| total %# e |", energy);
59 if (energy < best_energy) {
65 if (bests_unprinted) printf(" [%4d]",bests_unprinted);
71 best_f= fopen(best_file_tmp,"wb"); if (!best_f) diee("fopen new out");
72 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
73 if (fclose(best_f)) diee("fclose new best");
74 if (rename(best_file_tmp,best_file)) diee("rename install new best");
87 static void addcost(double *energy, double tweight, double tcost, int pr) {
88 double tenergy= tweight * tcost;
89 if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy);
93 /*---------- Precomputations ----------*/
95 void compute_edge_lengths(const Vertices vertices) {
99 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
102 void compute_vertex_areas(const Vertices vertices) {
107 double total= 0.0, edges_total=0;
110 FOR_VEDGE(v0,e1,v1) {
112 v2= EDGE_END2(v0,e2);
115 edges_total += edge_lengths[v0][e1];
117 // double e1v[D3], e2v[D3], av[D3];
119 // e1v[k]= vertices[v1][k] - vertices[v0][k];
120 // e2v[k]= vertices[v2][k] - vertices[v0][k];
122 // xprod(av, e1v, e2v);
123 // total += magnD(av);
127 vertex_areas[v0]= total / count;
128 vertex_mean_edge_lengths[v0]= edges_total / count;
132 /*---------- Edgewise vertex displacement ----------*/
137 * At each vertex Q, in each direction e:
146 * cost = delta (we use r=3)
156 * delta = tan -------
159 * which is always in the range 0..pi because the denominator
160 * is nonnegative. We add epsilon to |AxB| to avoid division
168 double line_bending_cost(const Vertices vertices) {
169 static const double axb_epsilon= 1e-6;
170 static const double exponent_r= 3;
173 double a[D3], b[D3], axb[D3];
174 double total_cost= 0;
177 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
179 K a[k]= -vertices[pi][k] + vertices[qi][k];
180 K b[k]= -vertices[qi][k] + vertices[ri][k];
184 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
185 double cost= pow(delta,exponent_r);
187 if (!e && !(qi & ~XMASK))
195 /*---------- edge length variation ----------*/
200 * See the diagram above.
202 * cost = ( |PQ| - |QR| )
206 double edge_length_variation_cost(const Vertices vertices) {
207 double diff, cost= 0, exponent_r= 2;
211 eback= edge_reverse(q,e);
212 diff= edge_lengths[q][e] - edge_lengths[q][eback];
213 cost += pow(diff,exponent_r);
218 /*---------- rim proximity cost ----------*/
220 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
221 /* By symmetry, nearest point on circle is the one with
222 * the same angle subtended at the z axis. */
226 double mult= 1.0/ magnD(oncircle);
231 double rim_proximity_cost(const Vertices vertices) {
232 double oncircle[3], cost=0;
237 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
238 if (nominal_edge_distance==0) continue;
240 find_nearest_oncircle(oncircle, vertices[v]);
243 vertex_mean_edge_lengths[v] *
244 (nominal_edge_distance*nominal_edge_distance) /
245 (hypotD2(vertices[v], oncircle) + 1e-6);
250 /*---------- noncircular rim cost ----------*/
252 double noncircular_rim_cost(const Vertices vertices) {
257 FOR_RIM_VERTEX(vy,vx,v) {
258 find_nearest_oncircle(oncircle, vertices[v]);
260 double d2= hypotD2(vertices[v], oncircle);
266 /*---------- overly sharp edge cost ----------*/
271 * / | `-_ P'Q' ------ S'
284 * Let delta = angle between two triangles' normals
286 * Giving energy contribution:
293 double edge_angle_cost(const Vertices vertices) {
294 double pq1[D3], rp[D3], ps[D3], rp_2d[D3], ps_2d[D3], rs_2d[D3];
296 const double minradius= 0.5;
298 int pi,e,qi,ri,si, k;
299 // double our_epsilon=1e-6;
300 double total_cost= 0;
303 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
305 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
306 ri= EDGE_END2(pi,(e +1)%V6); if (ri<0) continue;
309 pq1[k]= -vertices[pi][k] + vertices[qi][k];
310 rp[k]= -vertices[ri][k] + vertices[pi][k];
311 ps[k]= -vertices[pi][k] + vertices[si][k];
314 normalise(pq1,1,1e-6);
315 xprod(rp_2d, rp,pq1); /* projects RP into plane normal to PQ */
316 xprod(ps_2d, ps,pq1); /* likewise PS */
317 K rs_2d[k]= rp_2d[k] + ps_2d[k];
318 /* radius of circumcircle of R'P'S' from Wikipedia
319 * `Circumscribed circle' */
324 r= a*b*c / sqrt((a+b+c)*(a-b+c)*(b-c+a)*(c-a+b) + 1e-6);
325 //fprintf(stderr,"triangle a=%g b=%g c=%g -> s=%g r=%g\n",a,b,c,s,r);
327 double deficit= minradius - r;
328 if (deficit < 0) continue;
329 double cost= deficit*deficit;
337 /*---------- small triangles cost ----------*/
353 * Let delta = angle between two triangles' normals
355 * Giving energy contribution:
362 double small_triangles_cost(const Vertices vertices) {
363 double pq[D3], ps[D3];
366 // double our_epsilon=1e-6;
367 double total_cost= 0;
370 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
372 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
375 pq[k]= vertices[qi][k] - vertices[pi][k];
376 ps[k]= vertices[si][k] - vertices[pi][k];
380 double cost= 1/(magnD2(x) + 0.01);
382 //double cost= pow(magnD(spqxpqr), 3);
383 //assert(dot>=-1 && dot <=1);
384 //double cost= 1-dot;