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.8 by greg, Sat Jan 29 16:44:01 2011 UTC vs.
Revision 2.9 by greg, Mon Feb 21 22:48:51 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines