#!/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];
$StepSize = $pcs[1];
$StartImg = $pcs[2];

#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

#print "Creating spine copies for remaining quadrants\n";

# Generate spines for each quadrant
@first = &genquadrant(@spine,$Section);
#@second= &genquadrant(@spine,2);
#@third = &genquadrant(@spine,3);
#@fourth= &genquadrant(@spine,4);

# Modulate using the initial translation parameters
@firstex = &Modulate(@first,$Section);
#@secondex= &Modulate(@second,2);
#@thirdex = &Modulate(@third,3);
#@fourthex= &Modulate(@fourth,4);

#########################################################
# Print cross with Modulated spine

print "PROTO CORNICETEXTURE [ ] {\n";
print "  Appearance {\n";
print "    texture     ImageTexture { url \"../../IMG/64/onecornice.jpg\" }\n";
print "    textureTransform    TextureTransform { scale 1 3}\n";
#print "    material Material {\n";
#print "      diffuseColor 0.5 0.5 0.4\n";
#print "      emissiveColor 0.7 0.7 0.65\n";
#print "    }\n";
print "  }\n";
print "}\n";

print "\n\n#DEFINE CORNICE\n";
print "PROTO CORNICE [ ] {\n";
print "  Shape {\n";
print "    appearance CORNICETEXTURE { }\n";
print "    geometry IndexedFaceSet {\n";
print "    	 solid FALSE\n";
print "    	 coord Coordinate {\n";
print "    	   point [\n";
print "    	      0.0 0.0 0.0,\n";
print "    	      $StepSize 0.0 0.0,\n";
print "    	      $StepSize $height 0.0,\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";
print "}\n\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);

#&printRelief(@secondex,2);
#
#&printRelief(@thirdex,3);
#
#&printRelief(@fourthex,4);

#
#########################################################
# 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]);

	$Ulengthx = 0.0;
	$Ulengthz = 0.0;

	if ($lenx != 0.0) { $Ulengthx = $lenx - ($reliefSteps*$StepSize); }
	if ($lenz != 0.0) { $Ulengthz = $lenz - ($reliefSteps*$StepSize); }

	if ($lenx > $lenz) {
	    $StepSizex = $StepSize;
	    $StepSizez = 0.0;
	}
	else {
	    $StepSizex = 0.0;
	    $StepSizez = $StepSize;
	}

	$fracx = $Ulengthx / $reliefs;
	if ($coord[$nsec+1][0] < $coord[$nsec][0]) {
	    $fracx = -$fracx;
	    $StepSizex = -$StepSizex;
	}
	$fracz = $Ulengthz / $reliefs;
	if ($coord[$nsec+1][2] < $coord[$nsec][2]) {
	    $fracz = -$fracz;
	    $StepSizez = -$StepSizez;
	}

	$stepx = 0.0;
	$stepz = 0.0;
	for $nrel (0 .. (2*$reliefs) ) {
	    $pstepx = $stepx;
	    $pstepz = $stepz;
	    if ($nrel%2 == 0) {
		$stepx += $StepSizex;
		$stepz += $StepSizez;
	    }
	    else {
		$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;

	    if ($nrel%2 == 0) {
		print "Transform {\n";
		$tx = $x0 + $t[0];
		$ty = $y0 + $t[1];
		$tz = $z0 + $t[2];
		print " translation $tx $ty $tz\n";
		$dz = $z0-$z1;
		if ($dz > 0.0) {
		    print " rotation 0.0 1.0 0.0 1.57\n";
		}
		else {
		    if ($dz < 0.0) {
			print " rotation 0.0 1.0 0.0 -1.57\n";
		    }
		    else {
			$dx = $x0-$x1;
			if ($dx > 0.0) {
			    print " rotation 0.0 1.0 0.0 -3.14\n";
			}
		    }
		}
		print " children  CORNICE { }\n";
		print "}\n";
	    }
	    else {
		$picture ++;
# if ($picture > $EndImg) { $picture = $StarImg; }
		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";
		$flnm = "WRL/rel-".$tag.".".$nsec.".".$nrel."a.wrl";
		print "    Inline { url \"$flnm\" }\n";
		$flnm = ">".$flnm;
		open (RELIEF,$flnm);
		  print RELIEF "#VRML V2.0 utf8\n";
		  print RELIEF "    Shape {\n";
		  print RELIEF "      appearance Appearance { \n";
		  print RELIEF "        texture     ImageTexture {\n";
		  print RELIEF "          url       \"../512/$picture.jpg\"\n";
		  print RELIEF "        }\n";
#		  print RELIEF "        material Material {\n";
#		  print RELIEF "          diffuseColor 0.5 0.5 0.4\n";
#		  print RELIEF "          emissiveColor 0.7 0.7 0.65\n";
#		  print RELIEF "        }\n";
		  print RELIEF "      }\n";
		  print RELIEF "      geometry IndexedFaceSet {\n";
		  print RELIEF "        solid FALSE\n";
		  print RELIEF "        coord Coordinate {\n";
		  print RELIEF "    	point [\n";
		  print RELIEF "    	   0.0 0.0 0.0,\n";
		  print RELIEF "    	   $fracx 0.0 $fracz,\n";
		  print RELIEF "    	   $fracx $height $fracz,\n";
		  print RELIEF "    	   0.0 $height 0.0 \n";
		  print RELIEF "    	]\n";
		  print RELIEF "        }\n";
		  print RELIEF "        coordIndex [ 0, 1, 2, 3, -1 ]\n";
		  print RELIEF "        texCoord TextureCoordinate {\n";
		  print RELIEF "    	point [ 0 0, 1 0, 1 1, 0 1 ]\n";
		  print RELIEF "        }\n";
		  print RELIEF "      }\n";
		  print RELIEF "    }\n";
		close RELIEF ;
		$flnm = "WRL/rel-".$tag.".".$nsec.".".$nrel."b.wrl";
		print "    Inline { url \"$flnm\" }\n";
		$flnm = ">".$flnm;
		open (RELIEF,$flnm);
		  print RELIEF "#VRML V2.0 utf8\n";
		  print RELIEF "    Shape {\n";
		  print RELIEF "      appearance Appearance { \n";
		  print RELIEF "        texture     ImageTexture {\n";
		  print RELIEF "          url       \"../128/$picture.jpg\"\n";
		  print RELIEF "        }\n";
#		  print RELIEF "        material Material {\n";
#		  print RELIEF "          diffuseColor 0.5 0.5 0.4\n";
#		  print RELIEF "          emissiveColor 0.7 0.7 0.65\n";
#		  print RELIEF "        }\n";
		  print RELIEF "      }\n";
		  print RELIEF "      geometry IndexedFaceSet {\n";
		  print RELIEF "        solid FALSE\n";
		  print RELIEF "        coord Coordinate {\n";
		  print RELIEF "    	point [\n";
		  print RELIEF "    	   0.0 0.0 0.0,\n";
		  print RELIEF "    	   $fracx 0.0 $fracz,\n";
		  print RELIEF "    	   $fracx $height $fracz,\n";
		  print RELIEF "    	   0.0 $height 0.0 \n";
		  print RELIEF "    	]\n";
		  print RELIEF "        }\n";
		  print RELIEF "        coordIndex [ 0, 1, 2, 3, -1 ]\n";
		  print RELIEF "        texCoord TextureCoordinate {\n";
		  print RELIEF "    	point [ 0 0, 1 0, 1 1, 0 1 ]\n";
		  print RELIEF "        }\n";
		  print RELIEF "      }\n";
		  print RELIEF "    }\n";
		close RELIEF ;
		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 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";
		print "   ]\n";
		print " }\n";
		print "}\n";
	    }
	}
    }
}
#
#########################################################
# Generate quadrants
sub genquadrant {
    my @temp;
    my @spine;
    my $numele = $#_ - 1;
    my $flag = $_[$#_];

#    print "Number of elements are $numele $flag\n";
#    for $i (0 .. $numele) {
#	print "$i : $_[$i][0] $_[$i][1]\n";
#    }

    $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;
}
#
#########################################################






