| 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\"][-f \"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][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom units] [input ..]\n"; | 
| 13 |  | exit 1; | 
| 14 |  | } | 
| 15 |  | my ($td,$radscn,$mgfscn,$octree,$fsender,$bsender,$receivers,$facedat,$behinddat,$rmtmp); | 
| 66 |  | $rmtmp = "rm -rf $td"; | 
| 67 |  | } | 
| 68 |  | my @savedARGV = @ARGV; | 
| 69 | < | my $rfluxmtx = "rfluxmtx -v -ab 5 -ad 700 -lw 3e-6"; | 
| 69 | > | my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6"; | 
| 70 |  | my $wrapper = "wrapBSDF"; | 
| 71 |  | my $tensortree = 0; | 
| 72 |  | my $ttlog2 = 4; | 
| 104 |  | } elsif ("$ARGV[0]" eq "-t") { | 
| 105 |  | # Use value < 0 for rttree_reduce bypass | 
| 106 |  | $pctcull = $ARGV[1]; | 
| 107 | + | if ($pctcull >= 100) { | 
| 108 | + | die "Illegal -t culling percentage, must be < 100\n"; | 
| 109 | + | } | 
| 110 |  | shift @ARGV; | 
| 111 |  | } elsif ("$ARGV[0]" =~ /^-t[34]$/) { | 
| 112 |  | $tensortree = substr($ARGV[0], 2, 1); | 
| 113 |  | $ttlog2 = $ARGV[1]; | 
| 114 |  | shift @ARGV; | 
| 115 | < | } elsif ("$ARGV[0]" eq "-f") { | 
| 115 | > | } elsif ("$ARGV[0]" eq "-s") { | 
| 116 |  | $wrapper .= " -f \"$ARGV[1]\""; | 
| 117 |  | shift @ARGV; | 
| 118 |  | } elsif ("$ARGV[0]" eq "-W") { | 
| 174 |  | $wrapper .= " -g $mgfscn"; | 
| 175 |  | } | 
| 176 |  | # Create receiver & sender surfaces (rectangular) | 
| 177 | + | my $FEPS = 1e-5; | 
| 178 |  | my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1); | 
| 179 |  | my $ny = int($nsamp/$nx + 1); | 
| 180 |  | $nsamp = $nx * $ny; | 
| 181 |  | my $ns = 2**$ttlog2; | 
| 182 |  | open(RADSCN, "> $receivers"); | 
| 183 | < | print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=sc$ns\n" : "h=kf\n"); | 
| 184 | < | print RADSCN '#@rfluxmtx ' . "u=Y o=$facedat\n\n"; | 
| 183 | > | print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=-sc$ns\n" : "h=-kf\n"); | 
| 184 | > | print RADSCN '#@rfluxmtx ' . "u=-Y o=$facedat\n\n"; | 
| 185 |  | print RADSCN "void glow receiver_face\n0\n0\n4 1 1 1 0\n\n"; | 
| 186 |  | print RADSCN "receiver_face source f_receiver\n0\n0\n4 0 0 1 180\n\n"; | 
| 187 | < | print RADSCN '#@rfluxmtx ' . "u=Y o=$behinddat\n\n"; | 
| 187 | > | print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=+sc$ns\n" : "h=+kf\n"); | 
| 188 | > | print RADSCN '#@rfluxmtx ' . "u=-Y o=$behinddat\n\n"; | 
| 189 |  | print RADSCN "void glow receiver_behind\n0\n0\n4 1 1 1 0\n\n"; | 
| 190 |  | print RADSCN "receiver_behind source b_receiver\n0\n0\n4 0 0 -1 180\n"; | 
| 191 |  | close RADSCN; | 
| 193 |  | $rfluxmtx .= " -n $nproc -c $nsamp"; | 
| 194 |  | if ( $tensortree != 3 ) {       # Isotropic tensor tree is exception | 
| 195 |  | open (RADSCN, "> $fsender"); | 
| 196 | < | print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n"); | 
| 196 | > | print RADSCN '#@rfluxmtx u=-Y ' . ($tensortree ? "h=-sc$ns\n\n" : "h=-kf\n\n"); | 
| 197 |  | print RADSCN "void polygon fwd_sender\n0\n0\n12\n"; | 
| 198 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[4]; | 
| 199 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[4]; | 
| 200 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[4]; | 
| 201 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[4]; | 
| 198 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[2], $dim[4]-$FEPS; | 
| 199 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[3], $dim[4]-$FEPS; | 
| 200 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[3], $dim[4]-$FEPS; | 
| 201 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[2], $dim[4]-$FEPS; | 
| 202 |  | close RADSCN; | 
| 203 |  | open (RADSCN, "> $bsender"); | 
| 204 | < | print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n"); | 
| 205 | < | print RADSCN "void polygon fwd_sender\n0\n0\n12\n"; | 
| 206 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[5]; | 
| 207 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[5]; | 
| 208 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[5]; | 
| 209 | < | printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[5]; | 
| 204 | > | print RADSCN '#@rfluxmtx u=-Y ' . ($tensortree ? "h=+sc$ns\n\n" : "h=+kf\n\n"); | 
| 205 | > | print RADSCN "void polygon bwd_sender\n0\n0\n12\n"; | 
| 206 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[2], $dim[5]+$FEPS; | 
| 207 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[2], $dim[5]+$FEPS; | 
| 208 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[3], $dim[5]+$FEPS; | 
| 209 | > | printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[3], $dim[5]+$FEPS; | 
| 210 |  | close RADSCN; | 
| 211 |  | } | 
| 212 | + | # Calculate CIE (u',v') from Radiance RGB: | 
| 213 | + | my $CIEuv =     'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' . | 
| 214 | + | 'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' . | 
| 215 | + | 'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' . | 
| 216 | + | 'den=Xi+15*Yi+3*Zi;' . | 
| 217 | + | 'uprime=4*Xi/den;vprime=9*Yi/den;' ; | 
| 218 |  | # Create data segments (all the work happens here) | 
| 219 |  | if ( $tensortree ) { | 
| 220 |  | do_tree_bsdf(); | 
| 222 |  | do_matrix_bsdf(); | 
| 223 |  | } | 
| 224 |  | # Output XML | 
| 214 | – | my $old_fh = select(STDOUT); | 
| 215 | – | $| = 1; | 
| 216 | – | select($old_fh); | 
| 217 | – | print "<!-- Created by: genBSDF @savedARGV -->\n"; | 
| 225 |  | # print STDERR "Running: $wrapper\n"; | 
| 226 | < | system $wrapper; | 
| 226 | > | system "$wrapper -C \"Created by: genBSDF @savedARGV\""; | 
| 227 |  | die "Could not wrap BSDF data\n" if ( $? ); | 
| 228 |  | # Clean up temporary files and exit | 
| 229 | < | system $rmtmp; | 
| 229 | > | exec $rmtmp; | 
| 230 |  |  | 
| 231 |  | #-------------- End of main program segment --------------# | 
| 232 |  |  | 
| 302 |  | my $ttyp = ("tb","tf")[$forw]; | 
| 303 |  | ttree_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]); | 
| 304 |  | if ( $docolor ) { | 
| 305 | < | ttree_comp($ttyp, "CIE-X", $transdat, ($tbx,$tfx)[$forw]); | 
| 306 | < | ttree_comp($ttyp, "CIE-Z", $transdat, ($tbz,$tfz)[$forw]); | 
| 305 | > | ttree_comp($ttyp, "CIE-u", $transdat, ($tbx,$tfx)[$forw]); | 
| 306 | > | ttree_comp($ttyp, "CIE-v", $transdat, ($tbz,$tfz)[$forw]); | 
| 307 |  | } | 
| 308 |  | } | 
| 309 |  | # Output reflection | 
| 310 |  | my $rtyp = ("rb","rf")[$forw]; | 
| 311 |  | ttree_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]); | 
| 312 |  | if ( $docolor ) { | 
| 313 | < | ttree_comp($rtyp, "CIE-X", $refldat, ($rbx,$rfx)[$forw]); | 
| 314 | < | ttree_comp($rtyp, "CIE-Z", $refldat, ($rbz,$rfz)[$forw]); | 
| 313 | > | ttree_comp($rtyp, "CIE-u", $refldat, ($rbx,$rfx)[$forw]); | 
| 314 | > | ttree_comp($rtyp, "CIE-v", $refldat, ($rbz,$rfz)[$forw]); | 
| 315 |  | } | 
| 316 |  | }       # end of ttree_out() | 
| 317 |  |  | 
| 325 |  | if ($windoz) { | 
| 326 |  | if ("$spec" eq "Visible") { | 
| 327 |  | $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } . | 
| 328 | < | q{-e "$1=(0.265*$1+0.670*$2+0.065*$3)/Omega"}; | 
| 329 | < | } elsif ("$spec" eq "CIE-X") { | 
| 330 | < | $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } . | 
| 331 | < | q{-e "$1=(0.514*$1+0.324*$2+0.162*$3)/Omega"}; | 
| 332 | < | } elsif ("$spec" eq "CIE-Z") { | 
| 333 | < | $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } . | 
| 334 | < | q{-e "$1=(0.024*$1+0.123*$2+0.853*$3)/Omega"}; | 
| 328 | > | q{-e "Ri=$1;Gi=$2;Bi=$3" } . | 
| 329 | > | qq{-e "$CIEuv" } . | 
| 330 | > | q{-e "$1=Yi/Omega"}; | 
| 331 | > | } elsif ("$spec" eq "CIE-u") { | 
| 332 | > | $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } . | 
| 333 | > | qq{-e "$CIEuv" } . | 
| 334 | > | q{-e "$1=uprime"}; | 
| 335 | > | } elsif ("$spec" eq "CIE-v") { | 
| 336 | > | $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } . | 
| 337 | > | qq{-e "$CIEuv" } . | 
| 338 | > | q{-e "$1=vprime"}; | 
| 339 |  | } | 
| 340 |  | } else { | 
| 341 |  | if ("$spec" eq "Visible") { | 
| 342 |  | $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . | 
| 343 | < | q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega'}; | 
| 344 | < | } elsif ("$spec" eq "CIE-X") { | 
| 345 | < | $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . | 
| 346 | < | q{-e '$1=(0.514*$1+0.324*$2+0.162*$3)/Omega'}; | 
| 347 | < | } elsif ("$spec" eq "CIE-Z") { | 
| 348 | < | $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . | 
| 349 | < | q{-e '$1=(0.024*$1+0.123*$2+0.853*$3)/Omega'}; | 
| 343 | > | q{-e 'Ri=$1;Gi=$2;Bi=$3' } . | 
| 344 | > | "-e '$CIEuv' " . | 
| 345 | > | q{-e '$1=Yi/Omega'}; | 
| 346 | > | } elsif ("$spec" eq "CIE-u") { | 
| 347 | > | $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } . | 
| 348 | > | "-e '$CIEuv' " . | 
| 349 | > | q{-e '$1=uprime'}; | 
| 350 | > | } elsif ("$spec" eq "CIE-v") { | 
| 351 | > | $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } . | 
| 352 | > | "-e '$CIEuv' " . | 
| 353 | > | q{-e '$1=vprime'}; | 
| 354 |  | } | 
| 355 |  | } | 
| 356 |  | if ($pctcull >= 0) { | 
| 357 |  | my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : ""; | 
| 358 | + | my $pcull = ("$spec" eq "Visible") ? $pctcull : | 
| 359 | + | (100 - (100-$pctcull)*.25) ; | 
| 360 |  | if ($windoz) { | 
| 361 |  | $cmd = "rcollate -ho -oc 1 $src | " . | 
| 362 |  | $cmd . | 
| 363 | < | " | rttree_reduce$avg -h -fa -t $pctcull -r $tensortree -g $ttlog2"; | 
| 363 | > | " | rttree_reduce$avg -h -fa -t $pcull -r $tensortree -g $ttlog2"; | 
| 364 |  | } else { | 
| 365 |  | $cmd .= " -of $src " . | 
| 366 | < | "| rttree_reduce$avg -h -ff -t $pctcull -r $tensortree -g $ttlog2"; | 
| 366 | > | "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2"; | 
| 367 |  | } | 
| 368 |  | # print STDERR "Running: $cmd\n"; | 
| 369 |  | system "$cmd > $dest"; | 
| 451 |  | my $dest = shift; | 
| 452 |  | my $cmd = "rmtxop -fa -t"; | 
| 453 |  | if ("$spec" eq "Visible") { | 
| 454 | < | $cmd .= " -c 0.265 0.670 0.065"; | 
| 454 | > | $cmd .= " -c 0.2651 0.6701 0.0648"; | 
| 455 |  | } elsif ("$spec" eq "CIE-X") { | 
| 456 | < | $cmd .= " -c 0.514 0.324 0.162"; | 
| 456 | > | $cmd .= " -c 0.5141 0.3239 0.1620"; | 
| 457 |  | } elsif ("$spec" eq "CIE-Z") { | 
| 458 | < | $cmd .= " -c 0.024 0.123 0.853"; | 
| 458 | > | $cmd .= " -c 0.0241 0.1229 0.8530"; | 
| 459 |  | } | 
| 460 | < | $cmd .= " $src | rcollate -ho"; | 
| 460 | > | $cmd .= " $src | rcollate -ho -oc 145"; | 
| 461 |  | # print STDERR "Running: $cmd\n"; | 
| 462 |  | system "$cmd > $dest"; | 
| 463 | < | die "Failure running rttree_reduce" if ( $? ); | 
| 463 | > | die "Failure running rmtxop" if ( $? ); | 
| 464 |  | if ( "$spec" ne "$curspec" ) { | 
| 465 |  | $wrapper .= " -s $spec"; | 
| 466 |  | $curspec = $spec; |