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.5 by greg, Thu Dec 9 17:00:43 2010 UTC vs.
Revision 2.12 by greg, Thu Feb 24 20:27:00 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][-r \"ropts\"][-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;
# Line 31 | Line 41 | while ($#ARGV >= 0) {
41                  shift @ARGV;
42          } elsif ("$ARGV[0]" =~ /^-d/) {
43                  userror() if ($#ARGV < 6);
44 <                @dim = "@ARGV[1..6]";
44 >                @dim = @ARGV[1..6];
45                  shift @ARGV for (1..6);
46          } elsif ("$ARGV[0]" =~ /^[-+]./) {
47                  userror();
# Line 40 | Line 50 | while ($#ARGV >= 0) {
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";
# Line 54 | Line 66 | if ( $mgfin ) {
66          system "rad2mgf $radscn > $mgfscn" if ( $geout );
67   }
68   if ($#dim != 5) {
69 <        @dim = split /\s+/, `getbbox -h $radscn`;
58 <        shift @dim;
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";
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";
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;
# Line 86 | Line 96 | Kcol = Kbin - Kaccum(Krow-1);
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 + abs(x) : if(x, x, -x);
110   Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x)));
111   posangle(a) : if(-a, a + 2*PI, a);
112   Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x));
# Line 116 | Line 127 | kbin2(pol,azi) = select(kfindrow(1, pol),
127                  kaccum(7) + kazn(azi,360/knaz(8)),
128                  kaccum(8) + kazn(azi,360/knaz(9))
129          );
130 < kbin = kbin2(Acos(Dz), Atan2(Dy, -Dx));
130 > kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx));
131   ';
132   my $ndiv = 145;
133   my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5);
134   my $ny = int($nsamp/$nx + .5);
135   $nsamp = $nx * $ny;
136   # Compute scattering data using rtcontrib
137 < my $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
137 > my @tfarr;
138 > my @rfarr;
139 > my @tbarr;
140 > my @rbarr;
141 > my $cmd;
142 > my $rtcmd = "rtcontrib -h -ff -fo -n $nproc -c $nsamp " .
143 >        "-e '$kcal' -b kbin -bn $ndiv " .
144 >        "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $rtargs $octree";
145 > my $rccmd = "rcalc -e '$tcal' " .
146 >        "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " .
147 >        q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'};
148 > if ( $doforw ) {
149 > $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
150 >        "-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
151 >        "-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
152 >        "-e 'zp:$dim[4]' " .
153 >        q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } .
154 >        q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } .
155 >        "| $rtcmd";
156 > system "$cmd" || die "Failure running: $cmd\n";
157 > @tfarr = `$rccmd $td/$fmodnm.flt`;
158 > die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
159 > @rfarr = `$rccmd $td/$bmodnm.flt`;
160 > die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
161 > }
162 > if ( $doback ) {
163 > $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
164          "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
165          "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
166 <        "-e 'zp:$dim[4]-1e-5' " .
166 >        "-e 'zp:$dim[5]' " .
167          q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } .
168 <        q{-e '$1=xp;$2=yp;$3=zp;$4=Dx;$5=Dy;$6=Dz' } .
169 <        "| rtcontrib -h -ff -n $nproc -c $nsamp -e '$kcal' -b kbin -bn $ndiv " .
170 <        "-m $modnm -w -ab 5 -ad 700 -lw 3e-6 $octree " .
171 <        "| rcalc -e '$tcal' " .
172 <        "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-1,$ndiv)' " .
173 <        q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'};
174 < my @darr = `$cmd`;
175 < die "Failure running: $cmd\n" if ( $? );
168 >        q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } .
169 >        "| $rtcmd";
170 > system "$cmd" || die "Failure running: $cmd\n";
171 > @tbarr = `$rccmd $td/$bmodnm.flt`;
172 > die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
173 > @rbarr = `$rccmd $td/$fmodnm.flt`;
174 > die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
175 > }
176   # Output XML prologue
177   print
178   '<?xml version="1.0" encoding="UTF-8"?>
# Line 238 | Line 275 | print '                        </Material>
275                          </AngleBasisBlock>
276                  </AngleBasis>
277          </DataDefinition>
278 <        <WavelengthData>
278 > ';
279 > if ( $doforw ) {
280 > print '         <WavelengthData>
281                  <LayerNumber>System</LayerNumber>
282                  <Wavelength unit="Integral">Visible</Wavelength>
283                  <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
# Line 250 | Line 289 | print '                        </Material>
289                          <ScatteringDataType>BTDF</ScatteringDataType>
290                          <ScatteringData>
291   ';
292 < # Output computed data (transposed order)
292 > # Output front transmission (transposed order)
293   for (my $od = 0; $od < $ndiv; $od++) {
294          for (my $id = 0; $id < $ndiv; $id++) {
295 <                print $darr[$ndiv*$id + $od];
295 >                print $tfarr[$ndiv*$id + $od];
296          }
297          print "\n";
298   }
260 # Output XML epilogue
299   print
300   '               </ScatteringData>
301          </WavelengthDataBlock>
302          </WavelengthData>
303 < </Layer>
303 >        <WavelengthData>
304 >                <LayerNumber>System</LayerNumber>
305 >                <Wavelength unit="Integral">Visible</Wavelength>
306 >                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
307 >                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
308 >                <WavelengthDataBlock>
309 >                        <WavelengthDataDirection>Reflection Front</WavelengthDataDirection>
310 >                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
311 >                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
312 >                        <ScatteringDataType>BRDF</ScatteringDataType>
313 >                        <ScatteringData>
314 > ';
315 > # Output front reflection (transposed order)
316 > for (my $od = 0; $od < $ndiv; $od++) {
317 >        for (my $id = 0; $id < $ndiv; $id++) {
318 >                print $rfarr[$ndiv*$id + $od];
319 >        }
320 >        print "\n";
321 > }
322 > print
323 > '               </ScatteringData>
324 >        </WavelengthDataBlock>
325 >        </WavelengthData>
326 > ';
327 > }
328 > if ( $doback ) {
329 > print '         <WavelengthData>
330 >                <LayerNumber>System</LayerNumber>
331 >                <Wavelength unit="Integral">Visible</Wavelength>
332 >                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
333 >                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
334 >                <WavelengthDataBlock>
335 >                        <WavelengthDataDirection>Transmission Back</WavelengthDataDirection>
336 >                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
337 >                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
338 >                        <ScatteringDataType>BTDF</ScatteringDataType>
339 >                        <ScatteringData>
340 > ';
341 > # Output back transmission (transposed order)
342 > for (my $od = 0; $od < $ndiv; $od++) {
343 >        for (my $id = 0; $id < $ndiv; $id++) {
344 >                print $tbarr[$ndiv*$id + $od];
345 >        }
346 >        print "\n";
347 > }
348 > print
349 > '               </ScatteringData>
350 >        </WavelengthDataBlock>
351 >        </WavelengthData>
352 >        <WavelengthData>
353 >                <LayerNumber>System</LayerNumber>
354 >                <Wavelength unit="Integral">Visible</Wavelength>
355 >                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
356 >                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
357 >                <WavelengthDataBlock>
358 >                        <WavelengthDataDirection>Reflection Back</WavelengthDataDirection>
359 >                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
360 >                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
361 >                        <ScatteringDataType>BRDF</ScatteringDataType>
362 >                        <ScatteringData>
363 > ';
364 > # Output back reflection (transposed order)
365 > for (my $od = 0; $od < $ndiv; $od++) {
366 >        for (my $id = 0; $id < $ndiv; $id++) {
367 >                print $rbarr[$ndiv*$id + $od];
368 >        }
369 >        print "\n";
370 > }
371 > print
372 > '               </ScatteringData>
373 >        </WavelengthDataBlock>
374 >        </WavelengthData>
375 > ';
376 > }
377 > # Output XML epilogue
378 > print '</Layer>
379   </Optical>
380   </WindowElement>
381   ';

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines