ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
Revision: 2.4
Committed: Thu Dec 9 06:14:04 2010 UTC (13 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +3 -2 lines
Log Message:
Fixed normalization of computed BSDF

File Contents

# Content
1 #!/usr/bin/perl -w
2 # RCSid $Id: genBSDF.pl,v 2.3 2010/09/09 06:02:16 greg Exp $
3 #
4 # Compute BSDF based on geometry and material description
5 #
6 # G. Ward
7 #
8 use strict;
9 sub userror {
10 print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-dim xmin xmax ymin ymax zmin zmax][{+|-}mgf][{+|-}geom] [input ..]\n";
11 exit 1;
12 }
13 my $td = `mktemp -d /tmp/genBSDF.XXXXXX`;
14 chomp $td;
15 my $nsamp = 1000;
16 my $mgfin = 0;
17 my $geout = 1;
18 my $nproc = 1;
19 my @dim;
20 # Get options
21 while ($#ARGV >= 0) {
22 if ("$ARGV[0]" =~ /^[-+]m/) {
23 $mgfin = ("$ARGV[0]" =~ /^\+/);
24 } elsif ("$ARGV[0]" =~ /^[-+]g/) {
25 $geout = ("$ARGV[0]" =~ /^\+/);
26 } elsif ("$ARGV[0]" eq "-c") {
27 $nsamp = $ARGV[1];
28 shift @ARGV;
29 } elsif ("$ARGV[0]" eq "-n") {
30 $nproc = $ARGV[1];
31 shift @ARGV;
32 } elsif ("$ARGV[0]" =~ /^-d/) {
33 userror() if ($#ARGV < 6);
34 @dim = "@ARGV[1..6]";
35 shift @ARGV for (1..6);
36 } elsif ("$ARGV[0]" =~ /^[-+]./) {
37 userror();
38 } else {
39 last;
40 }
41 shift @ARGV;
42 }
43 # Get scene description and dimensions
44 my $radscn = "$td/device.rad";
45 my $mgfscn = "$td/device.mgf";
46 my $octree = "$td/device.oct";
47 if ( $mgfin ) {
48 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";
49 die "Could not load MGF input\n" if ( $? );
50 system "mgf2rad $mgfscn > $radscn";
51 } else {
52 system "cat @ARGV | xform -e > $radscn";
53 die "Could not load Radiance input\n" if ( $? );
54 system "rad2mgf $radscn > $mgfscn" if ( $geout );
55 }
56 if ($#dim != 5) {
57 @dim = split /\s+/, `getbbox -h $radscn`;
58 shift @dim;
59 }
60 print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5);
61 # Add receiver surface (rectangle)
62 my $modnm="_receiver_black_";
63 open(RADSCN, ">> $radscn");
64 print RADSCN "void glow $modnm\n0\n0\n4 0 0 0 0\n\n";
65 print RADSCN "$modnm polygon _receiver_\n0\n0\n12\n";
66 print RADSCN "\t",$dim[0],"\t",$dim[2],"\t",$dim[5]+1e-5,"\n";
67 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";
70 close RADSCN;
71 # Generate octree
72 system "oconv -w $radscn > $octree";
73 die "Could not compile scene\n" if ( $? );
74 # Set up sampling
75 # Kbin to produce incident direction in full Klems basis with (x1,x2) randoms
76 my $tcal = '
77 DEGREE : PI/180;
78 Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90);
79 Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
80 Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0);
81 Kmax : Kaccum(Knaz(0));
82 Kfindrow(r, rem) : if(rem-Knaz(r)+.5, Kfindrow(r+1, rem-Knaz(r)), r);
83 Krow = if(Kbin-(Kmax-.5), 0, Kfindrow(1, Kbin));
84 Kcol = Kbin - Kaccum(Krow-1);
85 Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow);
86 Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1));
87 sin_kpol = sin(Kpol);
88 Dx = -cos(Kazi)*sin_kpol;
89 Dy = sin(Kazi)*sin_kpol;
90 Dz = sqrt(1 - sin_kpol*sin_kpol);
91 Komega = 2*PI*if(Kbin-.5,
92 (cos(Kpola(Krow-1)*DEGREE) - cos(Kpola(Krow)*DEGREE))/Knaz(Krow),
93 1 - cos(Kpola(1)*DEGREE));
94 ';
95 # Compute Klems bin from exiting ray direction
96 my $kcal = '
97 DEGREE : PI/180;
98 Acos(x) : 1/DEGREE * if(x-1, 0, if(-1-x, 0, acos(x)));
99 posangle(a) : if(-a, a + 2*PI, a);
100 Atan2(y,x) : 1/DEGREE * posangle(atan2(y,x));
101 kpola(r) : select(r, 5, 15, 25, 35, 45, 55, 65, 75, 90);
102 knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12);
103 kaccum(r) : if(r-.5, knaz(r) + kaccum(r-1), 0);
104 kfindrow(r, pol) : if(r-kpola(0)+.5, r,
105 if(pol-kpola(r), kfindrow(r+1, pol), r) );
106 kazn(azi,inc) : if((360-.5*inc)-azi, floor((azi+.5*inc)/inc), 0);
107 kbin2(pol,azi) = select(kfindrow(1, pol),
108 kazn(azi,360/knaz(1)),
109 kaccum(1) + kazn(azi,360/knaz(2)),
110 kaccum(2) + kazn(azi,360/knaz(3)),
111 kaccum(3) + kazn(azi,360/knaz(4)),
112 kaccum(4) + kazn(azi,360/knaz(5)),
113 kaccum(5) + kazn(azi,360/knaz(6)),
114 kaccum(6) + kazn(azi,360/knaz(7)),
115 kaccum(7) + kazn(azi,360/knaz(8)),
116 kaccum(8) + kazn(azi,360/knaz(9))
117 );
118 kbin = kbin2(Acos(Dz), Atan2(Dy, -Dx));
119 ';
120 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;
124 # Compute scattering data using rtcontrib
125 my $cmd = "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
126 "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
127 "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
128 "-e 'zp:$dim[4]-1e-5' " .
129 q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } .
130 q{-e '$1=xp;$2=yp;$3=zp;$4=Dx;$5=Dy;$6=Dz' } .
131 "| rtcontrib -h -ff -n $nproc -c $nsamp -e '$kcal' -b kbin -bn $ndiv " .
132 "-m $modnm -w -ab 5 -ad 700 -lw 3e-6 $octree " .
133 "| rcalc -e 'x1:.5;x2:.5;$tcal' " .
134 "-e 'mod(n,d):n-floor(n/d)*d' -e 'Kbin=mod(recno-1,$ndiv)' " .
135 q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/(Komega*Dz)'};
136 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";
160 }
161 print ' </Material>
162 <DataDefinition>
163 <IncidentDataStructure>Columns</IncidentDataStructure>
164 <AngleBasis>
165 <AngleBasisName>LBNL/Klems Full</AngleBasisName>
166 <AngleBasisBlock>
167 <Theta>0</Theta>
168 <nPhis>1</nPhis>
169 <ThetaBounds>
170 <LowerTheta>0</LowerTheta>
171 <UpperTheta>5</UpperTheta>
172 </ThetaBounds>
173 </AngleBasisBlock>
174 <AngleBasisBlock>
175 <Theta>10</Theta>
176 <nPhis>8</nPhis>
177 <ThetaBounds>
178 <LowerTheta>5</LowerTheta>
179 <UpperTheta>15</UpperTheta>
180 </ThetaBounds>
181 </AngleBasisBlock>
182 <AngleBasisBlock>
183 <Theta>20</Theta>
184 <nPhis>16</nPhis>
185 <ThetaBounds>
186 <LowerTheta>15</LowerTheta>
187 <UpperTheta>25</UpperTheta>
188 </ThetaBounds>
189 </AngleBasisBlock>
190 <AngleBasisBlock>
191 <Theta>30</Theta>
192 <nPhis>20</nPhis>
193 <ThetaBounds>
194 <LowerTheta>25</LowerTheta>
195 <UpperTheta>35</UpperTheta>
196 </ThetaBounds>
197 </AngleBasisBlock>
198 <AngleBasisBlock>
199 <Theta>40</Theta>
200 <nPhis>24</nPhis>
201 <ThetaBounds>
202 <LowerTheta>35</LowerTheta>
203 <UpperTheta>45</UpperTheta>
204 </ThetaBounds>
205 </AngleBasisBlock>
206 <AngleBasisBlock>
207 <Theta>50</Theta>
208 <nPhis>24</nPhis>
209 <ThetaBounds>
210 <LowerTheta>45</LowerTheta>
211 <UpperTheta>55</UpperTheta>
212 </ThetaBounds>
213 </AngleBasisBlock>
214 <AngleBasisBlock>
215 <Theta>60</Theta>
216 <nPhis>24</nPhis>
217 <ThetaBounds>
218 <LowerTheta>55</LowerTheta>
219 <UpperTheta>65</UpperTheta>
220 </ThetaBounds>
221 </AngleBasisBlock>
222 <AngleBasisBlock>
223 <Theta>70</Theta>
224 <nPhis>16</nPhis>
225 <ThetaBounds>
226 <LowerTheta>65</LowerTheta>
227 <UpperTheta>75</UpperTheta>
228 </ThetaBounds>
229 </AngleBasisBlock>
230 <AngleBasisBlock>
231 <Theta>82.5</Theta>
232 <nPhis>12</nPhis>
233 <ThetaBounds>
234 <LowerTheta>75</LowerTheta>
235 <UpperTheta>90</UpperTheta>
236 </ThetaBounds>
237 </AngleBasisBlock>
238 </AngleBasis>
239 </DataDefinition>
240 <WavelengthData>
241 <LayerNumber>System</LayerNumber>
242 <Wavelength unit="Integral">Visible</Wavelength>
243 <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
244 <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
245 <WavelengthDataBlock>
246 <WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
247 <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
248 <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
249 <ScatteringDataType>BTDF</ScatteringDataType>
250 <ScatteringData>
251 ';
252 # Output computed data (transposed order)
253 for (my $od = 0; $od < $ndiv; $od++) {
254 for (my $id = 0; $id < $ndiv; $id++) {
255 print $darr[$ndiv*$id + $od];
256 }
257 print "\n";
258 }
259 # Output XML epilogue
260 print
261 ' </ScatteringData>
262 </WavelengthDataBlock>
263 </WavelengthData>
264 </Layer>
265 </Optical>
266 </WindowElement>
267 ';
268 # Clean up temporary files
269 system "rm -rf $td";