ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
Revision: 2.2
Committed: Fri Sep 3 23:53:50 2010 UTC (13 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +8 -4 lines
Log Message:
Working version of genBSDF with detail geometry support in mkillum

File Contents

# User Rev Content
1 greg 2.1 #!/usr/bin/perl -w
2 greg 2.2 # RCSid $Id: genBSDF.pl,v 2.1 2010/09/02 02:29:24 greg Exp $
3 greg 2.1 #
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 ..]";
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 greg 2.2 Kazi = 360*DEGREE * (Kcol + (.5 - x2)) / Knaz(Krow);
86 greg 2.1 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 greg 2.2 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 greg 2.1 ';
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     # Output XML prologue
125     print
126     '<?xml version="1.0" encoding="UTF-8"?>
127     <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">
128     <WindowElementType>System</WindowElementType>
129     <Optical>
130     <Layer>
131     <Material>
132     <Name>Name</Name>
133     <Manufacturer>Manufacturer</Manufacturer>
134     ';
135     printf "\t\t\t<Thickness unit=\"Meter\">%.3f</Thickness>\n", $dim[5] - $dim[4];
136     printf "\t\t\t<Width unit=\"Meter\">%.3f</Width>\n", $dim[1] - $dim[0];
137     printf "\t\t\t<Height unit=\"Meter\">%.3f</Height>\n", $dim[3] - $dim[2];
138     print "\t\t\t<DeviceType>Integral</DeviceType>\n";
139     # Output MGF description if requested
140     if ( $geout ) {
141     print "\t\t\t<Geometry format=\"MGF\" unit=\"Meter\">\n";
142     printf "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
143     system "cat $mgfscn";
144     print "xf\n";
145     print "\t\t\t</Geometry>\n";
146     }
147     print ' </Material>
148     <DataDefinition>
149     <IncidentDataStructure>Columns</IncidentDataStructure>
150     <AngleBasis>
151     <AngleBasisName>LBNL/Klems Full</AngleBasisName>
152     <AngleBasisBlock>
153     <Theta>0</Theta>
154     <nPhis>1</nPhis>
155     <ThetaBounds>
156     <LowerTheta>0</LowerTheta>
157     <UpperTheta>5</UpperTheta>
158     </ThetaBounds>
159     </AngleBasisBlock>
160     <AngleBasisBlock>
161     <Theta>10</Theta>
162     <nPhis>8</nPhis>
163     <ThetaBounds>
164     <LowerTheta>5</LowerTheta>
165     <UpperTheta>15</UpperTheta>
166     </ThetaBounds>
167     </AngleBasisBlock>
168     <AngleBasisBlock>
169     <Theta>20</Theta>
170     <nPhis>16</nPhis>
171     <ThetaBounds>
172     <LowerTheta>15</LowerTheta>
173     <UpperTheta>25</UpperTheta>
174     </ThetaBounds>
175     </AngleBasisBlock>
176     <AngleBasisBlock>
177     <Theta>30</Theta>
178     <nPhis>20</nPhis>
179     <ThetaBounds>
180     <LowerTheta>25</LowerTheta>
181     <UpperTheta>35</UpperTheta>
182     </ThetaBounds>
183     </AngleBasisBlock>
184     <AngleBasisBlock>
185     <Theta>40</Theta>
186     <nPhis>24</nPhis>
187     <ThetaBounds>
188     <LowerTheta>35</LowerTheta>
189     <UpperTheta>45</UpperTheta>
190     </ThetaBounds>
191     </AngleBasisBlock>
192     <AngleBasisBlock>
193     <Theta>50</Theta>
194     <nPhis>24</nPhis>
195     <ThetaBounds>
196     <LowerTheta>45</LowerTheta>
197     <UpperTheta>55</UpperTheta>
198     </ThetaBounds>
199     </AngleBasisBlock>
200     <AngleBasisBlock>
201     <Theta>60</Theta>
202     <nPhis>24</nPhis>
203     <ThetaBounds>
204     <LowerTheta>55</LowerTheta>
205     <UpperTheta>65</UpperTheta>
206     </ThetaBounds>
207     </AngleBasisBlock>
208     <AngleBasisBlock>
209     <Theta>70</Theta>
210     <nPhis>16</nPhis>
211     <ThetaBounds>
212     <LowerTheta>65</LowerTheta>
213     <UpperTheta>75</UpperTheta>
214     </ThetaBounds>
215     </AngleBasisBlock>
216     <AngleBasisBlock>
217     <Theta>82.5</Theta>
218     <nPhis>12</nPhis>
219     <ThetaBounds>
220     <LowerTheta>75</LowerTheta>
221     <UpperTheta>90</UpperTheta>
222     </ThetaBounds>
223     </AngleBasisBlock>
224     </AngleBasis>
225     </DataDefinition>
226     <WavelengthData>
227     <LayerNumber>System</LayerNumber>
228     <Wavelength unit="Integral">Visible</Wavelength>
229     <SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
230     <DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
231     <WavelengthDataBlock>
232     <WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
233     <ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
234     <RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
235     <ScatteringDataType>BTDF</ScatteringDataType>
236     <ScatteringData>
237     ';
238     # Compute actual scattering data using rtcontrib
239     system "cnt $ndiv $ny $nx | rcalc -of -e '$tcal' " .
240     "-e 'xp=(\$3+rand(.35*recno-15))*(($dim[1]-$dim[0])/$nx)+$dim[0]' " .
241     "-e 'yp=(\$2+rand(.86*recno+11))*(($dim[3]-$dim[2])/$ny)+$dim[2]' " .
242     "-e 'zp:$dim[4]-1e-5' " .
243     q{-e 'Kbin=$1;x1=rand(1.21*recno+2.75);x2=rand(-3.55*recno-7.57)' } .
244     q{-e '$1=xp;$2=yp;$3=zp;$4=Dx;$5=Dy;$6=Dz' } .
245     "| rtcontrib -h -ff -n $nproc -c $nsamp -e '$kcal' -b kbin -bn $ndiv " .
246 greg 2.2 "-m $modnm -w -ab 5 -ad 700 -lw 3e-6 $octree " .
247     "| rcalc -e 'x1:.5;x2:.5;$tcal' -e 'Kbin=floor((recno-1)/$ndiv)' " .
248     q{-if3 -e '$1=(0.265*$1+0.670*$2+0.065*$3)/(Komega*Dz)'};
249 greg 2.1 # Output XML epilogue
250     print
251     ' </ScatteringData>
252     </WavelengthDataBlock>
253     </WavelengthData>
254     </Layer>
255     </Optical>
256     </WindowElement>
257     ';
258     # Clean up temporary files
259     system "rm -rf $td";