| 9 | 
  | 
my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64"); | 
| 10 | 
  | 
use File::Temp qw/ :mktemp  /; | 
| 11 | 
  | 
sub userror { | 
| 12 | 
< | 
        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"; | 
| 12 | 
> | 
        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"; | 
| 13 | 
  | 
        exit 1; | 
| 14 | 
  | 
} | 
| 15 | 
  | 
my ($td,$radscn,$mgfscn,$octree,$fsender,$bsender,$receivers,$facedat,$behinddat,$rmtmp); | 
| 84 | 
  | 
        $rmtmp = "rm -rf $td"; | 
| 85 | 
  | 
} | 
| 86 | 
  | 
my @savedARGV = @ARGV; | 
| 87 | 
< | 
my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6"; | 
| 87 | 
> | 
my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6 -w-"; | 
| 88 | 
  | 
my $wrapper = "wrapBSDF"; | 
| 89 | 
  | 
my $tensortree = 0; | 
| 90 | 
  | 
my $ttlog2 = 4; | 
| 91 | 
+ | 
my $dorecip = 1; | 
| 92 | 
  | 
my $nsamp = 2000; | 
| 93 | 
  | 
my $mgfin = 0; | 
| 94 | 
  | 
my $geout = 1; | 
| 116 | 
  | 
                shift @ARGV; | 
| 117 | 
  | 
        } elsif ("$ARGV[0]" =~ /^[-+]C/) { | 
| 118 | 
  | 
                $docolor = ("$ARGV[0]" =~ /^\+/); | 
| 119 | 
+ | 
        } elsif ("$ARGV[0]" =~ /^[-+]a/) { | 
| 120 | 
+ | 
                $dorecip = ("$ARGV[0]" =~ /^\+/); | 
| 121 | 
  | 
        } elsif ("$ARGV[0]" =~ /^[-+]f/) { | 
| 122 | 
  | 
                $doforw = ("$ARGV[0]" =~ /^\+/); | 
| 123 | 
  | 
        } elsif ("$ARGV[0]" =~ /^[-+]b/) { | 
| 173 | 
  | 
        } | 
| 174 | 
  | 
} | 
| 175 | 
  | 
if ( $#dim != 5 ) { | 
| 176 | 
< | 
        @dim = split ' ', `getbbox -h $radscn`; | 
| 176 | 
> | 
        @dim = split ' ', `getbbox -h -w $radscn`; | 
| 177 | 
  | 
} | 
| 178 | 
  | 
die "Device entirely inside room!\n" if ( $dim[4] >= 0 ); | 
| 179 | 
  | 
if ( $dim[5] > 1e-5 ) { | 
| 189 | 
  | 
my $CIEuv =     'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' . | 
| 190 | 
  | 
                'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' . | 
| 191 | 
  | 
                'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' . | 
| 192 | 
< | 
                'den=Xi+15*Yi+3*Zi;' . | 
| 192 | 
> | 
                'den=Xi+15*Yi+3*Zi+1e-9;' . | 
| 193 | 
  | 
                'uprime=4*Xi/den;vprime=9*Yi/den;' ; | 
| 194 | 
  | 
my $FEPS = 1e-5; | 
| 195 | 
  | 
my $ns = 2**$ttlog2; | 
| 204 | 
  | 
        } | 
| 205 | 
  | 
        close MYAVH; | 
| 206 | 
  | 
        # Generate octree | 
| 207 | 
< | 
        system "oconv -w $radscn > $octree"; | 
| 207 | 
> | 
        system "oconv -w -f $radscn > $octree"; | 
| 208 | 
  | 
        die "Could not compile scene\n" if ( $? ); | 
| 209 | 
  | 
        # Add MGF description if requested | 
| 210 | 
  | 
        if ( $geout ) { | 
| 252 | 
  | 
        } | 
| 253 | 
  | 
        print STDERR "Recover using: $0 -recover $td\n"; | 
| 254 | 
  | 
} | 
| 252 | 
– | 
# Open unbuffered progress file | 
| 253 | 
– | 
open(MYPH, ">> $td/phase.txt"); | 
| 254 | 
– | 
{ | 
| 255 | 
– | 
        my $ofh = select MYPH; | 
| 256 | 
– | 
        $| = 1; | 
| 257 | 
– | 
        select $ofh; | 
| 258 | 
– | 
} | 
| 255 | 
  | 
$curphase = 0; | 
| 256 | 
  | 
# Create data segments (all the work happens here) | 
| 257 | 
  | 
if ( $tensortree ) { | 
| 271 | 
  | 
# Function to determine if next phase should be skipped or recovered | 
| 272 | 
  | 
sub do_phase { | 
| 273 | 
  | 
        $curphase++; | 
| 274 | 
< | 
        if (defined $recovery) { | 
| 275 | 
< | 
                if ($recovery > $curphase) { return 0; } | 
| 276 | 
< | 
                if ($recovery == $curphase) { return -1; } | 
| 274 | 
> | 
        if ( defined $recovery ) { | 
| 275 | 
> | 
                if ( $recovery > $curphase ) { return 0; } | 
| 276 | 
> | 
                if ( $recovery == $curphase ) { return -1; } | 
| 277 | 
  | 
        } | 
| 278 | 
+ | 
        open(MYPH, ">> $td/phase.txt"); | 
| 279 | 
  | 
        print MYPH "$curphase\n"; | 
| 280 | 
+ | 
        close MYPH; | 
| 281 | 
  | 
        return 1; | 
| 282 | 
  | 
} | 
| 283 | 
  | 
 | 
| 284 | 
+ | 
# Check if we are in active phase (not skipping parts) | 
| 285 | 
+ | 
sub active_phase { | 
| 286 | 
+ | 
        if ( defined $recovery ) { | 
| 287 | 
+ | 
                if ( $recovery > $curphase ) { return 0; } | 
| 288 | 
+ | 
                if ( $recovery == $curphase ) { return -1; } | 
| 289 | 
+ | 
        } | 
| 290 | 
+ | 
        return 1; | 
| 291 | 
+ | 
} | 
| 292 | 
+ | 
 | 
| 293 | 
+ | 
# Function to run program and check output if in active phase | 
| 294 | 
+ | 
sub run_check { | 
| 295 | 
+ | 
        if ( !active_phase() ) { return; } | 
| 296 | 
+ | 
        my $cmd = shift; | 
| 297 | 
+ | 
        # print STDERR "Running: $cmd\n"; | 
| 298 | 
+ | 
        system $cmd; | 
| 299 | 
+ | 
        die "Failure running: $cmd\n" if ( $? ); | 
| 300 | 
+ | 
} | 
| 301 | 
+ | 
 | 
| 302 | 
  | 
#++++++++++++++ Tensor tree BSDF generation ++++++++++++++# | 
| 303 | 
  | 
sub do_tree_bsdf { | 
| 304 | 
  | 
 | 
| 311 | 
  | 
# Call rfluxmtx and process tensor tree BSDF for the given direction | 
| 312 | 
  | 
sub do_ttree_dir { | 
| 313 | 
  | 
        my $forw = shift; | 
| 314 | 
< | 
        my $r = do_phase(); | 
| 315 | 
< | 
        if (!$r) { return; } | 
| 300 | 
< | 
        $r = ($r < 0) ? " -r" : ""; | 
| 314 | 
> | 
        my $dop = do_phase(); | 
| 315 | 
> | 
        my $r = ($dop < 0) ? " -r" : ""; | 
| 316 | 
  | 
        my $cmd; | 
| 317 | 
  | 
        if ( $tensortree == 3 ) { | 
| 318 | 
  | 
                # Isotropic BSDF | 
| 331 | 
  | 
                                "| $rfluxmtx$r -fa -y $ns2 - $receivers -i $octree"; | 
| 332 | 
  | 
                } else { | 
| 333 | 
  | 
                        $cmd = "cnt $ns2 $ny $nx " . | 
| 334 | 
< | 
                                qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } . | 
| 335 | 
< | 
                                qq{-e "r2=rand(-5.37138*recno+67.1737811)" } . | 
| 336 | 
< | 
                                qq{-e "r3=rand(+3.17603772*recno+83.766771)" } . | 
| 337 | 
< | 
                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } . | 
| 338 | 
< | 
                                qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } . | 
| 339 | 
< | 
                                qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } . | 
| 340 | 
< | 
                                qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } . | 
| 334 | 
> | 
                                qq{| rcalc -e 'r1=rand(.8681*recno-.673892)' } . | 
| 335 | 
> | 
                                qq{-e 'r2=rand(-5.37138*recno+67.1737811)' } . | 
| 336 | 
> | 
                                qq{-e 'r3=rand(+3.17603772*recno+83.766771)' } . | 
| 337 | 
> | 
                                qq{-e 'Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' } . | 
| 338 | 
> | 
                                qq{-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' } . | 
| 339 | 
> | 
                                qq{-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' } . | 
| 340 | 
> | 
                                qq{-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' } . | 
| 341 | 
  | 
                                qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } . | 
| 342 | 
  | 
                                qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } . | 
| 343 | 
  | 
                                "| $rfluxmtx$r -h -ff -y $ns2 - $receivers -i $octree"; | 
| 351 | 
  | 
                        $cmd = "$rfluxmtx$r -h -ff $sender $receivers -i $octree"; | 
| 352 | 
  | 
                } | 
| 353 | 
  | 
        } | 
| 354 | 
< | 
        # print STDERR "Starting: $cmd\n"; | 
| 355 | 
< | 
        system $cmd; | 
| 356 | 
< | 
        die "Failure running rfluxmtx" if ( $? ); | 
| 354 | 
> | 
        if ( $dop ) { | 
| 355 | 
> | 
                # print STDERR "Running: $cmd\n"; | 
| 356 | 
> | 
                system $cmd; | 
| 357 | 
> | 
                die "Failure running rfluxmtx" if ( $? ); | 
| 358 | 
> | 
        } | 
| 359 | 
  | 
        ttree_out($forw); | 
| 360 | 
  | 
}       # end of do_ttree_dir() | 
| 361 | 
  | 
 | 
| 427 | 
  | 
                } | 
| 428 | 
  | 
        } | 
| 429 | 
  | 
        if ($pctcull >= 0) { | 
| 430 | 
< | 
                my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : ""; | 
| 430 | 
> | 
                my $avg = ( $dorecip && "$typ" =~ /^r[fb]/ ) ? " -a" : ""; | 
| 431 | 
  | 
                my $pcull = ("$spec" eq "Visible") ? $pctcull : | 
| 432 | 
  | 
                                                     (100 - (100-$pctcull)*.25) ; | 
| 433 | 
  | 
                if ($windoz) { | 
| 438 | 
  | 
                        $cmd .= " -of $src " . | 
| 439 | 
  | 
                                        "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2"; | 
| 440 | 
  | 
                } | 
| 441 | 
< | 
                # print STDERR "Running: $cmd\n"; | 
| 425 | 
< | 
                system "$cmd > $dest"; | 
| 426 | 
< | 
                die "Failure running rttree_reduce" if ( $? ); | 
| 441 | 
> | 
                run_check "$cmd > $dest"; | 
| 442 | 
  | 
        } else { | 
| 443 | 
  | 
                if ($windoz) { | 
| 444 | 
  | 
                        $cmd = "rcollate -ho -oc 1 $src | " . $cmd ; | 
| 445 | 
  | 
                } else { | 
| 446 | 
  | 
                        $cmd .= " $src"; | 
| 447 | 
  | 
                } | 
| 448 | 
< | 
                open(DATOUT, "> $dest"); | 
| 449 | 
< | 
                print DATOUT "{\n"; | 
| 450 | 
< | 
                close DATOUT; | 
| 451 | 
< | 
                # print STDERR "Running: $cmd\n"; | 
| 452 | 
< | 
                system "$cmd >> $dest"; | 
| 453 | 
< | 
                die "Failure running rcalc" if ( $? ); | 
| 454 | 
< | 
                open(DATOUT, ">> $dest"); | 
| 455 | 
< | 
                for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) { | 
| 456 | 
< | 
                        print DATOUT "0\n"; | 
| 448 | 
> | 
                if ( active_phase() ) { | 
| 449 | 
> | 
                        open(DATOUT, "> $dest"); | 
| 450 | 
> | 
                        print DATOUT "{\n"; | 
| 451 | 
> | 
                        close DATOUT; | 
| 452 | 
> | 
                        # print STDERR "Running: $cmd\n"; | 
| 453 | 
> | 
                        system "$cmd >> $dest"; | 
| 454 | 
> | 
                        die "Failure running rcalc" if ( $? ); | 
| 455 | 
> | 
                        open(DATOUT, ">> $dest"); | 
| 456 | 
> | 
                        for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) { | 
| 457 | 
> | 
                                print DATOUT "0\n"; | 
| 458 | 
> | 
                        } | 
| 459 | 
> | 
                        print DATOUT "}\n"; | 
| 460 | 
> | 
                        close DATOUT; | 
| 461 | 
  | 
                } | 
| 443 | 
– | 
                print DATOUT "}\n"; | 
| 444 | 
– | 
                close DATOUT; | 
| 462 | 
  | 
        } | 
| 463 | 
  | 
        if ( "$spec" ne "$curspec" ) { | 
| 464 | 
  | 
                $wrapper .= " -s $spec"; | 
| 481 | 
  | 
# Call rfluxmtx and process tensor tree BSDF for the given direction | 
| 482 | 
  | 
sub do_matrix_dir { | 
| 483 | 
  | 
        my $forw = shift; | 
| 484 | 
< | 
        my $r = do_phase(); | 
| 485 | 
< | 
        if (!$r) { return; } | 
| 469 | 
< | 
        $r = ($r < 0) ? " -r" : ""; | 
| 470 | 
< | 
        my $cmd; | 
| 484 | 
> | 
        my $dop = do_phase(); | 
| 485 | 
> | 
        my $r = ($dop < 0) ? " -r" : ""; | 
| 486 | 
  | 
        my $sender = ($bsender,$fsender)[$forw]; | 
| 487 | 
< | 
        $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree"; | 
| 488 | 
< | 
        # print STDERR "Starting: $cmd\n"; | 
| 489 | 
< | 
        system $cmd; | 
| 490 | 
< | 
        die "Failure running rfluxmtx" if ( $? ); | 
| 487 | 
> | 
        my $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree"; | 
| 488 | 
> | 
        if ( $dop ) { | 
| 489 | 
> | 
                # print STDERR "Running: $cmd\n"; | 
| 490 | 
> | 
                system $cmd; | 
| 491 | 
> | 
                die "Failure running rfluxmtx" if ( $? ); | 
| 492 | 
> | 
        } | 
| 493 | 
  | 
        matrix_out($forw); | 
| 494 | 
  | 
}       # end of do_matrix_dir() | 
| 495 | 
  | 
 | 
| 533 | 
  | 
        } elsif ("$spec" eq "CIE-Z") { | 
| 534 | 
  | 
                $cmd .= " -c 0.0241 0.1229 0.8530"; | 
| 535 | 
  | 
        } | 
| 536 | 
< | 
        $cmd .= " $src | rcollate -ho -oc 145"; | 
| 537 | 
< | 
        # print STDERR "Running: $cmd\n"; | 
| 521 | 
< | 
        system "$cmd > $dest"; | 
| 522 | 
< | 
        die "Failure running rmtxop" if ( $? ); | 
| 536 | 
> | 
        $cmd .= " $src | getinfo -"; | 
| 537 | 
> | 
        run_check "$cmd > $dest"; | 
| 538 | 
  | 
        if ( "$spec" ne "$curspec" ) { | 
| 539 | 
  | 
                $wrapper .= " -s $spec"; | 
| 540 | 
  | 
                $curspec = $spec; |