#!/usr/local/bin/perl
#
#
# copyright July 23, 1999, Bill Walker W5GFE, bw@cs.ecok.edu
# This software is available under the terms of the GNU Public License
############################################
require 5.002;
use Tk;
use strict;
############################################


# Initialze Tk 
my $MW = MainWindow->new();

my $maxwidth = 400; # pixels
my $maxaspect = $maxwidth * 7 / 8;
my $centerx = $maxwidth/2;
my $centery = $maxwidth/2;

my (@colors) = ( 'green',  'cyan',  
    'PeachPuff3',  'RosyBrown1', 'IndianRed1',
     'PaleVioletRed1', 'MediumOrchid1','pink','blue');

my $colorindex = 0;
my $usecolor;


#########################


# set up the big frames

my $topframe = $MW->Frame()->pack();
my $bottomframe= $MW->Frame()->pack();




my $xygraph = $topframe->Scrolled(qw/Canvas -relief sunken -borderwidth 2
		       -scrollbars se -background yellow/);


$xygraph->pack(-side =>'left');


my $xzgraph = $topframe->Scrolled(qw/Canvas -relief sunken -borderwidth 2
		       -scrollbars se -background yellow/);

$xzgraph->pack(-side => 'left');


my $yzgraph = $bottomframe->Scrolled(qw/Canvas -relief sunken -borderwidth 2
		       -scrollbars se -background yellow/);

$yzgraph->pack(-side=>'left');

my $filebox = $bottomframe->Scrolled('Listbox',
				      -width => '30',
				     -height=>'10',
				       -setgrid=> 1,
				       -scrollbars=>'e') ->pack (-side=>'left');

$filebox->configure(-background=>'pink');



# Create 'quit' button 
my $quitter = $bottomframe->Button(-text => 'Quit',
                        -command => sub 
                          {
                           $MW->destroy;
                          })->pack(-side=>'left');


###################

## graph the lines

sub graphline{
  my $line = $_[0];
  $line =~ s/^[ \t][ \t]*//g;
  $line =~ s/[ \t][ \t]*/|/g;
  my ($GW,$tag,$segments,$x0,$y0,$z0,$x1,$y1,$z1,$radius) = split('\|',$line);
#  printf("(%s,%s,%s) to (%s,%s,%s)\n",$x0,$y0,$z0,$x1,$y1,$z1);
}


####################
###########3 some subprograms


sub maxmin {
  # returns ($max, $min) of a vector

  my $max = $_[0];
  my $min = $_[0];

  my $i = 0;
  while ($i < @_ ) {
    $max = ($max < $_[$i] ? $_[$i] : $max);
    $min = ($min > $_[$i] ? $_[$i] : $min);
    $i = $i + 1;
  }

  return ($max,$min);
  
}



##########################3
## translate coordinates to new origin, and flip the y axis
## if one of these lengths is 0, we add a fudge factor to make
## a "blob" of color

sub translate {
  # takes a set of (x0,y0,x1,y1) and does the translation and flip
  my (@C) = @_;

  if ((($C[0] - $C[2]) == 0) && (($C[1] - $C[3] ) == 0  )) { # it is a point
    $C[2] = $C[2] + 3;
    $C[3] = $C[3] + 3;
  }

  $C[0] = $C[0] + $centerx;
  $C[1] = $centery - $C[1];
  $C[2] = $C[2] + $centerx;
  $C[3] = $centery - $C[3];
  
  return @C;
}

################# some global variables


#  bind to the filebox a selection that will read the contents of a file
# a subroutine to open a file and read the data

sub readdata {
  my $filename = $_[0];
  my @X0;
  my @X1;
  my @Y0;
  my @Y1;
  my @Z0;
  my @Z1;
  my @RADIUS;

  

  my $GW;
  my $tag;
  my $segments;
  
  my $line;
  my $counter = 0;

  open (FH,"<" . $filename);
  while ($line = <FH> ) {
    chomp($line);
    if ($line =~ /^GW/ ) {
      # next two for somnec input
      $line =~ s/^GW/GW /;
      $line =~ s/,/ /g;
      
      $line =~ s/^[ \t][ \t]*//g;
      $line =~ s/[ \t][ \t]*/|/g;
      ($GW,$tag,$segments,$X0[$counter],$Y0[$counter],$Z0[$counter],
       $X1[$counter],$Y1[$counter],$Z1[$counter],$RADIUS[$counter]) = split('\|',$line);
      $counter++;
    }
  }
  close(FH);

  

  my ($MAXSCALE,$MINSCALE) = maxmin (maxmin(@X0),maxmin(@Y0),maxmin(@Z0),
				     maxmin(@X1),maxmin(@Y1),maxmin(@Z1));

  my $SCALE = $maxaspect/($MAXSCALE - $MINSCALE);

  my $i = 0;
  while ($i < $counter) {
    #scale it and put it on the graphs
    $colorindex = ($colorindex + 1) % (@colors);
    $usecolor = $colors[$colorindex];
    $xygraph->createLine(translate ($SCALE * $X0[$i],
			 $SCALE *$Y0[$i],
			 $SCALE *$X1[$i],
			 $SCALE *$Y1[$i]),
			 -tags=>"line",-fill=>$usecolor, -width=>'3');

    $yzgraph->createLine(translate($SCALE*$Y0[$i],
			 $SCALE*$Z0[$i],
			 $SCALE*$Y1[$i],
			 $SCALE*$Z1[$i]),
			 -tags=>"line",-fill=>$usecolor,-width=>'3');

    $xzgraph->createLine(translate($SCALE*$X0[$i],
			 $SCALE*$Z0[$i],
			 $SCALE*$X1[$i],
			 $SCALE*$Z1[$i]),
			 -tags=>"line",-fill=>$usecolor,-width=>'3');
    $i++;
  }

}

###########################################


$filebox->bind ('<Button-1>', [ sub {
				  $xygraph->delete('all');
				  $yzgraph->delete('all');
				  $xzgraph->delete('all');
				  # put origins on the graphs
				  $xygraph->createText($centerx,$centery,-text=>'0');
				  $yzgraph->createText($centerx,$centery,-text=>'0');
				  $xzgraph->createText($centerx,$centery,-text=>'0');
				  readdata($filebox->get('active'));
				  $xygraph->configure(-scrollregion => [$xygraph->bbox('all')]);
				  $yzgraph->configure(-scrollregion => [$yzgraph->bbox('all')]);
				  $xzgraph->configure(-scrollregion => [$xzgraph->bbox('all')]);
				}]);


########################################

## populate the file box

my $line;

open (FILEBOX,"ls |") || die "can't read this directory";

while ($line = <FILEBOX> ) {
  chomp($line);
  $filebox->insert('end',$line);
}

close (FILEBOX);

# open the right file

MainLoop();









