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.14 by greg, Sat Apr 16 01:13:22 2011 UTC vs.
Revision 2.53 by greg, Wed Mar 25 19:18:28 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 = mkdtemp("/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 +                shift @ARGV;
108 +        } elsif ("$ARGV[0]" =~ /^-t[34]$/) {
109 +                $tensortree = substr($ARGV[0], 2, 1);
110 +                $ttlog2 = $ARGV[1];
111 +                shift @ARGV;
112 +        } elsif ("$ARGV[0]" eq "-f") {
113 +                $wrapper .= " -f \"$ARGV[1]\"";
114 +                shift @ARGV;
115 +        } elsif ("$ARGV[0]" eq "-W") {
116 +                $wrapper .= " -W";
117          } elsif ("$ARGV[0]" eq "-c") {
118                  $nsamp = $ARGV[1];
119                  shift @ARGV;
# Line 52 | Line 132 | while ($#ARGV >= 0) {
132          shift @ARGV;
133   }
134   # Check that we're actually being asked to do something
135 < die "Must have at least one of +forward or +backward" if (!$doforw && !$doback);
135 > die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback);
136 > # Issue warning for unhandled reciprocity case
137 > print STDERR "Warning: recommend both +forward and +backward with -t3\n" if
138 >                ($tensortree==3 && !($doforw && $doback));
139 > $wrapper .= $tensortree ? " -a t$tensortree" : " -a kf -c";
140 > $wrapper .= " -u $gunit";
141   # Get scene description and dimensions
57 my $radscn = "$td/device.rad";
58 my $mgfscn = "$td/device.mgf";
59 my $octree = "$td/device.oct";
142   if ( $mgfin ) {
143 <        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";
143 >        system "mgf2rad @ARGV > $radscn";
144          die "Could not load MGF input\n" if ( $? );
63        system "mgf2rad $mgfscn > $radscn";
145   } else {
146          system "xform -e @ARGV > $radscn";
147          die "Could not load Radiance input\n" if ( $? );
67        system "rad2mgf $radscn > $mgfscn" if ( $geout );
148   }
149   if ($#dim != 5) {
150          @dim = split ' ', `getbbox -h $radscn`;
151   }
152   print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5);
153 < # Add receiver surfaces (rectangular)
154 < my $fmodnm="receiver_face";
75 < my $bmodnm="receiver_behind";
76 < open(RADSCN, ">> $radscn");
77 < print RADSCN "void glow $fmodnm\n0\n0\n4 1 1 1 0\n\n";
78 < print RADSCN "$fmodnm source f_receiver\n0\n0\n4 0 0 1 180\n";
79 < print RADSCN "void glow $bmodnm\n0\n0\n4 1 1 1 0\n\n";
80 < print RADSCN "$bmodnm source b_receiver\n0\n0\n4 0 0 -1 180\n";
81 < close RADSCN;
153 > $wrapper .= ' -f "t=' . ($dim[5] - $dim[4]) . ';w=' . ($dim[1] - $dim[0]) .
154 >                ';h=' . ($dim[3] - $dim[2]) . '"';
155   # Generate octree
156   system "oconv -w $radscn > $octree";
157   die "Could not compile scene\n" if ( $? );
158 < # Set up sampling of interior portal
86 < # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms
87 < my $tcal = '
88 < DEGREE : PI/180;
89 < sq(x) : x*x;
90 < Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);
91 < Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
92 < Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0);
93 < Kmax : Kaccum(Knaz(0));
94 < Kfindrow(r, rem) : if(rem-Knaz(r)+.5, Kfindrow(r+1, rem-Knaz(r)), r);
95 < Krow = if(Kbin-(Kmax-.5), 0, Kfindrow(1, Kbin));
96 < Kcol = Kbin - Kaccum(Krow-1);
97 < Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow);
98 < Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1));
99 < sin_kpol = sin(Kpol);
100 < Dx = cos(Kazi)*sin_kpol;
101 < Dy = sin(Kazi)*sin_kpol;
102 < Dz = sqrt(1 - sin_kpol*sin_kpol);
103 < KprojOmega = PI * if(Kbin-.5,
104 <        (sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow),
105 <        1 - sq(cos(Kpola(1)*DEGREE)));
106 < ';
107 < # Compute Klems bin from exiting ray direction (forward or backward)
108 < my $kcal = '
109 < DEGREE : PI/180;
110 < abs(x) : if(x, x, -x);
111 < Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x)));
112 < posangle(a) : if(-a, a + 2*PI, a);
113 < Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x));
114 < kpola(r) : select(r, 5, 15, 25, 35, 45, 55, 65, 75, 90);
115 < knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
116 < kaccum(r) : if(r-.5, knaz(r) + kaccum(r-1), 0);
117 < kfindrow(r, pol) : if(r-kpola(0)+.5, r,
118 <                if(pol-kpola(r), kfindrow(r+1, pol), r) );
119 < kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0);
120 < kbin2(pol,azi) = select(kfindrow(1, pol),
121 <                kazn(azi,360/knaz(1)),
122 <                kaccum(1) + kazn(azi,360/knaz(2)),
123 <                kaccum(2) + kazn(azi,360/knaz(3)),
124 <                kaccum(3) + kazn(azi,360/knaz(4)),
125 <                kaccum(4) + kazn(azi,360/knaz(5)),
126 <                kaccum(5) + kazn(azi,360/knaz(6)),
127 <                kaccum(6) + kazn(azi,360/knaz(7)),
128 <                kaccum(7) + kazn(azi,360/knaz(8)),
129 <                kaccum(8) + kazn(azi,360/knaz(9))
130 <        );
131 < kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx));
132 < ';
133 < my $ndiv = 145;
134 < my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5);
135 < my $ny = int($nsamp/$nx + .5);
136 < $nsamp = $nx * $ny;
137 < # Compute scattering data using rtcontrib
138 < my @tfarr;
139 < my @rfarr;
140 < my @tbarr;
141 < my @rbarr;
142 < my $cmd;
143 < my $rtcmd = "rtcontrib -h -ff -fo -n $nproc -c $nsamp " .
144 <        "-e '$kcal' -b kbin -bn $ndiv " .
145 <        "-o '$td/%s.flt' -m $fmodnm -m $bmodnm $rtargs $octree";
146 < my $rccmd = "rcalc -e '$tcal' " .
147 <        "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " .
148 <        q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega'};
149 < if ( $doforw ) {
150 < $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
151 <        "-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
152 <        "-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
153 <        "-e 'zp:$dim[4]' " .
154 <        q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } .
155 <        q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } .
156 <        "| $rtcmd";
157 < system "$cmd" || die "Failure running: $cmd\n";
158 < @tfarr = `$rccmd $td/$fmodnm.flt`;
159 < die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
160 < @rfarr = `$rccmd $td/$bmodnm.flt`;
161 < die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
162 < }
163 < if ( $doback ) {
164 < $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
165 <        "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
166 <        "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
167 <        "-e 'zp:$dim[5]' " .
168 <        q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } .
169 <        q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } .
170 <        "| $rtcmd";
171 < system "$cmd" || die "Failure running: $cmd\n";
172 < @tbarr = `$rccmd $td/$bmodnm.flt`;
173 < die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
174 < @rbarr = `$rccmd $td/$fmodnm.flt`;
175 < die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
176 < }
177 < # Output XML prologue
178 < print
179 < '<?xml version="1.0" encoding="UTF-8"?>
180 < <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">
181 <        <WindowElementType>System</WindowElementType>
182 <        <Optical>
183 <                <Layer>
184 <                <Material>
185 <                        <Name>Name</Name>
186 <                        <Manufacturer>Manufacturer</Manufacturer>
187 < ';
188 < printf "\t\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4];
189 < printf "\t\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0];
190 < printf "\t\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2];
191 < print "\t\t\t<DeviceType>Integral</DeviceType>\n";
192 < # Output MGF description if requested
158 > # Add MGF description if requested
159   if ( $geout ) {
160 <        print "\t\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n";
161 <        printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
196 <        open(MGFSCN, "< $mgfscn");
197 <        while (<MGFSCN>) { print $_; }
160 >        open(MGFSCN, "> $mgfscn");
161 >        printf MGFSCN "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
162          close MGFSCN;
163 <        print "xf\n";
164 <        print "\t\t\t</Geometry>\n";
165 < }
166 < print '                 </Material>
203 <                <DataDefinition>
204 <                        <IncidentDataStructure>Columns</IncidentDataStructure>
205 <                        <AngleBasis>
206 <                        <AngleBasisName>LBNL/Klems Full</AngleBasisName>
207 <                                <AngleBasisBlock>
208 <                                <Theta>0</Theta>
209 <                                <nPhis>1</nPhis>
210 <                                <ThetaBounds>
211 <                                <LowerTheta>0</LowerTheta>
212 <                                <UpperTheta>5</UpperTheta>
213 <                                </ThetaBounds>
214 <                                </AngleBasisBlock>
215 <                                <AngleBasisBlock>
216 <                                <Theta>10</Theta>
217 <                                <nPhis>8</nPhis>
218 <                                <ThetaBounds>
219 <                                        <LowerTheta>5</LowerTheta>
220 <                                        <UpperTheta>15</UpperTheta>
221 <                                </ThetaBounds>
222 <                                </AngleBasisBlock>
223 <                                <AngleBasisBlock>
224 <                                <Theta>20</Theta>
225 <                                <nPhis>16</nPhis>
226 <                                <ThetaBounds>
227 <                                        <LowerTheta>15</LowerTheta>
228 <                                        <UpperTheta>25</UpperTheta>
229 <                                </ThetaBounds>
230 <                                </AngleBasisBlock>
231 <                                <AngleBasisBlock>
232 <                                <Theta>30</Theta>
233 <                                <nPhis>20</nPhis>
234 <                                <ThetaBounds>
235 <                                        <LowerTheta>25</LowerTheta>
236 <                                        <UpperTheta>35</UpperTheta>
237 <                                </ThetaBounds>
238 <                                </AngleBasisBlock>
239 <                                <AngleBasisBlock>
240 <                                <Theta>40</Theta>
241 <                                <nPhis>24</nPhis>
242 <                                <ThetaBounds>
243 <                                        <LowerTheta>35</LowerTheta>
244 <                                        <UpperTheta>45</UpperTheta>
245 <                                </ThetaBounds>
246 <                                </AngleBasisBlock>
247 <                                <AngleBasisBlock>
248 <                                <Theta>50</Theta>
249 <                                <nPhis>24</nPhis>
250 <                                <ThetaBounds>
251 <                                        <LowerTheta>45</LowerTheta>
252 <                                        <UpperTheta>55</UpperTheta>
253 <                                </ThetaBounds>
254 <                                </AngleBasisBlock>
255 <                                <AngleBasisBlock>
256 <                                <Theta>60</Theta>
257 <                                <nPhis>24</nPhis>
258 <                                <ThetaBounds>
259 <                                        <LowerTheta>55</LowerTheta>
260 <                                        <UpperTheta>65</UpperTheta>
261 <                                </ThetaBounds>
262 <                                </AngleBasisBlock>
263 <                                <AngleBasisBlock>
264 <                                <Theta>70</Theta>
265 <                                <nPhis>16</nPhis>
266 <                                <ThetaBounds>
267 <                                        <LowerTheta>65</LowerTheta>
268 <                                        <UpperTheta>75</UpperTheta>
269 <                                </ThetaBounds>
270 <                                </AngleBasisBlock>
271 <                                <AngleBasisBlock>
272 <                                <Theta>82.5</Theta>
273 <                                <nPhis>12</nPhis>
274 <                                <ThetaBounds>
275 <                                        <LowerTheta>75</LowerTheta>
276 <                                        <UpperTheta>90</UpperTheta>
277 <                                </ThetaBounds>
278 <                        </AngleBasisBlock>
279 <                </AngleBasis>
280 <        </DataDefinition>
281 < ';
282 < if ( $doforw ) {
283 < print '         <WavelengthData>
284 <                <LayerNumber>System</LayerNumber>
285 <                <Wavelength unit="Integral">Visible</Wavelength>
286 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
287 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
288 <                <WavelengthDataBlock>
289 <                        <WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
290 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
291 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
292 <                        <ScatteringDataType>BTDF</ScatteringDataType>
293 <                        <ScatteringData>
294 < ';
295 < # Output front transmission (transposed order)
296 < for (my $od = 0; $od < $ndiv; $od++) {
297 <        for (my $id = 0; $id < $ndiv; $id++) {
298 <                print $tfarr[$ndiv*$id + $od];
163 >        if ( $mgfin ) {
164 >                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};
165 >        } else {
166 >                system "rad2mgf $radscn >> $mgfscn";
167          }
168 <        print "\n";
168 >        open(MGFSCN, ">> $mgfscn");
169 >        print MGFSCN "xf\n";
170 >        close MGFSCN;
171 >        $wrapper .= " -g $mgfscn";
172   }
173 < print
174 < '               </ScatteringData>
175 <        </WavelengthDataBlock>
176 <        </WavelengthData>
177 <        <WavelengthData>
178 <                <LayerNumber>System</LayerNumber>
179 <                <Wavelength unit="Integral">Visible</Wavelength>
180 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
181 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
182 <                <WavelengthDataBlock>
183 <                        <WavelengthDataDirection>Reflection Front</WavelengthDataDirection>
184 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
185 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
186 <                        <ScatteringDataType>BRDF</ScatteringDataType>
187 <                        <ScatteringData>
188 < ';
189 < # Output front reflection (transposed order)
190 < for (my $od = 0; $od < $ndiv; $od++) {
191 <        for (my $id = 0; $id < $ndiv; $id++) {
192 <                print $rfarr[$ndiv*$id + $od];
193 <        }
194 <        print "\n";
173 > # Create receiver & sender surfaces (rectangular)
174 > my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1);
175 > my $ny = int($nsamp/$nx + 1);
176 > $nsamp = $nx * $ny;
177 > my $ns = 2**$ttlog2;
178 > open(RADSCN, "> $receivers");
179 > print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=sc$ns\n" : "h=kf\n");
180 > print RADSCN '#@rfluxmtx ' . "u=Y o=$facedat\n\n";
181 > print RADSCN "void glow receiver_face\n0\n0\n4 1 1 1 0\n\n";
182 > print RADSCN "receiver_face source f_receiver\n0\n0\n4 0 0 1 180\n\n";
183 > print RADSCN '#@rfluxmtx ' . "u=Y o=$behinddat\n\n";
184 > print RADSCN "void glow receiver_behind\n0\n0\n4 1 1 1 0\n\n";
185 > print RADSCN "receiver_behind source b_receiver\n0\n0\n4 0 0 -1 180\n";
186 > close RADSCN;
187 > # Finish rfluxmtx command and prepare sender surfaces
188 > $rfluxmtx .= " -n $nproc -c $nsamp";
189 > if ( $tensortree != 3 ) {       # Isotropic tensor tree is exception
190 >        open (RADSCN, "> $fsender");
191 >        print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n");
192 >        print RADSCN "void polygon fwd_sender\n0\n0\n12\n";
193 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[4];
194 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[4];
195 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[4];
196 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[4];
197 >        close RADSCN;
198 >        open (RADSCN, "> $bsender");
199 >        print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n");
200 >        print RADSCN "void polygon fwd_sender\n0\n0\n12\n";
201 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[5];
202 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[5];
203 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[5];
204 >        printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[5];
205 >        close RADSCN;
206   }
207 < print
208 < '               </ScatteringData>
209 <        </WavelengthDataBlock>
210 <        </WavelengthData>
211 < ';
207 > # Calculate CIE (u',v') from Radiance RGB:
208 > my $CIEuv =     'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' .
209 >                'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' .
210 >                'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' .
211 >                'den=Xi+15*Yi+3*Zi;' .
212 >                'uprime=4*Xi/den;vprime=9*Yi/den;' ;
213 > # Create data segments (all the work happens here)
214 > if ( $tensortree ) {
215 >        do_tree_bsdf();
216 > } else {
217 >        do_matrix_bsdf();
218   }
219 < if ( $doback ) {
220 < print '         <WavelengthData>
221 <                <LayerNumber>System</LayerNumber>
222 <                <Wavelength unit="Integral">Visible</Wavelength>
223 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
224 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
225 <                <WavelengthDataBlock>
226 <                        <WavelengthDataDirection>Transmission Back</WavelengthDataDirection>
227 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
228 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
229 <                        <ScatteringDataType>BTDF</ScatteringDataType>
230 <                        <ScatteringData>
231 < ';
232 < # Output back transmission (transposed order)
233 < for (my $od = 0; $od < $ndiv; $od++) {
234 <        for (my $id = 0; $id < $ndiv; $id++) {
235 <                print $tbarr[$ndiv*$id + $od];
219 > # Output XML
220 > my $old_fh = select(STDOUT);
221 > $| = 1;
222 > select($old_fh);
223 > print "<!-- Created by: genBSDF @savedARGV -->\n";
224 > # print STDERR "Running: $wrapper\n";
225 > system $wrapper;
226 > die "Could not wrap BSDF data\n" if ( $? );
227 > # Clean up temporary files and exit
228 > exec $rmtmp;
229 >
230 > #-------------- End of main program segment --------------#
231 >
232 > #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
233 > sub do_tree_bsdf {
234 >
235 >        # Run rfluxmtx processes to compute each side
236 >        do_ttree_dir(0) if ( $doback );
237 >        do_ttree_dir(1) if ( $doforw );
238 >
239 > }       # end of sub do_tree_bsdf()
240 >
241 > # Call rfluxmtx and process tensor tree BSDF for the given direction
242 > sub do_ttree_dir {
243 >        my $forw = shift;
244 >        my $cmd;
245 >        if ( $tensortree == 3 ) {
246 >                # Isotropic BSDF
247 >                my $ns2 = $ns / 2;
248 >                if ($windoz) {
249 >                        $cmd = "cnt $ns2 $ny $nx " .
250 >                                qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
251 >                                qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
252 >                                qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
253 >                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
254 >                                qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
255 >                                qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
256 >                                qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
257 >                                qq{-e "\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz" } .
258 >                                qq{-e "\$4=Dx;\$5=Dy;\$6=myDz" } .
259 >                                "| $rfluxmtx -fa -y $ns2 - $receivers -i $octree";
260 >                } else {
261 >                        $cmd = "cnt $ns2 $ny $nx " .
262 >                                qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
263 >                                qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
264 >                                qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
265 >                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
266 >                                qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
267 >                                qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
268 >                                qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
269 >                                qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } .
270 >                                qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } .
271 >                                "| $rfluxmtx -h -ff -y $ns2 - $receivers -i $octree";
272 >                }
273 >        } else {
274 >                # Anisotropic BSDF
275 >                my $sender = ($bsender,$fsender)[$forw];
276 >                if ($windoz) {
277 >                        $cmd = "$rfluxmtx -fa $sender $receivers -i $octree";
278 >                } else {
279 >                        $cmd = "$rfluxmtx -h -ff $sender $receivers -i $octree";
280 >                }
281          }
282 <        print "\n";
283 < }
284 < print
285 < '               </ScatteringData>
286 <        </WavelengthDataBlock>
287 <        </WavelengthData>
288 <        <WavelengthData>
289 <                <LayerNumber>System</LayerNumber>
290 <                <Wavelength unit="Integral">Visible</Wavelength>
291 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
292 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
293 <                <WavelengthDataBlock>
294 <                        <WavelengthDataDirection>Reflection Back</WavelengthDataDirection>
295 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
296 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
297 <                        <ScatteringDataType>BRDF</ScatteringDataType>
365 <                        <ScatteringData>
366 < ';
367 < # Output back reflection (transposed order)
368 < for (my $od = 0; $od < $ndiv; $od++) {
369 <        for (my $id = 0; $id < $ndiv; $id++) {
370 <                print $rbarr[$ndiv*$id + $od];
282 >        # print STDERR "Starting: $cmd\n";
283 >        system $cmd;
284 >        die "Failure running rfluxmtx" if ( $? );
285 >        ttree_out($forw);
286 > }       # end of do_ttree_dir()
287 >
288 > # Simplify and store tensor tree results
289 > sub ttree_out {
290 >        my $forw = shift;
291 >        my ($refldat,$transdat);
292 >        if ( $forw ) {
293 >                $transdat = $facedat;
294 >                $refldat = $behinddat;
295 >        } else {
296 >                $transdat = $behinddat;
297 >                $refldat = $facedat;
298          }
299 <        print "\n";
300 < }
301 < print
302 < '               </ScatteringData>
303 <        </WavelengthDataBlock>
304 <        </WavelengthData>
305 < ';
306 < }
307 < # Output XML epilogue
308 < print '</Layer>
309 < </Optical>
310 < </WindowElement>
311 < ';
312 < # Clean up temporary files
313 < system "rm -rf $td";
299 >        # Only output one transmitted anisotropic distribution, preferring backwards
300 >        if ( !$forw || !$doback || $tensortree==3 ) {
301 >                my $ttyp = ("tb","tf")[$forw];
302 >                ttree_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
303 >                if ( $docolor ) {
304 >                        ttree_comp($ttyp, "CIE-u", $transdat, ($tbx,$tfx)[$forw]);
305 >                        ttree_comp($ttyp, "CIE-v", $transdat, ($tbz,$tfz)[$forw]);
306 >                }
307 >        }
308 >        # Output reflection
309 >        my $rtyp = ("rb","rf")[$forw];
310 >        ttree_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
311 >        if ( $docolor ) {
312 >                ttree_comp($rtyp, "CIE-u", $refldat, ($rbx,$rfx)[$forw]);
313 >                ttree_comp($rtyp, "CIE-v", $refldat, ($rbz,$rfz)[$forw]);
314 >        }
315 > }       # end of ttree_out()
316 >
317 > # Call rttree_reduce on the given component
318 > sub ttree_comp {
319 >        my $typ = shift;
320 >        my $spec = shift;
321 >        my $src = shift;
322 >        my $dest = shift;
323 >        my $cmd;
324 >        if ($windoz) {
325 >                if ("$spec" eq "Visible") {
326 >                        $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
327 >                                q{-e "Ri=$1;Gi=$2;Bi=$3" } .
328 >                                qq{-e "$CIEuv" } .
329 >                                q{-e "$1=Yi/Omega"};
330 >                } elsif ("$spec" eq "CIE-u") {
331 >                        $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
332 >                                q{-e "Ri=$1;Gi=$2;Bi=$3" } .
333 >                                qq{-e "$CIEuv" } .
334 >                                q{-e "$1=uprime/Omega"};
335 >                } elsif ("$spec" eq "CIE-v") {
336 >                        $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
337 >                                q{-e "Ri=$1;Gi=$2;Bi=$3" } .
338 >                                qq{-e "$CIEuv" } .
339 >                                q{-e "$1=vprime/Omega"};
340 >                }
341 >        } else {
342 >                if ("$spec" eq "Visible") {
343 >                        $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
344 >                                q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
345 >                                "-e '$CIEuv' " .
346 >                                q{-e '$1=Yi/Omega'};
347 >                } elsif ("$spec" eq "CIE-u") {
348 >                        $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
349 >                                q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
350 >                                "-e '$CIEuv' " .
351 >                                q{-e '$1=uprime/Omega'};
352 >                } elsif ("$spec" eq "CIE-v") {
353 >                        $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
354 >                                q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
355 >                                "-e '$CIEuv' " .
356 >                                q{-e '$1=vprime/Omega'};
357 >                }
358 >        }
359 >        if ($pctcull >= 0) {
360 >                my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : "";
361 >                my $pcull = ("$spec" eq "Visible") ? $pctcull :
362 >                                                     (100 - (100-$pctcull)/3) ;
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.265 0.670 0.065";
458 >        } elsif ("$spec" eq "CIE-X") {
459 >                $cmd .= " -c 0.514 0.324 0.162";
460 >        } elsif ("$spec" eq "CIE-Z") {
461 >                $cmd .= " -c 0.024 0.123 0.853";
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