#!/usr/bin/perl

$flnm = $ARGV[0];

open (SPINE, $flnm);

# bottom translation parameters
$line = <SPINE>;
@pcs = split(' ',$line);
$T1[0] = $pcs[0];
$T1[1] = $pcs[1];
$T1[2] = $pcs[2];

# top translation parameters
$line = <SPINE>;
@pcs = split(' ',$line);
$T2[0] = $pcs[0];
$T2[1] = $pcs[1];
$T2[2] = $pcs[2];

# End top stairs at this point
$line = <SPINE>;
@pcs = split(' ',$line);
$T3[0] = $pcs[0];
$T3[1] = $pcs[1];
$T3[2] = $pcs[2];

# blank Line
$line = <SPINE>;

$line = <SPINE>;
@pcs = split(' ',$line);
$Slength = $pcs[0];
$nSteps = $pcs[1];

close SPINE ;
print "#VRML V2.0 utf8\n\n";
print " PROTO BRICK [ ] {\n";
print "  Appearance {\n";
print "    texture ImageTexture { url \"../IMG/64/brick17.jpg\" }\n";
print "    textureTransform TextureTransform { rotation 1.57079 scale 2.5 1.6}\n";
print "    material Material {diffuseColor 0.6 0.6 0.6 }\n";
print "  }\n";
print " }\n\n";
print "DEF Q1 Transform {\n";
print "children [\n";
#########################################################
$t[0] = $T1[0];
$t[1] = $T1[1];
$t[2] = $T1[2];
@Stairs = &genStairs();
&printStairs(@Stairs);
#
##########################################################
# Replicate the object in remaining quadrants
print "]\n}\n\n";
print "Transform {\n";
print "  rotation 0.0 1.0 0.0 1.57079\n";
print "  children [\n";
print "    USE Q1\n";
print "    Transform {\n";
print "      rotation 0.0 1.0 0.0 1.57079\n";
print "      children [\n";
print "        USE Q1\n";
print "        Transform {\n";
print "        	 rotation 0.0 1.0 0.0 1.57079\n";
print "        	 children [\n";
print "        	   USE Q1\n";
print "          ]\n";
print "        }\n";
print "      ]\n";
print "    }\n";
print "  ]\n";
print "}\n";
#
#########################################################
# Generate Stairs
sub genStairs {
    my @Stairs;
    my $stepY = ($T2[1]-$T1[1])/$nSteps;
    my $stepZ = ($T2[2]-$T1[2])/$nSteps;

    $j = 0;
    $Stairs[$j][0] = 0.0;
    $Stairs[$j][1] = 0.0;
    $Stairs[$j][2] = 0.0;
    $Y = 0.0;
    $Z = 0.0;
    for $i (0 .. $nSteps-1) {
	$j++;
	$Stairs[$j][0] = 0.0;
	$Stairs[$j][1] = $Y + $stepY;
	$Stairs[$j][2] = $Z;
	$j++;
	$Stairs[$j][0] = 0.0;
	$Stairs[$j][1] = $Y + $stepY;
	$Stairs[$j][2] = $Z + $stepZ;
	$Y += $stepY;
	$Z += $stepZ;
    }
    $Stairs[$j][0] = 0.0;
    $Stairs[$j][1] = $T3[1]-$T1[1];
    $Stairs[$j][2] = $T3[2]-$T1[2];
    @Stairs;
}
#
#########################################################
# print VRML file
sub printStairs {
    my @coord = @_;
    my $numele = $#_;

# Texture mapping
    $len[0] = 0.0;
    for $i (1 .. $numele) {
	$len[$i] = $len[$i-1]
                 + abs($coord[$i][1]-$coord[$i-1][1])
		 + abs($coord[$i][2]-$coord[$i-1][2]);
    }

    print "Transform {\n";
    printf "  translation %7.2f %7.2f %7.2f\n",$T1[0],$T1[1],$T1[2];
    print "  children [\n";
    print "    Shape {\n";
    print "      appearance BRICK { }\n";
    print "      geometry IndexedFaceSet {\n";
    print "        solid FALSE\n";
    print "        coord Coordinate {\n";
    print "          point [\n";
    for $i (0 .. $numele) {
	printf "            %7.2f %7.2f %7.2f,\n",
         $coord[$i][0],$coord[$i][1],$coord[$i][2];
    }
    for $i (0 .. $numele) {
	$x = $coord[$i][0]+$Slength;
	printf "            %7.2f %7.2f %7.2f,\n",
                $x,$coord[$i][1],$coord[$i][2];
    }
    print "          ]\n";
    print "        }\n";
    print "        coordIndex [\n";
    for $i (0 .. $numele-1) {
	$i0 = $i;
	$i1 = $i+1;
	$i2 = $i + $numele+2;
	$i3 = $i + $numele+1;
	print "          $i0 $i1 $i2 $i3 -1,\n";
    }
    print "        ]\n";
    print "        texCoord TextureCoordinate {\n";
    print "          point [\n";
    for $i (0 .. $numele) {
	$fracT = $len[$i]/$len[$numele];
	printf "            0.0 %8.3f,\n",$fracT;
    }
    for $i (0 .. $numele) {
	$fracT = $len[$i]/$len[$numele];
	printf "            1.0 %8.3f,\n",$fracT;
    }
    print "                ]\n";
    print "        }\n";
    print "      }\n";
    print "    }\n";
    print "  ]\n";
    print "}\n";
}
#
#########################################################
