#!/usr/bin/perl

$flnm = $ARGV[0];

open (SPINE, $flnm);

# first line gives first translation parameters
$line = <SPINE>;
@pcs = split(' ',$line);
$T1[0] = $pcs[0];
$T1[1] = $pcs[1];
$T1[2] = $pcs[2];

$line = <SPINE>;
@pcs = split(' ',$line);
$Section = $pcs[0];

$line = <SPINE>;
@pcs = split(' ',$line);
$Dist = $pcs[0];

# Whether a Balustrade tour (-1) or main wall tour (1)
$line = <SPINE>;
@pcs = split(' ',$line);
$Balustrade = $pcs[0];
if ($Balustrade == -1) { $Dist = -$Dist; }


# blank Line
$line = <SPINE>;

# Definition of outer spine
$numele = -1;
do {
    $line = <SPINE>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs > 0) {
	$numele ++;
	$spine[$numele][0] = $pcs[0];
	$spine[$numele][1] = $pcs[1];
#	$numreliefs = $pcs[2];
    }
} while $#pcs > 0 ;
#print "\nNumber of elements in outer spine $#spine \n";

close SPINE ;

#-------------------------------------------------------
# Generate spine for the given Quadrant
@quad = &genspine(@spine);

#-------------------------------------------------------
# Print Tour Data
if ($Section == 1) {
    $t[0] = $T1[0];
    $t[1] = $T1[1];
    $t[2] = $T1[2];
}
if ($Section == 2) {
    $t[0] = $T1[2];
    $t[1] = $T1[1];
    $t[2] = $T1[0];
}
if ($Section == 3) {
    $t[0] = $T1[0];
    $t[1] = $T1[1];
    $t[2] =-$T1[2];
}
if ($Section == 4) {
    $t[0] =-$T1[2];
    $t[1] = $T1[1];
    $t[2] = $T1[0];
}
$t[0] = $T1[0];
$t[1] = $T1[1];
$t[2] = $T1[2];
&printTour(@quad,$Section);
#
#------------------------------------------------------
# print VRML file
sub getRotation {
    my $rotation=0.0;
    if (abs($_[0]) > 0) {
	if ($_[0] < 0) { $rotation = 3.14; }
	else { $rotation = 0.0; }
    }
    if (abs($_[1]) > 0) {
	if ($_[1] > 0) { $rotation = -1.57; }
	else { $rotation =  1.57; }
    }
    if ($Balustrade == -1) {
	if ($rotation == 0.0) { $rotation = 3.14; }
	else { if ($rotation == 1.57) { $rotation = -1.57; }
	       else { if ($rotation == 3.14) { $rotation = 0.0; }
		      else { if ($rotation == -1.57) { $rotation = 1.57; }
			 }
		  }
	   }
    }
    $rotation;
}

sub getaxes {
    my @axes;
    if (abs($_[0]) > 0) { $axes[0] = 2; $axes[1] = 0; }
    else { $axes[0] = 0; $axes[1] = 2; }
    @axes;
}

sub printTour {
    my @coord = @_;
    my $numele = $#_-1;
    my $len;
    my $reliefs;
    my $stepx;
    my $stepz;
    my @Length;
    my @Time;
    my @Coord;
    my @Ori;
    my @axis;

    $Length[0] = 0.0;
    for $nsec (1 .. $numele) {
	$x = $_[$nsec][0];
	$z = $_[$nsec][2];
	$Length[$nsec] = $Length[$nsec-1]
	                 + abs($_[$nsec][0]-$_[$nsec-1][0])
			 + abs($_[$nsec][2]-$_[$nsec-1][2]);
    }
    $numsteps = -1;
    for $nsec (0 .. $numele-1) {
	$dx = $coord[$nsec+1][0]-$coord[$nsec][0];
	$dz = $coord[$nsec+1][2]-$coord[$nsec][2];
	$rotation = &getRotation($dx,$dz);
	$rotation += ($Section-1)*1.57079;
	@axes = &getaxes($dx,$dz);
	$sign = 1;
	if ($axes[1]==2) { $sign = -1; }
	$Tour[0] = $t[0] + $_[$nsec][0];
	$Tour[1] = $t[1];
	$Tour[2] = $t[2] + $_[$nsec][2];
	$Tour[$axes[0]] += $Dist;
	$Tour[$axes[1]] += $sign;
	$numsteps ++;
	$Time[$numsteps] = $Length[$nsec]/$Length[$numele];
	$nextstep = $Length[$nsec+1]/$Length[$numele];
	$tdif = ($nextstep-$Time[$numsteps])*0.03;
	$dif = $Length[$nsec+1]-$Length[$nsec];
	if ($dif < 3.0) { $tdif *= 5.0; }
	$Time[$numsteps] += $tdif;
	$Coord[$numsteps][0] = $Tour[0];
	$Coord[$numsteps][1] = $Tour[1];
	$Coord[$numsteps][2] = $Tour[2];
	$Ori[$numsteps] = $rotation;

	$Tour[0] = $t[0] + $_[$nsec+1][0];
	$Tour[1] = $t[1];
	$Tour[2] = $t[2] + $_[$nsec+1][2];
	$Tour[$axes[0]] += $Dist;
	$Tour[$axes[1]] -= $sign;
	$numsteps ++;
	$Time[$numsteps] = $Length[$nsec+1]/$Length[$numele];
	$Coord[$numsteps][0] = $Tour[0];
	$Coord[$numsteps][1] = $Tour[1];
	$Coord[$numsteps][2] = $Tour[2];
	$Ori[$numsteps] = $rotation;
    }
    
    @Coord = &Rotatequad(@Coord,$Section);

#    print "#============================================\n";
#    print "#==    ANTI CLOCKWISE TOUR    ==\n";
#    print "DEF ONEPos PositionInterpolator {\n";
#    print " key [\n";
#    print "      0.0,\n";
#    for $i (0 .. $numsteps) {
#	 print "      $Time[$i],\n";
#    }
#    print "    ]\n";
#    print "keyValue [\n";
#    print "      0.0 0.0 0.0,\n";
#    for $i (0 .. $numsteps) {
#	 print "      $Coord[$i][0] $Coord[$i][1] $Coord[$i][2],\n";
#    }
#    print "         ]\n}\n";
#    print "#============================================\n";
#    print "DEF ONEOri OrientationInterpolator {\n";
#    print " key [\n";
#    print "      0.0,\n";
#    for $i (0 .. $numsteps) {
#	 print "      $Time[$i],\n";
#    }
#    print "    ]\n";
#    print "keyValue [\n";
#    print "      0.0 1.0 0.0 0.0,\n";
#    for $i (0 .. $numsteps) {
#	 print "      0.0 1.0 0.0 $Ori[$i],\n";
#    }
#    print "         ]\n}\n";
#    print "#============================================\n";
#    print "\n";
    print "#============================================\n";
    print "#==    CLOCKWISE TOUR    ==\n";
    print "DEF ONEPos PositionInterpolator {\n";
    print " key [\n";
    print "      0.0,\n";
    for $i (0 .. $numsteps) {
	print "      $Time[$i],\n";
    }
    print "    ]\n";
    print "keyValue [\n";
    print "      0.0 0.0 0.0,\n";
    for $j (0 .. $numsteps) {
	$i = $numsteps-$j;
	print "      $Coord[$i][0] $Coord[$i][1] $Coord[$i][2],\n";
    }
    print "         ]\n}\n";
    print "#============================================\n";
    print "DEF ONEOri OrientationInterpolator {\n";
    print " key [\n";
    print "      0.0,\n";
    for $i (0 .. $numsteps) {
	print "      $Time[$i],\n";
    }
    print "    ]\n";
    print "keyValue [\n";
    print "      0.0 1.0 0.0 0.0,\n";
    for $j (0 .. $numsteps) {
	$i = $numsteps-$j;
	print "      0.0 1.0 0.0 $Ori[$i],\n";
    }
    print "         ]\n}\n";
    print "#============================================\n";
#    for $i (0 .. $numsteps) {
#	 $tt = $Time[$i]/4.0;
#	 print "      $tt,\n";
#    }
#    for $i (0 .. $numsteps) {
#	 $tt = 0.25 + $Time[$i]/4.0;
#	 print "      $tt,\n";
#    }
#    for $i (0 .. $numsteps) {
#	 $tt = 0.5 + $Time[$i]/4.0;
#	 print "      $tt,\n";
#    }
#    for $i (0 .. $numsteps) {
#	 $tt = 0.75 + $Time[$i]/4.0;
#	 print "      $tt,\n";
#    }
}
#
#########################################################
# Generate quadrants
sub Rotatequad {
    my @temp;
    my @spine;
    my $numele = $#_-1;
    my $flag = $_[$#_];

    $j = -1;
    for $i (0 .. $numele) {
	$y =  $_[$i][1];
	if ($flag == 1) {
# Rotate by 0 degrees
	    $x =  $_[$i][0];
	    $z =  $_[$i][2];
	}
	if ($flag == 2) {
# Rotate by 90 degrees
    	    $x =  $_[$i][2];
	    $z = -$_[$i][0];
	}
	if ($flag == 3) {
# Rotate by 180 degrees
	    $x = -$_[$i][0];
	    $z = -$_[$i][2];
	}
	if ($flag == 4) {
# Rotate by 270 degrees
	    $x = -$_[$i][2];
	    $z =  $_[$i][0];
	}
	$j++;
	$temp[$j][0] = $x;
	$temp[$j][1] = $y;
	$temp[$j][2] = $z;
    }
    @temp;
}
#
#########################################################
# Generate spine
sub genspine {
    my @spine ;
    my $numele = 0;
    $spine[0][0] = 0.0;
    $spine[0][1] = 0.0;
    $spine[0][2] = 0.0;
    for $i (0 .. $#_) {
	$numele ++;
	$spine[$numele][0] = $spine[$numele-1][0] + $_[$i][0];
	$spine[$numele][1] = 0.0;
	$spine[$numele][2] = $spine[$numele-1][2] + $_[$i][1];
    }
    @spine;
}
#
#########################################################
