6 # $ctx->{CmdLog}= undef } not in defobj
7 # $ctx->{CmdLog}[]= [ command args ] } in defobj
8 # $ctx->{LocsMade}[]= $id
12 # $ctx->{Trans}{X} # transformation. is ev representing
13 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
14 # $ctx->{Trans}{A} # not at plot-time)
15 # $ctx->{Trans}{R} # but multiply all y coords by this!
16 # $ctx->{Draw}{T} # 1 or '' for drawing track
17 # $ctx->{Draw}{L} # L1 or 1 or '' for labelling or drawing locs
23 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
32 # Operate on Enhanced Vectors which are a location (coordinates) and a
33 # direction at that location. Representation is a hash with members X
34 # Y and A (angle of the direction in radians, anticlockwise from
35 # East). May be absolute, or interpreted as relative, according to
38 # Each function's first argument is a hashref whose X Y A members will
39 # be created or overwritten; this hashref will be returned (so you can
40 # use it `functionally' by passing {}). The other arguments may be ev
41 # hashrefs, or other info. The results are in general undefined if
42 # one of the arguments is the same hash as the result.
45 # ev_byang(R, ANG,[LEN])
46 # result is evec of specified angle and length (default=1.0)
47 my ($res,$ang,$len)=@_;
48 $len=1.0 unless defined $len;
49 $res->{X}= $len * cos($ang);
50 $res->{Y}= $len * sin($ang);
54 sub ev_compose ($$$) {
55 # ev_compose(SUM_R, A,B);
56 # appends B to A, result is end of B'
57 # A may have a member R, which if provided then it should be 1.0 or -1.0,
58 # and B's Y and A will be multiplied by R first (ie, we can reflect);
61 $r= defined $a->{R} ? $a->{R} : 1.0;
62 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
63 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
64 $sum->{A}= $a->{A} + $r * $b->{A};
67 sub ev_decompose ($$$) {
68 # ev_decompose(B_R, A,SUM)
69 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
72 $r= defined $a->{R} ? $a->{R} : 1.0;
73 $brx= $sum->{X} - $a->{X};
74 $bry= $r * ($sum->{Y} - $a->{Y});
75 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
76 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
77 $b->{A}= $r * ($sum->{A} - $a->{A});
80 sub ev_lincomb ($$$$) {
81 # ev_linkcomb(RES,A,B,P)
83 my ($r,$a,$b,$p) = @_;
85 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
90 # returns bearing of B from A (in radians)
91 # A->{A} and B->{A} are ignored
93 return atan2($b->{Y} - $a->{Y},
98 # returns distance from A to B
99 # A->{A} and B->{A} are ignored
102 $xd= $b->{X} - $a->{X};
103 $yd= $b->{Y} - $a->{Y};
104 return sqrt($xd*$xd + $yd*$yd);
108 my ($converter,$defaulter)=@_;
110 return &$defaulter unless @al;
112 $v= &$converter($spec);
113 dv('canf ','$spec',$spec, '$v',$v);
116 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
117 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
120 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
122 %units_len= qw(- mm mm 1 cm 10 m 1000);
123 %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
125 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
126 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
127 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
131 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
132 or die "lexically invalid quantity";
134 $u=$ua->{'-'} unless length $u;
135 defined $ua->{$u} or die "unknown unit $u";
137 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
142 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
147 my ($r,$d,$k,$neg,$na);
148 $neg= $sp =~ s/^\-//;
150 die "unknown $id" unless defined $ctx->{Loc}{$id};
151 $r= $ctx->{Loc}{$id};
153 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
154 printf DEBUG "%s\n", $d;
157 $na -= 2*$pi if $na >= 2*$pi;
158 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
166 die "duplicate $id" if exists $ctx->{Loc}{$id};
167 exists $ctx->{Loc}{$id}{X};
168 push @{ $ctx->{LocsMade} }, $id;
169 return $ctx->{Loc}{$id};
171 sub cva_cmd ($) { return cva_idstr($_[0]); }
174 return $sp if grep { $_ eq $sp } @$el;
175 die "invalid option (permitted: @$el)";
177 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
181 $nl= can(\&cva_idnew);
182 $i->{X}= can(\&cva_len);
183 $i->{Y}= can(\&cva_len);
184 $i->{A}= can(\&cva_ang);
185 ev_compose($nl, $ctx->{Trans}, $i);
188 my ($from,$to,$len,$right,$turn);
189 $from= can(\&cva_idex);
190 $to= can(\&cva_idnew);
191 $len= can(\&cva_len);
192 $right= can(\&cva_len);
193 $turn= cano(\&cva_absang, 0);
194 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
195 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
200 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
204 return 'undef' if !defined $v;
205 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
206 return $v if $v =~ m/^[0-9.]+/;
207 $v =~ s/[\\\']/\\$&/g;
211 sub dv1_kind ($$$$$$$) {
212 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
214 return 0 if $ref ne $ref_exp;
216 foreach $ix (&$ixesfn) {
218 my ($v)= &$ixmapfn($ix);
219 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
220 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
223 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
228 return 0 unless $debug;
229 my ($pfx,$expr,$v) = @_;
231 #print STDERR "dv1 >$pfx|$ref<\n";
233 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
235 } elsif ($ref eq 'SCALAR') {
236 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
239 $expr.='->' unless $expr =~ m/\]$|\}$/;
240 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
241 sub { ($[ .. $#$v) },
242 sub { $v->[$_[0]] });
243 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
244 sub { sort keys %$v },
245 sub { $v->{$_[0]} });
246 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
258 $ptscale= 72/25.4 / 7.0;
265 $psu_allwidth= 37.0/2;
273 $lmu_txtboxtxty= $lmu_marktpt * 0.300;
274 $lmu_txtboxh= $lmu_marktpt * 1.100;
275 $lmu_txtboxpadx= $lmu_marktpt * 0.335;
276 $lmu_txtboxoff= $lmu_marklw / 2;
279 sub o ($) { $o .= $_[0]; }
280 sub ol ($) { $ol .= $_[0]; }
282 sub o_path_begin () {
284 $o_path_verb= 'moveto';
286 sub o_path_point ($) {
288 o(" $pt $o_path_verb\n");
289 $o_path_verb= 'lineto';
291 sub o_path_stroke ($) {
293 o(" $width setlinewidth stroke\n");
297 my ($a,$b,$width)=@_;
301 o_path_stroke($width);
304 sub psu_coords ($$$) {
305 my ($ends,$inunit,$across)=@_;
306 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
307 # $across in mm, +ve to right.
309 $ea_zo{X}=$ea_zo{Y}=0;
310 foreach $zo (qw(0 1)) {
311 $prop= $zo ? $inunit : (1.0 - $inunit);
312 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
313 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
315 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
316 # '\\%ea_zo', \%ea_zo);
317 return $ea_zo{X}." ".$ea_zo{Y};
320 sub parametric_segment ($$$$$) {
321 my ($endstatuses,$p0,$p1,$lenperp,$calcfn) = @_;
322 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
323 # $ends is II, SI, IS, SS (I=actual lineobj end, S=in mid of lineobj)
324 # $lenperp is the length of one unit p, ie the curve
325 # must have a uniform `density' in parameter space
326 # $calcfn is invoked with $p set and should return a loc
327 # (ie, ref to X =>, Y =>, A =>).
328 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick);
329 return unless $ctx->{Draw}{T} =~ m/1/;
330 $ppu= $psu_ulen/$lenperp;
331 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
332 my ($tickend)=($psu_allwidth - $psu_ticklen);
333 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
334 my ($sleeperctr)=($psu_ulen*0.5);
335 my ($sleeperend)=($psu_sleeperlen*0.5);
336 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
337 for ($pa= $p0; $pa<$p1; $pa=$pb) {
339 $p= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
340 $p= $pb; $ends[1]= &$calcfn;
341 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
342 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
343 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
346 o_path_point(psu_coords(\@ends,0,-$psu_allwidth));
347 o_path_point(psu_coords(\@ends,0,$psu_allwidth));
348 o_path_point(psu_coords(\@ends,$e,$psu_allwidth));
349 o_path_point(psu_coords(\@ends,$e,-$psu_allwidth));
350 o(" closepath clip\n");
351 foreach $side qw(-1 1) {
352 o_line(psu_coords(\@ends,0,$side*$psu_allwidth),
353 psu_coords(\@ends,1.5,$side*$psu_allwidth),
355 o_line(psu_coords(\@ends,0,$side*$railctr),
356 psu_coords(\@ends,1.5,$side*$railctr),
358 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
359 o_line(psu_coords(\@ends,$tick,$side*$psu_allwidth),
360 psu_coords(\@ends,$tick,$side*$tickend),
364 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
365 psu_coords(\@ends,$sleeperctr,+$sleeperend),
372 my ($to, $endstatuses, $ctr,$from,$fromsense, $radius,$delta) = @_;
373 # does parametric_segment to draw an arc centred on $ctr
374 # from $from with radius $radius (this must be consistent!)
375 # and subtending an angle $delta.
376 # sets $to->... to be the other end, and returns $to
377 # $fromsense is 1 or -1, and affects only the interpretation
378 # of $from->{A} (not the result).
379 my ($beta, $fromadj);
380 $fromadj= (1.0 - $fromsense) * $pi;
381 $to->{A}= $beta= $from->{A} + $fromadj + $delta;
382 $to->{X}= $ctr->{X} - $radius * sin($beta);
383 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
384 parametric_segment($endstatuses, 0.0,1.0, abs($radius*$delta), sub {
385 my ($beta) = $from->{A} + $delta * $p;
386 return { X => $ctr->{X} - $radius * sin($beta),
387 Y => $ctr->{Y} + $radius * cos($beta),
394 $from= can(\&cva_idex);
395 $to= can(\&cva_idex);
396 $how= can(cvam_enum(qw(arcs arcsm)));
397 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d2, $r,$cfrom);
398 $sigma= v_bearing($from,$to);
399 $distfact= v_dist($from,$to);
400 $theta= $from - $sigma;
401 $phi= $to + 2 * $pi - $sigma;
402 $a= 2 * (1 + sin($theta - $phi));
403 $b= 2 * (sin($theta) - sin($phi));
405 die "too close" if $a<1.1e-10;
406 $d2= $b*$b - 4*$a*$c;
407 $pm = $how =~ /m$/ ? -1 : +1;
408 $r= (-$b + $pm*sqrt($d2))/$a;
410 $cfrom= ev_compose({}, $from, { Y => $rf, X => 0, A => 0 });
411 $cto= ev_compose({}, $to, { Y => -$rf, X => 0, A => 0 });
412 $cbearing= v_bearing($cfrom,$cto);
413 arc({}, IS, $cfrom,$from,1.0, $rf, $cbearing - $cfrom->{A});
414 arc({}, IS, $cto, $to, -1.0, -$rf, $cbearing - $cfrom->{A} + 2*$pi);
418 my ($from,$to,$radius,$ctr,$beta,$ang,$how,$sign_r);
419 $from= can(\&cva_idex);
420 $to= can(\&cva_idnew);
421 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
422 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
423 if ($how eq 'len') { $len= can(\&cva_len); }
424 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
425 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
426 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
427 if ($radius eq 'Inf') {
428 print DEBUG "extend inf $len\n";
429 if ($how eq 'ang') { die "len of straight spec by angle"; }
430 if ($how eq 'upto') {
431 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
432 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
434 printf DEBUG "len $len\n";
435 $to->{X}= $from->{X} + $len * cos($from->{A});
436 $to->{Y}= $from->{Y} + $len * sin($from->{A});
437 $to->{A}= $from->{A};
438 parametric_segment(II, 0.0, 1.0, abs($len), sub {
439 ev_lincomb({}, $from, $to, $p);
442 print DEBUG "radius >$radius<\n";
443 $radius *= $ctx->{Trans}{R};
444 $sign_r= signum($radius);
446 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
447 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
448 if ($how eq 'upto') {
449 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
450 $sign_r * ($upto->{Y} - $ctr->{Y}));
452 } elsif ($how eq 'parallel') {
455 } elsif ($how eq 'uptoang') {
456 $beta= input_absang($ang);
458 } elsif ($how eq 'len') {
459 $sign_ang= signum($len);
460 $beta= $from->{A} - $sign_r * $len / abs($radius);
463 $sign_ang= signum($ang);
464 $beta= $from->{A} - $sign_r * $ang;
467 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
468 $beta += $sign_ang * $sign_r * 4.0 * $pi;
470 $delta= $beta - $from->{A};
471 last if $sign_ang * $sign_r * $delta <= 0;
472 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
474 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
475 arc($to, II, ,$ctr,$from,1.0, $radius,$delta);
477 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
481 return $_[0] * 180 / $pi;
483 sub input_absang ($) {
484 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
486 sub input_abscoords ($$) {
488 ($in->{X}, $in->{Y}) = @_;
490 $out= ev_compose({}, $ctx->{Trans}, $in);
491 return ($out->{X}, $out->{Y});
496 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
498 Draw => { T => 1, L => L1 }
504 $id= can(\&cva_idstr);
505 die "nested defobj" if $defobj_save;
506 die "repeated defobj" if exists $objs{$id};
510 $ctx->{InDefObj}= $id;
511 $ctx->{Draw}= { T => '', L => '' }
516 $id= $ctx->{InDefObj};
517 die "unmatched enddefobj" unless defined $id;
518 foreach $bit (qw(CmdLog Loc)) {
519 $objs{$id}{$bit}= $ctx->{$bit};
525 sub cmd_obj { cmd__obj(1); }
526 sub cmd_objflip { cmd__obj(-1); }
529 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
531 $obj_id= can(\&cva_idstr);
532 $actual= can(\&cva_idex);
533 $formal_id= can(\&cva_idstr);
534 $obj= $objs{$obj_id};
535 dv("cmd__obj ",'$obj',$obj);
536 die "unknown obj $obj_id" unless $obj;
537 $formal= $obj->{Loc}{$formal_id};
538 die "unknown formal $formal_id" unless $formal;
541 $ctx->{Trans}{R}= $flipsignum;
542 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$flipsignum;
543 $formcv= ev_compose({}, $ctx->{Trans},$formal);
544 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
545 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
546 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
547 $ctx->{Draw}{L} =~ s/L//;
548 dv("cmd__obj $obj_id ",'$ctx',$ctx);
551 foreach $c (@{ $obj->{CmdLog} }) {
553 next if $al[0] eq 'enddefobj';
557 $pfx= cano(\&cva_idstr,'');
561 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
562 next if $id eq $formal_id;
564 next if exists $ctx_save->{Loc}{$newid};
565 $newpt= cva_idnew($newid);
566 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
573 dv("cmd__do $ctx @al ",'$ctx',$ctx);
574 $cmd= can(\&cva_cmd);
575 my ($id,$loc,$io,$ad);
576 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
577 o("%L cmd $io $cmd @al\n");
578 $ctx->{LocsMade}= [ ];
580 die "too many args" if @al;
581 foreach $id (@{ $ctx->{LocsMade} }) {
582 $loc= $ctx->{Loc}{$id};
583 $ad= ang2deg($loc->{A});
584 ol("%L point $io$id $loc->{X} $loc->{Y} $ad\n");
585 if (length $ctx->{Draw}{L}) {
587 " $loc->{X} $loc->{Y} translate $ad rotate\n");
588 if ($ctx->{Draw}{L} =~ m/1/) {
589 ol(" 0 $psu_allwidth newpath moveto\n".
590 " 0 -$psu_allwidth lineto\n".
591 " $lmu_marklw setlinewidth stroke\n");
593 if ($ctx->{Draw}{L} =~ m/L/) {
594 ol(" /s ($id) def\n".
596 " /sx5 s stringwidth pop\n".
597 " 0.5 mul $lmu_txtboxpadx add def\n".
598 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
599 " sx5 neg 0 moveto\n".
600 " sx5 neg $lmu_txtboxh lineto\n".
601 " sx5 $lmu_txtboxh lineto\n".
602 " sx5 0 lineto closepath\n".
603 " gsave 1 setgray fill grestore\n".
604 " $lmu_txtboxlw setlinewidth stroke\n".
605 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
619 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
620 " $ptscale $ptscale scale\n"
627 chomp; s/^\s+//; s/\s+$//;
628 @al= split /\s+/, $_;
630 print DEBUG "=== @al\n";
631 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
635 print $o, $ol, " showpage\n"