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.12 by greg, Thu Feb 24 20:27:00 2011 UTC vs.
Revision 2.55 by greg, Fri Mar 27 18:58:06 2015 UTC

# Line 6 | Line 6
6   #       G. Ward
7   #
8   use strict;
9 + my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64");
10 + use File::Temp qw/ :mktemp  /;
11   sub userror {
12 <        print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-r \"ropts\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom] [input ..]\n";
12 >        print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-W][-t{3|4} Nlog2][-r \"ropts\"][-f \"x=string;y=string\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}C][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom units] [input ..]\n";
13          exit 1;
14   }
15 < my $td = `mktemp -d /tmp/genBSDF.XXXXXX`;
16 < chomp $td;
17 < my $nsamp = 1000;
18 < my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6";
15 > my ($td,$radscn,$mgfscn,$octree,$fsender,$bsender,$receivers,$facedat,$behinddat,$rmtmp);
16 > my ($tf,$rf,$tb,$rb,$tfx,$rfx,$tbx,$rbx,$tfz,$rfz,$tbz,$rbz);
17 > if ($windoz) {
18 >        my $tmploc = `echo \%TMP\%`;
19 >        chomp($tmploc);
20 >        $td = mkdtemp("$tmploc\\genBSDF.XXXXXX");
21 >        $radscn = "$td\\device.rad";
22 >        $mgfscn = "$td\\device.mgf";
23 >        $octree = "$td\\device.oct";
24 >        $fsender = "$td\\fsender.rad";
25 >        $bsender = "$td\\bsender.rad";
26 >        $receivers = "$td\\receivers.rad";
27 >        $facedat = "$td\\face.dat";
28 >        $behinddat = "$td\\behind.dat";
29 >        $tf = "$td\\tf.dat";
30 >        $rf = "$td\\rf.dat";
31 >        $tb = "$td\\tb.dat";
32 >        $rb = "$td\\rb.dat";
33 >        $tfx = "$td\\tfx.dat";
34 >        $rfx = "$td\\rfx.dat";
35 >        $tbx = "$td\\tbx.dat";
36 >        $rbx = "$td\\rbx.dat";
37 >        $tfz = "$td\\tfz.dat";
38 >        $rfz = "$td\\rfz.dat";
39 >        $tbz = "$td\\tbz.dat";
40 >        $rbz = "$td\\rbz.dat";
41 >        chomp $td;
42 >        $rmtmp = "rmdir /S /Q $td";
43 > } else {
44 >        $td = mkdtemp("/tmp/genBSDF.XXXXXX");
45 >        chomp $td;
46 >        $radscn = "$td/device.rad";
47 >        $mgfscn = "$td/device.mgf";
48 >        $octree = "$td/device.oct";
49 >        $fsender = "$td/fsender.rad";
50 >        $bsender = "$td/bsender.rad";
51 >        $receivers = "$td/receivers.rad";
52 >        $facedat = "$td/face.dat";
53 >        $behinddat = "$td/behind.dat";
54 >        $tf = "$td/tf.dat";
55 >        $rf = "$td/rf.dat";
56 >        $tb = "$td/tb.dat";
57 >        $rb = "$td/rb.dat";
58 >        $tfx = "$td/tfx.dat";
59 >        $rfx = "$td/rfx.dat";
60 >        $tbx = "$td/tbx.dat";
61 >        $rbx = "$td/rbx.dat";
62 >        $tfz = "$td/tfz.dat";
63 >        $rfz = "$td/rfz.dat";
64 >        $tbz = "$td/tbz.dat";
65 >        $rbz = "$td/rbz.dat";
66 >        $rmtmp = "rm -rf $td";
67 > }
68 > my @savedARGV = @ARGV;
69 > my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6";
70 > my $wrapper = "wrapBSDF";
71 > my $tensortree = 0;
72 > my $ttlog2 = 4;
73 > my $nsamp = 2000;
74   my $mgfin = 0;
75   my $geout = 1;
76   my $nproc = 1;
77 + my $docolor = 0;
78   my $doforw = 0;
79   my $doback = 1;
80 + my $pctcull = 90;
81 + my $gunit = "meter";
82 + my $curspec = "Visible";
83   my @dim;
84   # Get options
85   while ($#ARGV >= 0) {
86          if ("$ARGV[0]" =~ /^[-+]m/) {
87                  $mgfin = ("$ARGV[0]" =~ /^\+/);
88          } elsif ("$ARGV[0]" eq "-r") {
89 <                $rtargs = "$rtargs $ARGV[1]";
89 >                $rfluxmtx .= " $ARGV[1]";
90                  shift @ARGV;
91          } elsif ("$ARGV[0]" =~ /^[-+]g/) {
92                  $geout = ("$ARGV[0]" =~ /^\+/);
93 +                $gunit = $ARGV[1];
94 +                if ($gunit !~ /^(?i)(meter|foot|inch|centimeter|millimeter)$/) {
95 +                        die "Illegal geometry unit '$gunit': must be meter, foot, inch, centimeter, or millimeter\n";
96 +                }
97 +                shift @ARGV;
98 +        } elsif ("$ARGV[0]" =~ /^[-+]C/) {
99 +                $docolor = ("$ARGV[0]" =~ /^\+/);
100          } elsif ("$ARGV[0]" =~ /^[-+]f/) {
101                  $doforw = ("$ARGV[0]" =~ /^\+/);
102          } elsif ("$ARGV[0]" =~ /^[-+]b/) {
103                  $doback = ("$ARGV[0]" =~ /^\+/);
104 +        } elsif ("$ARGV[0]" eq "-t") {
105 +                # Use value < 0 for rttree_reduce bypass
106 +                $pctcull = $ARGV[1];
107 +                if ($pctcull >= 100) {
108 +                        die "Illegal -t culling percentage, must be < 100\n";
109 +                }
110 +                shift @ARGV;
111 +        } elsif ("$ARGV[0]" =~ /^-t[34]$/) {
112 +                $tensortree = substr($ARGV[0], 2, 1);
113 +                $ttlog2 = $ARGV[1];
114 +                shift @ARGV;
115 +        } elsif ("$ARGV[0]" eq "-s") {
116 +                $wrapper .= " -f \"$ARGV[1]\"";
117 +                shift @ARGV;
118 +        } elsif ("$ARGV[0]" eq "-W") {
119 +                $wrapper .= " -W";
120          } elsif ("$ARGV[0]" eq "-c") {
121                  $nsamp = $ARGV[1];
122                  shift @ARGV;
# Line 51 | Line 135 | while ($#ARGV >= 0) {
135          shift @ARGV;
136   }
137   # Check that we're actually being asked to do something
138 < die "Must have at least one of +forward or +backward" if (!$doforw && !$doback);
138 > die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback);
139 > # Issue warning for unhandled reciprocity case
140 > print STDERR "Warning: recommend both +forward and +backward with -t3\n" if
141 >                ($tensortree==3 && !($doforw && $doback));
142 > $wrapper .= $tensortree ? " -a t$tensortree" : " -a kf -c";
143 > $wrapper .= " -u $gunit";
144   # Get scene description and dimensions
56 my $radscn = "$td/device.rad";
57 my $mgfscn = "$td/device.mgf";
58 my $octree = "$td/device.oct";
145   if ( $mgfin ) {
146 <        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";
146 >        system "mgf2rad @ARGV > $radscn";
147          die "Could not load MGF input\n" if ( $? );
62        system "mgf2rad $mgfscn > $radscn";
148   } else {
149 <        system "cat @ARGV | xform -e > $radscn";
149 >        system "xform -e @ARGV > $radscn";
150          die "Could not load Radiance input\n" if ( $? );
66        system "rad2mgf $radscn > $mgfscn" if ( $geout );
151   }
152   if ($#dim != 5) {
153          @dim = split ' ', `getbbox -h $radscn`;
154   }
155   print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5);
156 < # Add receiver surfaces (rectangular)
157 < my $fmodnm="receiver_face";
74 < my $bmodnm="receiver_behind";
75 < open(RADSCN, ">> $radscn");
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;
156 > $wrapper .= ' -f "t=' . ($dim[5] - $dim[4]) . ';w=' . ($dim[1] - $dim[0]) .
157 >                ';h=' . ($dim[3] - $dim[2]) . '"';
158   # Generate octree
159   system "oconv -w $radscn > $octree";
160   die "Could not compile scene\n" if ( $? );
161 < # 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;
88 < sq(x) : x*x;
89 < Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);
90 < Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
91 < Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0);
92 < Kmax : Kaccum(Knaz(0));
93 < Kfindrow(r, rem) : if(rem-Knaz(r)+.5, Kfindrow(r+1, rem-Knaz(r)), r);
94 < Krow = if(Kbin-(Kmax-.5), 0, Kfindrow(1, Kbin));
95 < 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;
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 (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));
113 < kpola(r) : select(r, 5, 15, 25, 35, 45, 55, 65, 75, 90);
114 < knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
115 < kaccum(r) : if(r-.5, knaz(r) + kaccum(r-1), 0);
116 < kfindrow(r, pol) : if(r-kpola(0)+.5, r,
117 <                if(pol-kpola(r), kfindrow(r+1, pol), r) );
118 < kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0);
119 < kbin2(pol,azi) = select(kfindrow(1, pol),
120 <                kazn(azi,360/knaz(1)),
121 <                kaccum(1) + kazn(azi,360/knaz(2)),
122 <                kaccum(2) + kazn(azi,360/knaz(3)),
123 <                kaccum(3) + kazn(azi,360/knaz(4)),
124 <                kaccum(4) + kazn(azi,360/knaz(5)),
125 <                kaccum(5) + kazn(azi,360/knaz(6)),
126 <                kaccum(6) + kazn(azi,360/knaz(7)),
127 <                kaccum(7) + kazn(azi,360/knaz(8)),
128 <                kaccum(8) + kazn(azi,360/knaz(9))
129 <        );
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 @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[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-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"?>
179 < <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">
180 <        <WindowElementType>System</WindowElementType>
181 <        <Optical>
182 <                <Layer>
183 <                <Material>
184 <                        <Name>Name</Name>
185 <                        <Manufacturer>Manufacturer</Manufacturer>
186 < ';
187 < printf "\t\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4];
188 < printf "\t\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0];
189 < printf "\t\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2];
190 < print "\t\t\t<DeviceType>Integral</DeviceType>\n";
191 < # Output MGF description if requested
161 > # Add MGF description if requested
162   if ( $geout ) {
163 <        print "\t\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n";
164 <        printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
165 <        system "cat $mgfscn";
166 <        print "xf\n";
167 <        print "\t\t\t</Geometry>\n";
168 < }
169 < print '                 </Material>
200 <                <DataDefinition>
201 <                        <IncidentDataStructure>Columns</IncidentDataStructure>
202 <                        <AngleBasis>
203 <                        <AngleBasisName>LBNL/Klems Full</AngleBasisName>
204 <                                <AngleBasisBlock>
205 <                                <Theta>0</Theta>
206 <                                <nPhis>1</nPhis>
207 <                                <ThetaBounds>
208 <                                <LowerTheta>0</LowerTheta>
209 <                                <UpperTheta>5</UpperTheta>
210 <                                </ThetaBounds>
211 <                                </AngleBasisBlock>
212 <                                <AngleBasisBlock>
213 <                                <Theta>10</Theta>
214 <                                <nPhis>8</nPhis>
215 <                                <ThetaBounds>
216 <                                        <LowerTheta>5</LowerTheta>
217 <                                        <UpperTheta>15</UpperTheta>
218 <                                </ThetaBounds>
219 <                                </AngleBasisBlock>
220 <                                <AngleBasisBlock>
221 <                                <Theta>20</Theta>
222 <                                <nPhis>16</nPhis>
223 <                                <ThetaBounds>
224 <                                        <LowerTheta>15</LowerTheta>
225 <                                        <UpperTheta>25</UpperTheta>
226 <                                </ThetaBounds>
227 <                                </AngleBasisBlock>
228 <                                <AngleBasisBlock>
229 <                                <Theta>30</Theta>
230 <                                <nPhis>20</nPhis>
231 <                                <ThetaBounds>
232 <                                        <LowerTheta>25</LowerTheta>
233 <                                        <UpperTheta>35</UpperTheta>
234 <                                </ThetaBounds>
235 <                                </AngleBasisBlock>
236 <                                <AngleBasisBlock>
237 <                                <Theta>40</Theta>
238 <                                <nPhis>24</nPhis>
239 <                                <ThetaBounds>
240 <                                        <LowerTheta>35</LowerTheta>
241 <                                        <UpperTheta>45</UpperTheta>
242 <                                </ThetaBounds>
243 <                                </AngleBasisBlock>
244 <                                <AngleBasisBlock>
245 <                                <Theta>50</Theta>
246 <                                <nPhis>24</nPhis>
247 <                                <ThetaBounds>
248 <                                        <LowerTheta>45</LowerTheta>
249 <                                        <UpperTheta>55</UpperTheta>
250 <                                </ThetaBounds>
251 <                                </AngleBasisBlock>
252 <                                <AngleBasisBlock>
253 <                                <Theta>60</Theta>
254 <                                <nPhis>24</nPhis>
255 <                                <ThetaBounds>
256 <                                        <LowerTheta>55</LowerTheta>
257 <                                        <UpperTheta>65</UpperTheta>
258 <                                </ThetaBounds>
259 <                                </AngleBasisBlock>
260 <                                <AngleBasisBlock>
261 <                                <Theta>70</Theta>
262 <                                <nPhis>16</nPhis>
263 <                                <ThetaBounds>
264 <                                        <LowerTheta>65</LowerTheta>
265 <                                        <UpperTheta>75</UpperTheta>
266 <                                </ThetaBounds>
267 <                                </AngleBasisBlock>
268 <                                <AngleBasisBlock>
269 <                                <Theta>82.5</Theta>
270 <                                <nPhis>12</nPhis>
271 <                                <ThetaBounds>
272 <                                        <LowerTheta>75</LowerTheta>
273 <                                        <UpperTheta>90</UpperTheta>
274 <                                </ThetaBounds>
275 <                        </AngleBasisBlock>
276 <                </AngleBasis>
277 <        </DataDefinition>
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>
284 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
285 <                <WavelengthDataBlock>
286 <                        <WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
287 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
288 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
289 <                        <ScatteringDataType>BTDF</ScatteringDataType>
290 <                        <ScatteringData>
291 < ';
292 < # Output front transmission (transposed order)
293 < for (my $od = 0; $od < $ndiv; $od++) {
294 <        for (my $id = 0; $id < $ndiv; $id++) {
295 <                print $tfarr[$ndiv*$id + $od];
163 >        open(MGFSCN, "> $mgfscn");
164 >        printf MGFSCN "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
165 >        close MGFSCN;
166 >        if ( $mgfin ) {
167 >                system qq{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};
168 >        } else {
169 >                system "rad2mgf $radscn >> $mgfscn";
170          }
171 <        print "\n";
171 >        open(MGFSCN, ">> $mgfscn");
172 >        print MGFSCN "xf\n";
173 >        close MGFSCN;
174 >        $wrapper .= " -g $mgfscn";
175   }
176 < print
177 < '               </ScatteringData>
178 <        </WavelengthDataBlock>
179 <        </WavelengthData>
180 <        <WavelengthData>
181 <                <LayerNumber>System</LayerNumber>
182 <                <Wavelength unit="Integral">Visible</Wavelength>
183 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
184 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
185 <                <WavelengthDataBlock>
186 <                        <WavelengthDataDirection>Reflection Front</WavelengthDataDirection>
187 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
188 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
189 <                        <ScatteringDataType>BRDF</ScatteringDataType>
190 <                        <ScatteringData>
191 < ';
192 < # Output front reflection (transposed order)
193 < for (my $od = 0; $od < $ndiv; $od++) {
194 <        for (my $id = 0; $id < $ndiv; $id++) {
195 <                print $rfarr[$ndiv*$id + $od];
196 <        }
197 <        print "\n";
176 > # Create receiver & sender surfaces (rectangular)
177 > my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1);
178 > my $ny = int($nsamp/$nx + 1);
179 > $nsamp = $nx * $ny;
180 > my $ns = 2**$ttlog2;
181 > open(RADSCN, "> $receivers");
182 > print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=+sc$ns\n" : "h=+kf\n");
183 > print RADSCN '#@rfluxmtx ' . "u=Y o=$facedat\n\n";
184 > print RADSCN "void glow receiver_face\n0\n0\n4 1 1 1 0\n\n";
185 > print RADSCN "receiver_face source f_receiver\n0\n0\n4 0 0 1 180\n\n";
186 > print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=-sc$ns\n" : "h=-kf\n");
187 > print RADSCN '#@rfluxmtx ' . "u=Y o=$behinddat\n\n";
188 > print RADSCN "void glow receiver_behind\n0\n0\n4 1 1 1 0\n\n";
189 > print RADSCN "receiver_behind source b_receiver\n0\n0\n4 0 0 -1 180\n";
190 > close RADSCN;
191 > # Finish rfluxmtx command and prepare sender surfaces
192 > $rfluxmtx .= " -n $nproc -c $nsamp";
193 > if ( $tensortree != 3 ) {       # Isotropic tensor tree is exception
194 >        open (RADSCN, "> $fsender");
195 >        print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=+sc$ns\n\n" : "h=+kf\n\n");
196 >        print RADSCN "void polygon fwd_sender\n0\n0\n12\n";
197 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[4];
198 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[4];
199 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[4];
200 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[4];
201 >        close RADSCN;
202 >        open (RADSCN, "> $bsender");
203 >        print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=-sc$ns\n\n" : "h=-kf\n\n");
204 >        print RADSCN "void polygon bwd_sender\n0\n0\n12\n";
205 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[5];
206 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[5];
207 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[5];
208 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[5];
209 >        close RADSCN;
210   }
211 < print
212 < '               </ScatteringData>
213 <        </WavelengthDataBlock>
214 <        </WavelengthData>
215 < ';
211 > # Calculate CIE (u',v') from Radiance RGB:
212 > my $CIEuv =     'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' .
213 >                'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' .
214 >                'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' .
215 >                'den=Xi+15*Yi+3*Zi;' .
216 >                'uprime=4*Xi/den;vprime=9*Yi/den;' ;
217 > # Create data segments (all the work happens here)
218 > if ( $tensortree ) {
219 >        do_tree_bsdf();
220 > } else {
221 >        do_matrix_bsdf();
222   }
223 < if ( $doback ) {
224 < print '         <WavelengthData>
225 <                <LayerNumber>System</LayerNumber>
226 <                <Wavelength unit="Integral">Visible</Wavelength>
227 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
228 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
229 <                <WavelengthDataBlock>
230 <                        <WavelengthDataDirection>Transmission Back</WavelengthDataDirection>
231 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
232 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
233 <                        <ScatteringDataType>BTDF</ScatteringDataType>
234 <                        <ScatteringData>
235 < ';
236 < # Output back transmission (transposed order)
237 < for (my $od = 0; $od < $ndiv; $od++) {
238 <        for (my $id = 0; $id < $ndiv; $id++) {
239 <                print $tbarr[$ndiv*$id + $od];
223 > # Output XML
224 > my $old_fh = select(STDOUT);
225 > $| = 1;
226 > select($old_fh);
227 > print "<!-- Created by: genBSDF @savedARGV -->\n";
228 > # print STDERR "Running: $wrapper\n";
229 > system $wrapper;
230 > die "Could not wrap BSDF data\n" if ( $? );
231 > # Clean up temporary files and exit
232 > exec $rmtmp;
233 >
234 > #-------------- End of main program segment --------------#
235 >
236 > #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
237 > sub do_tree_bsdf {
238 >
239 >        # Run rfluxmtx processes to compute each side
240 >        do_ttree_dir(0) if ( $doback );
241 >        do_ttree_dir(1) if ( $doforw );
242 >
243 > }       # end of sub do_tree_bsdf()
244 >
245 > # Call rfluxmtx and process tensor tree BSDF for the given direction
246 > sub do_ttree_dir {
247 >        my $forw = shift;
248 >        my $cmd;
249 >        if ( $tensortree == 3 ) {
250 >                # Isotropic BSDF
251 >                my $ns2 = $ns / 2;
252 >                if ($windoz) {
253 >                        $cmd = "cnt $ns2 $ny $nx " .
254 >                                qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
255 >                                qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
256 >                                qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
257 >                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
258 >                                qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
259 >                                qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
260 >                                qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
261 >                                qq{-e "\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz" } .
262 >                                qq{-e "\$4=Dx;\$5=Dy;\$6=myDz" } .
263 >                                "| $rfluxmtx -fa -y $ns2 - $receivers -i $octree";
264 >                } else {
265 >                        $cmd = "cnt $ns2 $ny $nx " .
266 >                                qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
267 >                                qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
268 >                                qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
269 >                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
270 >                                qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
271 >                                qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
272 >                                qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
273 >                                qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } .
274 >                                qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } .
275 >                                "| $rfluxmtx -h -ff -y $ns2 - $receivers -i $octree";
276 >                }
277 >        } else {
278 >                # Anisotropic BSDF
279 >                my $sender = ($bsender,$fsender)[$forw];
280 >                if ($windoz) {
281 >                        $cmd = "$rfluxmtx -fa $sender $receivers -i $octree";
282 >                } else {
283 >                        $cmd = "$rfluxmtx -h -ff $sender $receivers -i $octree";
284 >                }
285          }
286 <        print "\n";
287 < }
288 < print
289 < '               </ScatteringData>
290 <        </WavelengthDataBlock>
291 <        </WavelengthData>
292 <        <WavelengthData>
293 <                <LayerNumber>System</LayerNumber>
294 <                <Wavelength unit="Integral">Visible</Wavelength>
295 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
296 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
297 <                <WavelengthDataBlock>
298 <                        <WavelengthDataDirection>Reflection Back</WavelengthDataDirection>
299 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
300 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
301 <                        <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];
286 >        # print STDERR "Starting: $cmd\n";
287 >        system $cmd;
288 >        die "Failure running rfluxmtx" if ( $? );
289 >        ttree_out($forw);
290 > }       # end of do_ttree_dir()
291 >
292 > # Simplify and store tensor tree results
293 > sub ttree_out {
294 >        my $forw = shift;
295 >        my ($refldat,$transdat);
296 >        if ( $forw ) {
297 >                $transdat = $facedat;
298 >                $refldat = $behinddat;
299 >        } else {
300 >                $transdat = $behinddat;
301 >                $refldat = $facedat;
302          }
303 <        print "\n";
304 < }
305 < print
306 < '               </ScatteringData>
307 <        </WavelengthDataBlock>
308 <        </WavelengthData>
309 < ';
310 < }
311 < # Output XML epilogue
312 < print '</Layer>
313 < </Optical>
314 < </WindowElement>
315 < ';
316 < # Clean up temporary files
317 < system "rm -rf $td";
303 >        # Only output one transmitted anisotropic distribution, preferring backwards
304 >        if ( !$forw || !$doback || $tensortree==3 ) {
305 >                my $ttyp = ("tb","tf")[$forw];
306 >                ttree_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
307 >                if ( $docolor ) {
308 >                        ttree_comp($ttyp, "CIE-u", $transdat, ($tbx,$tfx)[$forw]);
309 >                        ttree_comp($ttyp, "CIE-v", $transdat, ($tbz,$tfz)[$forw]);
310 >                }
311 >        }
312 >        # Output reflection
313 >        my $rtyp = ("rb","rf")[$forw];
314 >        ttree_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
315 >        if ( $docolor ) {
316 >                ttree_comp($rtyp, "CIE-u", $refldat, ($rbx,$rfx)[$forw]);
317 >                ttree_comp($rtyp, "CIE-v", $refldat, ($rbz,$rfz)[$forw]);
318 >        }
319 > }       # end of ttree_out()
320 >
321 > # Call rttree_reduce on the given component
322 > sub ttree_comp {
323 >        my $typ = shift;
324 >        my $spec = shift;
325 >        my $src = shift;
326 >        my $dest = shift;
327 >        my $cmd;
328 >        if ($windoz) {
329 >                if ("$spec" eq "Visible") {
330 >                        $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
331 >                                q{-e "Ri=$1;Gi=$2;Bi=$3" } .
332 >                                qq{-e "$CIEuv" } .
333 >                                q{-e "$1=Yi/Omega"};
334 >                } elsif ("$spec" eq "CIE-u") {
335 >                        $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } .
336 >                                qq{-e "$CIEuv" } .
337 >                                q{-e "$1=uprime"};
338 >                } elsif ("$spec" eq "CIE-v") {
339 >                        $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } .
340 >                                qq{-e "$CIEuv" } .
341 >                                q{-e "$1=vprime"};
342 >                }
343 >        } else {
344 >                if ("$spec" eq "Visible") {
345 >                        $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
346 >                                q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
347 >                                "-e '$CIEuv' " .
348 >                                q{-e '$1=Yi/Omega'};
349 >                } elsif ("$spec" eq "CIE-u") {
350 >                        $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } .
351 >                                "-e '$CIEuv' " .
352 >                                q{-e '$1=uprime'};
353 >                } elsif ("$spec" eq "CIE-v") {
354 >                        $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } .
355 >                                "-e '$CIEuv' " .
356 >                                q{-e '$1=vprime'};
357 >                }
358 >        }
359 >        if ($pctcull >= 0) {
360 >                my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : "";
361 >                my $pcull = ("$spec" eq "Visible") ? $pctcull :
362 >                                                     (100 - (100-$pctcull)*.25) ;
363 >                if ($windoz) {
364 >                        $cmd = "rcollate -ho -oc 1 $src | " .
365 >                                        $cmd .
366 >                                        " | rttree_reduce$avg -h -fa -t $pcull -r $tensortree -g $ttlog2";
367 >                } else {
368 >                        $cmd .= " -of $src " .
369 >                                        "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2";
370 >                }
371 >                # print STDERR "Running: $cmd\n";
372 >                system "$cmd > $dest";
373 >                die "Failure running rttree_reduce" if ( $? );
374 >        } else {
375 >                if ($windoz) {
376 >                        $cmd = "rcollate -ho -oc 1 $src | " . $cmd ;
377 >                } else {
378 >                        $cmd .= " $src";
379 >                }
380 >                open(DATOUT, "> $dest");
381 >                print DATOUT "{\n";
382 >                close DATOUT;
383 >                # print STDERR "Running: $cmd\n";
384 >                system "$cmd >> $dest";
385 >                die "Failure running rcalc" if ( $? );
386 >                open(DATOUT, ">> $dest");
387 >                for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) {
388 >                        print DATOUT "0\n";
389 >                }
390 >                print DATOUT "}\n";
391 >                close DATOUT;
392 >        }
393 >        if ( "$spec" ne "$curspec" ) {
394 >                $wrapper .= " -s $spec";
395 >                $curspec = $spec;
396 >        }
397 >        $wrapper .= " -$typ $dest";
398 > }       # end of ttree_comp()
399 >
400 > #------------- End of do_tree_bsdf() & subroutines -------------#
401 >
402 > #+++++++++++++++ Klems matrix BSDF generation +++++++++++++++#
403 > sub do_matrix_bsdf {
404 >
405 >        # Run rfluxmtx processes to compute each side
406 >        do_matrix_dir(0) if ( $doback );
407 >        do_matrix_dir(1) if ( $doforw );
408 >
409 > }       # end of sub do_matrix_bsdf()
410 >
411 > # Call rfluxmtx and process tensor tree BSDF for the given direction
412 > sub do_matrix_dir {
413 >        my $forw = shift;
414 >        my $cmd;
415 >        my $sender = ($bsender,$fsender)[$forw];
416 >        $cmd = "$rfluxmtx -fd $sender $receivers -i $octree";
417 >        # print STDERR "Starting: $cmd\n";
418 >        system $cmd;
419 >        die "Failure running rfluxmtx" if ( $? );
420 >        matrix_out($forw);
421 > }       # end of do_matrix_dir()
422 >
423 > sub matrix_out {
424 >        my $forw = shift;
425 >        my ($refldat,$transdat);
426 >        if ( $forw ) {
427 >                $transdat = $facedat;
428 >                $refldat = $behinddat;
429 >        } else {
430 >                $transdat = $behinddat;
431 >                $refldat = $facedat;
432 >        }
433 >        # Output transmission
434 >        my $ttyp = ("tb","tf")[$forw];
435 >        matrix_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
436 >        if ( $docolor ) {
437 >                matrix_comp($ttyp, "CIE-X", $transdat, ($tbx,$tfx)[$forw]);
438 >                matrix_comp($ttyp, "CIE-Z", $transdat, ($tbz,$tfz)[$forw]);
439 >        }
440 >        # Output reflection
441 >        my $rtyp = ("rb","rf")[$forw];
442 >        matrix_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
443 >        if ( $docolor ) {
444 >                matrix_comp($rtyp, "CIE-X", $refldat, ($rbx,$rfx)[$forw]);
445 >                matrix_comp($rtyp, "CIE-Z", $refldat, ($rbz,$rfz)[$forw]);
446 >        }
447 > }       # end of matrix_out()
448 >
449 > # Transpose matrix component data and save to file
450 > sub matrix_comp {
451 >        my $typ = shift;
452 >        my $spec = shift;
453 >        my $src = shift;
454 >        my $dest = shift;
455 >        my $cmd = "rmtxop -fa -t";
456 >        if ("$spec" eq "Visible") {
457 >                $cmd .= " -c 0.2651 0.6701 0.0648";
458 >        } elsif ("$spec" eq "CIE-X") {
459 >                $cmd .= " -c 0.5141 0.3239 0.1620";
460 >        } elsif ("$spec" eq "CIE-Z") {
461 >                $cmd .= " -c 0.0241 0.1229 0.8530";
462 >        }
463 >        $cmd .= " $src | rcollate -ho -oc 145";
464 >        # print STDERR "Running: $cmd\n";
465 >        system "$cmd > $dest";
466 >        die "Failure running rttree_reduce" if ( $? );
467 >        if ( "$spec" ne "$curspec" ) {
468 >                $wrapper .= " -s $spec";
469 >                $curspec = $spec;
470 >        }
471 >        $wrapper .= " -$typ $dest";
472 > }       # end of matrix_comp()
473 >
474 > #------------- End of do_matrix_bsdf() & subroutines --------------#

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines