6 |
|
# G. Ward |
7 |
|
# |
8 |
|
use strict; |
9 |
+ |
my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64"); |
10 |
|
my @skycolor = (0.960, 1.004, 1.118); |
11 |
|
my $mf = 4; |
12 |
+ |
my $dosky = 1; |
13 |
+ |
my $dofive = 0; |
14 |
+ |
my $headout = 1; |
15 |
+ |
my @origARGV = @ARGV; |
16 |
|
while ($#ARGV >= 0) { |
17 |
|
if ("$ARGV[0]" eq "-c") { |
18 |
|
@skycolor = @ARGV[1..3]; |
19 |
< |
shift @ARGV; shift @ARGV; shift @ARGV; |
19 |
> |
shift @ARGV for (1..3); |
20 |
|
} elsif ("$ARGV[0]" eq "-m") { |
21 |
|
$mf = $ARGV[1]; |
22 |
|
shift @ARGV; |
23 |
+ |
} elsif ("$ARGV[0]" eq "-d") { |
24 |
+ |
$dosky = 0; |
25 |
+ |
} elsif ("$ARGV[0]" eq "-5") { |
26 |
+ |
$dofive = 1; |
27 |
+ |
} elsif ("$ARGV[0]" eq "-h") { |
28 |
+ |
$headout = 0; |
29 |
+ |
} else { |
30 |
+ |
die "Unexpected command-line argument: $ARGV[0]\n"; |
31 |
|
} |
32 |
|
shift @ARGV; |
33 |
|
} |
45 |
|
$srcmod = $_; |
46 |
|
$lightline = $#skydesc; |
47 |
|
} elsif (defined($srcmod) && /^($srcmod)\s+source\s/) { |
48 |
< |
@sunval = split(/\s+/, $skydesc[$lightline + 3]); |
48 |
> |
@sunval = split(' ', $skydesc[$lightline + 3]); |
49 |
|
shift @sunval; |
50 |
|
$sunline = $#skydesc; |
51 |
|
} elsif (/\sskyfunc\s*$/) { |
56 |
|
# Strip out the solar source if present |
57 |
|
my @sundir; |
58 |
|
if (defined $sunline) { |
59 |
< |
@sundir = split(/\s+/, $skydesc[$sunline + 3]); |
59 |
> |
@sundir = split(' ', $skydesc[$sunline + 3]); |
60 |
|
shift @sundir; |
61 |
|
undef @sundir if ($sundir[2] <= 0); |
62 |
|
splice(@skydesc, $sunline, 5); |
63 |
|
} |
64 |
|
# Reinhart sky sample generator |
65 |
< |
my $rhcal = ' |
66 |
< |
DEGREE : PI/180; |
67 |
< |
x1 = .5; x2 = .5; |
68 |
< |
alpha : 90/(MF*7 + .5); |
69 |
< |
tnaz(r) : select(r, 30, 30, 24, 24, 18, 12, 6); |
70 |
< |
rnaz(r) : if(r-(7*MF-.5), 1, MF*tnaz(floor((r+.5)/MF) + 1)); |
71 |
< |
raccum(r) : if(r-.5, rnaz(r-1) + raccum(r-1), 0); |
72 |
< |
RowMax : 7*MF + 1; |
73 |
< |
Rmax : raccum(RowMax); |
74 |
< |
Rfindrow(r, rem) : if(rem-rnaz(r)-.5, Rfindrow(r+1, rem-rnaz(r)), r); |
75 |
< |
Rrow = if(Rbin-(Rmax-.5), RowMax-1, Rfindrow(0, Rbin)); |
76 |
< |
Rcol = Rbin - raccum(Rrow) - 1; |
77 |
< |
Razi_width = 2*PI / rnaz(Rrow); |
78 |
< |
RAH : alpha*DEGREE; |
79 |
< |
Razi = if(Rbin-.5, (Rcol + x2 - .5)*Razi_width, 2*PI*x2); |
80 |
< |
Ralt = if(Rbin-.5, (Rrow + x1)*RAH, asin(-x1)); |
81 |
< |
Romega = if(.5-Rbin, 2*PI, if(Rmax-.5-Rbin, |
82 |
< |
Razi_width*(sin(RAH*(Rrow+1)) - sin(RAH*Rrow)), |
83 |
< |
2*PI*(1 - cos(RAH/2)) ) ); |
84 |
< |
cos_ralt = cos(Ralt); |
85 |
< |
Dx = sin(Razi)*cos_ralt; |
86 |
< |
Dy = cos(Razi)*cos_ralt; |
87 |
< |
Dz = sin(Ralt); |
88 |
< |
'; |
89 |
< |
my $nbins = `rcalc -n -e MF:$mf -e \'$rhcal\' -e \'\$1=Rmax+1\'`; |
90 |
< |
chomp $nbins; |
91 |
< |
# Create octree for rtrace |
92 |
< |
my $octree = "/tmp/gtv$$.oct"; |
93 |
< |
open OCONV, "| oconv - > $octree"; |
94 |
< |
print OCONV @skydesc; |
95 |
< |
print OCONV "skyfunc glow skyglow 0 0 4 @skycolor 0\n"; |
96 |
< |
print OCONV "skyglow source sky 0 0 4 0 0 1 360\n"; |
97 |
< |
close OCONV; |
98 |
< |
# Run rtrace and average output for every 16 samples |
99 |
< |
my $tregcommand = "cnt $nbins 16 | rcalc -of -e MF:$mf -e '$rhcal' " . |
100 |
< |
q{-e 'Rbin=$1;x1=rand(recno*.37-5.3);x2=rand(recno*-1.47+.86)' } . |
101 |
< |
q{-e '$1=0;$2=0;$3=0;$4=Dx;$5=Dy;$6=Dz' } . |
89 |
< |
"| rtrace -h -ff -ab 0 -w $octree | total -if3 -16 -m"; |
90 |
< |
my @tregval = `$tregcommand`; |
91 |
< |
unlink $octree; |
92 |
< |
# Find closest 3 patches to sun and divvy up direct solar contribution |
93 |
< |
my @bestdir; |
94 |
< |
if (@sundir) { |
95 |
< |
my $somega = ($sundir[3]/360)**2 * 3.141592654**3; |
96 |
< |
my $cmd = "cnt " . ($nbins-1) . |
97 |
< |
" | rcalc -e MF:$mf -e '$rhcal' -e Rbin=recno " . |
98 |
< |
"-e 'dot=Dx*$sundir[0] + Dy*$sundir[1] + Dz*$sundir[2]' " . |
99 |
< |
"-e 'cond=dot-.866' " . |
100 |
< |
q{-e '$1=if(1-dot,acos(dot),0);$2=Romega;$3=recno' }; |
101 |
< |
@bestdir = `$cmd | sort -n | head -3`; |
102 |
< |
my (@ang, @dom, @ndx); |
103 |
< |
my $wtot = 0; |
104 |
< |
for my $i (0..2) { |
105 |
< |
($ang[$i], $dom[$i], $ndx[$i]) = split(/\s+/, $bestdir[$i]); |
106 |
< |
$wtot += 1./($ang[$i]+.02); |
65 |
> |
my $rhcal = 'DEGREE : PI/180;' . |
66 |
> |
'x1 = .5; x2 = .5;' . |
67 |
> |
'alpha : 90/(MF*7 + .5);' . |
68 |
> |
'tnaz(r) : select(r, 30, 30, 24, 24, 18, 12, 6);' . |
69 |
> |
'rnaz(r) : if(r-(7*MF-.5), 1, MF*tnaz(floor((r+.5)/MF) + 1));' . |
70 |
> |
'raccum(r) : if(r-.5, rnaz(r-1) + raccum(r-1), 0);' . |
71 |
> |
'RowMax : 7*MF + 1;' . |
72 |
> |
'Rmax : raccum(RowMax);' . |
73 |
> |
'Rfindrow(r, rem) : if(rem-rnaz(r)-.5, Rfindrow(r+1, rem-rnaz(r)), r);' . |
74 |
> |
'Rrow = if(Rbin-(Rmax-.5), RowMax-1, Rfindrow(0, Rbin));' . |
75 |
> |
'Rcol = Rbin - raccum(Rrow) - 1;' . |
76 |
> |
'Razi_width = 2*PI / rnaz(Rrow);' . |
77 |
> |
'RAH : alpha*DEGREE;' . |
78 |
> |
'Razi = if(Rbin-.5, (Rcol + x2 - .5)*Razi_width, 2*PI*x2);' . |
79 |
> |
'Ralt = if(Rbin-.5, (Rrow + x1)*RAH, asin(-x1));' . |
80 |
> |
'Romega = if(.5-Rbin, 2*PI, if(Rmax-.5-Rbin, ' . |
81 |
> |
' Razi_width*(sin(RAH*(Rrow+1)) - sin(RAH*Rrow)),' . |
82 |
> |
' 2*PI*(1 - cos(RAH/2)) ) );' . |
83 |
> |
'cos_ralt = cos(Ralt);' . |
84 |
> |
'Dx = sin(Razi)*cos_ralt;' . |
85 |
> |
'Dy = cos(Razi)*cos_ralt;' . |
86 |
> |
'Dz = sin(Ralt);' ; |
87 |
> |
my ($nbins, $octree, $tregcommand, $suncmd); |
88 |
> |
if ($windoz) { |
89 |
> |
$nbins = `rcalc -n -e MF:$mf -e \"$rhcal\" -e \"\$1=Rmax+1\"`; |
90 |
> |
chomp $nbins; |
91 |
> |
$octree = "gtv$$.oct"; |
92 |
> |
$tregcommand = "cnt $nbins 16 | rcalc -e MF:$mf -e \"$rhcal\" " . |
93 |
> |
q{-e "Rbin=$1;x1=rand(recno*.37-5.3);x2=rand(recno*-1.47+.86)" } . |
94 |
> |
q{-e "$1=0;$2=0;$3=0;$4=Dx;$5=Dy;$6=Dz" } . |
95 |
> |
"| rtrace -h -ab 0 -w $octree | total -16 -m"; |
96 |
> |
if (@sundir) { |
97 |
> |
$suncmd = "cnt " . ($nbins-1) . |
98 |
> |
" | rcalc -e MF:$mf -e \"$rhcal\" -e Rbin=recno " . |
99 |
> |
"-e \"dot=Dx*$sundir[0] + Dy*$sundir[1] + Dz*$sundir[2]\" " . |
100 |
> |
"-e \"cond=dot-.866\" " . |
101 |
> |
q{-e "$1=if(1-dot,acos(dot),0);$2=Romega;$3=recno" }; |
102 |
|
} |
103 |
< |
for my $i (0..2) { |
104 |
< |
my $wt = 1./($ang[$i]+.02)/$wtot * $somega / $dom[$i]; |
105 |
< |
my @scolor = split(/\s+/, $tregval[$ndx[$i]]); |
106 |
< |
for my $j (0..2) { $scolor[$j] += $wt * $sunval[$j]; } |
107 |
< |
$tregval[$ndx[$i]] = "@scolor\n"; |
103 |
> |
} else { |
104 |
> |
$nbins = `rcalc -n -e MF:$mf -e \'$rhcal\' -e \'\$1=Rmax+1\'`; |
105 |
> |
chomp $nbins; |
106 |
> |
$octree = "/tmp/gtv$$.oct"; |
107 |
> |
$tregcommand = "cnt $nbins 16 | rcalc -of -e MF:$mf -e '$rhcal' " . |
108 |
> |
q{-e 'Rbin=$1;x1=rand(recno*.37-5.3);x2=rand(recno*-1.47+.86)' } . |
109 |
> |
q{-e '$1=0;$2=0;$3=0;$4=Dx;$5=Dy;$6=Dz' } . |
110 |
> |
"| rtrace -h -ff -ab 0 -w $octree | total -if3 -16 -m"; |
111 |
> |
if (@sundir) { |
112 |
> |
$suncmd = "cnt " . ($nbins-1) . |
113 |
> |
" | rcalc -e MF:$mf -e '$rhcal' -e Rbin=recno " . |
114 |
> |
"-e 'dot=Dx*$sundir[0] + Dy*$sundir[1] + Dz*$sundir[2]' " . |
115 |
> |
"-e 'cond=dot-.866' " . |
116 |
> |
q{-e '$1=if(1-dot,acos(dot),0);$2=Romega;$3=recno' }; |
117 |
|
} |
118 |
+ |
} |
119 |
+ |
my @tregval; |
120 |
+ |
if ($dosky) { |
121 |
+ |
# Create octree for rtrace |
122 |
+ |
open OCONV, "| oconv - > $octree"; |
123 |
+ |
print OCONV @skydesc; |
124 |
+ |
print OCONV "skyfunc glow skyglow 0 0 4 @skycolor 0\n"; |
125 |
+ |
print OCONV "skyglow source sky 0 0 4 0 0 1 360\n"; |
126 |
+ |
close OCONV; |
127 |
+ |
# Run rtrace and average output for every 16 samples |
128 |
+ |
@tregval = `$tregcommand`; |
129 |
+ |
unlink $octree; |
130 |
+ |
} else { |
131 |
+ |
push @tregval, "0\t0\t0\n" for (1..$nbins); |
132 |
+ |
} |
133 |
+ |
# Find closest patch(es) to sun and divvy up direct solar contribution |
134 |
+ |
sub numSort1 { |
135 |
+ |
my @a1 = split("\t", $a); |
136 |
+ |
my @b1 = split("\t", $b); |
137 |
+ |
return ($a1[0] <=> $b1[0]); |
138 |
+ |
} |
139 |
+ |
if (@sundir) { |
140 |
+ |
my @bestdir = `$suncmd`; |
141 |
+ |
@bestdir = sort numSort1 @bestdir; |
142 |
+ |
if ($dofive) { |
143 |
+ |
my ($ang, $dom, $ndx); |
144 |
+ |
($ang, $dom, $ndx) = split(' ', $bestdir[0]); |
145 |
+ |
$tregval[$ndx] = "$sunval[0]\t$sunval[1]\t$sunval[2]\n"; |
146 |
+ |
} else { |
147 |
+ |
my (@ang, @dom, @ndx); |
148 |
+ |
my $somega = ($sundir[3]/360)**2 * 3.141592654**3; |
149 |
+ |
my $wtot = 0; |
150 |
+ |
for my $i (0..2) { |
151 |
+ |
($ang[$i], $dom[$i], $ndx[$i]) = split(' ', $bestdir[$i]); |
152 |
+ |
$wtot += 1./($ang[$i]+.02); |
153 |
+ |
} |
154 |
+ |
for my $i (0..2) { |
155 |
+ |
my $wt = 1./($ang[$i]+.02)/$wtot * $somega / $dom[$i]; |
156 |
+ |
my @scolor = split(' ', $tregval[$ndx[$i]]); |
157 |
+ |
for my $j (0..2) { $scolor[$j] += $wt * $sunval[$j]; } |
158 |
+ |
$tregval[$ndx[$i]] = "$scolor[0]\t$scolor[1]\t$scolor[2]\n"; |
159 |
+ |
} |
160 |
+ |
} |
161 |
+ |
} |
162 |
+ |
# Output header if requested |
163 |
+ |
if ($headout) { |
164 |
+ |
print "#?RADIANCE\n"; |
165 |
+ |
print "genskyvec @origARGV\n"; |
166 |
+ |
print "NROWS=", $#tregval+1, "\n"; |
167 |
+ |
print "NCOLS=1\nNCOMP=3\n"; |
168 |
+ |
print "FORMAT=ascii\n"; |
169 |
+ |
print "\n"; |
170 |
|
} |
171 |
|
# Output our final vector |
172 |
|
print @tregval; |