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