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