X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=19f32e2fa3a0459ab94339a202b6844120b0a8b1;hp=9c351a52b9349b327547b7122172944071a9e0d3;hb=32c7d2a5c5cb58ed3a8fb9fc383a31a80b01c052;hpb=289edb461ff46150a66983e290438a725505d261 diff --git a/energy.c b/energy.c index 9c351a5..19f32e2 100644 --- a/energy.c +++ b/energy.c @@ -1,180 +1,304 @@ - /* - * We try to find an optimal triangle grid - */ - - #include "common.h" - #include "minimise.h" - #include "mgraph.h" - - double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6]; - - static double best_energy= DBL_MAX; - - static void addcost(double *energy, double tweight, double tcost, int pr); - #define COST(weight, compute) addcost(&energy, (weight), (compute), printing) - - void energy_init(void) { - } - - /*---------- main energy computation and subroutines ----------*/ - - double compute_energy(const struct Vertices *vs) { - static int bests_unprinted; - - double energy; - int printing; - - compute_edge_lengths(vs->a); - compute_vertex_areas(vs->a); - energy= 0; - - printing= printing_check(pr_cost,0); - - if (printing) printf("%15lld c>e |", evaluations); - - if (XBITS==3) { - COST( 3e3, line_bending_cost(vs->a)); - COST( 3e3, edge_length_variation_cost(vs->a)); - COST( 0.4e3, rim_proximity_cost(vs->a)); - COST( 1e6, edge_angle_cost(vs->a, 0.5/1.7)); - // COST( 1e1, small_triangles_cost(vs->a)); - COST( 1e12, noncircular_rim_cost(vs->a)); - stop_epsilon= 1e-6; - } else if (XBITS==4) { - COST( 3e5, line_bending_cost(vs->a)); - COST( 10e2, edge_length_variation_cost(vs->a)); - COST( 9.0e1, rim_proximity_cost(vs->a)); // 5e1 is too much - // 2.5e1 is too little - // 0.2e1 grows compared to previous ? - // 0.6e0 shrinks compared to previous ? - COST( 1e12, edge_angle_cost(vs->a, 0.5/1.3)); - COST( 1e18, noncircular_rim_cost(vs->a)); - stop_epsilon= 1e-6; - } else { - abort(); - } - - if (printing) printf("| total %# e |", energy); - - if (energy < best_energy) { - FILE *best_f; - int r; - - if (printing) { - printf(" BEST"); - if (bests_unprinted) printf(" [%4d]",bests_unprinted); - bests_unprinted= 0; - } else { - bests_unprinted++; - } - - best_f= fopen(best_file_tmp,"wb"); if (!best_f) diee("fopen new out"); - r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite"); - if (fclose(best_f)) diee("fclose new best"); - if (rename(best_file_tmp,best_file)) diee("rename install new best"); - - best_energy= energy; - } - if (printing) { - putchar('\n'); - flushoutput(); - } - - evaluations++; - return energy; - } - - static void addcost(double *energy, double tweight, double tcost, int pr) { - double tenergy= tweight * tcost; - if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy); - *energy += tenergy; - } - - /*---------- Precomputations ----------*/ - - void compute_edge_lengths(const Vertices vertices) { - int v1,e,v2; - - FOR_EDGE(v1,e,v2) - edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]); - } - - void compute_vertex_areas(const Vertices vertices) { - int v0,v1,v2, e1,e2; - // int k; - - FOR_VERTEX(v0) { - double total= 0.0, edges_total=0; - int count= 0; - - FOR_VEDGE(v0,e1,v1) { - e2= (e1+1) % V6; - v2= EDGE_END2(v0,e2); - if (v2<0) continue; - - edges_total += edge_lengths[v0][e1]; - - // double e1v[D3], e2v[D3], av[D3]; - // K { - // e1v[k]= vertices[v1][k] - vertices[v0][k]; - // e2v[k]= vertices[v2][k] - vertices[v0][k]; - // } - // xprod(av, e1v, e2v); - // total += magnD(av); - - count++; - } - vertex_areas[v0]= total / count; - vertex_mean_edge_lengths[v0]= edges_total / count; - } - } - - /*---------- Edgewise vertex displacement ----------*/ - - /* - * Definition: - * - * At each vertex Q, in each direction e: - * - * e - * Q ----->----- R - * _,-'\__/ - * _,-' delta - * P ' - * - * r - * cost = delta (we use r=3) - * Q,e - * - * - * Calculation: - * - * Let vector A = PQ - * B = QR - * - * -1 A . B - * delta = tan ------- - * | A x B | - * - * which is always in the range 0..pi because the denominator - * is nonnegative. We add epsilon to |AxB| to avoid division - * by zero. - * - * r - * cost = delta - * Q,e - */ - - double line_bending_cost(const Vertices vertices) { - static const double axb_epsilon= 1e-6; - static const double exponent_r= 4; - - int pi,e,qi,ri, k; - double a[D3], b[D3], axb[D3]; - double total_cost= 0; - - FOR_EDGE(qi,e,ri) { - pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue; +/* + * We try to find an optimal triangle grid + */ + +#include "common.h" +#include "minimise.h" +#include "mgraph.h" +#include "parallel.h" + +double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6]; + +static double best_energy= DBL_MAX; + +static void addcost(double *energy, double tweight, double tcost, int pr); + +/*---------- main energy computation, weights, etc. ----------*/ + +typedef double CostComputation(const Vertices vertices, int section); +typedef void PreComputation(const Vertices vertices, int section); + +typedef struct { + double weight; + CostComputation *fn; +} CostContribution; + +#define NPRECOMPS ((sizeof(precomps)/sizeof(precomps[0]))) +#define NCOSTS ((sizeof(costs)/sizeof(costs[0]))) +#define COST(weight, compute) { (weight),(compute) }, + +static PreComputation *const precomps[]= { + compute_edge_lengths, + compute_vertex_areas +}; + +static const CostContribution costs[]= { + +#if XBITS==3 +#define STOP_EPSILON 1e-6 + COST( 3e3, vertex_displacement_cost) + COST( 0.4e3, rim_proximity_cost) + COST( 1e7, edge_angle_cost) + #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.2/1.7) + COST( 1e2, small_triangles_cost) + COST( 1e12, noncircular_rim_cost) +#endif + +#if XBITS==4 +#define STOP_EPSILON 1.2e-4 + COST( 3e3, vertex_displacement_cost) + COST( 0.2e3, rim_proximity_cost) +// COST( 1e6, edge_angle_cost) + #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.7) +// COST( 1e1, small_triangles_cost) + COST( 1e12, noncircular_rim_cost) +#endif + +#if XBITS==5 +#define STOP_EPSILON 1.2e-4 + COST( 3e7, line_bending_cost) + COST( 10e2, prop_edge_length_variation_cost) + COST( 9.0e3, rim_proximity_cost) // 5e1 is too much + // 2.5e1 is too little + // 0.2e1 grows compared to previous ? + // 0.6e0 shrinks compared to previous ? + +// COST( 1e12, edge_angle_cost) + #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3) + COST( 1e18, noncircular_rim_cost) +#endif + +#if XBITS>=6 /* nonsense follows but never mind */ +#define STOP_EPSILON 1e-6 + COST( 3e5, line_bending_cost) + COST( 10e2, edge_length_variation_cost) + COST( 9.0e1, rim_proximity_cost) // 5e1 is too much + // 2.5e1 is too little + // 0.2e1 grows compared to previous ? + // 0.6e0 shrinks compared to previous ? + + COST( 1e12, edge_angle_cost) + #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3) + COST( 1e18, noncircular_rim_cost) +#endif + +}; + +const double edge_angle_cost_circcircrat= EDGE_ANGLE_COST_CIRCCIRCRAT; + +void energy_init(void) { + stop_epsilon= STOP_EPSILON; +} + +/*---------- energy computation machinery ----------*/ + +void compute_energy_separately(const struct Vertices *vs, + int section, void *energies_v, void *totals_v) { + double *energies= energies_v; + int ci; + + for (ci=0; cia, section); + inparallel_barrier(); + } + for (ci=0; cia, section); +} + +void compute_energy_combine(const struct Vertices *vertices, + int section, void *energies_v, void *totals_v) { + int ci; + double *energies= energies_v; + double *totals= totals_v; + + for (ci=0; cie |", evaluations); + + for (ci=0; cia,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite"); + if (fclose(best_f)) diee("fclose new best"); + if (rename(best_file_tmp,best_file)) diee("rename install new best"); + + best_energy= energy; + } + if (printing) { + putchar('\n'); + flushoutput(); + } + + evaluations++; + return energy; +} + +static void addcost(double *energy, double tweight, double tcost, int pr) { + double tenergy= tweight * tcost; + if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy); + *energy += tenergy; +} + +/*---------- Precomputations ----------*/ + +void compute_edge_lengths(const Vertices vertices, int section) { + int v1,e,v2; + + FOR_EDGE(v1,e,v2, OUTER) + edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]); +} + +void compute_vertex_areas(const Vertices vertices, int section) { + int v0,v1,v2, e1,e2; +// int k; + + FOR_VERTEX(v0, OUTER) { + double total= 0.0, edges_total=0; + int count= 0; + + FOR_VEDGE(v0,e1,v1) { + e2= (e1+1) % V6; + v2= EDGE_END2(v0,e2); + if (v2<0) continue; + + edges_total += edge_lengths[v0][e1]; + +// double e1v[D3], e2v[D3], av[D3]; +// K { +// e1v[k]= vertices[v1][k] - vertices[v0][k]; +// e2v[k]= vertices[v2][k] - vertices[v0][k]; +// } +// xprod(av, e1v, e2v); +// total += magnD(av); + + count++; + } + vertex_areas[v0]= total / count; + vertex_mean_edge_lengths[v0]= edges_total / count; + } +} + +/*---------- combined vertex displacement ----------*/ + + /* + * At vertex Q considering edge direction e to R + * and corresponding opposite edge to P. + * + * Let R' = Q + PQ + * D = R' - R + * delta + * [ -1 ] + * cost = [ lambda . ( D . PQ/|PQ| ) + | D x PQ/|PQ| | ] + * Q,e [ ------------------------------------------- ] + * [ |PQ| ] + */ + +double vertex_displacement_cost(const Vertices vertices, int section) { + const double inv_lambda= 1.0/1; //2; + const double delta= 4; + const double pqlen2_epsilon= 1e-12; + + int pi,e,qi,ri, k; + double pq[D3], d[D3], ddot, dcross[D3]; + double total_cost= 0; + + FOR_EDGE(qi,e,ri, OUTER) { + pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue; + + K pq[k]= -vertices[pi][k] + vertices[qi][k]; + K d[k]= vertices[qi][k] + pq[k] - vertices[ri][k]; + ddot= dotprod(d,pq); + xprod(dcross, d,pq); + double pqlen2= magnD2(pq); + double cost_basis= inv_lambda * ddot + magnD(dcross); + double cost= pow(cost_basis / (pqlen2 + pqlen2_epsilon), delta); + + total_cost += cost; + } + return total_cost; +} + +/*---------- at-vertex edge angles ----------*/ + + /* + * Definition: + * + * At each vertex Q, in each direction e: + * + * e + * Q ----->----- R + * _,-'\__/ + * _,-' delta + * P ' + * + * r + * cost = delta (we use r=3) + * Q,e + * + * + * Calculation: + * + * Let vector A = PQ + * B = QR + * + * -1 A . B + * delta = tan ------- + * | A x B | + * + * which is always in the range 0..pi because the denominator + * is nonnegative. We add epsilon to |AxB| to avoid division + * by zero. + * + * r + * cost = delta + * Q,e + */ + +double line_bending_cost(const Vertices vertices, int section) { + static const double axb_epsilon= 1e-6; + static const double exponent_r= 4; + + int pi,e,qi,ri, k; + double a[D3], b[D3], axb[D3]; + double total_cost= 0; + + FOR_EDGE(qi,e,ri, OUTER) { + pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue; //if (!(qi&XMASK)) fprintf(stderr,"%02x-%02x-%02x (%d)\n",pi,qi,ri,e); @@ -202,11 +326,11 @@ * Q,e */ -double edge_length_variation_cost(const Vertices vertices) { +double edge_length_variation_cost(const Vertices vertices, int section) { double diff, cost= 0, exponent_r= 2; int q, e,r, eback; - FOR_EDGE(q,e,r) { + FOR_EDGE(q,e,r, OUTER) { eback= edge_reverse(q,e); diff= edge_lengths[q][e] - edge_lengths[q][eback]; cost += pow(diff,exponent_r); @@ -214,6 +338,34 @@ double edge_length_variation_cost(const Vertices vertices) { return cost; } +/*---------- proportional edge length variation ----------*/ + + /* + * Definition: + * + * See the diagram above. + * r + * cost = ( |PQ| - |QR| ) + * Q,e + */ + +double prop_edge_length_variation_cost(const Vertices vertices, int section) { + const double num_epsilon= 1e-6; + + double cost= 0, exponent_r= 2; + int q, e,r, eback; + + FOR_EDGE(q,e,r, OUTER) { + eback= edge_reverse(q,e); + double le= edge_lengths[q][e]; + double leback= edge_lengths[q][eback]; + double diff= le - leback; + double num= MIN(le, leback); + cost += pow(diff / (num + num_epsilon), exponent_r); + } + return cost; +} + /*---------- rim proximity cost ----------*/ static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) { @@ -227,11 +379,11 @@ static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) { oncircle[1] *= mult; } -double rim_proximity_cost(const Vertices vertices) { +double rim_proximity_cost(const Vertices vertices, int section) { double oncircle[3], cost=0; int v; - FOR_VERTEX(v) { + FOR_VERTEX(v, OUTER) { int y= v >> YSHIFT; int nominal_edge_distance= y <= Y/2 ? y : Y-1-y; if (nominal_edge_distance==0) continue; @@ -248,12 +400,12 @@ double rim_proximity_cost(const Vertices vertices) { /*---------- noncircular rim cost ----------*/ -double noncircular_rim_cost(const Vertices vertices) { +double noncircular_rim_cost(const Vertices vertices, int section) { int vy,vx,v; double cost= 0.0; double oncircle[3]; - FOR_RIM_VERTEX(vy,vx,v) { + FOR_RIM_VERTEX(vy,vx,v, OUTER) { find_nearest_oncircle(oncircle, vertices[v]); double d2= hypotD2(vertices[v], oncircle); @@ -289,7 +441,7 @@ double noncircular_rim_cost(const Vertices vertices) { * vd, edge PQ vd */ -double edge_angle_cost(const Vertices vertices, double circcircrat) { +double edge_angle_cost(const Vertices vertices, int section) { double pq1[D3], rp[D3], ps[D3], rp_2d[D3], ps_2d[D3], rs_2d[D3]; double a,b,c,s,r; const double minradius_base= 0.2; @@ -298,7 +450,7 @@ double edge_angle_cost(const Vertices vertices, double circcircrat) { // double our_epsilon=1e-6; double total_cost= 0; - FOR_EDGE(pi,e,qi) { + FOR_EDGE(pi,e,qi, OUTER) { // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue; si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue; @@ -322,7 +474,7 @@ double edge_angle_cost(const Vertices vertices, double circcircrat) { s= 0.5*(a+b+c); r= a*b*c / sqrt((a+b+c)*(a-b+c)*(b-c+a)*(c-a+b) + 1e-6); - double minradius= minradius_base + circcircrat*(a+b); + double minradius= minradius_base + edge_angle_cost_circcircrat*(a+b); double deficit= minradius - r; if (deficit < 0) continue; double cost= deficit*deficit; @@ -358,14 +510,14 @@ double edge_angle_cost(const Vertices vertices, double circcircrat) { * vd, edge PQ vd */ -double small_triangles_cost(const Vertices vertices) { +double small_triangles_cost(const Vertices vertices, int section) { double pq[D3], ps[D3]; double x[D3]; int pi,e,qi,si, k; // double our_epsilon=1e-6; double total_cost= 0; - FOR_EDGE(pi,e,qi) { + FOR_EDGE(pi,e,qi, OUTER) { // if (!(RIM_VERTEX_P(pi) || RIM_VERTEX_P(qi))) continue; si= EDGE_END2(pi,(e+V6-1)%V6); if (si<0) continue;