ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
Revision: 2.53
Committed: Wed Mar 25 19:18:28 2015 UTC (9 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.52: +39 -19 lines
Log Message:
Changed output of tensor tree to use CIE (u',v') coordinates

File Contents

# Content
1 #!/usr/bin/perl -w
2 # RCSid $Id: genBSDF.pl,v 2.52 2015/03/16 00:40:51 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 shift @ARGV;
108 } elsif ("$ARGV[0]" =~ /^-t[34]$/) {
109 $tensortree = substr($ARGV[0], 2, 1);
110 $ttlog2 = $ARGV[1];
111 shift @ARGV;
112 } elsif ("$ARGV[0]" eq "-f") {
113 $wrapper .= " -f \"$ARGV[1]\"";
114 shift @ARGV;
115 } elsif ("$ARGV[0]" eq "-W") {
116 $wrapper .= " -W";
117 } elsif ("$ARGV[0]" eq "-c") {
118 $nsamp = $ARGV[1];
119 shift @ARGV;
120 } elsif ("$ARGV[0]" eq "-n") {
121 $nproc = $ARGV[1];
122 shift @ARGV;
123 } elsif ("$ARGV[0]" =~ /^-d/) {
124 userror() if ($#ARGV < 6);
125 @dim = @ARGV[1..6];
126 shift @ARGV for (1..6);
127 } elsif ("$ARGV[0]" =~ /^[-+]./) {
128 userror();
129 } else {
130 last;
131 }
132 shift @ARGV;
133 }
134 # Check that we're actually being asked to do something
135 die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback);
136 # Issue warning for unhandled reciprocity case
137 print STDERR "Warning: recommend both +forward and +backward with -t3\n" if
138 ($tensortree==3 && !($doforw && $doback));
139 $wrapper .= $tensortree ? " -a t$tensortree" : " -a kf -c";
140 $wrapper .= " -u $gunit";
141 # Get scene description and dimensions
142 if ( $mgfin ) {
143 system "mgf2rad @ARGV > $radscn";
144 die "Could not load MGF input\n" if ( $? );
145 } else {
146 system "xform -e @ARGV > $radscn";
147 die "Could not load Radiance input\n" if ( $? );
148 }
149 if ($#dim != 5) {
150 @dim = split ' ', `getbbox -h $radscn`;
151 }
152 print STDERR "Warning: Device extends into room\n" if ($dim[5] > 1e-5);
153 $wrapper .= ' -f "t=' . ($dim[5] - $dim[4]) . ';w=' . ($dim[1] - $dim[0]) .
154 ';h=' . ($dim[3] - $dim[2]) . '"';
155 # Generate octree
156 system "oconv -w $radscn > $octree";
157 die "Could not compile scene\n" if ( $? );
158 # Add MGF description if requested
159 if ( $geout ) {
160 open(MGFSCN, "> $mgfscn");
161 printf MGFSCN "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
162 close MGFSCN;
163 if ( $mgfin ) {
164 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};
165 } else {
166 system "rad2mgf $radscn >> $mgfscn";
167 }
168 open(MGFSCN, ">> $mgfscn");
169 print MGFSCN "xf\n";
170 close MGFSCN;
171 $wrapper .= " -g $mgfscn";
172 }
173 # Create receiver & sender surfaces (rectangular)
174 my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1);
175 my $ny = int($nsamp/$nx + 1);
176 $nsamp = $nx * $ny;
177 my $ns = 2**$ttlog2;
178 open(RADSCN, "> $receivers");
179 print RADSCN '#@rfluxmtx ' . ($tensortree ? "h=sc$ns\n" : "h=kf\n");
180 print RADSCN '#@rfluxmtx ' . "u=Y o=$facedat\n\n";
181 print RADSCN "void glow receiver_face\n0\n0\n4 1 1 1 0\n\n";
182 print RADSCN "receiver_face source f_receiver\n0\n0\n4 0 0 1 180\n\n";
183 print RADSCN '#@rfluxmtx ' . "u=Y o=$behinddat\n\n";
184 print RADSCN "void glow receiver_behind\n0\n0\n4 1 1 1 0\n\n";
185 print RADSCN "receiver_behind source b_receiver\n0\n0\n4 0 0 -1 180\n";
186 close RADSCN;
187 # Finish rfluxmtx command and prepare sender surfaces
188 $rfluxmtx .= " -n $nproc -c $nsamp";
189 if ( $tensortree != 3 ) { # Isotropic tensor tree is exception
190 open (RADSCN, "> $fsender");
191 print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n");
192 print RADSCN "void polygon fwd_sender\n0\n0\n12\n";
193 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[4];
194 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[4];
195 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[4];
196 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[4];
197 close RADSCN;
198 open (RADSCN, "> $bsender");
199 print RADSCN '#@rfluxmtx u=Y ' . ($tensortree ? "h=sc$ns\n\n" : "h=kf\n\n");
200 print RADSCN "void polygon fwd_sender\n0\n0\n12\n";
201 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[2], $dim[5];
202 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[2], $dim[5];
203 printf RADSCN "\t%f\t%f\t%f\n", $dim[1], $dim[3], $dim[5];
204 printf RADSCN "\t%f\t%f\t%f\n", $dim[0], $dim[3], $dim[5];
205 close RADSCN;
206 }
207 # Calculate CIE (u',v') from Radiance RGB:
208 my $CIEuv = 'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' .
209 'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' .
210 'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' .
211 'den=Xi+15*Yi+3*Zi;' .
212 'uprime=4*Xi/den;vprime=9*Yi/den;' ;
213 # Create data segments (all the work happens here)
214 if ( $tensortree ) {
215 do_tree_bsdf();
216 } else {
217 do_matrix_bsdf();
218 }
219 # Output XML
220 my $old_fh = select(STDOUT);
221 $| = 1;
222 select($old_fh);
223 print "<!-- Created by: genBSDF @savedARGV -->\n";
224 # print STDERR "Running: $wrapper\n";
225 system $wrapper;
226 die "Could not wrap BSDF data\n" if ( $? );
227 # Clean up temporary files and exit
228 exec $rmtmp;
229
230 #-------------- End of main program segment --------------#
231
232 #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
233 sub do_tree_bsdf {
234
235 # Run rfluxmtx processes to compute each side
236 do_ttree_dir(0) if ( $doback );
237 do_ttree_dir(1) if ( $doforw );
238
239 } # end of sub do_tree_bsdf()
240
241 # Call rfluxmtx and process tensor tree BSDF for the given direction
242 sub do_ttree_dir {
243 my $forw = shift;
244 my $cmd;
245 if ( $tensortree == 3 ) {
246 # Isotropic BSDF
247 my $ns2 = $ns / 2;
248 if ($windoz) {
249 $cmd = "cnt $ns2 $ny $nx " .
250 qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
251 qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
252 qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
253 qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
254 qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
255 qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
256 qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
257 qq{-e "\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz" } .
258 qq{-e "\$4=Dx;\$5=Dy;\$6=myDz" } .
259 "| $rfluxmtx -fa -y $ns2 - $receivers -i $octree";
260 } else {
261 $cmd = "cnt $ns2 $ny $nx " .
262 qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
263 qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
264 qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
265 qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
266 qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
267 qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
268 qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
269 qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } .
270 qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } .
271 "| $rfluxmtx -h -ff -y $ns2 - $receivers -i $octree";
272 }
273 } else {
274 # Anisotropic BSDF
275 my $sender = ($bsender,$fsender)[$forw];
276 if ($windoz) {
277 $cmd = "$rfluxmtx -fa $sender $receivers -i $octree";
278 } else {
279 $cmd = "$rfluxmtx -h -ff $sender $receivers -i $octree";
280 }
281 }
282 # print STDERR "Starting: $cmd\n";
283 system $cmd;
284 die "Failure running rfluxmtx" if ( $? );
285 ttree_out($forw);
286 } # end of do_ttree_dir()
287
288 # Simplify and store tensor tree results
289 sub ttree_out {
290 my $forw = shift;
291 my ($refldat,$transdat);
292 if ( $forw ) {
293 $transdat = $facedat;
294 $refldat = $behinddat;
295 } else {
296 $transdat = $behinddat;
297 $refldat = $facedat;
298 }
299 # Only output one transmitted anisotropic distribution, preferring backwards
300 if ( !$forw || !$doback || $tensortree==3 ) {
301 my $ttyp = ("tb","tf")[$forw];
302 ttree_comp($ttyp, "Visible", $transdat, ($tb,$tf)[$forw]);
303 if ( $docolor ) {
304 ttree_comp($ttyp, "CIE-u", $transdat, ($tbx,$tfx)[$forw]);
305 ttree_comp($ttyp, "CIE-v", $transdat, ($tbz,$tfz)[$forw]);
306 }
307 }
308 # Output reflection
309 my $rtyp = ("rb","rf")[$forw];
310 ttree_comp($rtyp, "Visible", $refldat, ($rb,$rf)[$forw]);
311 if ( $docolor ) {
312 ttree_comp($rtyp, "CIE-u", $refldat, ($rbx,$rfx)[$forw]);
313 ttree_comp($rtyp, "CIE-v", $refldat, ($rbz,$rfz)[$forw]);
314 }
315 } # end of ttree_out()
316
317 # Call rttree_reduce on the given component
318 sub ttree_comp {
319 my $typ = shift;
320 my $spec = shift;
321 my $src = shift;
322 my $dest = shift;
323 my $cmd;
324 if ($windoz) {
325 if ("$spec" eq "Visible") {
326 $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
327 q{-e "Ri=$1;Gi=$2;Bi=$3" } .
328 qq{-e "$CIEuv" } .
329 q{-e "$1=Yi/Omega"};
330 } elsif ("$spec" eq "CIE-u") {
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=uprime/Omega"};
335 } elsif ("$spec" eq "CIE-v") {
336 $cmd = qq{rcalc -e "Omega:PI/($ns*$ns)" } .
337 q{-e "Ri=$1;Gi=$2;Bi=$3" } .
338 qq{-e "$CIEuv" } .
339 q{-e "$1=vprime/Omega"};
340 }
341 } else {
342 if ("$spec" eq "Visible") {
343 $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
344 q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
345 "-e '$CIEuv' " .
346 q{-e '$1=Yi/Omega'};
347 } elsif ("$spec" eq "CIE-u") {
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=uprime/Omega'};
352 } elsif ("$spec" eq "CIE-v") {
353 $cmd = "rcalc -if3 -e 'Omega:PI/($ns*$ns)' " .
354 q{-e 'Ri=$1;Gi=$2;Bi=$3' } .
355 "-e '$CIEuv' " .
356 q{-e '$1=vprime/Omega'};
357 }
358 }
359 if ($pctcull >= 0) {
360 my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : "";
361 my $pcull = ("$spec" eq "Visible") ? $pctcull :
362 (100 - (100-$pctcull)/3) ;
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.265 0.670 0.065";
458 } elsif ("$spec" eq "CIE-X") {
459 $cmd .= " -c 0.514 0.324 0.162";
460 } elsif ("$spec" eq "CIE-Z") {
461 $cmd .= " -c 0.024 0.123 0.853";
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 --------------#