#!/bin/perl

# This program generates nice Povray views of a given vect file

use strict;
use warnings;
use File::Path;

my $tubename;
my $pngname;
my $imgname;
my $throttle;
my $axis;
my $dirname;
my $thickness;
my $cvtoutput;
my $borderW;
my $borderH;
my $jpgname;
my $filename;
my $cream   = "--red=0.9647  --green=0.9098 --blue=0.7647";
my $dkbrown = "--red=0.00392 --green=0.4    --blue=0.3686";
my $dkgreen = "--red=0.5490  --green=0.3176 --blue=0.0392";
my $ltbrown = "--red=0.8470  --green=0.7019 --blue=0.3960";
my $ltgreen = "--red=0.78039 --green=0.9176 --blue=0.8980";

$throttle = 0;

$filename = $ARGV[0];

print "Processing knot type represented by filename $filename.\n";
print "Computed thickness ...";
      
open(ROPELENGTH,"ropelength $filename |");
while (<ROPELENGTH>) {
  if (/Thickness:\s*(\S+)/) { 
    $thickness = $1;
  }
}
close ROPELENGTH;

$thickness /= 2.0; # ropelength returns diameter thickness.
print "$thickness\n";

open (KFILE,"<$filename");
my @vectdata = <KFILE>;
my $gresult = grep (/0 0 0 0/,@vectdata);

#print "grep result $gresult\n";
#print "$vectdata[$#vectdata]\n";

if ($gresult) {
  print "Adding colors to black VECT file...\n";
  $tubename = `tube $cream $ltbrown $ltgreen $dkbrown $dkgreen -q -r $thickness $filename 2> /dev/null`;
} else {
  print "VECT file has its' own colors...\n";
  $tubename = `tube -q -r $thickness $filename 2> /dev/null`;
}

chomp($tubename);

if ($tubename !~ /.tube.off/) {
  
  die("Tube run seems to have failed with command line\n tube $cream $ltbrown $ltgreen $dkbrown $dkgreen -q -r $thickness $filename 2> /dev/null ");

}

print "Generated tube $tubename. Now generating views.\n";

for ($axis=0;$axis<3;$axis++) {
  
  print "Orienting to axis $axis.\n";
  $imgname = $filename;
  $imgname =~ s/.vect//;
  $imgname = $imgname.".a".$axis.".png";
  print "Will output image file $imgname.\n";
  
  `orient -a $axis $tubename`;

  print "Now running povsnap.\n";
  
  $pngname = `povsnap -qp --NoDisplay $tubename`;
  chomp($pngname);

  unless (-f $pngname) { 
    
    warn("$pngname doesn't exist");
    print `povsnap -p $tubename`;
    die;

  }
			   
  print "Povsnap generated file $pngname.\n";
  
  #`convert -trim $pngname $imgname`;
  #`convert -resize 768x768 $imgname $imgname`;
  `convert -contrast -trim $pngname $imgname`;
  $cvtoutput = `convert -resize 768x768 -verbose $imgname $imgname`;
  
  #print "Convert output is: $cvtoutput";
  $cvtoutput =~ /\=\>(\d+)x(\d+)/;
  print "Read new image dimensions as $1 x $2\n";
  
  $borderW = (768 - $1)/2.0;
  $borderH = (768 - $2)/2.0;
  
  $cvtoutput = `convert -border ${borderW}x${borderH} -bordercolor white -verbose $imgname $imgname`;
  
  #print "Convert output is: $cvtoutput";
  $cvtoutput =~ /\=\>(\d+)x(\d+)/;
  print "After adding border, image is $1 x $2\n";
  
  $jpgname = $imgname;
  $jpgname =~ s/png/jpg/;
  `convert $imgname $jpgname`;
  
  print "Converted $imgname to $jpgname.\n"; 
  
  $dirname = $tubename;
  $dirname =~ s/.off//;
  
  print "Will now attempt to remove povsnap directory /".$dirname."\n";
  
  unlink($imgname) or die("Couldn't remove $imgname: $_");
  unlink($pngname) or die("Couldn't remove $pngname: $_");
  rmtree($dirname) or die("Couldn't remove $dirname: $_");
  
  $throttle++;
  
  if ($throttle > 400) { die ("Throttle killed it.\n"); }
  
}
