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