| 6 |  | #       G. Ward | 
| 7 |  | # | 
| 8 |  | use strict; | 
| 9 | + | use File::Temp qw/ :mktemp  /; | 
| 10 |  | sub userror { | 
| 11 | < | print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-dim xmin xmax ymin ymax zmin zmax][{+|-}mgf][{+|-}geom] [input ..]"; | 
| 11 | > | 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] [input ..]\n"; | 
| 12 |  | exit 1; | 
| 13 |  | } | 
| 14 | < | my $td = `mktemp -d /tmp/genBSDF.XXXXXX`; | 
| 14 | > | my $td = mkdtemp("/tmp/genBSDF.XXXXXX"); | 
| 15 |  | chomp $td; | 
| 16 | + | my $tensortree = 0; | 
| 17 | + | my $ttlog2 = 4; | 
| 18 |  | my $nsamp = 1000; | 
| 19 | + | my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6"; | 
| 20 |  | my $mgfin = 0; | 
| 21 |  | my $geout = 1; | 
| 22 |  | my $nproc = 1; | 
| 23 | + | my $doforw = 0; | 
| 24 | + | my $doback = 1; | 
| 25 |  | my @dim; | 
| 26 |  | # Get options | 
| 27 |  | while ($#ARGV >= 0) { | 
| 28 |  | if ("$ARGV[0]" =~ /^[-+]m/) { | 
| 29 |  | $mgfin = ("$ARGV[0]" =~ /^\+/); | 
| 30 | + | } elsif ("$ARGV[0]" eq "-r") { | 
| 31 | + | $rtargs = "$rtargs $ARGV[1]"; | 
| 32 | + | shift @ARGV; | 
| 33 |  | } elsif ("$ARGV[0]" =~ /^[-+]g/) { | 
| 34 |  | $geout = ("$ARGV[0]" =~ /^\+/); | 
| 35 | + | } elsif ("$ARGV[0]" =~ /^[-+]f/) { | 
| 36 | + | $doforw = ("$ARGV[0]" =~ /^\+/); | 
| 37 | + | } elsif ("$ARGV[0]" =~ /^[-+]b/) { | 
| 38 | + | $doback = ("$ARGV[0]" =~ /^\+/); | 
| 39 | + | } elsif ("$ARGV[0]" =~ /^-t[34]$/) { | 
| 40 | + | $tensortree = substr($ARGV[0], 2, 1); | 
| 41 | + | $ttlog2 = $ARGV[1]; | 
| 42 | + | shift @ARGV; | 
| 43 |  | } elsif ("$ARGV[0]" eq "-c") { | 
| 44 |  | $nsamp = $ARGV[1]; | 
| 45 |  | shift @ARGV; | 
| 48 |  | shift @ARGV; | 
| 49 |  | } elsif ("$ARGV[0]" =~ /^-d/) { | 
| 50 |  | userror() if ($#ARGV < 6); | 
| 51 | < | @dim = "@ARGV[1..6]"; | 
| 51 | > | @dim = @ARGV[1..6]; | 
| 52 |  | shift @ARGV for (1..6); | 
| 53 |  | } elsif ("$ARGV[0]" =~ /^[-+]./) { | 
| 54 |  | userror(); | 
| 57 |  | } | 
| 58 |  | shift @ARGV; | 
| 59 |  | } | 
| 60 | + | # Check that we're actually being asked to do something | 
| 61 | + | die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback); | 
| 62 | + | # Name our own persist file? | 
| 63 | + | my $persistfile; | 
| 64 | + | if ( $tensortree && $nproc > 1 && "$rtargs" !~ /-PP /) { | 
| 65 | + | $persistfile = "$td/pfile.txt"; | 
| 66 | + | $rtargs = "-PP $persistfile $rtargs"; | 
| 67 | + | } | 
| 68 |  | # Get scene description and dimensions | 
| 69 |  | my $radscn = "$td/device.rad"; | 
| 70 |  | my $mgfscn = "$td/device.mgf"; | 
| 74 |  | die "Could not load MGF input\n" if ( $? ); | 
| 75 |  | system "mgf2rad $mgfscn > $radscn"; | 
| 76 |  | } else { | 
| 77 | < | system "cat @ARGV | xform -e > $radscn"; | 
| 77 | > | system "xform -e @ARGV > $radscn"; | 
| 78 |  | die "Could not load Radiance input\n" if ( $? ); | 
| 79 |  | system "rad2mgf $radscn > $mgfscn" if ( $geout ); | 
| 80 |  | } | 
| 81 |  | if ($#dim != 5) { | 
| 82 | < | @dim = split /\s+/, `getbbox -h $radscn`; | 
| 58 | < | shift @dim; | 
| 82 | > | @dim = split ' ', `getbbox -h $radscn`; | 
| 83 |  | } | 
| 84 |  | print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5); | 
| 85 | < | # Add receiver surface (rectangle) | 
| 86 | < | my $modnm="_receiver_black_"; | 
| 85 | > | # Add receiver surfaces (rectangular) | 
| 86 | > | my $fmodnm="receiver_face"; | 
| 87 | > | my $bmodnm="receiver_behind"; | 
| 88 |  | open(RADSCN, ">> $radscn"); | 
| 89 | < | print RADSCN "void glow $modnm\n0\n0\n4 0 0 0 0\n\n"; | 
| 90 | < | print RADSCN "$modnm polygon _receiver_\n0\n0\n12\n"; | 
| 91 | < | print RADSCN "\t",$dim[0],"\t",$dim[2],"\t",$dim[5]+1e-5,"\n"; | 
| 92 | < | print RADSCN "\t",$dim[0],"\t",$dim[3],"\t",$dim[5]+1e-5,"\n"; | 
| 68 | < | print RADSCN "\t",$dim[1],"\t",$dim[3],"\t",$dim[5]+1e-5,"\n"; | 
| 69 | < | print RADSCN "\t",$dim[1],"\t",$dim[2],"\t",$dim[5]+1e-5,"\n"; | 
| 89 | > | print RADSCN "void glow $fmodnm\n0\n0\n4 1 1 1 0\n\n"; | 
| 90 | > | print RADSCN "$fmodnm source f_receiver\n0\n0\n4 0 0 1 180\n"; | 
| 91 | > | print RADSCN "void glow $bmodnm\n0\n0\n4 1 1 1 0\n\n"; | 
| 92 | > | print RADSCN "$bmodnm source b_receiver\n0\n0\n4 0 0 -1 180\n"; | 
| 93 |  | close RADSCN; | 
| 94 |  | # Generate octree | 
| 95 |  | system "oconv -w $radscn > $octree"; | 
| 96 |  | die "Could not compile scene\n" if ( $? ); | 
| 97 | < | # Set up sampling | 
| 97 | > | # Output XML prologue | 
| 98 | > | print | 
| 99 | > | '<?xml version="1.0" encoding="UTF-8"?> | 
| 100 | > | <WindowElement xmlns="http://windows.lbl.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windows.lbl.gov/BSDF-v1.4.xsd"> | 
| 101 | > | <WindowElementType>System</WindowElementType> | 
| 102 | > | <Optical> | 
| 103 | > | <Layer> | 
| 104 | > | <Material> | 
| 105 | > | <Name>Name</Name> | 
| 106 | > | <Manufacturer>Manufacturer</Manufacturer> | 
| 107 | > | '; | 
| 108 | > | printf "\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4]; | 
| 109 | > | printf "\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0]; | 
| 110 | > | printf "\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2]; | 
| 111 | > | print "\t\t<DeviceType>Integral</DeviceType>\n"; | 
| 112 | > | # Output MGF description if requested | 
| 113 | > | if ( $geout ) { | 
| 114 | > | print "\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n"; | 
| 115 | > | printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; | 
| 116 | > | open(MGFSCN, "< $mgfscn"); | 
| 117 | > | while (<MGFSCN>) { print $_; } | 
| 118 | > | close MGFSCN; | 
| 119 | > | print "xf\n"; | 
| 120 | > | print "\t\t</Geometry>\n"; | 
| 121 | > | } | 
| 122 | > | print " </Material>\n"; | 
| 123 | > | # Set up surface sampling | 
| 124 | > | my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); | 
| 125 | > | my $ny = int($nsamp/$nx + .5); | 
| 126 | > | $nsamp = $nx * $ny; | 
| 127 | > | my $ns = 2**$ttlog2; | 
| 128 | > | my (@pdiv, $disk2sq, $sq2disk, $tcal, $kcal); | 
| 129 | > | # Create data segments (all the work happens here) | 
| 130 | > | if ( $tensortree ) { | 
| 131 | > | do_tree_bsdf(); | 
| 132 | > | } else { | 
| 133 | > | do_matrix_bsdf(); | 
| 134 | > | } | 
| 135 | > | # Output XML epilogue | 
| 136 | > | print | 
| 137 | > | '</Layer> | 
| 138 | > | </Optical> | 
| 139 | > | </WindowElement> | 
| 140 | > | '; | 
| 141 | > | # Clean up temporary files and exit | 
| 142 | > | if ( $persistfile && open(PFI, "< $persistfile") ) { | 
| 143 | > | while (<PFI>) { | 
| 144 | > | s/^[^ ]* //; | 
| 145 | > | kill('ALRM', $_); | 
| 146 | > | last; | 
| 147 | > | } | 
| 148 | > | close PFI; | 
| 149 | > | } | 
| 150 | > | exec("rm -rf $td"); | 
| 151 | > |  | 
| 152 | > | #-------------- End of main program segment --------------# | 
| 153 | > |  | 
| 154 | > | #++++++++++++++ Tensor tree BSDF generation ++++++++++++++# | 
| 155 | > | sub do_tree_bsdf { | 
| 156 | > | # Get sampling rate and subdivide task | 
| 157 | > | my $ns2 = $ns; | 
| 158 | > | $ns2 /= 2 if ( $tensortree == 3 ); | 
| 159 | > | @pdiv = (0, int($ns2/$nproc)); | 
| 160 | > | my $nrem = $ns2 % $nproc; | 
| 161 | > | for (my $i = 1; $i < $nproc; $i++) { | 
| 162 | > | my $nv = $pdiv[$i] + $pdiv[1]; | 
| 163 | > | ++$nv if ( $nrem-- > 0 ); | 
| 164 | > | push @pdiv, $nv; | 
| 165 | > | } | 
| 166 | > | die "Script error 1" if ($pdiv[-1] != $ns2); | 
| 167 | > | # Shirley-Chiu mapping from unit square to disk | 
| 168 | > | $sq2disk = ' | 
| 169 | > | in_square_a = 2*in_square_x - 1; | 
| 170 | > | in_square_b = 2*in_square_y - 1; | 
| 171 | > | in_square_rgn = if(in_square_a + in_square_b, | 
| 172 | > | if(in_square_a - in_square_b, 1, 2), | 
| 173 | > | if(in_square_b - in_square_a, 3, 4)); | 
| 174 | > | out_disk_r = .999995*select(in_square_rgn, in_square_a, in_square_b, | 
| 175 | > | -in_square_a, -in_square_b); | 
| 176 | > | out_disk_phi = PI/4 * select(in_square_rgn, | 
| 177 | > | in_square_b/in_square_a, | 
| 178 | > | 2 - in_square_a/in_square_b, | 
| 179 | > | 4 + in_square_b/in_square_a, | 
| 180 | > | if(in_square_b*in_square_b, | 
| 181 | > | 6 - in_square_a/in_square_b, 0)); | 
| 182 | > | Dx = out_disk_r*cos(out_disk_phi); | 
| 183 | > | Dy = out_disk_r*sin(out_disk_phi); | 
| 184 | > | Dz = sqrt(1 - out_disk_r*out_disk_r); | 
| 185 | > | '; | 
| 186 | > | # Shirley-Chiu mapping from unit disk to square | 
| 187 | > | $disk2sq = ' | 
| 188 | > | norm_radians(p) : if(-p - PI/4, p + 2*PI, p); | 
| 189 | > | in_disk_r = .999995*sqrt(Dx*Dx + Dy*Dy); | 
| 190 | > | in_disk_phi = norm_radians(atan2(Dy, Dx)); | 
| 191 | > | in_disk_rgn = floor((in_disk_phi + PI/4)/(PI/2)) + 1; | 
| 192 | > | out_square_a = select(in_disk_rgn, | 
| 193 | > | in_disk_r, | 
| 194 | > | (PI/2 - in_disk_phi)*in_disk_r/(PI/4), | 
| 195 | > | -in_disk_r, | 
| 196 | > | (in_disk_phi - 3*PI/2)*in_disk_r/(PI/4)); | 
| 197 | > | out_square_b = select(in_disk_rgn, | 
| 198 | > | in_disk_phi*in_disk_r/(PI/4), | 
| 199 | > | in_disk_r, | 
| 200 | > | (PI - in_disk_phi)*in_disk_r/(PI/4), | 
| 201 | > | -in_disk_r); | 
| 202 | > | out_square_x = (out_square_a + 1)/2; | 
| 203 | > | out_square_y = (out_square_b + 1)/2; | 
| 204 | > | '; | 
| 205 | > | # Announce ourselves in XML output | 
| 206 | > | print "\t<DataDefinition>\n"; | 
| 207 | > | print "\t\t<IncidentDataStructure>TensorTree$tensortree</IncidentDataStructure>\n"; | 
| 208 | > | print "\t</DataDefinition>\n"; | 
| 209 | > | # Fork parallel rtcontrib processes to compute each side | 
| 210 | > | if ( $doback ) { | 
| 211 | > | for (my $proc = 0; $proc < $nproc; $proc++) { | 
| 212 | > | bg_tree_rtcontrib(0, $proc); | 
| 213 | > | } | 
| 214 | > | while (wait() >= 0) { | 
| 215 | > | die "rtcontrib process reported error" if ( $? ); | 
| 216 | > | } | 
| 217 | > | ttree_out(0); | 
| 218 | > | } | 
| 219 | > | if ( $doforw ) { | 
| 220 | > | for (my $proc = 0; $proc < $nproc; $proc++) { | 
| 221 | > | bg_tree_rtcontrib(1, $proc); | 
| 222 | > | } | 
| 223 | > | while (wait() >= 0) { | 
| 224 | > | die "rtcontrib process reported error" if ( $? ); | 
| 225 | > | } | 
| 226 | > | ttree_out(1); | 
| 227 | > | } | 
| 228 | > | }       # end of sub do_tree_bsdf() | 
| 229 | > |  | 
| 230 | > | # Run i'th rtcontrib process for generating tensor tree samples | 
| 231 | > | sub bg_tree_rtcontrib { | 
| 232 | > | my $pid = fork(); | 
| 233 | > | die "Cannot fork new process" unless defined $pid; | 
| 234 | > | if ($pid > 0) { return $pid; } | 
| 235 | > | my $forw = shift; | 
| 236 | > | my $pn = shift; | 
| 237 | > | my $pbeg = $pdiv[$pn]; | 
| 238 | > | my $plen = $pdiv[$pn+1] - $pbeg; | 
| 239 | > | my $matargs = "-m $bmodnm"; | 
| 240 | > | if ( !$forw || !$doback ) { $matargs .= " -m $fmodnm"; } | 
| 241 | > | my $cmd = "rtcontrib $rtargs -h -ff -fo -c $nsamp " . | 
| 242 | > | "-e '$disk2sq' -bn '$ns*$ns' " . | 
| 243 | > | "-b '$ns*floor(out_square_x*$ns)+floor(out_square_y*$ns)' " . | 
| 244 | > | "-o $td/%s_" . sprintf("%03d", $pn) . ".flt $matargs $octree"; | 
| 245 | > | if ( $tensortree == 3 ) { | 
| 246 | > | # Isotropic BSDF | 
| 247 | > | $cmd = "cnt $plen $ny $nx " . | 
| 248 | > | "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " . | 
| 249 | > | "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " . | 
| 250 | > | "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " . | 
| 251 | > | "-e 'Dx=1-($pbeg+\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' " . | 
| 252 | > | "-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . | 
| 253 | > | "-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . | 
| 254 | > | "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " . | 
| 255 | > | "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " . | 
| 256 | > | "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " . | 
| 257 | > | "| $cmd"; | 
| 258 | > | } else { | 
| 259 | > | # Anisotropic BSDF | 
| 260 | > | # Sample area vertically to improve load balance, since | 
| 261 | > | # shading systems usually have bilateral symmetry (L-R) | 
| 262 | > | $cmd = "cnt $plen $ns $ny $nx " . | 
| 263 | > | "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " . | 
| 264 | > | "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " . | 
| 265 | > | "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " . | 
| 266 | > | "-e 'r4=rand(($pn-2.3857833)*recno-964.72738)' " . | 
| 267 | > | "-e 'in_square_x=($pbeg+\$1+r1)/$ns' " . | 
| 268 | > | "-e 'in_square_y=(\$2+r2)/$ns' -e '$sq2disk' " . | 
| 269 | > | "-e 'xp=(\$4+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . | 
| 270 | > | "-e 'yp=(\$3+r4)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . | 
| 271 | > | "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " . | 
| 272 | > | "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " . | 
| 273 | > | "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " . | 
| 274 | > | "| $cmd"; | 
| 275 | > | } | 
| 276 | > | # print STDERR "Starting: $cmd\n"; | 
| 277 | > | exec($cmd);             # no return; status report to parent via wait | 
| 278 | > | die "Cannot exec: $cmd\n"; | 
| 279 | > | }       # end of bg_tree_rtcontrib() | 
| 280 | > |  | 
| 281 | > | # Simplify and output tensor tree results | 
| 282 | > | sub ttree_out { | 
| 283 | > | my $forw = shift; | 
| 284 | > | my $side = ("Back","Front")[$forw]; | 
| 285 | > | # Only output one transmitted distribution, preferring backwards | 
| 286 | > | if ( !$forw || !$doback ) { | 
| 287 | > | print | 
| 288 | > | '       <WavelengthData> | 
| 289 | > | <LayerNumber>System</LayerNumber> | 
| 290 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 291 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 292 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 293 | > | <WavelengthDataBlock> | 
| 294 | > | <WavelengthDataDirection>Transmission</WavelengthDataDirection> | 
| 295 | > | <AngleBasis>LBNL/Shirley-Chiu</AngleBasis> | 
| 296 | > | <ScatteringDataType>BTDF</ScatteringDataType> | 
| 297 | > | <ScatteringData> | 
| 298 | > | '; | 
| 299 | > | system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . | 
| 300 | > | q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } . | 
| 301 | > | "$td/" . ($bmodnm,$fmodnm)[$forw] . "_???.flt " . | 
| 302 | > | "| rttree_reduce -h -ff -r $tensortree -g $ttlog2"; | 
| 303 | > | die "Failure running rttree_reduce" if ( $? ); | 
| 304 | > | print | 
| 305 | > | '                       </ScatteringData> | 
| 306 | > | </WavelengthDataBlock> | 
| 307 | > | </WavelengthData> | 
| 308 | > | '; | 
| 309 | > | } | 
| 310 | > | # Output reflection | 
| 311 | > | print | 
| 312 | > | '       <WavelengthData> | 
| 313 | > | <LayerNumber>System</LayerNumber> | 
| 314 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 315 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 316 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 317 | > | <WavelengthDataBlock> | 
| 318 | > | '; | 
| 319 | > | print "\t\t\t<WavelengthDataDirection>Reflection $side</WavelengthDataDirection>\n"; | 
| 320 | > | print ' | 
| 321 | > | <AngleBasis>LBNL/Shirley-Chiu</AngleBasis> | 
| 322 | > | <ScatteringDataType>BRDF</ScatteringDataType> | 
| 323 | > | <ScatteringData> | 
| 324 | > | '; | 
| 325 | > | system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . | 
| 326 | > | q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } . | 
| 327 | > | "$td/" . ($fmodnm,$bmodnm)[$forw] . "_???.flt " . | 
| 328 | > | "| rttree_reduce -h -ff -r $tensortree -g $ttlog2"; | 
| 329 | > | die "Failure running rttree_reduce" if ( $? ); | 
| 330 | > | print | 
| 331 | > | '                       </ScatteringData> | 
| 332 | > | </WavelengthDataBlock> | 
| 333 | > | </WavelengthData> | 
| 334 | > | '; | 
| 335 | > | }       # end of ttree_out() | 
| 336 | > |  | 
| 337 | > | #------------- End of do_tree_bsdf() & subroutines -------------# | 
| 338 | > |  | 
| 339 | > | #+++++++++++++++ Klems matrix BSDF generation +++++++++++++++# | 
| 340 | > | sub do_matrix_bsdf { | 
| 341 | > | # Set up sampling of portal | 
| 342 |  | # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms | 
| 343 | < | my $tcal = ' | 
| 343 | > | $tcal = ' | 
| 344 |  | DEGREE : PI/180; | 
| 345 | + | sq(x) : x*x; | 
| 346 |  | Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90); | 
| 347 |  | Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12); | 
| 348 |  | Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0); | 
| 353 |  | Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow); | 
| 354 |  | Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1)); | 
| 355 |  | sin_kpol = sin(Kpol); | 
| 356 | < | Dx = -cos(Kazi)*sin_kpol; | 
| 356 | > | Dx = cos(Kazi)*sin_kpol; | 
| 357 |  | Dy = sin(Kazi)*sin_kpol; | 
| 358 |  | Dz = sqrt(1 - sin_kpol*sin_kpol); | 
| 359 | < | Komega = 2*PI*if(Kbin-.5, | 
| 360 | < | (cos(Kpola(Krow-1)*DEGREE) - cos(Kpola(Krow)*DEGREE))/Knaz(Krow), | 
| 361 | < | 1 - cos(Kpola(1)*DEGREE)); | 
| 359 | > | KprojOmega = PI * if(Kbin-.5, | 
| 360 | > | (sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow), | 
| 361 | > | 1 - sq(cos(Kpola(1)*DEGREE))); | 
| 362 |  | '; | 
| 363 | < | # Compute Klems bin from exiting ray direction | 
| 364 | < | my $kcal = ' | 
| 363 | > | # Compute Klems bin from exiting ray direction (forward or backward) | 
| 364 | > | $kcal = ' | 
| 365 |  | DEGREE : PI/180; | 
| 366 | + | abs(x) : if(x, x, -x); | 
| 367 |  | Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x))); | 
| 368 |  | posangle(a) : if(-a, a + 2*PI, a); | 
| 369 |  | Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x)); | 
| 384 |  | kaccum(7) + kazn(azi,360/knaz(8)), | 
| 385 |  | kaccum(8) + kazn(azi,360/knaz(9)) | 
| 386 |  | ); | 
| 387 | < | kbin = kbin2(Acos(Dz), Atan2(Dy, -Dx)); | 
| 387 | > | kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx)); | 
| 388 |  | '; | 
| 389 |  | my $ndiv = 145; | 
| 390 | < | my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); | 
| 391 | < | my $ny = int($nsamp/$nx + .5); | 
| 392 | < | $nsamp = $nx * $ny; | 
| 393 | < | # Output XML prologue | 
| 394 | < | print | 
| 395 | < | '<?xml version="1.0" encoding="UTF-8"?> | 
| 396 | < | <WindowElement xmlns="http://windows.lbl.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windows.lbl.gov/BSDF-v1.4.xsd"> | 
| 397 | < | <WindowElementType>System</WindowElementType> | 
| 398 | < | <Optical> | 
| 399 | < | <Layer> | 
| 400 | < | <Material> | 
| 401 | < | <Name>Name</Name> | 
| 402 | < | <Manufacturer>Manufacturer</Manufacturer> | 
| 403 | < | '; | 
| 404 | < | printf "\t\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4]; | 
| 405 | < | printf "\t\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0]; | 
| 406 | < | printf "\t\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2]; | 
| 407 | < | print "\t\t\t<DeviceType>Integral</DeviceType>\n"; | 
| 408 | < | # Output MGF description if requested | 
| 409 | < | if ( $geout ) { | 
| 410 | < | print "\t\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n"; | 
| 411 | < | printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; | 
| 412 | < | system "cat $mgfscn"; | 
| 413 | < | print "xf\n"; | 
| 414 | < | print "\t\t\t</Geometry>\n"; | 
| 390 | > | # Compute scattering data using rtcontrib | 
| 391 | > | my @tfarr; | 
| 392 | > | my @rfarr; | 
| 393 | > | my @tbarr; | 
| 394 | > | my @rbarr; | 
| 395 | > | my $cmd; | 
| 396 | > | my $rtcmd = "rtcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " . | 
| 397 | > | "-e '$kcal' -b kbin -bn $ndiv " . | 
| 398 | > | "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $octree"; | 
| 399 | > | my $rccmd = "rcalc -e '$tcal' " . | 
| 400 | > | "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " . | 
| 401 | > | q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'}; | 
| 402 | > | if ( $doforw ) { | 
| 403 | > | $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . | 
| 404 | > | "-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . | 
| 405 | > | "-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . | 
| 406 | > | "-e 'zp:$dim[4]' " . | 
| 407 | > | q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } . | 
| 408 | > | q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } . | 
| 409 | > | "| $rtcmd"; | 
| 410 | > | system "$cmd" || die "Failure running: $cmd\n"; | 
| 411 | > | @tfarr = `$rccmd $td/$fmodnm.flt`; | 
| 412 | > | die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); | 
| 413 | > | @rfarr = `$rccmd $td/$bmodnm.flt`; | 
| 414 | > | die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); | 
| 415 |  | } | 
| 416 | < | print '                 </Material> | 
| 417 | < | <DataDefinition> | 
| 418 | < | <IncidentDataStructure>Columns</IncidentDataStructure> | 
| 419 | < | <AngleBasis> | 
| 416 | > | if ( $doback ) { | 
| 417 | > | $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . | 
| 418 | > | "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . | 
| 419 | > | "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . | 
| 420 | > | "-e 'zp:$dim[5]' " . | 
| 421 | > | q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } . | 
| 422 | > | q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } . | 
| 423 | > | "| $rtcmd"; | 
| 424 | > | system "$cmd" || die "Failure running: $cmd\n"; | 
| 425 | > | @tbarr = `$rccmd $td/$bmodnm.flt`; | 
| 426 | > | die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); | 
| 427 | > | @rbarr = `$rccmd $td/$fmodnm.flt`; | 
| 428 | > | die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); | 
| 429 | > | } | 
| 430 | > | # Output angle basis | 
| 431 | > | print | 
| 432 | > | '       <DataDefinition> | 
| 433 | > | <IncidentDataStructure>Columns</IncidentDataStructure> | 
| 434 | > | <AngleBasis> | 
| 435 |  | <AngleBasisName>LBNL/Klems Full</AngleBasisName> | 
| 436 | < | <AngleBasisBlock> | 
| 436 | > | <AngleBasisBlock> | 
| 437 |  | <Theta>0</Theta> | 
| 438 |  | <nPhis>1</nPhis> | 
| 439 |  | <ThetaBounds> | 
| 440 | < | <LowerTheta>0</LowerTheta> | 
| 441 | < | <UpperTheta>5</UpperTheta> | 
| 440 | > | <LowerTheta>0</LowerTheta> | 
| 441 | > | <UpperTheta>5</UpperTheta> | 
| 442 |  | </ThetaBounds> | 
| 443 |  | </AngleBasisBlock> | 
| 444 |  | <AngleBasisBlock> | 
| 507 |  | </AngleBasisBlock> | 
| 508 |  | </AngleBasis> | 
| 509 |  | </DataDefinition> | 
| 510 | < | <WavelengthData> | 
| 510 | > | '; | 
| 511 | > | if ( $doforw ) { | 
| 512 | > | print | 
| 513 | > | '       <WavelengthData> | 
| 514 |  | <LayerNumber>System</LayerNumber> | 
| 515 |  | <Wavelength unit="Integral">Visible</Wavelength> | 
| 516 |  | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 522 |  | <ScatteringDataType>BTDF</ScatteringDataType> | 
| 523 |  | <ScatteringData> | 
| 524 |  | '; | 
| 525 | < | # Compute actual scattering data using rtcontrib | 
| 526 | < | system "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . | 
| 527 | < | "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . | 
| 528 | < | "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . | 
| 529 | < | "-e 'zp:$dim[4]-1e-5' " . | 
| 530 | < | q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } . | 
| 531 | < | q{-e '$1=xp;$2=yp;$3=zp;$4=Dx;$5=Dy;$6=Dz' } . | 
| 245 | < | "| rtcontrib -h -ff -n $nproc -c $nsamp -e '$kcal' -b kbin -bn $ndiv " . | 
| 246 | < | "-m $modnm -w -ab 5 -ad 700 -lw 3e-6 $octree " . | 
| 247 | < | "| rcalc -e 'x1:.5;x2:.5;$tcal' -e 'Kbin=floor((recno-1)/$ndiv)' " . | 
| 248 | < | q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/(Komega*Dz)'}; | 
| 249 | < | # Output XML epilogue | 
| 525 | > | # Output front transmission (transposed order) | 
| 526 | > | for (my $od = 0; $od < $ndiv; $od++) { | 
| 527 | > | for (my $id = 0; $id < $ndiv; $id++) { | 
| 528 | > | print $tfarr[$ndiv*$id + $od]; | 
| 529 | > | } | 
| 530 | > | print "\n"; | 
| 531 | > | } | 
| 532 |  | print | 
| 533 | < | '               </ScatteringData> | 
| 534 | < | </WavelengthDataBlock> | 
| 533 | > | '                       </ScatteringData> | 
| 534 | > | </WavelengthDataBlock> | 
| 535 |  | </WavelengthData> | 
| 536 | < | </Layer> | 
| 537 | < | </Optical> | 
| 538 | < | </WindowElement> | 
| 536 | > | <WavelengthData> | 
| 537 | > | <LayerNumber>System</LayerNumber> | 
| 538 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 539 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 540 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 541 | > | <WavelengthDataBlock> | 
| 542 | > | <WavelengthDataDirection>Reflection Front</WavelengthDataDirection> | 
| 543 | > | <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> | 
| 544 | > | <RowAngleBasis>LBNL/Klems Full</RowAngleBasis> | 
| 545 | > | <ScatteringDataType>BRDF</ScatteringDataType> | 
| 546 | > | <ScatteringData> | 
| 547 |  | '; | 
| 548 | < | # Clean up temporary files | 
| 549 | < | system "rm -rf $td"; | 
| 548 | > | # Output front reflection (transposed order) | 
| 549 | > | for (my $od = 0; $od < $ndiv; $od++) { | 
| 550 | > | for (my $id = 0; $id < $ndiv; $id++) { | 
| 551 | > | print $rfarr[$ndiv*$id + $od]; | 
| 552 | > | } | 
| 553 | > | print "\n"; | 
| 554 | > | } | 
| 555 | > | print | 
| 556 | > | '                       </ScatteringData> | 
| 557 | > | </WavelengthDataBlock> | 
| 558 | > | </WavelengthData> | 
| 559 | > | '; | 
| 560 | > | } | 
| 561 | > | if ( $doback ) { | 
| 562 | > | print | 
| 563 | > | '       <WavelengthData> | 
| 564 | > | <LayerNumber>System</LayerNumber> | 
| 565 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 566 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 567 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 568 | > | <WavelengthDataBlock> | 
| 569 | > | <WavelengthDataDirection>Transmission Back</WavelengthDataDirection> | 
| 570 | > | <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> | 
| 571 | > | <RowAngleBasis>LBNL/Klems Full</RowAngleBasis> | 
| 572 | > | <ScatteringDataType>BTDF</ScatteringDataType> | 
| 573 | > | <ScatteringData> | 
| 574 | > | '; | 
| 575 | > | # Output back transmission (transposed order) | 
| 576 | > | for (my $od = 0; $od < $ndiv; $od++) { | 
| 577 | > | for (my $id = 0; $id < $ndiv; $id++) { | 
| 578 | > | print $tbarr[$ndiv*$id + $od]; | 
| 579 | > | } | 
| 580 | > | print "\n"; | 
| 581 | > | } | 
| 582 | > | print | 
| 583 | > | '                       </ScatteringData> | 
| 584 | > | </WavelengthDataBlock> | 
| 585 | > | </WavelengthData> | 
| 586 | > | <WavelengthData> | 
| 587 | > | <LayerNumber>System</LayerNumber> | 
| 588 | > | <Wavelength unit="Integral">Visible</Wavelength> | 
| 589 | > | <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> | 
| 590 | > | <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> | 
| 591 | > | <WavelengthDataBlock> | 
| 592 | > | <WavelengthDataDirection>Reflection Back</WavelengthDataDirection> | 
| 593 | > | <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> | 
| 594 | > | <RowAngleBasis>LBNL/Klems Full</RowAngleBasis> | 
| 595 | > | <ScatteringDataType>BRDF</ScatteringDataType> | 
| 596 | > | <ScatteringData> | 
| 597 | > | '; | 
| 598 | > | # Output back reflection (transposed order) | 
| 599 | > | for (my $od = 0; $od < $ndiv; $od++) { | 
| 600 | > | for (my $id = 0; $id < $ndiv; $id++) { | 
| 601 | > | print $rbarr[$ndiv*$id + $od]; | 
| 602 | > | } | 
| 603 | > | print "\n"; | 
| 604 | > | } | 
| 605 | > | print | 
| 606 | > | '                       </ScatteringData> | 
| 607 | > | </WavelengthDataBlock> | 
| 608 | > | </WavelengthData> | 
| 609 | > | '; | 
| 610 | > | } | 
| 611 | > | } | 
| 612 | > | #------------- End of do_matrix_bsdf() --------------# |