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)
18 void energy_init(void) {
22 /*---------- main energy computation and subroutines ----------*/
24 double compute_energy(const struct Vertices *vs) {
28 compute_edge_lengths(vs->a);
29 compute_vertex_areas(vs->a);
32 printing= printing_check(pr_cost,0);
34 if (printing) printf("cost > energy |");
36 COST(2.25e3, line_bending_adjcost(vs->a));
37 COST(1e3, edge_length_variation_cost(vs->a));
38 COST(0.2e3, rim_proximity_cost(vs->a));
39 // COST(1e2, graph_layout_cost(vs->a));
40 COST(1e8, noncircular_rim_cost(vs->a));
42 if (printing) printf("| total %# e |", energy);
44 if (energy < best_energy) {
48 if (printing) printf(" BEST");
50 best_f= fopen(output_file_tmp,"wb"); if (!best_f) diee("fopen new out");
51 r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
52 if (fclose(best_f)) diee("fclose new best");
53 if (rename(output_file_tmp,output_file)) diee("rename install new best");
65 static void addcost(double *energy, double tweight, double tcost, int pr) {
66 double tenergy= tweight * tcost;
67 if (pr) printf(" %# e x %# e > %# e* |", tcost, tweight, tenergy);
71 /*---------- Precomputations ----------*/
73 void compute_edge_lengths(const Vertices vertices) {
77 edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
80 void compute_vertex_areas(const Vertices vertices) {
85 double total= 0.0, edges_total=0;
93 edges_total += edge_lengths[v0][e1];
95 // double e1v[D3], e2v[D3], av[D3];
97 // e1v[k]= vertices[v1][k] - vertices[v0][k];
98 // e2v[k]= vertices[v2][k] - vertices[v0][k];
100 // xprod(av, e1v, e2v);
101 // total += magnD(av);
105 vertex_areas[v0]= total / count;
106 vertex_mean_edge_lengths[v0]= edges_total / count;
110 /*---------- Edgewise vertex displacement ----------*/
115 * At each vertex Q, in each direction e:
124 * cost = delta (we use r=3)
134 * delta = tan -------
137 * which is always in the range 0..pi because the denominator
138 * is nonnegative. We add epsilon to |AxB| to avoid division
147 * We want the minimum energy to remain unchanged with changes in
148 * triangle densitiy, when the vertices lie evenly spaced on
149 * circles, and we do this by normalising the force ie the
150 * derivative of the energy with respect to linear motions of the
153 * We consider only the force on Q due to PQR, wlog. (Forces on
154 * P qnd R due to PQR are equal and opposite so normalising
155 * forces on Q will normalise them too.)
157 * Force on Q is in the plnae PQR and normal to PR, so we can
158 * consider it only linearly in that dimension. WLOG let that be
159 * the x dimension. So with f' representing df'/dx_Q:
163 * Q,e Q,e err looks like we can only do
164 * this if we make some kind of
165 * assumption about delta or
169 * Interposing M and N so that we have P-M-Q-N-R
170 * generates half as much delta for each vertex. So
173 In that case the force on Q
176 *Normalising for equal linear
180 * linear force on Q due to e = ------- cost
184 * (we will consider only one e and one coord and hope
185 * that doesn't lead us astray.)
192 * where D is the linear density.
195 * Sigma cost = N . D . Sigma cost
200 double line_bending_adjcost(const Vertices vertices) {
201 static const double axb_epsilon= 1e-6;
202 static const double exponent_r= 3;
205 double a[D3], b[D3], axb[D3];
206 double total_cost= 0;
209 pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
211 K a[k]= -vertices[pi][k] + vertices[qi][k];
212 K b[k]= -vertices[qi][k] + vertices[ri][k];
216 double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
217 double cost= pow(delta,exponent_r);
219 if (!e && !(qi & YMASK))
224 return total_cost / (N / density);
227 /*---------- edge length variation ----------*/
232 * See the diagram above.
237 double edge_length_variation_cost(const Vertices vertices) {
238 double diff, cost= 0;
242 eback= edge_reverse(q,e);
243 diff= edge_lengths[q][e] - edge_lengths[q][eback];
249 /*---------- rim proximity cost ----------*/
251 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {
252 /* By symmetry, nearest point on circle is the one with
253 * the same angle subtended at the z axis. */
257 double mult= 1.0/ magnD(oncircle);
262 double rim_proximity_cost(const Vertices vertices) {
263 double oncircle[3], cost=0;
268 int nominal_edge_distance= y <= Y/2 ? y : Y-1-y;
269 if (nominal_edge_distance==0) continue;
271 find_nearest_oncircle(oncircle, vertices[v]);
274 vertex_mean_edge_lengths[v] *
275 (nominal_edge_distance*nominal_edge_distance) /
276 (hypotD2(vertices[v], oncircle) + 1e-6);
281 /*---------- noncircular rim cost ----------*/
283 double noncircular_rim_cost(const Vertices vertices) {
288 FOR_RIM_VERTEX(vy,vx,v) {
289 find_nearest_oncircle(oncircle, vertices[v]);
291 double d2= hypotD2(vertices[v], oncircle);