#!/usr/bin/perl
#
# perl interpolcmdinwf.pl in out cmd DISTFN commandwithDISTDEPVALUE
#
# Throws in a distance dependendent command after each jump
#
# in can be a file with like 'part1' or 'part1_snap'
#
# e.g. perl interpolcmdinwf.pl part1_tripthruhipparcos_snap outfile 0.1+0.3\*\(DIST/860\) g8 slum DISTDEPVALUE
#
# Note that you need to put backslashes before * and ( and ).
# The above eg should be interpreted as
# define DISTDEPVALUE := 0.1+0.3*(DIST/860)
# where DIST is the distance from the camera to the origin
# and then the statement "g8 slum x", where x is the value of DISTDEPVALUE
# is inserted in the input file (in) after each jump statement
# to produce the resulting output file (out).
$in = $ARGV[0];
$out = $ARGV[1];
$distfn = $ARGV[2]; # defines the function of distance
print "$distfn\n\n";
$distcmd = $ARGV[3]; # defines the command to place after each jump command
for ($i=4; $i<=$#ARGV; $i++)
{
$distcmd = $distcmd." ".$ARGV[$i];
}
open (IN,"$in");
open (OUT,">$out");
while ($line = )
{
chomp $line;
@tmp = split /[\t\n ]+/, $line;
print OUT $line, "\n";
if ($line =~ /jump/)
{
$start = 0;
while (length($tmp[$start]) == 0) {$start++;}
$x = $tmp[$start+2];
$y = $tmp[$start+3];
$z = $tmp[$start+4];
$dist = sqrt ($x*$x + $y*$y + $z*$z);
# print "$x $y $z ... $dist ";
$somecmd = "\$val = $distfn;";
$somecmd =~ s/DIST/$dist/;
eval ($somecmd);
# print $somecmd, "\n";;
# print $val , "\n";
# print "------\n";
$somecmd = "\$cmd = \"echo $distcmd\"";
$somecmd =~ s/DISTDEPVALUE/$val/;
eval ($somecmd);
# print $somecmd, "\n";;
# print $cmd , "\n";
# print "------\n";
print OUT "$cmd\n";
}
}
sub min
{
my ($a, $b) = @_;
if ($a < $b) {return $a;} else {return $b;}
}
sub max
{
my ($a, $b) = @_;
if ($a > $b) {return $a;} else {return $b;}
}
sub sigmoid # creates function that increases from ya at a to yb at b, gives value at position x, which is between a and b.
{
my ($x,$a,$b,$ya,$yb) = @_;
$x = log(0.001 + $x);
$a = log(0.001 + $a);
$b = log(0.001 + $b);
# y = 1/(1+exp(-x)) goes from 0 to 1 as x goes from -5 to 5
# y = ya + (yb-ya)/(1+exp(5-x)) goes from ya to yb as x goes from 0 to 10
# y = ya + (yb-ya)/(1+exp(5-(10*x/(b-a)))) goes from ya to yb as x goes from 0 to b-a
# y = ya + (yb-ya)/(1+exp(5-(10*(x-a)/(b-a)))) goes from ya to yb as x goes from a to b
return $ya+($yb-$ya)/(1+exp(5-(10*($x-$a)/($b-$a))));
}
sub linear # creates function that linearly increases from ya at a to yb at b, gives value at position x, which is between a and b.
{
my ($x,$a,$b,$ya,$yb) = @_;
# y = 1/(1+exp(-x)) goes from 0 to 1 as x goes from -5 to 5
# y = ya + (yb-ya)/(1+exp(5-x)) goes from ya to yb as x goes from 0 to 10
# y = ya + (yb-ya)/(1+exp(5-(10*x/(b-a)))) goes from ya to yb as x goes from 0 to b-a
# y = ya + (yb-ya)/(1+exp(5-(10*(x-a)/(b-a)))) goes from ya to yb as x goes from a to b
$v = $ya+(($yb-$ya)*($x-$a)/($b-$a));
$v = min($v,max($ya,$yb));
$v = max($v,min($ya,$yb));
return $v;
}