#!/usr/bin/perl

print "#VRML V2.0 utf8\n\n";

print "DEF Q1 Transform {\n";
print "children [\n";

$flnm = $ARGV[0];

open (SPINE, $flnm);
#Read the title
$line = <SPINE>;
print "#$line\n";

# blank Line
$line = <SPINE>;

# first line gives first translation parameters for surface 1
$line = <SPINE>;
@pcs = split(' ',$line);
$T11[0] = $pcs[0];
$T11[1] = $pcs[1];
$T11[2] = $pcs[2];

# blank Line
$line = <SPINE>;

# Definition of inner cross Section
$numele = 0;
$cross1[$numele][0] = 0.0;
$cross1[$numele][1] = 0.0;
do {
    $line = <SPINE>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs > 0) {
	$numele ++;
	$cross1[$numele][0] = $pcs[0];
	$cross1[$numele][1] = $pcs[1];
	$cross1[$numele][0] += $cross1[$numele-1][0];
	$cross1[$numele][1] += $cross1[$numele-1][1];
	$tag1[$numele] = $pcs[2];
    }
} while $#pcs > 0 ;
#print "\nNumber of elements in inner cross section $#cross1 \n";

# Definition of inner spine
$numele = -1;
do {
    $line = <SPINE>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs > 0) {
	$numele ++;
	$spine1[$numele][0] = $pcs[0];
	$spine1[$numele][1] = $pcs[1];
    }
} while $#pcs > 0 ;
#print "\nNumber of elements in inner spine $#spine1 \n";

close SPINE ;

#--------------------------------------------------------

@first1 = &genquadrant(@spine1,1);
@cross = @cross1;
@firstex1 = &Extrude(@first1,1);
$maxht = getheight(@firstex1,$#cross1);

print "# Inner surface First quadrant\n";
$t[0] = $T11[0];
$t[1] = $T11[1];
$t[2] = $T11[2];
@tag = @tag1;
&printExtrusion(@firstex1,$#cross1,$maxht);


#--------------------------------------------------------
# Replicate the object in remaining quadrants
print "]\n}\n\n";
print "Transform {\n";
print "  rotation 0.0 1.0 0.0 1.57\n";
print "  children [\n";
print "    USE Q1\n";
print "    Transform {\n";
print "      rotation 0.0 1.0 0.0 1.57\n";
print "      children [\n";
print "        USE Q1\n";
print "        Transform {\n";
print "        	 rotation 0.0 1.0 0.0 1.57\n";
print "        	 children [\n";
print "        	   USE Q1\n";
print "          ]\n";
print "        }\n";
print "      ]\n";
print "    }\n";
print "  ]\n";
print "}\n";

#--------------------------------------------------------
# Finding height of extrusion
sub getheight {
    my $ht = 0.0;
    my $numcross = $_[$#_]-1;

    for $j (0 .. $numcross) {
	$ht = $ht + abs($_[0][$j][1] - $_[0][$j-1][1]);
    }
    $ht;
}

#--------------------------------------------------------
# Generate Extruded surface given cross section and spine
sub Extrude {
    my @surface ;
    my $numele = $#_ - 1;
    my $flag = $_[$#_];

    for $i (0 .. $numele) {
	if ($flag == 1){
	    if ($i==0) {$facx = 0; $facz = 1;}
	    else {
		if ($i==$numele) {$facx = 1; $facz = 0;}
		else {$facx = 1; $facz = 1;}
	    }
	}
	if ($flag == 2){
	    if ($i==0) {$facx = 1; $facz = 0;}
	    else {
		if ($i==$numele) {$facx = 0; $facz = -1;}
		else {$facx = 1; $facz = -1;}
	    }
	}
	if ($flag == 3){
	    if ($i==0) {$facx = 0; $facz = -1;}
	    else {
		if ($i==$numele) {$facx = -1; $facz = 0;}
		else {$facx = -1; $facz = -1;}
	    }
	}
	if ($flag == 4){
	    if ($i==0) {$facx = -1; $facz = 0;}
	    else {
		if ($i==$numele) {$facx = 0; $facz = 1;}
		else {$facx = -1; $facz = 1;}
	    }
	}
	for $j (0 .. $#cross) {
	    $surface[$i][$j][0] = $_[$i][0] + $facx*$cross[$j][1];;
	    $surface[$i][$j][1] = $_[$i][1] + $cross[$j][0];;
	    $surface[$i][$j][2] = $_[$i][2] + $facz*$cross[$j][1];;
	}
    }
    @surface;
}

#--------------------------------------------------------
# print VRML file
sub printExtrusion {
    my @coord = @_;
    my $numele = $#_-2;
    my $numcross = $_[$#_-1];
    my $maxht = $_[$#_];

## Finding mapping parameters for vertical axis
    $lenj[0] = 0.0;
    for $j (1 .. $numcross) {
	 $lenj[$j] = $lenj[$j-1] + 
	    abs($coord[0][$j][1] - $coord[0][$j-1][1]);
     }
    for $j (0 .. $numcross) {
	$fracT[$j] = $lenj[$j] / $maxht;
    }


    for $i (0 .. $numele-1) {
	$totlen = 0.0;
	$axis = 0;
	if (abs($firstex1[$i+1][0][2] - $firstex1[$i][0][2]) > 0.0001) {$axis = 2;}
## Finding mapping parameters for horizontal axis
	 for $j (0 .. $numcross) {
	     $fraction = $coord[$i][$j][$axis] + $t[$axis];
	     $fracS[0][$j] = $fraction/4;
	     $fraction = $coord[$i+1][$j][$axis] + $t[$axis];
	     $fracS[1][$j] = $fraction/4;
	 }

	$i1 = $i+1;
	for $j (0 .. $numcross-1) {
	    $j1 = $j+1;
	    print "Transform {\n";
	    print "  translation $t[0] $t[1] $t[2]\n";
	    print "  children [\n";
	    print "    Shape {\n";
	    print "      appearance BRICK$tag[$j1] { }\n";
	    print "      geometry IndexedFaceSet {\n";
	    print "        solid FALSE\n";
	    print "        coord Coordinate {\n";
	    print "          point [\n";
	    print "             $coord[$i][$j][0]  $coord[$i][$j][1] $coord[$i][$j][2],\n";
	    print "             $coord[$i1][$j][0] $coord[$i1][$j][1] $coord[$i1][$j][2],\n";
	    print "             $coord[$i1][$j1][0] $coord[$i1][$j1][1] $coord[$i1][$j1][2],\n";
	    print "             $coord[$i][$j1][0] $coord[$i][$j1][1] $coord[$i][$j1][2],\n";
	    print "          ]\n";
	    print "        }\n";
	    print "        coordIndex [ 0, 1, 2, 3, -1 ]\n";
	    print "        texCoord TextureCoordinate {\n";
	    print "          point [\n";
	    if ($tag[$j+1]>100) {
		print "                  $fracS[0][$j] 0,\n";
		print "                  $fracS[1][$j] 0,\n";
		print "                  $fracS[1][$j1] 1,\n";
		print "                  $fracS[0][$j1] 1\n";
	    }
	    else
	    {
		print "                  $fracS[0][$j] $fracT[$j],\n";
		print "                  $fracS[1][$j] $fracT[$j],\n";
		print "                  $fracS[1][$j1] $fracT[$j1],\n";
		print "                  $fracS[0][$j1] $fracT[$j1]\n";
	    }
	    print "                ]\n";
	    print "        }\n";
	    print "      }\n";
	    print "    }\n";
	    print "  ]\n";
	    print "}\n";
	}
    }
}

#--------------------------------------------------------
# Generate quadrants
sub genquadrant {
    my @temp;
    my @spine;
    my $numele = $#_ - 1;
    my $flag = $_[$#_];

    $j = -1;
    for $i (0 .. $numele) {
	if ($flag == 1) {
	    $x =       $_[$i][0];
	    $y =       $_[$i][1];
	}
	if ($flag == 2) {
	    $x = 0.0 - $_[$numele-$i][0];
	    $y =       $_[$numele-$i][1];
	}
	if ($flag == 3) {
	    $x = 0.0 - $_[$i][0];
	    $y = 0.0 - $_[$i][1];
	}
	if ($flag == 4) {
	    $x =       $_[$numele-$i][0];
	    $y = 0.0 - $_[$numele-$i][1];
	}
	$j++;
	$temp[$j][0] = $x;
	$temp[$j][1] = $y;
    }
    @spine = genspine(@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;
}
