--- ray/src/util/genklemsamp.pl 2011/04/16 00:39:07 2.10 +++ ray/src/util/genklemsamp.pl 2011/05/16 23:56:32 2.11 @@ -1,18 +1,17 @@ #!/usr/bin/perl -w -# RCSid $Id: genklemsamp.pl,v 2.10 2011/04/16 00:39:07 greg Exp $ +# RCSid $Id: genklemsamp.pl,v 2.11 2011/05/16 23:56:32 greg Exp $ # # Sample Klems (full) directions impinging on surface(s) # # G. Ward # use strict; +my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64"); use File::Temp qw/ :mktemp /; if ($#ARGV < 0) { print STDERR "Usage: genklemsamp [-c N ][-f{a|f|d}] [view opts] [geom.rad ..]\n"; exit 1; } -my $td = mkdtemp("/tmp/genklemsamp.XXXXXX"); -chomp $td; my $nsamp = 1000; my $fmt = "a"; my @vopts="-vo 1e-5"; @@ -44,29 +43,60 @@ $ofmt = "-o$fmt" if ("$fmt" ne "a"); # Require parallel view and compile settings push @vopts, "-vtl"; my $vwset = `vwright @vopts V`; +chomp $vwset; $_ = $vwset; s/^.*Vhn://; s/;.*$//; my $width = $_; $_ = $vwset; s/^.*Vvn://; s/;.*$//; my $height = $_; my $sca = sqrt($nsamp/($width*$height)); # Kbin is input to get direction in full Klems basis with (x1,x2) randoms -my $tcal = ' -DEGREE : PI/180; -Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90); -Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12); -Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0); -Kmax : Kaccum(Knaz(0)); -Kfindrow(r, rem) : if(rem-Knaz(r)-.5, Kfindrow(r+1, rem-Knaz(r)), r); -Krow = if(Kbin-(Kmax+.5), 0, Kfindrow(1, Kbin)); -Kcol = Kbin - Kaccum(Krow-1) - 1; -Kazi = 360*DEGREE * (Kcol + .5 - x2) / Knaz(Krow); -Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1)); -sin_kpol = sin(Kpol); -K0 = cos(Kazi)*sin_kpol; -K1 = sin(Kazi)*sin_kpol; -K2 = sqrt(1 - sin_kpol*sin_kpol); -'; +my $tcal = 'DEGREE : PI/180;' . + 'Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);' . + 'Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);' . + 'Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0);' . + 'Kmax : Kaccum(Knaz(0));' . + 'Kfindrow(r, rem) : if(rem-Knaz(r)-.5, Kfindrow(r+1, rem-Knaz(r)), r);' . + 'Krow = if(Kbin-(Kmax+.5), 0, Kfindrow(1, Kbin));' . + 'Kcol = Kbin - Kaccum(Krow-1) - 1;' . + 'Kazi = 360*DEGREE * (Kcol + .5 - x2) / Knaz(Krow);' . + 'Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1));' . + 'sin_kpol = sin(Kpol);' . + 'K0 = cos(Kazi)*sin_kpol;' . + 'K1 = sin(Kazi)*sin_kpol;' . + 'K2 = sqrt(1 - sin_kpol*sin_kpol);' ; my $ndiv = 145; +my ($td, $inchk, $rmtmp, $orgcnt); +if ($windoz) { + my $tmploc = `echo \%TMP\%`; + chomp($tmploc); + $td = mkdtemp("$tmploc\\genklemsamp.XXXXXX"); + chomp $td; + $inchk = "rtrace -w -h -opLN $td\\surf.oct " . + q{| rcalc -e "cond=and(5e9-$4,nOK);$1=$1;$2=$2;$3=$3" } . + "-e \"and(a,b):if(a,b,a);sq(x):x*x\" -e \"$vwset\" " . + q{-e "nOK=sq(Vdx*$5+Vdy*$6+Vdz*$7)-.999" } . + " > $td\\origins.txt"; + $rmtmp = "rmdir /S /Q $td"; +} else { + $td = mkdtemp("/tmp/genklemsamp.XXXXXX"); + chomp $td; + $inchk = "rtrace -w -h -faf -opLN $td/surf.oct " . + q{| rcalc -e 'cond=and(5e9-$4,nOK);$1=$1;$2=$2;$3=$3' } . + "-e 'and(a,b):if(a,b,a);sq(x):x*x' -e '$vwset' " . + q{-e 'nOK=sq(Vdx*$5+Vdy*$6+Vdz*$7)-.999' } . + "-if7 -of > $td/origins.flt"; + $rmtmp = "rm -rf $td"; +} +sub GetNOrigins { + if ($windoz) { + my $count = 0; + open(FILE, "< $td\\origins.txt"); + $count++ while ; + close(FILE); + return $count; + } + return (-s "$td/origins.flt") / (4*3); +} # Do we have any Radiance input files? if ($#ARGV >= 0) { system "oconv -f @ARGV > $td/surf.oct"; @@ -93,14 +123,8 @@ if ($#ARGV >= 0) { while ($ntot < $nsamp) { $xres = int($width*$sca) + 1; $yres = int($height*$sca) + 1; - system "vwrays -ff -x $xres -y $yres -pa 0 -pj .7 @vopts " . - "| rtrace -w -h -ff -opLN $td/surf.oct " . - q{| rcalc -e 'cond=and(5e9-$4,nOK);$1=$1;$2=$2;$3=$3' } . - "-e 'and(a,b):if(a,b,a);sq(x):x*x' -e '$vwset' " . - q{-e 'nOK=sq(Vdx*$5+Vdy*$6+Vdz*$7)-.999' } . - "-if7 -of > $td/origins.flt"; - $ntot = -s "$td/origins.flt"; - $ntot /= 3*4; # number of bytes per sample position + system "vwrays -x $xres -y $yres -pa 0 -pj .7 @vopts | $inchk"; + $ntot = GetNOrigins(); if ($ntot == 0) { if ($nsamp < 200) { $sca = sqrt(200/($width*$height)); @@ -116,7 +140,17 @@ if ($#ARGV >= 0) { my $rn = rand(10); my $r1 = rand; my $r2 = rand; # Chance of using = (number_still_needed)/(number_left_avail) - system "rcalc $ofmt -e '$tcal' -e '$vwset' " . + if ($windoz) { + system "rcalc $ofmt -e \"$tcal\" -e \"$vwset\" " . + "-e \"cond=($nsamp-outno+1)/($ntot-recno+1)-rand($rn+recno)\" " . + "-e \"Kbin=$k;x1=rand($r1+recno);x2=rand($r2+recno)\" " . + q{-e "$1=$1+Vo*Vdx; $2=$2+Vo*Vdy; $3=$3+Vo*Vdz" } . + q{-e "$4=-K0*Vhx-K1*Vvx+K2*Vdx" } . + q{-e "$5=-K0*Vhy-K1*Vvy+K2*Vdy" } . + q{-e "$6=-K0*Vhz-K1*Vvz+K2*Vdz" } . + "$td\\origins.txt"; + } else { + system "rcalc $ofmt -e '$tcal' -e '$vwset' " . "-e 'cond=($nsamp-outno+1)/($ntot-recno+1)-rand($rn+recno)' " . "-e 'Kbin=$k;x1=rand($r1+recno);x2=rand($r2+recno)' " . q{-e '$1=$1+Vo*Vdx; $2=$2+Vo*Vdy; $3=$3+Vo*Vdz' } . @@ -124,6 +158,7 @@ if ($#ARGV >= 0) { q{-e '$5=-K0*Vhy-K1*Vvy+K2*Vdy' } . q{-e '$6=-K0*Vhz-K1*Vvz+K2*Vdz' } . "-if3 $td/origins.flt"; + } } } else { # No Radiance input files, so just sample over parallel view @@ -135,7 +170,21 @@ if ($#ARGV >= 0) { my $r1 = rand; my $r2 = rand; my $r3 = rand; my $r4 = rand; # Chance of using = (number_still_needed)/(number_left_avail) - system "cnt $yres $xres | rcalc $ofmt -e '$tcal' -e '$vwset' " . + if ($windoz) { + system "cnt $yres $xres | rcalc $ofmt -e \"$tcal\" -e \"$vwset\" " . + "-e \"xc=\$2;yc=\$1\" ". + "-e \"cond=($nsamp-outno+1)/($ntot-recno+1)-rand($rn+recno)\" " . + "-e \"Kbin=$k;x1=rand($r1+recno);x2=rand($r2+recno)\" " . + "-e \"hpos=Vhn*((xc+rand($r3+recno))/$xres - .5)\" " . + "-e \"vpos=Vvn*((yc+rand($r4+recno))/$yres - .5)\" " . + q{-e "$1=Vpx+Vo*Vdx+hpos*Vhx+vpos*Vvx" } . + q{-e "$2=Vpy+Vo*Vdy+hpos*Vhy+vpos*Vvy" } . + q{-e "$3=Vpz+Vo*Vdz+hpos*Vhz+vpos*Vvz" } . + q{-e "$4=-K0*Vhx-K1*Vvx+K2*Vdx" } . + q{-e "$5=-K0*Vhy-K1*Vvy+K2*Vdy" } . + q{-e "$6=-K0*Vhz-K1*Vvz+K2*Vdz" } ; + } else { + system "cnt $yres $xres | rcalc $ofmt -e '$tcal' -e '$vwset' " . q{-e 'xc=$2;yc=$1' } . "-e 'cond=($nsamp-outno+1)/($ntot-recno+1)-rand($rn+recno)' " . "-e 'Kbin=$k;x1=rand($r1+recno);x2=rand($r2+recno)' " . @@ -147,6 +196,7 @@ if ($#ARGV >= 0) { q{-e '$4=-K0*Vhx-K1*Vvx+K2*Vdx' } . q{-e '$5=-K0*Vhy-K1*Vvy+K2*Vdy' } . q{-e '$6=-K0*Vhz-K1*Vvz+K2*Vdz' } ; + } } } -system "rm -rf $td"; +system $rmtmp;