1 |
greg |
2.1 |
#!/usr/bin/perl -w |
2 |
greg |
2.5 |
# RCSid $Id: gentregvec.pl,v 2.4 2009/06/24 19:19:38 greg Exp $ |
3 |
greg |
2.1 |
# |
4 |
|
|
# Generate Tregenza vector for a given sky description |
5 |
|
|
# |
6 |
|
|
# G. Ward |
7 |
|
|
# |
8 |
|
|
use strict; |
9 |
|
|
$ENV{RAYPATH} = ':/usr/local/lib/ray' if (! $ENV{RAYPATH}); |
10 |
|
|
my $tregsamp = "tregsamp.dat"; |
11 |
|
|
my @skycolor = (0.960, 1.004, 1.118); |
12 |
|
|
while ($#ARGV >= 0) { |
13 |
|
|
if ("$ARGV[0]" eq "-c") { |
14 |
|
|
@skycolor = @ARGV[1..3]; |
15 |
|
|
shift @ARGV; shift @ARGV; shift @ARGV; |
16 |
|
|
} |
17 |
|
|
shift @ARGV; |
18 |
|
|
} |
19 |
|
|
# Load sky description into line array, separating sun if one |
20 |
|
|
my @skydesc; |
21 |
|
|
my $lightline; |
22 |
|
|
my @sunval; |
23 |
|
|
my $sunline; |
24 |
greg |
2.4 |
my $skyOK = 0; |
25 |
greg |
2.2 |
my $srcmod; # putting this inside loop breaks code(?!) |
26 |
greg |
2.1 |
while (<>) { |
27 |
|
|
push @skydesc, $_; |
28 |
|
|
if (/^\w+\s+light\s+/) { |
29 |
greg |
2.4 |
s/\s*$//; s/^.*\s//; |
30 |
greg |
2.1 |
$srcmod = $_; |
31 |
|
|
$lightline = $#skydesc; |
32 |
greg |
2.2 |
} elsif (defined($srcmod) && /^($srcmod)\s+source\s/) { |
33 |
greg |
2.1 |
@sunval = split(/\s+/, $skydesc[$lightline + 3]); |
34 |
|
|
shift @sunval; |
35 |
|
|
$sunline = $#skydesc; |
36 |
greg |
2.4 |
} elsif (/\sskyfunc\s*$/) { |
37 |
|
|
$skyOK = 1; |
38 |
greg |
2.1 |
} |
39 |
|
|
} |
40 |
greg |
2.4 |
die "Bad sky description!\n" if (! $skyOK); |
41 |
greg |
2.1 |
# Strip out the solar source if present |
42 |
|
|
my @sundir; |
43 |
|
|
if (defined $sunline) { |
44 |
|
|
@sundir = split(/\s+/, $skydesc[$sunline + 3]); |
45 |
|
|
shift @sundir; |
46 |
|
|
undef @sundir if ($sundir[2] <= 0); |
47 |
greg |
2.2 |
splice(@skydesc, $sunline, 5); |
48 |
greg |
2.1 |
} |
49 |
|
|
# Get Tregenza sample file |
50 |
|
|
my $found; |
51 |
|
|
foreach my $dir (split /:/, $ENV{RAYPATH}) { |
52 |
|
|
if (-r "$dir/$tregsamp") { |
53 |
|
|
$found = "$dir/$tregsamp"; |
54 |
|
|
last; |
55 |
|
|
} |
56 |
|
|
} |
57 |
|
|
if (! $found) { |
58 |
|
|
print STDERR "Cannot find file '$tregsamp'\n"; |
59 |
|
|
exit 1; |
60 |
|
|
} |
61 |
|
|
$tregsamp = $found; |
62 |
|
|
# Create octree for rtrace |
63 |
|
|
my $octree = "/tmp/gtv$$.oct"; |
64 |
|
|
open OCONV, "| oconv - > $octree"; |
65 |
|
|
print OCONV @skydesc; |
66 |
|
|
print OCONV "skyfunc glow skyglow 0 0 4 @skycolor 0\n"; |
67 |
|
|
print OCONV "skyglow source sky 0 0 4 0 0 1 360\n"; |
68 |
|
|
close OCONV; |
69 |
|
|
# Run rtrace and average output for every 64 samples |
70 |
|
|
my @tregval = `rtrace -h -faf -ab 0 -w < $tregsamp $octree | total -if3 -m -64`; |
71 |
|
|
if ($#tregval != 145) { |
72 |
|
|
print STDERR "Expected 9344 samples in $tregsamp -- found ", |
73 |
|
|
`wc -l < $tregsamp`, "\n"; |
74 |
|
|
exit 1; |
75 |
|
|
} |
76 |
|
|
unlink $octree; |
77 |
greg |
2.2 |
# Find closest 3 patches to sun and divvy up direct solar contribution |
78 |
greg |
2.1 |
my @bestdir; |
79 |
|
|
if (@sundir) { |
80 |
greg |
2.2 |
my $somega = ($sundir[3]/360)**2 * 3.141592654**3; |
81 |
greg |
2.1 |
my @tindex = (30, 60, 84, 108, 126, 138, 144, 145); |
82 |
|
|
my @tomega = (0.0435449227, 0.0416418006, 0.0473984151, |
83 |
|
|
0.0406730411, 0.0428934136, 0.0445221864, |
84 |
|
|
0.0455168385, 0.0344199465); |
85 |
|
|
my $cmd = "total -m -64 < $tregsamp | tail +2 | rcalc " . |
86 |
|
|
q{-e 'Dx=$4;Dy=$5;Dz=$6' } . |
87 |
|
|
"-e 'dot=Dx*$sundir[0] + Dy*$sundir[1] + Dz*$sundir[2]' " . |
88 |
|
|
q{-e '$1=acos(dot);$2=recno' | sort -n}; |
89 |
|
|
@bestdir = `$cmd | head -3`; |
90 |
|
|
my @ang; my @ndx; |
91 |
|
|
($ang[0], $ndx[0], $ang[1], $ndx[1], $ang[2], $ndx[2]) = |
92 |
|
|
( split(/\s+/, $bestdir[0]), |
93 |
|
|
split(/\s+/, $bestdir[1]), |
94 |
|
|
split(/\s+/, $bestdir[2]) ); |
95 |
|
|
my $wtot = 1/($ang[0]+.02) + 1/($ang[1]+.02) + 1/($ang[2]+.02); |
96 |
|
|
for my $i (0..2) { |
97 |
|
|
my $ti = 0; |
98 |
|
|
while ($ndx[$i] > $tindex[$ti]) { $ti++ } |
99 |
greg |
2.3 |
my $wt = 1/($ang[$i]+.02)/$wtot * $somega / $tomega[$ti]; |
100 |
greg |
2.1 |
my @scolor = split(/\s+/, $tregval[$ndx[$i]]); |
101 |
|
|
for my $j (0..2) { $scolor[$j] += $wt * $sunval[$j]; } |
102 |
|
|
$tregval[$ndx[$i]] = "@scolor\n"; |
103 |
|
|
} |
104 |
|
|
} |
105 |
greg |
2.2 |
# Output our final vector |
106 |
greg |
2.1 |
print @tregval; |