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, vertex_displacement_cost)
40 COST( 0.4e3, rim_proximity_cost)
41 COST( 1e7, edge_angle_cost)
42 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.2/1.7)
43 COST( 1e2, small_triangles_cost)
44 COST( 1e12, noncircular_rim_cost)
48 #define STOP_EPSILON 1.2e-3
49 COST( 3e3, vertex_displacement_cost)
50 COST( 3e3, vertex_edgewise_displ_cost)
51 COST( 0.2e3, rim_proximity_cost)
52 COST( 1e12, noncircular_rim_cost)
53 COST( 10e1, nonequilateral_triangles_cost)
54 // COST( 1e1, small_triangles_cost)
55 // COST( 1e6, edge_angle_cost)
56 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.7)
60 #define STOP_EPSILON 1.2e-4
61 COST( 3e3, vertex_displacement_cost)
62 COST( 3e3, vertex_edgewise_displ_cost)
63 COST( 2e-1, rim_proximity_cost)
64 COST( 1e12, noncircular_rim_cost)
65 COST( 10e1, nonequilateral_triangles_cost)
66 // COST( 1e1, small_triangles_cost)
67 // COST( 1e6, edge_angle_cost)
68 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.7)
72 #define STOP_EPSILON 1.2e-4
73 COST( 3e3, vertex_displacement_cost)
74 COST( 3e3, vertex_edgewise_displ_cost)
75 COST( 0.02e0, rim_proximity_cost)
76 COST( 1e12, noncircular_rim_cost)
77 COST( 10e1, nonequilateral_triangles_cost)
78 // COST( 1e1, small_triangles_cost)
79 // COST( 1e6, edge_angle_cost)
80 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.7)
83 #if XBITS>=7 /* nonsense follows but never mind */
84 #define STOP_EPSILON 1e-6
85 COST( 3e5, line_bending_cost)
86 COST( 10e2, edge_length_variation_cost)
87 COST( 9.0e1, rim_proximity_cost) // 5e1 is too much
88 // 2.5e1 is too little
89 // 0.2e1 grows compared to previous ?
90 // 0.6e0 shrinks compared to previous ?
92 COST( 1e12, edge_angle_cost)
93 #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3)
94 COST( 1e18, noncircular_rim_cost)
99 const double edge_angle_cost_circcircrat= EDGE_ANGLE_COST_CIRCCIRCRAT;
101 void energy_init(void) {
102 stop_epsilon= STOP_EPSILON;
105 /*---------- energy computation machinery ----------*/
107 void compute_energy_separately(const struct Vertices *vs,
108 int section, void *energies_v, void *totals_v) {
109 double *energies= energies_v;
112 for (ci=0; ci<NPRECOMPS; ci++) {
113 precomps[ci](vs->a, section);
114 inparallel_barrier();
116 for (ci=0; ci<NCOSTS; ci++)
117 energies[ci]= costs[ci].fn(vs->a, section);
120 void compute_energy_combine(const struct Vertices *vertices,
121 int section, void *energies_v, void *totals_v) {
123 double *energies= energies_v;
124 double *totals= totals_v;
126 for (ci=0; ci<NCOSTS; ci++)
127 totals[ci] += energies[ci];
130 double compute_energy(const struct Vertices *vs) {
131 static int bests_unprinted;
133 double totals[NCOSTS], energy;
136 printing= printing_check(pr_cost,0);
138 if (printing) printf("%15lld c>e |", evaluations);
140 for (ci=0; ci<NCOSTS; ci++)
144 compute_energy_separately,
145 compute_energy_combine,
146 sizeof(totals) /* really, size of energies */,
150 for (ci=0; ci<NCOSTS; ci++)
151 addcost(&energy, costs[ci].weight, totals[ci], printing);
153 if (printing) printf("| total %# e |", energy);
155 if (energy < best_energy) {
161 if (bests_unprinted) printf(" [%4d]",bests_unprinted);
167 best_f= fopen(best_file_tmp,"wb"); if (!best_f) diee("fopen new out");
168 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
169 if (fclose(best_f)) diee("fclose new best");
170 if (rename(best_file_tmp,best_file)) diee("rename install new best");
183 static void addcost(double *energy, double tweight, double tcost, int pr) {
184 double tenergy= tweight * tcost;
185 if (pr) printf(" %# e > %# e* |", tcost, tenergy);
189 /*---------- Precomputations ----------*/
191 void compute_edge_lengths(const Vertices vertices, int section) {
194 FOR_EDGE(v1,e,v2, OUTER)
195 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
198 void compute_vertex_areas(const Vertices vertices, int section) {
202 FOR_VERTEX(v0, OUTER) {
203 double total= 0.0, edges_total=0;
206 FOR_VEDGE(v0,e1,v1) {
208 v2= EDGE_END2(v0,e2);
211 edges_total += edge_lengths[v0][e1];
213 // double e1v[D3], e2v[D3], av[D3];
215 // e1v[k]= vertices[v1][k] - vertices[v0][k];
216 // e2v[k]= vertices[v2][k] - vertices[v0][k];
218 // xprod(av, e1v, e2v);
219 // total += magnD(av);
223 vertex_areas[v0]= total / count;
224 vertex_mean_edge_lengths[v0]= edges_total / count;
228 /*---------- displacement of vertices across a midpoint ----------*/
231 * Subroutine used where we have
233 * R - - - - - - - M . - - - - R'
238 * and wish to say that the vector RM should be similar to MS
239 * or to put it another way S = M + RM
241 * NB this is not symmetric wrt R and S since it divides by
242 * |SM| but not |RM| so you probably want to call it twice.
249 * Then the (1/delta)th power of the cost is
250 * proportional to |D|, and
251 * inversely proportional to |SM|
252 * except that |D| is measured in a wierd way which counts
253 * distance in the same direction as SM 1/lambda times as much
254 * ie the equipotential surfaces are ellipsoids around R',
255 * lengthened by lambda in the direction of RM.
260 * cost = [ lambda . ( D . SM/|SM| ) + | D x SM/|SM| | ]
261 * R,S,M [ ------------------------------------------- ]
266 static double vertex_one_displ_cost(const double r[D3], const double s[D3],
267 const double midpoint[D3],
268 double delta, double inv_lambda) {
269 const double smlen2_epsilon= 1e-12;
270 double sm[D3], d[D3], ddot, dcross[D3];
273 K sm[k]= -s[k] + midpoint[k];
274 K d[k]= midpoint[k] + sm[k] - r[k];
277 double smlen2= magnD2(sm);
278 double cost_basis= inv_lambda * ddot + magnD(dcross);
279 double cost= pow(cost_basis / (smlen2 + smlen2_epsilon), delta);
284 /*---------- displacement of vertices opposite at a vertex ----------*/
287 * At vertex Q considering edge direction e to R
288 * and corresponding opposite edge to S.
290 * This is vertex displacement as above with M=Q
293 double vertex_displacement_cost(const Vertices vertices, int section) {
294 const double inv_lambda= 1.0/1; //2;
295 const double delta= 4;
298 double total_cost= 0;
300 FOR_EDGE(qi,e,ri, OUTER) {
301 si= EDGE_END2(qi,(e+3)%V6); if (si<0) continue;
303 total_cost += vertex_one_displ_cost(vertices[ri], vertices[si], vertices[qi],
309 /*---------- displacement of vertices opposite at an edge ----------*/
312 * At edge PQ considering vertices R and S (see diagram
313 * below for overly sharp edge cost).
315 * Let M = midpoint of PQ
318 double vertex_edgewise_displ_cost(const Vertices vertices, int section) {
319 const double inv_lambda= 1.0/1; //2;
320 const double delta= 4;
322 int pi,e,qi,ri,si, k;
324 double total_cost= 0;
326 FOR_EDGE(pi,e,qi, OUTER) {
327 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
328 ri= EDGE_END2(pi,(e +1)%V6); if (ri<0) continue;
330 K m[k]= 0.5 * (vertices[pi][k] + vertices[qi][k]);
332 total_cost += vertex_one_displ_cost(vertices[ri], vertices[si], m,
339 /*---------- at-vertex edge angles ----------*/
344 * At each vertex Q, in each direction e:
353 * cost = delta (we use r=3)
363 * delta = tan -------
366 * which is always in the range 0..pi because the denominator
367 * is nonnegative. We add epsilon to |AxB| to avoid division
375 double line_bending_cost(const Vertices vertices, int section) {
376 static const double axb_epsilon= 1e-6;
377 static const double exponent_r= 4;
380 double a[D3], b[D3], axb[D3];
381 double total_cost= 0;
383 FOR_EDGE(qi,e,ri, OUTER) {
384 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
386 //if (!(qi&XMASK)) fprintf(stderr,"%02x-%02x-%02x (%d)\n",pi,qi,ri,e);
388 K a[k]= -vertices[pi][k] + vertices[qi][k];
389 K b[k]= -vertices[qi][k] + vertices[ri][k];
393 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
394 double cost= pow(delta,exponent_r);
401 /*---------- edge length variation ----------*/
406 * See the diagram above.
408 * cost = ( |PQ| - |QR| )
412 double edge_length_variation_cost(const Vertices vertices, int section) {
413 double diff, cost= 0, exponent_r= 2;
416 FOR_EDGE(q,e,r, OUTER) {
417 eback= edge_reverse(q,e);
418 diff= edge_lengths[q][e] - edge_lengths[q][eback];
419 cost += pow(diff,exponent_r);
424 /*---------- proportional edge length variation ----------*/
429 * See the diagram above.
431 * cost = ( |PQ| - |QR| )
435 double prop_edge_length_variation_cost(const Vertices vertices, int section) {
436 const double num_epsilon= 1e-6;
438 double cost= 0, exponent_r= 2;
441 FOR_EDGE(q,e,r, OUTER) {
442 eback= edge_reverse(q,e);
443 double le= edge_lengths[q][e];
444 double leback= edge_lengths[q][eback];
445 double diff= le - leback;
446 double num= MIN(le, leback);
447 cost += pow(diff / (num + num_epsilon), exponent_r);
452 /*---------- rim proximity cost ----------*/
454 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
455 /* By symmetry, nearest point on circle is the one with
456 * the same angle subtended at the z axis. */
460 double mult= 1.0/ magnD(oncircle);
465 double rim_proximity_cost(const Vertices vertices, int section) {
466 double oncircle[3], cost=0;
469 FOR_VERTEX(v, OUTER) {
471 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
472 if (nominal_edge_distance==0) continue;
474 find_nearest_oncircle(oncircle, vertices[v]);
477 vertex_mean_edge_lengths[v] *
478 (nominal_edge_distance*nominal_edge_distance) /
479 (hypotD2(vertices[v], oncircle) + 1e-6);
484 /*---------- noncircular rim cost ----------*/
486 double noncircular_rim_cost(const Vertices vertices, int section) {
491 FOR_RIM_VERTEX(vy,vx,v, OUTER) {
492 find_nearest_oncircle(oncircle, vertices[v]);
494 double d2= hypotD2(vertices[v], oncircle);
500 /*---------- overly sharp edge cost ----------*/
505 * / | `-_ P'Q' ------ S'
518 * Let delta = angle between two triangles' normals
520 * Giving energy contribution:
527 double edge_angle_cost(const Vertices vertices, int section) {
528 double pq1[D3], rp[D3], ps[D3], rp_2d[D3], ps_2d[D3], rs_2d[D3];
530 const double minradius_base= 0.2;
532 int pi,e,qi,ri,si, k;
533 // double our_epsilon=1e-6;
534 double total_cost= 0;
536 FOR_EDGE(pi,e,qi, OUTER) {
537 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
539 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
540 ri= EDGE_END2(pi,(e +1)%V6); if (ri<0) continue;
543 pq1[k]= -vertices[pi][k] + vertices[qi][k];
544 rp[k]= -vertices[ri][k] + vertices[pi][k];
545 ps[k]= -vertices[pi][k] + vertices[si][k];
548 normalise(pq1,1,1e-6);
549 xprod(rp_2d, rp,pq1); /* projects RP into plane normal to PQ */
550 xprod(ps_2d, ps,pq1); /* likewise PS */
551 K rs_2d[k]= rp_2d[k] + ps_2d[k];
552 /* radius of circumcircle of R'P'S' from Wikipedia
553 * `Circumscribed circle' */
558 r= a*b*c / sqrt((a+b+c)*(a-b+c)*(b-c+a)*(c-a+b) + 1e-6);
560 double minradius= minradius_base + edge_angle_cost_circcircrat*(a+b);
561 double deficit= minradius - r;
562 if (deficit < 0) continue;
563 double cost= deficit*deficit;
571 /*---------- small triangles cost ----------*/
574 * Consider a triangle PQS
576 * Cost is 1/( area^2 )
579 double small_triangles_cost(const Vertices vertices, int section) {
580 double pq[D3], ps[D3];
583 // double our_epsilon=1e-6;
584 double total_cost= 0;
586 FOR_EDGE(pi,e,qi, OUTER) {
587 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
589 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
592 pq[k]= vertices[qi][k] - vertices[pi][k];
593 ps[k]= vertices[si][k] - vertices[pi][k];
597 double cost= 1/(magnD2(x) + 0.01);
599 //double cost= pow(magnD(spqxpqr), 3);
600 //assert(dot>=-1 && dot <=1);
601 //double cost= 1-dot;
608 /*---------- nonequilateral triangles cost ----------*/
611 * Consider a triangle PQR
613 * let edge lengths a=|PQ| b=|QR| c=|RP|
615 * predicted edge length p = 1/3 * (a+b+c)
617 * compute cost for each x in {a,b,c}
620 * cost = (x-p)^2 / p^2
624 double nonequilateral_triangles_cost(const Vertices vertices, int section) {
625 double pr[D3], abc[3];
626 int pi,e0,e1,qi,ri, k,i;
627 double our_epsilon=1e-6;
628 double total_cost= 0;
630 FOR_EDGE(pi,e0,qi, OUTER) {
632 ri= EDGE_END2(pi,e1); if (ri<0) continue;
634 K pr[k]= -vertices[pi][k] + vertices[ri][k];
636 abc[0]= edge_lengths[pi][e0]; /* PQ */
637 abc[1]= edge_lengths[qi][e1]; /* QR */
640 double p= (1/3.0) * (abc[0]+abc[1]+abc[2]);
641 double p_inv2= 1/(p*p + our_epsilon);
643 for (i=0; i<3; i++) {
644 double diff= (abc[i] - p);
645 double cost= diff*diff * p_inv2;