ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
Revision: 2.65
Committed: Thu Aug 18 15:09:29 2016 UTC (7 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.64: +2 -2 lines
Log Message:
Fixed warning about device being far beind Z==0 plane

File Contents

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