3 |
|
#endif |
4 |
|
/* |
5 |
|
* Load measured BSDF data in PAB-Opto format. |
6 |
+ |
* Assumes that surface-normal (Z-axis) faces into room unless -t option given. |
7 |
|
* |
8 |
|
* G. Ward |
9 |
|
*/ |
10 |
|
|
11 |
+ |
#define _USE_MATH_DEFINES |
12 |
|
#include <stdio.h> |
13 |
|
#include <stdlib.h> |
14 |
|
#include <string.h> |
16 |
|
#include <math.h> |
17 |
|
#include "platform.h" |
18 |
|
#include "bsdfrep.h" |
19 |
+ |
#include "resolu.h" |
20 |
|
/* global argv[0] */ |
21 |
|
char *progname; |
22 |
|
|
23 |
|
typedef struct { |
24 |
|
const char *fname; /* input file path */ |
25 |
< |
double theta, phi; /* incident angles (in degrees) */ |
25 |
> |
double theta, phi; /* input angles (degrees) */ |
26 |
> |
double up_phi; /* azimuth for "up" direction */ |
27 |
> |
int igp[2]; /* input grid position */ |
28 |
|
int isDSF; /* data is DSF (rather than BSDF)? */ |
29 |
+ |
int nspec; /* number of spectral samples */ |
30 |
|
long dstart; /* data start offset in file */ |
31 |
|
} PGINPUT; |
32 |
|
|
33 |
|
PGINPUT *inpfile; /* input files sorted by incidence */ |
34 |
|
int ninpfiles; /* number of input files */ |
35 |
|
|
36 |
+ |
int rev_orient = 0; /* shall we reverse surface orientation? */ |
37 |
+ |
|
38 |
|
/* Compare incident angles */ |
39 |
|
static int |
40 |
< |
cmp_inang(const void *p1, const void *p2) |
40 |
> |
cmp_indir(const void *p1, const void *p2) |
41 |
|
{ |
42 |
|
const PGINPUT *inp1 = (const PGINPUT *)p1; |
43 |
|
const PGINPUT *inp2 = (const PGINPUT *)p2; |
44 |
+ |
int ydif = inp1->igp[1] - inp2->igp[1]; |
45 |
|
|
46 |
< |
if (inp1->theta > inp2->theta+FTINY) |
47 |
< |
return(1); |
48 |
< |
if (inp1->theta < inp2->theta-FTINY) |
49 |
< |
return(-1); |
41 |
< |
if (inp1->phi > inp2->phi+FTINY) |
42 |
< |
return(1); |
43 |
< |
if (inp1->phi < inp2->phi-FTINY) |
44 |
< |
return(-1); |
45 |
< |
return(0); |
46 |
> |
if (ydif) |
47 |
> |
return(ydif); |
48 |
> |
|
49 |
> |
return(inp1->igp[0] - inp2->igp[0]); |
50 |
|
} |
51 |
|
|
52 |
|
/* Prepare a PAB-Opto input file by reading its header */ |
54 |
|
init_pabopto_inp(const int i, const char *fname) |
55 |
|
{ |
56 |
|
FILE *fp = fopen(fname, "r"); |
57 |
+ |
FVECT dv; |
58 |
|
char buf[2048]; |
59 |
|
int c; |
60 |
|
|
65 |
|
} |
66 |
|
inpfile[i].fname = fname; |
67 |
|
inpfile[i].isDSF = -1; |
68 |
+ |
inpfile[i].nspec = 0; |
69 |
+ |
inpfile[i].up_phi = 0; |
70 |
|
inpfile[i].theta = inpfile[i].phi = -10001.; |
71 |
|
/* read header information */ |
72 |
|
while ((c = getc(fp)) == '#' || c == EOF) { |
73 |
< |
char typ[32]; |
73 |
> |
char typ[64]; |
74 |
|
if (fgets(buf, sizeof(buf), fp) == NULL) { |
75 |
|
fputs(fname, stderr); |
76 |
|
fputs(": unexpected EOF\n", stderr); |
77 |
|
fclose(fp); |
78 |
|
return(0); |
79 |
|
} |
80 |
+ |
if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1) |
81 |
+ |
continue; |
82 |
+ |
if (sscanf(buf, "colorimetry: %s", typ) == 1) { |
83 |
+ |
if (!strcasecmp(typ, "CIE-XYZ")) |
84 |
+ |
inpfile[i].nspec = 3; |
85 |
+ |
else if (!strcasecmp(typ, "CIE-Y")) |
86 |
+ |
inpfile[i].nspec = 1; |
87 |
+ |
continue; |
88 |
+ |
} |
89 |
|
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
90 |
< |
if (!strcasecmp(typ, "DSF")) { |
90 |
> |
if (!strcasecmp(typ, "DSF")) |
91 |
|
inpfile[i].isDSF = 1; |
92 |
< |
continue; |
93 |
< |
} |
94 |
< |
if (!strcasecmp(typ, "BSDF")) { |
92 |
> |
else if (!strcasecmp(typ, "BSDF") || |
93 |
> |
!strcasecmp(typ, "BRDF") || |
94 |
> |
!strcasecmp(typ, "BTDF")) |
95 |
|
inpfile[i].isDSF = 0; |
96 |
< |
continue; |
81 |
< |
} |
96 |
> |
continue; |
97 |
|
} |
98 |
+ |
if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1) |
99 |
+ |
continue; |
100 |
|
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
101 |
|
continue; |
102 |
|
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
117 |
|
fputs(": unknown incident angle\n", stderr); |
118 |
|
return(0); |
119 |
|
} |
120 |
< |
while (inpfile[i].phi < 0) /* normalize phi direction */ |
121 |
< |
inpfile[i].phi += 360.; |
120 |
> |
if (rev_orient) { /* reverse Z-axis to face outside */ |
121 |
> |
inpfile[i].theta = 180. - inpfile[i].theta; |
122 |
> |
inpfile[i].phi = 360. - inpfile[i].phi; |
123 |
> |
} |
124 |
> |
/* convert to Y-up orientation */ |
125 |
> |
inpfile[i].phi += 90.-inpfile[i].up_phi; |
126 |
> |
/* convert angle to grid position */ |
127 |
> |
dv[2] = sin(M_PI/180.*inpfile[i].theta); |
128 |
> |
dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2]; |
129 |
> |
dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2]; |
130 |
> |
dv[2] = sqrt(1. - dv[2]*dv[2]); |
131 |
> |
pos_from_vec(inpfile[i].igp, dv); |
132 |
|
return(1); |
133 |
|
} |
134 |
|
|
136 |
|
static int |
137 |
|
add_pabopto_inp(const int i) |
138 |
|
{ |
139 |
< |
FILE *fp = fopen(inpfile[i].fname, "r"); |
140 |
< |
double theta_out, phi_out, val; |
141 |
< |
int n, c; |
139 |
> |
FILE *fp = fopen(inpfile[i].fname, "r"); |
140 |
> |
double theta_out, phi_out, val[3]; |
141 |
> |
int n, c; |
142 |
|
|
143 |
|
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
144 |
|
fputs(inpfile[i].fname, stderr); |
146 |
|
return(0); |
147 |
|
} |
148 |
|
/* prepare input grid */ |
149 |
< |
if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) { |
150 |
< |
if (i) /* need to process previous incidence */ |
149 |
> |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) { |
150 |
> |
if (i) /* process previous incidence */ |
151 |
|
make_rbfrep(); |
152 |
|
#ifdef DEBUG |
153 |
< |
fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n", |
153 |
> |
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n", |
154 |
|
inpfile[i].theta, inpfile[i].phi); |
155 |
|
#endif |
156 |
+ |
if (inpfile[i].nspec) |
157 |
+ |
set_spectral_samples(inpfile[i].nspec); |
158 |
|
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
159 |
|
} |
160 |
|
#ifdef DEBUG |
161 |
|
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
162 |
|
#endif |
163 |
|
/* read scattering data */ |
164 |
< |
while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) |
165 |
< |
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF); |
164 |
> |
while (fscanf(fp, "%lf %lf %lf", &theta_out, &phi_out, val) == 3) { |
165 |
> |
for (n = 1; n < inpfile[i].nspec; n++) |
166 |
> |
if (fscanf(fp, "%lf", val+n) != 1) { |
167 |
> |
fprintf(stderr, "%s: warning: unexpected EOF\n", |
168 |
> |
inpfile[i].fname); |
169 |
> |
fclose(fp); |
170 |
> |
return(1); |
171 |
> |
} |
172 |
> |
if (rev_orient) { /* reverse Z-axis to face outside */ |
173 |
> |
theta_out = 180. - theta_out; |
174 |
> |
phi_out = 360. - phi_out; |
175 |
> |
} |
176 |
> |
add_bsdf_data(theta_out, phi_out+90.-inpfile[i].up_phi, |
177 |
> |
val, inpfile[i].isDSF); |
178 |
> |
} |
179 |
|
n = 0; |
180 |
|
while ((c = getc(fp)) != EOF) |
181 |
|
n += !isspace(c); |
187 |
|
return(1); |
188 |
|
} |
189 |
|
|
190 |
< |
#if 1 |
190 |
> |
#ifndef TEST_MAIN |
191 |
|
/* Read in PAB-Opto BSDF files and output RBF interpolant */ |
192 |
|
int |
193 |
|
main(int argc, char *argv[]) |
202 |
|
progname = argv[0]; /* get options */ |
203 |
|
while (argc > 2 && argv[1][0] == '-') { |
204 |
|
switch (argv[1][1]) { |
205 |
+ |
case 't': |
206 |
+ |
rev_orient = !rev_orient; |
207 |
+ |
break; |
208 |
|
case 'n': |
209 |
|
nprocs = atoi(argv[2]); |
210 |
+ |
argv++; argc--; |
211 |
|
break; |
212 |
|
default: |
213 |
|
goto userr; |
214 |
|
} |
215 |
< |
argv += 2; argc -= 2; |
215 |
> |
argv++; argc--; |
216 |
|
} |
217 |
|
/* initialize & sort inputs */ |
218 |
|
ninpfiles = argc - 1; |
224 |
|
for (i = 0; i < ninpfiles; i++) |
225 |
|
if (!init_pabopto_inp(i, argv[i+1])) |
226 |
|
return(1); |
227 |
< |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang); |
227 |
> |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir); |
228 |
|
/* compile measurements */ |
229 |
|
for (i = 0; i < ninpfiles; i++) |
230 |
|
if (!add_pabopto_inp(i)) |
234 |
|
save_bsdf_rep(stdout); /* write it out */ |
235 |
|
return(0); |
236 |
|
userr: |
237 |
< |
fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n", |
237 |
> |
fprintf(stderr, "Usage: %s [-t][-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n", |
238 |
|
progname); |
239 |
|
return(1); |
240 |
|
} |
250 |
|
FVECT dir; |
251 |
|
int i, j, n; |
252 |
|
|
253 |
+ |
progname = argv[0]; |
254 |
|
if (argc != 2) { |
255 |
< |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]); |
255 |
> |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
256 |
|
return(1); |
257 |
|
} |
258 |
|
ninpfiles = 1; |
260 |
|
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
261 |
|
return(1); |
262 |
|
/* reduce data set */ |
263 |
< |
make_rbfrep(); |
264 |
< |
/* produce spheres at meas. */ |
263 |
> |
if (make_rbfrep() == NULL) { |
264 |
> |
fprintf(stderr, "%s: nothing to plot!\n", progname); |
265 |
> |
exit(1); |
266 |
> |
} |
267 |
> |
#ifdef DEBUG |
268 |
> |
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
269 |
> |
#endif |
270 |
> |
min_log = log(bsdf_min*.5 + 1e-5); |
271 |
> |
#if 1 /* produce spheres at meas. */ |
272 |
|
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
219 |
– |
min_log = log(bsdf_min*.5); |
273 |
|
n = 0; |
274 |
< |
for (i = 0; i < GRIDRES; i++) |
275 |
< |
for (j = 0; j < GRIDRES; j++) |
276 |
< |
if (dsf_grid[i][j].nval > 0) { |
274 |
> |
for (i = 0; i < grid_res; i++) |
275 |
> |
for (j = 0; j < grid_res; j++) |
276 |
> |
if (dsf_grid[i][j].sum.n > 0) { |
277 |
|
ovec_from_pos(dir, i, j); |
278 |
< |
bsdf = dsf_grid[i][j].vsum/(dsf_grid[i][j].nval*dir[2]); |
278 |
> |
bsdf = dsf_grid[i][j].sum.v / |
279 |
> |
((double)dsf_grid[i][j].sum.n*output_orient*dir[2]); |
280 |
|
if (bsdf <= bsdf_min*.6) |
281 |
|
continue; |
282 |
< |
bsdf = log(bsdf) - min_log; |
282 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
283 |
|
ovec_from_pos(dir, i, j); |
284 |
|
printf("yellow sphere s%04d\n0\n0\n", ++n); |
285 |
|
printf("4 %.6g %.6g %.6g %.6g\n\n", |
286 |
|
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
287 |
|
.007*bsdf); |
288 |
|
} |
289 |
< |
/* output continuous surface */ |
289 |
> |
#endif |
290 |
> |
#if 1 /* spheres at RBF peaks */ |
291 |
> |
puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n"); |
292 |
> |
for (n = 0; n < dsf_list->nrbf; n++) { |
293 |
> |
RBFVAL *rbf = &dsf_list->rbfa[n]; |
294 |
> |
ovec_from_pos(dir, rbf->gx, rbf->gy); |
295 |
> |
bsdf = eval_rbfrep(dsf_list, dir); |
296 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
297 |
> |
printf("red sphere p%04d\n0\n0\n", ++n); |
298 |
> |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
299 |
> |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
300 |
> |
.011*bsdf); |
301 |
> |
} |
302 |
> |
#endif |
303 |
> |
#if 1 /* output continuous surface */ |
304 |
|
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
305 |
|
fflush(stdout); |
306 |
< |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
306 |
> |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1); |
307 |
|
pfp = popen(buf, "w"); |
308 |
|
if (pfp == NULL) { |
309 |
< |
fputs(buf, stderr); |
242 |
< |
fputs(": cannot start command\n", stderr); |
309 |
> |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
310 |
|
return(1); |
311 |
|
} |
312 |
< |
for (i = 0; i < GRIDRES; i++) |
313 |
< |
for (j = 0; j < GRIDRES; j++) { |
312 |
> |
for (i = 0; i < grid_res; i++) |
313 |
> |
for (j = 0; j < grid_res; j++) { |
314 |
|
ovec_from_pos(dir, i, j); |
315 |
< |
bsdf = eval_rbfrep(dsf_list, dir) / dir[2]; |
316 |
< |
bsdf = log(bsdf) - min_log; |
315 |
> |
bsdf = eval_rbfrep(dsf_list, dir); |
316 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
317 |
|
fprintf(pfp, "%.8e %.8e %.8e\n", |
318 |
|
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
319 |
|
} |
320 |
< |
return(pclose(pfp)==0 ? 0 : 1); |
320 |
> |
if (pclose(pfp) != 0) |
321 |
> |
return(1); |
322 |
> |
#endif |
323 |
> |
return(0); |
324 |
|
} |
325 |
|
#endif |