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 1e-6
50 COST( 3e5, line_bending_cost)
51 COST( 10e2, edge_length_variation_cost)
52 COST( 9.0e1, 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 1e-6
64 COST( 3e5, line_bending_cost)
65 COST( 10e2, edge_length_variation_cost)
66 COST( 9.0e1, 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)
78 const double edge_angle_cost_circcircrat= EDGE_ANGLE_COST_CIRCCIRCRAT;
80 void energy_init(void) {
81 stop_epsilon= STOP_EPSILON;
84 /*---------- energy computation machinery ----------*/
86 void compute_energy_separately(const struct Vertices *vs,
87 int section, void *energies_v, void *totals_v) {
88 double *energies= energies_v;
91 for (ci=0; ci<NPRECOMPS; ci++) {
92 costs[ci].fn(vs->a, section);
95 for (ci=0; ci<NCOSTS; ci++)
96 energies[ci]= costs[ci].fn(vs->a, section);
99 void compute_energy_combine(const struct Vertices *vertices,
100 int section, void *energies_v, void *totals_v) {
102 double *energies= energies_v;
103 double *totals= totals_v;
105 for (ci=0; ci<NCOSTS; ci++)
106 totals[ci] += energies[ci];
109 double compute_energy(const struct Vertices *vs) {
110 static int bests_unprinted;
112 double totals[NCOSTS], energy;
115 printing= printing_check(pr_cost,0);
117 if (printing) printf("%15lld c>e |", evaluations);
119 for (ci=0; ci<NCOSTS; ci++)
123 compute_energy_separately,
124 compute_energy_combine,
125 sizeof(totals) /* really, size of energies */,
129 for (ci=0; ci<NCOSTS; ci++)
130 addcost(&energy, costs[ci].weight, totals[ci], printing);
132 if (printing) printf("| total %# e |", energy);
134 if (energy < best_energy) {
140 if (bests_unprinted) printf(" [%4d]",bests_unprinted);
146 best_f= fopen(best_file_tmp,"wb"); if (!best_f) diee("fopen new out");
147 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
148 if (fclose(best_f)) diee("fclose new best");
149 if (rename(best_file_tmp,best_file)) diee("rename install new best");
162 static void addcost(double *energy, double tweight, double tcost, int pr) {
163 double tenergy= tweight * tcost;
164 if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy);
168 /*---------- Precomputations ----------*/
170 void compute_edge_lengths(const Vertices vertices, int section) {
173 FOR_EDGE(v1,e,v2, OUTER)
174 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
179 void compute_vertex_areas(const Vertices vertices, int section) {
183 FOR_VERTEX(v0, OUTER) {
184 double total= 0.0, edges_total=0;
187 FOR_VEDGE(v0,e1,v1) {
189 v2= EDGE_END2(v0,e2);
192 edges_total += edge_lengths[v0][e1];
194 // double e1v[D3], e2v[D3], av[D3];
196 // e1v[k]= vertices[v1][k] - vertices[v0][k];
197 // e2v[k]= vertices[v2][k] - vertices[v0][k];
199 // xprod(av, e1v, e2v);
200 // total += magnD(av);
204 vertex_areas[v0]= total / count;
205 vertex_mean_edge_lengths[v0]= edges_total / count;
209 /*---------- Edgewise vertex displacement ----------*/
214 * At each vertex Q, in each direction e:
223 * cost = delta (we use r=3)
233 * delta = tan -------
236 * which is always in the range 0..pi because the denominator
237 * is nonnegative. We add epsilon to |AxB| to avoid division
245 double line_bending_cost(const Vertices vertices, int section) {
246 static const double axb_epsilon= 1e-6;
247 static const double exponent_r= 4;
250 double a[D3], b[D3], axb[D3];
251 double total_cost= 0;
253 if (quitting_last_iteration) {
256 "section=%d thr=%#08lx qi=0x%03x START\n",
257 section,(unsigned long)pthread_self(), N);
259 quitting_reported_threads= 1;
262 FOR_EDGE(qi,e,ri, OUTER) {
263 if (quitting_last_iteration) {
266 "section=%d thr=%#08lx qi=0x%03x,e=%d,ri=0x%03x\n",
267 section,(unsigned long)pthread_self(),qi,e,ri);
271 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
273 //if (!(qi&XMASK)) fprintf(stderr,"%02x-%02x-%02x (%d)\n",pi,qi,ri,e);
275 K a[k]= -vertices[pi][k] + vertices[qi][k];
276 K b[k]= -vertices[qi][k] + vertices[ri][k];
280 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
281 double cost= pow(delta,exponent_r);
288 /*---------- edge length variation ----------*/
293 * See the diagram above.
295 * cost = ( |PQ| - |QR| )
299 double edge_length_variation_cost(const Vertices vertices, int section) {
300 double diff, cost= 0, exponent_r= 2;
303 FOR_EDGE(q,e,r, OUTER) {
304 eback= edge_reverse(q,e);
305 diff= edge_lengths[q][e] - edge_lengths[q][eback];
306 cost += pow(diff,exponent_r);
311 /*---------- rim proximity cost ----------*/
313 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
314 /* By symmetry, nearest point on circle is the one with
315 * the same angle subtended at the z axis. */
319 double mult= 1.0/ magnD(oncircle);
324 double rim_proximity_cost(const Vertices vertices, int section) {
325 double oncircle[3], cost=0;
328 FOR_VERTEX(v, OUTER) {
330 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
331 if (nominal_edge_distance==0) continue;
333 find_nearest_oncircle(oncircle, vertices[v]);
336 vertex_mean_edge_lengths[v] *
337 (nominal_edge_distance*nominal_edge_distance) /
338 (hypotD2(vertices[v], oncircle) + 1e-6);
343 /*---------- noncircular rim cost ----------*/
345 double noncircular_rim_cost(const Vertices vertices, int section) {
350 FOR_RIM_VERTEX(vy,vx,v, OUTER) {
351 find_nearest_oncircle(oncircle, vertices[v]);
353 double d2= hypotD2(vertices[v], oncircle);
359 /*---------- overly sharp edge cost ----------*/
364 * / | `-_ P'Q' ------ S'
377 * Let delta = angle between two triangles' normals
379 * Giving energy contribution:
386 double edge_angle_cost(const Vertices vertices, int section) {
387 double pq1[D3], rp[D3], ps[D3], rp_2d[D3], ps_2d[D3], rs_2d[D3];
389 const double minradius_base= 0.2;
391 int pi,e,qi,ri,si, k;
392 // double our_epsilon=1e-6;
393 double total_cost= 0;
395 FOR_EDGE(pi,e,qi, OUTER) {
396 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
398 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
399 ri= EDGE_END2(pi,(e +1)%V6); if (ri<0) continue;
402 pq1[k]= -vertices[pi][k] + vertices[qi][k];
403 rp[k]= -vertices[ri][k] + vertices[pi][k];
404 ps[k]= -vertices[pi][k] + vertices[si][k];
407 normalise(pq1,1,1e-6);
408 xprod(rp_2d, rp,pq1); /* projects RP into plane normal to PQ */
409 xprod(ps_2d, ps,pq1); /* likewise PS */
410 K rs_2d[k]= rp_2d[k] + ps_2d[k];
411 /* radius of circumcircle of R'P'S' from Wikipedia
412 * `Circumscribed circle' */
417 r= a*b*c / sqrt((a+b+c)*(a-b+c)*(b-c+a)*(c-a+b) + 1e-6);
419 double minradius= minradius_base + edge_angle_cost_circcircrat*(a+b);
420 double deficit= minradius - r;
421 if (deficit < 0) continue;
422 double cost= deficit*deficit;
430 /*---------- small triangles cost ----------*/
446 * Let delta = angle between two triangles' normals
448 * Giving energy contribution:
455 double small_triangles_cost(const Vertices vertices, int section) {
456 double pq[D3], ps[D3];
459 // double our_epsilon=1e-6;
460 double total_cost= 0;
462 FOR_EDGE(pi,e,qi, OUTER) {
463 // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue;
465 si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;
468 pq[k]= vertices[qi][k] - vertices[pi][k];
469 ps[k]= vertices[si][k] - vertices[pi][k];
473 double cost= 1/(magnD2(x) + 0.01);
475 //double cost= pow(magnD(spqxpqr), 3);
476 //assert(dot>=-1 && dot <=1);
477 //double cost= 1-dot;