--- ray/src/util/genBSDF.pl 2013/04/23 23:19:09 2.41 +++ ray/src/util/genBSDF.pl 2015/02/20 18:26:09 2.49 @@ -1,37 +1,92 @@ #!/usr/bin/perl -w -# RCSid $Id: genBSDF.pl,v 2.41 2013/04/23 23:19:09 greg Exp $ +# RCSid $Id: genBSDF.pl,v 2.49 2015/02/20 18:26:09 greg Exp $ # # Compute BSDF based on geometry and material description # # G. Ward # use strict; +my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64"); use File::Temp qw/ :mktemp /; sub userror { - 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 units] [input ..]\n"; + print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-W][-t{3|4} Nlog2][-r \"ropts\"][-f \"x=string;y=string\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}C][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom units] [input ..]\n"; exit 1; } -my $td = mkdtemp("/tmp/genBSDF.XXXXXX"); -chomp $td; +my ($td,$radscn,$mgfscn,$octree,$fsender,$bsender,$receivers,$facedat,$behinddat,$rmtmp); +my ($tf,$rf,$tb,$rb,$tfx,$rfx,$tbx,$rbx,$tfz,$rfz,$tbz,$rbz); +if ($windoz) { + my $tmploc = `echo \%TMP\%`; + chomp($tmploc); + $td = mkdtemp("$tmploc\\genBSDF.XXXXXX"); + $radscn = "$td\\device.rad"; + $mgfscn = "$td\\device.mgf"; + $octree = "$td\\device.oct"; + $fsender = "$td\\fsender.rad"; + $bsender = "$td\\bsender.rad"; + $receivers = "$td\\receivers.rad"; + $facedat = "$td\\face.dat"; + $behinddat = "$td\\behind.dat"; + $tf = "$td\\tf.dat"; + $rf = "$td\\rf.dat"; + $tb = "$td\\tb.dat"; + $rb = "$td\\rb.dat"; + $tfx = "$td\\tfx.dat"; + $rfx = "$td\\rfx.dat"; + $tbx = "$td\\tbx.dat"; + $rbx = "$td\\rbx.dat"; + $tfz = "$td\\tfz.dat"; + $rfz = "$td\\rfz.dat"; + $tbz = "$td\\tbz.dat"; + $rbz = "$td\\rbz.dat"; + chomp $td; + $rmtmp = "rmdir /S /Q $td"; +} else { + $td = mkdtemp("/tmp/genBSDF.XXXXXX"); + chomp $td; + $radscn = "$td/device.rad"; + $mgfscn = "$td/device.mgf"; + $octree = "$td/device.oct"; + $fsender = "$td/fsender.rad"; + $bsender = "$td/bsender.rad"; + $receivers = "$td/receivers.rad"; + $facedat = "$td/face.dat"; + $behinddat = "$td/behind.dat"; + $tf = "$td/tf.dat"; + $rf = "$td/rf.dat"; + $tb = "$td/tb.dat"; + $rb = "$td/rb.dat"; + $tfx = "$td/tfx.dat"; + $rfx = "$td/rfx.dat"; + $tbx = "$td/tbx.dat"; + $rbx = "$td/rbx.dat"; + $tfz = "$td/tfz.dat"; + $rfz = "$td/rfz.dat"; + $tbz = "$td/tbz.dat"; + $rbz = "$td/rbz.dat"; + $rmtmp = "rm -rf $td"; +} my @savedARGV = @ARGV; +my $rfluxmtx = "rfluxmtx -v -ab 5 -ad 700 -lw 3e-6"; +my $wrapper = "wrapBSDF"; my $tensortree = 0; my $ttlog2 = 4; my $nsamp = 2000; -my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6"; my $mgfin = 0; my $geout = 1; my $nproc = 1; +my $docolor = 0; my $doforw = 0; my $doback = 1; my $pctcull = 90; my $gunit = "meter"; +my $curspec = "Visible"; my @dim; # Get options while ($#ARGV >= 0) { if ("$ARGV[0]" =~ /^[-+]m/) { $mgfin = ("$ARGV[0]" =~ /^\+/); } elsif ("$ARGV[0]" eq "-r") { - $rtargs = "$rtargs $ARGV[1]"; + $rfluxmtx .= " $ARGV[1]"; shift @ARGV; } elsif ("$ARGV[0]" =~ /^[-+]g/) { $geout = ("$ARGV[0]" =~ /^\+/); @@ -40,6 +95,8 @@ while ($#ARGV >= 0) { die "Illegal geometry unit '$gunit': must be meter, foot, inch, centimeter, or millimeter\n"; } shift @ARGV; + } elsif ("$ARGV[0]" =~ /^[-+]C/) { + $docolor = ("$ARGV[0]" =~ /^\+/); } elsif ("$ARGV[0]" =~ /^[-+]f/) { $doforw = ("$ARGV[0]" =~ /^\+/); } elsif ("$ARGV[0]" =~ /^[-+]b/) { @@ -52,6 +109,11 @@ while ($#ARGV >= 0) { $tensortree = substr($ARGV[0], 2, 1); $ttlog2 = $ARGV[1]; shift @ARGV; + } elsif ("$ARGV[0]" eq "-f") { + $wrapper .= " -f \"$ARGV[1]\""; + shift @ARGV; + } elsif ("$ARGV[0]" eq "-W") { + $wrapper .= " -W"; } elsif ("$ARGV[0]" eq "-c") { $nsamp = $ARGV[1]; shift @ARGV; @@ -71,543 +133,322 @@ while ($#ARGV >= 0) { } # Check that we're actually being asked to do something die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback); +# Issue warning for unhandled reciprocity case +print STDERR "Warning: recommend both +forward and +backward with -t3\n" if + ($tensortree==3 && !($doforw && $doback)); +$wrapper .= $tensortree ? " -a t$tensortree" : " -a kf -c"; +$wrapper .= " -u $gunit"; # Get scene description and dimensions -my $radscn = "$td/device.rad"; -my $mgfscn = "$td/device.mgf"; -my $octree = "$td/device.oct"; if ( $mgfin ) { - system "mgfilt '#,o,xf,c,cxy,cspec,cmix,m,sides,rd,td,rs,ts,ir,v,p,n,f,fh,sph,cyl,cone,prism,ring,torus' @ARGV > $mgfscn"; + system "mgf2rad @ARGV > $radscn"; die "Could not load MGF input\n" if ( $? ); - system "mgf2rad $mgfscn > $radscn"; } else { system "xform -e @ARGV > $radscn"; die "Could not load Radiance input\n" if ( $? ); - system "rad2mgf $radscn > $mgfscn" if ( $geout ); } if ($#dim != 5) { @dim = split ' ', `getbbox -h $radscn`; } print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5); -# Add receiver surfaces (rectangular) -my $fmodnm="receiver_face"; -my $bmodnm="receiver_behind"; -open(RADSCN, ">> $radscn"); -print RADSCN "void glow $fmodnm\n0\n0\n4 1 1 1 0\n\n"; -print RADSCN "$fmodnm source f_receiver\n0\n0\n4 0 0 1 180\n"; -print RADSCN "void glow $bmodnm\n0\n0\n4 1 1 1 0\n\n"; -print RADSCN "$bmodnm source b_receiver\n0\n0\n4 0 0 -1 180\n"; -close RADSCN; +$wrapper .= ' -f "t=' . ($dim[5] - $dim[4]) . ';w=' . ($dim[1] - $dim[0]) . + ';h=' . ($dim[3] - $dim[2]) . '"'; # Generate octree system "oconv -w $radscn > $octree"; die "Could not compile scene\n" if ( $? ); -# Output XML prologue -print -' - -'; -print "\n"; -print -'System -BSDF - - - - 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\tOther\n"; -print " \n"; -# Output MGF description if requested +# Add MGF description if requested if ( $geout ) { - print "\t\n"; - print "\n"; - printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; - open(MGFSCN, "< $mgfscn"); - while () { print $_; } + open(MGFSCN, "> $mgfscn"); + printf MGFSCN "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; close MGFSCN; - print "xf\n"; - print "\t\t\n"; - print "\t\n"; + if ( $mgfin ) { + system qq{mgfilt "#,o,xf,c,cxy,cspec,cmix,m,sides,rd,td,rs,ts,ir,v,p,n,f,fh,sph,cyl,cone,prism,ring,torus" @ARGV >> $mgfscn}; + } else { + system "rad2mgf $radscn >> $mgfscn"; + } + open(MGFSCN, ">> $mgfscn"); + print MGFSCN "xf\n"; + close MGFSCN; + $wrapper .= " -g $mgfscn"; } -# Set up surface sampling -my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); -my $ny = int($nsamp/$nx + .5); +# Create receiver & sender surfaces (rectangular) +my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1); +my $ny = int($nsamp/$nx + 1); $nsamp = $nx * $ny; my $ns = 2**$ttlog2; -my (@pdiv, $disk2sq, $sq2disk, $tcal, $kcal); +open(RADSCN, "> $receivers"); +print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=sc$ns\n" : "h=kf\n"); +print RADSCN '#@rfluxmtx ' . "u=Y o=$facedat\n\n"; +print RADSCN "void glow receiver_face\n0\n0\n4 1 1 1 0\n\n"; +print RADSCN "receiver_face source f_receiver\n0\n0\n4 0 0 1 180\n\n"; +print RADSCN '#@rfluxmtx ' . "u=Y o=$behinddat\n\n"; +print RADSCN "void glow receiver_behind\n0\n0\n4 1 1 1 0\n\n"; +print RADSCN "receiver_behind source b_receiver\n0\n0\n4 0 0 -1 180\n"; +close RADSCN; +# Finish rfluxmtx command and prepare sender surfaces +$rfluxmtx .= " -n $nproc -c $nsamp"; +if ( $tensortree != 3 ) { # Isotropic tensor tree is exception + open (RADSCN, "> $fsender"); + print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n"); + print RADSCN "void polygon fwd_sender\n0\n0\n12\n"; + printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[4]; + printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[4]; + printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[4]; + printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[4]; + close RADSCN; + open (RADSCN, "> $bsender"); + print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n"); + print RADSCN "void polygon fwd_sender\n0\n0\n12\n"; + printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[5]; + printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[5]; + printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[5]; + printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[5]; + close RADSCN; +} # Create data segments (all the work happens here) if ( $tensortree ) { do_tree_bsdf(); } else { do_matrix_bsdf(); } -# Output XML epilogue -print -' - - -'; +# Output XML +my $old_fh = select(STDOUT); +$| = 1; +select($old_fh); +print "\n"; +# print STDERR "Running: $wrapper\n"; +system $wrapper; +die "Could not wrap BSDF data\n" if ( $? ); # Clean up temporary files and exit -exec("rm -rf $td"); +system $rmtmp; #-------------- End of main program segment --------------# #++++++++++++++ Tensor tree BSDF generation ++++++++++++++# sub do_tree_bsdf { -# 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 "\t\n"; -print "\t\tTensorTree$tensortree\n"; -print "\t\n"; -# Start rcontrib processes for compute each side -do_tree_rtcontrib(0) if ( $doback ); -do_tree_rtcontrib(1) if ( $doforw ); + # Run rfluxmtx processes to compute each side + do_ttree_dir(0) if ( $doback ); + do_ttree_dir(1) if ( $doforw ); } # end of sub do_tree_bsdf() -# Run rcontrib process to generate tensor tree samples -sub do_tree_rtcontrib { +# Call rfluxmtx and process tensor tree BSDF for the given direction +sub do_ttree_dir { my $forw = shift; - my $matargs = "-m $bmodnm"; - if ( !$forw || !$doback || $tensortree==3 ) { $matargs .= " -m $fmodnm"; } - my $cmd = "rcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " . - "-e '$disk2sq' -bn '$ns*$ns' " . - "-b '$ns*floor(out_square_x*$ns)+floor(out_square_y*$ns)' " . - "-o $td/%s.flt $matargs $octree"; + my $cmd; if ( $tensortree == 3 ) { # Isotropic BSDF my $ns2 = $ns / 2; - $cmd = "cnt $ns2 $ny $nx " . - "| rcalc -e 'r1=rand(.8681*recno-.673892)' " . - "-e 'r2=rand(-5.37138*recno+67.1737811)' " . - "-e 'r3=rand(+3.17603772*recno+83.766771)' " . - "-e 'Dx=1-2*(\$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"; + if ($windoz) { + $cmd = "cnt $ns2 $ny $nx " . + qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } . + qq{-e "r2=rand(-5.37138*recno+67.1737811)" } . + qq{-e "r3=rand(+3.17603772*recno+83.766771)" } . + qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } . + qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } . + qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } . + qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } . + qq{-e "\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz" } . + qq{-e "\$4=Dx;\$5=Dy;\$6=myDz" } . + "| $rfluxmtx -fa -y $ns2 - $receivers -i $octree"; + } else { + $cmd = "cnt $ns2 $ny $nx " . + qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } . + qq{-e "r2=rand(-5.37138*recno+67.1737811)" } . + qq{-e "r3=rand(+3.17603772*recno+83.766771)" } . + qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } . + qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } . + qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } . + qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } . + qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } . + qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } . + "| $rfluxmtx -h -ff -y $ns2 - $receivers -i $octree"; + } } else { # Anisotropic BSDF - # Sample area vertically to improve load balance, since - # shading systems usually have bilateral symmetry (L-R) - $cmd = "cnt $ns $ns $ny $nx " . - "| rcalc -e 'r1=rand(.8681*recno-.673892)' " . - "-e 'r2=rand(-5.37138*recno+67.1737811)' " . - "-e 'r3=rand(3.17603772*recno+83.766771)' " . - "-e 'r4=rand(-2.3857833*recno-964.72738)' " . - "-e 'in_square_x=(\$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"; + my $sender = ($bsender,$fsender)[$forw]; + if ($windoz) { + $cmd = "$rfluxmtx -fa $sender $receivers -i $octree"; + } else { + $cmd = "$rfluxmtx -h -ff $sender $receivers -i $octree"; + } } -# print STDERR "Starting: $cmd\n"; - system "$cmd" || die "Failure running rcontrib"; + # print STDERR "Starting: $cmd\n"; + system $cmd; + die "Failure running rfluxmtx" if ( $? ); ttree_out($forw); -} # end of do_tree_rtcontrib() +} # end of do_ttree_dir() -# Simplify and output tensor tree results +# Simplify and store tensor tree results sub ttree_out { my $forw = shift; - my $side = ("Back","Front")[$forw]; - my $cmd; -# Only output one transmitted anisotropic distribution, preferring backwards -if ( !$forw || !$doback || $tensortree==3 ) { -print -' - System - Visible - CIE Illuminant D65 1nm.ssp - ASTM E308 1931 Y.dsp - -'; -print "\t\t\tTransmission $side\n"; -print -' LBNL/Shirley-Chiu - BTDF - -'; -$cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . - q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' }; -if ($pctcull >= 0) { - $cmd .= "-of $td/" . ($bmodnm,$fmodnm)[$forw] . ".flt " . - "| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2"; - system "$cmd" || die "Failure running rttree_reduce"; -} else { - $cmd .= "$td/" . ($bmodnm,$fmodnm)[$forw] . ".flt"; - print "{\n"; - system "$cmd" || die "Failure running rcalc"; - for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) { - print "0\n"; + my ($refldat,$transdat); + if ( $forw ) { + $transdat = $facedat; + $refldat = $behinddat; + } else { + $transdat = $behinddat; + $refldat = $facedat; } - print "}\n"; -} -print -' - - -'; -} -# Output reflection -print -' - System - Visible - CIE Illuminant D65 1nm.ssp - ASTM E308 1931 Y.dsp - -'; -print "\t\t\tReflection $side\n"; -print -' LBNL/Shirley-Chiu - BTDF - -'; -$cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . - q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' }; -if ($pctcull >= 0) { - $cmd .= "-of $td/" . ($fmodnm,$bmodnm)[$forw] . ".flt " . - "| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2"; - system "$cmd" || die "Failure running rttree_reduce"; -} else { - $cmd .= "$td/" . ($fmodnm,$bmodnm)[$forw] . ".flt"; - print "{\n"; - system "$cmd" || die "Failure running rcalc"; - for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) { - print "0\n"; + # Only output one transmitted anisotropic distribution, preferring backwards + if ( !$forw || !$doback || $tensortree==3 ) { + my $ttyp = ("tb","tf")[$forw]; + ttree_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]); + if ( $docolor ) { + ttree_comp($ttyp, "CIE-X", $transdat, ($tbx,$tfx)[$forw]); + ttree_comp($ttyp, "CIE-Z", $transdat, ($tbz,$tfz)[$forw]); + } } - print "}\n"; -} -print -' - - -'; + # Output reflection + my $rtyp = ("rb","rf")[$forw]; + ttree_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]); + if ( $docolor ) { + ttree_comp($rtyp, "CIE-X", $refldat, ($rbx,$rfx)[$forw]); + ttree_comp($rtyp, "CIE-Z", $refldat, ($rbz,$rfz)[$forw]); + } } # end of ttree_out() +# Call rttree_reduce on the given component +sub ttree_comp { + my $typ = shift; + my $spec = shift; + my $src = shift; + my $dest = shift; + my $cmd; + if ($windoz) { + if ("$spec" eq "Visible") { + $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } . + q{-e "$1=(0.265*$1+0.670*$2+0.065*$3)/Omega"}; + } elsif ("$spec" eq "CIE-X") { + $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } . + q{-e "$1=(0.514*$1+0.324*$2+0.162*$3)/Omega"}; + } elsif ("$spec" eq "CIE-Z") { + $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } . + q{-e "$1=(0.024*$1+0.123*$2+0.853*$3)/Omega"}; + } + } else { + if ("$spec" eq "Visible") { + $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . + q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega'}; + } elsif ("$spec" eq "CIE-X") { + $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . + q{-e '$1=(0.514*$1+0.324*$2+0.162*$3)/Omega'}; + } elsif ("$spec" eq "CIE-Z") { + $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . + q{-e '$1=(0.024*$1+0.123*$2+0.853*$3)/Omega'}; + } + } + if ($pctcull >= 0) { + my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : ""; + if ($windoz) { + $cmd = "rcollate -ho -oc 1 $src | " . + $cmd . + " | rttree_reduce$avg -h -fa -t $pctcull -r $tensortree -g $ttlog2"; + } else { + $cmd .= " -of $src " . + "| rttree_reduce$avg -h -ff -t $pctcull -r $tensortree -g $ttlog2"; + } + # print STDERR "Running: $cmd\n"; + system "$cmd > $dest"; + die "Failure running rttree_reduce" if ( $? ); + } else { + if ($windoz) { + $cmd = "rcollate -ho -oc 1 $src | " . $cmd ; + } else { + $cmd .= " $src"; + } + open(DATOUT, "> $dest"); + print DATOUT "{\n"; + close DATOUT; + # print STDERR "Running: $cmd\n"; + system "$cmd >> $dest"; + die "Failure running rcalc" if ( $? ); + open(DATOUT, ">> $dest"); + for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) { + print DATOUT "0\n"; + } + print DATOUT "}\n"; + close DATOUT; + } + if ( "$spec" ne "$curspec" ) { + $wrapper .= " -s $spec" + $curspec = $spec; + } + $wrapper .= " -$typ $dest"; +} # end of ttree_comp() + #------------- 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 -$tcal = ' -DEGREE : PI/180; -sq(x) : x*x; -Kpola(r) : select(r+1, 0, 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); -Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow); -Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1)); -sin_kpol = sin(Kpol); -Dx = cos(Kazi)*sin_kpol; -Dy = sin(Kazi)*sin_kpol; -Dz = sqrt(1 - sin_kpol*sin_kpol); -KprojOmega = PI * if(Kbin-.5, - (sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow), - 1 - sq(cos(Kpola(1)*DEGREE))); -'; -# Compute Klems bin from exiting ray direction (forward or backward) -$kcal = ' -DEGREE : PI/180; -abs(x) : if(x, x, -x); -Acos(x) : if(x-1, 0, if(-1-x, PI, acos(x))) / DEGREE; -posangle(a) : if(-a, a + 2*PI, a); -Atan2(y,x) : posangle(atan2(y,x)) / DEGREE; -kpola(r) : select(r, 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); -kfindrow(r, pol) : if(r-kpola(0)+.5, r, - if(pol-kpola(r), kfindrow(r+1, pol), r) ); -kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0); -kbin2(pol,azi) = select(kfindrow(1, pol), - kazn(azi,360/knaz(1)), - kaccum(1) + kazn(azi,360/knaz(2)), - kaccum(2) + kazn(azi,360/knaz(3)), - kaccum(3) + kazn(azi,360/knaz(4)), - kaccum(4) + kazn(azi,360/knaz(5)), - kaccum(5) + kazn(azi,360/knaz(6)), - kaccum(6) + kazn(azi,360/knaz(7)), - kaccum(7) + kazn(azi,360/knaz(8)), - kaccum(8) + kazn(azi,360/knaz(9)) - ); -kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx)); -'; -my $ndiv = 145; -# Compute scattering data using rcontrib -my @tfarr; -my @rfarr; -my @tbarr; -my @rbarr; -my $cmd; -my $rtcmd = "rcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " . - "-e '$kcal' -b kbin -bn $ndiv " . - "-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' }; -if ( $doforw ) { -$cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . - "-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . - "-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . - "-e 'zp:$dim[4]' " . - q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } . - q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } . - "| $rtcmd"; -system "$cmd" || die "Failure running: $cmd\n"; -@tfarr = `$rccmd $td/$fmodnm.flt`; -die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); -chomp(@tfarr); -@rfarr = `$rccmd $td/$bmodnm.flt`; -die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); -chomp(@rfarr); -} -if ( $doback ) { -$cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . - "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . - "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . - "-e 'zp:$dim[5]' " . - q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } . - q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } . - "| $rtcmd"; -system "$cmd" || die "Failure running: $cmd\n"; -@tbarr = `$rccmd $td/$bmodnm.flt`; -die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); -chomp(@tbarr); -@rbarr = `$rccmd $td/$fmodnm.flt`; -die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); -chomp(@rbarr); -} -# Output angle basis -print -' - Columns - - LBNL/Klems Full - - 0 - 1 - - 0 - 5 - - - - 10 - 8 - - 5 - 15 - - - - 20 - 16 - - 15 - 25 - - - - 30 - 20 - - 25 - 35 - - - - 40 - 24 - - 35 - 45 - - - - 50 - 24 - - 45 - 55 - - - - 60 - 24 - - 55 - 65 - - - - 70 - 16 - - 65 - 75 - - - - 82.5 - 12 - - 75 - 90 - - - - -'; -if ( $doforw ) { -print -' - System - Visible - CIE Illuminant D65 1nm.ssp - ASTM E308 1931 Y.dsp - - Transmission Front - LBNL/Klems Full - LBNL/Klems Full - BTDF - -'; -# Output front transmission (transposed order) -for (my $od = 0; $od < $ndiv; $od++) { - for (my $id = 0; $id < $ndiv; $id++) { - print $tfarr[$ndiv*$id + $od], ",\n"; + + # Run rfluxmtx processes to compute each side + do_matrix_dir(0) if ( $doback ); + do_matrix_dir(1) if ( $doforw ); + +} # end of sub do_matrix_bsdf() + +# Call rfluxmtx and process tensor tree BSDF for the given direction +sub do_matrix_dir { + my $forw = shift; + my $cmd; + my $sender = ($bsender,$fsender)[$forw]; + $cmd = "$rfluxmtx -fd $sender $receivers -i $octree"; + # print STDERR "Starting: $cmd\n"; + system $cmd; + die "Failure running rfluxmtx" if ( $? ); + matrix_out($forw); +} # end of do_matrix_dir() + +sub matrix_out { + my $forw = shift; + my ($refldat,$transdat); + if ( $forw ) { + $transdat = $facedat; + $refldat = $behinddat; + } else { + $transdat = $behinddat; + $refldat = $facedat; } - print "\n"; -} -print -' - - - - System - Visible - CIE Illuminant D65 1nm.ssp - ASTM E308 1931 Y.dsp - - Reflection Front - LBNL/Klems Full - LBNL/Klems Full - BTDF - -'; -# Output front reflection (transposed order) -for (my $od = 0; $od < $ndiv; $od++) { - for (my $id = 0; $id < $ndiv; $id++) { - print $rfarr[$ndiv*$id + $od], ",\n"; + # Output transmission + my $ttyp = ("tb","tf")[$forw]; + matrix_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]); + if ( $docolor ) { + matrix_comp($ttyp, "CIE-X", $transdat, ($tbx,$tfx)[$forw]); + matrix_comp($ttyp, "CIE-Z", $transdat, ($tbz,$tfz)[$forw]); } - print "\n"; -} -print -' - - -'; -} -if ( $doback ) { -print -' - System - Visible - CIE Illuminant D65 1nm.ssp - ASTM E308 1931 Y.dsp - - Transmission Back - LBNL/Klems Full - LBNL/Klems Full - BTDF - -'; -# Output back transmission (transposed order) -for (my $od = 0; $od < $ndiv; $od++) { - for (my $id = 0; $id < $ndiv; $id++) { - print $tbarr[$ndiv*$id + $od], ",\n"; + # Output reflection + my $rtyp = ("rb","rf")[$forw]; + matrix_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]); + if ( $docolor ) { + matrix_comp($rtyp, "CIE-X", $refldat, ($rbx,$rfx)[$forw]); + matrix_comp($rtyp, "CIE-Z", $refldat, ($rbz,$rfz)[$forw]); } - print "\n"; -} -print -' - - - - System - Visible - CIE Illuminant D65 1nm.ssp - ASTM E308 1931 Y.dsp - - Reflection Back - LBNL/Klems Full - LBNL/Klems Full - BTDF - -'; -# Output back reflection (transposed order) -for (my $od = 0; $od < $ndiv; $od++) { - for (my $id = 0; $id < $ndiv; $id++) { - print $rbarr[$ndiv*$id + $od], ",\n"; +} # end of matrix_out() + +# Transpose matrix component data and save to file +sub matrix_comp { + my $typ = shift; + my $spec = shift; + my $src = shift; + my $dest = shift; + my $cmd = "rmtxop -fa -t"; + if ("$spec" eq "Visible") { + $cmd .= " -c 0.265 0.670 0.065"; + } elsif ("$spec" eq "CIE-X") { + $cmd .= " -c 0.514 0.324 0.162"; + } elsif ("$spec" eq "CIE-Z") { + $cmd .= " -c 0.024 0.123 0.853"; } - print "\n"; -} -print -' - - -'; -} -} -#------------- End of do_matrix_bsdf() --------------# + $cmd .= " $src | getinfo -"; + # print STDERR "Running: $cmd\n"; + system "$cmd > $dest"; + die "Failure running rttree_reduce" if ( $? ); + if ( "$spec" ne "$curspec" ) { + $wrapper .= " -s $spec" + $curspec = $spec; + } + $wrapper .= " -$typ $dest"; +} # end of matrix_comp() + +#------------- End of do_matrix_bsdf() & subroutines --------------#