6 |
|
# G. Ward |
7 |
|
# |
8 |
|
use strict; |
9 |
+ |
use File::Temp qw/ :mktemp /; |
10 |
|
sub userror { |
11 |
< |
print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-dim xmin xmax ymin ymax zmin zmax][{+|-}mgf][{+|-}geom] [input ..]\n"; |
11 |
> |
print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-t{3|4} Nlog2][-r \"ropts\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom] [input ..]\n"; |
12 |
|
exit 1; |
13 |
|
} |
14 |
< |
my $td = `mktemp -d /tmp/genBSDF.XXXXXX`; |
14 |
> |
my $td = mkdtemp("/tmp/genBSDF.XXXXXX"); |
15 |
|
chomp $td; |
16 |
< |
my $nsamp = 1000; |
16 |
> |
my @savedARGV = @ARGV; |
17 |
> |
my $tensortree = 0; |
18 |
> |
my $ttlog2 = 4; |
19 |
> |
my $nsamp = 2000; |
20 |
> |
my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6"; |
21 |
|
my $mgfin = 0; |
22 |
|
my $geout = 1; |
23 |
|
my $nproc = 1; |
24 |
+ |
my $doforw = 0; |
25 |
+ |
my $doback = 1; |
26 |
+ |
my $pctcull = 95; |
27 |
+ |
my $gunit = "Meter"; |
28 |
|
my @dim; |
29 |
|
# Get options |
30 |
|
while ($#ARGV >= 0) { |
31 |
|
if ("$ARGV[0]" =~ /^[-+]m/) { |
32 |
|
$mgfin = ("$ARGV[0]" =~ /^\+/); |
33 |
+ |
} elsif ("$ARGV[0]" eq "-r") { |
34 |
+ |
$rtargs = "$rtargs $ARGV[1]"; |
35 |
+ |
shift @ARGV; |
36 |
|
} elsif ("$ARGV[0]" =~ /^[-+]g/) { |
37 |
|
$geout = ("$ARGV[0]" =~ /^\+/); |
38 |
+ |
$gunit = $ARGV[1]; |
39 |
+ |
if ($gunit !~ /^(?i)(meter|foot|inch|centimeter|millimeter)$/) { |
40 |
+ |
die "Illegal geometry unit '$gunit': must be meter, foot, inch, centimeter, or millimeter\n"; |
41 |
+ |
} |
42 |
+ |
shift @ARGV; |
43 |
+ |
} elsif ("$ARGV[0]" =~ /^[-+]f/) { |
44 |
+ |
$doforw = ("$ARGV[0]" =~ /^\+/); |
45 |
+ |
} elsif ("$ARGV[0]" =~ /^[-+]b/) { |
46 |
+ |
$doback = ("$ARGV[0]" =~ /^\+/); |
47 |
+ |
} elsif ("$ARGV[0]" eq "-t") { |
48 |
+ |
$pctcull = $ARGV[1]; |
49 |
+ |
shift @ARGV; |
50 |
+ |
} elsif ("$ARGV[0]" =~ /^-t[34]$/) { |
51 |
+ |
$tensortree = substr($ARGV[0], 2, 1); |
52 |
+ |
$ttlog2 = $ARGV[1]; |
53 |
+ |
shift @ARGV; |
54 |
|
} elsif ("$ARGV[0]" eq "-c") { |
55 |
|
$nsamp = $ARGV[1]; |
56 |
|
shift @ARGV; |
59 |
|
shift @ARGV; |
60 |
|
} elsif ("$ARGV[0]" =~ /^-d/) { |
61 |
|
userror() if ($#ARGV < 6); |
62 |
< |
@dim = "@ARGV[1..6]"; |
62 |
> |
@dim = @ARGV[1..6]; |
63 |
|
shift @ARGV for (1..6); |
64 |
|
} elsif ("$ARGV[0]" =~ /^[-+]./) { |
65 |
|
userror(); |
68 |
|
} |
69 |
|
shift @ARGV; |
70 |
|
} |
71 |
+ |
# Check that we're actually being asked to do something |
72 |
+ |
die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback); |
73 |
+ |
# Name our own persist file? |
74 |
+ |
my $persistfile; |
75 |
+ |
if ( $tensortree && $nproc > 1 && "$rtargs" !~ /-PP /) { |
76 |
+ |
$persistfile = "$td/pfile.txt"; |
77 |
+ |
$rtargs = "-PP $persistfile $rtargs"; |
78 |
+ |
} |
79 |
|
# Get scene description and dimensions |
80 |
|
my $radscn = "$td/device.rad"; |
81 |
|
my $mgfscn = "$td/device.mgf"; |
85 |
|
die "Could not load MGF input\n" if ( $? ); |
86 |
|
system "mgf2rad $mgfscn > $radscn"; |
87 |
|
} else { |
88 |
< |
system "cat @ARGV | xform -e > $radscn"; |
88 |
> |
system "xform -e @ARGV > $radscn"; |
89 |
|
die "Could not load Radiance input\n" if ( $? ); |
90 |
|
system "rad2mgf $radscn > $mgfscn" if ( $geout ); |
91 |
|
} |
92 |
|
if ($#dim != 5) { |
93 |
< |
@dim = split /\s+/, `getbbox -h $radscn`; |
58 |
< |
shift @dim; |
93 |
> |
@dim = split ' ', `getbbox -h $radscn`; |
94 |
|
} |
95 |
|
print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5); |
96 |
< |
# Add receiver surface (rectangle) |
97 |
< |
my $modnm="_receiver_black_"; |
96 |
> |
# Add receiver surfaces (rectangular) |
97 |
> |
my $fmodnm="receiver_face"; |
98 |
> |
my $bmodnm="receiver_behind"; |
99 |
|
open(RADSCN, ">> $radscn"); |
100 |
< |
print RADSCN "void glow $modnm\n0\n0\n4 0 0 0 0\n\n"; |
101 |
< |
print RADSCN "$modnm polygon _receiver_\n0\n0\n12\n"; |
102 |
< |
print RADSCN "\t",$dim[0],"\t",$dim[2],"\t",$dim[5]+1e-5,"\n"; |
103 |
< |
print RADSCN "\t",$dim[0],"\t",$dim[3],"\t",$dim[5]+1e-5,"\n"; |
68 |
< |
print RADSCN "\t",$dim[1],"\t",$dim[3],"\t",$dim[5]+1e-5,"\n"; |
69 |
< |
print RADSCN "\t",$dim[1],"\t",$dim[2],"\t",$dim[5]+1e-5,"\n"; |
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; |
105 |
|
# Generate octree |
106 |
|
system "oconv -w $radscn > $octree"; |
107 |
|
die "Could not compile scene\n" if ( $? ); |
108 |
< |
# Set up sampling |
108 |
> |
# 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 |
127 |
> |
if ( $geout ) { |
128 |
> |
print "\t\t<Geometry format=\"MGF\" unit=\"$gunit\">\n"; |
129 |
> |
printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; |
130 |
> |
open(MGFSCN, "< $mgfscn"); |
131 |
> |
while (<MGFSCN>) { print $_; } |
132 |
> |
close MGFSCN; |
133 |
> |
print "xf\n"; |
134 |
> |
print "\t\t</Geometry>\n"; |
135 |
> |
} |
136 |
> |
print " </Material>\n"; |
137 |
> |
# Set up surface sampling |
138 |
> |
my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); |
139 |
> |
my $ny = int($nsamp/$nx + .5); |
140 |
> |
$nsamp = $nx * $ny; |
141 |
> |
my $ns = 2**$ttlog2; |
142 |
> |
my (@pdiv, $disk2sq, $sq2disk, $tcal, $kcal); |
143 |
> |
# Create data segments (all the work happens here) |
144 |
> |
if ( $tensortree ) { |
145 |
> |
do_tree_bsdf(); |
146 |
> |
} else { |
147 |
> |
do_matrix_bsdf(); |
148 |
> |
} |
149 |
> |
# Output XML epilogue |
150 |
> |
print |
151 |
> |
'</Layer> |
152 |
> |
</Optical> |
153 |
> |
</WindowElement> |
154 |
> |
'; |
155 |
> |
# Clean up temporary files and exit |
156 |
> |
exec("rm -rf $td"); |
157 |
> |
|
158 |
> |
#-------------- End of main program segment --------------# |
159 |
> |
|
160 |
> |
#++++++++++++++ Kill persistent rtrace +++++++++++++++++++# |
161 |
> |
sub persist_end { |
162 |
> |
if ( $persistfile && open(PFI, "< $persistfile") ) { |
163 |
> |
while (<PFI>) { |
164 |
> |
s/^[^ ]* //; |
165 |
> |
kill('ALRM', $_); |
166 |
> |
last; |
167 |
> |
} |
168 |
> |
close PFI; |
169 |
> |
} |
170 |
> |
} |
171 |
> |
|
172 |
> |
#++++++++++++++ Tensor tree BSDF generation ++++++++++++++# |
173 |
> |
sub do_tree_bsdf { |
174 |
> |
# Get sampling rate and subdivide task |
175 |
> |
my $ns2 = $ns; |
176 |
> |
$ns2 /= 2 if ( $tensortree == 3 ); |
177 |
> |
my $nsplice = $nproc; |
178 |
> |
$nsplice *= 10 if ($nproc > 1); |
179 |
> |
$nsplice = $ns2 if ($nsplice > $ns2); |
180 |
> |
$nsplice = 999 if ($nsplice > 999); |
181 |
> |
@pdiv = (0, int($ns2/$nsplice)); |
182 |
> |
my $nrem = $ns2 % $nsplice; |
183 |
> |
for (my $i = 1; $i < $nsplice; $i++) { |
184 |
> |
my $nv = $pdiv[$i] + $pdiv[1]; |
185 |
> |
++$nv if ( $nrem-- > 0 ); |
186 |
> |
push @pdiv, $nv; |
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 |
> |
} |
267 |
> |
} # end of sub do_tree_bsdf() |
268 |
> |
|
269 |
> |
# Run rtcontrib process in background to generate tensor tree samples |
270 |
> |
sub bg_tree_rtcontrib { |
271 |
> |
my $pid = fork(); |
272 |
> |
die "Cannot fork new process" unless defined $pid; |
273 |
> |
if ($pid > 0) { return $pid; } |
274 |
> |
my $forw = shift; |
275 |
> |
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"; |
284 |
> |
if ( $tensortree == 3 ) { |
285 |
> |
# Isotropic BSDF |
286 |
> |
$cmd = "cnt $plen $ny $nx " . |
287 |
> |
"| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " . |
288 |
> |
"-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " . |
289 |
> |
"-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " . |
290 |
> |
"-e 'Dx=1-2*($pbeg+\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' " . |
291 |
> |
"-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . |
292 |
> |
"-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . |
293 |
> |
"-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " . |
294 |
> |
"-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " . |
295 |
> |
"-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " . |
296 |
> |
"| $cmd"; |
297 |
> |
} else { |
298 |
> |
# Anisotropic BSDF |
299 |
> |
# Sample area vertically to improve load balance, since |
300 |
> |
# shading systems usually have bilateral symmetry (L-R) |
301 |
> |
$cmd = "cnt $plen $ns $ny $nx " . |
302 |
> |
"| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " . |
303 |
> |
"-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " . |
304 |
> |
"-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"; |
314 |
> |
} |
315 |
> |
# print STDERR "Starting: $cmd\n"; |
316 |
> |
exec($cmd); # no return; status report to parent via wait |
317 |
> |
die "Cannot exec: $cmd\n"; |
318 |
> |
} # end of bg_tree_rtcontrib() |
319 |
> |
|
320 |
> |
# Simplify and output tensor tree results |
321 |
> |
sub ttree_out { |
322 |
> |
my $forw = shift; |
323 |
> |
my $side = ("Back","Front")[$forw]; |
324 |
> |
# Only output one transmitted distribution, preferring backwards |
325 |
> |
if ( !$forw || !$doback ) { |
326 |
> |
print |
327 |
> |
' <WavelengthData> |
328 |
> |
<LayerNumber>System</LayerNumber> |
329 |
> |
<Wavelength unit="Integral">Visible</Wavelength> |
330 |
> |
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
331 |
> |
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
332 |
> |
<WavelengthDataBlock> |
333 |
> |
<WavelengthDataDirection>Transmission</WavelengthDataDirection> |
334 |
> |
<AngleBasis>LBNL/Shirley-Chiu</AngleBasis> |
335 |
> |
<ScatteringDataType>BTDF</ScatteringDataType> |
336 |
> |
<ScatteringData> |
337 |
> |
'; |
338 |
> |
system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . |
339 |
> |
q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } . |
340 |
> |
"$td/" . ($bmodnm,$fmodnm)[$forw] . "_???.flt " . |
341 |
> |
"| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2"; |
342 |
> |
die "Failure running rttree_reduce" if ( $? ); |
343 |
> |
print |
344 |
> |
' </ScatteringData> |
345 |
> |
</WavelengthDataBlock> |
346 |
> |
</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 -a -h -ff -t $pctcull -r $tensortree -g $ttlog2"; |
368 |
> |
die "Failure running rttree_reduce" if ( $? ); |
369 |
> |
print |
370 |
> |
' </ScatteringData> |
371 |
> |
</WavelengthDataBlock> |
372 |
> |
</WavelengthData> |
373 |
> |
'; |
374 |
> |
} # end of ttree_out() |
375 |
> |
|
376 |
> |
#------------- End of do_tree_bsdf() & subroutines -------------# |
377 |
> |
|
378 |
> |
#+++++++++++++++ Klems matrix BSDF generation +++++++++++++++# |
379 |
> |
sub do_matrix_bsdf { |
380 |
> |
# Set up sampling of portal |
381 |
|
# Kbin to produce incident direction in full Klems basis with (x1,x2) randoms |
382 |
< |
my $tcal = ' |
382 |
> |
$tcal = ' |
383 |
|
DEGREE : PI/180; |
384 |
+ |
sq(x) : x*x; |
385 |
|
Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90); |
386 |
|
Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12); |
387 |
|
Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0); |
392 |
|
Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow); |
393 |
|
Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1)); |
394 |
|
sin_kpol = sin(Kpol); |
395 |
< |
Dx = -cos(Kazi)*sin_kpol; |
395 |
> |
Dx = cos(Kazi)*sin_kpol; |
396 |
|
Dy = sin(Kazi)*sin_kpol; |
397 |
|
Dz = sqrt(1 - sin_kpol*sin_kpol); |
398 |
< |
Komega = 2*PI*if(Kbin-.5, |
399 |
< |
(cos(Kpola(Krow-1)*DEGREE) - cos(Kpola(Krow)*DEGREE))/Knaz(Krow), |
400 |
< |
1 - cos(Kpola(1)*DEGREE)); |
398 |
> |
KprojOmega = PI * if(Kbin-.5, |
399 |
> |
(sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow), |
400 |
> |
1 - sq(cos(Kpola(1)*DEGREE))); |
401 |
|
'; |
402 |
< |
# Compute Klems bin from exiting ray direction |
403 |
< |
my $kcal = ' |
402 |
> |
# Compute Klems bin from exiting ray direction (forward or backward) |
403 |
> |
$kcal = ' |
404 |
|
DEGREE : PI/180; |
405 |
+ |
abs(x) : if(x, x, -x); |
406 |
|
Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x))); |
407 |
|
posangle(a) : if(-a, a + 2*PI, a); |
408 |
|
Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x)); |
423 |
|
kaccum(7) + kazn(azi,360/knaz(8)), |
424 |
|
kaccum(8) + kazn(azi,360/knaz(9)) |
425 |
|
); |
426 |
< |
kbin = kbin2(Acos(Dz), Atan2(Dy, -Dx)); |
426 |
> |
kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx)); |
427 |
|
'; |
428 |
|
my $ndiv = 145; |
121 |
– |
my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); |
122 |
– |
my $ny = int($nsamp/$nx + .5); |
123 |
– |
$nsamp = $nx * $ny; |
429 |
|
# Compute scattering data using rtcontrib |
430 |
< |
my $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . |
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[4]-1e-5' " . |
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;$2=yp;$3=zp;$4=Dx;$5=Dy;$6=Dz' } . |
463 |
< |
"| rtcontrib -h -ff -n $nproc -c $nsamp -e '$kcal' -b kbin -bn $ndiv " . |
464 |
< |
"-m $modnm -w -ab 5 -ad 700 -lw 3e-6 $octree " . |
465 |
< |
"| rcalc -e 'x1:.5;x2:.5;$tcal' " . |
466 |
< |
"-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-1,$ndiv)' " . |
467 |
< |
q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/(Komega*Dz)'}; |
468 |
< |
my @darr = `$cmd`; |
137 |
< |
die "Failure running: $cmd\n" if ( $? ); |
138 |
< |
# Output XML prologue |
139 |
< |
print |
140 |
< |
'<?xml version="1.0" encoding="UTF-8"?> |
141 |
< |
<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"> |
142 |
< |
<WindowElementType>System</WindowElementType> |
143 |
< |
<Optical> |
144 |
< |
<Layer> |
145 |
< |
<Material> |
146 |
< |
<Name>Name</Name> |
147 |
< |
<Manufacturer>Manufacturer</Manufacturer> |
148 |
< |
'; |
149 |
< |
printf "\t\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4]; |
150 |
< |
printf "\t\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0]; |
151 |
< |
printf "\t\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2]; |
152 |
< |
print "\t\t\t<DeviceType>Integral</DeviceType>\n"; |
153 |
< |
# Output MGF description if requested |
154 |
< |
if ( $geout ) { |
155 |
< |
print "\t\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n"; |
156 |
< |
printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; |
157 |
< |
system "cat $mgfscn"; |
158 |
< |
print "xf\n"; |
159 |
< |
print "\t\t\t</Geometry>\n"; |
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 |
< |
print ' </Material> |
471 |
< |
<DataDefinition> |
472 |
< |
<IncidentDataStructure>Columns</IncidentDataStructure> |
473 |
< |
<AngleBasis> |
470 |
> |
# Output angle basis |
471 |
> |
print |
472 |
> |
' <DataDefinition> |
473 |
> |
<IncidentDataStructure>Columns</IncidentDataStructure> |
474 |
> |
<AngleBasis> |
475 |
|
<AngleBasisName>LBNL/Klems Full</AngleBasisName> |
476 |
< |
<AngleBasisBlock> |
476 |
> |
<AngleBasisBlock> |
477 |
|
<Theta>0</Theta> |
478 |
|
<nPhis>1</nPhis> |
479 |
|
<ThetaBounds> |
480 |
< |
<LowerTheta>0</LowerTheta> |
481 |
< |
<UpperTheta>5</UpperTheta> |
480 |
> |
<LowerTheta>0</LowerTheta> |
481 |
> |
<UpperTheta>5</UpperTheta> |
482 |
|
</ThetaBounds> |
483 |
|
</AngleBasisBlock> |
484 |
|
<AngleBasisBlock> |
547 |
|
</AngleBasisBlock> |
548 |
|
</AngleBasis> |
549 |
|
</DataDefinition> |
550 |
< |
<WavelengthData> |
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> |
562 |
|
<ScatteringDataType>BTDF</ScatteringDataType> |
563 |
|
<ScatteringData> |
564 |
|
'; |
565 |
< |
# Output computed data (transposed order) |
565 |
> |
# Output front transmission (transposed order) |
566 |
|
for (my $od = 0; $od < $ndiv; $od++) { |
567 |
|
for (my $id = 0; $id < $ndiv; $id++) { |
568 |
< |
print $darr[$ndiv*$id + $od]; |
568 |
> |
print $tfarr[$ndiv*$id + $od]; |
569 |
|
} |
570 |
|
print "\n"; |
571 |
|
} |
259 |
– |
# Output XML epilogue |
572 |
|
print |
573 |
< |
' </ScatteringData> |
574 |
< |
</WavelengthDataBlock> |
573 |
> |
' </ScatteringData> |
574 |
> |
</WavelengthDataBlock> |
575 |
|
</WavelengthData> |
576 |
< |
</Layer> |
577 |
< |
</Optical> |
578 |
< |
</WindowElement> |
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 |
< |
# Clean up temporary files |
589 |
< |
system "rm -rf $td"; |
588 |
> |
# Output front reflection (reciprocity averaging) |
589 |
> |
for (my $od = 0; $od < $ndiv; $od++) { |
590 |
> |
for (my $id = 0; $id < $ndiv; $id++) { |
591 |
> |
print .5*($rfarr[$ndiv*$id + $od] + $rfarr[$ndiv*$od + $id]); |
592 |
> |
} |
593 |
> |
print "\n"; |
594 |
> |
} |
595 |
> |
print |
596 |
> |
' </ScatteringData> |
597 |
> |
</WavelengthDataBlock> |
598 |
> |
</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]; |
619 |
> |
} |
620 |
> |
print "\n"; |
621 |
> |
} |
622 |
> |
print |
623 |
> |
' </ScatteringData> |
624 |
> |
</WavelengthDataBlock> |
625 |
> |
</WavelengthData> |
626 |
> |
<WavelengthData> |
627 |
> |
<LayerNumber>System</LayerNumber> |
628 |
> |
<Wavelength unit="Integral">Visible</Wavelength> |
629 |
> |
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
630 |
> |
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
631 |
> |
<WavelengthDataBlock> |
632 |
> |
<WavelengthDataDirection>Reflection Back</WavelengthDataDirection> |
633 |
> |
<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> |
634 |
> |
<RowAngleBasis>LBNL/Klems Full</RowAngleBasis> |
635 |
> |
<ScatteringDataType>BRDF</ScatteringDataType> |
636 |
> |
<ScatteringData> |
637 |
> |
'; |
638 |
> |
# Output back reflection (reciprocity averaging) |
639 |
> |
for (my $od = 0; $od < $ndiv; $od++) { |
640 |
> |
for (my $id = 0; $id < $ndiv; $id++) { |
641 |
> |
print .5*($rbarr[$ndiv*$id + $od] + $rbarr[$ndiv*$od + $id]); |
642 |
> |
} |
643 |
> |
print "\n"; |
644 |
> |
} |
645 |
> |
print |
646 |
> |
' </ScatteringData> |
647 |
> |
</WavelengthDataBlock> |
648 |
> |
</WavelengthData> |
649 |
> |
'; |
650 |
> |
} |
651 |
> |
} |
652 |
> |
#------------- End of do_matrix_bsdf() --------------# |