chiark / gitweb /
merge
[moebius2.git] / energy.c
index ae592cf4817fb6895f40cf2454cda90b190153bd..9bd66e31a93e0360c8c9614d0f3760afa6ef3fa8 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -16,18 +16,23 @@ 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;
 
-static const CostContribution costs[]= {
-#define PRECOMP(compute) { 0,(compute) },
+#define NPRECOMPS ((sizeof(precomps)/sizeof(precomps[0])))
+#define NCOSTS ((sizeof(costs)/sizeof(costs[0])))
 #define COST(weight, compute) { (weight),(compute) },
 
-  PRECOMP(compute_edge_lengths)
-  PRECOMP(compute_vertex_areas)
+static PreComputation *const precomps[]= {
+  compute_edge_lengths,
+  compute_vertex_areas
+};
+
+static const CostContribution costs[]= {
 
 #if XBITS==3
 #define STOP_EPSILON 1e-6
@@ -41,6 +46,34 @@ static const CostContribution costs[]= {
 #endif
 
 #if XBITS==4
+#define STOP_EPSILON 1.2e-4
+    COST(  3e5,   line_bending_cost)
+    COST( 10e3,   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==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)
@@ -53,9 +86,8 @@ static const CostContribution costs[]= {
                   #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3)
     COST(  1e18,   noncircular_rim_cost)
 #endif
-};
 
-#define NCOSTS ((sizeof(costs)/sizeof(costs[0])))
+};
 
 const double edge_angle_cost_circcircrat= EDGE_ANGLE_COST_CIRCCIRCRAT;
 
@@ -65,51 +97,51 @@ void energy_init(void) {
 
 /*---------- energy computation machinery ----------*/
 
-typedef struct {
-  double total;
-  const CostContribution *cc;
-} CostComputationData;
-
 void compute_energy_separately(const struct Vertices *vs,
-                        int section, void *energy_v, void *ccd_v) {
-  CostComputationData *ccd= ccd_v;
-  double *energy= energy_v;
-  *energy= ccd->cc->fn(vs->a, section);
+                        int section, void *energies_v, void *totals_v) {
+  double *energies= energies_v;
+  int ci;
+  
+  for (ci=0; ci<NPRECOMPS; ci++) {
+    precomps[ci](vs->a, section);
+    inparallel_barrier();
+  }
+  for (ci=0; ci<NCOSTS; ci++)
+    energies[ci]= costs[ci].fn(vs->a, section);
 }
 
 void compute_energy_combine(const struct Vertices *vertices,
-                        int section, void *energy_v, void *ccd_v) {
-  CostComputationData *ccd= ccd_v;
-  double *energy= energy_v;
-  ccd->total += *energy;
+                        int section, void *energies_v, void *totals_v) {
+  int ci;
+  double *energies= energies_v;
+  double *totals= totals_v;
+
+  for (ci=0; ci<NCOSTS; ci++)
+    totals[ci] += energies[ci];
 }
 
 double compute_energy(const struct Vertices *vs) {
   static int bests_unprinted;
 
-  double energy;
+  double totals[NCOSTS], energy;
   int ci, printing;
-  CostComputationData ccd;
 
   printing= printing_check(pr_cost,0);
 
   if (printing) printf("%15lld c>e |", evaluations);
 
-  energy= 0;
+  for (ci=0; ci<NCOSTS; ci++)
+    totals[ci]= 0;
 
-  for (ci=0; ci<NCOSTS; ci++) {
-    ccd.total= 0;
-    ccd.cc= &costs[ci];
-    
-    inparallel(vs,
-              compute_energy_separately,
-              compute_energy_combine,
-              sizeof(energy),
-              &ccd);
-
-    if (ccd.cc->weight != 0)
-      addcost(&energy, costs[ci].weight, ccd.total, printing);
-  }
+  inparallel(vs,
+            compute_energy_separately,
+            compute_energy_combine,
+            sizeof(totals) /* really, size of energies */,
+            totals);
+
+  energy= 0;
+  for (ci=0; ci<NCOSTS; ci++)
+    addcost(&energy, costs[ci].weight, totals[ci], printing);
 
   if (printing) printf("| total %# e |", energy);
 
@@ -149,16 +181,14 @@ static void addcost(double *energy, double tweight, double tcost, int pr) {
 
 /*---------- Precomputations ----------*/
 
-double compute_edge_lengths(const Vertices vertices, int section) {
+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]);
-
-  return 0;
 }
 
-double compute_vertex_areas(const Vertices vertices, int section) {
+void compute_vertex_areas(const Vertices vertices, int section) {
   int v0,v1,v2, e1,e2;
 //  int k;
 
@@ -186,8 +216,6 @@ double compute_vertex_areas(const Vertices vertices, int section) {
     vertex_areas[v0]= total / count;
     vertex_mean_edge_lengths[v0]= edges_total / count;
   }
-
-  return 0;
 }
 
 /*---------- Edgewise vertex displacement ----------*/
@@ -275,6 +303,32 @@ double edge_length_variation_cost(const Vertices vertices, int section) {
   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) {
+  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 + 1e-6), exponent_r);
+  }
+  return cost;
+}
+
 /*---------- rim proximity cost ----------*/
 
 static void find_nearest_oncircle(double oncircle[D3], const double p[D3]) {