1 |
greg |
2.1 |
#!/usr/bin/perl -w |
2 |
greg |
2.44 |
# RCSid $Id: genBSDF.pl,v 2.43 2013/08/05 22:56:50 greg Exp $ |
3 |
greg |
2.1 |
# |
4 |
|
|
# Compute BSDF based on geometry and material description |
5 |
|
|
# |
6 |
|
|
# G. Ward |
7 |
|
|
# |
8 |
|
|
use strict; |
9 |
greg |
2.13 |
use File::Temp qw/ :mktemp /; |
10 |
greg |
2.1 |
sub userror { |
11 |
greg |
2.29 |
print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-t{3|4} Nlog2][-r \"ropts\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom units] [input ..]\n"; |
12 |
greg |
2.1 |
exit 1; |
13 |
|
|
} |
14 |
greg |
2.13 |
my $td = mkdtemp("/tmp/genBSDF.XXXXXX"); |
15 |
greg |
2.1 |
chomp $td; |
16 |
greg |
2.20 |
my @savedARGV = @ARGV; |
17 |
greg |
2.15 |
my $tensortree = 0; |
18 |
|
|
my $ttlog2 = 4; |
19 |
greg |
2.25 |
my $nsamp = 2000; |
20 |
greg |
2.10 |
my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6"; |
21 |
greg |
2.1 |
my $mgfin = 0; |
22 |
|
|
my $geout = 1; |
23 |
|
|
my $nproc = 1; |
24 |
greg |
2.9 |
my $doforw = 0; |
25 |
|
|
my $doback = 1; |
26 |
greg |
2.33 |
my $pctcull = 90; |
27 |
greg |
2.41 |
my $gunit = "meter"; |
28 |
greg |
2.1 |
my @dim; |
29 |
|
|
# Get options |
30 |
|
|
while ($#ARGV >= 0) { |
31 |
|
|
if ("$ARGV[0]" =~ /^[-+]m/) { |
32 |
|
|
$mgfin = ("$ARGV[0]" =~ /^\+/); |
33 |
greg |
2.10 |
} elsif ("$ARGV[0]" eq "-r") { |
34 |
|
|
$rtargs = "$rtargs $ARGV[1]"; |
35 |
|
|
shift @ARGV; |
36 |
greg |
2.1 |
} elsif ("$ARGV[0]" =~ /^[-+]g/) { |
37 |
|
|
$geout = ("$ARGV[0]" =~ /^\+/); |
38 |
greg |
2.22 |
$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 |
greg |
2.9 |
} elsif ("$ARGV[0]" =~ /^[-+]f/) { |
44 |
|
|
$doforw = ("$ARGV[0]" =~ /^\+/); |
45 |
|
|
} elsif ("$ARGV[0]" =~ /^[-+]b/) { |
46 |
|
|
$doback = ("$ARGV[0]" =~ /^\+/); |
47 |
greg |
2.27 |
} elsif ("$ARGV[0]" eq "-t") { |
48 |
greg |
2.33 |
# Use value < 0 for rttree_reduce bypass |
49 |
greg |
2.27 |
$pctcull = $ARGV[1]; |
50 |
|
|
shift @ARGV; |
51 |
greg |
2.15 |
} elsif ("$ARGV[0]" =~ /^-t[34]$/) { |
52 |
|
|
$tensortree = substr($ARGV[0], 2, 1); |
53 |
|
|
$ttlog2 = $ARGV[1]; |
54 |
|
|
shift @ARGV; |
55 |
greg |
2.1 |
} elsif ("$ARGV[0]" eq "-c") { |
56 |
|
|
$nsamp = $ARGV[1]; |
57 |
|
|
shift @ARGV; |
58 |
|
|
} elsif ("$ARGV[0]" eq "-n") { |
59 |
|
|
$nproc = $ARGV[1]; |
60 |
|
|
shift @ARGV; |
61 |
|
|
} elsif ("$ARGV[0]" =~ /^-d/) { |
62 |
|
|
userror() if ($#ARGV < 6); |
63 |
greg |
2.8 |
@dim = @ARGV[1..6]; |
64 |
greg |
2.1 |
shift @ARGV for (1..6); |
65 |
|
|
} elsif ("$ARGV[0]" =~ /^[-+]./) { |
66 |
|
|
userror(); |
67 |
|
|
} else { |
68 |
|
|
last; |
69 |
|
|
} |
70 |
|
|
shift @ARGV; |
71 |
|
|
} |
72 |
greg |
2.9 |
# Check that we're actually being asked to do something |
73 |
greg |
2.15 |
die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback); |
74 |
greg |
2.1 |
# Get scene description and dimensions |
75 |
|
|
my $radscn = "$td/device.rad"; |
76 |
|
|
my $mgfscn = "$td/device.mgf"; |
77 |
|
|
my $octree = "$td/device.oct"; |
78 |
|
|
if ( $mgfin ) { |
79 |
|
|
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"; |
80 |
|
|
die "Could not load MGF input\n" if ( $? ); |
81 |
|
|
system "mgf2rad $mgfscn > $radscn"; |
82 |
|
|
} else { |
83 |
greg |
2.13 |
system "xform -e @ARGV > $radscn"; |
84 |
greg |
2.1 |
die "Could not load Radiance input\n" if ( $? ); |
85 |
|
|
system "rad2mgf $radscn > $mgfscn" if ( $geout ); |
86 |
|
|
} |
87 |
|
|
if ($#dim != 5) { |
88 |
greg |
2.7 |
@dim = split ' ', `getbbox -h $radscn`; |
89 |
greg |
2.1 |
} |
90 |
|
|
print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5); |
91 |
greg |
2.9 |
# Add receiver surfaces (rectangular) |
92 |
greg |
2.10 |
my $fmodnm="receiver_face"; |
93 |
greg |
2.9 |
my $bmodnm="receiver_behind"; |
94 |
greg |
2.1 |
open(RADSCN, ">> $radscn"); |
95 |
greg |
2.10 |
print RADSCN "void glow $fmodnm\n0\n0\n4 1 1 1 0\n\n"; |
96 |
|
|
print RADSCN "$fmodnm source f_receiver\n0\n0\n4 0 0 1 180\n"; |
97 |
|
|
print RADSCN "void glow $bmodnm\n0\n0\n4 1 1 1 0\n\n"; |
98 |
|
|
print RADSCN "$bmodnm source b_receiver\n0\n0\n4 0 0 -1 180\n"; |
99 |
greg |
2.1 |
close RADSCN; |
100 |
|
|
# Generate octree |
101 |
|
|
system "oconv -w $radscn > $octree"; |
102 |
|
|
die "Could not compile scene\n" if ( $? ); |
103 |
greg |
2.15 |
# Output XML prologue |
104 |
|
|
print |
105 |
|
|
'<?xml version="1.0" encoding="UTF-8"?> |
106 |
|
|
<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"> |
107 |
greg |
2.20 |
'; |
108 |
|
|
print "<!-- File produced by: genBSDF @savedARGV -->\n"; |
109 |
|
|
print |
110 |
|
|
'<WindowElementType>System</WindowElementType> |
111 |
greg |
2.40 |
<FileType>BSDF</FileType> |
112 |
greg |
2.15 |
<Optical> |
113 |
|
|
<Layer> |
114 |
|
|
<Material> |
115 |
|
|
<Name>Name</Name> |
116 |
|
|
<Manufacturer>Manufacturer</Manufacturer> |
117 |
|
|
'; |
118 |
greg |
2.22 |
printf "\t\t<Thickness unit=\"$gunit\">%.3f</Thickness>\n", $dim[5] - $dim[4]; |
119 |
|
|
printf "\t\t<Width unit=\"$gunit\">%.3f</Width>\n", $dim[1] - $dim[0]; |
120 |
|
|
printf "\t\t<Height unit=\"$gunit\">%.3f</Height>\n", $dim[3] - $dim[2]; |
121 |
greg |
2.40 |
print "\t\t<DeviceType>Other</DeviceType>\n"; |
122 |
|
|
print " </Material>\n"; |
123 |
greg |
2.15 |
# Output MGF description if requested |
124 |
|
|
if ( $geout ) { |
125 |
greg |
2.40 |
print "\t<Geometry format=\"MGF\">\n"; |
126 |
greg |
2.42 |
print "\t\t<MGFblock unit=\"$gunit\">\n"; |
127 |
greg |
2.15 |
printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2; |
128 |
|
|
open(MGFSCN, "< $mgfscn"); |
129 |
|
|
while (<MGFSCN>) { print $_; } |
130 |
|
|
close MGFSCN; |
131 |
|
|
print "xf\n"; |
132 |
greg |
2.42 |
print "</MGFblock>\n"; |
133 |
greg |
2.40 |
print "\t</Geometry>\n"; |
134 |
greg |
2.15 |
} |
135 |
|
|
# Set up surface sampling |
136 |
|
|
my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5); |
137 |
|
|
my $ny = int($nsamp/$nx + .5); |
138 |
|
|
$nsamp = $nx * $ny; |
139 |
|
|
my $ns = 2**$ttlog2; |
140 |
|
|
my (@pdiv, $disk2sq, $sq2disk, $tcal, $kcal); |
141 |
|
|
# Create data segments (all the work happens here) |
142 |
|
|
if ( $tensortree ) { |
143 |
|
|
do_tree_bsdf(); |
144 |
|
|
} else { |
145 |
|
|
do_matrix_bsdf(); |
146 |
|
|
} |
147 |
|
|
# Output XML epilogue |
148 |
|
|
print |
149 |
|
|
'</Layer> |
150 |
|
|
</Optical> |
151 |
|
|
</WindowElement> |
152 |
|
|
'; |
153 |
|
|
# Clean up temporary files and exit |
154 |
greg |
2.16 |
exec("rm -rf $td"); |
155 |
|
|
|
156 |
greg |
2.15 |
#-------------- End of main program segment --------------# |
157 |
|
|
|
158 |
|
|
#++++++++++++++ Tensor tree BSDF generation ++++++++++++++# |
159 |
|
|
sub do_tree_bsdf { |
160 |
|
|
# Shirley-Chiu mapping from unit square to disk |
161 |
|
|
$sq2disk = ' |
162 |
|
|
in_square_a = 2*in_square_x - 1; |
163 |
|
|
in_square_b = 2*in_square_y - 1; |
164 |
|
|
in_square_rgn = if(in_square_a + in_square_b, |
165 |
|
|
if(in_square_a - in_square_b, 1, 2), |
166 |
|
|
if(in_square_b - in_square_a, 3, 4)); |
167 |
|
|
out_disk_r = .999995*select(in_square_rgn, in_square_a, in_square_b, |
168 |
|
|
-in_square_a, -in_square_b); |
169 |
|
|
out_disk_phi = PI/4 * select(in_square_rgn, |
170 |
|
|
in_square_b/in_square_a, |
171 |
|
|
2 - in_square_a/in_square_b, |
172 |
|
|
4 + in_square_b/in_square_a, |
173 |
|
|
if(in_square_b*in_square_b, |
174 |
|
|
6 - in_square_a/in_square_b, 0)); |
175 |
|
|
Dx = out_disk_r*cos(out_disk_phi); |
176 |
|
|
Dy = out_disk_r*sin(out_disk_phi); |
177 |
|
|
Dz = sqrt(1 - out_disk_r*out_disk_r); |
178 |
|
|
'; |
179 |
|
|
# Shirley-Chiu mapping from unit disk to square |
180 |
|
|
$disk2sq = ' |
181 |
|
|
norm_radians(p) : if(-p - PI/4, p + 2*PI, p); |
182 |
|
|
in_disk_r = .999995*sqrt(Dx*Dx + Dy*Dy); |
183 |
|
|
in_disk_phi = norm_radians(atan2(Dy, Dx)); |
184 |
greg |
2.44 |
in_disk_rgn = floor((.999995*in_disk_phi + PI/4)/(PI/2)) + 1; |
185 |
greg |
2.15 |
out_square_a = select(in_disk_rgn, |
186 |
|
|
in_disk_r, |
187 |
|
|
(PI/2 - in_disk_phi)*in_disk_r/(PI/4), |
188 |
|
|
-in_disk_r, |
189 |
|
|
(in_disk_phi - 3*PI/2)*in_disk_r/(PI/4)); |
190 |
|
|
out_square_b = select(in_disk_rgn, |
191 |
|
|
in_disk_phi*in_disk_r/(PI/4), |
192 |
|
|
in_disk_r, |
193 |
|
|
(PI - in_disk_phi)*in_disk_r/(PI/4), |
194 |
|
|
-in_disk_r); |
195 |
|
|
out_square_x = (out_square_a + 1)/2; |
196 |
|
|
out_square_y = (out_square_b + 1)/2; |
197 |
|
|
'; |
198 |
|
|
# Announce ourselves in XML output |
199 |
greg |
2.16 |
print "\t<DataDefinition>\n"; |
200 |
|
|
print "\t\t<IncidentDataStructure>TensorTree$tensortree</IncidentDataStructure>\n"; |
201 |
|
|
print "\t</DataDefinition>\n"; |
202 |
greg |
2.36 |
|
203 |
greg |
2.37 |
# Start rcontrib processes for compute each side |
204 |
greg |
2.36 |
do_tree_rtcontrib(0) if ( $doback ); |
205 |
|
|
do_tree_rtcontrib(1) if ( $doforw ); |
206 |
|
|
|
207 |
greg |
2.15 |
} # end of sub do_tree_bsdf() |
208 |
|
|
|
209 |
greg |
2.38 |
# Run rcontrib process to generate tensor tree samples |
210 |
greg |
2.36 |
sub do_tree_rtcontrib { |
211 |
greg |
2.15 |
my $forw = shift; |
212 |
|
|
my $matargs = "-m $bmodnm"; |
213 |
greg |
2.38 |
if ( !$forw || !$doback || $tensortree==3 ) { $matargs .= " -m $fmodnm"; } |
214 |
greg |
2.36 |
my $cmd = "rcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " . |
215 |
greg |
2.15 |
"-e '$disk2sq' -bn '$ns*$ns' " . |
216 |
|
|
"-b '$ns*floor(out_square_x*$ns)+floor(out_square_y*$ns)' " . |
217 |
greg |
2.36 |
"-o $td/%s.flt $matargs $octree"; |
218 |
greg |
2.15 |
if ( $tensortree == 3 ) { |
219 |
|
|
# Isotropic BSDF |
220 |
greg |
2.36 |
my $ns2 = $ns / 2; |
221 |
|
|
$cmd = "cnt $ns2 $ny $nx " . |
222 |
|
|
"| rcalc -e 'r1=rand(.8681*recno-.673892)' " . |
223 |
|
|
"-e 'r2=rand(-5.37138*recno+67.1737811)' " . |
224 |
|
|
"-e 'r3=rand(+3.17603772*recno+83.766771)' " . |
225 |
|
|
"-e 'Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' " . |
226 |
greg |
2.15 |
"-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . |
227 |
|
|
"-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . |
228 |
|
|
"-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " . |
229 |
|
|
"-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " . |
230 |
|
|
"-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " . |
231 |
|
|
"| $cmd"; |
232 |
|
|
} else { |
233 |
|
|
# Anisotropic BSDF |
234 |
|
|
# Sample area vertically to improve load balance, since |
235 |
|
|
# shading systems usually have bilateral symmetry (L-R) |
236 |
greg |
2.36 |
$cmd = "cnt $ns $ns $ny $nx " . |
237 |
|
|
"| rcalc -e 'r1=rand(.8681*recno-.673892)' " . |
238 |
|
|
"-e 'r2=rand(-5.37138*recno+67.1737811)' " . |
239 |
|
|
"-e 'r3=rand(3.17603772*recno+83.766771)' " . |
240 |
|
|
"-e 'r4=rand(-2.3857833*recno-964.72738)' " . |
241 |
|
|
"-e 'in_square_x=(\$1+r1)/$ns' " . |
242 |
greg |
2.15 |
"-e 'in_square_y=(\$2+r2)/$ns' -e '$sq2disk' " . |
243 |
|
|
"-e 'xp=(\$4+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . |
244 |
|
|
"-e 'yp=(\$3+r4)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . |
245 |
|
|
"-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " . |
246 |
|
|
"-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " . |
247 |
|
|
"-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " . |
248 |
|
|
"| $cmd"; |
249 |
|
|
} |
250 |
|
|
# print STDERR "Starting: $cmd\n"; |
251 |
greg |
2.37 |
system "$cmd" || die "Failure running rcontrib"; |
252 |
greg |
2.36 |
ttree_out($forw); |
253 |
|
|
} # end of do_tree_rtcontrib() |
254 |
greg |
2.15 |
|
255 |
|
|
# Simplify and output tensor tree results |
256 |
|
|
sub ttree_out { |
257 |
|
|
my $forw = shift; |
258 |
|
|
my $side = ("Back","Front")[$forw]; |
259 |
greg |
2.33 |
my $cmd; |
260 |
greg |
2.38 |
# Only output one transmitted anisotropic distribution, preferring backwards |
261 |
|
|
if ( !$forw || !$doback || $tensortree==3 ) { |
262 |
greg |
2.15 |
print |
263 |
|
|
' <WavelengthData> |
264 |
|
|
<LayerNumber>System</LayerNumber> |
265 |
|
|
<Wavelength unit="Integral">Visible</Wavelength> |
266 |
|
|
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
267 |
|
|
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
268 |
|
|
<WavelengthDataBlock> |
269 |
greg |
2.38 |
'; |
270 |
|
|
print "\t\t\t<WavelengthDataDirection>Transmission $side</WavelengthDataDirection>\n"; |
271 |
|
|
print |
272 |
|
|
' <AngleBasis>LBNL/Shirley-Chiu</AngleBasis> |
273 |
greg |
2.15 |
<ScatteringDataType>BTDF</ScatteringDataType> |
274 |
|
|
<ScatteringData> |
275 |
|
|
'; |
276 |
greg |
2.33 |
$cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . |
277 |
|
|
q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' }; |
278 |
|
|
if ($pctcull >= 0) { |
279 |
greg |
2.36 |
$cmd .= "-of $td/" . ($bmodnm,$fmodnm)[$forw] . ".flt " . |
280 |
greg |
2.43 |
"| rttree_reduce -h -ff -t $pctcull -r $tensortree -g $ttlog2"; |
281 |
greg |
2.33 |
system "$cmd" || die "Failure running rttree_reduce"; |
282 |
|
|
} else { |
283 |
greg |
2.36 |
$cmd .= "$td/" . ($bmodnm,$fmodnm)[$forw] . ".flt"; |
284 |
greg |
2.33 |
print "{\n"; |
285 |
|
|
system "$cmd" || die "Failure running rcalc"; |
286 |
greg |
2.34 |
for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) { |
287 |
|
|
print "0\n"; |
288 |
|
|
} |
289 |
greg |
2.33 |
print "}\n"; |
290 |
|
|
} |
291 |
greg |
2.15 |
print |
292 |
|
|
' </ScatteringData> |
293 |
|
|
</WavelengthDataBlock> |
294 |
|
|
</WavelengthData> |
295 |
|
|
'; |
296 |
|
|
} |
297 |
|
|
# Output reflection |
298 |
|
|
print |
299 |
|
|
' <WavelengthData> |
300 |
|
|
<LayerNumber>System</LayerNumber> |
301 |
|
|
<Wavelength unit="Integral">Visible</Wavelength> |
302 |
|
|
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
303 |
|
|
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
304 |
|
|
<WavelengthDataBlock> |
305 |
greg |
2.17 |
'; |
306 |
|
|
print "\t\t\t<WavelengthDataDirection>Reflection $side</WavelengthDataDirection>\n"; |
307 |
greg |
2.20 |
print |
308 |
|
|
' <AngleBasis>LBNL/Shirley-Chiu</AngleBasis> |
309 |
greg |
2.40 |
<ScatteringDataType>BTDF</ScatteringDataType> |
310 |
greg |
2.15 |
<ScatteringData> |
311 |
|
|
'; |
312 |
greg |
2.33 |
$cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " . |
313 |
|
|
q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' }; |
314 |
|
|
if ($pctcull >= 0) { |
315 |
greg |
2.36 |
$cmd .= "-of $td/" . ($fmodnm,$bmodnm)[$forw] . ".flt " . |
316 |
greg |
2.28 |
"| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2"; |
317 |
greg |
2.33 |
system "$cmd" || die "Failure running rttree_reduce"; |
318 |
|
|
} else { |
319 |
greg |
2.36 |
$cmd .= "$td/" . ($fmodnm,$bmodnm)[$forw] . ".flt"; |
320 |
greg |
2.33 |
print "{\n"; |
321 |
|
|
system "$cmd" || die "Failure running rcalc"; |
322 |
greg |
2.34 |
for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) { |
323 |
|
|
print "0\n"; |
324 |
|
|
} |
325 |
greg |
2.33 |
print "}\n"; |
326 |
|
|
} |
327 |
greg |
2.15 |
print |
328 |
|
|
' </ScatteringData> |
329 |
|
|
</WavelengthDataBlock> |
330 |
|
|
</WavelengthData> |
331 |
|
|
'; |
332 |
|
|
} # end of ttree_out() |
333 |
|
|
|
334 |
|
|
#------------- End of do_tree_bsdf() & subroutines -------------# |
335 |
|
|
|
336 |
|
|
#+++++++++++++++ Klems matrix BSDF generation +++++++++++++++# |
337 |
|
|
sub do_matrix_bsdf { |
338 |
|
|
# Set up sampling of portal |
339 |
greg |
2.1 |
# Kbin to produce incident direction in full Klems basis with (x1,x2) randoms |
340 |
greg |
2.15 |
$tcal = ' |
341 |
greg |
2.1 |
DEGREE : PI/180; |
342 |
greg |
2.5 |
sq(x) : x*x; |
343 |
greg |
2.35 |
Kpola(r) : select(r+1, 0, 5, 15, 25, 35, 45, 55, 65, 75, 90); |
344 |
greg |
2.1 |
Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12); |
345 |
|
|
Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0); |
346 |
|
|
Kmax : Kaccum(Knaz(0)); |
347 |
|
|
Kfindrow(r, rem) : if(rem-Knaz(r)+.5, Kfindrow(r+1, rem-Knaz(r)), r); |
348 |
|
|
Krow = if(Kbin-(Kmax-.5), 0, Kfindrow(1, Kbin)); |
349 |
|
|
Kcol = Kbin - Kaccum(Krow-1); |
350 |
greg |
2.2 |
Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow); |
351 |
greg |
2.1 |
Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1)); |
352 |
|
|
sin_kpol = sin(Kpol); |
353 |
greg |
2.9 |
Dx = cos(Kazi)*sin_kpol; |
354 |
greg |
2.1 |
Dy = sin(Kazi)*sin_kpol; |
355 |
|
|
Dz = sqrt(1 - sin_kpol*sin_kpol); |
356 |
greg |
2.5 |
KprojOmega = PI * if(Kbin-.5, |
357 |
|
|
(sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow), |
358 |
|
|
1 - sq(cos(Kpola(1)*DEGREE))); |
359 |
greg |
2.1 |
'; |
360 |
greg |
2.9 |
# Compute Klems bin from exiting ray direction (forward or backward) |
361 |
greg |
2.15 |
$kcal = ' |
362 |
greg |
2.1 |
DEGREE : PI/180; |
363 |
greg |
2.11 |
abs(x) : if(x, x, -x); |
364 |
greg |
2.30 |
Acos(x) : if(x-1, 0, if(-1-x, PI, acos(x))) / DEGREE; |
365 |
greg |
2.1 |
posangle(a) : if(-a, a + 2*PI, a); |
366 |
greg |
2.30 |
Atan2(y,x) : posangle(atan2(y,x)) / DEGREE; |
367 |
greg |
2.1 |
kpola(r) : select(r, 5, 15, 25, 35, 45, 55, 65, 75, 90); |
368 |
|
|
knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12); |
369 |
|
|
kaccum(r) : if(r-.5, knaz(r) + kaccum(r-1), 0); |
370 |
|
|
kfindrow(r, pol) : if(r-kpola(0)+.5, r, |
371 |
|
|
if(pol-kpola(r), kfindrow(r+1, pol), r) ); |
372 |
|
|
kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0); |
373 |
|
|
kbin2(pol,azi) = select(kfindrow(1, pol), |
374 |
|
|
kazn(azi,360/knaz(1)), |
375 |
|
|
kaccum(1) + kazn(azi,360/knaz(2)), |
376 |
|
|
kaccum(2) + kazn(azi,360/knaz(3)), |
377 |
|
|
kaccum(3) + kazn(azi,360/knaz(4)), |
378 |
|
|
kaccum(4) + kazn(azi,360/knaz(5)), |
379 |
|
|
kaccum(5) + kazn(azi,360/knaz(6)), |
380 |
|
|
kaccum(6) + kazn(azi,360/knaz(7)), |
381 |
|
|
kaccum(7) + kazn(azi,360/knaz(8)), |
382 |
|
|
kaccum(8) + kazn(azi,360/knaz(9)) |
383 |
|
|
); |
384 |
greg |
2.11 |
kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx)); |
385 |
greg |
2.1 |
'; |
386 |
|
|
my $ndiv = 145; |
387 |
greg |
2.37 |
# Compute scattering data using rcontrib |
388 |
greg |
2.9 |
my @tfarr; |
389 |
|
|
my @rfarr; |
390 |
|
|
my @tbarr; |
391 |
|
|
my @rbarr; |
392 |
|
|
my $cmd; |
393 |
greg |
2.36 |
my $rtcmd = "rcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " . |
394 |
greg |
2.9 |
"-e '$kcal' -b kbin -bn $ndiv " . |
395 |
greg |
2.15 |
"-o '$td/%s.flt' -m $fmodnm -m $bmodnm $octree"; |
396 |
greg |
2.9 |
my $rccmd = "rcalc -e '$tcal' " . |
397 |
|
|
"-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " . |
398 |
greg |
2.29 |
q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega' }; |
399 |
greg |
2.9 |
if ( $doforw ) { |
400 |
|
|
$cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . |
401 |
greg |
2.11 |
"-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . |
402 |
|
|
"-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . |
403 |
greg |
2.10 |
"-e 'zp:$dim[4]' " . |
404 |
greg |
2.11 |
q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } . |
405 |
greg |
2.10 |
q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } . |
406 |
greg |
2.9 |
"| $rtcmd"; |
407 |
|
|
system "$cmd" || die "Failure running: $cmd\n"; |
408 |
|
|
@tfarr = `$rccmd $td/$fmodnm.flt`; |
409 |
|
|
die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); |
410 |
greg |
2.39 |
chomp(@tfarr); |
411 |
greg |
2.9 |
@rfarr = `$rccmd $td/$bmodnm.flt`; |
412 |
|
|
die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); |
413 |
greg |
2.39 |
chomp(@rfarr); |
414 |
greg |
2.9 |
} |
415 |
|
|
if ( $doback ) { |
416 |
|
|
$cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " . |
417 |
|
|
"-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " . |
418 |
|
|
"-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " . |
419 |
greg |
2.10 |
"-e 'zp:$dim[5]' " . |
420 |
greg |
2.9 |
q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } . |
421 |
greg |
2.11 |
q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } . |
422 |
greg |
2.9 |
"| $rtcmd"; |
423 |
|
|
system "$cmd" || die "Failure running: $cmd\n"; |
424 |
|
|
@tbarr = `$rccmd $td/$bmodnm.flt`; |
425 |
|
|
die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? ); |
426 |
greg |
2.32 |
chomp(@tbarr); |
427 |
greg |
2.9 |
@rbarr = `$rccmd $td/$fmodnm.flt`; |
428 |
|
|
die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? ); |
429 |
greg |
2.32 |
chomp(@rbarr); |
430 |
greg |
2.9 |
} |
431 |
greg |
2.15 |
# Output angle basis |
432 |
greg |
2.1 |
print |
433 |
greg |
2.15 |
' <DataDefinition> |
434 |
|
|
<IncidentDataStructure>Columns</IncidentDataStructure> |
435 |
|
|
<AngleBasis> |
436 |
greg |
2.1 |
<AngleBasisName>LBNL/Klems Full</AngleBasisName> |
437 |
greg |
2.15 |
<AngleBasisBlock> |
438 |
greg |
2.35 |
<Theta>0</Theta> |
439 |
|
|
<nPhis>1</nPhis> |
440 |
|
|
<ThetaBounds> |
441 |
|
|
<LowerTheta>0</LowerTheta> |
442 |
|
|
<UpperTheta>5</UpperTheta> |
443 |
|
|
</ThetaBounds> |
444 |
|
|
</AngleBasisBlock> |
445 |
|
|
<AngleBasisBlock> |
446 |
|
|
<Theta>10</Theta> |
447 |
|
|
<nPhis>8</nPhis> |
448 |
|
|
<ThetaBounds> |
449 |
|
|
<LowerTheta>5</LowerTheta> |
450 |
|
|
<UpperTheta>15</UpperTheta> |
451 |
|
|
</ThetaBounds> |
452 |
|
|
</AngleBasisBlock> |
453 |
|
|
<AngleBasisBlock> |
454 |
|
|
<Theta>20</Theta> |
455 |
|
|
<nPhis>16</nPhis> |
456 |
|
|
<ThetaBounds> |
457 |
|
|
<LowerTheta>15</LowerTheta> |
458 |
|
|
<UpperTheta>25</UpperTheta> |
459 |
|
|
</ThetaBounds> |
460 |
|
|
</AngleBasisBlock> |
461 |
|
|
<AngleBasisBlock> |
462 |
|
|
<Theta>30</Theta> |
463 |
|
|
<nPhis>20</nPhis> |
464 |
|
|
<ThetaBounds> |
465 |
|
|
<LowerTheta>25</LowerTheta> |
466 |
|
|
<UpperTheta>35</UpperTheta> |
467 |
|
|
</ThetaBounds> |
468 |
|
|
</AngleBasisBlock> |
469 |
|
|
<AngleBasisBlock> |
470 |
|
|
<Theta>40</Theta> |
471 |
|
|
<nPhis>24</nPhis> |
472 |
|
|
<ThetaBounds> |
473 |
|
|
<LowerTheta>35</LowerTheta> |
474 |
|
|
<UpperTheta>45</UpperTheta> |
475 |
|
|
</ThetaBounds> |
476 |
|
|
</AngleBasisBlock> |
477 |
|
|
<AngleBasisBlock> |
478 |
|
|
<Theta>50</Theta> |
479 |
|
|
<nPhis>24</nPhis> |
480 |
|
|
<ThetaBounds> |
481 |
|
|
<LowerTheta>45</LowerTheta> |
482 |
|
|
<UpperTheta>55</UpperTheta> |
483 |
|
|
</ThetaBounds> |
484 |
|
|
</AngleBasisBlock> |
485 |
|
|
<AngleBasisBlock> |
486 |
|
|
<Theta>60</Theta> |
487 |
|
|
<nPhis>24</nPhis> |
488 |
|
|
<ThetaBounds> |
489 |
|
|
<LowerTheta>55</LowerTheta> |
490 |
|
|
<UpperTheta>65</UpperTheta> |
491 |
|
|
</ThetaBounds> |
492 |
|
|
</AngleBasisBlock> |
493 |
|
|
<AngleBasisBlock> |
494 |
|
|
<Theta>70</Theta> |
495 |
|
|
<nPhis>16</nPhis> |
496 |
|
|
<ThetaBounds> |
497 |
|
|
<LowerTheta>65</LowerTheta> |
498 |
|
|
<UpperTheta>75</UpperTheta> |
499 |
|
|
</ThetaBounds> |
500 |
|
|
</AngleBasisBlock> |
501 |
|
|
<AngleBasisBlock> |
502 |
|
|
<Theta>82.5</Theta> |
503 |
|
|
<nPhis>12</nPhis> |
504 |
|
|
<ThetaBounds> |
505 |
|
|
<LowerTheta>75</LowerTheta> |
506 |
|
|
<UpperTheta>90</UpperTheta> |
507 |
|
|
</ThetaBounds> |
508 |
greg |
2.1 |
</AngleBasisBlock> |
509 |
|
|
</AngleBasis> |
510 |
|
|
</DataDefinition> |
511 |
greg |
2.9 |
'; |
512 |
|
|
if ( $doforw ) { |
513 |
greg |
2.15 |
print |
514 |
|
|
' <WavelengthData> |
515 |
greg |
2.9 |
<LayerNumber>System</LayerNumber> |
516 |
|
|
<Wavelength unit="Integral">Visible</Wavelength> |
517 |
|
|
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
518 |
|
|
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
519 |
|
|
<WavelengthDataBlock> |
520 |
|
|
<WavelengthDataDirection>Transmission Front</WavelengthDataDirection> |
521 |
|
|
<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> |
522 |
|
|
<RowAngleBasis>LBNL/Klems Full</RowAngleBasis> |
523 |
|
|
<ScatteringDataType>BTDF</ScatteringDataType> |
524 |
|
|
<ScatteringData> |
525 |
|
|
'; |
526 |
|
|
# Output front transmission (transposed order) |
527 |
|
|
for (my $od = 0; $od < $ndiv; $od++) { |
528 |
|
|
for (my $id = 0; $id < $ndiv; $id++) { |
529 |
greg |
2.29 |
print $tfarr[$ndiv*$id + $od], ",\n"; |
530 |
greg |
2.9 |
} |
531 |
|
|
print "\n"; |
532 |
|
|
} |
533 |
|
|
print |
534 |
greg |
2.15 |
' </ScatteringData> |
535 |
|
|
</WavelengthDataBlock> |
536 |
greg |
2.9 |
</WavelengthData> |
537 |
greg |
2.1 |
<WavelengthData> |
538 |
|
|
<LayerNumber>System</LayerNumber> |
539 |
|
|
<Wavelength unit="Integral">Visible</Wavelength> |
540 |
|
|
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
541 |
|
|
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
542 |
|
|
<WavelengthDataBlock> |
543 |
greg |
2.9 |
<WavelengthDataDirection>Reflection Front</WavelengthDataDirection> |
544 |
|
|
<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> |
545 |
|
|
<RowAngleBasis>LBNL/Klems Full</RowAngleBasis> |
546 |
greg |
2.40 |
<ScatteringDataType>BTDF</ScatteringDataType> |
547 |
greg |
2.9 |
<ScatteringData> |
548 |
|
|
'; |
549 |
greg |
2.31 |
# Output front reflection (transposed order) |
550 |
greg |
2.9 |
for (my $od = 0; $od < $ndiv; $od++) { |
551 |
|
|
for (my $id = 0; $id < $ndiv; $id++) { |
552 |
greg |
2.31 |
print $rfarr[$ndiv*$id + $od], ",\n"; |
553 |
greg |
2.9 |
} |
554 |
|
|
print "\n"; |
555 |
|
|
} |
556 |
|
|
print |
557 |
greg |
2.15 |
' </ScatteringData> |
558 |
|
|
</WavelengthDataBlock> |
559 |
greg |
2.9 |
</WavelengthData> |
560 |
|
|
'; |
561 |
|
|
} |
562 |
|
|
if ( $doback ) { |
563 |
greg |
2.15 |
print |
564 |
|
|
' <WavelengthData> |
565 |
greg |
2.9 |
<LayerNumber>System</LayerNumber> |
566 |
|
|
<Wavelength unit="Integral">Visible</Wavelength> |
567 |
|
|
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
568 |
|
|
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
569 |
|
|
<WavelengthDataBlock> |
570 |
|
|
<WavelengthDataDirection>Transmission Back</WavelengthDataDirection> |
571 |
greg |
2.1 |
<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> |
572 |
|
|
<RowAngleBasis>LBNL/Klems Full</RowAngleBasis> |
573 |
|
|
<ScatteringDataType>BTDF</ScatteringDataType> |
574 |
|
|
<ScatteringData> |
575 |
|
|
'; |
576 |
greg |
2.9 |
# Output back transmission (transposed order) |
577 |
greg |
2.3 |
for (my $od = 0; $od < $ndiv; $od++) { |
578 |
|
|
for (my $id = 0; $id < $ndiv; $id++) { |
579 |
greg |
2.29 |
print $tbarr[$ndiv*$id + $od], ",\n"; |
580 |
greg |
2.9 |
} |
581 |
|
|
print "\n"; |
582 |
|
|
} |
583 |
|
|
print |
584 |
greg |
2.15 |
' </ScatteringData> |
585 |
|
|
</WavelengthDataBlock> |
586 |
greg |
2.9 |
</WavelengthData> |
587 |
|
|
<WavelengthData> |
588 |
|
|
<LayerNumber>System</LayerNumber> |
589 |
|
|
<Wavelength unit="Integral">Visible</Wavelength> |
590 |
|
|
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum> |
591 |
|
|
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum> |
592 |
|
|
<WavelengthDataBlock> |
593 |
|
|
<WavelengthDataDirection>Reflection Back</WavelengthDataDirection> |
594 |
|
|
<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis> |
595 |
|
|
<RowAngleBasis>LBNL/Klems Full</RowAngleBasis> |
596 |
greg |
2.40 |
<ScatteringDataType>BTDF</ScatteringDataType> |
597 |
greg |
2.9 |
<ScatteringData> |
598 |
|
|
'; |
599 |
greg |
2.33 |
# Output back reflection (transposed order) |
600 |
greg |
2.9 |
for (my $od = 0; $od < $ndiv; $od++) { |
601 |
|
|
for (my $id = 0; $id < $ndiv; $id++) { |
602 |
greg |
2.31 |
print $rbarr[$ndiv*$id + $od], ",\n"; |
603 |
greg |
2.3 |
} |
604 |
|
|
print "\n"; |
605 |
|
|
} |
606 |
greg |
2.1 |
print |
607 |
greg |
2.15 |
' </ScatteringData> |
608 |
|
|
</WavelengthDataBlock> |
609 |
greg |
2.1 |
</WavelengthData> |
610 |
greg |
2.9 |
'; |
611 |
|
|
} |
612 |
greg |
2.15 |
} |
613 |
|
|
#------------- End of do_matrix_bsdf() --------------# |