ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
Revision: 2.57
Committed: Thu Apr 2 17:42:46 2015 UTC (9 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.56: +2 -2 lines
Log Message:
Minor fix to usage message

File Contents

# Content
1 #!/usr/bin/perl -w
2 # RCSid $Id: genBSDF.pl,v 2.56 2015/04/01 01:38:35 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\"][-s \"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 $FEPS = 1e-5;
178 my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1);
179 my $ny = int($nsamp/$nx + 1);
180 $nsamp = $nx * $ny;
181 my $ns = 2**$ttlog2;
182 open(RADSCN, "> $receivers");
183 print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=-sc$ns\n" : "h=-kf\n");
184 print RADSCN '#@rfluxmtx ' . "u=-Y o=$facedat\n\n";
185 print RADSCN "void glow receiver_face\n0\n0\n4 1 1 1 0\n\n";
186 print RADSCN "receiver_face source f_receiver\n0\n0\n4 0 0 1 180\n\n";
187 print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=+sc$ns\n" : "h=+kf\n");
188 print RADSCN '#@rfluxmtx ' . "u=-Y o=$behinddat\n\n";
189 print RADSCN "void glow receiver_behind\n0\n0\n4 1 1 1 0\n\n";
190 print RADSCN "receiver_behind source b_receiver\n0\n0\n4 0 0 -1 180\n";
191 close RADSCN;
192 # Finish rfluxmtx command and prepare sender surfaces
193 $rfluxmtx .= " -n $nproc -c $nsamp";
194 if ( $tensortree != 3 ) { # Isotropic tensor tree is exception
195 open (RADSCN, "> $fsender");
196 print RADSCN '#@rfluxmtx u=-Y ' . ($tensortree ? "h=-sc$ns\n\n" : "h=-kf\n\n");
197 print RADSCN "void polygon fwd_sender\n0\n0\n12\n";
198 printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[2], $dim[4]-$FEPS;
199 printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[3], $dim[4]-$FEPS;
200 printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[3], $dim[4]-$FEPS;
201 printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[2], $dim[4]-$FEPS;
202 close RADSCN;
203 open (RADSCN, "> $bsender");
204 print RADSCN '#@rfluxmtx u=-Y ' . ($tensortree ? "h=+sc$ns\n\n" : "h=+kf\n\n");
205 print RADSCN "void polygon bwd_sender\n0\n0\n12\n";
206 printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[2], $dim[5]+$FEPS;
207 printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[2], $dim[5]+$FEPS;
208 printf RADSCN "\t%e\t%e\t%e\n", $dim[1], $dim[3], $dim[5]+$FEPS;
209 printf RADSCN "\t%e\t%e\t%e\n", $dim[0], $dim[3], $dim[5]+$FEPS;
210 close RADSCN;
211 }
212 # Calculate CIE (u',v') from Radiance RGB:
213 my $CIEuv = 'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' .
214 'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' .
215 'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' .
216 'den=Xi+15*Yi+3*Zi;' .
217 'uprime=4*Xi/den;vprime=9*Yi/den;' ;
218 # Create data segments (all the work happens here)
219 if ( $tensortree ) {
220 do_tree_bsdf();
221 } else {
222 do_matrix_bsdf();
223 }
224 # Output XML
225 my $old_fh = select(STDOUT);
226 $| = 1;
227 select($old_fh);
228 print "<!-- Created by: genBSDF @savedARGV -->\n";
229 # print STDERR "Running: $wrapper\n";
230 system $wrapper;
231 die "Could not wrap BSDF data\n" if ( $? );
232 # Clean up temporary files and exit
233 exec $rmtmp;
234
235 #-------------- End of main program segment --------------#
236
237 #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
238 sub do_tree_bsdf {
239
240 # Run rfluxmtx processes to compute each side
241 do_ttree_dir(0) if ( $doback );
242 do_ttree_dir(1) if ( $doforw );
243
244 } # end of sub do_tree_bsdf()
245
246 # Call rfluxmtx and process tensor tree BSDF for the given direction
247 sub do_ttree_dir {
248 my $forw = shift;
249 my $cmd;
250 if ( $tensortree == 3 ) {
251 # Isotropic BSDF
252 my $ns2 = $ns / 2;
253 if ($windoz) {
254 $cmd = "cnt $ns2 $ny $nx " .
255 qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
256 qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
257 qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
258 qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
259 qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
260 qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
261 qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
262 qq{-e "\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz" } .
263 qq{-e "\$4=Dx;\$5=Dy;\$6=myDz" } .
264 "| $rfluxmtx -fa -y $ns2 - $receivers -i $octree";
265 } else {
266 $cmd = "cnt $ns2 $ny $nx " .
267 qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
268 qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
269 qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
270 qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
271 qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
272 qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
273 qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
274 qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } .
275 qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } .
276 "| $rfluxmtx -h -ff -y $ns2 - $receivers -i $octree";
277 }
278 } else {
279 # Anisotropic BSDF
280 my $sender = ($bsender,$fsender)[$forw];
281 if ($windoz) {
282 $cmd = "$rfluxmtx -fa $sender $receivers -i $octree";
283 } else {
284 $cmd = "$rfluxmtx -h -ff $sender $receivers -i $octree";
285 }
286 }
287 # print STDERR "Starting: $cmd\n";
288 system $cmd;
289 die "Failure running rfluxmtx" if ( $? );
290 ttree_out($forw);
291 } # end of do_ttree_dir()
292
293 # Simplify and store tensor tree results
294 sub ttree_out {
295 my $forw = shift;
296 my ($refldat,$transdat);
297 if ( $forw ) {
298 $transdat = $facedat;
299 $refldat = $behinddat;
300 } else {
301 $transdat = $behinddat;
302 $refldat = $facedat;
303 }
304 # Only output one transmitted anisotropic distribution, preferring backwards
305 if ( !$forw || !$doback || $tensortree==3 ) {
306 my $ttyp = ("tb","tf")[$forw];
307 ttree_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
308 if ( $docolor ) {
309 ttree_comp($ttyp, "CIE-u", $transdat, ($tbx,$tfx)[$forw]);
310 ttree_comp($ttyp, "CIE-v", $transdat, ($tbz,$tfz)[$forw]);
311 }
312 }
313 # Output reflection
314 my $rtyp = ("rb","rf")[$forw];
315 ttree_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
316 if ( $docolor ) {
317 ttree_comp($rtyp, "CIE-u", $refldat, ($rbx,$rfx)[$forw]);
318 ttree_comp($rtyp, "CIE-v", $refldat, ($rbz,$rfz)[$forw]);
319 }
320 } # end of ttree_out()
321
322 # Call rttree_reduce on the given component
323 sub ttree_comp {
324 my $typ = shift;
325 my $spec = shift;
326 my $src = shift;
327 my $dest = shift;
328 my $cmd;
329 if ($windoz) {
330 if ("$spec" eq "Visible") {
331 $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
332 q{-e "Ri=$1;Gi=$2;Bi=$3" } .
333 qq{-e "$CIEuv" } .
334 q{-e "$1=Yi/Omega"};
335 } elsif ("$spec" eq "CIE-u") {
336 $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } .
337 qq{-e "$CIEuv" } .
338 q{-e "$1=uprime"};
339 } elsif ("$spec" eq "CIE-v") {
340 $cmd = q{rcalc -e "Ri=$1;Gi=$2;Bi=$3" } .
341 qq{-e "$CIEuv" } .
342 q{-e "$1=vprime"};
343 }
344 } else {
345 if ("$spec" eq "Visible") {
346 $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
347 q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
348 "-e '$CIEuv' " .
349 q{-e '$1=Yi/Omega'};
350 } elsif ("$spec" eq "CIE-u") {
351 $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } .
352 "-e '$CIEuv' " .
353 q{-e '$1=uprime'};
354 } elsif ("$spec" eq "CIE-v") {
355 $cmd = q{rcalc -if3 -e 'Ri=$1;Gi=$2;Bi=$3' } .
356 "-e '$CIEuv' " .
357 q{-e '$1=vprime'};
358 }
359 }
360 if ($pctcull >= 0) {
361 my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : "";
362 my $pcull = ("$spec" eq "Visible") ? $pctcull :
363 (100 - (100-$pctcull)*.25) ;
364 if ($windoz) {
365 $cmd = "rcollate -ho -oc 1 $src | " .
366 $cmd .
367 " | rttree_reduce$avg -h -fa -t $pcull -r $tensortree -g $ttlog2";
368 } else {
369 $cmd .= " -of $src " .
370 "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2";
371 }
372 # print STDERR "Running: $cmd\n";
373 system "$cmd > $dest";
374 die "Failure running rttree_reduce" if ( $? );
375 } else {
376 if ($windoz) {
377 $cmd = "rcollate -ho -oc 1 $src | " . $cmd ;
378 } else {
379 $cmd .= " $src";
380 }
381 open(DATOUT, "> $dest");
382 print DATOUT "{\n";
383 close DATOUT;
384 # print STDERR "Running: $cmd\n";
385 system "$cmd >> $dest";
386 die "Failure running rcalc" if ( $? );
387 open(DATOUT, ">> $dest");
388 for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) {
389 print DATOUT "0\n";
390 }
391 print DATOUT "}\n";
392 close DATOUT;
393 }
394 if ( "$spec" ne "$curspec" ) {
395 $wrapper .= " -s $spec";
396 $curspec = $spec;
397 }
398 $wrapper .= " -$typ $dest";
399 } # end of ttree_comp()
400
401 #------------- End of do_tree_bsdf() & subroutines -------------#
402
403 #+++++++++++++++ Klems matrix BSDF generation +++++++++++++++#
404 sub do_matrix_bsdf {
405
406 # Run rfluxmtx processes to compute each side
407 do_matrix_dir(0) if ( $doback );
408 do_matrix_dir(1) if ( $doforw );
409
410 } # end of sub do_matrix_bsdf()
411
412 # Call rfluxmtx and process tensor tree BSDF for the given direction
413 sub do_matrix_dir {
414 my $forw = shift;
415 my $cmd;
416 my $sender = ($bsender,$fsender)[$forw];
417 $cmd = "$rfluxmtx -fd $sender $receivers -i $octree";
418 # print STDERR "Starting: $cmd\n";
419 system $cmd;
420 die "Failure running rfluxmtx" if ( $? );
421 matrix_out($forw);
422 } # end of do_matrix_dir()
423
424 sub matrix_out {
425 my $forw = shift;
426 my ($refldat,$transdat);
427 if ( $forw ) {
428 $transdat = $facedat;
429 $refldat = $behinddat;
430 } else {
431 $transdat = $behinddat;
432 $refldat = $facedat;
433 }
434 # Output transmission
435 my $ttyp = ("tb","tf")[$forw];
436 matrix_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
437 if ( $docolor ) {
438 matrix_comp($ttyp, "CIE-X", $transdat, ($tbx,$tfx)[$forw]);
439 matrix_comp($ttyp, "CIE-Z", $transdat, ($tbz,$tfz)[$forw]);
440 }
441 # Output reflection
442 my $rtyp = ("rb","rf")[$forw];
443 matrix_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
444 if ( $docolor ) {
445 matrix_comp($rtyp, "CIE-X", $refldat, ($rbx,$rfx)[$forw]);
446 matrix_comp($rtyp, "CIE-Z", $refldat, ($rbz,$rfz)[$forw]);
447 }
448 } # end of matrix_out()
449
450 # Transpose matrix component data and save to file
451 sub matrix_comp {
452 my $typ = shift;
453 my $spec = shift;
454 my $src = shift;
455 my $dest = shift;
456 my $cmd = "rmtxop -fa -t";
457 if ("$spec" eq "Visible") {
458 $cmd .= " -c 0.2651 0.6701 0.0648";
459 } elsif ("$spec" eq "CIE-X") {
460 $cmd .= " -c 0.5141 0.3239 0.1620";
461 } elsif ("$spec" eq "CIE-Z") {
462 $cmd .= " -c 0.0241 0.1229 0.8530";
463 }
464 $cmd .= " $src | rcollate -ho -oc 145";
465 # print STDERR "Running: $cmd\n";
466 system "$cmd > $dest";
467 die "Failure running rttree_reduce" if ( $? );
468 if ( "$spec" ne "$curspec" ) {
469 $wrapper .= " -s $spec";
470 $curspec = $spec;
471 }
472 $wrapper .= " -$typ $dest";
473 } # end of matrix_comp()
474
475 #------------- End of do_matrix_bsdf() & subroutines --------------#