| 1 | greg | 2.1 | #!/usr/bin/perl -w | 
| 2 |  |  | # RCSid $Id$ | 
| 3 |  |  | # | 
| 4 |  |  | # Sample Klems (full) directions impinging on surface(s) | 
| 5 |  |  | # | 
| 6 |  |  | if ($#ARGV < 0) { | 
| 7 |  |  | print "Usage: genklemsamp [-c N ][-f{a|f|d}] [view opts] [geom.rad ..]\n"; | 
| 8 |  |  | exit 1; | 
| 9 |  |  | } | 
| 10 |  |  | my $td = `mktemp -d /tmp/genklemsamp.XXXXXX`; | 
| 11 |  |  | chomp $td; | 
| 12 |  |  | my $nsamp = 1000; | 
| 13 |  |  | my $fmt = "a"; | 
| 14 |  |  | my @vopts="-vo 1e-5"; | 
| 15 |  |  | while ($#ARGV >= 0) { | 
| 16 |  |  | if ("$ARGV[0]" eq "-c") { | 
| 17 |  |  | shift @ARGV; | 
| 18 |  |  | $nsamp = $ARGV[0]; | 
| 19 |  |  | } elsif ("$ARGV[0]" =~ /^-f[afd]$/) { | 
| 20 |  |  | $fmt = substr($ARGV[0], 2, 1); | 
| 21 |  |  | } elsif ("$ARGV[0]" =~ /^-v[pdu]$/) { | 
| 22 |  |  | push @vopts, "@ARGV[0..3]"; | 
| 23 |  |  | shift @ARGV; shift @ARGV; shift @ARGV; | 
| 24 |  |  | } elsif ("$ARGV[0]" =~ /^-v[fhvo]$/) { | 
| 25 |  |  | push @vopts, "@ARGV[0..1]"; | 
| 26 |  |  | shift @ARGV; | 
| 27 |  |  | } elsif ("$ARGV[0]" =~ /^-v./) { | 
| 28 |  |  | print "Unsupported view option: $ARGV[0]\n"; | 
| 29 |  |  | exit 1; | 
| 30 |  |  | } elsif ("$ARGV[0]" =~ /^-./) { | 
| 31 |  |  | print "Unknown option: $ARGV[0]\n"; | 
| 32 |  |  | exit 1; | 
| 33 |  |  | } else { | 
| 34 |  |  | last; | 
| 35 |  |  | } | 
| 36 |  |  | shift @ARGV; | 
| 37 |  |  | } | 
| 38 |  |  | my $ofmt = ""; | 
| 39 |  |  | $ofmt = "-o$fmt" if ("$fmt" ne "a"); | 
| 40 |  |  | # Require parallel view and compile settings | 
| 41 |  |  | push @vopts, "-vtl"; | 
| 42 |  |  | my $vwset = `vwright @vopts V`; | 
| 43 |  |  | $_ = $vwset; s/^.*Vhn://; s/;.*$//; | 
| 44 |  |  | my $width = $_; | 
| 45 |  |  | $_ = $vwset; s/^.*Vvn://; s/;.*$//; | 
| 46 |  |  | my $height = $_; | 
| 47 |  |  | my $sca = sqrt($nsamp/($width*$height)); | 
| 48 |  |  | # Kbin is input to get direction in full Klems basis with (x1,x2) randoms | 
| 49 |  |  | my $tcal = ' | 
| 50 |  |  | DEGREE : PI/180; | 
| 51 |  |  | Kpola(r) : select(r+1, -5, 5, 15, 25, 35, 45, 55, 65, 75, 90); | 
| 52 |  |  | Knaz(r) : select(r, 1, 8, 16, 20, 24, 24, 24, 16, 12); | 
| 53 |  |  | Kaccum(r) : if(r-.5, Knaz(r) + Kaccum(r-1), 0); | 
| 54 |  |  | Kmax : Kaccum(Knaz(0)); | 
| 55 |  |  | Kfindrow(r, rem) : if(rem-Knaz(r)-.5, Kfindrow(r+1, rem-Knaz(r)), r); | 
| 56 |  |  | Krow = if(Kbin-(Kmax+.5), 0, Kfindrow(1, Kbin)); | 
| 57 |  |  | Kcol = Kbin - Kaccum(Krow-1) - 1; | 
| 58 |  |  | Kazi = 360*DEGREE * (Kcol + .5 - x2) / Knaz(Krow); | 
| 59 |  |  | Kpol = DEGREE * (x1*Kpola(Krow) + (1-x1)*Kpola(Krow-1)); | 
| 60 |  |  | sin_kpol = sin(Kpol); | 
| 61 |  |  | K0 = cos(Kazi)*sin_kpol; | 
| 62 |  |  | K1 = sin(Kazi)*sin_kpol; | 
| 63 |  |  | K2 = cos(Kpol); | 
| 64 |  |  | '; | 
| 65 |  |  | my $ndiv = 145; | 
| 66 |  |  | # Do we have any Radiance input files? | 
| 67 |  |  | if ($#ARGV >= 0) { | 
| 68 |  |  | system "oconv -f @ARGV > $td/surf.oct"; | 
| 69 |  |  | # Set our own view center and size based on bounding cube | 
| 70 |  |  | $_ = $vwset; s/^.*Vdx://; s/;.*$//; | 
| 71 |  |  | my @vd = $_; | 
| 72 |  |  | $_ = $vwset; s/^.*Vdy://; s/;.*$//; | 
| 73 |  |  | push @vd, $_; | 
| 74 |  |  | $_ = $vwset; s/^.*Vdz://; s/;.*$//; | 
| 75 |  |  | push @vd, $_; | 
| 76 |  |  | my @bcube = split /\s+/, `getinfo -d < $td/surf.oct`; | 
| 77 |  |  | $width = $bcube[3]*sqrt(3); | 
| 78 |  |  | $height = $width; | 
| 79 |  |  | push @vopts, ("-vp", $bcube[0]+$bcube[3]/2-$width/2*$vd[0], | 
| 80 |  |  | $bcube[1]+$bcube[3]/2-$width/2*$vd[1], | 
| 81 |  |  | $bcube[2]+$bcube[3]/2-$width/2*$vd[2]); | 
| 82 |  |  | push @vopts, ("-vh", $width, "-vh", $height); | 
| 83 |  |  | $vwset = `vwright @vopts V`; | 
| 84 |  |  | my $xres; | 
| 85 |  |  | my $yres; | 
| 86 |  |  | my $ntot; | 
| 87 |  |  | # This generally passes through the loop twice to get density right | 
| 88 |  |  | do { | 
| 89 |  |  | $xres = int($width*$sca) + 1; | 
| 90 |  |  | $yres = int($height*$sca) + 1; | 
| 91 |  |  | system "vwrays -ff -x $xres -y $yres -pa 0 -pj .7 @vopts " . | 
| 92 |  |  | "| rtrace -w -h -ff -opLN $td/surf.oct " . | 
| 93 |  |  | q{| rcalc -e 'cond=and(5e9-$4,nOK);$1=$1;$2=$2;$3=$3' } . | 
| 94 |  |  | "-e 'and(a,b):if(a,b,a);sq(x):x*x' -e '$vwset' " . | 
| 95 |  |  | q{-e 'nOK=sq(Vdx*$5+Vdy*$6+Vdz*$7)-.999' } . | 
| 96 |  |  | "-if7 -of > $td/origins.flt"; | 
| 97 |  |  | $ntot = -s "$td/origins.flt"; | 
| 98 |  |  | $ntot /= 3*4; | 
| 99 |  |  | if ($ntot == 0) { | 
| 100 |  |  | print "View direction does not correspond to any surfaces\n"; | 
| 101 |  |  | exit 1; | 
| 102 |  |  | } | 
| 103 |  |  | $sca *= 1.05 * sqrt($nsamp/$ntot); | 
| 104 |  |  | } while ($ntot < $nsamp); | 
| 105 |  |  | # All set to produce our samples | 
| 106 |  |  | for ($k = 1; $k <= $ndiv; $k++) { | 
| 107 |  |  | my $rn = rand(10); | 
| 108 |  |  | my $r1 = rand; my $r2 = rand; | 
| 109 |  |  | # Chance of using = (number_still_needed)/(number_left_avail) | 
| 110 |  |  | system "rcalc $ofmt -e '$tcal' -e '$vwset' " . | 
| 111 |  |  | "-e 'cond=($nsamp-outno+1)/($ntot-recno+1)-rand($rn+recno)' " . | 
| 112 |  |  | "-e 'Kbin=$k;x1=rand($r1+recno);x2=rand($r2+recno)' " . | 
| 113 |  |  | q{-e '$1=$1+Vo*Vdx; $2=$2+Vo*Vdy; $3=$3+Vo*Vdz' } . | 
| 114 |  |  | q{-e '$4=K0*Vhx+K1*Vvx+K2*Vdx' } . | 
| 115 |  |  | q{-e '$5=K0*Vhy+K1*Vvy+K2*Vdy' } . | 
| 116 |  |  | q{-e '$6=K0*Vhz+K1*Vvz+K2*Vdz' } . | 
| 117 |  |  | "-if3 $td/origins.flt"; | 
| 118 |  |  | } | 
| 119 |  |  | } else { | 
| 120 |  |  | # No Radiance input files, so just sample over parallel view | 
| 121 |  |  | my $xres = int($width*$sca) + 1; | 
| 122 |  |  | my $yres = int($height*$sca) + 1; | 
| 123 |  |  | my $ntot = $xres * $yres; | 
| 124 |  |  | for ($k = 1; $k <= $ndiv; $k++) { | 
| 125 |  |  | my $rn = rand(10); | 
| 126 |  |  | my $r1 = rand; my $r2 = rand; | 
| 127 |  |  | my $r3 = rand; my $r4 = rand; | 
| 128 |  |  | # Chance of using = (number_still_needed)/(number_left_avail) | 
| 129 |  |  | system "cnt $yres $xres | rcalc $ofmt -e '$tcal' -e '$vwset' " . | 
| 130 |  |  | q{-e 'xc=$2;yc=$1' } . | 
| 131 |  |  | "-e 'cond=($nsamp-outno+1)/($ntot-recno+1)-rand($rn+recno)' " . | 
| 132 |  |  | "-e 'Kbin=$k;x1=rand($r1+recno);x2=rand($r2+recno)' " . | 
| 133 |  |  | "-e 'hpos=Vhn*((xc+rand($r3+recno))/$xres - .5)' " . | 
| 134 |  |  | "-e 'vpos=Vvn*((yc+rand($r4+recno))/$yres - .5)' " . | 
| 135 |  |  | q{-e '$1=Vpx+Vo*Vdx+hpos*Vhx+vpos*Vvx' } . | 
| 136 |  |  | q{-e '$2=Vpy+Vo*Vdy+hpos*Vhy+vpos*Vvy' } . | 
| 137 |  |  | q{-e '$3=Vpz+Vo*Vdz+hpos*Vhz+vpos*Vvz' } . | 
| 138 |  |  | q{-e '$4=K0*Vhx+K1*Vvx+K2*Vdx' } . | 
| 139 |  |  | q{-e '$5=K0*Vhy+K1*Vvy+K2*Vdy' } . | 
| 140 |  |  | q{-e '$6=K0*Vhz+K1*Vvz+K2*Vdz' } ; | 
| 141 |  |  | } | 
| 142 |  |  | } | 
| 143 |  |  | system "rm -rf $td"; |