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) {
75 int v0,v1,v2, e1,e2, k;
78 double total= 0.0, edges_total=0;
86 edges_total += edge_lengths[v0][e1];
88 double e1v[D3], e2v[D3], av[D3];
90 e1v[k]= vertices[v1][k] - vertices[v0][k];
91 e2v[k]= vertices[v2][k] - vertices[v0][k];
98 vertex_areas[v0]= total / count;
99 vertex_mean_edge_lengths[v0]= edges_total / count;
103 /*---------- Edgewise vertex displacement ----------*/
121 * Let delta = 180deg - angle RMS
126 * Giving energy contribution:
134 * (The dimensions of this are those of F_vd.)
136 * We calculate delta as atan2(|AxB|, A.B)
137 * where A = PQ, B = QR
139 * In practice to avoid division by zero we'll add epsilon to d and
140 * |AxB| and the huge energy ought then to be sufficient for the
141 * model to avoid being close to R=S.
144 double edgewise_vertex_displacement_cost(const Vertices vertices) {
145 static const double axb_epsilon= 1e-6;
147 int pi,e,qi,ri, k; //,si
148 double a[D3], b[D3], axb[D3]; //m[D3],
149 double total_cost= 0;
152 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
154 // K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5;
155 K a[k]= -vertices[pi][k] + vertices[qi][k];
156 K b[k]= -vertices[qi][k] + vertices[ri][k];
160 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
161 double cost= pow(delta,3);
163 if (!e && !(qi & YMASK))
171 /*---------- edge length variation ----------*/
173 double edge_length_variation_cost(const Vertices vertices) {
174 double diff, cost= 0;
175 int v0, efwd,vfwd, eback;
177 FOR_EDGE(v0,efwd,vfwd) {
178 eback= edge_reverse(v0,efwd);
179 diff= edge_lengths[v0][efwd] - edge_lengths[v0][eback];
185 /*---------- rim proximity cost ----------*/
187 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
188 /* By symmetry, nearest point on circle is the one with
189 * the same angle subtended at the z axis. */
193 double mult= 1.0/ magnD(oncircle);
198 double rim_proximity_cost(const Vertices vertices) {
199 double oncircle[3], cost=0;
204 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
205 if (nominal_edge_distance==0) continue;
207 find_nearest_oncircle(oncircle, vertices[v]);
210 vertex_mean_edge_lengths[v] *
211 (nominal_edge_distance*nominal_edge_distance) /
212 (hypotD2(vertices[v], oncircle) + 1e-6);
217 /*---------- noncircular rim cost ----------*/
219 double noncircular_rim_cost(const Vertices vertices) {
224 FOR_RIM_VERTEX(vy,vx,v) {
225 find_nearest_oncircle(oncircle, vertices[v]);
227 double d2= hypotD2(vertices[v], oncircle);