7 |
|
* G. Ward |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#define _USE_MATH_DEFINES |
11 |
|
#include <stdio.h> |
12 |
|
#include <stdlib.h> |
13 |
|
#include <string.h> |
14 |
|
#include <ctype.h> |
15 |
+ |
#include <math.h> |
16 |
|
#include "platform.h" |
17 |
|
#include "bsdfrep.h" |
18 |
+ |
#include "resolu.h" |
19 |
|
/* global argv[0] */ |
20 |
|
char *progname; |
21 |
|
|
22 |
|
typedef struct { |
23 |
|
const char *fname; /* input file path */ |
24 |
< |
double theta, phi; /* incident angles (in degrees) */ |
24 |
> |
double theta, phi; /* input angles */ |
25 |
> |
int igp[2]; /* input grid position */ |
26 |
|
int isDSF; /* data is DSF (rather than BSDF)? */ |
27 |
|
long dstart; /* data start offset in file */ |
28 |
|
} PGINPUT; |
32 |
|
|
33 |
|
/* Compare incident angles */ |
34 |
|
static int |
35 |
< |
cmp_inang(const void *p1, const void *p2) |
35 |
> |
cmp_indir(const void *p1, const void *p2) |
36 |
|
{ |
37 |
|
const PGINPUT *inp1 = (const PGINPUT *)p1; |
38 |
|
const PGINPUT *inp2 = (const PGINPUT *)p2; |
39 |
+ |
int ydif = inp1->igp[1] - inp2->igp[1]; |
40 |
|
|
41 |
< |
if (inp1->theta > inp2->theta+FTINY) |
42 |
< |
return(1); |
43 |
< |
if (inp1->theta < inp2->theta-FTINY) |
44 |
< |
return(-1); |
40 |
< |
if (inp1->phi > inp2->phi+FTINY) |
41 |
< |
return(1); |
42 |
< |
if (inp1->phi < inp2->phi-FTINY) |
43 |
< |
return(-1); |
44 |
< |
return(0); |
41 |
> |
if (ydif) |
42 |
> |
return(ydif); |
43 |
> |
|
44 |
> |
return(inp1->igp[0] - inp2->igp[0]); |
45 |
|
} |
46 |
|
|
47 |
|
/* Prepare a PAB-Opto input file by reading its header */ |
49 |
|
init_pabopto_inp(const int i, const char *fname) |
50 |
|
{ |
51 |
|
FILE *fp = fopen(fname, "r"); |
52 |
+ |
FVECT dv; |
53 |
|
char buf[2048]; |
54 |
|
int c; |
55 |
|
|
63 |
|
inpfile[i].theta = inpfile[i].phi = -10001.; |
64 |
|
/* read header information */ |
65 |
|
while ((c = getc(fp)) == '#' || c == EOF) { |
66 |
+ |
char typ[32]; |
67 |
|
if (fgets(buf, sizeof(buf), fp) == NULL) { |
68 |
|
fputs(fname, stderr); |
69 |
|
fputs(": unexpected EOF\n", stderr); |
70 |
|
fclose(fp); |
71 |
|
return(0); |
72 |
|
} |
73 |
< |
if (!strcmp(buf, "format: theta phi DSF\n")) { |
72 |
< |
inpfile[i].isDSF = 1; |
73 |
> |
if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1) |
74 |
|
continue; |
75 |
+ |
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
76 |
+ |
if (!strcasecmp(typ, "DSF")) { |
77 |
+ |
inpfile[i].isDSF = 1; |
78 |
+ |
continue; |
79 |
+ |
} |
80 |
+ |
if (!strcasecmp(typ, "BSDF") || |
81 |
+ |
!strcasecmp(typ, "BRDF") || |
82 |
+ |
!strcasecmp(typ, "BTDF")) { |
83 |
+ |
inpfile[i].isDSF = 0; |
84 |
+ |
continue; |
85 |
+ |
} |
86 |
|
} |
75 |
– |
if (!strcmp(buf, "format: theta phi BSDF\n")) { |
76 |
– |
inpfile[i].isDSF = 0; |
77 |
– |
continue; |
78 |
– |
} |
87 |
|
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
88 |
|
continue; |
89 |
|
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
104 |
|
fputs(": unknown incident angle\n", stderr); |
105 |
|
return(0); |
106 |
|
} |
107 |
+ |
/* convert angle to grid position */ |
108 |
+ |
dv[2] = sin(M_PI/180.*inpfile[i].theta); |
109 |
+ |
dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2]; |
110 |
+ |
dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2]; |
111 |
+ |
dv[2] = sqrt(1. - dv[2]*dv[2]); |
112 |
+ |
pos_from_vec(inpfile[i].igp, dv); |
113 |
|
return(1); |
114 |
|
} |
115 |
|
|
117 |
|
static int |
118 |
|
add_pabopto_inp(const int i) |
119 |
|
{ |
120 |
< |
FILE *fp = fopen(inpfile[i].fname, "r"); |
121 |
< |
double theta_out, phi_out, val; |
122 |
< |
int n, c; |
120 |
> |
FILE *fp = fopen(inpfile[i].fname, "r"); |
121 |
> |
double theta_out, phi_out, val; |
122 |
> |
int n, c; |
123 |
|
|
124 |
|
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
125 |
|
fputs(inpfile[i].fname, stderr); |
126 |
|
fputs(": cannot open\n", stderr); |
127 |
|
return(0); |
128 |
|
} |
115 |
– |
#ifdef DEBUG |
116 |
– |
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
117 |
– |
#endif |
129 |
|
/* prepare input grid */ |
130 |
< |
if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) { |
131 |
< |
if (i) /* need to process previous incidence */ |
130 |
> |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) { |
131 |
> |
if (i) /* process previous incidence */ |
132 |
|
make_rbfrep(); |
133 |
|
#ifdef DEBUG |
134 |
< |
fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n", |
134 |
> |
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n", |
135 |
|
inpfile[i].theta, inpfile[i].phi); |
136 |
|
#endif |
137 |
|
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
138 |
|
} |
139 |
+ |
#ifdef DEBUG |
140 |
+ |
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
141 |
+ |
#endif |
142 |
|
/* read scattering data */ |
143 |
|
while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) |
144 |
|
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF); |
153 |
|
return(1); |
154 |
|
} |
155 |
|
|
156 |
+ |
#ifndef TEST_MAIN |
157 |
|
/* Read in PAB-Opto BSDF files and output RBF interpolant */ |
158 |
|
int |
159 |
|
main(int argc, char *argv[]) |
186 |
|
for (i = 0; i < ninpfiles; i++) |
187 |
|
if (!init_pabopto_inp(i, argv[i+1])) |
188 |
|
return(1); |
189 |
< |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang); |
189 |
> |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_indir); |
190 |
|
/* compile measurements */ |
191 |
|
for (i = 0; i < ninpfiles; i++) |
192 |
|
if (!add_pabopto_inp(i)) |
200 |
|
progname); |
201 |
|
return(1); |
202 |
|
} |
203 |
+ |
#else |
204 |
+ |
/* Test main produces a Radiance model from the given input file */ |
205 |
+ |
int |
206 |
+ |
main(int argc, char *argv[]) |
207 |
+ |
{ |
208 |
+ |
PGINPUT pginp; |
209 |
+ |
char buf[128]; |
210 |
+ |
FILE *pfp; |
211 |
+ |
double bsdf, min_log; |
212 |
+ |
FVECT dir; |
213 |
+ |
int i, j, n; |
214 |
+ |
|
215 |
+ |
progname = argv[0]; |
216 |
+ |
if (argc != 2) { |
217 |
+ |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
218 |
+ |
return(1); |
219 |
+ |
} |
220 |
+ |
ninpfiles = 1; |
221 |
+ |
inpfile = &pginp; |
222 |
+ |
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
223 |
+ |
return(1); |
224 |
+ |
/* reduce data set */ |
225 |
+ |
if (make_rbfrep() == NULL) { |
226 |
+ |
fprintf(stderr, "%s: nothing to plot!\n", progname); |
227 |
+ |
exit(1); |
228 |
+ |
} |
229 |
+ |
#ifdef DEBUG |
230 |
+ |
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
231 |
+ |
#endif |
232 |
+ |
min_log = log(bsdf_min*.5 + 1e-5); |
233 |
+ |
#if 1 /* produce spheres at meas. */ |
234 |
+ |
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
235 |
+ |
n = 0; |
236 |
+ |
for (i = 0; i < GRIDRES; i++) |
237 |
+ |
for (j = 0; j < GRIDRES; j++) |
238 |
+ |
if (dsf_grid[i][j].sum.n > 0) { |
239 |
+ |
ovec_from_pos(dir, i, j); |
240 |
+ |
bsdf = dsf_grid[i][j].sum.v / |
241 |
+ |
(dsf_grid[i][j].sum.n*output_orient*dir[2]); |
242 |
+ |
if (bsdf <= bsdf_min*.6) |
243 |
+ |
continue; |
244 |
+ |
bsdf = log(bsdf + 1e-5) - min_log; |
245 |
+ |
ovec_from_pos(dir, i, j); |
246 |
+ |
printf("yellow sphere s%04d\n0\n0\n", ++n); |
247 |
+ |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
248 |
+ |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
249 |
+ |
.007*bsdf); |
250 |
+ |
} |
251 |
+ |
#endif |
252 |
+ |
#if 1 /* spheres at RBF peaks */ |
253 |
+ |
puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n"); |
254 |
+ |
for (n = 0; n < dsf_list->nrbf; n++) { |
255 |
+ |
RBFVAL *rbf = &dsf_list->rbfa[n]; |
256 |
+ |
ovec_from_pos(dir, rbf->gx, rbf->gy); |
257 |
+ |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
258 |
+ |
bsdf = log(bsdf + 1e-5) - min_log; |
259 |
+ |
printf("red sphere p%04d\n0\n0\n", ++n); |
260 |
+ |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
261 |
+ |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
262 |
+ |
.011*bsdf); |
263 |
+ |
} |
264 |
+ |
#endif |
265 |
+ |
#if 1 /* output continuous surface */ |
266 |
+ |
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
267 |
+ |
fflush(stdout); |
268 |
+ |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
269 |
+ |
pfp = popen(buf, "w"); |
270 |
+ |
if (pfp == NULL) { |
271 |
+ |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
272 |
+ |
return(1); |
273 |
+ |
} |
274 |
+ |
for (i = 0; i < GRIDRES; i++) |
275 |
+ |
for (j = 0; j < GRIDRES; j++) { |
276 |
+ |
ovec_from_pos(dir, i, j); |
277 |
+ |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
278 |
+ |
bsdf = log(bsdf + 1e-5) - min_log; |
279 |
+ |
fprintf(pfp, "%.8e %.8e %.8e\n", |
280 |
+ |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
281 |
+ |
} |
282 |
+ |
if (pclose(pfp) != 0) |
283 |
+ |
return(1); |
284 |
+ |
#endif |
285 |
+ |
return(0); |
286 |
+ |
} |
287 |
+ |
#endif |