chiark / gitweb /
extractgraph wip - currently written node comparer and librarian, but no edge librari...
[trains.git] / layout / extractgraph
1 #!/usr/bin/perl
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 $conf{MinClearLayer}= 6;
47 $conf{MaxTolerLayer}= 4;
48 $conf{MinClearDist}= 2.0;
49 $conf{MaxTolerDist}= 0.2;
50 $conf{MinClearAngle}= 5.0;
51 $conf{MaxTolerAngle}= 0.5;
52 $conf{LayerKinds}= ''; # comma-separated list
53
54 our @nodes;
55 # $nodes[$nodenum]{X}
56 # $nodes[$nodenum]{Y}
57 # $nodes[$nodenum]{A}
58 # $nodes[$nodenum]{LayerMin}
59 # $nodes[$nodenum]{LayerMax}
60
61 sub find_node (@) {
62     my ($l,$x,$y,$a) = @_;
63     my ($any_outside_tol);
64     for ($ni=0; $ni<@nodes; $ni++) {
65         $node= $nodes[$ni];
66         $diff{Layer}= (($d = $l - $node->{LayerMin}) < 0 ? $d :
67                        ($d = $l - $node->{LayerMax}) > 0 ? $d :
68                        0);
69         $diff{Dist}= sqrt(sqr($x - $node->{X}) +
70                           sqr($y - $node->{Y}));
71         $diff{Angle}= $a - $node->{A};                    # <-360,360>
72         if ($diff{Angle} < 0) { $diff{Angle} += 360; }    # [0,360>
73         $back= $diff{Angle} >= 90 && $diff{Angle} < 270;  # $back <=> [90,270>
74         if ($back) { $diff{Angle} -= 180; }               # [0,90> or [270,360>
75         if ($diff{Angle} > 180) { $diff{Angle} -= 360; }  # [-90,90>
76         $any_outside_clear= 0;
77         $any_outside_toler= 0;
78         foreach $k (keys %diff) {
79             if (abs($diff{$k}) >= $conf{"MinClear$k"}) {
80                 $any_outside_clear=1; last;
81             } elsif (abs($diff{$k}) <= $conf{"MaxToler$k"}) {
82             } else {
83                 $any_outside_toler=1;
84             }
85         }
86         if ($any_outside_clear) {
87         } else if ($any_outside_toler) {
88             die "$l,$x,$y,$a vs. $node->{LayerMin}..$node->{LayerMax}".
89                 ",$node->{X},$node->{Y},$node->{A}";
90         } else {
91             if ($diff{Layer} < 0) { $node->{LayerMin}= $l }
92             if ($diff{Layer} > 0) { $node->{LayerMax}= $l }
93             return ($node,$back);
94         }
95     }
96     $node= { X => $x, Y => $y, A => $a,
97              LayerMin => $l, LayerMax => $l };
98     push @nodes, $node;
99     return ($node,0);
100 }
101     
102 while (<>) {
103     next unless m/^\%L /;
104     die unless m/^\%L (\w+)\b/;
105     next unless $1 eq 'segmentpart';
106     die unless m/^\%L segmentpart ([A-Za-z_]*)(\d+) (\S+) ([-.eE0-9 ]+)$/;
107     ($layerkind, $level, $subsegspec, $numbers) = ($1,$2,$3,$4);
108     next unless grep { $layerkind eq $_ } split /\,/, $conf{LayerKinds};
109     @numbers = map { $_ + 0 } split / /, $numbers;
110     for ($pti=0; $pti<2; $pti++) {
111         ($node[$pti], $back[$pti])=
112             find_node($level, $numbers[($i*3)..($i*3+2)]);
113     }
114 }
115
116     ($pts[0]{X}, $pts[0]{Y}, $pts[0]{A},
117      $pts[1]{X}, $pts[1]{Y}, $pts[1]{A}) = 
118     $node[0]
119      
120
121 (\w+(?:(?:\/([A-Za-z]+)(\d+))?)?)