#!/usr/bin/perl

print "#VRML V2.0 utf8\n\n";

# Read the images numbers for basement reliefs
# The images may not be in sequence
open (IMGINFO, "Relief.Info");
$nimg = -1;
do {
    $line = <IMGINFO>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs >= 0) {
	$nimg ++;
	$Imgno[$nimg] = $pcs[0];
    }
} while $#pcs >= 0 ;
#print "\nNumber of Images $#Imgno \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 line1
$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];
	for $rw (0 .. $numrelief[$numele]-1) {
#	    $relief_width[$numele][$rw] = $pcs[$rw+3];
	    $relief_width[$numele][$rw] = 1;
	}
    }
} 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,4);
#@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,4);
#@firstex = &Modulate(@first,$Section);
#@secondex= &Modulate(@second,2);
#@thirdex = &Modulate(@third,3);
#@fourthex= &Modulate(@fourth,4);

#########################################################
# Print cross with Modulated spine

if ($StepSize > 0.0) {
    print "PROTO CORNICETEXTURE [ ] {\n";
    print "  Appearance {\n";
    print "    texture     ImageTexture { url \"../../../IMG/128/decoration1.jpg\" }\n";
    print "    textureTransform    TextureTransform { scale 1 1}\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;

    $flnm = ">ViewPoints";
    open (VP,$flnm);
    my $nsec = $numele + 1;
    for $nsec1 (0 .. $numele) {
	$nsec -= 1;
	$reliefs = $numrelief[$nsec];

	$panel_width = 0.0;
	for $rw (0 .. $reliefs-1) {
	    $panel_width = $panel_width + $relief_width[$nsec][$rw];
	}

	$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;
	$O_fracx = $Ulengthx / $panel_width;
	if ($coord[$nsec+1][0] < $coord[$nsec][0]) {
	    $O_fracx = -$O_fracx;
	    $StepSizex = -$StepSizex;
	}
# $fracz = $Ulengthz / $reliefs;
	$O_fracz = $Ulengthz / $panel_width;
	if ($coord[$nsec+1][2] < $coord[$nsec][2]) {
	    $O_fracz = -$O_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 += ($O_fracx*$relief_width[$nsec][$nrel/2]);
		$stepz += ($O_fracz*$relief_width[$nsec][$nrel/2]);
	    }
	    $fracx = $O_fracx*$relief_width[$nsec][$nrel/2];
	    $fracz = $O_fracz*$relief_width[$nsec][$nrel/2];

	    $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) {
		if ($StepSize > 0.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 {
		$tx = $x0 + $t[0];
		$ty = $y0 + $t[1];
		$tz = $z0 + $t[2];
		$picture ++;
		$jpg = ".jpg";
		$wrl = ".wrl";
		$html = ".html";
		$pictureU = $Imgno[$picture];
# if ($picture%2 == 0) {
# $wrl = "a.wrl";
# $html= "a.html";
# }
# else {
# $wrl = "b.wrl";
# $html= "b.html";
# }
		print "Transform {\n";
		$tx = $x0 + $t[0];
		$ty = $y0 + $t[1];
		$tz = $z0 + $t[2];
		print " translation $tx $ty $tz\n";
		print " children LOD { range [10.0]  level [\n";	
		$flnm = "WRL/rel-$tag\.$nsec\.$nrel$wrl";
		print "    Inline { url \"$flnm\" }\n";
		print "    Transform { }\n";
		print " ] }\n";
		print "}\n";
		$flnm = ">$flnm";
		open (RELIEF,$flnm);
		 $h0=0.0;
		 $h1=$height/2.0;
		 $fz=$fracz/3.0;
		 $fx=$fracx/3.0;
		 $fz2=$fracz/2.0;
		 $fx2=$fracx/2.0;
		 $fy2 = $ty+$h1;
		 $flnm = "rel-$tag\.$nsec\.$nrel$html";
		 open (HTML,">WRL/$flnm");
		  print HTML "<html><head></head><body>\n";
                  print HTML "<A HREF=\"../Q$Section.wrl#U_$pictureU\"> Jump directly to the corresponding gallery section</A>\n";
		  print HTML "<BR>\n<IMG SRC=\"../../Reliefs/$pictureU$jpg\">\n</body>\n</html>\n";
		 close HTML ;
		 print RELIEF "#VRML V2.0 utf8\n";
		 print VP "DEF U_$pictureU Viewpoint {\n";
		 print VP "	description \"$pictureU\"\n";
		 if ($fz > 0.0) {
		     $fx2 = $tx+2.0;
		     $fz2 = $tz+$fz2;
		     print VP "	orientation 0 1 0 1.57\n";
		 }
		 else {
		     $fz2 = $tz-2.0;
		     $fx2 = $tx+$fx2;
		     print VP "	orientation 0 1 0 3.14\n";
		 }
		 print VP "	position    $fx2 $fy2 $fz2\n";
		 print VP "}\n";
		 print RELIEF "Anchor {\n";
                 print RELIEF " url \"$flnm\"\n";
                 print RELIEF " parameter \"target=frame2\"\n";
                 print RELIEF " children [\n";
                 print RELIEF "	 Shape {\n";
                 print RELIEF "	   appearance Appearance { material Material {transparency 1.0} }\n";
                 print RELIEF "	   geometry IndexedFaceSet {\n";
                 print RELIEF "	     solid FALSE\n";
                 print RELIEF "	     coord Coordinate {\n";
                 print RELIEF "	      point [\n";
		 print RELIEF "	       0.0 $h0 0.0,\n";
		 print RELIEF "	       $fx $h0 $fz,\n";
		 print RELIEF "	       $fx $h1 $fz,\n";
		 print RELIEF "	       0.0 $h1 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";
                 print RELIEF " ]\n";
                 print RELIEF "}\n";
		 print RELIEF "Shape {\n";
		 print RELIEF "  appearance Appearance { \n";
		 print RELIEF "    texture     ImageTexture {\n";
		 print RELIEF "      url       \"../IMG/$pictureU$jpg\"\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 $h0 0.0,\n";
		 print RELIEF "	   $fracx $h0 $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 [ 1 0, 0 0, 0 1, 1 1 ]\n";
		 print RELIEF "    }\n";
		 print RELIEF "  }\n";
		 print RELIEF "}\n";
		close RELIEF ;

	    }
	}
    }
    close VP;
}
#
#########################################################
# 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;
}
#
#########################################################






