chiark / gitweb /
better vertex displacement
authorIan Jackson <ian@davenant.greenend.org.uk>
Wed, 2 Jan 2008 21:07:47 +0000 (21:07 +0000)
committerIan Jackson <ian@davenant.greenend.org.uk>
Wed, 2 Jan 2008 21:07:47 +0000 (21:07 +0000)
bgl.h
common.c
common.h
energy.c
mgraph.h

diff --git a/bgl.h b/bgl.h
index d9c5d6be86d6e26b5cdcdd246c47b0da8606d43a..34736bb2888cf56aeef85758ecd842a93af74f18 100644 (file)
--- a/bgl.h
+++ b/bgl.h
@@ -9,4 +9,7 @@
 
 double graph_layout_cost(const Vertices v, const double vertex_areas[N]);
 
+double noncircular_rim_cost(const Vertices vertices);
+double edgewise_vertex_displacement_cost(const Vertices vertices);
+
 #endif /*BGL_H*/
index ba61341dc9bc45c457feba0ba87b112190df3c6a..34381e890494a489c95e0161856dccdc598a6ce6 100644 (file)
--- a/common.c
+++ b/common.c
@@ -43,6 +43,13 @@ void xprod(double r[D3], const double a[D3], const double b[D3]) {
   r[2]= a[0]*b[1] - a[1]*b[0];
 }
 
+double dotprod(const double a[D3], const double b[D3]) {
+  int k;
+  double result= 0;
+  K result += a[k] * b[k];
+  return result;
+}
+
 void libdie(const char *lib, int l, const char *str) {
   fprintf(stderr,"%s library call failed, line %d: %s\n", lib, l, str);
 }
index ebc34d17a27eb3d578f7f60ca33bd2e9057ad6c4..7e0e86d7622da0698d992bdf94af0dfef3e4676e 100644 (file)
--- a/common.h
+++ b/common.h
@@ -32,6 +32,7 @@ double hypotD2plus(const double p[D3], const double q[D3], double add);
 
 double magnD(const double pq[D3]);
 void xprod(double r[D3], const double a[D3], const double b[D3]);
+double dotprod(const double a[D3], const double b[D3]);
 
 void flushoutput(void);
 void diee(const char *what);
index 54c73faff22786536eabcbf66a3d76f6af863869..8961f549979ddf5eed7f0b5c9773b67d1d442df0 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -12,9 +12,6 @@
 static const char *input_file, *output_file;
 static char *output_file_tmp;
 
-static double edgewise_vertex_displacement_cost(const Vertices vertices);
-static double noncircular_rim_cost(const Vertices vertices);
-
 static void compute_vertex_areas(const Vertices vertices, double areas[N]);
 static double best_energy= DBL_MAX;
 
@@ -31,16 +28,16 @@ static double compute_energy(const Vertices vertices) {
   printf("cost > energy |");
 
   COST(1e4, edgewise_vertex_displacement_cost(vertices));
-  COST(1e2, graph_layout_cost(vertices,vertex_areas));
-  COST(1e4, noncircular_rim_cost(vertices));
-  
+//  COST(1e2, graph_layout_cost(vertices,vertex_areas));
+//  COST(1e4, noncircular_rim_cost(vertices));
+
   printf("| total %# e |", energy);
   if (energy < best_energy) {
     FILE *best_f;
     int r;
-    
+
     printf(" BEST");
-    
+
     best_f= fopen(output_file_tmp,"wb");  if (!best_f) diee("fopen new out");
     r= fwrite(vertices,sizeof(Vertices),1,best_f); if (r!=1) diee("fwrite");
     if (fclose(best_f)) diee("fclose new best");
@@ -52,7 +49,7 @@ static double compute_energy(const Vertices vertices) {
   flushoutput();
 
   return energy;
-}    
+}
 
 static void addcost(double *energy, double tweight, double tcost) {
   double tenergy= tweight * tcost;
@@ -62,16 +59,16 @@ static void addcost(double *energy, double tweight, double tcost) {
 
 static void compute_vertex_areas(const Vertices vertices, double areas[N]) {
   int v0,v1,v2, e1,e2, k;
-  
+
   FOR_VERTEX(v0) {
     double total= 0.0;
     int count= 0;
-    
+
     FOR_VEDGE(v0,e1,v1) {
       e2= (e1+1) % V6;
       v2= EDGE_END2(v0,e2);
       if (v2<0) continue;
-      
+
       double e1v[D3], e2v[D3], av[D3];
       K {
        e1v[k]= vertices[v1][k] - vertices[v0][k];
@@ -105,7 +102,7 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) {
    * In our application we are searching for the optimal locations of
    * N actualvertices in D3 (3) dimensions - ie, we are searching for
    * the optimal metapoint in an N*D3-dimensional space.
-   * 
+   *
    * So eg with X=Y=100, the simplex will contain 300 metavertices
    * each of which is an array of 300 doubles for the actualvertex
    * coordinates.  Hopefully this won't be too slow ...
@@ -128,7 +125,7 @@ int main(int argc, const char *const *argv) {
   FILE *initial_f;
   gsl_vector initial_gsl, step_size_gsl;
   int r, v, k;
-  
+
   if (argc!=3 || argv[1][0]=='-' || strncmp(argv[2],"-o",2))
     { fputs("usage: minimise <input> -o<output\n",stderr); exit(8); }
 
@@ -166,7 +163,7 @@ int main(int argc, const char *const *argv) {
 
   GA( gsl_multimin_fminimizer_set(minimiser, &multimin_function,
                                  &initial_gsl, &step_size_gsl) );
-  
+
   for (;;) {
     GA( gsl_multimin_fminimizer_iterate(minimiser) );
 
@@ -185,95 +182,78 @@ int main(int argc, const char *const *argv) {
 /*---------- Edgewise vertex displacement ----------*/
 
   /*
-   *  
+   *
    *
    *
    *                       Q `-_
    *              / |    `-_
-   *   R' - _ _ _/_ |       `-.
-   *    .       /   M - - - - - S
-   *    .      /    |      _,-'
-   *    .     /     |  _,-'
-   *    .    /    , P '
-   *    .   /  ,-'
-   *    .  /,-'
-   *    . /'
+   *             /  |       `-.
+   *            /   M - - - - - S
+   *           /  ' |      _,-'
+   *          /  '  |  _,-'
+   *         / '  , P '
+   *        / ',-'
+   *       /,-'
+   *      /'
    *            R
    *
+   *  Let delta =  180deg - angle RMS
    *
-   *
-   *  Find R', the `expected' location of R, by
-   *  reflecting S in M (the midpoint of QP).
-   *
-   *  Let 2d = |RR'|
-   *       b = |PQ|
-   *       l = |RS|
+   *  Let  l = |PQ|
+   *       d = |RS|
    *
    *  Giving energy contribution:
    *
-   *                               2
-   *                            b d
-   *    E             =  F   .  ----
-   *     vd, edge PQ      vd      3
-   *                             l
+   *                                   2
+   *                            l delta
+   *    E             =  F   .  --------
+   *     vd, edge PQ      vd       d
    *
-   *  (The dimensions of this are those of F_vd.)
    *
-   *  By symmetry, this calculation gives the same answer with R and S
-   *  exchanged.  Looking at the projection in the RMS plane:
-   *
-   *
-   *                          S'
-   *                        ,'
-   *                      ,'
-   *           R'               ,'             2d" = |SS'| = |RR'| = 2d
-   *     `-._         ,'
-   *                 `-._   ,'                 By congruent triangles,
-   *              ` M                  with M' = midpoint of RS,
-   *             ,'  `-._              |MM'| = |RR'|/2 = d
-   *           ,'        `-._
-   *         ,'              ` S       So use
-   *       ,'       M' _ , - '            d = |MM'|
-   *     ,'   _ , - '
-   *    R - '
+   *  (The dimensions of this are those of F_vd.)
    *
-   *  We choose this value for l (rather than |RM|+|MS|, say, or |RM|)
-   *  because we want this symmetry and because we're happy to punish
-   *  bending more than uneveness in the metric.
+   *  We calculate delta as  atan2(|AxB|, A.B)
+   *  where A = RM, B = MS
    *
-   *  In practice to avoid division by zero we'll add epsilon to l^3
-   *  and the huge energy ought then to be sufficient for the model to
-   *  avoid being close to R=S.
+   *  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.
    */
 
-static double edgewise_vertex_displacement_cost(const Vertices vertices) {
-  static const double l3_epsilon= 1e-6;
+double edgewise_vertex_displacement_cost(const Vertices vertices) {
+  static const double /*d_epsilon= 1e-6,*/ axb_epsilon= 1e-6;
 
   int pi,e,qi,ri,si, k;
-  double m[D3], mprime[D3], b, d2, l, sigma_bd2_l3=0;
+  double m[D3], a[D3], b[D3], axb[D3];
+  double total_cost= 0;
 
   FOR_EDGE(pi,e,qi) {
     ri= EDGE_END2(pi,(e+1)%V6); if (ri<0) continue;
     si= EDGE_END2(pi,(e+5)%V6); if (si<0) continue;
-    
+
     K m[k]= (vertices[pi][k] + vertices[qi][k]) * 0.5;
-    K mprime[k]= (vertices[ri][k] + vertices[si][k]) * 0.5;
-    b= hypotD(vertices[pi], vertices[qi]);
-    d2= hypotD2(m, mprime);
-    l= hypotD(vertices[ri], vertices[si]);
-    double l3 = l*l*l + l3_epsilon;
+    K a[k]= -vertices[ri][k] + m[k];
+    K b[k]= -m[k] + vertices[si][k];
+
+    xprod(axb,a,b);
+    
+    double l= 1; //hypotD(vertices[pi], vertices[qi]);
+    double d= 1; //hypotD(vertices[ri], vertices[si]) + d_epsilon;
+    double delta= atan2(magnD(axb) + axb_epsilon, dotprod(a,b));
+    
+    double cost= l * delta * delta / d;
 
-    sigma_bd2_l3 += b * d2 / l3;
+    total_cost += cost;
   }
-  return sigma_bd2_l3;
+  return total_cost;
 }
 
 /*---------- noncircular rim cost ----------*/
 
-static double noncircular_rim_cost(const Vertices vertices) {
+double noncircular_rim_cost(const Vertices vertices) {
   int vy,vx,v;
   double cost= 0.0;
-  
+
   FOR_RIM_VERTEX(vy,vx,v) {
     double oncircle[3];
     /* By symmetry, nearest point on circle is the one with
index eb4bd129fb990bb650d1da0605e780e1a13680af..9cc20eb407e4d4779da311f0f3df661befbf8a6b 100644 (file)
--- a/mgraph.h
+++ b/mgraph.h
@@ -62,9 +62,9 @@
 
 #include "common.h"
 
-#define XBITS 3
+#define XBITS 4/*3*/
 #define X (1<<XBITS)
-#define YBITS 3
+#define YBITS 3/*3*/
 #define Y ((1<<YBITS) - 1)
 
 /* vertex number:   0000 | y     | x