1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.6 |
static const char RCSid[] = "$Id: bsdf2rad.c,v 2.5 2013/11/26 17:33:55 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
greg |
2.4 |
* Plot 3-D BSDF output based on scattering interpolant or XML representation |
6 |
greg |
2.1 |
*/ |
7 |
|
|
|
8 |
|
|
#define _USE_MATH_DEFINES |
9 |
|
|
#include <stdio.h> |
10 |
greg |
2.4 |
#include <string.h> |
11 |
greg |
2.1 |
#include <stdlib.h> |
12 |
|
|
#include <math.h> |
13 |
greg |
2.5 |
#include "rtprocess.h" |
14 |
greg |
2.1 |
#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 |
greg |
2.2 |
int showPeaks = 0; |
32 |
greg |
2.4 |
int doTrans = 0; |
33 |
|
|
int inpXML = -1; |
34 |
|
|
RBFNODE *rbf = NULL; |
35 |
|
|
FILE *fp; |
36 |
greg |
2.1 |
char buf[128]; |
37 |
greg |
2.4 |
SDData myBSDF; |
38 |
greg |
2.1 |
double bsdf, min_log; |
39 |
greg |
2.4 |
FVECT idir, odir; |
40 |
greg |
2.1 |
int i, j, n; |
41 |
greg |
2.4 |
/* check arguments */ |
42 |
greg |
2.1 |
progname = argv[0]; |
43 |
greg |
2.2 |
if (argc > 1 && !strcmp(argv[1], "-p")) { |
44 |
|
|
++showPeaks; |
45 |
|
|
++argv; --argc; |
46 |
|
|
} |
47 |
greg |
2.4 |
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 |
greg |
2.2 |
fprintf(stderr, "Usage: %s [-p] bsdf.sir theta1 phi1 .. > output.rad\n", progname); |
59 |
greg |
2.4 |
fprintf(stderr, " Or: %s [-t] bsdf.xml theta1 phi1 .. > output.rad\n", progname); |
60 |
greg |
2.1 |
return(1); |
61 |
|
|
} |
62 |
|
|
/* load input */ |
63 |
greg |
2.4 |
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 |
greg |
2.1 |
} |
91 |
greg |
2.4 |
#ifdef DEBUG |
92 |
|
|
fprintf(stderr, "Minimum BSDF set to %.4f\n", bsdf_min); |
93 |
|
|
#endif |
94 |
greg |
2.6 |
min_log = log(bsdf_min*.5 + 1e-5); |
95 |
greg |
2.2 |
/* output BSDF rep. */ |
96 |
greg |
2.1 |
for (n = 0; (n < 6) & (2*n+3 < argc); n++) { |
97 |
greg |
2.4 |
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 |
greg |
2.3 |
#ifdef DEBUG |
107 |
greg |
2.4 |
fprintf(stderr, "Computing BSDF for incident direction (%.1f,%.1f)\n", |
108 |
|
|
get_theta180(idir), get_phi360(idir)); |
109 |
greg |
2.3 |
#endif |
110 |
greg |
2.4 |
if (!inpXML) |
111 |
|
|
rbf = advect_rbf(idir, 15000); |
112 |
greg |
2.3 |
#ifdef DEBUG |
113 |
greg |
2.4 |
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 |
greg |
2.1 |
else |
122 |
greg |
2.4 |
fprintf(stderr, "Hemispherical %s: %.3f\n", |
123 |
|
|
(output_orient > 0 ? "reflection" : "transmission"), |
124 |
|
|
rbf->vtotal); |
125 |
greg |
2.3 |
#endif |
126 |
greg |
2.2 |
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 |
greg |
2.4 |
ovec_from_pos(odir, rbf->rbfa[i].gx, rbf->rbfa[i].gy); |
133 |
|
|
bsdf = eval_rbfrep(rbf, odir) / (output_orient*odir[2]); |
134 |
greg |
2.2 |
bsdf = log(bsdf) - min_log; |
135 |
|
|
printf("pmat sphere p%d\n0\n0\n4 %f %f %f %f\n", |
136 |
greg |
2.4 |
i+1, odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf, |
137 |
greg |
2.2 |
.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 |
greg |
2.1 |
for (i = 0; i < GRIDRES; i++) |
148 |
|
|
for (j = 0; j < GRIDRES; j++) { |
149 |
greg |
2.4 |
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 |
greg |
2.1 |
bsdf = log(bsdf) - min_log; |
160 |
|
|
fprintf(fp, "%.8e %.8e %.8e\n", |
161 |
greg |
2.4 |
odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf); |
162 |
greg |
2.1 |
} |
163 |
|
|
if (rbf != NULL) |
164 |
|
|
free(rbf); |
165 |
|
|
if (pclose(fp)) |
166 |
|
|
return(1); |
167 |
|
|
} |
168 |
|
|
return(0); |
169 |
|
|
} |