#!/usr/bin/perl

print "#VRML V2.0 utf8\n\n";

$flnm = $ARGV[0];

open (SPINE, $flnm);
$line = <SPINE>;
print "#$line";

$line = <SPINE>;
@pcs = split(' ',$line);
$Section = $pcs[0];

#blank line
$line = <SPINE>;

# first line gives first translation parameters
$line = <SPINE>;
@pcs = split(' ',$line);
$T1[0] = $pcs[0];
$T1[1] = $pcs[1];
$T1[2] = $pcs[2];
# second line gives second translation parameters
$line = <SPINE>;
@pcs = split(' ',$line);
$T2[0] = $pcs[0];
$T2[1] = $pcs[1];
$T2[2] = $pcs[2];

# blank Line
$line = <SPINE>;

# Give initial translation
$line = <SPINE>;
@pcs = split(' ',$line);
$ti[0] = $pcs[0];
$ti[1] = $pcs[1];

# blank Line
$line = <SPINE>;

# Height of relief
$line = <SPINE>;
@pcs = split(' ',$line);
$height = $pcs[0];

# 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];
	$numrelief[$numele] = $pcs[2];
    }
} while $#pcs > 0 ;
#print "\nNumber of elements in outer spine $#spine \n";

close SPINE ;

#########################################################
# reflect spine into four quadrants

# Generate spines for each quadrant
@first = &genquadrant(@spine,$Section);

# Modulate using the initial translation parameters
@firstex = &Modulate(@first,$Section);

#########################################################
# Print cross with Modulated spine

print "PROTO NICHETEXTURE1 [ ] {\n";
print "  Appearance {\n";
print "    texture     ImageTexture { url \"IMG/2555.1.512.png\" }\n";
print "  }\n";
print "}\n";

print "PROTO NICHETEXTURE2 [ ] {\n";
print "  Appearance {\n";
print "    texture     ImageTexture { url \"IMG/2555.1.256.png\" }\n";
print "  }\n";
print "}\n";


if ($Section == 1) {
    print "# Relief First quadrant\n";
    $t[0] = $T1[0];
    $t[1] = $T1[1];
    $t[2] = $T1[2];
}
if ($Section == 2) {
    print "# Relief Second quadrant\n";
    $t[0] =  $T2[0];
    $t[1] =  $T2[1];
    $t[2] = -$T2[2];
}
if ($Section == 3) {
    print "# Relief Third quadrant\n";
    $t[0] = -$T1[0];
    $t[1] =  $T1[1];
    $t[2] = -$T1[2];
}
if ($Section == 4) {
    print "# Relief Fourth quadrant\n";
    $t[0] = -$T2[0];
    $t[1] =  $T2[1];
    $t[2] =  $T2[2];
}
&printRelief(@firstex,$Section);

#
#########################################################
# Generate Modulated spine
sub Modulate {
    my @surface ;
    my $numele = $#_ - 1;
    my $flag = $_[$#_];

#    print "Into Modulate with $numele $#cross and $flag\n";

    for $i (0 .. $numele) {
	if ($flag == 1){
	    if ($i==0) {
		$dx = $_[0][0]-$_[1][0];
		if ($dx != 0.0) { $facx = 0; $facz = 1; }
		else { $facx = 1; $facz = 0; }
	    }
	    else {
		if ($i==$numele) {
		    $dx = $_[$numele-1][0]-$_[$numele][0];
		    if ($dx != 0.0) { $facx = 0; $facz = 1; }
		    else { $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;}
	    }
	}
	$surface[$i][0] = $_[$i][0] + $facx*$ti[1];;
	$surface[$i][1] = $_[$i][1] + $ti[0];;
	$surface[$i][2] = $_[$i][2] + $facz*$ti[1];;
    }
    @surface;
}
#
#########################################################
# print VRML file
sub printRelief {
    my @coord = @_;
    my $numele = $#_-2;
    my $tag = $coord[$#_];
    my $len;
    my $reliefs;
    my $stepx;
    my $stepz;

    my $picture = $StartImg-1;
    for $nsec (0 .. $numele) {
	$reliefs = $numrelief[$nsec];

	$reliefSteps = $reliefs + 1;

	$lenx = abs($coord[$nsec+1][0] - $coord[$nsec][0]);
	$lenz = abs($coord[$nsec+1][2] - $coord[$nsec][2]);

	$fracx = $lenx / $reliefs;
	$fracz = $lenz / $reliefs;
	if ($coord[$nsec+1][0] < $coord[$nsec][0]) { $fracx = -$fracx; }
	if ($coord[$nsec+1][2] < $coord[$nsec][2]) { $fracz = -$fracz; }

	print "PROTO NICHE$tag$nsec [ ] {\n";
	print "  IndexedFaceSet {\n";
	print "    solid FALSE\n";
	print "    coord Coordinate {\n";
	print "      point [\n";
	print "         0.0 0.0 0.0,\n";
	print "         $fracx 0.0 $fracz,\n";
	print "         $fracx $height $fracz,\n";
	print "         0.0 $height 0.0 \n";
	print "      ]\n";
	print "    }\n";
	print "    coordIndex [ 0, 1, 2, 3, -1 ]\n";
	print "    texCoord TextureCoordinate {\n";
	print "      point [ 0 0, 1 0, 1 1, 0 1 ]\n";
	print "    }\n";
	print "  }\n";
	print "}\n";

	$stepx = 0.0;
	$stepz = 0.0;
	for $nrel (0 .. $reliefs-1 ) {
	    $pstepx = $stepx;
	    $pstepz = $stepz;
	    $stepx += $fracx;
	    $stepz += $fracz;

	    $x0 = $coord[$nsec][0] + $pstepx;
	    $y0 = $coord[$nsec][1];
	    $z0 = $coord[$nsec][2] + $pstepz;

	    $x1 = $coord[$nsec][0] + $stepx;
	    $y1 = $coord[$nsec][1];
	    $z1 = $coord[$nsec][2] + $stepz;

	    $x2 = $coord[$nsec][0] + $stepx;
	    $y2 = $coord[$nsec][1] + $height;
	    $z2 = $coord[$nsec][2] + $stepz;

	    $x3 = $coord[$nsec][0] + $pstepx;
	    $y3 = $coord[$nsec][1] + $height;
	    $z3 = $coord[$nsec][2] + $pstepz;

	    $picture ++;
	    print "#NICHE $tag:$nsec.$nrel\n";
	    print "Transform {\n";
	    $tx = $x0 + $t[0];
	    $ty = $y0 + $t[1];
	    $tz = $z0 + $t[2];
	    print " translation $tx $ty $tz\n";
	    print " children LOD {\n";
	    $xc = $fracx/2.0;
	    $yc = $height/2.0;
	    $zc = $fracz/2.0;
	    print "   center $xc $yc $zc\n";
	    print "   range [10 30]\n";
	    print "   level [\n";
	    print "    Shape {\n";
	    print "      appearance NICHETEXTURE1 { }\n";
	    print "      geometry NICHE$tag$nsec { }\n";
	    print "    }\n";
	    print "    Shape {\n";
	    print "      appearance NICHETEXTURE2 { }\n";
	    print "      geometry NICHE$tag$nsec { }\n";
	    print "    }\n";
	    print "    Shape {\n";
	    print "      appearance Appearance {\n";
	    print "        material Material {\n";
	    print "          diffuseColor 0.2 0.2 0.2\n";
	    print "          emissiveColor 0.1 0.1 0.1\n";
	    print "        }\n";
	    print "      }\n";
	    print "      geometry NICHE$tag$nsec { }\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;
}
#
#########################################################




