chiark / gitweb /
C redactgraph going well I think
[trains.git] / layout / extractgraph
1 #!/usr/bin/perl -w
2 #
3 # Reads the special comments in the subsegment encoding output from
4 # layout, determines the segment graph, and outputs a description of
5 # that graph.
6
7 # Approach/algorithm:
8 #
9 #  We read the segenco.ps and each time we find a `%L segmentpart'
10 #  comment we add it to an annotated graph we construct.  Each node in
11 #  the annotated graph is a tuple consisting of a loc and range of
12 #  layer levels.  Each edge is a segment part from a %L comment.  Each
13 #  node has a `front' and a `back', and each edget attach either to
14 #  one or the other.
15 #
16 #  Only segment parts with certain layer kinds are processed: by
17 #  default, only the empty layer kind.
18 #
19 #  When a loc is found in the input, as one end of a segmentpart, it
20 #  is considered identical to a existing node (if its details are
21 #  sufficiently similar) or creates a new node (if its details are
22 #  sufficiently different).  If the segmentpart's end is considered
23 #  identical to an existing node then the existing node's layer level
24 #  range is extended, but the existing node's X Y and A are not
25 #  modified.
26 #
27 #  A loc and layer level are compared with a node as follows:
28 #
29 #    The difference between each of the loc's details and the node's
30 #    details is computed.  If any of the differences is at least the
31 #    min clearance, then the loc/layerb is a new node.  Otherwise, all
32 #    of the differences must be within the max tolerance and the
33 #    loc/layer is the same as the node (coming out of the back if the
34 #    180deg was added to make the angle difference).  Otherwise it is
35 #    an error.
36 #
37 #    The detail differences are:
38 #       Position difference: horizontal distance between loc and node
39 #       Angle difference: difference betwen loc's and node's A, or
40 #        difference minus 180deg between loc's and node's A, whichever
41 #        is the smaller (both reduced mod 360deg to value with 
42 #        smallest magnitude).
43 #       Level difference: 0 if layer level is within node's range
44 #        or distance by which it is outside that range.
45
46 use strict qw(vars);
47
48 our %conf;
49 $conf{MinClearLayer}= 6;
50 $conf{MaxTolerLayer}= 4;
51 $conf{MinClearDist}= 0.5;
52 $conf{MaxTolerDist}= 0.05;
53 $conf{MinClearAngle}= 5.0;
54 $conf{MaxTolerAngle}= 0.5;
55 $conf{LayerKinds}= ','; # comma-separated list as for split /\,/, ..., -1;
56
57 our @layerkinds;
58 @layerkinds= split /\,/, $conf{LayerKinds}, -1;
59
60 our @nodes;
61 # $nodes[]{X}
62 # $nodes[]{Y}
63 # $nodes[]{A}
64 # $nodes[]{LayerMin}
65 # $nodes[]{LayerMax}
66 # $nodes[]{"Edges$back"}[] = [ \$edges[], $far ]
67
68 our @edges;
69 # $edges[]{"Node$far"}= [ \$nodes[], $back ]
70 # $edges[]{Dist}
71 # $edges[]{SubSegSpec}
72
73 sub comment ($) {
74     print "/* $_[0] */\n";
75 }
76
77 sub sqr ($) { return $_[0]*$_[0]; }
78
79 sub find_node (@) {
80     my ($lni,$isdest,$l,$x,$y,$a) = @_;
81     my ($any_outside_toler, $any_outside_clear, $updlayer);
82     my ($ni, $node, %diff, $back, $d, $k);
83     for $node (@nodes) {
84         $diff{Layer}= (($d = $l - $node->{LayerMin}) < 0 ? $d :
85                        ($d = $l - $node->{LayerMax}) > 0 ? $d :
86                        0);
87         $diff{Dist}= sqrt(sqr($x - $node->{X}) +
88                           sqr($y - $node->{Y}));
89         $diff{Angle}= $a - $node->{A};                    # <-360,360>
90         if ($diff{Angle} < 0) { $diff{Angle} += 360; }    # [0,360>
91         $back= $diff{Angle} >= 90 && $diff{Angle} < 270;  # $back <=> [90,270>
92         $back= !!$isdest != !!$back; # logical xor
93         $back += 0;
94         if ($back) { $diff{Angle} -= 180; }               # [0,90> or [270,360>
95         if ($diff{Angle} > 180) { $diff{Angle} -= 360; }  # [-90,90>
96         $any_outside_clear= 0;
97         $any_outside_toler= 0;
98         foreach $k (keys %diff) {
99             if (abs($diff{$k}) >= $conf{"MinClear$k"}) {
100                 $any_outside_clear=1; last;
101             } elsif (abs($diff{$k}) <= $conf{"MaxToler$k"}) {
102             } else {
103                 $any_outside_toler=1;
104             }
105         }
106         if ($any_outside_clear) {
107         } elsif ($any_outside_toler) {
108             die ("mismatch/clash:\n".
109                  " $lni has L=$l XY=$x,$y A=$a\n".
110                  " $node->{LineInfo} has ".
111                  "L=$node->{LayerMin}..$node->{LayerMax}".
112                  " XY=$node->{X},$node->{Y} A=$node->{A}\n ");
113         } else {
114             $updlayer= ($diff{Layer} < 0 ? "Min" :
115                         $diff{Layer} > 0 ? "Max" :
116                         '');
117             if ($updlayer) {
118                 $node->{"Layer$updlayer"}= $l;
119                 $node->{LineInfo}.="($l<-$lni)";
120             }
121             trace("node $lni existing ".pnode($node)."/$back");
122             return ($node,$back);
123         }
124     }
125     $node= { X => $x, Y => $y, A => $a,
126              LayerMin => $l, LayerMax => $l, LineInfo => $lni };
127     $back= 0;
128     push @nodes, $node;
129     trace("node $lni created ".pnode($node)."/$back");
130     return ($node,$back);
131 }
132
133 sub readin () {
134     my ($layerkind, $level, $subsegspec, $numbers, @numbers, $dist);
135     my ($pti,@nodeinfo);
136     while (<>) {
137         next unless m/^\%L /;
138         die unless m/^\%L (\w+)\b/;
139         next unless $1 eq 'segmentpart';
140         die unless m/^\%L segmentpart ([A-Za-z_]*)(\d+) (\S+) ([-.eE0-9 ]+)$/;
141         ($layerkind, $level, $subsegspec, $numbers) = ($1,$2,$3,$4);
142         next unless grep { $layerkind eq $_ } @layerkinds;
143         @numbers = map { $_ + 0 } split / /, $numbers;
144         $dist= shift @numbers;
145         @numbers == 6 or die;
146         $edge= { Dist = $dist, SubSegSpec= $subsegspec };
147         for ($far=0; $far<2; $far++) {
148             @endnums= @numbers[($far*3)..($far*3+2)];
149             ($node,$back)= find_node("$.:$far",$far,$level,@endnums);
150             $edge{"Node$far"}= [ $node, $back ];
151             push @{ $node->{"Edges$back"} }, [ $edge, $far ];
152         }
153         push @edges, $edge;
154     }
155 }
156
157     
158
159
160 readin();
161 splitcontin();
162 elimtrivial();
163
164
165 #    ($pts[0]{X}, $pts[0]{Y}, $pts[0]{A},
166 #     $pts[1]{X}, $pts[1]{Y}, $pts[1]{A}) = 
167 #    $node[0]
168  
169 #(\w+(?:(?:\/([A-Za-z]+)(\d+))?)?)