2 |
|
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
< |
* Plot 3-D BSDF output based on scattering interpolant representation |
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] = { |
29 |
|
main(int argc, char *argv[]) |
30 |
|
{ |
31 |
|
int showPeaks = 0; |
32 |
< |
char buf[128]; |
32 |
> |
int doTrans = 0; |
33 |
> |
int inpXML = -1; |
34 |
> |
RBFNODE *rbf = NULL; |
35 |
|
FILE *fp; |
36 |
< |
RBFNODE *rbf; |
36 |
> |
char buf[128]; |
37 |
> |
SDData myBSDF; |
38 |
|
double bsdf, min_log; |
39 |
< |
FVECT dir; |
39 |
> |
FVECT idir, odir; |
40 |
|
int i, j, n; |
41 |
< |
|
41 |
> |
/* check arguments */ |
42 |
|
progname = argv[0]; |
43 |
|
if (argc > 1 && !strcmp(argv[1], "-p")) { |
44 |
|
++showPeaks; |
45 |
|
++argv; --argc; |
46 |
|
} |
47 |
< |
if (argc < 4) { |
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 ((fp = fopen(argv[1], "rb")) == NULL) { |
64 |
< |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
65 |
< |
progname, argv[1]); |
66 |
< |
return(1); |
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 |
< |
if (!load_bsdf_rep(fp)) |
92 |
< |
return(1); |
93 |
< |
fclose(fp); |
94 |
< |
min_log = log(bsdf_min*.5); |
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 |
< |
dir[2] = sin((M_PI/180.)*atof(argv[2*n+2])); |
98 |
< |
dir[0] = dir[2] * cos((M_PI/180.)*atof(argv[2*n+3])); |
99 |
< |
dir[1] = dir[2] * sin((M_PI/180.)*atof(argv[2*n+3])); |
100 |
< |
dir[2] = input_orient * sqrt(1. - dir[2]*dir[2]); |
101 |
< |
fprintf(stderr, "Computing DSF for incident direction (%.1f,%.1f)\n", |
102 |
< |
get_theta180(dir), get_phi360(dir)); |
103 |
< |
rbf = advect_rbf(dir, 15000); |
104 |
< |
if (rbf == NULL) |
105 |
< |
fputs("NULL RBF\n", stderr); |
97 |
> |
double theta = (M_PI/180.)*atof(argv[2*n+2]); |
98 |
> |
double phi = (M_PI/180.)*atof(argv[2*n+3]); |
99 |
> |
if (theta < -FTINY) { |
100 |
> |
fprintf(stderr, "%s: theta values must be positive\n", |
101 |
> |
progname); |
102 |
> |
return(1); |
103 |
> |
} |
104 |
> |
if (inpXML) { |
105 |
> |
input_orient = (theta <= M_PI/2.) ? 1 : -1; |
106 |
> |
output_orient = doTrans ? -input_orient : input_orient; |
107 |
> |
} |
108 |
> |
idir[2] = sin(theta); |
109 |
> |
idir[0] = idir[2] * cos(phi); |
110 |
> |
idir[1] = idir[2] * sin(phi); |
111 |
> |
idir[2] = input_orient * sqrt(1. - idir[2]*idir[2]); |
112 |
> |
#ifdef DEBUG |
113 |
> |
fprintf(stderr, "Computing BSDF for incident direction (%.1f,%.1f)\n", |
114 |
> |
get_theta180(idir), get_phi360(idir)); |
115 |
> |
#endif |
116 |
> |
if (!inpXML) |
117 |
> |
rbf = advect_rbf(idir, 15000); |
118 |
> |
#ifdef DEBUG |
119 |
> |
if (inpXML) |
120 |
> |
fprintf(stderr, "Hemispherical %s: %.3f\n", |
121 |
> |
(output_orient > 0 ? "reflection" : "transmission"), |
122 |
> |
SDdirectHemi(idir, SDsampSp|SDsampDf | |
123 |
> |
(output_orient > 0 ? |
124 |
> |
SDsampR : SDsampT), &myBSDF)); |
125 |
> |
else if (rbf == NULL) |
126 |
> |
fputs("Empty RBF\n", stderr); |
127 |
|
else |
128 |
< |
fprintf(stderr, "Hemispherical reflectance: %.3f\n", rbf->vtotal); |
128 |
> |
fprintf(stderr, "Hemispherical %s: %.3f\n", |
129 |
> |
(output_orient > 0 ? "reflection" : "transmission"), |
130 |
> |
rbf->vtotal); |
131 |
> |
#endif |
132 |
|
printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n", |
133 |
|
colarr[n][0], colarr[n][1], colarr[n][2]); |
134 |
|
if (showPeaks && rbf != NULL) { |
135 |
|
printf("void plastic pmat\n0\n0\n5 %f %f %f .04 .08\n", |
136 |
|
1.-colarr[n][0], 1.-colarr[n][1], 1.-colarr[n][2]); |
137 |
|
for (i = 0; i < rbf->nrbf; i++) { |
138 |
< |
ovec_from_pos(dir, rbf->rbfa[i].gx, rbf->rbfa[i].gy); |
139 |
< |
bsdf = eval_rbfrep(rbf, dir) / (output_orient*dir[2]); |
140 |
< |
bsdf = log(bsdf) - min_log; |
138 |
> |
ovec_from_pos(odir, rbf->rbfa[i].gx, rbf->rbfa[i].gy); |
139 |
> |
bsdf = eval_rbfrep(rbf, odir) / (output_orient*odir[2]); |
140 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
141 |
|
printf("pmat sphere p%d\n0\n0\n4 %f %f %f %f\n", |
142 |
< |
i+1, dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
142 |
> |
i+1, odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf, |
143 |
|
.007*bsdf); |
144 |
|
} |
145 |
|
} |
146 |
|
fflush(stdout); |
147 |
< |
sprintf(buf, "gensurf tmat bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
147 |
> |
sprintf(buf, "gensurf tmat bsdf%d - - - %d %d", n+1, |
148 |
> |
GRIDRES-1, GRIDRES-1); |
149 |
|
fp = popen(buf, "w"); |
150 |
|
if (fp == NULL) { |
151 |
|
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
153 |
|
} |
154 |
|
for (i = 0; i < GRIDRES; i++) |
155 |
|
for (j = 0; j < GRIDRES; j++) { |
156 |
< |
ovec_from_pos(dir, i, j); |
157 |
< |
bsdf = eval_rbfrep(rbf, dir) / (output_orient*dir[2]); |
158 |
< |
bsdf = log(bsdf) - min_log; |
156 |
> |
ovec_from_pos(odir, i, j); |
157 |
> |
if (inpXML) { |
158 |
> |
SDValue sval; |
159 |
> |
if (SDreportError(SDevalBSDF(&sval, odir, |
160 |
> |
idir, &myBSDF), stderr)) |
161 |
> |
return(1); |
162 |
> |
bsdf = sval.cieY; |
163 |
> |
} else |
164 |
> |
bsdf = eval_rbfrep(rbf, odir) / |
165 |
> |
(output_orient*odir[2]); |
166 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
167 |
|
fprintf(fp, "%.8e %.8e %.8e\n", |
168 |
< |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
168 |
> |
odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf); |
169 |
|
} |
170 |
|
if (rbf != NULL) |
171 |
|
free(rbf); |