--- ray/src/util/genBSDF.pl 2016/09/16 17:54:56 2.66 +++ ray/src/util/genBSDF.pl 2020/05/05 22:35:58 2.85 @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# RCSid $Id: genBSDF.pl,v 2.66 2016/09/16 17:54:56 greg Exp $ +# RCSid $Id: genBSDF.pl,v 2.85 2020/05/05 22:35:58 greg Exp $ # # Compute BSDF based on geometry and material description # @@ -9,11 +9,11 @@ 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][-W][-t{3|4} Nlog2][-r \"ropts\"][-s \"x=string;y=string\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}C][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom units] [input ..]\n"; + print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-W][-t{3|4} Nlog2][-r \"ropts\"][-s \"x=string;y=string\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}C][{+|-}a][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom units] [input ..]\n"; exit 1; } my ($td,$radscn,$mgfscn,$octree,$fsender,$bsender,$receivers,$facedat,$behinddat,$rmtmp); -my ($tf,$rf,$tb,$rb,$tfx,$rfx,$tbx,$rbx,$tfz,$rfz,$tbz,$rbz,$cph,$sav); +my ($tf,$rf,$tb,$rb,$tfx,$rfx,$tbx,$rbx,$tfz,$rfz,$tbz,$rbz,$cph); my ($curphase, $recovery); if ($#ARGV == 1 && "$ARGV[0]" =~ /^-rec/) { $td = $ARGV[1]; @@ -21,6 +21,7 @@ if ($#ARGV == 1 && "$ARGV[0]" =~ /^-rec/) { @ARGV = ; close MYAVH; chomp @ARGV; + $recovery = 0; if (open(MYPH, "< $td/phase.txt")) { while () { chomp($recovery = $_); @@ -83,10 +84,11 @@ if ($windoz) { $rmtmp = "rm -rf $td"; } my @savedARGV = @ARGV; -my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6"; +my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6 -w-"; my $wrapper = "wrapBSDF"; my $tensortree = 0; my $ttlog2 = 4; +my $dorecip = 1; my $nsamp = 2000; my $mgfin = 0; my $geout = 1; @@ -114,6 +116,8 @@ while ($#ARGV >= 0) { shift @ARGV; } elsif ("$ARGV[0]" =~ /^[-+]C/) { $docolor = ("$ARGV[0]" =~ /^\+/); + } elsif ("$ARGV[0]" =~ /^[-+]a/) { + $dorecip = ("$ARGV[0]" =~ /^\+/); } elsif ("$ARGV[0]" =~ /^[-+]f/) { $doforw = ("$ARGV[0]" =~ /^\+/); } elsif ("$ARGV[0]" =~ /^[-+]b/) { @@ -155,11 +159,11 @@ while ($#ARGV >= 0) { die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback); $wrapper .= $tensortree ? " -a t$tensortree" : " -a kf -c"; $wrapper .= " -u $gunit"; -if (!defined $recovery) { +if ( !defined $recovery ) { # Issue warning for unhandled reciprocity case print STDERR "Warning: recommend both +forward and +backward with -t3\n" if ($tensortree==3 && !($doforw && $doback)); - # Get scene description and dimensions + # Get scene description if ( $mgfin ) { system "mgf2rad @ARGV > $radscn"; die "Could not load MGF input\n" if ( $? ); @@ -168,31 +172,33 @@ if (!defined $recovery) { die "Could not load Radiance input\n" if ( $? ); } } -if ($#dim != 5) { - @dim = split ' ', `getbbox -h $radscn`; +if ( $#dim != 5 ) { + @dim = split ' ', `getbbox -h -w $radscn`; } -die "Device entirely inside room!\n" if ($dim[4] >= 0); -if ($dim[5] > 1e-5) { +die "Device entirely inside room!\n" if ( $dim[4] >= 0 ); +if ( $dim[5] > 1e-5 ) { print STDERR "Warning: Device extends into room\n"; -} elsif ($dim[5]*$dim[5] > .01*($dim[1]-$dim[0])*($dim[3]-$dim[2])) { +} elsif ( $dim[5]*$dim[5] > .01*($dim[1]-$dim[0])*($dim[3]-$dim[2]) ) { print STDERR "Warning: Device far behind Z==0 plane\n"; } # Assume Zmax==0 to derive thickness so pkgBSDF will work $wrapper .= ' -f "t=' . (-$dim[4]) . ';w=' . ($dim[1] - $dim[0]) . ';h=' . ($dim[3] - $dim[2]) . '"'; +$wrapper .= " -g $mgfscn" if ( $geout ); # Calculate CIE (u',v') from Radiance RGB: my $CIEuv = 'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' . 'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' . 'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' . 'den=Xi+15*Yi+3*Zi;' . - 'uprime=4*Xi/den;vprime=9*Yi/den;' ; + 'uprime=if(Yi,4*Xi/den,4/19);' . + 'vprime=if(Yi,9*Yi/den,9/19);' ; my $FEPS = 1e-5; my $ns = 2**$ttlog2; my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1); my $ny = int($nsamp/$nx + 1); $nsamp = $nx * $ny; $rfluxmtx .= " -n $nproc -c $nsamp"; -if (!defined $recovery) { +if ( !defined $recovery ) { open(MYAVH, "> $td/savedARGV.txt"); foreach (@savedARGV) { print MYAVH "$_\n"; @@ -214,7 +220,6 @@ if (!defined $recovery) { open(MGFSCN, ">> $mgfscn"); print MGFSCN "xf\n"; close MGFSCN; - $wrapper .= " -g $mgfscn"; } # Create receiver & sender surfaces (rectangular) open(RADSCN, "> $receivers"); @@ -248,23 +253,7 @@ if (!defined $recovery) { } print STDERR "Recover using: $0 -recover $td\n"; } -open(MYPH, ">> $td/phase.txt"); -{ # unbuffer output to MYPH - my $ofh = select MYPH; - $| = 1; - select $ofh; -} $curphase = 0; -# Function to determine if next phase should be skipped or recovered -sub do_phase { - $curphase++; - if (defined $recovery) { - if ($recovery == $curphase) { return -1; } - if ($recovery > $curphase) { return 0; } - } - print MYPH "$curphase\n"; - return 1; -} # Create data segments (all the work happens here) if ( $tensortree ) { do_tree_bsdf(); @@ -272,14 +261,45 @@ if ( $tensortree ) { do_matrix_bsdf(); } # Output XML -print STDERR "Running: $wrapper\n"; +# print STDERR "Running: $wrapper\n"; system "$wrapper -C \"Created by: genBSDF @savedARGV\""; die "Could not wrap BSDF data\n" if ( $? ); # Clean up temporary files and exit exec $rmtmp; -#-------------- End of main program segment --------------# +#============== End of main program segment ==============# +# Function to determine if next phase should be skipped or recovered +sub do_phase { + $curphase++; + if ( defined $recovery ) { + if ( $recovery > $curphase ) { return 0; } + if ( $recovery == $curphase ) { return -1; } + } + open(MYPH, ">> $td/phase.txt"); + print MYPH "$curphase\n"; + close MYPH; + return 1; +} + +# Check if we are in active phase (not skipping parts) +sub active_phase { + if ( defined $recovery ) { + if ( $recovery > $curphase ) { return 0; } + if ( $recovery == $curphase ) { return -1; } + } + return 1; +} + +# Function to run program and check output if in active phase +sub run_check { + if ( !active_phase() ) { return; } + my $cmd = shift; + # print STDERR "Running: $cmd\n"; + system $cmd; + die "Failure running: $cmd\n" if ( $? ); +} + #++++++++++++++ Tensor tree BSDF generation ++++++++++++++# sub do_tree_bsdf { @@ -292,9 +312,8 @@ sub do_tree_bsdf { # Call rfluxmtx and process tensor tree BSDF for the given direction sub do_ttree_dir { my $forw = shift; - my $r = do_phase(); - if (!$r) { return; } - $r = ($r < 0) ? " -r" : ""; + my $dop = do_phase(); + my $r = ($dop < 0) ? " -r" : ""; my $cmd; if ( $tensortree == 3 ) { # Isotropic BSDF @@ -304,7 +323,11 @@ sub do_ttree_dir { 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 "r4=rand(-1.5839226*recno-59.82712)" } . + qq{-e "odds(n):if(.5*n-floor(.5*n)-.25,-1,1)"} . + qq{-e "Dx=1-(\$1+r1)/$ns2" } . + qq{-e "Dy=odds(\$1)/$ns*r2" } . + qq{-e "Dz=sqrt(1-Dx*Dx-Dy*Dy)" } . 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)" } . @@ -313,13 +336,17 @@ sub do_ttree_dir { "| $rfluxmtx$r -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{| 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 'r4=rand(-1.5839226*recno-59.82712)' } . + qq{-e 'odds(n):if(.5*n-floor(.5*n)-.25,-1,1)' } . + qq{-e 'Dx=1-(\$1+r1)/$ns2' } . + qq{-e 'Dy=odds(\$1)/$ns*r2' } . + qq{-e 'Dz=sqrt(1-Dx*Dx-Dy*Dy)' } . + qq{-e 'xp=(\$3+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' } . + qq{-e 'yp=(\$2+r4)*(($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$r -h -ff -y $ns2 - $receivers -i $octree"; @@ -333,9 +360,11 @@ sub do_ttree_dir { $cmd = "$rfluxmtx$r -h -ff $sender $receivers -i $octree"; } } - print STDERR "Starting: $cmd\n"; - system $cmd; - die "Failure running rfluxmtx" if ( $? ); + if ( $dop ) { + # print STDERR "Running: $cmd\n"; + system $cmd; + die "Failure running rfluxmtx" if ( $? ); + } ttree_out($forw); } # end of do_ttree_dir() @@ -407,7 +436,7 @@ sub ttree_comp { } } if ($pctcull >= 0) { - my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : ""; + my $avg = ( $dorecip && "$typ" =~ /^r[fb]/ ) ? " -a" : ""; my $pcull = ("$spec" eq "Visible") ? $pctcull : (100 - (100-$pctcull)*.25) ; if ($windoz) { @@ -418,27 +447,27 @@ sub ttree_comp { $cmd .= " -of $src " . "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2"; } - print STDERR "Running: $cmd\n"; - system "$cmd > $dest"; - die "Failure running rttree_reduce" if ( $? ); + run_check "$cmd > $dest"; } 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"; + if ( active_phase() ) { + 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; } - print DATOUT "}\n"; - close DATOUT; } if ( "$spec" ne "$curspec" ) { $wrapper .= " -s $spec"; @@ -461,15 +490,15 @@ sub do_matrix_bsdf { # Call rfluxmtx and process tensor tree BSDF for the given direction sub do_matrix_dir { my $forw = shift; - my $r = do_phase(); - if (!$r) { return; } - $r = ($r < 0) ? " -r" : ""; - my $cmd; + my $dop = do_phase(); + my $r = ($dop < 0) ? " -r" : ""; my $sender = ($bsender,$fsender)[$forw]; - $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree"; - print STDERR "Starting: $cmd\n"; - system $cmd; - die "Failure running rfluxmtx" if ( $? ); + my $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree"; + if ( $dop ) { + # print STDERR "Running: $cmd\n"; + system $cmd; + die "Failure running rfluxmtx" if ( $? ); + } matrix_out($forw); } # end of do_matrix_dir() @@ -513,10 +542,8 @@ sub matrix_comp { } elsif ("$spec" eq "CIE-Z") { $cmd .= " -c 0.0241 0.1229 0.8530"; } - $cmd .= " $src | rcollate -ho -oc 145"; - print STDERR "Running: $cmd\n"; - system "$cmd > $dest"; - die "Failure running rmtxop" if ( $? ); + $cmd .= " $src | getinfo -"; + run_check "$cmd > $dest"; if ( "$spec" ne "$curspec" ) { $wrapper .= " -s $spec"; $curspec = $spec;