chiark / gitweb /
tidy up and normalise line bending cost
authorIan Jackson <ian@davenant.relativity.greenend.org.uk>
Wed, 9 Jan 2008 18:34:59 +0000 (18:34 +0000)
committerIan Jackson <ian@davenant.relativity.greenend.org.uk>
Wed, 9 Jan 2008 18:34:59 +0000 (18:34 +0000)
energy.c
minimise.c
minimise.h

index e9b93fd2577c1daca16b850baebab3d99e410eb6..db13fbffcf866a23b87d308914bcc8362b9e6728 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -13,6 +13,12 @@ static double best_energy= DBL_MAX;
 static void addcost(double *energy, double tweight, double tcost, int pr);
 #define COST(weight, compute) addcost(&energy, (weight), (compute), printing)
 
 static void addcost(double *energy, double tweight, double tcost, int pr);
 #define COST(weight, compute) addcost(&energy, (weight), (compute), printing)
 
+double density;
+
+void energy_init(void) {
+  density= sqrt(N);
+}
+
 /*---------- main energy computation and subroutines ----------*/
 
 double compute_energy(const struct Vertices *vs) {
 /*---------- main energy computation and subroutines ----------*/
 
 double compute_energy(const struct Vertices *vs) {
@@ -27,7 +33,7 @@ double compute_energy(const struct Vertices *vs) {
 
   if (printing) printf("cost > energy |");
 
 
   if (printing) printf("cost > energy |");
 
-  COST(3e2, edgewise_vertex_displacement_cost(vs->a));
+  COST(2.25e3, line_bending_adjcost(vs->a));
   COST(1e3, edge_length_variation_cost(vs->a));
   COST(0.2e3, rim_proximity_cost(vs->a));
 //  COST(1e2, graph_layout_cost(vs->a));
   COST(1e3, edge_length_variation_cost(vs->a));
   COST(0.2e3, rim_proximity_cost(vs->a));
 //  COST(1e2, graph_layout_cost(vs->a));
@@ -58,7 +64,7 @@ double compute_energy(const struct Vertices *vs) {
 
 static void addcost(double *energy, double tweight, double tcost, int pr) {
   double tenergy= tweight * tcost;
 
 static void addcost(double *energy, double tweight, double tcost, int pr) {
   double tenergy= tweight * tcost;
-  if (pr) printf(" %# e > %# e* |", tcost, tenergy);
+  if (pr) printf(" %# e x %# e > %# e* |", tcost, tweight, tenergy);
   *energy += tenergy;
 }
 
   *energy += tenergy;
 }
 
@@ -66,7 +72,7 @@ static void addcost(double *energy, double tweight, double tcost, int pr) {
 
 void compute_edge_lengths(const Vertices vertices) {
   int v1,e,v2;
 
 void compute_edge_lengths(const Vertices vertices) {
   int v1,e,v2;
-  
+
   FOR_EDGE(v1,e,v2)
     edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
 }
   FOR_EDGE(v1,e,v2)
     edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
 }
@@ -93,7 +99,7 @@ void compute_vertex_areas(const Vertices vertices) {
 //      }
 //      xprod(av, e1v, e2v);
 //      total += magnD(av);
 //      }
 //      xprod(av, e1v, e2v);
 //      total += magnD(av);
-      
+
       count++;
     }
     vertex_areas[v0]= total / count;
       count++;
     }
     vertex_areas[v0]= total / count;
@@ -104,69 +110,78 @@ void compute_vertex_areas(const Vertices vertices) {
 /*---------- Edgewise vertex displacement ----------*/
 
   /*
 /*---------- Edgewise vertex displacement ----------*/
 
   /*
+   * Definition:
+   *
+   *    At each vertex Q, in each direction e:
    *
    *
+   *                                         e
+   *                           Q ----->----- R
+   *                     _,-'\__/
+   *                         _,-'       delta
+   *              P '
    *
    *
+   *                      r
+   *       cost    = delta          (we use r=3)
+   *           Q,e
    *
    *
-   *                       Q `-_
-   *              / |    `-_
-   *             /  |       `-.
-   *            /   M - - - - - S
-   *           /  ' |      _,-'
-   *          /  '  |  _,-'
-   *         / '  , P '
-   *        / ',-'
-   *       /,-'
-   *      /'
-   *            R
    *
    *
-   *  Let delta =  180deg - angle RMS
+   * Calculation:
    *
    *
-   *  Let  l = |PQ|
-   *       d = |RS|
+   *      Let vector A = PQ
+   *                 B = QR
    *
    *
-   *  Giving energy contribution:
+   *                          -1   A . B
+   *      delta =  tan     -------
+   *                     | A x B |
    *
    *
-   *                                   3
-   *                            l delta
-   *    E             =  F   .  --------
-   *     vd, edge PQ      vd       d
+   *      which is always in the range 0..pi because the denominator
+   *      is nonnegative.  We add epsilon to |AxB| to avoid division
+   *      by zero.
    *
    *
+   * Normalisation:
    *
    *
-   *  (The dimensions of this are those of F_vd.)
+   *    We want the answer to remain unchanged when the vertices lie
+   *    on circles.  Interposing M and N so that we have P-M-Q-N-R
+   *    generates half as much delta for each vertex.  So
+   *
+   *           ,       -1
+   *       cost    =  D   . cost
+   *           Q,e              Q,e
+   *
+   *                           where D is the linear density.
+   *
+   *                ,              -1   
+   *     Sigma  cost      =   N . D  .  Sigma  cost
+   *      Q,e       Q,e                  Q,e       Q,e
    *
    *
-   *  We calculate delta as  atan2(|AxB|, A.B)
-   *  where A = PQ, B = QR
    *
    *
-   *  In practice to avoid division by zero we'll add epsilon to d and
-   *  |AxB| and the huge energy ought then to be sufficient for the
-   *  model to avoid being close to R=S.
    */
 
    */
 
-double edgewise_vertex_displacement_cost(const Vertices vertices) {
+double line_bending_adjcost(const Vertices vertices) {
   static const double axb_epsilon= 1e-6;
   static const double axb_epsilon= 1e-6;
+  static const double exponent_r= 3;
 
 
-  int pi,e,qi,ri, k; //,si
-  double  a[D3], b[D3], axb[D3]; //m[D3],
+  int pi,e,qi,ri, k;
+  double  a[D3], b[D3], axb[D3];
   double total_cost= 0;
 
   FOR_EDGE(qi,e,ri) {
     pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
 
   double total_cost= 0;
 
   FOR_EDGE(qi,e,ri) {
     pi= EDGE_END2(qi,(e+3)%V6); if (pi<0) continue;
 
-//    K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5;
     K a[k]= -vertices[pi][k] + vertices[qi][k];
     K b[k]= -vertices[qi][k] + vertices[ri][k];
 
     xprod(axb,a,b);
     K a[k]= -vertices[pi][k] + vertices[qi][k];
     K b[k]= -vertices[qi][k] + vertices[ri][k];
 
     xprod(axb,a,b);
-    
+
     double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
     double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
-    double cost= pow(delta,3);
+    double cost= pow(delta,exponent_r);
 
     if (!e && !(qi & YMASK))
       cost *= 10;
 
     total_cost += cost;
   }
 
     if (!e && !(qi & YMASK))
       cost *= 10;
 
     total_cost += cost;
   }
-  return total_cost;
+  return total_cost / (N / density);
 }
 
 /*---------- edge length variation ----------*/
 }
 
 /*---------- edge length variation ----------*/
@@ -181,7 +196,7 @@ double edge_length_variation_cost(const Vertices vertices) {
     cost += diff*diff;
   }
   return cost;
     cost += diff*diff;
   }
   return cost;
-}    
+}
 
 /*---------- rim proximity cost ----------*/
 
 
 /*---------- rim proximity cost ----------*/
 
@@ -224,7 +239,7 @@ double noncircular_rim_cost(const Vertices vertices) {
 
   FOR_RIM_VERTEX(vy,vx,v) {
     find_nearest_oncircle(oncircle, vertices[v]);
 
   FOR_RIM_VERTEX(vy,vx,v) {
     find_nearest_oncircle(oncircle, vertices[v]);
-    
+
     double d2= hypotD2(vertices[v], oncircle);
     cost += d2*d2;
   }
     double d2= hypotD2(vertices[v], oncircle);
     cost += d2*d2;
   }
index 51114a6c59613480a4d77b4186132db7337394af..e5bbc87bb6e3523ec43cd50b9665a80aae281b96 100644 (file)
@@ -73,6 +73,7 @@ int main(int argc, const char *const *argv) {
 
   graph_layout_prepare();
   printing_init();
 
   graph_layout_prepare();
   printing_init();
+  energy_init();
 
   printf("X=%d=0x%x  Y=%d=0x%x  DIM=%d\n",X,X,Y,Y,DIM);
   
 
   printf("X=%d=0x%x  Y=%d=0x%x  DIM=%d\n",X,X,Y,Y,DIM);
   
index f1da76f1103458e54f32af6107511f42d947956c..b0d285d9b1ac16f7c4e09468cd3d5072483eaae5 100644 (file)
@@ -8,6 +8,7 @@
 #include "mgraph.h"
 
 double compute_energy(const struct Vertices *vs);
 #include "mgraph.h"
 
 double compute_energy(const struct Vertices *vs);
+void energy_init(void);
 
 double graph_layout_cost(const Vertices v);
 void graph_layout_prepare();
 
 double graph_layout_cost(const Vertices v);
 void graph_layout_prepare();
@@ -16,8 +17,10 @@ void compute_vertex_areas(const Vertices vertices);
 void compute_edge_lengths(const Vertices vertices);
 extern double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6];
 
 void compute_edge_lengths(const Vertices vertices);
 extern double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6];
 
+extern double density;
+
+double line_bending_adjcost(const Vertices vertices);
 double noncircular_rim_cost(const Vertices vertices);
 double noncircular_rim_cost(const Vertices vertices);
-double edgewise_vertex_displacement_cost(const Vertices vertices);
 double edge_length_variation_cost(const Vertices vertices);
 double rim_proximity_cost(const Vertices vertices);
 
 double edge_length_variation_cost(const Vertices vertices);
 double rim_proximity_cost(const Vertices vertices);