#!/usr/bin/perl
# ndt2wrl.pl
# Andrew Daviel <advax@triumf.ca> Jan 1997
# create VRML 1.0 from NDT data and Anemone "nodebase" file
# Usage: anemone -startup nodebase.xxx -ndt <xxx.ndt  (save startup)
#        ndt2wrl.pl nodebase.xxx < xxx.ndt > xxx.wrl

$all = 1 ; # plot all nodes from nodebase ( even if not in NDT data)
$date = `date` ; chop($date) ;

open (IN,@ARGV[0]) || die "Can't open nodebase file \"@ARGV[0]\"" ;
#node_hint portal-rtr.hc.BC.net 200.000000 50.000000 oval 75.000000 50.000000
while (<IN>){ 
  chop ;
  ($hint,$node,$x,$y,$shape,$width,$height) = split (/ /) ;
  $node_x{$node} = sprintf("%7.1f",$x) ;
  $node_y{$node} = sprintf("%7.1f",$y) ;
  $node_width{$node} = $width ;
  $node_height{node} = $height ;
}
close(IN) ;
#( "srchost=arnet.gw.ualberta.ca","acked=1.0000","srcip=198.73.177.4","color=1.000000 0.300000 0.200000","samples=10","variation=12.1514","timestamp=853188143","dsthost=meb.gw.ualberta.ca","dstip=129.128.1.8")
while (<STDIN>) {
  chop ;
  s/^\(\s*"// ; s/"\s*\)$// ;  s/"\s*,\s*"/","/g ;
  @f = split(/","/) ;
  undef(%name) ;
  foreach $_ (@f) {
    ($name,$value)  = split(/=/) ;
    $name{$name} = $value ;
  }
  $src = $name{'srchost'} ; if (!$src) { $src = $name{'srcip'} ; } 
  $dst = $name{'dsthost'} ; if (!$dst) { $dst = $name{'dstip'} ; }
  $color = $name{'color'} ; $color =~ tr/ //s ; 
  $srchost{$src} = 1 ; 
  $var{$dst} = $name{'variation'} ; $delay{$dst} = $name{'delay'} ;
  $acked{$dst} = $name{'acked'} ;
  $dsthost{$dst} = 1 ; $color{$dst} = $color ;
  push(@edge,join(',',$src,$dst,$color)) ;
}

$height = 50 ; # default height for all objects
$base = 0 ; # default base (z-coordinate) for all objects
$base2 = $base + $height/2 ; $base2 = sprintf("%7.1f",$base2) ;
$base3 = $base2 + 22 ;

$anchor = 1 ; # default is not to generate anchor

#
# print standard header for standalone file. May be removed for 
# building more complex world.
#
print <<EOH;
#VRML V1.0 ascii

Separator {
  DEF Title Info {
    string "Network Visualization from TRIUMF for $date"
  }
  Separator {
    WWWAnchor {
    name "http://sitka.triumf.ca/net/about-wrl.html"
    Material { diffuseColor 1 0 0 }
    Translation { translation   -500 -600 0 }
    FontStyle { family SANS size 100 }
     AsciiText { string "From TRIUMF $date" }
     Translation { translation 0 -150 0 }
      LOD { range [ 900 ] # center   -500 -400 0
        Separator {
          Translation { translation  480 20 0 } # centre of zoom box
          Translation { translation -45 30 0 } # top left
          Separator {
            Coordinate3 { point [ 0 0 0, 90 0 0, 90 -60 0, 0 -60 0 ] }
            IndexedLineSet { coordIndex [ 0,1,2,3,0,-1 ] }
          }
          FontStyle { family SANS size 3 }
          Translation { translation  15 -15 0 }
          AsciiText { string "Network view from TRIUMF" }
          Translation { translation   0 -5 0 }
          AsciiText { string "for $date" }
          Translation { translation   0 -5 0 }
          AsciiText { string "Height represents percentage of packets acked"
}
          Translation { translation   0 -5 0 }
          AsciiText { string "Colour depends on variation in delay" }
          Translation { translation   0 -5 0 }
          AsciiText { string "Zoom on a node to reveal name"}
          Translation { translation   0 -5 0 }
          AsciiText { string "Ctrl-Click for information (HTML)"}
        }
        Separator {
          FontStyle { family SANS style BOLD size 150 }
          AsciiText { string "zoom     README"}
          # Zoom Box located between "zoom" and "README"
          Material { diffuseColor 0 0 1 }
          Translation { translation  480 20 0 }
          Cube { width 90 depth 00  height 60 }
        }
      }
    } }
  }
  FontStyle {
    family SANS
    #style BOLD
    size 10
  }
EOH
while (($name,$node_x) = each(%node_x)) {
  if (!$all && !$srchost{$name} && !$dsthost{$name}) { next ; }
  $x1 = $node_x ; $y1 = -$node_y{$name} ;
  $radius = sprintf("%7.1f",$node_width{$name}/2) ; $nradius = -$radius ;
  if ($anchor) {
    $lname = $name ; $lname =~ tr/A-Z/a-z/ ; 
    print "    WWWAnchor {\n      " ;
    #print "name \"http://sitka.triumf.ca/cgi-bin/wrap-plot-day-pkt2-smooth/$lname\"\n" ;
    print "name \"http://sitka.triumf.ca/cgi-bin/node-info?$lname\"\n" ;
  }
#    $height = 100/$delay{$name} ; 
  $vheight = $height * $acked{$name} ; 
  $vheight = sprintf("%7.1f",$vheight) ;
  $nodecolor= $color{$name} ? $color{$name} : "0 0 0" ; # use color else 1 1 1
  print <<EOT ;
    Separator {
      # $name
      Translation { translation $x1 $y1 $base2 }
      LOD { range [ 500 ] # center $x1 $y1 $base2
        Separator {
          Translation { translation $nradius 0 $base3 }
          Material { diffuseColor 1 1 1 }
          AsciiText { string"$name" }
        }
        Separator {}
      }
      Material { diffuseColor $nodecolor }
      Rotation { rotation 1 0 0 -1.5707 }
#      Cylinder { height $vheight radius $radius }
      Cube { width $radius depth $radius height $vheight }
    }
EOT
   if ($anchor) {print "    }\n";}
}
foreach $_ (@edge) {
  ($src,$dst,$color) = split(/,/) ;
  if (!$color) { $color = "0 0 0" ; }
  $y1 = 0 -$node_y{$src} ; $y2 = 0 -$node_y{$dst} ;
  $x1 = $node_x{$src} + 0 ; $x2 = $node_x{$dst} + 0 ;
  print <<EOT ;
  Separator {
    # $src - $dst
    Material { diffuseColor $color }
    Coordinate3 { point [ $x1 $y1 $base2, $x2 $y2 $base2 ] }
    IndexedLineSet { coordIndex [ 0,1,-1 ] }
  }
EOT
}  

# clean up
#
print "}\n";
