ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/genskyvec.pl
Revision: 2.6
Committed: Mon May 16 23:56:32 2011 UTC (11 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +59 -39 lines
Log Message:
Made scripts Windows-compatible.  Thanks to Andy McNeil for sorting it out

File Contents

# Content
1 #!/usr/bin/perl -w
2 # RCSid $Id: genskyvec.pl,v 2.5 2011/01/27 22:28:12 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 $suncmd = "cnt " . ($nbins-1) .
85 " | rcalc -e MF:$mf -e \"$rhcal\" -e Rbin=recno " .
86 "-e \"dot=Dx*$sundir[0] + Dy*$sundir[1] + Dz*$sundir[2]\" " .
87 "-e \"cond=dot-.866\" " .
88 q{-e "$1=if(1-dot,acos(dot),0);$2=Romega;$3=recno" };
89 } else {
90 $nbins = `rcalc -n -e MF:$mf -e \'$rhcal\' -e \'\$1=Rmax+1\'`;
91 chomp $nbins;
92 $octree = "/tmp/gtv$$.oct";
93 $tregcommand = "cnt $nbins 16 | rcalc -of -e MF:$mf -e '$rhcal' " .
94 q{-e 'Rbin=$1;x1=rand(recno*.37-5.3);x2=rand(recno*-1.47+.86)' } .
95 q{-e '$1=0;$2=0;$3=0;$4=Dx;$5=Dy;$6=Dz' } .
96 "| rtrace -h -ff -ab 0 -w $octree | total -if3 -16 -m";
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 # Create octree for rtrace
104 open OCONV, "| oconv - > $octree";
105 print OCONV @skydesc;
106 print OCONV "skyfunc glow skyglow 0 0 4 @skycolor 0\n";
107 print OCONV "skyglow source sky 0 0 4 0 0 1 360\n";
108 close OCONV;
109 # Run rtrace and average output for every 16 samples
110 my @tregval = `$tregcommand`;
111 unlink $octree;
112 # Find closest 3 patches to sun and divvy up direct solar contribution
113 sub numSort1 {
114 my @a1 = split("\t", $a);
115 my @b1 = split("\t", $b);
116 return ($a1[0] <=> $b1[0]);
117 }
118 if (@sundir) {
119 my $somega = ($sundir[3]/360)**2 * 3.141592654**3;
120 my @bestdir = `$suncmd`;
121 @bestdir = sort numSort1 @bestdir;
122 my (@ang, @dom, @ndx);
123 my $wtot = 0;
124 for my $i (0..2) {
125 ($ang[$i], $dom[$i], $ndx[$i]) = split(' ', $bestdir[$i]);
126 $wtot += 1./($ang[$i]+.02);
127 }
128 for my $i (0..2) {
129 my $wt = 1./($ang[$i]+.02)/$wtot * $somega / $dom[$i];
130 my @scolor = split(' ', $tregval[$ndx[$i]]);
131 for my $j (0..2) { $scolor[$j] += $wt * $sunval[$j]; }
132 $tregval[$ndx[$i]] = "$scolor[0]\t$scolor[1]\t$scolor[2]\n";
133 }
134 }
135 # Output our final vector
136 print @tregval;