#!/usr/bin/perl 


# constellations script modified by arlduc (arlduc@amnh.org)
# this script pulls the requested constellation from "constellations.speck"
# and turns it into a MEL file. Then it pulls each constellation-star's color, 
# size, and luminance from "stars.speck"
# Open constellations.speck to find the constellation's abbreviation--
# for instance, Orion = Ori
# to download the Digital Universe atlas that includes constellations.speck, 
# go to http://haydenplanetarium.org/universe/

# using Stuart's magnitude-to-luminance formula  (NOT SURE IT'S CORRECT TO USE)

my ($constName, $x, $y, $z, $HipName, $lambertNumber, $StarFirstName, $StarLastName, $CONST_OUT, @ConstStars, $StarsNum, $partOfConst);

$sizefac = .1;
$lum_thresh = 0.20;  
$lambertNumber = 1;
$HipName= "";
$StarsNum = 1;
$partOfConst = 0;

open ( CONST_IN, "constellations.speck");
open (STARS_IN, "stars.speck");
open ( CONST_OUT, ">And.mel" );
print STDOUT ("what should the MEL file be called? \n ");
chop ($CONST_OUT2 = <STDIN>);
#$CONST_OUT2= join '', ">", $CONST_OUT2;

if (rename "And.mel", $CONST_OUT2) {
	print STDOUT "WORKING\n";
}
else {print STDOUT "NOT WORKING\n";}

print STDOUT ("what constellation? (enter abbreviation) \n");
chop($constName = <STDIN>);

print CONST_OUT ("CreateEmptyGroup;\n");
print CONST_OUT ("rename \"$constName\";\n\n");
print CONST_OUT ("CreateEmptyGroup;\n");
print CONST_OUT ("rename \"OtherStars\";\n\n");
print CONST_OUT ("setAttr \"persp.translateX\" 0;\n");print CONST_OUT ("setAttr \"persp.translateY\" 0;\n");print CONST_OUT ("setAttr \"persp.translateZ\" 0;\n");print CONST_OUT ("setAttr \"persp.rotateX\" 0;\n\n");

#while-loop to go through each line of the dataset
while (defined($line = <CONST_IN>)) {
        $x = "yo";
	$line =~ s/^\s+//;
	($x, $y, $z, $pound, $HipName, $StarFirstName, $StarLastName) = split(/\s+/, $line);
	
	#if-struc to check if the line has a constellation name
	if ($x eq "#") {
		chop $y;

		#if-struc to check if the constellation name is the one we want
		if ($y eq $constName)  {$partOfConst = 1;}
		else {$partOfConst = 0;}
	}

	#if struc to negatively-check if the line contains xyz coordinates 
	if (($x ne "#") && ($x ne "mesh") && ($x ne "}") && ($x ne "1") && ($x != 0) && ($partOfConst == 1)) {		    				
		chop $HipName;
		$HipName = join 'HIP', split /Hip:/, $HipName;	
		$putItIn = 1;
		
		#for loop to check if the  star is a copy of one we already added
		for ($i=1; $i<$StarsNum; $i++) {
			if ($ConstStar[$i]{HipName} eq $HipName) {
				$putItIn = 0;
			}
		}
	
		#if-struc to print star to MEL file if it's not a copy
		if ($putItIn == 1) {
			$ConstStar[$StarsNum]{HipName} = $HipName;
			$ConstStar[$StarsNum]{x} = $x;
			$ConstStar[$StarsNum]{y} = $y;
			$ConstStar[$StarsNum]{z} = $z;
			$StarsNum++;
		}
	}
}


while (defined($line = <STARS_IN>)) {
	$line =~ s/^\s+//;
	($x, $y, $z, $colorb_v, $lum, $absmag, $appmag, $txno, $dist, $dcalc, $plx, $hipno, $pound, $Name) = split(/\s+/, $line);
	
	#if struc to negatively-check if the line contains xyz coordinates 
	if (($x ne "#") && ($x ne "datavar") && ($x ne "texturevar") && ($x ne "texture") && ($x ne "maxcomment")) {

		$lum = 100.0 ** (-1.0 * $absmag / 5.0);

		#if ($lum > $lum_thresh) {

	    		$size = sqrt($lum) * $sizefac;			
		    	$inConstellation = 0;

			#for loop to check if star is part of constellation
			for ($i=1; $i<$StarsNum; $i++) {

				#if-struc to print if there's a match
				if ($ConstStar[$i]{HipName} eq $Name) {
					printf CONST_OUT ("polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -tx 1 -ch 0 -name \"%s\";\n", $Name);
	   				printf CONST_OUT ("scale -r %g %g %g;\n", $size, $size, $size);
	    				printf CONST_OUT ("move -r %s %s %s;\n", $x, $y, $z);
					$lambertNumber++;
					printf CONST_OUT ("shadingNode -asShader lambert;\n");
					printf CONST_OUT ("sets -renderable true -noSurfaceShader true -empty -name lambert%gSG;\n", $lambertNumber);	    				printf CONST_OUT ("connectAttr -f lambert%g.outColor lambert%gSG.surfaceShader;\n", $lambertNumber, $lambertNumber);	   		 		printf CONST_OUT ("select -r lambert%g ;\n", $lambertNumber);
	    				printf CONST_OUT ("select -r %s;\n", $Name);	    				printf CONST_OUT ("sets -e -forceElement lambert%gSG;\n", $lambertNumber);

					printf STDOUT ("%s is a part of %s\n", $Name, $constName);
					#printf CONST_OUT ("setAttr \"lambert%g.glowIntensity\" 1.0;\n", $lambertNumber);
					printf CONST_OUT ("setAttr \"lambert%g.color\" -type double3 1 1 1;\n", $lambertNumber);
					printf CONST_OUT ("setAttr \"lambert%g.ambientColor\" -type double3 0 0 0 ;\n", $lambertNumber);					printf CONST_OUT ("setAttr \"lambert%g.incandescence\" -type double3 0.31008 0.31008 0.31008 ;\n", $lambertNumber);
	    				printf CONST_OUT ("parent -r \"%s\" \"%s\";\n\n\n", $Name, $constName );
					$inConstellation = 1;
				}
			}

			#if  ($inConstellation == 0) { printf CONST_OUT ("parent -r \"%s\" \"OtherStars\";\n\n\n", $Name);}
		#}
	}
    	


}


#for-loop to print the list of star names
#for ($i=1; $i<$StarsNum; $i++) {
#	printf STDOUT ("%s %s %s %s \n", $ConstStar[$i]{HipName}, $ConstStar[$i]{x}, $ConstStar[$i]{y}, $ConstStar[$i]{z});
#}

printf STDOUT ("written to specified MEL file.\n");
