| 1 |
#!/usr/bin/perl -w
|
| 2 |
# RCSid $Id: genskyvec.pl,v 2.10 2016/12/12 17:40:07 greg Exp $
|
| 3 |
#
|
| 4 |
# Generate Reinhart vector for a given sky description
|
| 5 |
#
|
| 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 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 |
}
|
| 34 |
# Load sky description into line array, separating sun if one
|
| 35 |
my @skydesc;
|
| 36 |
my $lightline;
|
| 37 |
my @sunval;
|
| 38 |
my $sunline;
|
| 39 |
my $skyOK = 0;
|
| 40 |
my $srcmod; # putting this inside loop breaks code(?!)
|
| 41 |
while (<>) {
|
| 42 |
push @skydesc, $_;
|
| 43 |
if (/^\w+\s+light\s+/) {
|
| 44 |
s/\s*$//; s/^.*\s//;
|
| 45 |
$srcmod = $_;
|
| 46 |
$lightline = $#skydesc;
|
| 47 |
} elsif (defined($srcmod) && /^($srcmod)\s+source\s/) {
|
| 48 |
@sunval = split(' ', $skydesc[$lightline + 3]);
|
| 49 |
shift @sunval;
|
| 50 |
$sunline = $#skydesc;
|
| 51 |
} elsif (/\sskyfunc\s*$/) {
|
| 52 |
$skyOK = 1;
|
| 53 |
}
|
| 54 |
}
|
| 55 |
die "Bad sky description!\n" if (! $skyOK);
|
| 56 |
# Strip out the solar source if present
|
| 57 |
my @sundir;
|
| 58 |
if (defined $sunline) {
|
| 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 = '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 |
} 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;
|