ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/gentregvec.pl
Revision: 2.1
Committed: Tue Jun 16 04:54:08 2009 UTC (14 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Created gentregvec utility to evaluate Tregenza sky patches

File Contents

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