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.27 by greg, Thu Oct 27 16:35:54 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][-t{3|4} Nlog2][-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;
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;
20 my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6";
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 = 95;
81 < my $gunit = "Meter";
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]" =~ /^\+/);
# Line 40 | Line 95 | while ($#ARGV >= 0) {
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 70 | Line 133 | while ($#ARGV >= 0) {
133   }
134   # Check that we're actually being asked to do something
135   die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback);
136 < # Name our own persist file?
137 < my $persistfile;
138 < if ( $tensortree && $nproc > 1 && "$rtargs" !~ /-PP /) {
139 <        $persistfile = "$td/pfile.txt";
140 <        $rtargs = "-PP $persistfile $rtargs";
78 < }
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
80 my $radscn = "$td/device.rad";
81 my $mgfscn = "$td/device.mgf";
82 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 ( $? );
86        system "mgf2rad $mgfscn > $radscn";
145   } else {
146          system "xform -e @ARGV > $radscn";
147          die "Could not load Radiance input\n" if ( $? );
90        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";
98 < my $bmodnm="receiver_behind";
99 < open(RADSCN, ">> $radscn");
100 < print RADSCN "void glow $fmodnm\n0\n0\n4 1 1 1 0\n\n";
101 < print RADSCN "$fmodnm source f_receiver\n0\n0\n4 0 0 1 180\n";
102 < print RADSCN "void glow $bmodnm\n0\n0\n4 1 1 1 0\n\n";
103 < print RADSCN "$bmodnm source b_receiver\n0\n0\n4 0 0 -1 180\n";
104 < 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 < # Output XML prologue
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 < ';
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=\"$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
158 > # Add MGF description if requested
159   if ( $geout ) {
160 <        print "\t\t<Geometry format=\"MGF\" unit=\"$gunit\">\n";
161 <        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 $_; }
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</Geometry>\n";
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 >        open(MGFSCN, ">> $mgfscn");
169 >        print MGFSCN "xf\n";
170 >        close MGFSCN;
171 >        $wrapper .= " -g $mgfscn";
172   }
173 < print " </Material>\n";
174 < # Set up surface sampling
175 < my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5);
139 < my $ny = int($nsamp/$nx + .5);
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 < my (@pdiv, $disk2sq, $sq2disk, $tcal, $kcal);
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 > # 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 < # Output XML epilogue
220 < print
221 < '</Layer>
222 < </Optical>
223 < </WindowElement>
224 < ';
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("rm -rf $td");
228 > exec $rmtmp;
229  
230   #-------------- End of main program segment --------------#
231  
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
232   #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
233   sub do_tree_bsdf {
234 < # Get sampling rate and subdivide task
235 < my $ns2 = $ns;
236 < $ns2 /= 2 if ( $tensortree == 3 );
237 < my $nsplice = $nproc;
238 < $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;
187 < }
188 < die "Script error 1" if ($pdiv[-1] != $ns2);
189 < # Shirley-Chiu mapping from unit square to disk
190 < $sq2disk = '
191 < in_square_a = 2*in_square_x - 1;
192 < in_square_b = 2*in_square_y - 1;
193 < in_square_rgn = if(in_square_a + in_square_b,
194 <                        if(in_square_a - in_square_b, 1, 2),
195 <                        if(in_square_b - in_square_a, 3, 4));
196 < out_disk_r = .999995*select(in_square_rgn, in_square_a, in_square_b,
197 <                        -in_square_a, -in_square_b);
198 < out_disk_phi = PI/4 * select(in_square_rgn,
199 <                                in_square_b/in_square_a,
200 <                                2 - in_square_a/in_square_b,
201 <                                4 + in_square_b/in_square_a,
202 <                                if(in_square_b*in_square_b,
203 <                                        6 - in_square_a/in_square_b, 0));
204 < Dx = out_disk_r*cos(out_disk_phi);
205 < Dy = out_disk_r*sin(out_disk_phi);
206 < Dz = sqrt(1 - out_disk_r*out_disk_r);
207 < ';
208 < # Shirley-Chiu mapping from unit disk to square
209 < $disk2sq = '
210 < norm_radians(p) : if(-p - PI/4, p + 2*PI, p);
211 < in_disk_r = .999995*sqrt(Dx*Dx + Dy*Dy);
212 < in_disk_phi = norm_radians(atan2(Dy, Dx));
213 < in_disk_rgn = floor((in_disk_phi + PI/4)/(PI/2)) + 1;
214 < out_square_a = select(in_disk_rgn,
215 <                        in_disk_r,
216 <                        (PI/2 - in_disk_phi)*in_disk_r/(PI/4),
217 <                        -in_disk_r,
218 <                        (in_disk_phi - 3*PI/2)*in_disk_r/(PI/4));
219 < out_square_b = select(in_disk_rgn,
220 <                        in_disk_phi*in_disk_r/(PI/4),
221 <                        in_disk_r,
222 <                        (PI - in_disk_phi)*in_disk_r/(PI/4),
223 <                        -in_disk_r);
224 < out_square_x = (out_square_a + 1)/2;
225 < out_square_y = (out_square_b + 1)/2;
226 < ';
227 < # Announce ourselves in XML output
228 < 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 $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 $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 < }
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 < # Run rtcontrib process in background to generate tensor tree samples
242 < sub bg_tree_rtcontrib {
271 <        my $pid = fork();
272 <        die "Cannot fork new process" unless defined $pid;
273 <        if ($pid > 0) { return $pid; }
241 > # Call rfluxmtx and process tensor tree BSDF for the given direction
242 > sub do_ttree_dir {
243          my $forw = shift;
244 <        my $pn = shift;
276 <        my $pbeg = $pdiv[$pn];
277 <        my $plen = $pdiv[$pn+1] - $pbeg;
278 <        my $matargs = "-m $bmodnm";
279 <        if ( !$forw || !$doback ) { $matargs .= " -m $fmodnm"; }
280 <        my $cmd = "rtcontrib $rtargs -h -ff -fo -c $nsamp " .
281 <                "-e '$disk2sq' -bn '$ns*$ns' " .
282 <                "-b '$ns*floor(out_square_x*$ns)+floor(out_square_y*$ns)' " .
283 <                "-o $td/%s_" . sprintf("%03d", $pn) . ".flt $matargs $octree";
244 >        my $cmd;
245          if ( $tensortree == 3 ) {
246                  # Isotropic BSDF
247 <                $cmd = "cnt $plen $ny $nx " .
248 <                        "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " .
249 <                        "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " .
250 <                        "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " .
251 <                        "-e 'Dx=1-2*($pbeg+\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' " .
252 <                        "-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
253 <                        "-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
254 <                        "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " .
255 <                        "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " .
256 <                        "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " .
257 <                        "| $cmd";
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 <                # Sample area vertically to improve load balance, since
276 <                # shading systems usually have bilateral symmetry (L-R)
277 <                $cmd = "cnt $plen $ns $ny $nx " .
278 <                        "| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " .
279 <                        "-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " .
280 <                        "-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " .
305 <                        "-e 'r4=rand(($pn-2.3857833)*recno-964.72738)' " .
306 <                        "-e 'in_square_x=($pbeg+\$1+r1)/$ns' " .
307 <                        "-e 'in_square_y=(\$2+r2)/$ns' -e '$sq2disk' " .
308 <                        "-e 'xp=(\$4+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
309 <                        "-e 'yp=(\$3+r4)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
310 <                        "-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " .
311 <                        "-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " .
312 <                        "-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " .
313 <                        "| $cmd";
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 STDERR "Starting: $cmd\n";
283 <        exec($cmd);             # no return; status report to parent via wait
284 <        die "Cannot exec: $cmd\n";
285 < }       # end of bg_tree_rtcontrib()
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 output tensor tree results
288 > # Simplify and store tensor tree results
289   sub ttree_out {
290          my $forw = shift;
291 <        my $side = ("Back","Front")[$forw];
292 < # Only output one transmitted distribution, preferring backwards
293 < if ( !$forw || !$doback ) {
294 < print
295 < '       <WavelengthData>
296 <                <LayerNumber>System</LayerNumber>
297 <                <Wavelength unit="Integral">Visible</Wavelength>
298 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
299 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
300 <                <WavelengthDataBlock>
301 <                        <WavelengthDataDirection>Transmission</WavelengthDataDirection>
302 <                        <AngleBasis>LBNL/Shirley-Chiu</AngleBasis>
303 <                        <ScatteringDataType>BTDF</ScatteringDataType>
304 <                        <ScatteringData>
305 < ';
306 < system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
307 <        q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } .
308 <        "$td/" . ($bmodnm,$fmodnm)[$forw] . "_???.flt " .
309 <        "| rttree_reduce -h -ff -t $pctcull -r $tensortree -g $ttlog2";
310 < die "Failure running rttree_reduce" if ( $? );
311 < print
312 < '                       </ScatteringData>
313 <                </WavelengthDataBlock>
314 <        </WavelengthData>
347 < ';
348 < }
349 < # Output reflection
350 < print
351 < '       <WavelengthData>
352 <                <LayerNumber>System</LayerNumber>
353 <                <Wavelength unit="Integral">Visible</Wavelength>
354 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
355 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
356 <                <WavelengthDataBlock>
357 < ';
358 < print "\t\t\t<WavelengthDataDirection>Reflection $side</WavelengthDataDirection>\n";
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 -t $pctcull -r $tensortree -g $ttlog2";
368 < die "Failure running rttree_reduce" if ( $? );
369 < print
370 < '                       </ScatteringData>
371 <                </WavelengthDataBlock>
372 <        </WavelengthData>
373 < ';
291 >        my ($refldat,$transdat);
292 >        if ( $forw ) {
293 >                $transdat = $facedat;
294 >                $refldat = $behinddat;
295 >        } else {
296 >                $transdat = $behinddat;
297 >                $refldat = $facedat;
298 >        }
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 < # Set up sampling of portal
405 < # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms
406 < $tcal = '
407 < DEGREE : PI/180;
408 < sq(x) : x*x;
409 < Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);
410 < Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
411 < Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0);
412 < Kmax : Kaccum(Knaz(0));
413 < Kfindrow(r, rem) : if(rem-Knaz(r)+.5, Kfindrow(r+1, rem-Knaz(r)), r);
414 < Krow = if(Kbin-(Kmax-.5), 0, Kfindrow(1, Kbin));
415 < Kcol = Kbin - Kaccum(Krow-1);
416 < Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow);
417 < Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1));
418 < sin_kpol = sin(Kpol);
419 < Dx = cos(Kazi)*sin_kpol;
420 < Dy = sin(Kazi)*sin_kpol;
421 < Dz = sqrt(1 - sin_kpol*sin_kpol);
422 < KprojOmega = PI * if(Kbin-.5,
423 <        (sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow),
424 <        1 - sq(cos(Kpola(1)*DEGREE)));
425 < ';
426 < # Compute Klems bin from exiting ray direction (forward or backward)
427 < $kcal = '
428 < DEGREE : PI/180;
429 < abs(x) : if(x, x, -x);
430 < Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x)));
431 < posangle(a) : if(-a, a + 2*PI, a);
408 < Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x));
409 < kpola(r) : select(r, 5, 15, 25, 35, 45, 55, 65, 75, 90);
410 < knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
411 < kaccum(r) : if(r-.5, knaz(r) + kaccum(r-1), 0);
412 < kfindrow(r, pol) : if(r-kpola(0)+.5, r,
413 <                if(pol-kpola(r), kfindrow(r+1, pol), r) );
414 < kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0);
415 < kbin2(pol,azi) = select(kfindrow(1, pol),
416 <                kazn(azi,360/knaz(1)),
417 <                kaccum(1) + kazn(azi,360/knaz(2)),
418 <                kaccum(2) + kazn(azi,360/knaz(3)),
419 <                kaccum(3) + kazn(azi,360/knaz(4)),
420 <                kaccum(4) + kazn(azi,360/knaz(5)),
421 <                kaccum(5) + kazn(azi,360/knaz(6)),
422 <                kaccum(6) + kazn(azi,360/knaz(7)),
423 <                kaccum(7) + kazn(azi,360/knaz(8)),
424 <                kaccum(8) + kazn(azi,360/knaz(9))
425 <        );
426 < kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx));
427 < ';
428 < my $ndiv = 145;
429 < # Compute scattering data using rtcontrib
430 < my @tfarr;
431 < my @rfarr;
432 < my @tbarr;
433 < my @rbarr;
434 < my $cmd;
435 < my $rtcmd = "rtcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " .
436 <        "-e '$kcal' -b kbin -bn $ndiv " .
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 'oval=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega' } .
441 <        q[-o '${  oval  },'];
442 < if ( $doforw ) {
443 < $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
444 <        "-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
445 <        "-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
446 <        "-e 'zp:$dim[4]' " .
447 <        q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } .
448 <        q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } .
449 <        "| $rtcmd";
450 < system "$cmd" || die "Failure running: $cmd\n";
451 < @tfarr = `$rccmd $td/$fmodnm.flt`;
452 < die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
453 < @rfarr = `$rccmd $td/$bmodnm.flt`;
454 < die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
455 < }
456 < if ( $doback ) {
457 < $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
458 <        "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
459 <        "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
460 <        "-e 'zp:$dim[5]' " .
461 <        q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } .
462 <        q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } .
463 <        "| $rtcmd";
464 < system "$cmd" || die "Failure running: $cmd\n";
465 < @tbarr = `$rccmd $td/$bmodnm.flt`;
466 < die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
467 < @rbarr = `$rccmd $td/$fmodnm.flt`;
468 < die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
469 < }
470 < # Output angle basis
471 < print
472 < '       <DataDefinition>
473 <                <IncidentDataStructure>Columns</IncidentDataStructure>
474 <                <AngleBasis>
475 <                        <AngleBasisName>LBNL/Klems Full</AngleBasisName>
476 <                        <AngleBasisBlock>
477 <                                <Theta>0</Theta>
478 <                                <nPhis>1</nPhis>
479 <                                <ThetaBounds>
480 <                                        <LowerTheta>0</LowerTheta>
481 <                                        <UpperTheta>5</UpperTheta>
482 <                                </ThetaBounds>
483 <                                </AngleBasisBlock>
484 <                                <AngleBasisBlock>
485 <                                <Theta>10</Theta>
486 <                                <nPhis>8</nPhis>
487 <                                <ThetaBounds>
488 <                                        <LowerTheta>5</LowerTheta>
489 <                                        <UpperTheta>15</UpperTheta>
490 <                                </ThetaBounds>
491 <                                </AngleBasisBlock>
492 <                                <AngleBasisBlock>
493 <                                <Theta>20</Theta>
494 <                                <nPhis>16</nPhis>
495 <                                <ThetaBounds>
496 <                                        <LowerTheta>15</LowerTheta>
497 <                                        <UpperTheta>25</UpperTheta>
498 <                                </ThetaBounds>
499 <                                </AngleBasisBlock>
500 <                                <AngleBasisBlock>
501 <                                <Theta>30</Theta>
502 <                                <nPhis>20</nPhis>
503 <                                <ThetaBounds>
504 <                                        <LowerTheta>25</LowerTheta>
505 <                                        <UpperTheta>35</UpperTheta>
506 <                                </ThetaBounds>
507 <                                </AngleBasisBlock>
508 <                                <AngleBasisBlock>
509 <                                <Theta>40</Theta>
510 <                                <nPhis>24</nPhis>
511 <                                <ThetaBounds>
512 <                                        <LowerTheta>35</LowerTheta>
513 <                                        <UpperTheta>45</UpperTheta>
514 <                                </ThetaBounds>
515 <                                </AngleBasisBlock>
516 <                                <AngleBasisBlock>
517 <                                <Theta>50</Theta>
518 <                                <nPhis>24</nPhis>
519 <                                <ThetaBounds>
520 <                                        <LowerTheta>45</LowerTheta>
521 <                                        <UpperTheta>55</UpperTheta>
522 <                                </ThetaBounds>
523 <                                </AngleBasisBlock>
524 <                                <AngleBasisBlock>
525 <                                <Theta>60</Theta>
526 <                                <nPhis>24</nPhis>
527 <                                <ThetaBounds>
528 <                                        <LowerTheta>55</LowerTheta>
529 <                                        <UpperTheta>65</UpperTheta>
530 <                                </ThetaBounds>
531 <                                </AngleBasisBlock>
532 <                                <AngleBasisBlock>
533 <                                <Theta>70</Theta>
534 <                                <nPhis>16</nPhis>
535 <                                <ThetaBounds>
536 <                                        <LowerTheta>65</LowerTheta>
537 <                                        <UpperTheta>75</UpperTheta>
538 <                                </ThetaBounds>
539 <                                </AngleBasisBlock>
540 <                                <AngleBasisBlock>
541 <                                <Theta>82.5</Theta>
542 <                                <nPhis>12</nPhis>
543 <                                <ThetaBounds>
544 <                                        <LowerTheta>75</LowerTheta>
545 <                                        <UpperTheta>90</UpperTheta>
546 <                                </ThetaBounds>
547 <                        </AngleBasisBlock>
548 <                </AngleBasis>
549 <        </DataDefinition>
550 < ';
551 < if ( $doforw ) {
552 < print
553 < '       <WavelengthData>
554 <                <LayerNumber>System</LayerNumber>
555 <                <Wavelength unit="Integral">Visible</Wavelength>
556 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
557 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
558 <                <WavelengthDataBlock>
559 <                        <WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
560 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
561 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
562 <                        <ScatteringDataType>BTDF</ScatteringDataType>
563 <                        <ScatteringData>
564 < ';
565 < # Output front transmission (transposed order)
566 < for (my $od = 0; $od < $ndiv; $od++) {
567 <        for (my $id = 0; $id < $ndiv; $id++) {
568 <                print $tfarr[$ndiv*$id + $od];
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 <        print "\n";
434 < }
435 < print
436 < '                       </ScatteringData>
437 <                </WavelengthDataBlock>
438 <        </WavelengthData>
576 <        <WavelengthData>
577 <                <LayerNumber>System</LayerNumber>
578 <                <Wavelength unit="Integral">Visible</Wavelength>
579 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
580 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
581 <                <WavelengthDataBlock>
582 <                        <WavelengthDataDirection>Reflection Front</WavelengthDataDirection>
583 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
584 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
585 <                        <ScatteringDataType>BRDF</ScatteringDataType>
586 <                        <ScatteringData>
587 < ';
588 < # Output front reflection (transposed order)
589 < for (my $od = 0; $od < $ndiv; $od++) {
590 <        for (my $id = 0; $id < $ndiv; $id++) {
591 <                print $rfarr[$ndiv*$id + $od];
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 <        print "\n";
441 < }
442 < print
443 < '                       </ScatteringData>
444 <                </WavelengthDataBlock>
445 <        </WavelengthData>
599 < ';
600 < }
601 < if ( $doback ) {
602 < print
603 < '       <WavelengthData>
604 <                <LayerNumber>System</LayerNumber>
605 <                <Wavelength unit="Integral">Visible</Wavelength>
606 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
607 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
608 <                <WavelengthDataBlock>
609 <                        <WavelengthDataDirection>Transmission Back</WavelengthDataDirection>
610 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
611 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
612 <                        <ScatteringDataType>BTDF</ScatteringDataType>
613 <                        <ScatteringData>
614 < ';
615 < # Output back transmission (transposed order)
616 < for (my $od = 0; $od < $ndiv; $od++) {
617 <        for (my $id = 0; $id < $ndiv; $id++) {
618 <                print $tbarr[$ndiv*$id + $od];
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 <        print "\n";
448 < }
449 < print
450 < '                       </ScatteringData>
451 <                </WavelengthDataBlock>
452 <        </WavelengthData>
453 <        <WavelengthData>
454 <                <LayerNumber>System</LayerNumber>
455 <                <Wavelength unit="Integral">Visible</Wavelength>
456 <                <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
457 <                <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
458 <                <WavelengthDataBlock>
459 <                        <WavelengthDataDirection>Reflection Back</WavelengthDataDirection>
460 <                        <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
461 <                        <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
635 <                        <ScatteringDataType>BRDF</ScatteringDataType>
636 <                        <ScatteringData>
637 < ';
638 < # Output back reflection (transposed order)
639 < for (my $od = 0; $od < $ndiv; $od++) {
640 <        for (my $id = 0; $id < $ndiv; $id++) {
641 <                print $rbarr[$ndiv*$id + $od];
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 <        print "\n";
464 < }
465 < print
466 < '                       </ScatteringData>
467 <                </WavelengthDataBlock>
468 <        </WavelengthData>
469 < ';
470 < }
471 < }
472 < #------------- End of do_matrix_bsdf() --------------#
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