######################################################################################################################
#!/usr/local/bin/perl
######################################################################################################################
# dymaxion sphere generation script currently parses data sets dervived from Simon Tatman's Polyhedra: 
# http://www.chiark.greenend.org.uk/~sgtatham/polyhedra/polyhedra.tar.gz
######################################################################################################################
use Switch;

if ( $#ARGV < 0 )
   {
    print "syntax: perl povdymaxiongen.pl < resolution >\n";
    exit(-1);
   }

# adding what to draw ???
print "syntax: perl povdymaxiongen.pl < resolution >\n";

$resolution= $ARGV[0];  

$filename= "sphere.pov";          # Name the pov file
open (FILE, ">$filename");
flock(FILE, $exclusive_lock);

###########################################################
###########################################################
###########################################################
# the povray template
###########################################################
print FILE "global_settings {\n";
print FILE "                 assumed_gamma 1.5\n";
print FILE "                 noise_generator 2\n";
print FILE "                }\n";

print FILE "light_source {\n";
print FILE "              <4, 5, -5>, rgb <1, 1, 1>\n";
print FILE "             }\n";

print FILE "light_source {\n";
print FILE "             <4, 5, -5>, rgb <1, 1, 1>\n";
print FILE "             }\n";

print FILE "light_source {\n";
print FILE "             <-6, 5, 6>, rgb <1, 1, 1>\n";
print FILE "             }\n";

print FILE "camera {\n";
print FILE "        location <-1.5, 1.5, 1.5>\n";
print FILE "        look_at <0, 0, 0>\n";
print FILE "       }\n\n";

#print FILE plane {\n";
#print FILE "      y, -5\n";
#print FILE "      pigment {color <0,0,0.5>}\n";
#print FILE "      finish {reflection 0.3}\n";
#print FILE "     }\n";
###########################################################
###########################################################
###########################################################

##########################################################################################################################
# center, packed emanating sphere --
# print FILE "            sphere { < 0, 0, 0 >, 0.99\n";
# print FILE "                     texture {pigment { rgb <1, 1, 1> } } translate < 0, 0, 0 >}\n";
##########################################################################################################################

open(VERTEX, "./vertices") or die "cannot open vertices file!";
@lines = <VERTEX>;
$pts=0;
foreach $content(@lines)
        {
         # this is a hash of 60 elements
         @elements= split(/ /, $content);
         $axis=0;
         foreach $coord(@elements)
                 {
                  chop $coord;
                  # x, y, z
                  switch ($axis)
                         { 
                          case 2 
                                { 
                                 $p[$axis]=$coord;
                                }
                          case 3 
                                { 
                                 $p[$axis]=$coord;
                                }
                          case 4 
                                { 
                                 $p[$axis]=$coord;
                                }
                         }
                  $axis++;
                 }
         $point[$pts]="<".$p[2].", " .$p[3].", ".$p[4].">";
         $dpoint[$pts]=$p[2]." ".$p[3]." ".$p[4];

         # print "$point[$pts]\n"; # draw each sphere here...

         # printf FILE ("sphere { $point[$pts], 0.025 pigment { rgb <1, 1, 1> } }\n"); }
         $pts++; # count vertices
        }
close (VERTEX);

##########################################################################################################################
#$resolution=1000;
open(FACES, "./faces") or die "cannot open faces file!";
@lines = <FACES>;

$tri=0;
foreach $content(@lines)
        {
         @elements= split(/ /, $content);
         $ends=0;
         foreach $endpointnumbers(@elements)
                 {
                  chomp $endpointnumbers;
                  switch ($ends)
                         { 
                          case 0 { $a=$endpointnumbers; } 
                          case 1 { $b=$endpointnumbers; } 
                         } 
                  $ends++;
                 }
         # find vector between a & b 
         @diffs= split(/ /, $dpoint[$a]);
         $sphere=0;
         foreach $coord(@diffs)
                 {
                  chomp $coord;
                  switch ($sphere)
                         { 
                          case 0 { $xa=$coord; } 
                          case 1 { $ya=$coord; } 
                          case 2 { $za=$coord; } 
                         } 
                  $sphere++;
                 }
         @diffs= split(/ /, $dpoint[$b]);
         $sphere=0;
         foreach $coord(@diffs)
                 {
                  chomp $coord;
                  switch ($sphere)
                         {
                          case 0 { $xb=$coord; }
                          case 1 { $yb=$coord; }
                          case 2 { $zb=$coord; }
                         }
                  $sphere++;
                 }

         switch ($tri)
            {
             case 0
                   {
                    $x1=$xa;
                    $y1=$ya;
                    $z1=$za;
                    $tri++;
                   }
             case 1
                   {
                    $x2=$xa;
                    $y2=$ya;
                    $z2=$za;
                    $tri++;
                   }
             case 2
                   {
                    $x3=$xb;
                    $y3=$yb;
                    $z3=$zb;
                    # calc. equicenter
                    $ex=($x1+$x2+$x3)/3;
                    $ey=($y1+$y2+$y3)/3;
                    $ez=($z1+$z2+$z3)/3;
                    printf FILE ("sphere { <$ex, $ey, $ez > 0.01 texture { pigment { rgb <1, 1, 1> } } }\n"); # draw equicenter
                    #
                    # draw lines to equicenter
                    #
                    $xdiff=$ex; $ydiff=$ey; $zdiff=$ez;
                    $x=0; $y=0; $z=0;
                    #
                    #for ($rodblob=1; $rodblob <= $resolution; $rodblob+=1) # draws extensors
                    #    {
                    #     $x+=($xdiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     $y+=($ydiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     $z+=($zdiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     printf FILE ("sphere { <$x, $y, $z > 0.005 texture { pigment { rgb <1, 1, 1> } } }\n");
                    #    }

                    ### a=>cen draw all connectors here

                    # calc. a=>cen diff
                    
                    if ( ($x1 gt $ex) && ($x1 gt 0) && ($ex gt 0) ) { $xdiff=($x1-$ex)*(-1); }
                    if ( ($ex gt $x1) && ($x1 gt 0) && ($ex gt 0) ) { $xdiff=($ex-$x1); }
                    if ( ($x1 gt $ex) && ($x1 lt 0) && ($ex lt 0) ) { $xdiff=($x1-$ex)*(-1); }
                    if ( ($ex gt $x1) && ($x1 lt 0) && ($ex lt 0) ) { $xdiff=($ex-$x1); }
                    if ( ($x1 gt $ex) && ($x1 gt 0) && ($ex lt 0) ) { $xdiff=($x1-$ex)*(-1); }
                    if ( ($ex gt $x1) && ($ex gt 0) && ($x1 lt 0) ) { $xdiff=($ex-$x1); }

                    if ( ($y1 gt $ey) && ($y1 gt 0) && ($ey gt 0) ) { $ydiff=($y1-$ey)*(-1); }
                    if ( ($ey gt $y1) && ($y1 gt 0) && ($ey gt 0) ) { $ydiff=($ey-$y1); }
                    if ( ($y1 gt $ey) && ($y1 lt 0) && ($ey lt 0) ) { $ydiff=($y1-$ey)*(-1); }
                    if ( ($ey gt $y1) && ($y1 lt 0) && ($ey lt 0) ) { $ydiff=($ey-$y1); }
                    if ( ($y1 gt $ey) && ($y1 gt 0) && ($ey lt 0) ) { $ydiff=($y1-$ey)*(-1); }
                    if ( ($ey gt $y1) && ($ey gt 0) && ($y1 lt 0) ) { $ydiff=($ey-$y1); }

                    if ( ($z1 gt $ez) && ($z1 gt 0) && ($ez gt 0) ) { $zdiff=($z1-$ez)*(-1); }
                    if ( ($ez gt $z1) && ($z1 gt 0) && ($ez gt 0) ) { $zdiff=($ez-$z1); }
                    if ( ($z1 gt $ez) && ($z1 lt 0) && ($ez lt 0) ) { $zdiff=($z1-$ez)*(-1); }
                    if ( ($ez gt $z1) && ($z1 lt 0) && ($ez lt 0) ) { $zdiff=($ez-$z1); }
                    if ( ($z1 gt $ez) && ($z1 gt 0) && ($ez lt 0) ) { $zdiff=($z1-$ez)*(-1); }
                    if ( ($ez gt $z1) && ($ez gt 0) && ($z1 lt 0) ) { $zdiff=($ez-$z1); }

                    $x=$x1; $y=$y1; $z=$z1;
                    for ($rodblob=1; $rodblob <= $resolution; $rodblob+=1) # draws a-> equicenter connector
                        {
                         $x+=($xdiff/$resolution);  # vector indicated in sign (+/-) of step
                         $y+=($ydiff/$resolution);  # vector indicated in sign (+/-) of step
                         $z+=($zdiff/$resolution);  # vector indicated in sign (+/-) of step
                         printf FILE ("sphere { <$x, $y, $z > 0.005 texture { pigment { rgb <1, 0, 0> } } }\n");
                        }

                    # calc. b=>cen diff
                    $x=$x2; $y=$y2; $z=$z2;
                    #for ($rodblob=1; $rodblob <= $resolution; $rodblob+=1) # draws a-> equicenter connector
                    #    {
                    #     $x+=($xdiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     $y+=($ydiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     $z+=($zdiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     printf FILE ("sphere { <$x, $y, $z > 0.005 texture { pigment { rgb <1, 1, 1> } } }\n");
                    #    }


                    # calc. c=>cen diff
                    $x=$x3; $y=$y3; $z=$z3;
                    #for ($rodblob=1; $rodblob <= $resolution; $rodblob+=1) # draws a-> equicenter connector
                    #    {
                    #     $x+=($xdiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     $y+=($ydiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     $z+=($zdiff/$resolution);  # vector indicated in sign (+/-) of step
                    #     printf FILE ("sphere { <$x, $y, $z > 0.005 texture { pigment { rgb <1, 1, 1> } } }\n");
                    #    }

                    $tri=0;
                   }
            }

         $xdiff=0; $ydiff=0; $zdiff=0;
         if ( ($xa gt $xb) && ($xa gt 0) && ($xb gt 0) ) { $xdiff=($xa-$xb)*(-1); }
         if ( ($xb gt $xa) && ($xa gt 0) && ($xb gt 0) ) { $xdiff=($xb-$xa); }
         if ( ($xa gt $xb) && ($xa lt 0) && ($xb lt 0) ) { $xdiff=($xa-$xb)*(-1); }     
         if ( ($xb gt $xa) && ($xa lt 0) && ($xb lt 0) ) { $xdiff=($xb-$xa); }
         if ( ($xa gt $xb) && ($xa gt 0) && ($xb lt 0) ) { $xdiff=($xa-$xb)*(-1); }          
         if ( ($xb gt $xa) && ($xb gt 0) && ($xa lt 0) ) { $xdiff=($xb-$xa); }  

         if ( ($ya gt $yb) && ($ya gt 0) && ($yb gt 0) ) { $ydiff=($ya-$yb)*(-1); }        
         if ( ($yb gt $ya) && ($ya gt 0) && ($yb gt 0) ) { $ydiff=($yb-$ya); }
         if ( ($ya gt $yb) && ($ya lt 0) && ($yb lt 0) ) { $ydiff=($ya-$yb)*(-1); }   
         if ( ($yb gt $ya) && ($ya lt 0) && ($yb lt 0) ) { $ydiff=($yb-$ya); }
         if ( ($ya gt $yb) && ($ya gt 0) && ($yb lt 0) ) { $ydiff=($ya-$yb)*(-1); }     
         if ( ($yb gt $ya) && ($yb gt 0) && ($ya lt 0) ) { $ydiff=($yb-$ya); }

         if ( ($za gt $zb) && ($za gt 0) && ($zb gt 0) ) { $zdiff=($za-$zb)*(-1); }      
         if ( ($zb gt $za) && ($za gt 0) && ($zb gt 0) ) { $zdiff=($zb-$za); }
         if ( ($za gt $zb) && ($za lt 0) && ($zb lt 0) ) { $zdiff=($za-$zb)*(-1); }
         if ( ($zb gt $za) && ($za lt 0) && ($zb lt 0) ) { $zdiff=($zb-$za); }
         if ( ($za gt $zb) && ($za gt 0) && ($zb lt 0) ) { $zdiff=($za-$zb)*(-1); }
         if ( ($zb gt $za) && ($zb gt 0) && ($za lt 0) ) { $zdiff=($zb-$za); }

         $intradius=0.954998137711105;
         $radius=sqrt(($x*$x)+($y*$y)+($z*$z));

         $x=$xa*$intradius;
         $y=$ya*$intradius;
         $z=$za*$intradius;

         $ixdiff=$xdiff*$intradius;
         $iydiff=$ydiff*$intradius;
         $izdiff=$zdiff*$intradius;

         for ($rodblob=1; $rodblob <= $resolution; $rodblob+=1) # draws extensors
             {
              $x+=($ixdiff/$resolution);  # vector indicated in sign (+/-) of step
              $y+=($iydiff/$resolution);  # vector indicated in sign (+/-) of step
              $z+=($izdiff/$resolution);  # vector indicated in sign (+/-) of step

              $xi=0;
              $yi=0;
              $zi=0;

              $currentradius=0;

              while ($currentradius <= $intradius) # this in facts extends out the vector a certain amount
                 {
                  $xi+=($x/($resolution));  # vector indicated in sign (+/-) of step
                  $yi+=($y/($resolution));  # vector indicated in sign (+/-) of step
                  $zi+=($z/($resolution));  # vector indicated in sign (+/-) of step
                  #printf FILE ("sphere { <$xe, $ye, $ze > 0.005 texture { pigment { rgb <1, 1, 1> } } }\n");
                  $currentradius=sqrt(($xi*$xi)+($yi*$yi)+($zi*$zi));
                 }
              #printf FILE ("sphere { <$xi, $yi, $zi > 0.005 texture { pigment { rgb <0, 1, 1> } } }\n"); # draw interior arcs
             }

         $x=$xa;
         $y=$ya;
         $z=$za;

         for ($rodblob=1; $rodblob <= $resolution; $rodblob+=1) # draws extensors
             {
              printf FILE ("sphere { < %5f, %5f, %5f > 0.005 texture { pigment { rgb <1, 0, 0> } } }\n", $x, $y, $z); # draw struts
              $x+=($xdiff/$resolution);  # vector indicated in sign (+/-) of step
              $y+=($ydiff/$resolution);  # vector indicated in sign (+/-) of step
              $z+=($zdiff/$resolution);  # vector indicated in sign (+/-) of step

              $xe=0;
              $ye=0;
              $ze=0;

              $extradius=0;

              while ($extradius <= $radius) # this in facts extends out the vector a certain amount
                 {
                  $xe+=($x/($resolution));  # vector indicated in sign (+/-) of step
                  $ye+=($y/($resolution));  # vector indicated in sign (+/-) of step
                  $ze+=($z/($resolution));  # vector indicated in sign (+/-) of step
                  #printf FILE ("sphere { <$xe, $ye, $ze > 0.005 texture { pigment { rgb <1, 1, 1> } } }\n");
                  $extradius=sqrt(($xe*$xe)+($ye*$ye)+($ze*$ze));
                 }
              #printf FILE ("sphere { <$xe, $ye, $ze > 0.005 texture { pigment { rgb <0, 0, 1> } } }\n"); # draw exterior arc
             }
       }
close (FACES);
flock(FILE, $unlock_lock);
close (FILE);
