1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdf2rad.c,v 2.6 2014/02/17 21:56:22 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Plot 3-D BSDF output based on scattering interpolant or XML representation |
6 |
*/ |
7 |
|
8 |
#define _USE_MATH_DEFINES |
9 |
#include <stdio.h> |
10 |
#include <string.h> |
11 |
#include <stdlib.h> |
12 |
#include <math.h> |
13 |
#include "rtprocess.h" |
14 |
#include "bsdfrep.h" |
15 |
|
16 |
const float colarr[6][3] = { |
17 |
.7, 1., .7, |
18 |
1., .7, .7, |
19 |
.7, .7, 1., |
20 |
1., .5, 1., |
21 |
1., 1., .5, |
22 |
.5, 1., 1. |
23 |
}; |
24 |
|
25 |
char *progname; |
26 |
|
27 |
/* Produce a Radiance model plotting the indicated incident direction(s) */ |
28 |
int |
29 |
main(int argc, char *argv[]) |
30 |
{ |
31 |
int showPeaks = 0; |
32 |
int doTrans = 0; |
33 |
int inpXML = -1; |
34 |
RBFNODE *rbf = NULL; |
35 |
FILE *fp; |
36 |
char buf[128]; |
37 |
SDData myBSDF; |
38 |
double bsdf, min_log; |
39 |
FVECT idir, odir; |
40 |
int i, j, n; |
41 |
/* check arguments */ |
42 |
progname = argv[0]; |
43 |
if (argc > 1 && !strcmp(argv[1], "-p")) { |
44 |
++showPeaks; |
45 |
++argv; --argc; |
46 |
} |
47 |
if (argc > 1 && !strcmp(argv[1], "-t")) { |
48 |
++doTrans; |
49 |
++argv; --argc; |
50 |
} |
51 |
if (argc >= 4 && (n = strlen(argv[1])-4) > 0) { |
52 |
if (!strcasecmp(argv[1]+n, ".xml")) |
53 |
inpXML = 1; |
54 |
else if (!strcasecmp(argv[1]+n, ".sir")) |
55 |
inpXML = 0; |
56 |
} |
57 |
if (inpXML < 0) { |
58 |
fprintf(stderr, "Usage: %s [-p] bsdf.sir theta1 phi1 .. > output.rad\n", progname); |
59 |
fprintf(stderr, " Or: %s [-t] bsdf.xml theta1 phi1 .. > output.rad\n", progname); |
60 |
return(1); |
61 |
} |
62 |
/* load input */ |
63 |
if (inpXML) { |
64 |
SDclearBSDF(&myBSDF, argv[1]); |
65 |
if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr)) |
66 |
return(1); |
67 |
bsdf_min = 1./M_PI; |
68 |
if (myBSDF.rf != NULL && myBSDF.rLambFront.cieY < bsdf_min*M_PI) |
69 |
bsdf_min = myBSDF.rLambFront.cieY/M_PI; |
70 |
if (myBSDF.rb != NULL && myBSDF.rLambBack.cieY < bsdf_min*M_PI) |
71 |
bsdf_min = myBSDF.rLambBack.cieY/M_PI; |
72 |
if ((myBSDF.tf != NULL) | (myBSDF.tb != NULL) && |
73 |
myBSDF.tLamb.cieY < bsdf_min*M_PI) |
74 |
bsdf_min = myBSDF.tLamb.cieY/M_PI; |
75 |
if (doTrans && (myBSDF.tf == NULL) & (myBSDF.tb == NULL)) { |
76 |
fprintf(stderr, "%s: no transmitted component in '%s'\n", |
77 |
progname, argv[1]); |
78 |
return(1); |
79 |
} |
80 |
} else { |
81 |
fp = fopen(argv[1], "rb"); |
82 |
if (fp == NULL) { |
83 |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
84 |
progname, argv[1]); |
85 |
return(1); |
86 |
} |
87 |
if (!load_bsdf_rep(fp)) |
88 |
return(1); |
89 |
fclose(fp); |
90 |
} |
91 |
#ifdef DEBUG |
92 |
fprintf(stderr, "Minimum BSDF set to %.4f\n", bsdf_min); |
93 |
#endif |
94 |
min_log = log(bsdf_min*.5 + 1e-5); |
95 |
/* output BSDF rep. */ |
96 |
for (n = 0; (n < 6) & (2*n+3 < argc); n++) { |
97 |
double theta = atof(argv[2*n+2]); |
98 |
if (inpXML) { |
99 |
input_orient = (theta <= 90.) ? 1 : -1; |
100 |
output_orient = doTrans ? -input_orient : input_orient; |
101 |
} |
102 |
idir[2] = sin((M_PI/180.)*theta); |
103 |
idir[0] = idir[2] * cos((M_PI/180.)*atof(argv[2*n+3])); |
104 |
idir[1] = idir[2] * sin((M_PI/180.)*atof(argv[2*n+3])); |
105 |
idir[2] = input_orient * sqrt(1. - idir[2]*idir[2]); |
106 |
#ifdef DEBUG |
107 |
fprintf(stderr, "Computing BSDF for incident direction (%.1f,%.1f)\n", |
108 |
get_theta180(idir), get_phi360(idir)); |
109 |
#endif |
110 |
if (!inpXML) |
111 |
rbf = advect_rbf(idir, 15000); |
112 |
#ifdef DEBUG |
113 |
if (inpXML) |
114 |
fprintf(stderr, "Hemispherical %s: %.3f\n", |
115 |
(output_orient > 0 ? "reflection" : "transmission"), |
116 |
SDdirectHemi(idir, SDsampSp|SDsampDf | |
117 |
(output_orient > 0 ? |
118 |
SDsampR : SDsampT), &myBSDF)); |
119 |
else if (rbf == NULL) |
120 |
fputs("Empty RBF\n", stderr); |
121 |
else |
122 |
fprintf(stderr, "Hemispherical %s: %.3f\n", |
123 |
(output_orient > 0 ? "reflection" : "transmission"), |
124 |
rbf->vtotal); |
125 |
#endif |
126 |
printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n", |
127 |
colarr[n][0], colarr[n][1], colarr[n][2]); |
128 |
if (showPeaks && rbf != NULL) { |
129 |
printf("void plastic pmat\n0\n0\n5 %f %f %f .04 .08\n", |
130 |
1.-colarr[n][0], 1.-colarr[n][1], 1.-colarr[n][2]); |
131 |
for (i = 0; i < rbf->nrbf; i++) { |
132 |
ovec_from_pos(odir, rbf->rbfa[i].gx, rbf->rbfa[i].gy); |
133 |
bsdf = eval_rbfrep(rbf, odir) / (output_orient*odir[2]); |
134 |
bsdf = log(bsdf + 1e-5) - min_log; |
135 |
printf("pmat sphere p%d\n0\n0\n4 %f %f %f %f\n", |
136 |
i+1, odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf, |
137 |
.007*bsdf); |
138 |
} |
139 |
} |
140 |
fflush(stdout); |
141 |
sprintf(buf, "gensurf tmat bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
142 |
fp = popen(buf, "w"); |
143 |
if (fp == NULL) { |
144 |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
145 |
return(1); |
146 |
} |
147 |
for (i = 0; i < GRIDRES; i++) |
148 |
for (j = 0; j < GRIDRES; j++) { |
149 |
ovec_from_pos(odir, i, j); |
150 |
if (inpXML) { |
151 |
SDValue sval; |
152 |
if (SDreportError(SDevalBSDF(&sval, odir, |
153 |
idir, &myBSDF), stderr)) |
154 |
return(1); |
155 |
bsdf = sval.cieY; |
156 |
} else |
157 |
bsdf = eval_rbfrep(rbf, odir) / |
158 |
(output_orient*odir[2]); |
159 |
bsdf = log(bsdf + 1e-5) - min_log; |
160 |
fprintf(fp, "%.8e %.8e %.8e\n", |
161 |
odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf); |
162 |
} |
163 |
if (rbf != NULL) |
164 |
free(rbf); |
165 |
if (pclose(fp)) |
166 |
return(1); |
167 |
} |
168 |
return(0); |
169 |
} |