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