ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genBSDF.pl
(Generate patch)

Comparing ray/src/util/genBSDF.pl (file contents):
Revision 2.66 by greg, Fri Sep 16 17:54:56 2016 UTC vs.
Revision 2.78 by greg, Mon Jun 19 03:45:57 2017 UTC

# Line 9 | Line 9 | 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";
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][{+|-}a][{+|-}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,$cph,$sav);
16 > my ($tf,$rf,$tb,$rb,$tfx,$rfx,$tbx,$rbx,$tfz,$rfz,$tbz,$rbz,$cph);
17   my ($curphase, $recovery);
18   if ($#ARGV == 1 && "$ARGV[0]" =~ /^-rec/) {
19          $td = $ARGV[1];
# Line 21 | Line 21 | if ($#ARGV == 1 && "$ARGV[0]" =~ /^-rec/) {
21          @ARGV = <MYAVH>;
22          close MYAVH;
23          chomp @ARGV;
24 +        $recovery = 0;
25          if (open(MYPH, "< $td/phase.txt")) {
26                  while (<MYPH>) {
27                          chomp($recovery = $_);
# Line 83 | Line 84 | if ($windoz) {
84          $rmtmp = "rm -rf $td";
85   }
86   my @savedARGV = @ARGV;
87 < my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6";
87 > my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6 -w-";
88   my $wrapper = "wrapBSDF";
89   my $tensortree = 0;
90   my $ttlog2 = 4;
91 + my $dorecip = 1;
92   my $nsamp = 2000;
93   my $mgfin = 0;
94   my $geout = 1;
# Line 114 | Line 116 | while ($#ARGV >= 0) {
116                  shift @ARGV;
117          } elsif ("$ARGV[0]" =~ /^[-+]C/) {
118                  $docolor = ("$ARGV[0]" =~ /^\+/);
119 +        } elsif ("$ARGV[0]" =~ /^[-+]a/) {
120 +                $dorecip = ("$ARGV[0]" =~ /^\+/);
121          } elsif ("$ARGV[0]" =~ /^[-+]f/) {
122                  $doforw = ("$ARGV[0]" =~ /^\+/);
123          } elsif ("$ARGV[0]" =~ /^[-+]b/) {
# Line 155 | Line 159 | while ($#ARGV >= 0) {
159   die "Must have at least one of +forward or +backward\n" if (!$doforw && !$doback);
160   $wrapper .= $tensortree ? " -a t$tensortree" : " -a kf -c";
161   $wrapper .= " -u $gunit";
162 < if (!defined $recovery) {
162 > if ( !defined $recovery ) {
163          # Issue warning for unhandled reciprocity case
164          print STDERR "Warning: recommend both +forward and +backward with -t3\n" if
165                          ($tensortree==3 && !($doforw && $doback));
166 <        # Get scene description and dimensions
166 >        # Get scene description
167          if ( $mgfin ) {
168                  system "mgf2rad @ARGV > $radscn";
169                  die "Could not load MGF input\n" if ( $? );
# Line 168 | Line 172 | if (!defined $recovery) {
172                  die "Could not load Radiance input\n" if ( $? );
173          }
174   }
175 < if ($#dim != 5) {
176 <        @dim = split ' ', `getbbox -h $radscn`;
175 > if ( $#dim != 5 ) {
176 >        @dim = split ' ', `getbbox -h -w $radscn`;
177   }
178 < die "Device entirely inside room!\n" if ($dim[4] >= 0);
179 < if ($dim[5] > 1e-5) {
178 > die "Device entirely inside room!\n" if ( $dim[4] >= 0 );
179 > if ( $dim[5] > 1e-5 ) {
180          print STDERR "Warning: Device extends into room\n";
181 < } elsif ($dim[5]*$dim[5] > .01*($dim[1]-$dim[0])*($dim[3]-$dim[2])) {
181 > } elsif ( $dim[5]*$dim[5] > .01*($dim[1]-$dim[0])*($dim[3]-$dim[2]) ) {
182          print STDERR "Warning: Device far behind Z==0 plane\n";
183   }
184   # Assume Zmax==0 to derive thickness so pkgBSDF will work
185   $wrapper .= ' -f "t=' . (-$dim[4]) . ';w=' . ($dim[1] - $dim[0]) .
186                  ';h=' . ($dim[3] - $dim[2]) . '"';
187 + $wrapper .= " -g $mgfscn" if ( $geout );
188   # Calculate CIE (u',v') from Radiance RGB:
189   my $CIEuv =     'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' .
190                  'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' .
# Line 192 | Line 197 | my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$d
197   my $ny = int($nsamp/$nx + 1);
198   $nsamp = $nx * $ny;
199   $rfluxmtx .= " -n $nproc -c $nsamp";
200 < if (!defined $recovery) {
200 > if ( !defined $recovery ) {
201          open(MYAVH, "> $td/savedARGV.txt");
202          foreach (@savedARGV) {
203                  print MYAVH "$_\n";
204          }
205          close MYAVH;
206          # Generate octree
207 <        system "oconv -w $radscn > $octree";
207 >        system "oconv -w -f $radscn > $octree";
208          die "Could not compile scene\n" if ( $? );
209          # Add MGF description if requested
210          if ( $geout ) {
# Line 214 | Line 219 | if (!defined $recovery) {
219                  open(MGFSCN, ">> $mgfscn");
220                  print MGFSCN "xf\n";
221                  close MGFSCN;
217                $wrapper .= " -g $mgfscn";
222          }
223          # Create receiver & sender surfaces (rectangular)
224          open(RADSCN, "> $receivers");
# Line 248 | Line 252 | if (!defined $recovery) {
252          }
253          print STDERR "Recover using: $0 -recover $td\n";
254   }
251 open(MYPH, ">> $td/phase.txt");
252 {               # unbuffer output to MYPH
253        my $ofh = select MYPH;
254        $| = 1;
255        select $ofh;
256 }
255   $curphase = 0;
258 # Function to determine if next phase should be skipped or recovered
259 sub do_phase {
260        $curphase++;
261        if (defined $recovery) {
262                if ($recovery == $curphase) { return -1; }
263                if ($recovery > $curphase) { return 0; }
264        }
265        print MYPH "$curphase\n";
266        return 1;
267 }
256   # Create data segments (all the work happens here)
257   if ( $tensortree ) {
258          do_tree_bsdf();
# Line 272 | Line 260 | if ( $tensortree ) {
260          do_matrix_bsdf();
261   }
262   # Output XML
263 < print STDERR "Running: $wrapper\n";
263 > # print STDERR "Running: $wrapper\n";
264   system "$wrapper -C \"Created by: genBSDF @savedARGV\"";
265   die "Could not wrap BSDF data\n" if ( $? );
266   # Clean up temporary files and exit
267   exec $rmtmp;
268  
269 < #-------------- End of main program segment --------------#
269 > #============== End of main program segment ==============#
270  
271 + # Function to determine if next phase should be skipped or recovered
272 + sub do_phase {
273 +        $curphase++;
274 +        if ( defined $recovery ) {
275 +                if ( $recovery > $curphase ) { return 0; }
276 +                if ( $recovery == $curphase ) { return -1; }
277 +        }
278 +        open(MYPH, ">> $td/phase.txt");
279 +        print MYPH "$curphase\n";
280 +        close MYPH;
281 +        return 1;
282 + }
283 +
284 + # Check if we are in active phase (not skipping parts)
285 + sub active_phase {
286 +        if ( defined $recovery ) {
287 +                if ( $recovery > $curphase ) { return 0; }
288 +                if ( $recovery == $curphase ) { return -1; }
289 +        }
290 +        return 1;
291 + }
292 +
293 + # Function to run program and check output if in active phase
294 + sub run_check {
295 +        if ( !active_phase() ) { return; }
296 +        my $cmd = shift;
297 +        # print STDERR "Running: $cmd\n";
298 +        system $cmd;
299 +        die "Failure running: $cmd\n" if ( $? );
300 + }
301 +
302   #++++++++++++++ Tensor tree BSDF generation ++++++++++++++#
303   sub do_tree_bsdf {
304  
# Line 292 | Line 311 | sub do_tree_bsdf {
311   # Call rfluxmtx and process tensor tree BSDF for the given direction
312   sub do_ttree_dir {
313          my $forw = shift;
314 <        my $r = do_phase();
315 <        if (!$r) { return; }
297 <        $r = ($r < 0) ? " -r" : "";
314 >        my $dop = do_phase();
315 >        my $r = ($dop < 0) ? " -r" : "";
316          my $cmd;
317          if ( $tensortree == 3 ) {
318                  # Isotropic BSDF
# Line 313 | Line 331 | sub do_ttree_dir {
331                                  "| $rfluxmtx$r -fa -y $ns2 - $receivers -i $octree";
332                  } else {
333                          $cmd = "cnt $ns2 $ny $nx " .
334 <                                qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
335 <                                qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
336 <                                qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
337 <                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
338 <                                qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
339 <                                qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
340 <                                qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
334 >                                qq{| rcalc -e 'r1=rand(.8681*recno-.673892)' } .
335 >                                qq{-e 'r2=rand(-5.37138*recno+67.1737811)' } .
336 >                                qq{-e 'r3=rand(+3.17603772*recno+83.766771)' } .
337 >                                qq{-e 'Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)' } .
338 >                                qq{-e 'xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]' } .
339 >                                qq{-e 'yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]' } .
340 >                                qq{-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' } .
341                                  qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } .
342                                  qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } .
343                                  "| $rfluxmtx$r -h -ff -y $ns2 - $receivers -i $octree";
# Line 333 | Line 351 | sub do_ttree_dir {
351                          $cmd = "$rfluxmtx$r -h -ff $sender $receivers -i $octree";
352                  }
353          }
354 <        print STDERR "Starting: $cmd\n";
355 <        system $cmd;
356 <        die "Failure running rfluxmtx" if ( $? );
354 >        if ( $dop ) {
355 >                # print STDERR "Running: $cmd\n";
356 >                system $cmd;
357 >                die "Failure running rfluxmtx" if ( $? );
358 >        }
359          ttree_out($forw);
360   }       # end of do_ttree_dir()
361  
# Line 407 | Line 427 | sub ttree_comp {
427                  }
428          }
429          if ($pctcull >= 0) {
430 <                my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : "";
430 >                my $avg = ( $dorecip && "$typ" =~ /^r[fb]/ ) ? " -a" : "";
431                  my $pcull = ("$spec" eq "Visible") ? $pctcull :
432                                                       (100 - (100-$pctcull)*.25) ;
433                  if ($windoz) {
# Line 418 | Line 438 | sub ttree_comp {
438                          $cmd .= " -of $src " .
439                                          "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2";
440                  }
441 <                print STDERR "Running: $cmd\n";
422 <                system "$cmd > $dest";
423 <                die "Failure running rttree_reduce" if ( $? );
441 >                run_check "$cmd > $dest";
442          } else {
443                  if ($windoz) {
444                          $cmd = "rcollate -ho -oc 1 $src | " . $cmd ;
445                  } else {
446                          $cmd .= " $src";
447                  }
448 <                open(DATOUT, "> $dest");
449 <                print DATOUT "{\n";
450 <                close DATOUT;
451 <                print STDERR "Running: $cmd\n";
452 <                system "$cmd >> $dest";
453 <                die "Failure running rcalc" if ( $? );
454 <                open(DATOUT, ">> $dest");
455 <                for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) {
456 <                        print DATOUT "0\n";
448 >                if ( active_phase() ) {
449 >                        open(DATOUT, "> $dest");
450 >                        print DATOUT "{\n";
451 >                        close DATOUT;
452 >                        # print STDERR "Running: $cmd\n";
453 >                        system "$cmd >> $dest";
454 >                        die "Failure running rcalc" if ( $? );
455 >                        open(DATOUT, ">> $dest");
456 >                        for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) {
457 >                                print DATOUT "0\n";
458 >                        }
459 >                        print DATOUT "}\n";
460 >                        close DATOUT;
461                  }
440                print DATOUT "}\n";
441                close DATOUT;
462          }
463          if ( "$spec" ne "$curspec" ) {
464                  $wrapper .= " -s $spec";
# Line 461 | Line 481 | sub do_matrix_bsdf {
481   # Call rfluxmtx and process tensor tree BSDF for the given direction
482   sub do_matrix_dir {
483          my $forw = shift;
484 <        my $r = do_phase();
485 <        if (!$r) { return; }
466 <        $r = ($r < 0) ? " -r" : "";
467 <        my $cmd;
484 >        my $dop = do_phase();
485 >        my $r = ($dop < 0) ? " -r" : "";
486          my $sender = ($bsender,$fsender)[$forw];
487 <        $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree";
488 <        print STDERR "Starting: $cmd\n";
489 <        system $cmd;
490 <        die "Failure running rfluxmtx" if ( $? );
487 >        my $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree";
488 >        if ( $dop ) {
489 >                # print STDERR "Running: $cmd\n";
490 >                system $cmd;
491 >                die "Failure running rfluxmtx" if ( $? );
492 >        }
493          matrix_out($forw);
494   }       # end of do_matrix_dir()
495  
# Line 513 | Line 533 | sub matrix_comp {
533          } elsif ("$spec" eq "CIE-Z") {
534                  $cmd .= " -c 0.0241 0.1229 0.8530";
535          }
536 <        $cmd .= " $src | rcollate -ho -oc 145";
537 <        print STDERR "Running: $cmd\n";
518 <        system "$cmd > $dest";
519 <        die "Failure running rmtxop" if ( $? );
536 >        $cmd .= " $src | getinfo -";
537 >        run_check "$cmd > $dest";
538          if ( "$spec" ne "$curspec" ) {
539                  $wrapper .= " -s $spec";
540                  $curspec = $spec;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines