2 * We try to find an optimal triangle grid
10 double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6];
12 static double best_energy= DBL_MAX;
14 static void addcost(double *energy, double tweight, double tcost, int pr);
16 /*---------- main energy computation, weights, etc. ----------*/
18 typedef double CostComputation(const Vertices vertices, int section);
19 typedef void PreComputation(const Vertices vertices, int section);
26 #define NPRECOMPS ((sizeof(precomps)/sizeof(precomps[0])))
27 #define NCOSTS ((sizeof(costs)/sizeof(costs[0])))
28 #define COST(weight, compute) { (weight),(compute) },
30 static PreComputation *const precomps[]= {
35 static const CostContribution costs[]= {
38 #define STOP_EPSILON 1e-6
39 COST( 3e3, line_bending_cost)
40 COST( 3e3, edge_length_variation_cost)
41 COST( 0.4e3, rim_proximity_cost)
42 COST( 1e6, edge_angle_cost)
43 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.7)
44 // COST( 1e1, small_triangles_cost)
45 COST( 1e12, noncircular_rim_cost)
49 #define STOP_EPSILON 1.2e-4
50 COST( 3e5, line_bending_cost)
51 COST( 10e3, edge_length_variation_cost)
52 COST( 9.0e3, rim_proximity_cost) // 5e1 is too much
53 // 2.5e1 is too little
54 // 0.2e1 grows compared to previous ?
55 // 0.6e0 shrinks compared to previous ?
57 // COST( 1e12, edge_angle_cost)
58 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3)
59 COST( 1e18, noncircular_rim_cost)
63 #define STOP_EPSILON 1.2e-4
64 COST( 3e7, line_bending_cost)
65 COST( 10e2, prop_edge_length_variation_cost)
66 COST( 9.0e3, rim_proximity_cost) // 5e1 is too much
67 // 2.5e1 is too little
68 // 0.2e1 grows compared to previous ?
69 // 0.6e0 shrinks compared to previous ?
71 // COST( 1e12, edge_angle_cost)
72 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3)
73 COST( 1e18, noncircular_rim_cost)
76 #if XBITS>=6 /* nonsense follows but never mind */
77 #define STOP_EPSILON 1e-6
78 COST( 3e5, line_bending_cost)
79 COST( 10e2, edge_length_variation_cost)
80 COST( 9.0e1, rim_proximity_cost) // 5e1 is too much
81 // 2.5e1 is too little
82 // 0.2e1 grows compared to previous ?
83 // 0.6e0 shrinks compared to previous ?
85 COST( 1e12, edge_angle_cost)
86 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3)
87 COST( 1e18, noncircular_rim_cost)
92 const double edge_angle_cost_circcircrat= EDGE_ANGLE_COST_CIRCCIRCRAT;
94 void energy_init(void) {
95 stop_epsilon= STOP_EPSILON;
98 /*---------- energy computation machinery ----------*/
100 void compute_energy_separately(const struct Vertices *vs,
101 int section, void *energies_v, void *totals_v) {
102 double *energies= energies_v;
105 for (ci=0; ci<NPRECOMPS; ci++) {
106 precomps[ci](vs->a, section);
107 inparallel_barrier();
109 for (ci=0; ci<NCOSTS; ci++)
110 energies[ci]= costs[ci].fn(vs->a, section);
113 void compute_energy_combine(const struct Vertices *vertices,
114 int section, void *energies_v, void *totals_v) {
116 double *energies= energies_v;
117 double *totals= totals_v;
119 for (ci=0; ci<NCOSTS; ci++)
120 totals[ci] += energies[ci];
123 double compute_energy(const struct Vertices *vs) {
124 static int bests_unprinted;
126 double totals[NCOSTS], energy;
129 printing= printing_check(pr_cost,0);
131 if (printing) printf("%15lld c>e |", evaluations);
133 for (ci=0; ci<NCOSTS; ci++)
137 compute_energy_separately,
138 compute_energy_combine,
139 sizeof(totals) /* really, size of energies */,
143 for (ci=0; ci<NCOSTS; ci++)
144 addcost(&energy, costs[ci].weight, totals[ci], printing);
146 if (printing) printf("| total %# e |", energy);
148 if (energy < best_energy) {
154 if (bests_unprinted) printf(" [%4d]",bests_unprinted);
160 best_f= fopen(best_file_tmp,"wb"); if (!best_f) diee("fopen new out");
161 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
162 if (fclose(best_f)) diee("fclose new best");
163 if (rename(best_file_tmp,best_file)) diee("rename install new best");
176 static void addcost(double *energy, double tweight, double tcost, int pr) {
177 double tenergy= tweight * tcost;
178 if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy);
182 /*---------- Precomputations ----------*/
184 void compute_edge_lengths(const Vertices vertices, int section) {
187 FOR_EDGE(v1,e,v2, OUTER)
188 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
191 void compute_vertex_areas(const Vertices vertices, int section) {
195 FOR_VERTEX(v0, OUTER) {
196 double total= 0.0, edges_total=0;
199 FOR_VEDGE(v0,e1,v1) {
201 v2= EDGE_END2(v0,e2);
204 edges_total += edge_lengths[v0][e1];
206 // double e1v[D3], e2v[D3], av[D3];
208 // e1v[k]= vertices[v1][k] - vertices[v0][k];
209 // e2v[k]= vertices[v2][k] - vertices[v0][k];
211 // xprod(av, e1v, e2v);
212 // total += magnD(av);
216 vertex_areas[v0]= total / count;
217 vertex_mean_edge_lengths[v0]= edges_total / count;
221 /*---------- Edgewise vertex displacement ----------*/
226 * At each vertex Q, in each direction e:
235 * cost = delta (we use r=3)
245 * delta = tan -------
248 * which is always in the range 0..pi because the denominator
249 * is nonnegative. We add epsilon to |AxB| to avoid division
257 double line_bending_cost(const Vertices vertices, int section) {
258 static const double axb_epsilon= 1e-6;
259 static const double exponent_r= 4;
262 double a[D3], b[D3], axb[D3];
263 double total_cost= 0;
265 FOR_EDGE(qi,e,ri, OUTER) {
266 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
268 //if (!(qi&XMASK)) fprintf(stderr,"%02x-%02x-%02x (%d)\n",pi,qi,ri,e);
270 K a[k]= -vertices[pi][k] + vertices[qi][k];
271 K b[k]= -vertices[qi][k] + vertices[ri][k];
275 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
276 double cost= pow(delta,exponent_r);
283 /*---------- edge length variation ----------*/
288 * See the diagram above.
290 * cost = ( |PQ| - |QR| )
294 double edge_length_variation_cost(const Vertices vertices, int section) {
295 double diff, cost= 0, exponent_r= 2;
298 FOR_EDGE(q,e,r, OUTER) {
299 eback= edge_reverse(q,e);
300 diff= edge_lengths[q][e] - edge_lengths[q][eback];
301 cost += pow(diff,exponent_r);
306 /*---------- proportional edge length variation ----------*/
311 * See the diagram above.
313 * cost = ( |PQ| - |QR| )
317 double prop_edge_length_variation_cost(const Vertices vertices, int section) {
318 double cost= 0, exponent_r= 2;
321 FOR_EDGE(q,e,r, OUTER) {
322 eback= edge_reverse(q,e);
323 double le= edge_lengths[q][e];
324 double leback= edge_lengths[q][eback];
325 double diff= le - leback;
326 double num= MIN(le, leback);
327 cost += pow(diff / (num + 1e-6), exponent_r);
332 /*---------- rim proximity cost ----------*/
334 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
335 /* By symmetry, nearest point on circle is the one with
336 * the same angle subtended at the z axis. */
340 double mult= 1.0/ magnD(oncircle);
345 double rim_proximity_cost(const Vertices vertices, int section) {
346 double oncircle[3], cost=0;
349 FOR_VERTEX(v, OUTER) {
351 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
352 if (nominal_edge_distance==0) continue;
354 find_nearest_oncircle(oncircle, vertices[v]);
357 vertex_mean_edge_lengths[v] *
358 (nominal_edge_distance*nominal_edge_distance) /
359 (hypotD2(vertices[v], oncircle) + 1e-6);
364 /*---------- noncircular rim cost ----------*/
366 double noncircular_rim_cost(const Vertices vertices, int section) {
371 FOR_RIM_VERTEX(vy,vx,v, OUTER) {
372 find_nearest_oncircle(oncircle, vertices[v]);
374 double d2= hypotD2(vertices[v], oncircle);
380 /*---------- overly sharp edge cost ----------*/
385 * / | `-_ P'Q' ------ S'
398 * Let delta = angle between two triangles' normals
400 * Giving energy contribution:
407 double edge_angle_cost(const Vertices vertices, int section) {
408 double pq1[D3], rp[D3], ps[D3], rp_2d[D3], ps_2d[D3], rs_2d[D3];
410 const double minradius_base= 0.2;
412 int pi,e,qi,ri,si, k;
413 // double our_epsilon=1e-6;
414 double total_cost= 0;
416 FOR_EDGE(pi,e,qi, OUTER) {
417 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
419 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
420 ri= EDGE_END2(pi,(e +1)%V6); if (ri<0) continue;
423 pq1[k]= -vertices[pi][k] + vertices[qi][k];
424 rp[k]= -vertices[ri][k] + vertices[pi][k];
425 ps[k]= -vertices[pi][k] + vertices[si][k];
428 normalise(pq1,1,1e-6);
429 xprod(rp_2d, rp,pq1); /* projects RP into plane normal to PQ */
430 xprod(ps_2d, ps,pq1); /* likewise PS */
431 K rs_2d[k]= rp_2d[k] + ps_2d[k];
432 /* radius of circumcircle of R'P'S' from Wikipedia
433 * `Circumscribed circle' */
438 r= a*b*c / sqrt((a+b+c)*(a-b+c)*(b-c+a)*(c-a+b) + 1e-6);
440 double minradius= minradius_base + edge_angle_cost_circcircrat*(a+b);
441 double deficit= minradius - r;
442 if (deficit < 0) continue;
443 double cost= deficit*deficit;
451 /*---------- small triangles cost ----------*/
467 * Let delta = angle between two triangles' normals
469 * Giving energy contribution:
476 double small_triangles_cost(const Vertices vertices, int section) {
477 double pq[D3], ps[D3];
480 // double our_epsilon=1e-6;
481 double total_cost= 0;
483 FOR_EDGE(pi,e,qi, OUTER) {
484 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
486 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
489 pq[k]= vertices[qi][k] - vertices[pi][k];
490 ps[k]= vertices[si][k] - vertices[pi][k];
494 double cost= 1/(magnD2(x) + 0.01);
496 //double cost= pow(magnD(spqxpqr), 3);
497 //assert(dot>=-1 && dot <=1);
498 //double cost= 1-dot;