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.19 by greg, Fri Jun 3 19:41:14 2011 UTC vs.
Revision 2.29 by greg, Fri Feb 17 00:14:46 2012 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][-t{3|4} Nlog2][-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 units] [input ..]\n";
12          exit 1;
13   }
14   my $td = mkdtemp("/tmp/genBSDF.XXXXXX");
15   chomp $td;
16 + my @savedARGV = @ARGV;
17   my $tensortree = 0;
18   my $ttlog2 = 4;
19 < my $nsamp = 1000;
19 > my $nsamp = 2000;
20   my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6";
21   my $mgfin = 0;
22   my $geout = 1;
23   my $nproc = 1;
24   my $doforw = 0;
25   my $doback = 1;
26 + my $pctcull = 95;
27 + my $gunit = "Meter";
28   my @dim;
29   # Get options
30   while ($#ARGV >= 0) {
# Line 32 | Line 35 | while ($#ARGV >= 0) {
35                  shift @ARGV;
36          } elsif ("$ARGV[0]" =~ /^[-+]g/) {
37                  $geout = ("$ARGV[0]" =~ /^\+/);
38 +                $gunit = $ARGV[1];
39 +                if ($gunit !~ /^(?i)(meter|foot|inch|centimeter|millimeter)$/) {
40 +                        die "Illegal geometry unit '$gunit': must be meter, foot, inch, centimeter, or millimeter\n";
41 +                }
42 +                shift @ARGV;
43          } elsif ("$ARGV[0]" =~ /^[-+]f/) {
44                  $doforw = ("$ARGV[0]" =~ /^\+/);
45          } elsif ("$ARGV[0]" =~ /^[-+]b/) {
46                  $doback = ("$ARGV[0]" =~ /^\+/);
47 +        } elsif ("$ARGV[0]" eq "-t") {
48 +                $pctcull = $ARGV[1];
49 +                shift @ARGV;
50          } elsif ("$ARGV[0]" =~ /^-t[34]$/) {
51                  $tensortree = substr($ARGV[0], 2, 1);
52                  $ttlog2 = $ARGV[1];
# Line 98 | Line 109 | die "Could not compile scene\n" if ( $? );
109   print
110   '<?xml version="1.0" encoding="UTF-8"?>
111   <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">
112 < <WindowElementType>System</WindowElementType>
112 > ';
113 > print "<!-- File produced by: genBSDF @savedARGV -->\n";
114 > print
115 > '<WindowElementType>System</WindowElementType>
116   <Optical>
117   <Layer>
118          <Material>
119                  <Name>Name</Name>
120                  <Manufacturer>Manufacturer</Manufacturer>
121   ';
122 < printf "\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4];
123 < printf "\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0];
124 < printf "\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2];
122 > printf "\t\t<Thickness unit=\"$gunit\">%.3f</Thickness>\n", $dim[5] - $dim[4];
123 > printf "\t\t<Width unit=\"$gunit\">%.3f</Width>\n", $dim[1] - $dim[0];
124 > printf "\t\t<Height unit=\"$gunit\">%.3f</Height>\n", $dim[3] - $dim[2];
125   print "\t\t<DeviceType>Integral</DeviceType>\n";
126   # Output MGF description if requested
127   if ( $geout ) {
128 <        print "\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n";
128 >        print "\t\t<Geometry format=\"MGF\" unit=\"$gunit\">\n";
129          printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
130          open(MGFSCN, "< $mgfscn");
131          while (<MGFSCN>) { print $_; }
# Line 139 | Line 153 | print
153   </WindowElement>
154   ';
155   # 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 }
156   exec("rm -rf $td");
157  
158   #-------------- End of main program segment --------------#
159  
160 + #++++++++++++++ Kill persistent rtrace +++++++++++++++++++#
161 + sub persist_end {
162 +        if ( $persistfile && open(PFI, "< $persistfile") ) {
163 +                while (<PFI>) {
164 +                        s/^[^ ]* //;
165 +                        kill('ALRM', $_);
166 +                        last;
167 +                }
168 +                close PFI;
169 +        }
170 + }
171 +
172   #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
173   sub do_tree_bsdf {
174   # Get sampling rate and subdivide task
175   my $ns2 = $ns;
176   $ns2 /= 2 if ( $tensortree == 3 );
177 < @pdiv = (0, int($ns2/$nproc));
178 < my $nrem = $ns2 % $nproc;
179 < for (my $i = 1; $i < $nproc; $i++) {
177 > my $nsplice = $nproc;
178 > $nsplice *= 10 if ($nproc > 1);
179 > $nsplice = $ns2 if ($nsplice > $ns2);
180 > $nsplice = 999 if ($nsplice > 999);
181 > @pdiv = (0, int($ns2/$nsplice));
182 > my $nrem = $ns2 % $nsplice;
183 > for (my $i = 1; $i < $nsplice; $i++) {
184          my $nv = $pdiv[$i] + $pdiv[1];
185          ++$nv if ( $nrem-- > 0 );
186          push @pdiv, $nv;
# Line 207 | Line 229 | print "\t<DataDefinition>\n";
229   print "\t\t<IncidentDataStructure>TensorTree$tensortree</IncidentDataStructure>\n";
230   print "\t</DataDefinition>\n";
231   # Fork parallel rtcontrib processes to compute each side
232 + my $npleft = $nproc;
233   if ( $doback ) {
234 <        for (my $proc = 0; $proc < $nproc; $proc++) {
235 <                bg_tree_rtcontrib(0, $proc);
234 >        for (my $splice = 0; $splice < $nsplice; $splice++) {
235 >                if (! $npleft ) {
236 >                        wait();
237 >                        die "rtcontrib process reported error" if ( $? );
238 >                        $npleft++;
239 >                }
240 >                bg_tree_rtcontrib(0, $splice);
241 >                $npleft--;
242          }
243          while (wait() >= 0) {
244                  die "rtcontrib process reported error" if ( $? );
245 +                $npleft++;
246          }
247 +        persist_end();
248          ttree_out(0);
249   }
250   if ( $doforw ) {
251 <        for (my $proc = 0; $proc < $nproc; $proc++) {
252 <                bg_tree_rtcontrib(1, $proc);
251 >        for (my $splice = 0; $splice < $nsplice; $splice++) {
252 >                if (! $npleft ) {
253 >                        wait();
254 >                        die "rtcontrib process reported error" if ( $? );
255 >                        $npleft++;
256 >                }
257 >                bg_tree_rtcontrib(1, $splice);
258 >                $npleft--;
259          }
260          while (wait() >= 0) {
261                  die "rtcontrib process reported error" if ( $? );
262 +                $npleft++;
263          }
264 +        persist_end();
265          ttree_out(1);
266   }
267   }       # end of sub do_tree_bsdf()
268  
269 < # Run i'th rtcontrib process for generating tensor tree samples
269 > # Run rtcontrib process in background to generate tensor tree samples
270   sub bg_tree_rtcontrib {
271          my $pid = fork();
272          die "Cannot fork new process" unless defined $pid;
# Line 299 | Line 338 | print
338   system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
339          q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } .
340          "$td/" . ($bmodnm,$fmodnm)[$forw] . "_???.flt " .
341 <        "| rttree_reduce -h -ff -r $tensortree -g $ttlog2";
341 >        "| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2";
342   die "Failure running rttree_reduce" if ( $? );
343   print
344   '                       </ScatteringData>
# Line 317 | Line 356 | print
356                  <WavelengthDataBlock>
357   ';
358   print "\t\t\t<WavelengthDataDirection>Reflection $side</WavelengthDataDirection>\n";
359 < print '                 <AngleBasis>LBNL/Shirley-Chiu</AngleBasis>
359 > print
360 > '                       <AngleBasis>LBNL/Shirley-Chiu</AngleBasis>
361                          <ScatteringDataType>BRDF</ScatteringDataType>
362                          <ScatteringData>
363   ';
364   system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
365          q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } .
366          "$td/" . ($fmodnm,$bmodnm)[$forw] . "_???.flt " .
367 <        "| rttree_reduce -h -ff -r $tensortree -g $ttlog2";
367 >        "| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2";
368   die "Failure running rttree_reduce" if ( $? );
369   print
370   '                       </ScatteringData>
# Line 397 | Line 437 | my $rtcmd = "rtcontrib $rtargs -h -ff -fo -n $nproc -c
437          "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $octree";
438   my $rccmd = "rcalc -e '$tcal' " .
439          "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " .
440 <        q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'};
440 >        q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega' };
441   if ( $doforw ) {
442   $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
443          "-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
# Line 524 | Line 564 | print
564   # Output front transmission (transposed order)
565   for (my $od = 0; $od < $ndiv; $od++) {
566          for (my $id = 0; $id < $ndiv; $id++) {
567 <                print $tfarr[$ndiv*$id + $od];
567 >                print $tfarr[$ndiv*$id + $od], ",\n";
568          }
569          print "\n";
570   }
# Line 544 | Line 584 | print
584                          <ScatteringDataType>BRDF</ScatteringDataType>
585                          <ScatteringData>
586   ';
587 < # Output front reflection (transposed order)
587 > # Output front reflection (reciprocity averaging)
588   for (my $od = 0; $od < $ndiv; $od++) {
589          for (my $id = 0; $id < $ndiv; $id++) {
590 <                print $rfarr[$ndiv*$id + $od];
590 >                print .5*($rfarr[$ndiv*$id + $od] + $rfarr[$ndiv*$od + $id]), ",\n";
591          }
592          print "\n";
593   }
# Line 574 | Line 614 | print
614   # Output back transmission (transposed order)
615   for (my $od = 0; $od < $ndiv; $od++) {
616          for (my $id = 0; $id < $ndiv; $id++) {
617 <                print $tbarr[$ndiv*$id + $od];
617 >                print $tbarr[$ndiv*$id + $od], ",\n";
618          }
619          print "\n";
620   }
# Line 594 | Line 634 | print
634                          <ScatteringDataType>BRDF</ScatteringDataType>
635                          <ScatteringData>
636   ';
637 < # Output back reflection (transposed order)
637 > # Output back reflection (reciprocity averaging)
638   for (my $od = 0; $od < $ndiv; $od++) {
639          for (my $id = 0; $id < $ndiv; $id++) {
640 <                print $rbarr[$ndiv*$id + $od];
640 >                print .5*($rbarr[$ndiv*$id + $od] + $rbarr[$ndiv*$od + $id]), ",\n";
641          }
642          print "\n";
643   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines