ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
Revision: 2.55
Committed: Fri Mar 27 18:58:06 2015 UTC (9 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.54: +14 -10 lines
Log Message:
Hopeful fix to bugs in genBSDF due to lack of left-handed coordinates

File Contents

# Content
1 #!/usr/bin/perl -w
2 # RCSid $Id: genBSDF.pl,v 2.54 2015/03/25 22:50:48 greg Exp $
3 #
4 # Compute BSDF based on geometry and material description
5 #
6 # G. Ward
7 #
8 use strict;
9 my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64");
10 use File::Temp qw/ :mktemp /;
11 sub userror {
12 print STDERR "Usage: genBSDF [-n Nproc][-c Nsamp][-W][-t{3|4} Nlog2][-r \"ropts\"][-f \"x=string;y=string\"][-dim xmin xmax ymin ymax zmin zmax][{+|-}C][{+|-}f][{+|-}b][{+|-}mgf][{+|-}geom units] [input ..]\n";
13 exit 1;
14 }
15 my ($td,$radscn,$mgfscn,$octree,$fsender,$bsender,$receivers,$facedat,$behinddat,$rmtmp);
16 my ($tf,$rf,$tb,$rb,$tfx,$rfx,$tbx,$rbx,$tfz,$rfz,$tbz,$rbz);
17 if ($windoz) {
18 my $tmploc = `echo \%TMP\%`;
19 chomp($tmploc);
20 $td = mkdtemp("$tmploc\\genBSDF.XXXXXX");
21 $radscn = "$td\\device.rad";
22 $mgfscn = "$td\\device.mgf";
23 $octree = "$td\\device.oct";
24 $fsender = "$td\\fsender.rad";
25 $bsender = "$td\\bsender.rad";
26 $receivers = "$td\\receivers.rad";
27 $facedat = "$td\\face.dat";
28 $behinddat = "$td\\behind.dat";
29 $tf = "$td\\tf.dat";
30 $rf = "$td\\rf.dat";
31 $tb = "$td\\tb.dat";
32 $rb = "$td\\rb.dat";
33 $tfx = "$td\\tfx.dat";
34 $rfx = "$td\\rfx.dat";
35 $tbx = "$td\\tbx.dat";
36 $rbx = "$td\\rbx.dat";
37 $tfz = "$td\\tfz.dat";
38 $rfz = "$td\\rfz.dat";
39 $tbz = "$td\\tbz.dat";
40 $rbz = "$td\\rbz.dat";
41 chomp $td;
42 $rmtmp = "rmdir /S /Q $td";
43 } else {
44 $td = mkdtemp("/tmp/genBSDF.XXXXXX");
45 chomp $td;
46 $radscn = "$td/device.rad";
47 $mgfscn = "$td/device.mgf";
48 $octree = "$td/device.oct";
49 $fsender = "$td/fsender.rad";
50 $bsender = "$td/bsender.rad";
51 $receivers = "$td/receivers.rad";
52 $facedat = "$td/face.dat";
53 $behinddat = "$td/behind.dat";
54 $tf = "$td/tf.dat";
55 $rf = "$td/rf.dat";
56 $tb = "$td/tb.dat";
57 $rb = "$td/rb.dat";
58 $tfx = "$td/tfx.dat";
59 $rfx = "$td/rfx.dat";
60 $tbx = "$td/tbx.dat";
61 $rbx = "$td/rbx.dat";
62 $tfz = "$td/tfz.dat";
63 $rfz = "$td/rfz.dat";
64 $tbz = "$td/tbz.dat";
65 $rbz = "$td/rbz.dat";
66 $rmtmp = "rm -rf $td";
67 }
68 my @savedARGV = @ARGV;
69 my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6";
70 my $wrapper = "wrapBSDF";
71 my $tensortree = 0;
72 my $ttlog2 = 4;
73 my $nsamp = 2000;
74 my $mgfin = 0;
75 my $geout = 1;
76 my $nproc = 1;
77 my $docolor = 0;
78 my $doforw = 0;
79 my $doback = 1;
80 my $pctcull = 90;
81 my $gunit = "meter";
82 my $curspec = "Visible";
83 my @dim;
84 # Get options
85 while ($#ARGV >= 0) {
86 if ("$ARGV[0]" =~ /^[-+]m/) {
87 $mgfin = ("$ARGV[0]" =~ /^\+/);
88 } elsif ("$ARGV[0]" eq "-r") {
89 $rfluxmtx .= " $ARGV[1]";
90 shift @ARGV;
91 } elsif ("$ARGV[0]" =~ /^[-+]g/) {
92 $geout = ("$ARGV[0]" =~ /^\+/);
93 $gunit = $ARGV[1];
94 if ($gunit !~ /^(?i)(meter|foot|inch|centimeter|millimeter)$/) {
95 die "Illegal geometry unit '$gunit': must be meter, foot, inch, centimeter, or millimeter\n";
96 }
97 shift @ARGV;
98 } elsif ("$ARGV[0]" =~ /^[-+]C/) {
99 $docolor = ("$ARGV[0]" =~ /^\+/);
100 } elsif ("$ARGV[0]" =~ /^[-+]f/) {
101 $doforw = ("$ARGV[0]" =~ /^\+/);
102 } elsif ("$ARGV[0]" =~ /^[-+]b/) {
103 $doback = ("$ARGV[0]" =~ /^\+/);
104 } elsif ("$ARGV[0]" eq "-t") {
105 # Use value < 0 for rttree_reduce bypass
106 $pctcull = $ARGV[1];
107 if ($pctcull >= 100) {
108 die "Illegal -t culling percentage, must be < 100\n";
109 }
110 shift @ARGV;
111 } elsif ("$ARGV[0]" =~ /^-t[34]$/) {
112 $tensortree = substr($ARGV[0], 2, 1);
113 $ttlog2 = $ARGV[1];
114 shift @ARGV;
115 } elsif ("$ARGV[0]" eq "-s") {
116 $wrapper .= " -f \"$ARGV[1]\"";
117 shift @ARGV;
118 } elsif ("$ARGV[0]" eq "-W") {
119 $wrapper .= " -W";
120 } elsif ("$ARGV[0]" eq "-c") {
121 $nsamp = $ARGV[1];
122 shift @ARGV;
123 } elsif ("$ARGV[0]" eq "-n") {
124 $nproc = $ARGV[1];
125 shift @ARGV;
126 } elsif ("$ARGV[0]" =~ /^-d/) {
127 userror() if ($#ARGV < 6);
128 @dim = @ARGV[1..6];
129 shift @ARGV for (1..6);
130 } elsif ("$ARGV[0]" =~ /^[-+]./) {
131 userror();
132 } else {
133 last;
134 }
135 shift @ARGV;
136 }
137 # Check that we're actually being asked to do something
138 die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback);
139 # Issue warning for unhandled reciprocity case
140 print STDERR "Warning: recommend both +forward and +backward with -t3\n" if
141 ($tensortree==3 && !($doforw && $doback));
142 $wrapper .= $tensortree ? " -a t$tensortree" : " -a kf -c";
143 $wrapper .= " -u $gunit";
144 # Get scene description and dimensions
145 if ( $mgfin ) {
146 system "mgf2rad @ARGV > $radscn";
147 die "Could not load MGF input\n" if ( $? );
148 } else {
149 system "xform -e @ARGV > $radscn";
150 die "Could not load Radiance input\n" if ( $? );
151 }
152 if ($#dim != 5) {
153 @dim = split ' ', `getbbox -h $radscn`;
154 }
155 print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5);
156 $wrapper .= ' -f "t=' . ($dim[5] - $dim[4]) . ';w=' . ($dim[1] - $dim[0]) .
157 ';h=' . ($dim[3] - $dim[2]) . '"';
158 # Generate octree
159 system "oconv -w $radscn > $octree";
160 die "Could not compile scene\n" if ( $? );
161 # Add MGF description if requested
162 if ( $geout ) {
163 open(MGFSCN, "> $mgfscn");
164 printf MGFSCN "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
165 close MGFSCN;
166 if ( $mgfin ) {
167 system qq{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};
168 } else {
169 system "rad2mgf $radscn >> $mgfscn";
170 }
171 open(MGFSCN, ">> $mgfscn");
172 print MGFSCN "xf\n";
173 close MGFSCN;
174 $wrapper .= " -g $mgfscn";
175 }
176 # Create receiver & sender surfaces (rectangular)
177 my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1);
178 my $ny = int($nsamp/$nx + 1);
179 $nsamp = $nx * $ny;
180 my $ns = 2**$ttlog2;
181 open(RADSCN, "> $receivers");
182 print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=+sc$ns\n" : "h=+kf\n");
183 print RADSCN '#@rfluxmtx ' . "u=Y o=$facedat\n\n";
184 print RADSCN "void glow receiver_face\n0\n0\n4 1 1 1 0\n\n";
185 print RADSCN "receiver_face source f_receiver\n0\n0\n4 0 0 1 180\n\n";
186 print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=-sc$ns\n" : "h=-kf\n");
187 print RADSCN '#@rfluxmtx ' . "u=Y o=$behinddat\n\n";
188 print RADSCN "void glow receiver_behind\n0\n0\n4 1 1 1 0\n\n";
189 print RADSCN "receiver_behind source b_receiver\n0\n0\n4 0 0 -1 180\n";
190 close RADSCN;
191 # Finish rfluxmtx command and prepare sender surfaces
192 $rfluxmtx .= " -n $nproc -c $nsamp";
193 if ( $tensortree != 3 ) { # Isotropic tensor tree is exception
194 open (RADSCN, "> $fsender");
195 print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=+sc$ns\n\n" : "h=+kf\n\n");
196 print RADSCN "void polygon fwd_sender\n0\n0\n12\n";
197 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[4];
198 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[4];
199 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[4];
200 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[4];
201 close RADSCN;
202 open (RADSCN, "> $bsender");
203 print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=-sc$ns\n\n" : "h=-kf\n\n");
204 print RADSCN "void polygon bwd_sender\n0\n0\n12\n";
205 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[5];
206 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[5];
207 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[5];
208 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[5];
209 close RADSCN;
210 }
211 # Calculate CIE (u',v') from Radiance RGB:
212 my $CIEuv = 'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' .
213 'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' .
214 'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' .
215 'den=Xi+15*Yi+3*Zi;' .
216 'uprime=4*Xi/den;vprime=9*Yi/den;' ;
217 # Create data segments (all the work happens here)
218 if ( $tensortree ) {
219 do_tree_bsdf();
220 } else {
221 do_matrix_bsdf();
222 }
223 # Output XML
224 my $old_fh = select(STDOUT);
225 $| = 1;
226 select($old_fh);
227 print "<!-- Created by: genBSDF @savedARGV -->\n";
228 # print STDERR "Running: $wrapper\n";
229 system $wrapper;
230 die "Could not wrap BSDF data\n" if ( $? );
231 # Clean up temporary files and exit
232 exec $rmtmp;
233
234 #-------------- End of main program segment --------------#
235
236 #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
237 sub do_tree_bsdf {
238
239 # Run rfluxmtx processes to compute each side
240 do_ttree_dir(0) if ( $doback );
241 do_ttree_dir(1) if ( $doforw );
242
243 } # end of sub do_tree_bsdf()
244
245 # Call rfluxmtx and process tensor tree BSDF for the given direction
246 sub do_ttree_dir {
247 my $forw = shift;
248 my $cmd;
249 if ( $tensortree == 3 ) {
250 # Isotropic BSDF
251 my $ns2 = $ns / 2;
252 if ($windoz) {
253 $cmd = "cnt $ns2 $ny $nx " .
254 qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
255 qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
256 qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
257 qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
258 qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
259 qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
260 qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
261 qq{-e "\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz" } .
262 qq{-e "\$4=Dx;\$5=Dy;\$6=myDz" } .
263 "| $rfluxmtx -fa -y $ns2 - $receivers -i $octree";
264 } else {
265 $cmd = "cnt $ns2 $ny $nx " .
266 qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
267 qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
268 qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
269 qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
270 qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
271 qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
272 qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
273 qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } .
274 qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } .
275 "| $rfluxmtx -h -ff -y $ns2 - $receivers -i $octree";
276 }
277 } else {
278 # Anisotropic BSDF
279 my $sender = ($bsender,$fsender)[$forw];
280 if ($windoz) {
281 $cmd = "$rfluxmtx -fa $sender $receivers -i $octree";
282 } else {
283 $cmd = "$rfluxmtx -h -ff $sender $receivers -i $octree";
284 }
285 }
286 # print STDERR "Starting: $cmd\n";
287 system $cmd;
288 die "Failure running rfluxmtx" if ( $? );
289 ttree_out($forw);
290 } # end of do_ttree_dir()
291
292 # Simplify and store tensor tree results
293 sub ttree_out {
294 my $forw = shift;
295 my ($refldat,$transdat);
296 if ( $forw ) {
297 $transdat = $facedat;
298 $refldat = $behinddat;
299 } else {
300 $transdat = $behinddat;
301 $refldat = $facedat;
302 }
303 # Only output one transmitted anisotropic distribution, preferring backwards
304 if ( !$forw || !$doback || $tensortree==3 ) {
305 my $ttyp = ("tb","tf")[$forw];
306 ttree_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
307 if ( $docolor ) {
308 ttree_comp($ttyp, "CIE-u", $transdat, ($tbx,$tfx)[$forw]);
309 ttree_comp($ttyp, "CIE-v", $transdat, ($tbz,$tfz)[$forw]);
310 }
311 }
312 # Output reflection
313 my $rtyp = ("rb","rf")[$forw];
314 ttree_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
315 if ( $docolor ) {
316 ttree_comp($rtyp, "CIE-u", $refldat, ($rbx,$rfx)[$forw]);
317 ttree_comp($rtyp, "CIE-v", $refldat, ($rbz,$rfz)[$forw]);
318 }
319 } # end of ttree_out()
320
321 # Call rttree_reduce on the given component
322 sub ttree_comp {
323 my $typ = shift;
324 my $spec = shift;
325 my $src = shift;
326 my $dest = shift;
327 my $cmd;
328 if ($windoz) {
329 if ("$spec" eq "Visible") {
330 $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
331 q{-e "Ri=$1;Gi=$2;Bi=$3" } .
332 qq{-e "$CIEuv" } .
333 q{-e "$1=Yi/Omega"};
334 } elsif ("$spec" eq "CIE-u") {
335 $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } .
336 qq{-e "$CIEuv" } .
337 q{-e "$1=uprime"};
338 } elsif ("$spec" eq "CIE-v") {
339 $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } .
340 qq{-e "$CIEuv" } .
341 q{-e "$1=vprime"};
342 }
343 } else {
344 if ("$spec" eq "Visible") {
345 $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
346 q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
347 "-e '$CIEuv' " .
348 q{-e '$1=Yi/Omega'};
349 } elsif ("$spec" eq "CIE-u") {
350 $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } .
351 "-e '$CIEuv' " .
352 q{-e '$1=uprime'};
353 } elsif ("$spec" eq "CIE-v") {
354 $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } .
355 "-e '$CIEuv' " .
356 q{-e '$1=vprime'};
357 }
358 }
359 if ($pctcull >= 0) {
360 my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : "";
361 my $pcull = ("$spec" eq "Visible") ? $pctcull :
362 (100 - (100-$pctcull)*.25) ;
363 if ($windoz) {
364 $cmd = "rcollate -ho -oc 1 $src | " .
365 $cmd .
366 " | rttree_reduce$avg -h -fa -t $pcull -r $tensortree -g $ttlog2";
367 } else {
368 $cmd .= " -of $src " .
369 "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2";
370 }
371 # print STDERR "Running: $cmd\n";
372 system "$cmd > $dest";
373 die "Failure running rttree_reduce" if ( $? );
374 } else {
375 if ($windoz) {
376 $cmd = "rcollate -ho -oc 1 $src | " . $cmd ;
377 } else {
378 $cmd .= " $src";
379 }
380 open(DATOUT, "> $dest");
381 print DATOUT "{\n";
382 close DATOUT;
383 # print STDERR "Running: $cmd\n";
384 system "$cmd >> $dest";
385 die "Failure running rcalc" if ( $? );
386 open(DATOUT, ">> $dest");
387 for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) {
388 print DATOUT "0\n";
389 }
390 print DATOUT "}\n";
391 close DATOUT;
392 }
393 if ( "$spec" ne "$curspec" ) {
394 $wrapper .= " -s $spec";
395 $curspec = $spec;
396 }
397 $wrapper .= " -$typ $dest";
398 } # end of ttree_comp()
399
400 #------------- End of do_tree_bsdf() & subroutines -------------#
401
402 #+++++++++++++++ Klems matrix BSDF generation +++++++++++++++#
403 sub do_matrix_bsdf {
404
405 # Run rfluxmtx processes to compute each side
406 do_matrix_dir(0) if ( $doback );
407 do_matrix_dir(1) if ( $doforw );
408
409 } # end of sub do_matrix_bsdf()
410
411 # Call rfluxmtx and process tensor tree BSDF for the given direction
412 sub do_matrix_dir {
413 my $forw = shift;
414 my $cmd;
415 my $sender = ($bsender,$fsender)[$forw];
416 $cmd = "$rfluxmtx -fd $sender $receivers -i $octree";
417 # print STDERR "Starting: $cmd\n";
418 system $cmd;
419 die "Failure running rfluxmtx" if ( $? );
420 matrix_out($forw);
421 } # end of do_matrix_dir()
422
423 sub matrix_out {
424 my $forw = shift;
425 my ($refldat,$transdat);
426 if ( $forw ) {
427 $transdat = $facedat;
428 $refldat = $behinddat;
429 } else {
430 $transdat = $behinddat;
431 $refldat = $facedat;
432 }
433 # Output transmission
434 my $ttyp = ("tb","tf")[$forw];
435 matrix_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
436 if ( $docolor ) {
437 matrix_comp($ttyp, "CIE-X", $transdat, ($tbx,$tfx)[$forw]);
438 matrix_comp($ttyp, "CIE-Z", $transdat, ($tbz,$tfz)[$forw]);
439 }
440 # Output reflection
441 my $rtyp = ("rb","rf")[$forw];
442 matrix_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
443 if ( $docolor ) {
444 matrix_comp($rtyp, "CIE-X", $refldat, ($rbx,$rfx)[$forw]);
445 matrix_comp($rtyp, "CIE-Z", $refldat, ($rbz,$rfz)[$forw]);
446 }
447 } # end of matrix_out()
448
449 # Transpose matrix component data and save to file
450 sub matrix_comp {
451 my $typ = shift;
452 my $spec = shift;
453 my $src = shift;
454 my $dest = shift;
455 my $cmd = "rmtxop -fa -t";
456 if ("$spec" eq "Visible") {
457 $cmd .= " -c 0.2651 0.6701 0.0648";
458 } elsif ("$spec" eq "CIE-X") {
459 $cmd .= " -c 0.5141 0.3239 0.1620";
460 } elsif ("$spec" eq "CIE-Z") {
461 $cmd .= " -c 0.0241 0.1229 0.8530";
462 }
463 $cmd .= " $src | rcollate -ho -oc 145";
464 # print STDERR "Running: $cmd\n";
465 system "$cmd > $dest";
466 die "Failure running rttree_reduce" if ( $? );
467 if ( "$spec" ne "$curspec" ) {
468 $wrapper .= " -s $spec";
469 $curspec = $spec;
470 }
471 $wrapper .= " -$typ $dest";
472 } # end of matrix_comp()
473
474 #------------- End of do_matrix_bsdf() & subroutines --------------#