#!/usr/bin/perl
############################################
# This script will reflect the given spine into four quardrants.
# Assumption : spine given in first quadrant (+ve,+ve region)
############################################


$flnm = $ARGV[0];
print "Reading input from $flnm\n";

open (SPINE, $flnm);
# first line gives translation parameters
$line = <SPINE>;
@pcs = split(' ',$line);
$t[0] = $pcs[0];
$t[1] = $pcs[1];
$t[2] = $pcs[2];

# blank Line
$line = <SPINE>;

# Definition of spine (quarter section)
$numele = -1;
do {
    $line = <SPINE>;
    chomp $line;
    @pcs = split(' ',$line);
    if ($#pcs > 0) {
	$numele ++;
	$spine[$numele][0] = $pcs[0];
	$spine[$numele][1] = $pcs[1];
    }
} while $#pcs > 0 ;
print "\nNumber of elements in spine $#spine \n";

close SPINE ;

print " translation $t[0] $t[1] $t[2]\n";
# Generate spines for each quadrant
@first = &genquadrant(@spine,1);
@second= &genquadrant(@spine,2);
@third = &genquadrant(@spine,3);
@fourth= &genquadrant(@spine,4);

for $i (0 .. $#first) {
    print "$first[$i][0] $first[$i][1] $first[$i][2],\n";
}
print"\n";
for $i (1 .. $#second) {
    $second[$i][0] += $first[$#first][0];
    $second[$i][1] += $first[$#first][1];
    $second[$i][2] += $first[$#first][2];
    print "$second[$i][0] $second[$i][1] $second[$i][2],\n";
}
print"\n";
for $i (1 .. $#third) {
    $third[$i][0] += $second[$#second][0];
    $third[$i][1] += $second[$#second][1];
    $third[$i][2] += $second[$#second][2];
    print "$third[$i][0] $third[$i][1] $third[$i][2],\n";
}
print"\n";
for $i (1 .. $#fourth) {
    $fourth[$i][0] += $third[$#third][0];
    $fourth[$i][1] += $third[$#third][1];
    $fourth[$i][2] += $third[$#third][2];
    print "$fourth[$i][0] $fourth[$i][1] $fourth[$i][2],\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;
}
