1 |
#!/usr/bin/perl -w |
2 |
# RCSid $Id$ |
3 |
# |
4 |
# Convert tabulated isotropic direct-hemispherical and direct-direct to Klems XML |
5 |
# |
6 |
# G. Ward |
7 |
# |
8 |
use strict; |
9 |
my $windoz = ($^O eq "MSWin32" or $^O eq "MSWin64"); |
10 |
use File::Temp qw/ :mktemp /; |
11 |
sub userror { |
12 |
print STDERR "Usage: iso2klems [-t][-W][-s \"x=string;y=string\"] [input.dat]\n"; |
13 |
exit 1; |
14 |
} |
15 |
my ($td,$rmtmp,$cmd); |
16 |
if ($windoz) { |
17 |
my $tmploc = `echo \%TMP\%`; |
18 |
chomp $tmploc; |
19 |
$td = mkdtemp("$tmploc\\iso2klems.XXXXXX"); |
20 |
$rmtmp = "rd /S /Q $td"; |
21 |
} else { |
22 |
$td = mkdtemp("/tmp/iso2klems.XXXXXX"); |
23 |
chomp $td; |
24 |
$rmtmp = "rm -rf $td"; |
25 |
} |
26 |
my $wrapper = 'wrapBSDF -W -a kf -C "Generated by iso2klems"'; |
27 |
my $reverse = 0; |
28 |
# Get options |
29 |
while ($#ARGV >= 0) { |
30 |
if ("$ARGV[0]" eq "-t") { |
31 |
$reverse = ! $reverse; |
32 |
} elsif ("$ARGV[0]" eq "-s") { |
33 |
$wrapper .= " -f \"$ARGV[1]\""; |
34 |
shift @ARGV; |
35 |
} elsif ("$ARGV[0]" =~ /^-./) { |
36 |
userror(); |
37 |
} else { |
38 |
last; |
39 |
} |
40 |
shift @ARGV; |
41 |
} |
42 |
my $funcfile = "$td/isofuncs.cal"; |
43 |
my @vnm = ("Tspec","Tdiff","Rspec","Rdiff"); |
44 |
system "cat @ARGV | tabfunc -i @vnm > $funcfile"; |
45 |
die "Invalid input data, requires 5 columns\n" if ( $? ); |
46 |
open (MYFH, ">> $funcfile"); |
47 |
print MYFH "DEG : PI/180;\n"; |
48 |
print MYFH "and(a,b) : if(a,b,a);\n"; |
49 |
print MYFH "eq(a,b) : min(a-b+1e-5,b-a+1e-5);\n"; |
50 |
print MYFH "rtheta(i) : select(i,5,15,25,35,45,55,65,75,90);\n"; |
51 |
print MYFH "nphis(i) : select(i,1,8,16,20,24,24,24,16,12);\n"; |
52 |
print MYFH "tdeg1(tb) : if(tb-1.5, (rtheta(tb)+rtheta(tb-1))/2, 0);\n"; |
53 |
print MYFH "omega1(tb) : 2*PI*if(tb-1.5, (cos(rtheta(tb-1)*DEG)-cos(rtheta(tb)*DEG))/nphis(tb),"; |
54 |
print MYFH "\t(1 - cos(rtheta(1)*DEG)));\n"; |
55 |
print MYFH "tbin(b,cnt) : if(nphis(b)-cnt-.5, b, tbin(b+1,cnt-nphis(b)));\n"; |
56 |
print MYFH "tdeg(b) : tdeg1(tbin(1,b-1));\n"; |
57 |
print MYFH "omega(b) : omega1(tbin(1,b-1));\n"; |
58 |
close MYFH; |
59 |
# Compute front and back diffuse hemispherical integrals |
60 |
my $hsamps = 26; |
61 |
my $nsamps = $hsamps * 2; |
62 |
if ($windoz) { |
63 |
$cmd = qq{cnt $nsamps } . |
64 |
qq{| rcalc -f $funcfile -w -e "tdeg=180/$nsamps*(\$1+.5);abs(x):if(x,x,-x)" } . |
65 |
qq{-e "theta=tdeg*DEG;ifact=2*PI*abs(cos(theta))*sin(theta)" } . |
66 |
q{-e "$1=ifact*Tdiff(tdeg);$2=ifact*Rdiff(tdeg)" } . |
67 |
qq{| total -$hsamps -m}; |
68 |
} else { |
69 |
$cmd = qq{cnt $nsamps } . |
70 |
qq{| rcalc -f $funcfile -w -e 'tdeg=180/$nsamps*(\$1+.5);abs(x):if(x,x,-x)' } . |
71 |
qq{-e 'theta=tdeg*DEG;ifact=2*PI*abs(cos(theta))*sin(theta)' } . |
72 |
q{-e '$1=ifact*Tdiff(tdeg);$2=ifact*Rdiff(tdeg)' -od } . |
73 |
qq{| total -id2 -$hsamps -m}; |
74 |
} |
75 |
# Returns side 1 Th-h, side 1 Rh-h, side 2 Th-h, side 2 Rh-h |
76 |
my @h2h = split /\s+/, `$cmd`; |
77 |
die "Error running rcalc" if ( $? || $#h2h != 3 ); |
78 |
# Compute Klems matrices |
79 |
my $TK1dataf = "$td/trans1m.txt"; |
80 |
my $RK1dataf = "$td/refl1m.txt"; |
81 |
my $TK2dataf = "$td/trans2m.txt"; |
82 |
my $RK2dataf = "$td/refl2m.txt"; |
83 |
if ($windoz) { |
84 |
$cmd = qq{cnt 145 145 } . |
85 |
qq{| rcalc -f $funcfile } . |
86 |
q{-e "diag=eq($1,$2);tideg=tdeg($1+1);tsdeg=tdeg($2+1);om=omega($1+1);corr=PI/(PI-om)" } . |
87 |
qq{-e "Td1=if($h2h[0]-.001,Tdiff(tideg)*Tdiff(tsdeg)/$h2h[0],0)" } . |
88 |
qq{-e "Rd1=if($h2h[1]-.001,Rdiff(tideg)*Rdiff(tsdeg)/$h2h[1],0)" } . |
89 |
qq{-e "Td2=if($h2h[2]-.001,Tdiff(180-tideg)*Tdiff(180-tsdeg)/$h2h[2],0)" } . |
90 |
qq{-e "Rd2=if($h2h[3]-.001,Rdiff(180-tideg)*Rdiff(180-tsdeg)/$h2h[3],0)" } . |
91 |
qq{-e "\$1=if(Tspec(0),if(diag,Tspec(tideg)/om,Td1*corr),Td1)" } . |
92 |
qq{-e "\$2=if(Rspec(0),if(diag,Rspec(tideg)/om,Rd1*corr),Rd1)" } . |
93 |
qq{-e "\$3=if(Tspec(180),if(diag,Tspec(180-tideg)/om,Td2*corr),Td2)" } . |
94 |
qq{-e "\$4=if(Rspec(180),if(diag,Rspec(180-tideg)/om,Rd2*corr),Rd2)" }; |
95 |
} else { |
96 |
$cmd = qq{cnt 145 145 } . |
97 |
qq{| rcalc -f $funcfile } . |
98 |
q{-e 'diag=eq($1,$2);tideg=tdeg($1+1);tsdeg=tdeg($2+1);om=omega($1+1);corr=PI/(PI-om)' } . |
99 |
qq{-e 'Td1=if($h2h[0]-.001,Tdiff(tideg)*Tdiff(tsdeg)/$h2h[0],0)' } . |
100 |
qq{-e 'Rd1=if($h2h[1]-.001,Rdiff(tideg)*Rdiff(tsdeg)/$h2h[1],0)' } . |
101 |
qq{-e 'Td2=if($h2h[2]-.001,Tdiff(180-tideg)*Tdiff(180-tsdeg)/$h2h[2],0)' } . |
102 |
qq{-e 'Rd2=if($h2h[3]-.001,Rdiff(180-tideg)*Rdiff(180-tsdeg)/$h2h[3],0)' } . |
103 |
qq{-e '\$1=if(Tspec(0),if(diag,Tspec(tideg)/om,Td1*corr),Td1)' } . |
104 |
qq{-e '\$2=if(Rspec(0),if(diag,Rspec(tideg)/om,Rd1*corr),Rd1)' } . |
105 |
qq{-e '\$3=if(Tspec(180),if(diag,Tspec(180-tideg)/om,Td2*corr),Td2)' } . |
106 |
qq{-e '\$4=if(Rspec(180),if(diag,Rspec(180-tideg)/om,Rd2*corr),Rd2)' }; |
107 |
} |
108 |
system qq{$cmd | rsplit "-t " $TK1dataf $RK1dataf $TK2dataf $RK2dataf}; |
109 |
die "Error running rcalc or rsplit" if ( $? ); |
110 |
if ($reverse) { |
111 |
$wrapper .= " -tf $TK2dataf -rf $RK2dataf -tb $TK1dataf -rb $RK1dataf"; |
112 |
} else { |
113 |
$wrapper .= " -tf $TK1dataf -rf $RK1dataf -tb $TK2dataf -rb $RK2dataf"; |
114 |
} |
115 |
system $wrapper; |
116 |
die "Error running: $wrapper\n" if ( $? ); |
117 |
exec $rmtmp; |