#!/usr/bin/perl

print "#VRML V2.0 utf8\n\n";

$flnm = $ARGV[0];

open (SPINE, $flnm);
#Read the title
$line = <SPINE>;

print "#$line\n";
$line = <SPINE>;
@pcs = split(' ',$line);
# Read flags 
# $Section = 1 : First section (+x,+z)
#            2 : First section (+x,-z)
#            3 : First section (-x,-z)
#            4 : First section (-x,+z)
# $GenFlag = 0 : everything
#            1 : walls without relief placements
#            2 : only relief placements
$Section = $pcs[0];
$GenFlag = $pcs[1];

# 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];
# second line gives second translation parameters for surface 1
$line = <SPINE>;
@pcs = split(' ',$line);
$T12[0] = $pcs[0];
$T12[1] = $pcs[1];
$T12[2] = $pcs[2];
# first line gives first translation parameters for surface 2
$line = <SPINE>;
@pcs = split(' ',$line);
$T21[0] = $pcs[0];
$T21[1] = $pcs[1];
$T21[2] = $pcs[2];
# second line gives second translation parameters for surface 2
$line = <SPINE>;
@pcs = split(' ',$line);
$T22[0] = $pcs[0];
$T22[1] = $pcs[1];
$T22[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 outer cross Section
$numele = 0;
$cross2[$numele][0] = 0.0;
$cross2[$numele][1] = 0.0;
do {
    $line = <SPINE>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs > 0) {
	$numele ++;
	$cross2[$numele][0] = $pcs[0];
	$cross2[$numele][1] = $pcs[1];
	$cross2[$numele][0] += $cross2[$numele-1][0];
	$cross2[$numele][1] += $cross2[$numele-1][1];
	$tag2[$numele] = $pcs[2];
    }
} while $#pcs > 0 ;
#print "\nNumber of elements in outer cross section $#cross2 \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";

# Definition of outer spine
$numele = -1;
do {
    $line = <SPINE>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs > 0) {
	$numele ++;
	$spine2[$numele][0] = $pcs[0];
	$spine2[$numele][1] = $pcs[1];
    }
} while $#pcs > 0 ;
#print "\nNumber of elements in outer spine $#spine2 \n";

close SPINE ;

#########################################################

@first1 = &genquadrant(@spine1,$Section);
@cross = @cross1;
@firstex1 = &Extrude(@first1,$Section);
$maxht = getheight(@firstex1,$#cross1);
@first2 = &genquadrant(@spine2,$Section);
@cross = @cross2;
@firstex2 = &Extrude(@first2,$Section);
$ht1 = getheight(@firstex2,$#cross2);
if ($maxht < $ht1) { $maxht = $ht1; }
#
#########################################################
# Print Extruded surface 1
if ($Section==1) {
    print "# Inner surface First Quadrant\n";
    $t[0] = $T11[0];
    $t[1] = $T11[1];
    $t[2] = $T11[2];
}
if ($Section==2) {
    print "# Inner surface Second Quadrant\n";
    $t[0] = $T12[0];
    $t[1] = $T12[1];
    $t[2] =-$T12[2];
}
if ($Section==3) {
    print "# Inner surface Third Quadrant\n";
    $t[0] =-$T11[0];
    $t[1] = $T11[1];
    $t[2] =-$T11[2];
}
if ($Section==4) {
    print "# Inner surface Fourth Quadrant\n";
    $t[0] =-$T12[0];
    $t[1] = $T12[1];
    $t[2] = $T12[2];
}
@tag = @tag1;
&printExtrusion(@firstex1,$#cross1,$maxht);
##########################################################
# Print Extruded surface 2
if ($Section==1) {
    print "# Outer surface First Quadrant\n";
    $t[0] = $T21[0];
    $t[1] = $T21[1];
    $t[2] = $T21[2];
}
if ($Section==2) {
    print "# Outer surface Second Quadrant\n";
    $t[0] = $T22[0];
    $t[1] = $T22[1];
    $t[2] =-$T22[2];
}
if ($Section==3) {
    print "# Outer surface Third Quadrant\n";
    $t[0] =-$T21[0];
    $t[1] = $T21[1];
    $t[2] =-$T21[2];
}
if ($Section==4) {
     print "# Outer surface Fourth Quadrant\n";
    $t[0] =-$T22[0];
    $t[1] = $T22[1];
    $t[2] = $T22[2];
}
@tag = @tag2;
&printExtrusion(@firstex2,$#cross2,$maxht);
#
#########################################################
#
# Generate and Print Top and Side surfaces
if ($Section == 1) {
    print "\n# Top surface First quadrant\n";
    $t[0] = $T11[0];
    $t[1] = $T11[1];
    $t[2] = $T11[2];
}
if ($Section == 2) {
    print "\n# Top surface Second quadrant\n";
    $t[0] = $T12[0];
    $t[1] = $T12[1];
    $t[2] =-$T12[2];
}
if ($Section == 3) {
    print "\n# Top surface Third quadrant\n";
    $t[0] =-$T11[0];
    $t[1] = $T11[1];
    $t[2] =-$T11[2];
}
if ($Section == 4) {
    print "\n# Top surface Fourth quadrant\n";
    $t[0] =-$T12[0];
    $t[1] = $T12[1];
    $t[2] = $T12[2];
}
$d[0] = 0.0;
$d[1] = 0.0;
$d[2] = 0.0;
@top1 = &getTop(@firstex1,1);
@side1 = &getSide(@firstex1,1);

if ($Section==1) {
    $d[0] = $T11[0] - $T21[0];
    $d[1] = $T11[1] - $T21[1];
    $d[2] = $T11[2] - $T21[2];
}
if ($Section==2) {
    $d[0] = $T12[0] - $T22[0];
    $d[1] = $T12[1] - $T22[1];
    $d[2] =-$T12[2] + $T22[2];
}
if ($Section==3) {
    $d[0] =-$T11[0] + $T21[0];
    $d[1] = $T11[1] - $T21[1];
    $d[2] =-$T11[2] + $T21[2];
}
if ($Section==4) {
    $d[0] =-$T12[0] + $T22[0];
    $d[1] = $T12[1] - $T22[1];
    $d[2] = $T12[2] - $T22[2];
}
@top2 = &getTop(@firstex2,2);
&printFace(@top1,@top2);

print "\n# Side surfaces\n";
@side2 = &getSide(@firstex2,2);
@Side = &collate(@side1,@side2);
&printSide(@Side,$maxht);
#
#########################################################
#
# Generate and Print Side surfaces
# print "\n# Side surfaces First quadrant\n";
# $t[0] = $T11[0];
# $t[1] = $T11[1];
# $t[2] = $T11[2];
# $d[0] = 0.0;
# $d[1] = 0.0;
# $d[2] = 0.0;
# @one = &getSide(@firstex1,1);
# $d[0] = $T11[0] - $T21[0];
# $d[1] = $T11[1] - $T21[1];
# $d[2] = $T11[2] - $T21[2];
# @two = &getSide(@firstex2,2);
# @Side = &collate(@one,@two);
# &printSide(@Side,$maxht);
#
#########################################################
# extract Top spine portion of the walls
sub getTop {
    my @temp;
    my $numcross = 0;
    my $numele = $#_-1;
    my $flag = $_[$#_];

    $numcross = $#cross1;
    if ($flag == 2) {$numcross = $#cross2;}

    for $i (0 .. $numele) {
	$temp[$i][0] = $_[$i][$numcross][0] - $d[0];
	$temp[$i][1] = $_[$i][$numcross][1] - $d[1];
	$temp[$i][2] = $_[$i][$numcross][2] - $d[2];
    }
    @temp;
}
#
#########################################################
# Collate side cross Sections of the walls
sub collate {
    my @temp;

    for $j (0 .. $#cross1) {
	$temp[0][$j][0] = $_[0][$j][0];
	$temp[0][$j][1] = $_[0][$j][1];
	$temp[0][$j][2] = $_[0][$j][2];
	$temp[1][$j][0] = $_[1][$j][0];
	$temp[1][$j][1] = $_[1][$j][1];
	$temp[1][$j][2] = $_[1][$j][2];
    }
    for $j (0 .. $#cross2) {
	$temp[0][$#cross1+1+$j][0] = $_[2][$#cross2-$j][0];
	$temp[0][$#cross1+1+$j][1] = $_[2][$#cross2-$j][1];
	$temp[0][$#cross1+1+$j][2] = $_[2][$#cross2-$j][2];
	$temp[1][$#cross1+1+$j][0] = $_[3][$#cross2-$j][0];
	$temp[1][$#cross1+1+$j][1] = $_[3][$#cross2-$j][1];
	$temp[1][$#cross1+1+$j][2] = $_[3][$#cross2-$j][2];
    }
    @temp;
}
#
#########################################################
# Finding height of extrusion
sub getheight {
    my $ht = 0.0;
    my $numcross = $_[$#_]-1;

    for $j (1 .. $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 ($i==0) {
	    $dx = $_[0][0]-$_[1][0];
	    if (abs($dx) > 0.0001) { $facx = 0; $facz = 1; }
	    else { $facx = 1; $facz = 0; }
	}
	else {
	    if ($i==$numele) {
		$dx = $_[$numele-1][0]-$_[$numele][0];
		if (abs($dx) > 0.0001) { $facx = 0; $facz = 1; }
		else { $facx = 1; $facz = 0; }
	    }
	    else {
		$facx = 1; $facz = 1;
	    }
	}
	if ($flag==2) { $facz = -$facz; }
	if ($flag==3) { $facx = -$facx; $facz = -$facz; }
	if ($flag==4) { $facx = -$facx; }
	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;
	print "Group {\n";
	print "children[\n";
	for $j (0 .. $numcross-1) {
	  $j1 = $j+1;
	  if ( ($GenFlag == 0) ||
	       ($GenFlag == 1 && $tag[$j1] < 1000) ||
	       ($GenFlag == 2 && $tag[$j1] > 1000) ) {
	    $xc = ($coord[$i][$j][0] + $coord[$i1][$j1][0])/2.0;
	    $yc = ($coord[$i][$j][1] + $coord[$i1][$j1][1])/2.0;
	    $zc = ($coord[$i][$j][2] + $coord[$i1][$j1][2])/2.0;
	    print "Transform {\n";
	    printf "  translation %7.2f %7.2f %7.2f\n",$t[0],$t[1],$t[2];
	    print "  children LOD {\n";
	    printf "    center %7.2f %7.2f %7.2f\n",$xc,$yc,$zc;
	    print "    range [10 20 30]\n";
	    print "    level [\n";

	    print "     Shape {\n";
	    print "      appearance BRICK256$tag[$j1] { }\n";
	    print "      geometry DEF WALL IndexedFaceSet {\n";
	    print "        solid FALSE\n";
	    print "        coord Coordinate {\n";
	    print "          point [\n";
	    printf "             %7.2f %7.2f %7.2f,\n",$coord[$i][$j][0],$coord[$i][$j][1],$coord[$i][$j][2];
	    printf "             %7.2f %7.2f %7.2f,\n",$coord[$i1][$j][0],$coord[$i1][$j][1],$coord[$i1][$j][2];
	    printf "             %7.2f %7.2f %7.2f,\n",$coord[$i1][$j1][0],$coord[$i1][$j1][1],$coord[$i1][$j1][2];
	    printf "             %7.2f %7.2f %7.2f\n",$coord[$i][$j1][0],$coord[$i][$j1][1],$coord[$i][$j1][2];
	    print "          ]\n";
	    print "        }\n";
	    print "        coordIndex [ 0, 1, 2, 3, -1 ]\n";
	    print "        texCoord TextureCoordinate {\n";
	    print "          point [\n";
	    if ($tag[$j+1]>100) {
		printf "                  %8.3f 0,\n",$fracS[0][$j];
		printf "                  %8.3f 0,\n",$fracS[1][$j];
		printf "                  %8.3f 1,\n",$fracS[1][$j1];
		printf "                  %8.3f 1,\n",$fracS[0][$j1];
	    }
	    else
	    {
		printf "                  %8.3f %8.3f,\n",$fracS[0][$j],$fracT[$j];
		printf "                  %8.3f %8.3f,\n",$fracS[1][$j],$fracT[$j];
		printf "                  %8.3f %8.3f,\n",$fracS[1][$j1],$fracT[$j1];
		printf "                  %8.3f %8.3f,\n",$fracS[0][$j1],$fracT[$j1];
	    }
	    print "                ]\n";
	    print "        }\n";
	    print "      }\n";
	    print "     }\n";

	    print "     Shape {\n";
	    print "      appearance BRICK128$tag[$j1] { }\n";
	    print "      geometry USE WALL\n";
	    print "     }\n";

	    print "     Shape {\n";
	    print "      appearance BRICK64$tag[$j1] { }\n";
	    print "      geometry USE WALL\n";
	    print "     }\n";

	    print "     Shape {\n";
	    print "      appearance BRICK32$tag[$j1] { }\n";
	    print "      geometry USE WALL\n";
	    print "     }\n";

	    print "   ]\n";
	    print "  }\n";
	    print "}\n";
	}
      }
      print "]\n}\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;
}
#
#########################################################
# extract side cross Sections of the walls
sub getSide {
    my @temp;
    my $numcross = 0;
    my $numele = $#_-1;
    my $flag = $_[$#_];

    $numcross = $#cross1;
    if ($flag == 2) {$numcross = $#cross2;}

    for $j (0 .. $numcross) {
	$temp[0][$j][0] = $_[0][$j][0] - $d[0];
	$temp[0][$j][1] = $_[0][$j][1] - $d[1];
	$temp[0][$j][2] = $_[0][$j][2] - $d[2];
    }
    for $j (0 .. $numcross) {
	$temp[1][$j][0] = $_[$numele][$j][0] - $d[0];
	$temp[1][$j][1] = $_[$numele][$j][1] - $d[1];
	$temp[1][$j][2] = $_[$numele][$j][2] - $d[2];
    }
    @temp;
}
#
#########################################################
# print Side surfaces
sub printSide {

    my @coord = @_;
    my $numele = $#_-1;
    my $numcross = $#cross1 + $#cross2 + 1;
    my $maxht = $_[$#_];

## Finding mapping parameters for vertical axis
    $lenj[0] = 0.0;
    for $j (1 .. $#cross1) {
	$lenj[$j] = $lenj[$j-1] + abs($coord[0][$j][1] - $coord[0][$j-1][1]);
     }
    for $j (0 .. $#cross1) {
	$fracT[$j] = $lenj[$j] / $maxht;
    }
    $lenj[0] = 0.0;
    for $j (1 .. $#cross2) {
	$lenj[$j] = $lenj[$j-1] + abs($coord[0][$numcross-$j][1]
			            - $coord[0][$numcross-$j+1][1]);
     }
    for $j (0 .. $#cross2) {
	$fracT[$#cross1+$j+1] = $lenj[$#cross2-$j] / $maxht;
    }

# Find mapping parameters for horizontal axis
    $lenx0 = abs($coord[0][$#cross1][0]-$coord[0][$#cross1+1][0]);
    $lenx1 = abs($coord[1][$#cross1][0]-$coord[1][$#cross1+1][0]);
    $lenz0 = abs($coord[0][$#cross1][2]-$coord[0][$#cross1+1][2]);
    $lenz1 = abs($coord[1][$#cross1][2]-$coord[1][$#cross1+1][2]);
    $len0 = $lenz0;
    $axis0 = 2;
    if ($lenx0 > $lenz0) {
	$len0 = $lenx0;
	$axis0 = 0;
    }
    $center0 = $coord[0][$#cross1][$axis]+$len0/2;
    $axis1 = 2;
    $len1 = $lenz1;
    if ($lenx1 > $lenz1) {
	$len1 = $lenx1;
	$axis1 = 0;
    }
    $center1 = $coord[1][$#cross1][$axis]+$len1/2;

#First Side
    $xc = ($coord[0][0][0] + $coord[0][$numcross/2][0])/2.0;
    $yc = ($coord[0][0][1] + $coord[0][$numcross/2][1])/2.0;
    $zc = ($coord[0][0][2] + $coord[0][$numcross/2][2])/2.0;
    print "Transform {\n";
    printf "  translation %7.2f %7.2f %7.2f\n",$t[0],$t[1],$t[2];
    print "  children LOD {\n";
    printf "   center %7.2f %7.2f %7.2f\n",$xc,$yc,$zc;
    print "   range [10 20 30]\n";
    print "   level [\n";
    print "    Shape {\n";
    print "      appearance BRICK2560 { }\n";
    print "      geometry DEF SIDE IndexedFaceSet {\n";
    print "        solid FALSE\n";
    print "        convex FALSE\n";
    print "        coord Coordinate {\n";
    print "          point [\n";
    for $j (0 .. $numcross) {
	printf "             %7.2f %7.2f %7.2f,\n",
                            $coord[0][$j][0],$coord[0][$j][1],$coord[0][$j][2];
    }
    print "          ]\n";
    print "        }\n";
    print "        coordIndex [\n";
    print "        ";
    for $i (0 .. $numcross) { print "$i, "; }
    print "-1]\n";
    print "        texCoord TextureCoordinate {\n";
    print "          point [\n";
    for $j (0 .. $numcross) {
	$fracS = ($coord[0][$j][$axis0]-$center0)/4.0;
	printf "                 %8.3f %8.3f,\n",$fracS,$fracT[$j];
    }
    print "          ]\n";
    print "        }\n";
    print "      }\n";
    print "    }\n";

    print "    Shape {\n";
    print "      appearance BRICK1280 { }\n";
    print "      geometry USE SIDE\n";
    print "    }\n";

    print "    Shape {\n";
    print "      appearance BRICK640 { }\n";
    print "      geometry USE SIDE\n";
    print "    }\n";

    print "    Shape {\n";
    print "      appearance BRICK320 { }\n";
    print "      geometry USE SIDE\n";
    print "    }\n";

    print "   ]\n";
    print "  }\n";
    print "}\n";

# Second Side
    $xc = ($coord[1][0][0] + $coord[1][$numcross/2][0])/2.0;
    $yc = ($coord[1][0][1] + $coord[1][$numcross/2][1])/2.0;
    $zc = ($coord[1][0][2] + $coord[1][$numcross/2][2])/2.0;
    print "Transform {\n";
    printf "  translation %7.2f %7.2f %7.2f\n",$t[0],$t[1],$t[2];
    print "  children LOD {\n";
    printf "   center %7.2f %7.2f %7.2f\n",$xc,$yc,$zc;
    print "   range [10 20 30]\n";
    print "   level [\n";
    print "    Shape {\n";
    print "      appearance BRICK2560 { }\n";
    print "      geometry DEF SIDE IndexedFaceSet {\n";
    print "        solid FALSE\n";
    print "        convex FALSE\n";
    print "        coord Coordinate {\n";
    print "          point [\n";
    for $j (0 .. $numcross) {
	printf "             %7.2f %7.2f %7.2f,\n",$coord[1][$j][0],$coord[1][$j][1],$coord[1][$j][2];
    }
    print "          ]\n";
    print "        }\n";
    print "        coordIndex [\n";
    print "        ";
    for $i (0 .. $numcross) { print "$i, "; }
    print "-1]\n";
    print "        texCoord TextureCoordinate {\n";
    print "          point [\n";
    for $j (0 .. $numcross) {
	$fracS = ($coord[1][$j][$axis1]-$center1)/4.0;
	printf "                 %8.3f %8.3f,\n",$fracS,$fracT[$j];
    }
    print "          ]\n";
    print "        }\n";
    print "      }\n";
    print "    }\n";

    print "    Shape {\n";
    print "      appearance BRICK1280 { }\n";
    print "      geometry USE SIDE\n";
    print "    }\n";

    print "    Shape {\n";
    print "      appearance BRICK640 { }\n";
    print "      geometry USE SIDE\n";
    print "    }\n";

    print "    Shape {\n";
    print "      appearance BRICK320 { }\n";
    print "      geometry USE SIDE\n";
    print "    }\n";

    print "   ]\n";
    print "  }\n";
    print "}\n";
}
#
#########################################################
# print floors/tops
sub printFace {

    my @coord = @_;
    my $numele = $#_;
    my $numone = ($numele+1)/2;
    
    for $i (0 .. $numone-2) {	
	$i1 = $i+1;
	$n = $numone+$i;
	$n1 = $numone+1+$i;

	$axisS = 0;
	$axisT = 2;

	$fSi0 = ($coord[$i][$axisS]+$t[$axisS])/4.0;
	$fSi1 = ($coord[$i1][$axisS]+$t[$axisS])/4.0;
	$fSn0 = ($coord[$n][$axisS]+$t[$axisS])/4.0;
	$fSn1 = ($coord[$n1][$axisS]+$t[$axisS])/4.0;

	$fTi0 = ($coord[$i][$axisT]+$t[$axisT])/4.0;
	$fTi1 = ($coord[$i1][$axisT]+$t[$axisT])/4.0;
	$fTn0 = ($coord[$n][$axisT]+$t[$axisT])/4.0;
	$fTn1 = ($coord[$n1][$axisT]+$t[$axisT])/4.0;

	$xc = ($coord[$i][0] + $coord[$n1][0])/2.0;
	$yc = ($coord[$i][1] + $coord[$n1][1])/2.0;
	$zc = ($coord[$i][2] + $coord[$n1][2])/2.0;
	print "Transform {\n";
	print "  translation $t[0] $t[1] $t[2]\n";
	print "  children LOD {\n";
	print "   center $xc $yc $zc\n";
	print "   range [5 15 25]\n";
	print "   level [\n";
	print "    Shape {\n";
	print "      appearance BRICK2560 { }\n";
	print "      geometry DEF TOP IndexedFaceSet {\n";
	print "        solid FALSE\n";
	print "        coord Coordinate {\n";
	print "          point [\n";
	printf "             %7.2f %7.2f %7.2f,\n",$coord[$i][0],$coord[$i][1],$coord[$i][2];
	printf "             %7.2f %7.2f %7.2f,\n",$coord[$i1][0],$coord[$i1][1],$coord[$i1][2];
	printf "             %7.2f %7.2f %7.2f,\n",$coord[$n1][0],$coord[$n1][1],$coord[$n1][2];
	printf "             %7.2f %7.2f %7.2f\n",$coord[$n][0],$coord[$n][1],$coord[$n][2];
	print "          ]\n";
	print "        }\n";
	print "        coordIndex [0, 1, 2, 3, -1]\n";
	print "        texCoord TextureCoordinate {\n";
	print "          point [\n";
	printf "            %8.3f %8.3f, %8.3f %8.3f, %8.3f %8.3f, %8.3f %8.3f\n",
	                   $fSi0,$fTi0,$fSi1,$fTi1,$fSn1,$fTn1,$fSn0,$fTn0;
	print "          ]\n";
	print "        }\n";
	print "      }\n";
	print "    }\n";
	print "    Shape {\n";
	print "      appearance BRICK1280 { }\n";
	print "      geometry USE TOP\n";
	print "    }\n";
	print "    Shape {\n";
	print "      appearance BRICK640 { }\n";
	print "      geometry USE TOP\n";
	print "    }\n";
	print "    Shape {\n";
	print "      appearance BRICK320 { }\n";
	print "      geometry USE TOP\n";
	print "    }\n";
	print "  ]\n";
	print " }\n";
	print "}\n";
    }
}
