ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
Revision: 2.15
Committed: Wed May 25 19:24:11 2011 UTC (12 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +284 -59 lines
Log Message:
Incomplete implementation of variable-resolution BSDF calculation

File Contents

# User Rev Content
1 greg 2.1 #!/usr/bin/perl -w
2 greg 2.15 # RCSid $Id: genBSDF.pl,v 2.14 2011/04/16 01:13:22 greg Exp $
3 greg 2.1 #
4     # Compute BSDF based on geometry and material description
5     #
6     # G. Ward
7     #
8     use strict;
9 greg 2.13 use File::Temp qw/ :mktemp /;
10 greg 2.1 sub userror {
11 greg 2.15 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 greg 2.1 exit 1;
13     }
14 greg 2.13 my $td = mkdtemp("/tmp/genBSDF.XXXXXX");
15 greg 2.1 chomp $td;
16 greg 2.15 my $tensortree = 0;
17     my $ttlog2 = 4;
18 greg 2.1 my $nsamp = 1000;
19 greg 2.10 my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6";
20 greg 2.1 my $mgfin = 0;
21     my $geout = 1;
22     my $nproc = 1;
23 greg 2.9 my $doforw = 0;
24     my $doback = 1;
25 greg 2.1 my @dim;
26     # Get options
27     while ($#ARGV >= 0) {
28     if ("$ARGV[0]" =~ /^[-+]m/) {
29     $mgfin = ("$ARGV[0]" =~ /^\+/);
30 greg 2.10 } elsif ("$ARGV[0]" eq "-r") {
31     $rtargs = "$rtargs $ARGV[1]";
32     shift @ARGV;
33 greg 2.1 } elsif ("$ARGV[0]" =~ /^[-+]g/) {
34     $geout = ("$ARGV[0]" =~ /^\+/);
35 greg 2.9 } elsif ("$ARGV[0]" =~ /^[-+]f/) {
36     $doforw = ("$ARGV[0]" =~ /^\+/);
37     } elsif ("$ARGV[0]" =~ /^[-+]b/) {
38     $doback = ("$ARGV[0]" =~ /^\+/);
39 greg 2.15 } elsif ("$ARGV[0]" =~ /^-t[34]$/) {
40     $tensortree = substr($ARGV[0], 2, 1);
41     $ttlog2 = $ARGV[1];
42     shift @ARGV;
43 greg 2.1 } elsif ("$ARGV[0]" eq "-c") {
44     $nsamp = $ARGV[1];
45     shift @ARGV;
46     } elsif ("$ARGV[0]" eq "-n") {
47     $nproc = $ARGV[1];
48     shift @ARGV;
49     } elsif ("$ARGV[0]" =~ /^-d/) {
50     userror() if ($#ARGV < 6);
51 greg 2.8 @dim = @ARGV[1..6];
52 greg 2.1 shift @ARGV for (1..6);
53     } elsif ("$ARGV[0]" =~ /^[-+]./) {
54     userror();
55     } else {
56     last;
57     }
58     shift @ARGV;
59     }
60 greg 2.9 # Check that we're actually being asked to do something
61 greg 2.15 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 greg 2.1 # Get scene description and dimensions
69     my $radscn = "$td/device.rad";
70     my $mgfscn = "$td/device.mgf";
71     my $octree = "$td/device.oct";
72     if ( $mgfin ) {
73     system "mgfilt '#,o,xf,c,cxy,cspec,cmix,m,sides,rd,td,rs,ts,ir,v,p,n,f,fh,sph,cyl,cone,prism,ring,torus' @ARGV > $mgfscn";
74     die "Could not load MGF input\n" if ( $? );
75     system "mgf2rad $mgfscn > $radscn";
76     } else {
77 greg 2.13 system "xform -e @ARGV > $radscn";
78 greg 2.1 die "Could not load Radiance input\n" if ( $? );
79     system "rad2mgf $radscn > $mgfscn" if ( $geout );
80     }
81     if ($#dim != 5) {
82 greg 2.7 @dim = split ' ', `getbbox -h $radscn`;
83 greg 2.1 }
84     print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5);
85 greg 2.9 # Add receiver surfaces (rectangular)
86 greg 2.10 my $fmodnm="receiver_face";
87 greg 2.9 my $bmodnm="receiver_behind";
88 greg 2.1 open(RADSCN, ">> $radscn");
89 greg 2.10 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 greg 2.1 close RADSCN;
94     # Generate octree
95     system "oconv -w $radscn > $octree";
96     die "Could not compile scene\n" if ( $? );
97 greg 2.15 # 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 greg 2.1 # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms
342 greg 2.15 $tcal = '
343 greg 2.1 DEGREE : PI/180;
344 greg 2.5 sq(x) : x*x;
345 greg 2.1 Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);
346     Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
347     Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0);
348     Kmax : Kaccum(Knaz(0));
349     Kfindrow(r, rem) : if(rem-Knaz(r)+.5, Kfindrow(r+1, rem-Knaz(r)), r);
350     Krow = if(Kbin-(Kmax-.5), 0, Kfindrow(1, Kbin));
351     Kcol = Kbin - Kaccum(Krow-1);
352 greg 2.2 Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow);
353 greg 2.1 Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1));
354     sin_kpol = sin(Kpol);
355 greg 2.9 Dx = cos(Kazi)*sin_kpol;
356 greg 2.1 Dy = sin(Kazi)*sin_kpol;
357     Dz = sqrt(1 - sin_kpol*sin_kpol);
358 greg 2.5 KprojOmega = PI * if(Kbin-.5,
359     (sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow),
360     1 - sq(cos(Kpola(1)*DEGREE)));
361 greg 2.1 ';
362 greg 2.9 # Compute Klems bin from exiting ray direction (forward or backward)
363 greg 2.15 $kcal = '
364 greg 2.1 DEGREE : PI/180;
365 greg 2.11 abs(x) : if(x, x, -x);
366 greg 2.1 Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x)));
367     posangle(a) : if(-a, a + 2*PI, a);
368     Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x));
369     kpola(r) : select(r, 5, 15, 25, 35, 45, 55, 65, 75, 90);
370     knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
371     kaccum(r) : if(r-.5, knaz(r) + kaccum(r-1), 0);
372     kfindrow(r, pol) : if(r-kpola(0)+.5, r,
373     if(pol-kpola(r), kfindrow(r+1, pol), r) );
374     kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0);
375     kbin2(pol,azi) = select(kfindrow(1, pol),
376     kazn(azi,360/knaz(1)),
377     kaccum(1) + kazn(azi,360/knaz(2)),
378     kaccum(2) + kazn(azi,360/knaz(3)),
379     kaccum(3) + kazn(azi,360/knaz(4)),
380     kaccum(4) + kazn(azi,360/knaz(5)),
381     kaccum(5) + kazn(azi,360/knaz(6)),
382     kaccum(6) + kazn(azi,360/knaz(7)),
383     kaccum(7) + kazn(azi,360/knaz(8)),
384     kaccum(8) + kazn(azi,360/knaz(9))
385     );
386 greg 2.11 kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx));
387 greg 2.1 ';
388     my $ndiv = 145;
389 greg 2.3 # Compute scattering data using rtcontrib
390 greg 2.9 my @tfarr;
391     my @rfarr;
392     my @tbarr;
393     my @rbarr;
394     my $cmd;
395 greg 2.15 my $rtcmd = "rtcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " .
396 greg 2.9 "-e '$kcal' -b kbin -bn $ndiv " .
397 greg 2.15 "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $octree";
398 greg 2.9 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'};
401     if ( $doforw ) {
402     $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
403 greg 2.11 "-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
404     "-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
405 greg 2.10 "-e 'zp:$dim[4]' " .
406 greg 2.11 q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } .
407 greg 2.10 q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } .
408 greg 2.9 "| $rtcmd";
409     system "$cmd" || die "Failure running: $cmd\n";
410     @tfarr = `$rccmd $td/$fmodnm.flt`;
411     die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
412     @rfarr = `$rccmd $td/$bmodnm.flt`;
413     die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
414     }
415     if ( $doback ) {
416     $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
417     "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
418     "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
419 greg 2.10 "-e 'zp:$dim[5]' " .
420 greg 2.9 q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } .
421 greg 2.11 q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } .
422 greg 2.9 "| $rtcmd";
423     system "$cmd" || die "Failure running: $cmd\n";
424     @tbarr = `$rccmd $td/$bmodnm.flt`;
425     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 greg 2.15 # Output angle basis
430 greg 2.1 print
431 greg 2.15 ' <DataDefinition>
432     <IncidentDataStructure>Columns</IncidentDataStructure>
433     <AngleBasis>
434 greg 2.1 <AngleBasisName>LBNL/Klems Full</AngleBasisName>
435 greg 2.15 <AngleBasisBlock>
436 greg 2.1 <Theta>0</Theta>
437     <nPhis>1</nPhis>
438     <ThetaBounds>
439 greg 2.15 <LowerTheta>0</LowerTheta>
440     <UpperTheta>5</UpperTheta>
441 greg 2.1 </ThetaBounds>
442     </AngleBasisBlock>
443     <AngleBasisBlock>
444     <Theta>10</Theta>
445     <nPhis>8</nPhis>
446     <ThetaBounds>
447     <LowerTheta>5</LowerTheta>
448     <UpperTheta>15</UpperTheta>
449     </ThetaBounds>
450     </AngleBasisBlock>
451     <AngleBasisBlock>
452     <Theta>20</Theta>
453     <nPhis>16</nPhis>
454     <ThetaBounds>
455     <LowerTheta>15</LowerTheta>
456     <UpperTheta>25</UpperTheta>
457     </ThetaBounds>
458     </AngleBasisBlock>
459     <AngleBasisBlock>
460     <Theta>30</Theta>
461     <nPhis>20</nPhis>
462     <ThetaBounds>
463     <LowerTheta>25</LowerTheta>
464     <UpperTheta>35</UpperTheta>
465     </ThetaBounds>
466     </AngleBasisBlock>
467     <AngleBasisBlock>
468     <Theta>40</Theta>
469     <nPhis>24</nPhis>
470     <ThetaBounds>
471     <LowerTheta>35</LowerTheta>
472     <UpperTheta>45</UpperTheta>
473     </ThetaBounds>
474     </AngleBasisBlock>
475     <AngleBasisBlock>
476     <Theta>50</Theta>
477     <nPhis>24</nPhis>
478     <ThetaBounds>
479     <LowerTheta>45</LowerTheta>
480     <UpperTheta>55</UpperTheta>
481     </ThetaBounds>
482     </AngleBasisBlock>
483     <AngleBasisBlock>
484     <Theta>60</Theta>
485     <nPhis>24</nPhis>
486     <ThetaBounds>
487     <LowerTheta>55</LowerTheta>
488     <UpperTheta>65</UpperTheta>
489     </ThetaBounds>
490     </AngleBasisBlock>
491     <AngleBasisBlock>
492     <Theta>70</Theta>
493     <nPhis>16</nPhis>
494     <ThetaBounds>
495     <LowerTheta>65</LowerTheta>
496     <UpperTheta>75</UpperTheta>
497     </ThetaBounds>
498     </AngleBasisBlock>
499     <AngleBasisBlock>
500     <Theta>82.5</Theta>
501     <nPhis>12</nPhis>
502     <ThetaBounds>
503     <LowerTheta>75</LowerTheta>
504     <UpperTheta>90</UpperTheta>
505     </ThetaBounds>
506     </AngleBasisBlock>
507     </AngleBasis>
508     </DataDefinition>
509 greg 2.9 ';
510     if ( $doforw ) {
511 greg 2.15 print
512     ' <WavelengthData>
513 greg 2.9 <LayerNumber>System</LayerNumber>
514     <Wavelength unit="Integral">Visible</Wavelength>
515     <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
516     <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
517     <WavelengthDataBlock>
518     <WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
519     <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
520     <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
521     <ScatteringDataType>BTDF</ScatteringDataType>
522     <ScatteringData>
523     ';
524     # Output front transmission (transposed order)
525     for (my $od = 0; $od < $ndiv; $od++) {
526     for (my $id = 0; $id < $ndiv; $id++) {
527     print $tfarr[$ndiv*$id + $od];
528     }
529     print "\n";
530     }
531     print
532 greg 2.15 ' </ScatteringData>
533     </WavelengthDataBlock>
534 greg 2.9 </WavelengthData>
535 greg 2.1 <WavelengthData>
536     <LayerNumber>System</LayerNumber>
537     <Wavelength unit="Integral">Visible</Wavelength>
538     <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
539     <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
540     <WavelengthDataBlock>
541 greg 2.9 <WavelengthDataDirection>Reflection Front</WavelengthDataDirection>
542     <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
543     <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
544     <ScatteringDataType>BRDF</ScatteringDataType>
545     <ScatteringData>
546     ';
547     # Output front reflection (transposed order)
548     for (my $od = 0; $od < $ndiv; $od++) {
549     for (my $id = 0; $id < $ndiv; $id++) {
550     print $rfarr[$ndiv*$id + $od];
551     }
552     print "\n";
553     }
554     print
555 greg 2.15 ' </ScatteringData>
556     </WavelengthDataBlock>
557 greg 2.9 </WavelengthData>
558     ';
559     }
560     if ( $doback ) {
561 greg 2.15 print
562     ' <WavelengthData>
563 greg 2.9 <LayerNumber>System</LayerNumber>
564     <Wavelength unit="Integral">Visible</Wavelength>
565     <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
566     <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
567     <WavelengthDataBlock>
568     <WavelengthDataDirection>Transmission Back</WavelengthDataDirection>
569 greg 2.1 <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
570     <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
571     <ScatteringDataType>BTDF</ScatteringDataType>
572     <ScatteringData>
573     ';
574 greg 2.9 # Output back transmission (transposed order)
575 greg 2.3 for (my $od = 0; $od < $ndiv; $od++) {
576     for (my $id = 0; $id < $ndiv; $id++) {
577 greg 2.9 print $tbarr[$ndiv*$id + $od];
578     }
579     print "\n";
580     }
581     print
582 greg 2.15 ' </ScatteringData>
583     </WavelengthDataBlock>
584 greg 2.9 </WavelengthData>
585     <WavelengthData>
586     <LayerNumber>System</LayerNumber>
587     <Wavelength unit="Integral">Visible</Wavelength>
588     <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
589     <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
590     <WavelengthDataBlock>
591     <WavelengthDataDirection>Reflection Back</WavelengthDataDirection>
592     <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
593     <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
594     <ScatteringDataType>BRDF</ScatteringDataType>
595     <ScatteringData>
596     ';
597     # Output back reflection (transposed order)
598     for (my $od = 0; $od < $ndiv; $od++) {
599     for (my $id = 0; $id < $ndiv; $id++) {
600     print $rbarr[$ndiv*$id + $od];
601 greg 2.3 }
602     print "\n";
603     }
604 greg 2.1 print
605 greg 2.15 ' </ScatteringData>
606     </WavelengthDataBlock>
607 greg 2.1 </WavelengthData>
608 greg 2.9 ';
609     }
610 greg 2.15 }
611     #------------- End of do_matrix_bsdf() --------------#