| 7 |  | # | 
| 8 |  | use strict; | 
| 9 |  | sub userror { | 
| 10 | < | print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-dim xmin xmax ymin ymax zmin zmax][{+|-}mgf][{+|-}geom] [input ..]\n"; | 
| 10 | > | print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-dim xmin xmax ymin ymax zmin zmax][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom] [input ..]\n"; | 
| 11 |  | exit 1; | 
| 12 |  | } | 
| 13 |  | my $td = `mktemp -d /tmp/genBSDF.XXXXXX`; | 
| 14 |  | chomp $td; | 
| 15 |  | my $nsamp = 1000; | 
| 16 | + | my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6"; | 
| 17 |  | my $mgfin = 0; | 
| 18 |  | my $geout = 1; | 
| 19 |  | my $nproc = 1; | 
| 20 | + | my $doforw = 0; | 
| 21 | + | my $doback = 1; | 
| 22 |  | my @dim; | 
| 23 |  | # Get options | 
| 24 |  | while ($#ARGV >= 0) { | 
| 25 |  | if ("$ARGV[0]" =~ /^[-+]m/) { | 
| 26 |  | $mgfin = ("$ARGV[0]" =~ /^\+/); | 
| 27 | + | } elsif ("$ARGV[0]" eq "-r") { | 
| 28 | + | $rtargs = "$rtargs $ARGV[1]"; | 
| 29 | + | shift @ARGV; | 
| 30 |  | } elsif ("$ARGV[0]" =~ /^[-+]g/) { | 
| 31 |  | $geout = ("$ARGV[0]" =~ /^\+/); | 
| 32 | + | } elsif ("$ARGV[0]" =~ /^[-+]f/) { | 
| 33 | + | $doforw = ("$ARGV[0]" =~ /^\+/); | 
| 34 | + | } elsif ("$ARGV[0]" =~ /^[-+]b/) { | 
| 35 | + | $doback = ("$ARGV[0]" =~ /^\+/); | 
| 36 |  | } elsif ("$ARGV[0]" eq "-c") { | 
| 37 |  | $nsamp = $ARGV[1]; | 
| 38 |  | shift @ARGV; | 
| 50 |  | } | 
| 51 |  | shift @ARGV; | 
| 52 |  | } | 
| 53 | + | # Check that we're actually being asked to do something | 
| 54 | + | die "Must have at least one of +forward or +backward" if (!$doforw && !$doback); | 
| 55 |  | # Get scene description and dimensions | 
| 56 |  | my $radscn = "$td/device.rad"; | 
| 57 |  | my $mgfscn = "$td/device.mgf"; | 
| 69 |  | @dim = split ' ', `getbbox -h $radscn`; | 
| 70 |  | } | 
| 71 |  | print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5); | 
| 72 | < | # Add receiver surface (rectangle) | 
| 73 | < | my $modnm="_receiver_black_"; | 
| 72 | > | # Add receiver surfaces (rectangular) | 
| 73 | > | my $fmodnm="receiver_face"; | 
| 74 | > | my $bmodnm="receiver_behind"; | 
| 75 |  | open(RADSCN, ">> $radscn"); | 
| 76 | < | print RADSCN "void glow $modnm\n0\n0\n4 0 0 0 0\n\n"; | 
| 77 | < | print RADSCN "$modnm polygon _receiver_\n0\n0\n12\n"; | 
| 78 | < | print RADSCN "\t",$dim[0],"\t",$dim[2],"\t",$dim[5]+1e-5,"\n"; | 
| 79 | < | print RADSCN "\t",$dim[0],"\t",$dim[3],"\t",$dim[5]+1e-5,"\n"; | 
| 67 | < | print RADSCN "\t",$dim[1],"\t",$dim[3],"\t",$dim[5]+1e-5,"\n"; | 
| 68 | < | print RADSCN "\t",$dim[1],"\t",$dim[2],"\t",$dim[5]+1e-5,"\n"; | 
| 76 | > | print RADSCN "void glow $fmodnm\n0\n0\n4 1 1 1 0\n\n"; | 
| 77 | > | print RADSCN "$fmodnm source f_receiver\n0\n0\n4 0 0 1 180\n"; | 
| 78 | > | print RADSCN "void glow $bmodnm\n0\n0\n4 1 1 1 0\n\n"; | 
| 79 | > | print RADSCN "$bmodnm source b_receiver\n0\n0\n4 0 0 -1 180\n"; | 
| 80 |  | close RADSCN; | 
| 81 |  | # Generate octree | 
| 82 |  | system "oconv -w $radscn > $octree"; | 
| 83 |  | die "Could not compile scene\n" if ( $? ); | 
| 84 | < | # Set up sampling | 
| 84 | > | # Set up sampling of interior portal | 
| 85 |  | # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms | 
| 86 |  | my $tcal = ' | 
| 87 |  | DEGREE : PI/180; | 
| 96 |  | Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow); | 
| 97 |  | Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1)); | 
| 98 |  | sin_kpol = sin(Kpol); | 
| 99 | < | Dx = -cos(Kazi)*sin_kpol; | 
| 99 | > | Dx = cos(Kazi)*sin_kpol; | 
| 100 |  | Dy = sin(Kazi)*sin_kpol; | 
| 101 |  | Dz = sqrt(1 - sin_kpol*sin_kpol); | 
| 102 |  | KprojOmega = PI * if(Kbin-.5, | 
| 103 |  | (sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow), | 
| 104 |  | 1 - sq(cos(Kpola(1)*DEGREE))); | 
| 105 |  | '; | 
| 106 | < | # Compute Klems bin from exiting ray direction | 
| 106 | > | # Compute Klems bin from exiting ray direction (forward or backward) | 
| 107 |  | my $kcal = ' | 
| 108 |  | DEGREE : PI/180; | 
| 109 |  | Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x))); | 
| 126 |  | kaccum(7) + kazn(azi,360/knaz(8)), | 
| 127 |  | kaccum(8) + kazn(azi,360/knaz(9)) | 
| 128 |  | ); | 
| 129 | < | kbin = kbin2(Acos(Dz), Atan2(Dy, -Dx)); | 
| 129 | > | kbin = if(Dz, kbin2(Acos(Dz),Atan2(Dy,Dx)), kbin2(Acos(-Dz),Atan2(-Dy,-Dx))); | 
| 130 |  | '; | 
| 131 |  | my $ndiv = 145; | 
| 132 |  | my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); | 
| 133 |  | my $ny = int($nsamp/$nx + .5); | 
| 134 |  | $nsamp = $nx * $ny; | 
| 135 |  | # Compute scattering data using rtcontrib | 
| 136 | < | my $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . | 
| 136 | > | my @tfarr; | 
| 137 | > | my @rfarr; | 
| 138 | > | my @tbarr; | 
| 139 | > | my @rbarr; | 
| 140 | > | my $cmd; | 
| 141 | > | my $rtcmd = "rtcontrib -h -ff -fo -n $nproc -c $nsamp " . | 
| 142 | > | "-e '$kcal' -b kbin -bn $ndiv " . | 
| 143 | > | "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $rtargs $octree"; | 
| 144 | > | my $rccmd = "rcalc -e '$tcal' " . | 
| 145 | > | "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " . | 
| 146 | > | q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'}; | 
| 147 | > | if ( $doforw ) { | 
| 148 | > | $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . | 
| 149 |  | "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . | 
| 150 |  | "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . | 
| 151 | < | "-e 'zp:$dim[4]-1e-5' " . | 
| 151 | > | "-e 'zp:$dim[4]' " . | 
| 152 |  | q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } . | 
| 153 | < | q{-e '$1=xp;$2=yp;$3=zp;$4=Dx;$5=Dy;$6=Dz' } . | 
| 154 | < | "| rtcontrib -h -ff -n $nproc -c $nsamp -e '$kcal' -b kbin -bn $ndiv " . | 
| 155 | < | "-m $modnm -w -ab 5 -ad 700 -lw 3e-6 $octree " . | 
| 156 | < | "| rcalc -e '$tcal' " . | 
| 157 | < | "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " . | 
| 158 | < | q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'}; | 
| 159 | < | my @darr = `$cmd`; | 
| 160 | < | die "Failure running: $cmd\n" if ( $? ); | 
| 153 | > | q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } . | 
| 154 | > | "| $rtcmd"; | 
| 155 | > | system "$cmd" || die "Failure running: $cmd\n"; | 
| 156 | > | @tfarr = `$rccmd $td/$fmodnm.flt`; | 
| 157 | > | die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); | 
| 158 | > | @rfarr = `$rccmd $td/$bmodnm.flt`; | 
| 159 | > | die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); | 
| 160 | > | } | 
| 161 | > | if ( $doback ) { | 
| 162 | > | $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . | 
| 163 | > | "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . | 
| 164 | > | "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . | 
| 165 | > | "-e 'zp:$dim[5]' " . | 
| 166 | > | q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } . | 
| 167 | > | q{-e '$1=xp+Dx;$2=yp+Dy;$3=zp+Dz;$4=-Dx;$5=-Dy;$6=-Dz' } . | 
| 168 | > | "| $rtcmd"; | 
| 169 | > | system "$cmd" || die "Failure running: $cmd\n"; | 
| 170 | > | @tbarr = `$rccmd $td/$bmodnm.flt`; | 
| 171 | > | die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); | 
| 172 | > | @rbarr = `$rccmd $td/$fmodnm.flt`; | 
| 173 | > | die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); | 
| 174 | > | } | 
| 175 |  | # Output XML prologue | 
| 176 |  | print | 
| 177 |  | '<?xml version="1.0" encoding="UTF-8"?> | 
| 274 |  | </AngleBasisBlock> | 
| 275 |  | </AngleBasis> | 
| 276 |  | </DataDefinition> | 
| 277 | < | <WavelengthData> | 
| 277 | > | '; | 
| 278 | > | if ( $doforw ) { | 
| 279 | > | print '         <WavelengthData> | 
| 280 |  | <LayerNumber>System</LayerNumber> | 
| 281 |  | <Wavelength unit="Integral">Visible</Wavelength> | 
| 282 |  | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 288 |  | <ScatteringDataType>BTDF</ScatteringDataType> | 
| 289 |  | <ScatteringData> | 
| 290 |  | '; | 
| 291 | < | # Output computed data (transposed order) | 
| 291 | > | # Output front transmission (transposed order) | 
| 292 |  | for (my $od = 0; $od < $ndiv; $od++) { | 
| 293 |  | for (my $id = 0; $id < $ndiv; $id++) { | 
| 294 | < | print $darr[$ndiv*$id + $od]; | 
| 294 | > | print $tfarr[$ndiv*$id + $od]; | 
| 295 |  | } | 
| 296 |  | print "\n"; | 
| 297 |  | } | 
| 259 | – | # Output XML epilogue | 
| 298 |  | print | 
| 299 |  | '               </ScatteringData> | 
| 300 |  | </WavelengthDataBlock> | 
| 301 |  | </WavelengthData> | 
| 302 | < | </Layer> | 
| 302 | > | <WavelengthData> | 
| 303 | > | <LayerNumber>System</LayerNumber> | 
| 304 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 305 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 306 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 307 | > | <WavelengthDataBlock> | 
| 308 | > | <WavelengthDataDirection>Reflection Front</WavelengthDataDirection> | 
| 309 | > | <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> | 
| 310 | > | <RowAngleBasis>LBNL/Klems Full</RowAngleBasis> | 
| 311 | > | <ScatteringDataType>BRDF</ScatteringDataType> | 
| 312 | > | <ScatteringData> | 
| 313 | > | '; | 
| 314 | > | # Output front reflection (transposed order) | 
| 315 | > | for (my $od = 0; $od < $ndiv; $od++) { | 
| 316 | > | for (my $id = 0; $id < $ndiv; $id++) { | 
| 317 | > | print $rfarr[$ndiv*$id + $od]; | 
| 318 | > | } | 
| 319 | > | print "\n"; | 
| 320 | > | } | 
| 321 | > | print | 
| 322 | > | '               </ScatteringData> | 
| 323 | > | </WavelengthDataBlock> | 
| 324 | > | </WavelengthData> | 
| 325 | > | '; | 
| 326 | > | } | 
| 327 | > | if ( $doback ) { | 
| 328 | > | print '         <WavelengthData> | 
| 329 | > | <LayerNumber>System</LayerNumber> | 
| 330 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 331 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 332 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 333 | > | <WavelengthDataBlock> | 
| 334 | > | <WavelengthDataDirection>Transmission Back</WavelengthDataDirection> | 
| 335 | > | <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> | 
| 336 | > | <RowAngleBasis>LBNL/Klems Full</RowAngleBasis> | 
| 337 | > | <ScatteringDataType>BTDF</ScatteringDataType> | 
| 338 | > | <ScatteringData> | 
| 339 | > | '; | 
| 340 | > | # Output back transmission (transposed order) | 
| 341 | > | for (my $od = 0; $od < $ndiv; $od++) { | 
| 342 | > | for (my $id = 0; $id < $ndiv; $id++) { | 
| 343 | > | print $tbarr[$ndiv*$id + $od]; | 
| 344 | > | } | 
| 345 | > | print "\n"; | 
| 346 | > | } | 
| 347 | > | print | 
| 348 | > | '               </ScatteringData> | 
| 349 | > | </WavelengthDataBlock> | 
| 350 | > | </WavelengthData> | 
| 351 | > | <WavelengthData> | 
| 352 | > | <LayerNumber>System</LayerNumber> | 
| 353 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 354 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 355 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 356 | > | <WavelengthDataBlock> | 
| 357 | > | <WavelengthDataDirection>Reflection Back</WavelengthDataDirection> | 
| 358 | > | <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> | 
| 359 | > | <RowAngleBasis>LBNL/Klems Full</RowAngleBasis> | 
| 360 | > | <ScatteringDataType>BRDF</ScatteringDataType> | 
| 361 | > | <ScatteringData> | 
| 362 | > | '; | 
| 363 | > | # Output back reflection (transposed order) | 
| 364 | > | for (my $od = 0; $od < $ndiv; $od++) { | 
| 365 | > | for (my $id = 0; $id < $ndiv; $id++) { | 
| 366 | > | print $rbarr[$ndiv*$id + $od]; | 
| 367 | > | } | 
| 368 | > | print "\n"; | 
| 369 | > | } | 
| 370 | > | print | 
| 371 | > | '               </ScatteringData> | 
| 372 | > | </WavelengthDataBlock> | 
| 373 | > | </WavelengthData> | 
| 374 | > | '; | 
| 375 | > | } | 
| 376 | > | # Output XML epilogue | 
| 377 | > | print '</Layer> | 
| 378 |  | </Optical> | 
| 379 |  | </WindowElement> | 
| 380 |  | '; |