chiark / gitweb /
compile ok so far; before OutEdge iterator redo
authorIan Jackson <ian@davenant.relativity.greenend.org.uk>
Sun, 30 Dec 2007 12:44:17 +0000 (12:44 +0000)
committerIan Jackson <ian@davenant.relativity.greenend.org.uk>
Sun, 30 Dec 2007 12:44:17 +0000 (12:44 +0000)
bgl.cpp
common.h
energy.c
mgraph.h

diff --git a/bgl.cpp b/bgl.cpp
index fbc5b7db955017e34a19e28c004ce945cc5becfe..1beae8cc59f8032c94c2f91e59527a83fad7de94 100644 (file)
--- a/bgl.cpp
+++ b/bgl.cpp
@@ -5,10 +5,20 @@
 
 #include <math.h>
 
+#include <iterator>
+
+#include <boost/config.hpp>
+#include <boost/iterator/iterator_facade.hpp>
+#include <boost/graph/graph_traits.hpp>
+#include <boost/graph/graph_concepts.hpp>
+#include <boost/graph/dijkstra_shortest_paths.hpp>
+#include <boost/graph/properties.hpp>
+#include <boost/iterator/counting_iterator.hpp>
+#include <boost/iterator/iterator_categories.hpp>
+
 extern "C" {
 #include "bgl.h"
 #include "mgraph.h"
-#include "common.h"
 }
 
 /*
@@ -38,6 +48,12 @@ extern "C" {
 
 class Graph { }; // this is a dummy as our graph has no actual representation
 
+struct OutEdgeIncrable {
+  int f;
+  OutEdgeIncrable& operator++() { f += 1<<ESHIFT; return *this; }
+  OutEdgeIncrable(int v, int e) : f(v | (e << ESHIFT)) { }
+};
 namespace boost {
   // We make Graph a model of various BGL Graph concepts.
   // This mainly means that graph_traits<Graph> has lots of stuff.
@@ -49,12 +65,6 @@ namespace boost {
     public virtual vertex_list_graph_tag,
     public virtual edge_list_graph_tag { };
   
-  struct OutEdgeIncrable {
-    int f;
-    OutEdgeIncrable& operator++() { f += 1<<ESHIFT; return self; }
-    OutEdgeIncrable(int v, int e) : f(v | (e << ESHIFT)) { }
-  };
   struct graph_traits<Graph> {
 
     // Concept Graph:
@@ -62,15 +72,15 @@ namespace boost {
     typedef int vertex_descriptor; /* vertex number, -1 => none */
     typedef int edge_descriptor; /* see above */
     typedef undirected_tag directed_category;
-    typedef disallow_parallel_ege edge_parallel_category;
+    typedef disallow_parallel_edge_tag edge_parallel_category;
     typedef layout_graph_traversal_category traversal_category;
     inline int null_vertex() { return -1; }
 
     // Concept IncidenceGraph:
     
     typedef counting_iterator<OutEdgeIncrable,
-      forward_iterator_tag> out_edge_iterator;
-    typedef int degree_size_type;
+      std::forward_iterator_tag> out_edge_iterator;
+    typedef unsigned degree_size_type;
     
     inline int source(int f, const Graph&) { return f&VMASK; }
     inline int target(int f, const Graph&) { return EDGE_END2(f&VMASK, f>>ESHIFT); }
@@ -79,7 +89,9 @@ namespace boost {
       return std::make_pair(out_edge_iterator(OutEdgeIncrable(v, VE_MIN(v))),
                            out_edge_iterator(OutEdgeIncrable(v, VE_MAX(v))));
     }
-    inline out_degree(int v, const Graph&) { return VE_MAX(v) - VE_MIN(v); }
+    inline unsigned out_degree(int v, const Graph&) {
+      return VE_MAX(v) - VE_MIN(v);
+    }
 
     // Concept VertexListGraph:
     typedef counting_iterator<int> vertex_iterator;
index 77589f98c45dcbba5770130e0d13fd317a14ab60..3978b5bc83ef8fa06bc3001276483831cf31beac 100644 (file)
--- a/common.h
+++ b/common.h
@@ -5,10 +5,16 @@
 #ifndef COMMON_H
 #define COMMON_H
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
+
 #include <math.h>
 #include <float.h>
 #include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
 
 #define D3 3
 
@@ -17,6 +23,7 @@ double hypotD2(const double p[D3], const double q[D3]);
 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]);
 
 #ifdef FP_FAST_FMA
 # define fma_fast fma
index 139e7e1304d11937fe5d5261e6dba81d9ad55c3e..f28135a3bf4d2bc02ce9f700c89d0d6c77dfba20 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -6,22 +6,26 @@
 #include "bgl.h"
 #include "mgraph.h"
 
+#include <gsl/gsl_errno.h>
+#include <gsl/gsl_multimin.h>
+
 #define BEST_F "best"
 #define INITIAL_F "initial"
 
 static double edgewise_vertex_displacement_cost(const Vertices vertices);
-static double noncircular_rim_cost(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;
 static void flushoutput(void);
+static void diee(const char *what) { perror(what); exit(16); }
 
 static void cost(double *energy, double tweight, double tcost);
 #define COST(weight, compute) cost(&energy, (weight), (compute))
 
 /*---------- main energy computation and subroutines ----------*/
 
-static double compute_energy(Vertices vertices) {
+static double compute_energy(const Vertices vertices) {
   double vertex_areas[N], energy;
 
   compute_vertex_areas(vertices,vertex_areas);
@@ -34,7 +38,9 @@ static double compute_energy(Vertices vertices) {
   
   printf("| total %# e |", energy);
   if (energy < best_energy) {
-    FILE *best;
+    FILE *best_f;
+    int r;
+    
     printf(" BEST");
     
     best_f= fopen(BEST_F ".new","wb");  if (!best_f) diee("fopen new best");
@@ -55,10 +61,12 @@ static void cost(double *energy, double tweight, double tcost) {
 }
 
 static void flushoutput(void) {
-  if (fflush(stdout) || ferror(stdout)) { perror("stdout"); exit(-1); }
+  if (fflush(stdout) || ferror(stdout)) diee("stdout");
 }
 
 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;
@@ -74,7 +82,7 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) {
        e2v[k]= vertices[v2][k] - vertices[v0][k];
       }
       xprod(av, e1v, e2v);
-      total += hypotD1(av);
+      total += magnD(av);
       count++;
     }
     areas[v0]= total / count;
@@ -114,23 +122,23 @@ static void gsldie(const char *what, int status) {
 
 static gsl_multimin_fminimizer *minimiser;
 
-static const stop_epsilon= 1e-4;
+static const double stop_epsilon= 1e-4;
 
 #define DIM (N*D3)
 
 static double minfunc_f(const gsl_vector *x, void *params) {
   assert(x->size == DIM);
   assert(x->stride == 1);
-  return compute_energy((Vertices)x->data);
+  return compute_energy((const double(*)[D3])x->data);
 }
 
 int main(int argc, const char *const *argv) {
-  struct gsl_multimin_function multimin_function;
+  gsl_multimin_function multimin_function;
   double size;
   Vertices initial, step_size;
-  FILE *initial;
+  FILE *initial_f;
   gsl_vector initial_gsl, step_size_gsl;
-  int r;
+  int r, v, vx,vy, k;
   
   if (argc>1) { fputs("takes no arguments\n",stderr); exit(8); }
 
@@ -153,27 +161,16 @@ int main(int argc, const char *const *argv) {
   initial_gsl.owner= 0;
   step_size_gsl= initial_gsl;
 
-  initial_gsl.data= initial;
-  step_size_gsl.data= step_size;
+  initial_gsl.data= (double*)initial;
+  step_size_gsl.data= (double*)step_size;
 
   FOR_VERTEX(v)
     K step_size[v][k]= 1e-3;
   FOR_RIM_VERTEX(vx,vy,v)
     step_size[v][3] *= 0.1;
-  
-  for (vy=0; vy<Y; vy+=Y-1)
-  for (vx=0; vx<x
-  for (i=0; i<DIM; i++) step_size[i]= step_size;
-  
-
-  step_size= gsl_vector_alloc(DIM);  if (!step_size) gsldie("alloc step");
-  gsl_vector_set_all(step_size, 1e-3);
-
-  assert(step_
-  step_
 
   r= gsl_multimin_fminimizer_set(minimiser, &multimin_function,
-                                &initial_gsl, &step_size);
+                                &initial_gsl, &step_size_gsl);
   if (r) { gsldie("fminimizer_set",r); }
   
   for (;;) {
@@ -189,6 +186,7 @@ int main(int argc, const char *const *argv) {
     if (r==GSL_SUCCESS) break;
     assert(r==GSL_CONTINUE);
   }
+  return 0;
 }
 
 /*---------- Edgewise vertex displacement ----------*/
@@ -256,14 +254,14 @@ int main(int argc, const char *const *argv) {
    */
 
 static double edgewise_vertex_displacement_cost(const Vertices vertices) {
-  static const l3_epsison= 1e-6;
+  static const double l3_epsilon= 1e-6;
 
   int pi,e,qi,ri,si, k;
   double m[D3], mprime[D3], b, d2, l, sigma_bd2_l3;
 
   FOR_EDGE(pi,e,qi) {
-    ri= EDGE_END2(pi,(e+1)%V6); if (r<0) continue;
-    si= EDGE_END2(pi,(e+5)%V6); if (s<0) continue;
+    ri= EDGE_END2(pi,(e+1)%V6); if (ri<0) continue;
+    si= EDGE_END2(pi,(e+5)%V6); if (si<0) continue;
     assert(ri == EDGE_END2(qi,(e+2)%V6));
     assert(si == EDGE_END2(qi,(e+4)%V6));
     
@@ -271,8 +269,8 @@ static double edgewise_vertex_displacement_cost(const Vertices vertices) {
     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][k] - vertices[si][k]);
-    l3 = l*l*l + l3_epsilon;
+    l= hypotD(vertices[ri], vertices[si]);
+    double l3 = l*l*l + l3_epsilon;
 
     sigma_bd2_l3 += b * d2 / l3;
   }
@@ -281,7 +279,7 @@ static double edgewise_vertex_displacement_cost(const Vertices vertices) {
 
 /*---------- noncircular rim cost ----------*/
 
-static double noncircular_rim_cost(Vertices vertices) {
+static double noncircular_rim_cost(const Vertices vertices) {
   int vy,vx,v;
   double cost= 0.0;
   
@@ -298,4 +296,5 @@ static double noncircular_rim_cost(Vertices vertices) {
     double d2= hypotD2(vertices[v], oncircle);
     cost += d2*d2;
   }
+  return cost;
 }
index 46383677d2ec6c153e799d5936329529d3e75de2..24b02dc97318d6e8fc76316cac21f659ae90a3fd 100644 (file)
--- a/mgraph.h
+++ b/mgraph.h
@@ -52,6 +52,8 @@
 #ifndef MGRAPH_H
 #define MGRAPH_H
 
+#include "common.h"
+
 #define XBITS 4
 #define X (1<<XBITS)
 #define YBITS 4
@@ -65,7 +67,7 @@
 #define XMASK (X-1)
 #define YSHIFT XBITS
 #define Y1 (1 << YSHIFT)
-#define YMASK (Y-1 << YSHIFT)
+#define YMASK ((Y-1) << YSHIFT)
 
 #define V6 6
 
@@ -85,7 +87,7 @@ extern int edge_end2(unsigned v1, int e);
   FOR_VPEDGE((v1),(e))                         \
     if (((v2)= EDGE_END2((v1),(e)),            \
         (init),                                \
-        (v2)) < 0) { otherwise } else
+        (v2)) < 0) { otherwise; } else
 
 #define NOTHING ((void)0)