This file is indexed.

/usr/bin/genBSDF is in radiance 4R1+20120125-1.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/usr/bin/perl -w
# RCSid $Id: genBSDF.pl,v 2.28 2011/11/21 20:07:50 greg Exp $
#
# Compute BSDF based on geometry and material description
#
#	G. Ward
#
use strict;
use File::Temp qw/ :mktemp  /;
sub userror {
	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";
	exit 1;
}
my $td = mkdtemp("/tmp/genBSDF.XXXXXX");
chomp $td;
my @savedARGV = @ARGV;
my $tensortree = 0;
my $ttlog2 = 4;
my $nsamp = 2000;
my $rtargs = "-w -ab 5 -ad 700 -lw 3e-6";
my $mgfin = 0;
my $geout = 1;
my $nproc = 1;
my $doforw = 0;
my $doback = 1;
my $pctcull = 95;
my $gunit = "Meter";
my @dim;
# Get options
while ($#ARGV >= 0) {
	if ("$ARGV[0]" =~ /^[-+]m/) {
		$mgfin = ("$ARGV[0]" =~ /^\+/);
	} elsif ("$ARGV[0]" eq "-r") {
		$rtargs = "$rtargs $ARGV[1]";
		shift @ARGV;
	} elsif ("$ARGV[0]" =~ /^[-+]g/) {
		$geout = ("$ARGV[0]" =~ /^\+/);
		$gunit = $ARGV[1];
		if ($gunit !~ /^(?i)(meter|foot|inch|centimeter|millimeter)$/) {
			die "Illegal geometry unit '$gunit': must be meter, foot, inch, centimeter, or millimeter\n";
		}
		shift @ARGV;
	} elsif ("$ARGV[0]" =~ /^[-+]f/) {
		$doforw = ("$ARGV[0]" =~ /^\+/);
	} elsif ("$ARGV[0]" =~ /^[-+]b/) {
		$doback = ("$ARGV[0]" =~ /^\+/);
	} elsif ("$ARGV[0]" eq "-t") {
		$pctcull = $ARGV[1];
		shift @ARGV;
	} elsif ("$ARGV[0]" =~ /^-t[34]$/) {
		$tensortree = substr($ARGV[0], 2, 1);
		$ttlog2 = $ARGV[1];
		shift @ARGV;
	} elsif ("$ARGV[0]" eq "-c") {
		$nsamp = $ARGV[1];
		shift @ARGV;
	} elsif ("$ARGV[0]" eq "-n") {
		$nproc = $ARGV[1];
		shift @ARGV;
	} elsif ("$ARGV[0]" =~ /^-d/) {
		userror() if ($#ARGV < 6);
		@dim = @ARGV[1..6];
		shift @ARGV for (1..6);
	} elsif ("$ARGV[0]" =~ /^[-+]./) {
		userror();
	} else {
		last;
	}
	shift @ARGV;
}
# Check that we're actually being asked to do something
die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback);
# Name our own persist file?
my $persistfile;
if ( $tensortree && $nproc > 1 && "$rtargs" !~ /-PP /) {
	$persistfile = "$td/pfile.txt";
	$rtargs = "-PP $persistfile $rtargs";
}
# Get scene description and dimensions
my $radscn = "$td/device.rad";
my $mgfscn = "$td/device.mgf";
my $octree = "$td/device.oct";
if ( $mgfin ) {
	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";
	die "Could not load MGF input\n" if ( $? );
	system "mgf2rad $mgfscn > $radscn";
} else {
	system "xform -e @ARGV > $radscn";
	die "Could not load Radiance input\n" if ( $? );
	system "rad2mgf $radscn > $mgfscn" if ( $geout );
}
if ($#dim != 5) {
	@dim = split ' ', `getbbox -h $radscn`;
}
print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5);
# Add receiver surfaces (rectangular)
my $fmodnm="receiver_face";
my $bmodnm="receiver_behind";
open(RADSCN, ">> $radscn");
print RADSCN "void glow $fmodnm\n0\n0\n4 1 1 1 0\n\n";
print RADSCN "$fmodnm source f_receiver\n0\n0\n4 0 0 1 180\n";
print RADSCN "void glow $bmodnm\n0\n0\n4 1 1 1 0\n\n";
print RADSCN "$bmodnm source b_receiver\n0\n0\n4 0 0 -1 180\n";
close RADSCN;
# Generate octree
system "oconv -w $radscn > $octree";
die "Could not compile scene\n" if ( $? );
# Output XML prologue
print
'<?xml version="1.0" encoding="UTF-8"?>
<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">
';
print "<!-- File produced by: genBSDF @savedARGV -->\n";
print
'<WindowElementType>System</WindowElementType>
<Optical>
<Layer>
	<Material>
		<Name>Name</Name>
		<Manufacturer>Manufacturer</Manufacturer>
';
printf "\t\t<Thickness unit=\"$gunit\">%.3f</Thickness>\n", $dim[5] - $dim[4];
printf "\t\t<Width unit=\"$gunit\">%.3f</Width>\n", $dim[1] - $dim[0];
printf "\t\t<Height unit=\"$gunit\">%.3f</Height>\n", $dim[3] - $dim[2];
print "\t\t<DeviceType>Integral</DeviceType>\n";
# Output MGF description if requested
if ( $geout ) {
	print "\t\t<Geometry format=\"MGF\" unit=\"$gunit\">\n";
	printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
	open(MGFSCN, "< $mgfscn");
	while (<MGFSCN>) { print $_; }
	close MGFSCN;
	print "xf\n";
	print "\t\t</Geometry>\n";
}
print "	</Material>\n";
# Set up surface sampling
my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + .5);
my $ny = int($nsamp/$nx + .5);
$nsamp = $nx * $ny;
my $ns = 2**$ttlog2;
my (@pdiv, $disk2sq, $sq2disk, $tcal, $kcal);
# Create data segments (all the work happens here)
if ( $tensortree ) {
	do_tree_bsdf();
} else {
	do_matrix_bsdf();
}
# Output XML epilogue
print
'</Layer>
</Optical>
</WindowElement>
';
# Clean up temporary files and exit
exec("rm -rf $td");

#-------------- End of main program segment --------------#

#++++++++++++++ Kill persistent rtrace +++++++++++++++++++#
sub persist_end {
	if ( $persistfile && open(PFI, "< $persistfile") ) {
		while (<PFI>) {
			s/^[^ ]* //;
			kill('ALRM', $_);
			last;
		}
		close PFI;
	}
}

#++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
sub do_tree_bsdf {
# Get sampling rate and subdivide task
my $ns2 = $ns;
$ns2 /= 2 if ( $tensortree == 3 );
my $nsplice = $nproc;
$nsplice *= 10 if ($nproc > 1);
$nsplice = $ns2 if ($nsplice > $ns2);
$nsplice = 999 if ($nsplice > 999);
@pdiv = (0, int($ns2/$nsplice));
my $nrem = $ns2 % $nsplice;
for (my $i = 1; $i < $nsplice; $i++) {
	my $nv = $pdiv[$i] + $pdiv[1];
	++$nv if ( $nrem-- > 0 );
	push @pdiv, $nv;
}
die "Script error 1" if ($pdiv[-1] != $ns2);
# Shirley-Chiu mapping from unit square to disk
$sq2disk = '
in_square_a = 2*in_square_x - 1;
in_square_b = 2*in_square_y - 1;
in_square_rgn = if(in_square_a + in_square_b,
			if(in_square_a - in_square_b, 1, 2),
			if(in_square_b - in_square_a, 3, 4));
out_disk_r = .999995*select(in_square_rgn, in_square_a, in_square_b,
			-in_square_a, -in_square_b);
out_disk_phi = PI/4 * select(in_square_rgn,
				in_square_b/in_square_a,
				2 - in_square_a/in_square_b,
				4 + in_square_b/in_square_a,
				if(in_square_b*in_square_b,
					6 - in_square_a/in_square_b, 0));
Dx = out_disk_r*cos(out_disk_phi);
Dy = out_disk_r*sin(out_disk_phi);
Dz = sqrt(1 - out_disk_r*out_disk_r);
';
# Shirley-Chiu mapping from unit disk to square
$disk2sq = '
norm_radians(p) : if(-p - PI/4, p + 2*PI, p);
in_disk_r = .999995*sqrt(Dx*Dx + Dy*Dy);
in_disk_phi = norm_radians(atan2(Dy, Dx));
in_disk_rgn = floor((in_disk_phi + PI/4)/(PI/2)) + 1;
out_square_a = select(in_disk_rgn,
			in_disk_r,
			(PI/2 - in_disk_phi)*in_disk_r/(PI/4),
			-in_disk_r,
			(in_disk_phi - 3*PI/2)*in_disk_r/(PI/4));
out_square_b = select(in_disk_rgn,
			in_disk_phi*in_disk_r/(PI/4),
			in_disk_r,
			(PI - in_disk_phi)*in_disk_r/(PI/4),
			-in_disk_r);
out_square_x = (out_square_a + 1)/2;
out_square_y = (out_square_b + 1)/2;
';
# Announce ourselves in XML output
print "\t<DataDefinition>\n";
print "\t\t<IncidentDataStructure>TensorTree$tensortree</IncidentDataStructure>\n";
print "\t</DataDefinition>\n";
# Fork parallel rtcontrib processes to compute each side
my $npleft = $nproc;
if ( $doback ) {
	for (my $splice = 0; $splice < $nsplice; $splice++) {
		if (! $npleft ) {
			wait();
			die "rtcontrib process reported error" if ( $? );
			$npleft++;
		}
		bg_tree_rtcontrib(0, $splice);
		$npleft--;
	}
	while (wait() >= 0) {
		die "rtcontrib process reported error" if ( $? );
		$npleft++;
	}
	persist_end();
	ttree_out(0);
}
if ( $doforw ) {
	for (my $splice = 0; $splice < $nsplice; $splice++) {
		if (! $npleft ) {
			wait();
			die "rtcontrib process reported error" if ( $? );
			$npleft++;
		}
		bg_tree_rtcontrib(1, $splice);
		$npleft--;
	}
	while (wait() >= 0) {
		die "rtcontrib process reported error" if ( $? );
		$npleft++;
	}
	persist_end();
	ttree_out(1);
}
}	# end of sub do_tree_bsdf()

# Run rtcontrib process in background to generate tensor tree samples
sub bg_tree_rtcontrib {
	my $pid = fork();
	die "Cannot fork new process" unless defined $pid;
	if ($pid > 0) { return $pid; }
	my $forw = shift;
	my $pn = shift;
	my $pbeg = $pdiv[$pn];
	my $plen = $pdiv[$pn+1] - $pbeg;
	my $matargs = "-m $bmodnm";
	if ( !$forw || !$doback ) { $matargs .= " -m $fmodnm"; }
	my $cmd = "rtcontrib $rtargs -h -ff -fo -c $nsamp " .
		"-e '$disk2sq' -bn '$ns*$ns' " .
		"-b '$ns*floor(out_square_x*$ns)+floor(out_square_y*$ns)' " .
		"-o $td/%s_" . sprintf("%03d", $pn) . ".flt $matargs $octree";
	if ( $tensortree == 3 ) {
		# Isotropic BSDF
		$cmd = "cnt $plen $ny $nx " .
			"| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " .
			"-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " .
			"-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " .
			"-e 'Dx=1-2*($pbeg+\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' " .
			"-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
			"-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
			"-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " .
			"-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " .
			"-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " .
			"| $cmd";
	} else {
		# Anisotropic BSDF
		# Sample area vertically to improve load balance, since
		# shading systems usually have bilateral symmetry (L-R)
		$cmd = "cnt $plen $ns $ny $nx " .
			"| rcalc -e 'r1=rand(($pn+.8681)*recno-.673892)' " .
			"-e 'r2=rand(($pn-5.37138)*recno+67.1737811)' " .
			"-e 'r3=rand(($pn+3.17603772)*recno+83.766771)' " .
			"-e 'r4=rand(($pn-2.3857833)*recno-964.72738)' " .
			"-e 'in_square_x=($pbeg+\$1+r1)/$ns' " .
			"-e 'in_square_y=(\$2+r2)/$ns' -e '$sq2disk' " .
			"-e 'xp=(\$4+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
			"-e 'yp=(\$3+r4)*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
			"-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' " .
			"-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' " .
			"-e '\$4=Dx;\$5=Dy;\$6=myDz' -of " .
			"| $cmd";
	}
# print STDERR "Starting: $cmd\n";
	exec($cmd);		# no return; status report to parent via wait
	die "Cannot exec: $cmd\n";
}	# end of bg_tree_rtcontrib()

# Simplify and output tensor tree results
sub ttree_out {
	my $forw = shift;
	my $side = ("Back","Front")[$forw];
# Only output one transmitted distribution, preferring backwards
if ( !$forw || !$doback ) {
print
'	<WavelengthData>
		<LayerNumber>System</LayerNumber>
		<Wavelength unit="Integral">Visible</Wavelength>
		<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
		<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
		<WavelengthDataBlock>
			<WavelengthDataDirection>Transmission</WavelengthDataDirection>
			<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>
			<ScatteringDataType>BTDF</ScatteringDataType>
			<ScatteringData>
';
system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
	q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } .
	"$td/" . ($bmodnm,$fmodnm)[$forw] . "_???.flt " .
	"| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2";
die "Failure running rttree_reduce" if ( $? );
print
'			</ScatteringData>
		</WavelengthDataBlock>
	</WavelengthData>
';
}
# Output reflection
print
'	<WavelengthData>
		<LayerNumber>System</LayerNumber>
		<Wavelength unit="Integral">Visible</Wavelength>
		<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
		<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
		<WavelengthDataBlock>
';
print "\t\t\t<WavelengthDataDirection>Reflection $side</WavelengthDataDirection>\n";
print
'			<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>
			<ScatteringDataType>BRDF</ScatteringDataType>
			<ScatteringData>
';
system "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
	q{-e '$1=(0.265*$1+0.670*$2+0.065*$3)/Omega' -of } .
	"$td/" . ($fmodnm,$bmodnm)[$forw] . "_???.flt " .
	"| rttree_reduce -a -h -ff -t $pctcull -r $tensortree -g $ttlog2";
die "Failure running rttree_reduce" if ( $? );
print
'			</ScatteringData>
		</WavelengthDataBlock>
	</WavelengthData>
';
}	# end of ttree_out()

#------------- End of do_tree_bsdf() & subroutines -------------#

#+++++++++++++++ Klems matrix BSDF generation +++++++++++++++#
sub do_matrix_bsdf {
# Set up sampling of portal
# Kbin to produce incident direction in full Klems basis with (x1,x2) randoms
$tcal = '
DEGREE : PI/180;
sq(x) : x*x;
Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);
Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0);
Kmax : Kaccum(Knaz(0));
Kfindrow(r, rem) : if(rem-Knaz(r)+.5, Kfindrow(r+1, rem-Knaz(r)), r);
Krow = if(Kbin-(Kmax-.5), 0, Kfindrow(1, Kbin));
Kcol = Kbin - Kaccum(Krow-1);
Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow);
Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1));
sin_kpol = sin(Kpol);
Dx = cos(Kazi)*sin_kpol;
Dy = sin(Kazi)*sin_kpol;
Dz = sqrt(1 - sin_kpol*sin_kpol);
KprojOmega = PI * if(Kbin-.5,
	(sq(cos(Kpola(Krow-1)*DEGREE)) - sq(cos(Kpola(Krow)*DEGREE)))/Knaz(Krow),
	1 - sq(cos(Kpola(1)*DEGREE)));
';
# Compute Klems bin from exiting ray direction (forward or backward)
$kcal = '
DEGREE : PI/180;
abs(x) : if(x, x, -x);
Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x)));
posangle(a) : if(-a, a + 2*PI, a);
Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x));
kpola(r) : select(r, 5, 15, 25, 35, 45, 55, 65, 75, 90);
knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
kaccum(r) : if(r-.5, knaz(r) + kaccum(r-1), 0);
kfindrow(r, pol) : if(r-kpola(0)+.5, r,
		if(pol-kpola(r), kfindrow(r+1, pol), r) );
kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0);
kbin2(pol,azi) = select(kfindrow(1, pol),
		kazn(azi,360/knaz(1)),
		kaccum(1) + kazn(azi,360/knaz(2)),
		kaccum(2) + kazn(azi,360/knaz(3)),
		kaccum(3) + kazn(azi,360/knaz(4)),
		kaccum(4) + kazn(azi,360/knaz(5)),
		kaccum(5) + kazn(azi,360/knaz(6)),
		kaccum(6) + kazn(azi,360/knaz(7)),
		kaccum(7) + kazn(azi,360/knaz(8)),
		kaccum(8) + kazn(azi,360/knaz(9))
	);
kbin = kbin2(Acos(abs(Dz)),Atan2(Dy,Dx));
';
my $ndiv = 145;
# Compute scattering data using rtcontrib
my @tfarr;
my @rfarr;
my @tbarr;
my @rbarr;
my $cmd;
my $rtcmd = "rtcontrib $rtargs -h -ff -fo -n $nproc -c $nsamp " .
	"-e '$kcal' -b kbin -bn $ndiv " .
	"-o '$td/%s.flt' -m $fmodnm -m $bmodnm $octree";
my $rccmd = "rcalc -e '$tcal' " .
	"-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-.999,$ndiv)' " .
	q{-if3 -e 'oval=(0.265*$1+0.670*$2+0.065*$3)/KprojOmega' } .
	q[-o '${  oval  },'];
if ( $doforw ) {
$cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
	"-e 'xp=(\$3+rand(.12*recno+288))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
	"-e 'yp=(\$2+rand(.37*recno-44))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
	"-e 'zp:$dim[4]' " .
	q{-e 'Kbin=$1;x1=rand(2.75*recno+3.1);x2=rand(-2.01*recno-3.37)' } .
	q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp-Dz;$4=Dx;$5=Dy;$6=Dz' } .
	"| $rtcmd";
system "$cmd" || die "Failure running: $cmd\n";
@tfarr = `$rccmd $td/$fmodnm.flt`;
die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
@rfarr = `$rccmd $td/$bmodnm.flt`;
die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
}
if ( $doback ) {
$cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
	"-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
	"-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
	"-e 'zp:$dim[5]' " .
	q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } .
	q{-e '$1=xp-Dx;$2=yp-Dy;$3=zp+Dz;$4=Dx;$5=Dy;$6=-Dz' } .
	"| $rtcmd";
system "$cmd" || die "Failure running: $cmd\n";
@tbarr = `$rccmd $td/$bmodnm.flt`;
die "Failure running: $rccmd $td/$bmodnm.flt\n" if ( $? );
@rbarr = `$rccmd $td/$fmodnm.flt`;
die "Failure running: $rccmd $td/$fmodnm.flt\n" if ( $? );
}
# Output angle basis
print
'	<DataDefinition>
		<IncidentDataStructure>Columns</IncidentDataStructure>
		<AngleBasis>
			<AngleBasisName>LBNL/Klems Full</AngleBasisName>
			<AngleBasisBlock>
				<Theta>0</Theta>
				<nPhis>1</nPhis>
				<ThetaBounds>
					<LowerTheta>0</LowerTheta>
					<UpperTheta>5</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>10</Theta>
				<nPhis>8</nPhis>
				<ThetaBounds>
					<LowerTheta>5</LowerTheta>
					<UpperTheta>15</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>20</Theta>
				<nPhis>16</nPhis>
				<ThetaBounds>
					<LowerTheta>15</LowerTheta>
					<UpperTheta>25</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>30</Theta>
				<nPhis>20</nPhis>
				<ThetaBounds>
					<LowerTheta>25</LowerTheta>
					<UpperTheta>35</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>40</Theta>
				<nPhis>24</nPhis>
				<ThetaBounds>
					<LowerTheta>35</LowerTheta>
					<UpperTheta>45</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>50</Theta>
				<nPhis>24</nPhis>
				<ThetaBounds>
					<LowerTheta>45</LowerTheta>
					<UpperTheta>55</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>60</Theta>
				<nPhis>24</nPhis>
				<ThetaBounds>
					<LowerTheta>55</LowerTheta>
					<UpperTheta>65</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>70</Theta>
				<nPhis>16</nPhis>
				<ThetaBounds>
					<LowerTheta>65</LowerTheta>
					<UpperTheta>75</UpperTheta>
				</ThetaBounds>
				</AngleBasisBlock>
				<AngleBasisBlock>
				<Theta>82.5</Theta>
				<nPhis>12</nPhis>
				<ThetaBounds>
					<LowerTheta>75</LowerTheta>
					<UpperTheta>90</UpperTheta>
				</ThetaBounds>
			</AngleBasisBlock>
		</AngleBasis>
	</DataDefinition>
';
if ( $doforw ) {
print
'	<WavelengthData>
		<LayerNumber>System</LayerNumber>
		<Wavelength unit="Integral">Visible</Wavelength>
		<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
		<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
		<WavelengthDataBlock>
			<WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
			<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
			<RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
			<ScatteringDataType>BTDF</ScatteringDataType>
			<ScatteringData>
';
# Output front transmission (transposed order)
for (my $od = 0; $od < $ndiv; $od++) {
	for (my $id = 0; $id < $ndiv; $id++) {
		print $tfarr[$ndiv*$id + $od];
	}
	print "\n";
}
print
'			</ScatteringData>
		</WavelengthDataBlock>
	</WavelengthData>
	<WavelengthData>
		<LayerNumber>System</LayerNumber>
		<Wavelength unit="Integral">Visible</Wavelength>
		<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
		<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
		<WavelengthDataBlock>
			<WavelengthDataDirection>Reflection Front</WavelengthDataDirection>
			<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
			<RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
			<ScatteringDataType>BRDF</ScatteringDataType>
			<ScatteringData>
';
# Output front reflection (reciprocity averaging)
for (my $od = 0; $od < $ndiv; $od++) {
	for (my $id = 0; $id < $ndiv; $id++) {
		print .5*($rfarr[$ndiv*$id + $od] + $rfarr[$ndiv*$od + $id]);
	}
	print "\n";
}
print
'			</ScatteringData>
		</WavelengthDataBlock>
	</WavelengthData>
';
}
if ( $doback ) {
print
'	<WavelengthData>
		<LayerNumber>System</LayerNumber>
		<Wavelength unit="Integral">Visible</Wavelength>
		<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
		<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
		<WavelengthDataBlock>
			<WavelengthDataDirection>Transmission Back</WavelengthDataDirection>
			<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
			<RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
			<ScatteringDataType>BTDF</ScatteringDataType>
			<ScatteringData>
';
# Output back transmission (transposed order)
for (my $od = 0; $od < $ndiv; $od++) {
	for (my $id = 0; $id < $ndiv; $id++) {
		print $tbarr[$ndiv*$id + $od];
	}
	print "\n";
}
print
'			</ScatteringData>
		</WavelengthDataBlock>
	</WavelengthData>
	<WavelengthData>
		<LayerNumber>System</LayerNumber>
		<Wavelength unit="Integral">Visible</Wavelength>
		<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
		<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
		<WavelengthDataBlock>
			<WavelengthDataDirection>Reflection Back</WavelengthDataDirection>
			<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
			<RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
			<ScatteringDataType>BRDF</ScatteringDataType>
			<ScatteringData>
';
# Output back reflection (reciprocity averaging)
for (my $od = 0; $od < $ndiv; $od++) {
	for (my $id = 0; $id < $ndiv; $id++) {
		print .5*($rbarr[$ndiv*$id + $od] + $rbarr[$ndiv*$od + $id]);
	}
	print "\n";
}
print
'			</ScatteringData>
		</WavelengthDataBlock>
	</WavelengthData>
';
}
}
#------------- End of do_matrix_bsdf() --------------#