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