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,0);
28 if (printing) printf("cost > energy |");
30 COST(3e2, edgewise_vertex_displacement_cost(vs->a));
31 COST(1e3, edge_length_variation_cost(vs->a));
32 COST(0.2e3, rim_proximity_cost(vs->a));
33 // COST(1e2, graph_layout_cost(vs->a));
34 COST(1e8, noncircular_rim_cost(vs->a));
36 if (printing) printf("| total %# e |", energy);
38 if (energy < best_energy) {
42 if (printing) printf(" BEST");
44 best_f= fopen(output_file_tmp,"wb"); if (!best_f) diee("fopen new out");
45 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
46 if (fclose(best_f)) diee("fclose new best");
47 if (rename(output_file_tmp,output_file)) diee("rename install new best");
59 static void addcost(double *energy, double tweight, double tcost, int pr) {
60 double tenergy= tweight * tcost;
61 if (pr) printf(" %# e > %# e* |", tcost, tenergy);
65 /*---------- Precomputations ----------*/
67 void compute_edge_lengths(const Vertices vertices) {
71 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
74 void compute_vertex_areas(const Vertices vertices) {
79 double total= 0.0, edges_total=0;
87 edges_total += edge_lengths[v0][e1];
89 // double e1v[D3], e2v[D3], av[D3];
91 // e1v[k]= vertices[v1][k] - vertices[v0][k];
92 // e2v[k]= vertices[v2][k] - vertices[v0][k];
94 // xprod(av, e1v, e2v);
95 // total += magnD(av);
99 vertex_areas[v0]= total / count;
100 vertex_mean_edge_lengths[v0]= edges_total / count;
104 /*---------- Edgewise vertex displacement ----------*/
122 * Let delta = 180deg - angle RMS
127 * Giving energy contribution:
135 * (The dimensions of this are those of F_vd.)
137 * We calculate delta as atan2(|AxB|, A.B)
138 * where A = PQ, B = QR
140 * In practice to avoid division by zero we'll add epsilon to d and
141 * |AxB| and the huge energy ought then to be sufficient for the
142 * model to avoid being close to R=S.
145 double edgewise_vertex_displacement_cost(const Vertices vertices) {
146 static const double axb_epsilon= 1e-6;
148 int pi,e,qi,ri, k; //,si
149 double a[D3], b[D3], axb[D3]; //m[D3],
150 double total_cost= 0;
153 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
155 // K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5;
156 K a[k]= -vertices[pi][k] + vertices[qi][k];
157 K b[k]= -vertices[qi][k] + vertices[ri][k];
161 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
162 double cost= pow(delta,3);
164 if (!e && !(qi & YMASK))
172 /*---------- edge length variation ----------*/
174 double edge_length_variation_cost(const Vertices vertices) {
175 double diff, cost= 0;
176 int v0, efwd,vfwd, eback;
178 FOR_EDGE(v0,efwd,vfwd) {
179 eback= edge_reverse(v0,efwd);
180 diff= edge_lengths[v0][efwd] - edge_lengths[v0][eback];
186 /*---------- rim proximity cost ----------*/
188 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
189 /* By symmetry, nearest point on circle is the one with
190 * the same angle subtended at the z axis. */
194 double mult= 1.0/ magnD(oncircle);
199 double rim_proximity_cost(const Vertices vertices) {
200 double oncircle[3], cost=0;
205 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
206 if (nominal_edge_distance==0) continue;
208 find_nearest_oncircle(oncircle, vertices[v]);
211 vertex_mean_edge_lengths[v] *
212 (nominal_edge_distance*nominal_edge_distance) /
213 (hypotD2(vertices[v], oncircle) + 1e-6);
218 /*---------- noncircular rim cost ----------*/
220 double noncircular_rim_cost(const Vertices vertices) {
225 FOR_RIM_VERTEX(vy,vx,v) {
226 find_nearest_oncircle(oncircle, vertices[v]);
228 double d2= hypotD2(vertices[v], oncircle);