chiark / gitweb /
rename project to view; do not clean best
[moebius2.git] / bgl.cpp
diff --git a/bgl.cpp b/bgl.cpp
index e75918a9d6d8b5799e51caedb2ae39072728b7db..0d1f355323cb9f064281e0079e709f1e55f61d9d 100644 (file)
--- a/bgl.cpp
+++ b/bgl.cpp
@@ -22,10 +22,11 @@ extern "C" {
 }
 
 /*
- * edge descriptor f =   00 | e | y     | x
- *                            3  YBITS   XBITS
+ * edge descriptor f =   0000 | e | y     | x
+ *                              3  YBITS   XBITS
  *
- * e is 0..5.  The edge is edge e out of vertex (x,y).
+ * e is 0..6.  The edge is edge e out of vertex (x,y), or if
+ * e==6 it's the `at end' value for the out edge iterator.
  *
  * BGL expects an undirected graph's edges to have two descriptors
  * each, one in each direction (otherwise e would be just 0..2).
@@ -44,28 +45,36 @@ extern "C" {
  */
 
 #define VMASK (YMASK|XMASK)
-#define ESHIFT (YBITS|XBITS)
+#define ESHIFT (YBITS+XBITS)
 
 class Graph { }; // this is a dummy as our graph has no actual representation
 
 using namespace boost;
 
 /*
- * We use the following alternative numbering for iterating over edges:
+ * We iterate over edges in the following order:
  *
- *      ix:        \0   /1
- *                  \  /
- *               ___ 0   __
- *               2    1   3
- *                  /  \
- *                4/   5\
+ *          \#0  /1#
+ *          \  /
+ *       ___ 0   __
+ *      #2    1   #3
+ *          /  \
+ *       #4/  #5\     and finally #6 is V6
  *
  *
- * This numbering permits the order-4 nodes at the strip's edge
- * to have a contiguous edge iterator values 2..5 or 0..3.
+ * This ordering permits the order-4 nodes at the strip's edge
+ * to have a contiguous edge iterator values.  The iterator
+ * starts at #0 which is edge 2 (see mgraph.h), or #2 (edge 3).
  */
-static const int oee_edgemap[V6]=
-  { 2<<ESHIFT, 1<<ESHIFT, 3<<ESHIFT, 2<<ESHIFT, 4<<ESHIFT, 5<<ESHIFT };
+static const int oei_edge_delta[V6]=
+  /*    0       1       2       3       4       5      initial e
+   *   #3      #1      #0      #2      #4      #5      initial ix
+   *   #4      #2      #1      #3      #5      #6      next ix
+   *    4       3       1       0       5      V6      next e
+   */ {
+     4<<ESHIFT, 2<<ESHIFT, -1<<ESHIFT,
+                              -3<<ESHIFT, 1<<ESHIFT, (V6-5)<<ESHIFT
+};
 
 class OutEdgeIterator :
   public iterator_facade<
@@ -73,18 +82,24 @@ class OutEdgeIterator :
     int const,
     forward_traversal_tag
 > {
-  int v, ix, f;
-  
-  void setf() { f= v | oee_edgemap[ix]; }
+  int f;
  public:
-  void increment() { ix++; setf(); }
-  bool equal(OutEdgeIterator const& other) const { return ix == other.ix; }
+  void increment() {
+    //printf("incrementing f=%03x..",f);
+    f += oei_edge_delta[f>>ESHIFT];
+    //printf("%03x\n",f);
+  }
+  bool equal(OutEdgeIterator const& other) const { return f == other.f; }
   int const& dereference() const { return f; }
   OutEdgeIterator() { }
-  OutEdgeIterator(int _v, int _ix) : v(_v), ix(_ix) { setf(); }
+  OutEdgeIterator(int _f) : f(_f) { }
+  OutEdgeIterator(int v, int e) : f(e<<ESHIFT | v) {
+    //printf("constructed v=%02x e=%x f=%03x\n",v,e,f);
+  }
 
-  static int voe_min(int _v) { return _v & YMASK ? 0 : 2; }
-  static int voe_max(int _v) { return ~_v & YMASK ? V6 : 4; }
+  static int voe_min(int _v) { return (_v & YMASK) ? 2 : 3; }
+  static int voe_max(int _v) { return (~_v & YMASK) ? V6 : 4; }
+  static int voe_degree(int _v) { return (_v & YMASK | ~_v & YMASK) ? 4 : V6; }
 };
  
 typedef counting_iterator<int> VertexIterator;
@@ -122,14 +137,18 @@ namespace boost {
 
   // Concept IncidenceGraph:
   inline int source(int f, const Graph&) { return f&VMASK; }
-  inline int target(int f, const Graph&) { return EDGE_END2(f&VMASK, f>>ESHIFT); }
+  inline int target(int f, const Graph&) {
+    int v2= EDGE_END2(f&VMASK, f>>ESHIFT);
+    //printf("traversed %03x..%02x\n",f,v2);
+    return v2;
+  }
   inline std::pair<OutEdgeIterator,OutEdgeIterator>
   out_edges(int v, const Graph&) {
     return std::make_pair(OutEdgeIterator(v, OutEdgeIterator::voe_min(v)),
                          OutEdgeIterator(v, OutEdgeIterator::voe_max(v)));
   }
   inline unsigned out_degree(int v, const Graph&) {
-    return OutEdgeIterator::voe_max(v) - OutEdgeIterator::voe_min(v);
+    return OutEdgeIterator::voe_degree(v);
   }
 
   // Concept VertexListGraph:
@@ -184,9 +203,13 @@ double graph_layout_cost(const Vertices v, const double vertex_areas[N]) {
     FOR_VERTEX(v2) {
       double a2= vertex_areas[v2];
       double d2= hypotD2plus(v[v1],v[v2], d2_epsilon);
-      double sd= vertex_distances[v2] / d2;
+      double s= vertex_distances[v2];
+      double sd= s / d2;
       double sd2= sd*sd;
-      total_cost += a1*a2 * (sd2 - 1) / (d2*d2);
+      double cost_contrib= a1*a2 * (sd2 - 1) / (d2*d2);
+      //printf("layout %03x..%03x (a=%g,%g) s=%g d2=%g cost+=%g\n",
+      //            v1,v2, a1,a2, s,d2, cost_contrib);
+      total_cost += cost_contrib;
     }
   }
   return total_cost;