--- ray/src/util/genBSDF.pl 2011/04/16 01:13:22 2.14 +++ ray/src/util/genBSDF.pl 2011/05/25 19:24:11 2.15 @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# RCSid $Id: genBSDF.pl,v 2.14 2011/04/16 01:13:22 greg Exp $ +# RCSid $Id: genBSDF.pl,v 2.15 2011/05/25 19:24:11 greg Exp $ # # Compute BSDF based on geometry and material description # @@ -8,11 +8,13 @@ use strict; use File::Temp qw/ :mktemp /; sub userror { - print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-r \"ropts\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom] [input ..]\n"; + print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-t{3|4} Nlog2][-r \"ropts\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom] [input ..]\n"; exit 1; } my $td = mkdtemp("/tmp/genBSDF.XXXXXX"); chomp $td; +my $tensortree = 0; +my $ttlog2 = 4; my $nsamp = 1000; my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6"; my $mgfin = 0; @@ -34,6 +36,10 @@ while ($#ARGV >= 0) { $doforw = ("$ARGV[0]" =~ /^\+/); } elsif ("$ARGV[0]" =~ /^[-+]b/) { $doback = ("$ARGV[0]" =~ /^\+/); + } elsif ("$ARGV[0]" =~ /^-t[34]$/) { + $tensortree = substr($ARGV[0], 2, 1); + $ttlog2 = $ARGV[1]; + shift @ARGV; } elsif ("$ARGV[0]" eq "-c") { $nsamp = $ARGV[1]; shift @ARGV; @@ -52,7 +58,13 @@ while ($#ARGV >= 0) { shift @ARGV; } # Check that we're actually being asked to do something -die "Must have at least one of +forward or +backward" if (!$doforw && !$doback); +die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback); +# Name our own persist file? +my $persistfile; +if ( $tensortree && $nproc > 1 && "$rtargs" !~ /-PP /) { + $persistfile = "$td/pfile.txt"; + $rtargs = "-PP $persistfile $rtargs"; +} # Get scene description and dimensions my $radscn = "$td/device.rad"; my $mgfscn = "$td/device.mgf"; @@ -82,9 +94,252 @@ close RADSCN; # Generate octree system "oconv -w $radscn > $octree"; die "Could not compile scene\n" if ( $? ); -# Set up sampling of interior portal +# Output XML prologue +print +' + +System + + + + Name + Manufacturer +'; +printf "\t\t%.3f\n", $dim[5] - $dim[4]; +printf "\t\t%.3f\n", $dim[1] - $dim[0]; +printf "\t\t%.3f\n", $dim[3] - $dim[2]; +print "\t\tIntegral\n"; +# Output MGF description if requested +if ( $geout ) { + print "\t\t\n"; + printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; + open(MGFSCN, "< $mgfscn"); + while () { print $_; } + close MGFSCN; + print "xf\n"; + print "\t\t\n"; +} +print " \n"; +# Set up surface sampling +my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); +my $ny = int($nsamp/$nx + .5); +$nsamp = $nx * $ny; +my $ns = 2**$ttlog2; +my (@pdiv, $disk2sq, $sq2disk, $tcal, $kcal); +# Create data segments (all the work happens here) +if ( $tensortree ) { + do_tree_bsdf(); +} else { + do_matrix_bsdf(); +} +# Output XML epilogue +print +' + + +'; +# Clean up temporary files and exit +if ( $persistfile ) { + open(PFI, "< $persistfile"); + while () { + s/^[^ ]* //; + kill('ALRM', $_); + last; + } + close PFI; +} +system "rm -rf $td"; +exit 0; +#-------------- End of main program segment --------------# + +#++++++++++++++ Tensor tree BSDF generation ++++++++++++++# +sub do_tree_bsdf { +# Get sampling rate and subdivide task +my $ns2 = $ns; +$ns2 /= 2 if ( $tensortree == 3 ); +@pdiv = (0, int($ns2/$nproc)); +my $nrem = $ns2 % $nproc; +for (my $i = 1; $i < $nproc; $i++) { + my $nv = $pdiv[$i] + $pdiv[1]; + ++$nv if ( $nrem-- > 0 ); + push @pdiv, $nv; +} +die "Script error 1" if ($pdiv[-1] != $ns2); +# Shirley-Chiu mapping from unit square to disk +$sq2disk = ' +in_square_a = 2*in_square_x - 1; +in_square_b = 2*in_square_y - 1; +in_square_rgn = if(in_square_a + in_square_b, + if(in_square_a - in_square_b, 1, 2), + if(in_square_b - in_square_a, 3, 4)); +out_disk_r = .999995*select(in_square_rgn, in_square_a, in_square_b, + -in_square_a, -in_square_b); +out_disk_phi = PI/4 * select(in_square_rgn, + in_square_b/in_square_a, + 2 - in_square_a/in_square_b, + 4 + in_square_b/in_square_a, + if(in_square_b*in_square_b, + 6 - in_square_a/in_square_b, 0)); +Dx = out_disk_r*cos(out_disk_phi); +Dy = out_disk_r*sin(out_disk_phi); +Dz = sqrt(1 - out_disk_r*out_disk_r); +'; +# Shirley-Chiu mapping from unit disk to square +$disk2sq = ' +norm_radians(p) : if(-p - PI/4, p + 2*PI, p); +in_disk_r = .999995*sqrt(Dx*Dx + Dy*Dy); +in_disk_phi = norm_radians(atan2(Dy, Dx)); +in_disk_rgn = floor((in_disk_phi + PI/4)/(PI/2)) + 1; +out_square_a = select(in_disk_rgn, + in_disk_r, + (PI/2 - in_disk_phi)*in_disk_r/(PI/4), + -in_disk_r, + (in_disk_phi - 3*PI/2)*in_disk_r/(PI/4)); +out_square_b = select(in_disk_rgn, + in_disk_phi*in_disk_r/(PI/4), + in_disk_r, + (PI - in_disk_phi)*in_disk_r/(PI/4), + -in_disk_r); +out_square_x = (out_square_a + 1)/2; +out_square_y = (out_square_b + 1)/2; +'; +# Announce ourselves in XML output +print " \n"; +print " TensorTree$tensortree\n"; +print " \n"; +# Fork parallel rtcontrib processes to compute each side +if ( $doback ) { + for (my $proc = 0; $proc < $nproc; $proc++) { + bg_tree_rtcontrib(0, $proc); + } + while (wait() >= 0) { + die "rtcontrib process reported error" if ( $? ); + } + ttree_out(0); +} +if ( $doforw ) { + for (my $proc = 0; $proc < $nproc; $proc++) { + bg_tree_rtcontrib(1, $proc); + } + while (wait() >= 0) { + die "rtcontrib process reported error" if ( $? ); + } + ttree_out(1); +} +} # end of sub do_tree_bsdf() + +# Run i'th rtcontrib process for generating tensor tree samples +sub bg_tree_rtcontrib { + my $pid = fork(); + die "Cannot fork new process" unless defined $pid; + if ($pid > 0) { return $pid; } + my $forw = shift; + my $pn = shift; + my $pbeg = $pdiv[$pn]; + my $plen = $pdiv[$pn+1] - $pbeg; + my $matargs = "-m $bmodnm"; + if ( !$forw || !$doback ) { $matargs .= " -m $fmodnm"; } + my $cmd = "rtcontrib $rtargs -h -ff -fo -c $nsamp " . + "-e '$disk2sq' -bn '$ns*$ns' " . + "-b '$ns*floor(out_square_x*$ns)+floor(out_square_y*$ns)' " . + "-o $td/%s_" . sprintf("%03d", $pn) . ".flt $matargs $octree"; + if ( $tensortree == 3 ) { + # Isotropic BSDF + $cmd = "cnt $plen $ny $nx " . + "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " . + "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " . + "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " . + "-e 'Dx=1-($pbeg+\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' " . + "-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . + "-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . + "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " . + "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " . + "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " . + "| $cmd"; + } else { + # Anisotropic BSDF + # Sample area vertically to improve load balance, since + # shading systems usually have bilateral symmetry (L-R) + $cmd = "cnt $plen $ns $ny $nx " . + "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " . + "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " . + "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " . + "-e 'r4=rand(($pn-2.3857833)*recno-964.72738)' " . + "-e 'in_square_x=($pbeg+\$1+r1)/$ns' " . + "-e 'in_square_y=(\$2+r2)/$ns' -e '$sq2disk' " . + "-e 'xp=(\$4+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . + "-e 'yp=(\$3+r4)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . + "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " . + "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " . + "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " . + "| $cmd"; + } +# print STDERR "Starting: $cmd\n"; + exec($cmd); # no return; status report to parent via wait + die "Cannot exec: $cmd\n"; +} # end of bg_tree_rtcontrib() + +# Simplify and output tensor tree results +sub ttree_out { + my $forw = shift; + my $side = ("Back","Front")[$forw]; +# Only output one transmitted distribution, preferring backwards +if ( !$forw || !$doback ) { +print +' + System + Visible + CIE Illuminant D65 1nm.ssp + ASTM E308 1931 Y.dsp + + Transmission + LBNL/Shirley-Chiu + BTDF + +'; +system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . + q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } . + "$td/" . ($bmodnm,$fmodnm)[$forw] . "_???.flt " . + "| rttree_reduce -h -ff -r $tensortree -g $ttlog2"; +die "Failure running rttree_reduce" if ( $? ); +print +' + + +'; +} +# Output reflection +print +' + System + Visible + CIE Illuminant D65 1nm.ssp + ASTM E308 1931 Y.dsp + + Reflection $side + LBNL/Shirley-Chiu + BRDF + +'; +system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . + q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } . + "$td/" . ($fmodnm,$bmodnm)[$forw] . "_???.flt " . + "| rttree_reduce -h -ff -r $tensortree -g $ttlog2"; +die "Failure running rttree_reduce" if ( $? ); +print +' + + +'; +} # end of ttree_out() + +#------------- End of do_tree_bsdf() & subroutines -------------# + +#+++++++++++++++ Klems matrix BSDF generation +++++++++++++++# +sub do_matrix_bsdf { +# Set up sampling of portal # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms -my $tcal = ' +$tcal = ' DEGREE : PI/180; sq(x) : x*x; Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90); @@ -105,7 +360,7 @@ KprojOmega = PI * if(Kbin-.5, 1 - sq(cos(Kpola(1)*DEGREE))); '; # Compute Klems bin from exiting ray direction (forward or backward) -my $kcal = ' +$kcal = ' DEGREE : PI/180; abs(x) : if(x, x, -x); Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x))); @@ -131,18 +386,15 @@ kbin2(pol,azi) = select(kfindrow(1, pol), kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx)); '; my $ndiv = 145; -my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); -my $ny = int($nsamp/$nx + .5); -$nsamp = $nx * $ny; # Compute scattering data using rtcontrib my @tfarr; my @rfarr; my @tbarr; my @rbarr; my $cmd; -my $rtcmd = "rtcontrib -h -ff -fo -n $nproc -c $nsamp " . +my $rtcmd = "rtcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " . "-e '$kcal' -b kbin -bn $ndiv " . - "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $rtargs $octree"; + "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $octree"; my $rccmd = "rcalc -e '$tcal' " . "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " . q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'}; @@ -174,42 +426,18 @@ die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $ @rbarr = `$rccmd $td/$fmodnm.flt`; die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); } -# Output XML prologue +# Output angle basis print -' - - System - - - - Name - Manufacturer -'; -printf "\t\t\t%.3f\n", $dim[5] - $dim[4]; -printf "\t\t\t%.3f\n", $dim[1] - $dim[0]; -printf "\t\t\t%.3f\n", $dim[3] - $dim[2]; -print "\t\t\tIntegral\n"; -# Output MGF description if requested -if ( $geout ) { - print "\t\t\t\n"; - printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; - open(MGFSCN, "< $mgfscn"); - while () { print $_; } - close MGFSCN; - print "xf\n"; - print "\t\t\t\n"; -} -print ' - - Columns - +' + Columns + LBNL/Klems Full - + 0 1 - 0 - 5 + 0 + 5 @@ -280,7 +508,8 @@ print ' '; if ( $doforw ) { -print ' +print +' System Visible CIE Illuminant D65 1nm.ssp @@ -300,8 +529,8 @@ for (my $od = 0; $od < $ndiv; $od++) { print "\n"; } print -' - +' + System @@ -323,13 +552,14 @@ for (my $od = 0; $od < $ndiv; $od++) { print "\n"; } print -' - +' + '; } if ( $doback ) { -print ' +print +' System Visible CIE Illuminant D65 1nm.ssp @@ -349,8 +579,8 @@ for (my $od = 0; $od < $ndiv; $od++) { print "\n"; } print -' - +' + System @@ -372,15 +602,10 @@ for (my $od = 0; $od < $ndiv; $od++) { print "\n"; } print -' - +' + '; } -# Output XML epilogue -print ' - - -'; -# Clean up temporary files -system "rm -rf $td"; +} +#------------- End of do_matrix_bsdf() --------------#