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