ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
(Generate patch)

Comparing ray/src/util/genBSDF.pl (file contents):
Revision 2.14 by greg, Sat Apr 16 01:13:22 2011 UTC vs.
Revision 2.15 by greg, Wed May 25 19:24:11 2011 UTC

# Line 8 | Line 8
8   use strict;
9   use File::Temp qw/ :mktemp  /;
10   sub userror {
11 <        print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-r \"ropts\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom] [input ..]\n";
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 = 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;
# Line 34 | Line 36 | while ($#ARGV >= 0) {
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;
# Line 52 | Line 58 | while ($#ARGV >= 0) {
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" if (!$doforw && !$doback);
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";
# Line 82 | Line 94 | close RADSCN;
94   # Generate octree
95   system "oconv -w $radscn > $octree";
96   die "Could not compile scene\n" if ( $? );
97 < # Set up sampling of interior portal
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 ) {
143 >        open(PFI, "< $persistfile");
144 >        while (<PFI>) {
145 >                s/^[^ ]* //;
146 >                kill('ALRM', $_);
147 >                last;
148 >        }
149 >        close PFI;
150 > }
151 > system "rm -rf $td";
152 > exit 0;
153 > #-------------- End of main program segment --------------#
154 >
155 > #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
156 > sub do_tree_bsdf {
157 > # Get sampling rate and subdivide task
158 > my $ns2 = $ns;
159 > $ns2 /= 2 if ( $tensortree == 3 );
160 > @pdiv = (0, int($ns2/$nproc));
161 > my $nrem = $ns2 % $nproc;
162 > for (my $i = 1; $i < $nproc; $i++) {
163 >        my $nv = $pdiv[$i] + $pdiv[1];
164 >        ++$nv if ( $nrem-- > 0 );
165 >        push @pdiv, $nv;
166 > }
167 > die "Script error 1" if ($pdiv[-1] != $ns2);
168 > # Shirley-Chiu mapping from unit square to disk
169 > $sq2disk = '
170 > in_square_a = 2*in_square_x - 1;
171 > in_square_b = 2*in_square_y - 1;
172 > in_square_rgn = if(in_square_a + in_square_b,
173 >                        if(in_square_a - in_square_b, 1, 2),
174 >                        if(in_square_b - in_square_a, 3, 4));
175 > out_disk_r = .999995*select(in_square_rgn, in_square_a, in_square_b,
176 >                        -in_square_a, -in_square_b);
177 > out_disk_phi = PI/4 * select(in_square_rgn,
178 >                                in_square_b/in_square_a,
179 >                                2 - in_square_a/in_square_b,
180 >                                4 + in_square_b/in_square_a,
181 >                                if(in_square_b*in_square_b,
182 >                                        6 - in_square_a/in_square_b, 0));
183 > Dx = out_disk_r*cos(out_disk_phi);
184 > Dy = out_disk_r*sin(out_disk_phi);
185 > Dz = sqrt(1 - out_disk_r*out_disk_r);
186 > ';
187 > # Shirley-Chiu mapping from unit disk to square
188 > $disk2sq = '
189 > norm_radians(p) : if(-p - PI/4, p + 2*PI, p);
190 > in_disk_r = .999995*sqrt(Dx*Dx + Dy*Dy);
191 > in_disk_phi = norm_radians(atan2(Dy, Dx));
192 > in_disk_rgn = floor((in_disk_phi + PI/4)/(PI/2)) + 1;
193 > out_square_a = select(in_disk_rgn,
194 >                        in_disk_r,
195 >                        (PI/2 - in_disk_phi)*in_disk_r/(PI/4),
196 >                        -in_disk_r,
197 >                        (in_disk_phi - 3*PI/2)*in_disk_r/(PI/4));
198 > out_square_b = select(in_disk_rgn,
199 >                        in_disk_phi*in_disk_r/(PI/4),
200 >                        in_disk_r,
201 >                        (PI - in_disk_phi)*in_disk_r/(PI/4),
202 >                        -in_disk_r);
203 > out_square_x = (out_square_a + 1)/2;
204 > out_square_y = (out_square_b + 1)/2;
205 > ';
206 > # Announce ourselves in XML output
207 > print "         <DataDefinition>\n";
208 > print "                 <IncidentDataStructure>TensorTree$tensortree</IncidentDataStructure>\n";
209 > print "         </DataDefinition>\n";
210 > # Fork parallel rtcontrib processes to compute each side
211 > if ( $doback ) {
212 >        for (my $proc = 0; $proc < $nproc; $proc++) {
213 >                bg_tree_rtcontrib(0, $proc);
214 >        }
215 >        while (wait() >= 0) {
216 >                die "rtcontrib process reported error" if ( $? );
217 >        }
218 >        ttree_out(0);
219 > }
220 > if ( $doforw ) {
221 >        for (my $proc = 0; $proc < $nproc; $proc++) {
222 >                bg_tree_rtcontrib(1, $proc);
223 >        }
224 >        while (wait() >= 0) {
225 >                die "rtcontrib process reported error" if ( $? );
226 >        }
227 >        ttree_out(1);
228 > }
229 > }       # end of sub do_tree_bsdf()
230 >
231 > # Run i'th rtcontrib process for generating tensor tree samples
232 > sub bg_tree_rtcontrib {
233 >        my $pid = fork();
234 >        die "Cannot fork new process" unless defined $pid;
235 >        if ($pid > 0) { return $pid; }
236 >        my $forw = shift;
237 >        my $pn = shift;
238 >        my $pbeg = $pdiv[$pn];
239 >        my $plen = $pdiv[$pn+1] - $pbeg;
240 >        my $matargs = "-m $bmodnm";
241 >        if ( !$forw || !$doback ) { $matargs .= " -m $fmodnm"; }
242 >        my $cmd = "rtcontrib $rtargs -h -ff -fo -c $nsamp " .
243 >                "-e '$disk2sq' -bn '$ns*$ns' " .
244 >                "-b '$ns*floor(out_square_x*$ns)+floor(out_square_y*$ns)' " .
245 >                "-o $td/%s_" . sprintf("%03d", $pn) . ".flt $matargs $octree";
246 >        if ( $tensortree == 3 ) {
247 >                # Isotropic BSDF
248 >                $cmd = "cnt $plen $ny $nx " .
249 >                        "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " .
250 >                        "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " .
251 >                        "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " .
252 >                        "-e 'Dx=1-($pbeg+\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' " .
253 >                        "-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
254 >                        "-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
255 >                        "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " .
256 >                        "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " .
257 >                        "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " .
258 >                        "| $cmd";
259 >        } else {
260 >                # Anisotropic BSDF
261 >                # Sample area vertically to improve load balance, since
262 >                # shading systems usually have bilateral symmetry (L-R)
263 >                $cmd = "cnt $plen $ns $ny $nx " .
264 >                        "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " .
265 >                        "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " .
266 >                        "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " .
267 >                        "-e 'r4=rand(($pn-2.3857833)*recno-964.72738)' " .
268 >                        "-e 'in_square_x=($pbeg+\$1+r1)/$ns' " .
269 >                        "-e 'in_square_y=(\$2+r2)/$ns' -e '$sq2disk' " .
270 >                        "-e 'xp=(\$4+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
271 >                        "-e 'yp=(\$3+r4)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
272 >                        "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " .
273 >                        "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " .
274 >                        "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " .
275 >                        "| $cmd";
276 >        }
277 > # print STDERR "Starting: $cmd\n";
278 >        exec($cmd);             # no return; status report to parent via wait
279 >        die "Cannot exec: $cmd\n";
280 > }       # end of bg_tree_rtcontrib()
281 >
282 > # Simplify and output tensor tree results
283 > sub ttree_out {
284 >        my $forw = shift;
285 >        my $side = ("Back","Front")[$forw];
286 > # Only output one transmitted distribution, preferring backwards
287 > if ( !$forw || !$doback ) {
288 > print
289 > '       <WavelengthData>
290 >                <LayerNumber>System</LayerNumber>
291 >                <Wavelength unit="Integral">Visible</Wavelength>
292 >                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
293 >                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
294 >                <WavelengthDataBlock>
295 >                        <WavelengthDataDirection>Transmission</WavelengthDataDirection>
296 >                        <AngleBasis>LBNL/Shirley-Chiu</AngleBasis>
297 >                        <ScatteringDataType>BTDF</ScatteringDataType>
298 >                        <ScatteringData>
299 > ';
300 > system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
301 >        q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } .
302 >        "$td/" . ($bmodnm,$fmodnm)[$forw] . "_???.flt " .
303 >        "| rttree_reduce -h -ff -r $tensortree -g $ttlog2";
304 > die "Failure running rttree_reduce" if ( $? );
305 > print
306 > '                       </ScatteringData>
307 >                </WavelengthDataBlock>
308 >        </WavelengthData>
309 > ';
310 > }
311 > # Output reflection
312 > print
313 > '       <WavelengthData>
314 >                <LayerNumber>System</LayerNumber>
315 >                <Wavelength unit="Integral">Visible</Wavelength>
316 >                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
317 >                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
318 >                <WavelengthDataBlock>
319 >                        <WavelengthDataDirection>Reflection $side</WavelengthDataDirection>
320 >                        <AngleBasis>LBNL/Shirley-Chiu</AngleBasis>
321 >                        <ScatteringDataType>BRDF</ScatteringDataType>
322 >                        <ScatteringData>
323 > ';
324 > system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
325 >        q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } .
326 >        "$td/" . ($fmodnm,$bmodnm)[$forw] . "_???.flt " .
327 >        "| rttree_reduce -h -ff -r $tensortree -g $ttlog2";
328 > die "Failure running rttree_reduce" if ( $? );
329 > print
330 > '                       </ScatteringData>
331 >                </WavelengthDataBlock>
332 >        </WavelengthData>
333 > ';
334 > }       # end of ttree_out()
335 >
336 > #------------- End of do_tree_bsdf() & subroutines -------------#
337 >
338 > #+++++++++++++++ Klems matrix BSDF generation +++++++++++++++#
339 > sub do_matrix_bsdf {
340 > # Set up sampling of portal
341   # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms
342 < my $tcal = '
342 > $tcal = '
343   DEGREE : PI/180;
344   sq(x) : x*x;
345   Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);
# Line 105 | Line 360 | KprojOmega = PI * if(Kbin-.5,
360          1 - sq(cos(Kpola(1)*DEGREE)));
361   ';
362   # Compute Klems bin from exiting ray direction (forward or backward)
363 < my $kcal = '
363 > $kcal = '
364   DEGREE : PI/180;
365   abs(x) : if(x, x, -x);
366   Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x)));
# Line 131 | Line 386 | kbin2(pol,azi) = select(kfindrow(1, pol),
386   kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx));
387   ';
388   my $ndiv = 145;
134 my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5);
135 my $ny = int($nsamp/$nx + .5);
136 $nsamp = $nx * $ny;
389   # Compute scattering data using rtcontrib
390   my @tfarr;
391   my @rfarr;
392   my @tbarr;
393   my @rbarr;
394   my $cmd;
395 < my $rtcmd = "rtcontrib -h -ff -fo -n $nproc -c $nsamp " .
395 > my $rtcmd = "rtcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " .
396          "-e '$kcal' -b kbin -bn $ndiv " .
397 <        "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $rtargs $octree";
397 >        "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $octree";
398   my $rccmd = "rcalc -e '$tcal' " .
399          "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " .
400          q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'};
# Line 174 | Line 426 | die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $
426   @rbarr = `$rccmd $td/$fmodnm.flt`;
427   die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
428   }
429 < # Output XML prologue
429 > # Output angle basis
430   print
431 < '<?xml version="1.0" encoding="UTF-8"?>
432 < <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">
433 <        <WindowElementType>System</WindowElementType>
182 <        <Optical>
183 <                <Layer>
184 <                <Material>
185 <                        <Name>Name</Name>
186 <                        <Manufacturer>Manufacturer</Manufacturer>
187 < ';
188 < printf "\t\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4];
189 < printf "\t\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0];
190 < printf "\t\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2];
191 < print "\t\t\t<DeviceType>Integral</DeviceType>\n";
192 < # Output MGF description if requested
193 < if ( $geout ) {
194 <        print "\t\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n";
195 <        printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
196 <        open(MGFSCN, "< $mgfscn");
197 <        while (<MGFSCN>) { print $_; }
198 <        close MGFSCN;
199 <        print "xf\n";
200 <        print "\t\t\t</Geometry>\n";
201 < }
202 < print '                 </Material>
203 <                <DataDefinition>
204 <                        <IncidentDataStructure>Columns</IncidentDataStructure>
205 <                        <AngleBasis>
431 > '       <DataDefinition>
432 >                <IncidentDataStructure>Columns</IncidentDataStructure>
433 >                <AngleBasis>
434                          <AngleBasisName>LBNL/Klems Full</AngleBasisName>
435 <                                <AngleBasisBlock>
435 >                        <AngleBasisBlock>
436                                  <Theta>0</Theta>
437                                  <nPhis>1</nPhis>
438                                  <ThetaBounds>
439 <                                <LowerTheta>0</LowerTheta>
440 <                                <UpperTheta>5</UpperTheta>
439 >                                        <LowerTheta>0</LowerTheta>
440 >                                        <UpperTheta>5</UpperTheta>
441                                  </ThetaBounds>
442                                  </AngleBasisBlock>
443                                  <AngleBasisBlock>
# Line 280 | Line 508 | print '                        </Material>
508          </DataDefinition>
509   ';
510   if ( $doforw ) {
511 < print '         <WavelengthData>
511 > print
512 > '       <WavelengthData>
513                  <LayerNumber>System</LayerNumber>
514                  <Wavelength unit="Integral">Visible</Wavelength>
515                  <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
# Line 300 | Line 529 | for (my $od = 0; $od < $ndiv; $od++) {
529          print "\n";
530   }
531   print
532 < '               </ScatteringData>
533 <        </WavelengthDataBlock>
532 > '                       </ScatteringData>
533 >                </WavelengthDataBlock>
534          </WavelengthData>
535          <WavelengthData>
536                  <LayerNumber>System</LayerNumber>
# Line 323 | Line 552 | for (my $od = 0; $od < $ndiv; $od++) {
552          print "\n";
553   }
554   print
555 < '               </ScatteringData>
556 <        </WavelengthDataBlock>
555 > '                       </ScatteringData>
556 >                </WavelengthDataBlock>
557          </WavelengthData>
558   ';
559   }
560   if ( $doback ) {
561 < print '         <WavelengthData>
561 > print
562 > '       <WavelengthData>
563                  <LayerNumber>System</LayerNumber>
564                  <Wavelength unit="Integral">Visible</Wavelength>
565                  <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
# Line 349 | Line 579 | for (my $od = 0; $od < $ndiv; $od++) {
579          print "\n";
580   }
581   print
582 < '               </ScatteringData>
583 <        </WavelengthDataBlock>
582 > '                       </ScatteringData>
583 >                </WavelengthDataBlock>
584          </WavelengthData>
585          <WavelengthData>
586                  <LayerNumber>System</LayerNumber>
# Line 372 | Line 602 | for (my $od = 0; $od < $ndiv; $od++) {
602          print "\n";
603   }
604   print
605 < '               </ScatteringData>
606 <        </WavelengthDataBlock>
605 > '                       </ScatteringData>
606 >                </WavelengthDataBlock>
607          </WavelengthData>
608   ';
609   }
610 < # Output XML epilogue
611 < print '</Layer>
382 < </Optical>
383 < </WindowElement>
384 < ';
385 < # Clean up temporary files
386 < system "rm -rf $td";
610 > }
611 > #------------- End of do_matrix_bsdf() --------------#

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines