chiark / gitweb /
nice graph colourings
[trains.git] / layout / redactgraph.c
index 70953b41215053bdc5ea3ffd7d7cf2e2e0374c9e..3b55b54807d976a59e77503245f55f0c1092c7a4 100644 (file)
@@ -1,4 +1,30 @@
 /*
+ * output format from printforforsafety is lines like this:
+ *   segment <node>.<side> <node>.<side> <segment>[/<movinfo>] <distance>
+ *   #...         (comment)
+ *                (blank)
+ *   end          (at end of file, mandatory)
+ * syntactic restrictions:
+ *   <node> is alphanumeric and starts with a letter
+ *   <side> is precisely the digit 0 or the digit 1
+ *   <segment> is alphanumeric (and comes from the layout)
+ *   <movinfo> is  <movfeat><movpos>[<movfeat><movpos>...]*<combpos>
+ *            where <movfeat> is alphabetic and <movpos> is and
+ *            <combpos> are in decimal and have no unnecessary leading 0's
+ *   <distance> contains only digits and perhaps decimal point
+ * semantic restrictions (not enforced by redactgraph but ought to
+ * be impossible for layout designer to violate):
+ *   Each <segment>[/<movinfo>] appears exactly once.
+ *   <segment> appears either once without /<movinfo> or always with.
+ *   Each side of each node appears either
+ *    - once as the 0th or 1th end of a <segment>
+ *    - one or more times as the same end of <segment>/<movinfo>'s with
+ *      the same <segment>
+ *    - not at all (indicates a terminus)
+ * Refer to safety.h for info regarding <combpos>.
+ *
+ * See layout-data.h comment for info regarding layout data for
+ * control system.
  */
 /* for debugging, runes like
  *  ./ours.redactgraph consistency movfeatsplitedges consistency movfeatrmstubs consistency movfeatsplitnodes consistency trivpairnodes consistency trivnullnodes consistency printforneato | neato -Tps >u.ps
@@ -244,7 +270,7 @@ static void node_surely_orphaned(Node *node) {
   LIST_UNLINK(all_nodes, node);
 }
 
-/*---------- operations ----------*/
+/*---------- operations - heavyweight graph redaction ----------*/
 
   /* We combine the individual moveable features in each segment into
    * a single `feature' with the cross-product of all of the
@@ -429,7 +455,7 @@ static int movfeat_isinnernode(Node *node) {
     trace(" not inner: no edges?!\n");
     return 0;
   }
-  trace(" inner %s ",segment->segname);
+  trace(" inner %s ",all_segment->segname);
   return 1;
 }
 
@@ -504,6 +530,8 @@ static void movfeatsplitnodes(void) {
   }
 }
 
+/*---------- operations - trivial graph redaction etc. ----------*/
+
 static void trivpairnodes(void) {
   /* Eliminate near-trivial nodes - in this case, ones which have only
    * two edges, which come in on opposite sides, have the same
@@ -621,6 +649,8 @@ static void consistency(void) {
   check("command-line request");
 }
 
+/*---------- operations - output ----------*/
+
 static void printforneato(void) {
   Node *node;
   EdgeEnd *edgeend;
@@ -656,6 +686,155 @@ static void printforneato(void) {
   output("}\n");
 }
 
+static void printforforsafety(void) {
+  Node *node;
+  Edge *edge;
+  EdgeEnd *edgeend;
+  int side, end, weight, i;
+  Segment *segment;
+  MovFeat *movfeat;
+  
+  FOR_ALL_NODES(node) {
+    FOR_EDGES(edge, node,side,edgeend) {
+      output("segment ");
+      FOR_BOTH(end) {
+       output("n%s.%d ",
+              edge->ends[end].node->node->pname,
+              edge->ends[end].node->side);
+      }
+      segment= edge->subseg->segment;
+      output(segment->segname);
+      if (segment->n_movfeats>1) {
+       if (!segment->starfeature) {
+         fprintf(stderr,"printforforsafety before movfeatsplitedges!\n");
+         exit(8);
+       }
+       output("/");
+       weight= 1;
+       FOR_SEGMENT_MOVFEATS(i,movfeat, segment) {
+         if (!movfeat->movfeat) continue;
+         output("%s%d", movfeat->movfeat,
+                (edge->movpos / weight) % movfeat->n_positions);
+         weight *= movfeat->n_positions;
+       }
+       output("*%d", edge->movpos);
+      }
+      output(" %f\n", edge->distance);
+    }
+  }
+  output("end\n");
+}
+
+static void printforlayoutsegjoins(void) {
+  Node *node;
+  EdgeEnd *edgeend, *edgeend2;
+  Segment **segmentp, *segment, *segment2;
+  int side, side2, order, max_order, colour, any_left;
+  
+  /* we format segment->u as follows: */
+#define LSJ_U_VERTEXORDER_MASK 0x000000ff
+#define LSJ_U_COLOUR_SHIFT                8
+#define LSJ_U_COLOUR_BITS                 16
+#define LSJ_U_COLOUR_UNIT                 (1<<LSJ_U_COLOUR_SHIFT)
+#define LSJ_U_COLOUR_MASK      0x00ffff00
+#define LSJ_U_SEGEND_DONE      0x01000000
+#define LSJ_U_COLOUR_CLASH     0x02000000
+
+  max_order= 0;
+  FOR_ALL_SEGMENTS(segmentp,segment)
+    segment->u= 0;
+  
+  FOR_ALL_NODES(node) {
+    output("layer ");
+    FOR_BOTH(side)
+      if (!node->sides[side].head) {
+       output("segterminus");
+       goto one_sided;
+      }
+    output("segjoin");
+  one_sided:
+    output("%d\n", (node->layermin + node->layermax)/2);
+
+    output("abs segjoin_%s",node->pname);
+    FOR_NODE_EDGEENDS(side,edgeend, node)
+      output("_%s",edgeend->edge->subseg->segment->segname);
+    output(" %f %f %f\n", node->x,node->y,node->a);
+    FOR_NODE_EDGEENDS(side,edgeend, node) {
+      segment= edgeend->edge->subseg->segment;
+      if (segment->u & LSJ_U_SEGEND_DONE) continue;
+      segment->u |= LSJ_U_SEGEND_DONE;
+      assert(~segment->u & LSJ_U_VERTEXORDER_MASK); /* detect overflow */
+      segment->u++;
+      order= segment->u & LSJ_U_VERTEXORDER_MASK;
+      if (order > max_order) max_order= order;
+      output("segend %s\n",segment->segname);
+    }
+    output("\n");
+    FOR_ALL_SEGMENTS(segmentp,segment)
+      segment->u &= ~LSJ_U_SEGEND_DONE;
+  }
+
+  /* Welsh and Powell's graph colouring algorithm
+   * (Wikipedia Graph_coloring) */
+  output("# max-order %d\n",max_order);
+
+  for (colour=LSJ_U_COLOUR_UNIT, any_left=1;
+       any_left;
+       colour+=LSJ_U_COLOUR_UNIT) {
+    output("# colour %x\n", colour);
+    assert(!(colour & ~LSJ_U_COLOUR_MASK));
+   something_done:
+    any_left= 0;
+
+    FOR_ALL_SEGMENTS(segmentp,segment)
+      segment->u &= ~LSJ_U_COLOUR_CLASH;
+
+    FOR_ALL_NODES(node)
+      FOR_NODE_EDGEENDS(side,edgeend, node) {
+        segment= edgeend->edge->subseg->segment;
+       if (colour == (segment->u & LSJ_U_COLOUR_MASK)) {
+         output("#  already %s\n", segment->segname);
+         FOR_NODE_EDGEENDS(side2,edgeend2, node) {
+           segment2= edgeend2->edge->subseg->segment;
+           output("#   therefore-not %s\n", segment2->segname);
+           segment2->u |= LSJ_U_COLOUR_CLASH;
+         }
+       }
+      }
+
+    for (order=max_order; order>0; order--) { /* idiot-sort */
+      output("#  order %d\n", order);
+      FOR_ALL_SEGMENTS(segmentp,segment)
+       if ((segment->u & LSJ_U_VERTEXORDER_MASK) == order) {
+         if (segment->u & LSJ_U_COLOUR_MASK) continue;
+         any_left= 1;
+         if (segment->u & LSJ_U_COLOUR_CLASH) {
+           output("#   still-clashes %s\n", segment->segname);
+           continue;
+         }
+         segment->u |= colour;
+         output("#   coloured %s\n", segment->segname);
+         goto something_done;
+       } else {
+         output("# (skip %s %x)\n", segment->segname,segment->u);
+       }
+    }
+  }
+
+  FOR_ALL_SEGMENTS(segmentp,segment) {
+    int tcolour= segment->u & LSJ_U_COLOUR_MASK;
+    const double val1=0.75, val2=0.50;
+    double value;
+    if (!tcolour) continue;
+    value= val1 +
+            (tcolour - LSJ_U_COLOUR_UNIT*2) * (val1-val2) /
+            (colour - LSJ_U_COLOUR_UNIT*2);
+    output("segcmap %s  %f setgray\n", segment->segname,value);
+  }
+}
+
+/*---------- main program ----------*/
+
 typedef struct {
   const char *name;
   void (*fn)(void);
@@ -664,6 +843,8 @@ typedef struct {
 static const OpInfo opinfos[]= {
 #define OI(x) { #x, x },
   OI(printforneato)
+  OI(printforforsafety)
+  OI(printforlayoutsegjoins)
   OI(consistency)
   OI(movfeatsplitedges)
   OI(movfeatrmstubs)