#!/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 ;


print "PROTO BRICK2560 [ ] {\n";
print "	Appearance {\n";
print "	   material Material {\n";
print "	     diffuseColor 1.0 1.0 0.1\n";
print "	     emissiveColor 0.7 0.7 0.5\n";
print "      shininess 1\n";
print "	   }\n";
print "	}\n";
print "}\n";
print "PROTO BRICK256101 [ ] {\n";
print "	 Appearance {\n";
print "	   material Material {\n";
print "	     diffuseColor 1.0 0.0 1.0\n";
print "	     emissiveColor 0.7 0.1 0.6\n";
print "      shininess 1\n";
print "	   }\n";
print "	 }\n";
print "}\n";
print "PROTO BRICK256500 [ ] {\n";
print "	 Appearance {\n";
print "	   material Material {\n";
print "	     diffuseColor 1.0 0.0 0.0\n";
print "	     emissiveColor 0.7 0.1 0.1\n";
print "      shininess 1\n";
print "	   }\n";
print "	 }\n";
print "}\n";
print "DEF MainWall Transform {\n";
print "children [\n";
#
#########################################################
@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
# Dummy walls no Rear #if ($Section==1) {
# Dummy walls no Rear #	   print "# Inner surface First Quadrant\n";
# Dummy walls no Rear #	   $t[0] = $T11[0];
# Dummy walls no Rear #	   $t[1] = $T11[1];
# Dummy walls no Rear #	   $t[2] = $T11[2];
# Dummy walls no Rear #}
# Dummy walls no Rear #if ($Section==2) {
# Dummy walls no Rear #	   print "# Inner surface Second Quadrant\n";
# Dummy walls no Rear #	   $t[0] = $T12[0];
# Dummy walls no Rear #	   $t[1] = $T12[1];
# Dummy walls no Rear #	   $t[2] =-$T12[2];
# Dummy walls no Rear #}
# Dummy walls no Rear #if ($Section==3) {
# Dummy walls no Rear #	   print "# Inner surface Third Quadrant\n";
# Dummy walls no Rear #	   $t[0] =-$T11[0];
# Dummy walls no Rear #	   $t[1] = $T11[1];
# Dummy walls no Rear #	   $t[2] =-$T11[2];
# Dummy walls no Rear #}
# Dummy walls no Rear #if ($Section==4) {
# Dummy walls no Rear #	   print "# Inner surface Fourth Quadrant\n";
# Dummy walls no Rear #	   $t[0] =-$T12[0];
# Dummy walls no Rear #	   $t[1] = $T12[1];
# Dummy walls no Rear #	   $t[2] = $T12[2];
# Dummy walls no Rear #}
# Dummy walls no Rear #@tag = @tag1;
# Dummy walls no Rear #&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);
#Dummy walls no Top surface #&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);
#
#########################################################
# Replicate the structure
print "]\n";
print "}\n";
print "\n";
print "Transform {\n";
print "	 rotation 0.0 1.0 0.0 1.57079\n";
print "	 children [\n";
print "	   USE MainWall\n";
print "	   Transform {\n";
print "	     rotation 0.0 1.0 0.0 1.57079\n";
print "	     children [\n";
print "	       USE MainWall\n";
print "	       Transform {\n";
print "		 rotation 0.0 1.0 0.0 1.57079\n";
print "		 children [\n";
print "		   USE MainWall\n";
print "		 ]\n";
print "	       }\n";
print "	     ]\n";
print "	   }\n";
print "	 ]\n";
print "}\n";
#
#########################################################
# 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.0) { $facx = 0; $facz = 1; }
	    else { $facx = 1; $facz = 0; }
	}
	else {
	    if ($i==$numele) {
		$dx = $_[$numele-1][0]-$_[$numele][0];
		if (abs($dx) > 0.0) { $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];
	}
    }
#    print "\n\nPrinting Extrusion\n";
#    for $i (0 .. $numele) {
#	print "$i : $surface[$i][0][0],$surface[$i][0][1],$surface[$i][0][2]\n";
#    }
#    print "\n";
    @surface;
}
#
#########################################################
# print VRML file
sub printExtrusion {
    my @coord = @_;
    my $numele = $#_-2;
    my $numcross = $_[$#_-1];
    my $maxht = $_[$#_];

    for $i (0 .. $numele-1) {
	$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) ) {
	    print " DEF WALL$j Transform {\n";
	    printf "  translation %7.2f %7.2f %7.2f\n",$t[0],$t[1],$t[2];
	    print "  children Shape {\n";
	    print "    appearance BRICK256$tag[$j1] { }\n";
	    print "    geometry IndexedLineSet {\n";
	    print "      coord Coordinate {\n";
	    $xc = $t[0] + ($coord[$i][$j][0] +  $coord[$i1][$j][0] + $coord[$i1][$j1][0] +  $coord[$i][$j1][0])/4.0;
	    $yc = $t[1] + ($coord[$i][$j][1] +  $coord[$i1][$j][1] + $coord[$i1][$j1][1] +  $coord[$i][$j1][1])/4.0;
	    $zc = $t[2] + ($coord[$i][$j][2] +  $coord[$i1][$j][2] + $coord[$i1][$j1][2] +  $coord[$i][$j1][2])/4.0;
	    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, 0, -1 ]\n";
	    print "    }\n";
	    print "   }\n";
	    print " }\n";
	    print " Transform {\n";
	    print "  scale 0.75 0.75 0.75\n";
	    print "  center $xc $yc $zc\n";
	    print "  children USE WALL$j\n";
	    print " }\n";	
	    print " Transform {\n";
	    print "  scale 0.5 0.5 0.5\n";
	    print "  center $xc $yc $zc\n";
	    print "  children USE WALL$j\n";
	    print " }\n";	
	    print " Transform {\n";
	    print "  scale 0.25 0.25 0.25\n";
	    print "  center $xc $yc $zc\n";
	    print "  children USE WALL$j\n";
	    print " }\n\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;
    print "Printing Spine\n";
    print "$spine[$numele][0] $spine[$numele][1] $spine[$numele][2],\n";
    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];
        print "$spine[$numele][0] $spine[$numele][1] $spine[$numele][2],\n";
    }
    print "\n";
    @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
    print "Transform {\n";
    printf "  translation %7.2f %7.2f %7.2f\n",$t[0],$t[1],$t[2];
    print "  children Shape {\n";
    print "    appearance BRICK2560 { }\n";
    print "    geometry DEF SIDE IndexedLineSet {\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 "    }\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 Shape {\n";
    print "    appearance BRICK2560 { }\n";
    print "    geometry DEF SIDE IndexedLineSet {\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 "    }\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;

	print "Transform {\n";
	print "  translation $t[0] $t[1] $t[2]\n";
	print "  children Shape {\n";
	print "    appearance BRICK2560 { }\n";
	print "    geometry DEF TOP IndexedLineSet {\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 "    }\n";
	print "  }\n";
	print "}\n";
    }
}
