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 /*---------- main energy computation and subroutines ----------*/
18 double compute_energy(const struct Vertices *vs) {
22 compute_edge_lengths(vs->a);
23 compute_vertex_areas(vs->a);
26 printing= printing_check(pr_cost);
28 if (printing) printf("cost > energy |");
30 COST(1e2, edgewise_vertex_displacement_cost(vs->a));
31 COST(1e2, graph_layout_cost(vs->a));
32 COST(1e3, edge_length_variation_cost(vs->a));
33 // COST(1e6, noncircular_rim_cost(vs->a));
35 if (printing) printf("| total %# e |", energy);
37 if (energy < best_energy) {
41 if (printing) printf(" BEST");
43 best_f= fopen(output_file_tmp,"wb"); if (!best_f) diee("fopen new out");
44 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
45 if (fclose(best_f)) diee("fclose new best");
46 if (rename(output_file_tmp,output_file)) diee("rename install new best");
58 static void addcost(double *energy, double tweight, double tcost, int pr) {
59 double tenergy= tweight * tcost;
60 if (pr) printf(" %# e > %# e |", tcost, tenergy);
64 /*---------- Precomputations ----------*/
66 void compute_edge_lengths(const Vertices vertices) {
70 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
73 void compute_vertex_areas(const Vertices vertices) {
74 int v0,v1,v2, e1,e2, k;
77 double total= 0.0, edges_total=0;
85 edges_total += edge_lengths[v0][e1];
87 double e1v[D3], e2v[D3], av[D3];
89 e1v[k]= vertices[v1][k] - vertices[v0][k];
90 e2v[k]= vertices[v2][k] - vertices[v0][k];
97 vertex_areas[v0]= total / count;
98 vertex_mean_edge_lengths[v0]= edges_total / count;
102 /*---------- Edgewise vertex displacement ----------*/
120 * Let delta = 180deg - angle RMS
125 * Giving energy contribution:
133 * (The dimensions of this are those of F_vd.)
135 * We calculate delta as atan2(|AxB|, A.B)
136 * where A = PQ, B = QR
138 * In practice to avoid division by zero we'll add epsilon to d and
139 * |AxB| and the huge energy ought then to be sufficient for the
140 * model to avoid being close to R=S.
143 double edgewise_vertex_displacement_cost(const Vertices vertices) {
144 static const double axb_epsilon= 1e-6;
146 int pi,e,qi,ri, k; //,si
147 double a[D3], b[D3], axb[D3]; //m[D3],
148 double total_cost= 0;
151 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
153 // K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5;
154 K a[k]= -vertices[pi][k] + vertices[qi][k];
155 K b[k]= -vertices[qi][k] + vertices[ri][k];
159 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
160 double cost= pow(delta,3);
162 if (!e && !(qi & YMASK))
170 /*---------- edge length variation ----------*/
172 double edge_length_variation_cost(const Vertices vertices) {
173 double diff, cost= 0;
174 int v0, efwd,vfwd, eback;
176 FOR_EDGE(v0,efwd,vfwd) {
177 eback= edge_reverse(v0,efwd);
178 diff= edge_lengths[v0][efwd] - edge_lengths[v0][eback];
184 /*---------- noncircular rim cost ----------*/
186 double noncircular_rim_cost(const Vertices vertices) {
190 FOR_RIM_VERTEX(vy,vx,v) {
192 /* By symmetry, nearest point on circle is the one with
193 * the same angle subtended at the z axis. */
194 oncircle[0]= vertices[v][0];
195 oncircle[1]= vertices[v][1];
197 double mult= 1.0/ magnD(oncircle);
200 double d2= hypotD2(vertices[v], oncircle);