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.68 by greg, Fri Sep 16 20:01:29 2016 UTC vs.
Revision 2.91 by greg, Fri Jan 5 18:04:28 2024 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);
16 > my ($tf,$rf,$tb,$rb,$tfx,$rfx,$tbx,$rbx,$tfz,$rfz,$tbz,$rbz);
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 56 | Line 57 | if ($windoz) {
57          $rfz = "$td\\rfz.dat";
58          $tbz = "$td\\tbz.dat";
59          $rbz = "$td\\rbz.dat";
59        $cph = "$td\\phase.txt";
60          $rmtmp = "rd /S /Q $td";
61   } else {
62          $radscn = "$td/device.rad";
# Line 79 | Line 79 | if ($windoz) {
79          $rfz = "$td/rfz.dat";
80          $tbz = "$td/tbz.dat";
81          $rbz = "$td/rbz.dat";
82        $cph = "$td/phase.txt";
82          $rmtmp = "rm -rf $td";
83   }
84   my @savedARGV = @ARGV;
85 < my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6";
85 > my $rfluxmtx = "rfluxmtx -ab 5 -ad 700 -lw 3e-6 -w-";
86   my $wrapper = "wrapBSDF";
87   my $tensortree = 0;
88   my $ttlog2 = 4;
89 + my $dorecip = 1;
90   my $nsamp = 2000;
91   my $mgfin = 0;
92   my $geout = 1;
# Line 114 | Line 114 | while ($#ARGV >= 0) {
114                  shift @ARGV;
115          } elsif ("$ARGV[0]" =~ /^[-+]C/) {
116                  $docolor = ("$ARGV[0]" =~ /^\+/);
117 +        } elsif ("$ARGV[0]" =~ /^[-+]a/) {
118 +                $dorecip = ("$ARGV[0]" =~ /^\+/);
119          } elsif ("$ARGV[0]" =~ /^[-+]f/) {
120                  $doforw = ("$ARGV[0]" =~ /^\+/);
121          } elsif ("$ARGV[0]" =~ /^[-+]b/) {
# Line 169 | Line 171 | if ( !defined $recovery ) {
171          }
172   }
173   if ( $#dim != 5 ) {
174 <        @dim = split ' ', `getbbox -h $radscn`;
174 >        @dim = split ' ', `getbbox -h -w $radscn`;
175   }
176   die "Device entirely inside room!\n" if ( $dim[4] >= 0 );
177   if ( $dim[5] > 1e-5 ) {
# Line 186 | Line 188 | my $CIEuv =    'Xi=.5141*Ri+.3239*Gi+.1620*Bi;' .
188                  'Yi=.2651*Ri+.6701*Gi+.0648*Bi;' .
189                  'Zi=.0241*Ri+.1229*Gi+.8530*Bi;' .
190                  'den=Xi+15*Yi+3*Zi;' .
191 <                'uprime=4*Xi/den;vprime=9*Yi/den;' ;
191 >                'uprime=if(Yi,4*Xi/den,4/19);' .
192 >                'vprime=if(Yi,9*Yi/den,9/19);' ;
193   my $FEPS = 1e-5;
194   my $ns = 2**$ttlog2;
195   my $nx = int(sqrt($nsamp*($dim[1]-$dim[0])/($dim[3]-$dim[2])) + 1);
196   my $ny = int($nsamp/$nx + 1);
197   $nsamp = $nx * $ny;
198 < $rfluxmtx .= " -n $nproc -c $nsamp";
198 > $rfluxmtx .= " -n $nproc -c $nsamp -cs 3";
199   if ( !defined $recovery ) {
200          open(MYAVH, "> $td/savedARGV.txt");
201          foreach (@savedARGV) {
# Line 208 | Line 211 | if ( !defined $recovery ) {
211                  printf MGFSCN "xf -t %.6f %.6f 0\n", -($dim[0]+$dim[1])/2, -($dim[2]+$dim[3])/2;
212                  close MGFSCN;
213                  if ( $mgfin ) {
214 +                        die "+mgf requires input file with +geom\n" if ($#ARGV < 0);
215                          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};
216                  } else {
217                          system "rad2mgf $radscn >> $mgfscn";
# Line 248 | Line 252 | if ( !defined $recovery ) {
252          }
253          print STDERR "Recover using: $0 -recover $td\n";
254   }
251 # Open unbuffered progress file
252 open(MYPH, ">> $td/phase.txt");
253 {
254        my $ofh = select MYPH;
255        $| = 1;
256        select $ofh;
257 }
255   $curphase = 0;
256   # Create data segments (all the work happens here)
257   if ( $tensortree ) {
# Line 274 | Line 271 | exec $rmtmp;
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; }
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 294 | 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; }
299 <        $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 306 | Line 322 | sub do_ttree_dir {
322                                  qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
323                                  qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
324                                  qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
325 <                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
325 >                                qq{-e "r4=rand(-1.5839226*recno-59.82712)" } .
326 >                                qq{-e "odds(n):if(.5*n-floor(.5*n)-.25,-1,1)"} .
327 >                                qq{-e "Dx=1-(\$1+r1)/$ns2" } .
328 >                                qq{-e "Dy=min(1/$ns,sqrt(1-Dx*Dx))*odds(\$1)*r2" } .
329 >                                qq{-e "Dz=sqrt(1-Dx*Dx-Dy*Dy)" } .
330                                  qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
331                                  qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
332                                  qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
# Line 315 | Line 335 | sub do_ttree_dir {
335                                  "| $rfluxmtx$r -fa -y $ns2 - $receivers -i $octree";
336                  } else {
337                          $cmd = "cnt $ns2 $ny $nx " .
338 <                                qq{| rcalc -e "r1=rand(.8681*recno-.673892)" } .
339 <                                qq{-e "r2=rand(-5.37138*recno+67.1737811)" } .
340 <                                qq{-e "r3=rand(+3.17603772*recno+83.766771)" } .
341 <                                qq{-e "Dx=1-2*(\$1+r1)/$ns;Dy:0;Dz=sqrt(1-Dx*Dx)" } .
342 <                                qq{-e "xp=(\$3+r2)*(($dim[1]-$dim[0])/$nx)+$dim[0]" } .
343 <                                qq{-e "yp=(\$2+r3)*(($dim[3]-$dim[2])/$ny)+$dim[2]" } .
344 <                                qq{-e "zp=$dim[5-$forw]" -e "myDz=Dz*($forw*2-1)" } .
338 >                                qq{| rcalc -e 'r1=rand(.8681*recno-.673892)' } .
339 >                                qq{-e 'r2=rand(-5.37138*recno+67.1737811)' } .
340 >                                qq{-e 'r3=rand(+3.17603772*recno+83.766771)' } .
341 >                                qq{-e 'r4=rand(-1.5839226*recno-59.82712)' } .
342 >                                qq{-e 'odds(n):if(.5*n-floor(.5*n)-.25,-1,1)' } .
343 >                                qq{-e 'Dx=1-(\$1+r1)/$ns2' } .
344 >                                qq{-e 'Dy=min(1/$ns,sqrt(1-Dx*Dx))*odds(\$1)*r2' } .
345 >                                qq{-e 'Dz=sqrt(1-Dx*Dx-Dy*Dy)' } .
346 >                                qq{-e 'xp=(\$3+r3)*(($dim[1]-$dim[0])/$nx)+$dim[0]' } .
347 >                                qq{-e 'yp=(\$2+r4)*(($dim[3]-$dim[2])/$ny)+$dim[2]' } .
348 >                                qq{-e 'zp=$dim[5-$forw]' -e 'myDz=Dz*($forw*2-1)' } .
349                                  qq{-e '\$1=xp-Dx;\$2=yp-Dy;\$3=zp-myDz' } .
350                                  qq{-e '\$4=Dx;\$5=Dy;\$6=myDz' -of } .
351                                  "| $rfluxmtx$r -h -ff -y $ns2 - $receivers -i $octree";
# Line 335 | Line 359 | sub do_ttree_dir {
359                          $cmd = "$rfluxmtx$r -h -ff $sender $receivers -i $octree";
360                  }
361          }
362 <        # print STDERR "Starting: $cmd\n";
363 <        system $cmd;
364 <        die "Failure running rfluxmtx" if ( $? );
362 >        if ( $dop ) {
363 >                # print STDERR "Running: $cmd\n";
364 >                system $cmd;
365 >                die "Failure running rfluxmtx" if ( $? );
366 >        }
367          ttree_out($forw);
368   }       # end of do_ttree_dir()
369  
# Line 409 | Line 435 | sub ttree_comp {
435                  }
436          }
437          if ($pctcull >= 0) {
438 <                my $avg = ( "$typ" =~ /^r[fb]/ ) ? " -a" : "";
438 >                my $avg = ( $dorecip && ( $tensortree == 3 || "$typ" =~ /^r[fb]/ ) ) ? " -a" : "";
439                  my $pcull = ("$spec" eq "Visible") ? $pctcull :
440                                                       (100 - (100-$pctcull)*.25) ;
441                  if ($windoz) {
# Line 420 | Line 446 | sub ttree_comp {
446                          $cmd .= " -of $src " .
447                                          "| rttree_reduce$avg -h -ff -t $pcull -r $tensortree -g $ttlog2";
448                  }
449 <                # print STDERR "Running: $cmd\n";
424 <                system "$cmd > $dest";
425 <                die "Failure running rttree_reduce" if ( $? );
449 >                run_check "$cmd > $dest";
450          } else {
451                  if ($windoz) {
452                          $cmd = "rcollate -ho -oc 1 $src | " . $cmd ;
453                  } else {
454                          $cmd .= " $src";
455                  }
456 <                open(DATOUT, "> $dest");
457 <                print DATOUT "{\n";
458 <                close DATOUT;
459 <                # print STDERR "Running: $cmd\n";
460 <                system "$cmd >> $dest";
461 <                die "Failure running rcalc" if ( $? );
462 <                open(DATOUT, ">> $dest");
463 <                for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) {
464 <                        print DATOUT "0\n";
456 >                if ( active_phase() ) {
457 >                        open(DATOUT, "> $dest");
458 >                        print DATOUT "{\n";
459 >                        close DATOUT;
460 >                        # print STDERR "Running: $cmd\n";
461 >                        system "$cmd >> $dest";
462 >                        die "Failure running rcalc" if ( $? );
463 >                        open(DATOUT, ">> $dest");
464 >                        for (my $i = ($tensortree==3)*$ns*$ns*$ns/2; $i-- > 0; ) {
465 >                                print DATOUT "0\n";
466 >                        }
467 >                        print DATOUT "}\n";
468 >                        close DATOUT;
469                  }
442                print DATOUT "}\n";
443                close DATOUT;
470          }
471          if ( "$spec" ne "$curspec" ) {
472                  $wrapper .= " -s $spec";
# Line 463 | Line 489 | sub do_matrix_bsdf {
489   # Call rfluxmtx and process tensor tree BSDF for the given direction
490   sub do_matrix_dir {
491          my $forw = shift;
492 <        my $r = do_phase();
493 <        if (!$r) { return; }
468 <        $r = ($r < 0) ? " -r" : "";
469 <        my $cmd;
492 >        my $dop = do_phase();
493 >        my $r = ($dop < 0) ? " -r" : "";
494          my $sender = ($bsender,$fsender)[$forw];
495 <        $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree";
496 <        # print STDERR "Starting: $cmd\n";
497 <        system $cmd;
498 <        die "Failure running rfluxmtx" if ( $? );
495 >        my $cmd = "$rfluxmtx$r -fd $sender $receivers -i $octree";
496 >        if ( $dop ) {
497 >                # print STDERR "Running: $cmd\n";
498 >                system $cmd;
499 >                die "Failure running rfluxmtx" if ( $? );
500 >        }
501          matrix_out($forw);
502   }       # end of do_matrix_dir()
503  
# Line 515 | Line 541 | sub matrix_comp {
541          } elsif ("$spec" eq "CIE-Z") {
542                  $cmd .= " -c 0.0241 0.1229 0.8530";
543          }
544 <        $cmd .= " $src | rcollate -ho -oc 145";
545 <        # print STDERR "Running: $cmd\n";
520 <        system "$cmd > $dest";
521 <        die "Failure running rmtxop" if ( $? );
544 >        $cmd .= " $src | getinfo -";
545 >        run_check "$cmd > $dest";
546          if ( "$spec" ne "$curspec" ) {
547                  $wrapper .= " -s $spec";
548                  $curspec = $spec;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines