1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.35 2019/04/23 23:48:33 greg Exp $"; |
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 <stdlib.h> |
13 |
#include <ctype.h> |
14 |
#include <math.h> |
15 |
#include "rtio.h" |
16 |
#include "platform.h" |
17 |
#include "bsdfrep.h" |
18 |
/* global argv[0] */ |
19 |
char *progname; |
20 |
|
21 |
typedef struct { |
22 |
const char *fname; /* input file path */ |
23 |
double theta, phi; /* input angles (degrees) */ |
24 |
double up_phi; /* azimuth for "up" direction */ |
25 |
int igp[2]; /* input grid position */ |
26 |
int isDSF; /* data is DSF (rather than BSDF)? */ |
27 |
int nspec; /* number of spectral samples */ |
28 |
long dstart; /* data start offset in file */ |
29 |
} PGINPUT; |
30 |
|
31 |
PGINPUT *inpfile; /* input files sorted by incidence */ |
32 |
int ninpfiles; /* number of input files */ |
33 |
|
34 |
int rev_orient = 0; /* shall we reverse surface orientation? */ |
35 |
|
36 |
/* Compare incident angles */ |
37 |
static int |
38 |
cmp_indir(const void *p1, const void *p2) |
39 |
{ |
40 |
const PGINPUT *inp1 = (const PGINPUT *)p1; |
41 |
const PGINPUT *inp2 = (const PGINPUT *)p2; |
42 |
int ydif = inp1->igp[1] - inp2->igp[1]; |
43 |
|
44 |
if (ydif) |
45 |
return(ydif); |
46 |
|
47 |
return(inp1->igp[0] - inp2->igp[0]); |
48 |
} |
49 |
|
50 |
/* Prepare a PAB-Opto input file by reading its header */ |
51 |
static int |
52 |
init_pabopto_inp(const int i, const char *fname) |
53 |
{ |
54 |
FILE *fp = fopen(fname, "r"); |
55 |
FVECT dv; |
56 |
char buf[2048]; |
57 |
int c; |
58 |
|
59 |
if (fp == NULL) { |
60 |
fputs(fname, stderr); |
61 |
fputs(": cannot open\n", stderr); |
62 |
return(0); |
63 |
} |
64 |
inpfile[i].fname = fname; |
65 |
inpfile[i].isDSF = -1; |
66 |
inpfile[i].nspec = 0; |
67 |
inpfile[i].up_phi = 0; |
68 |
inpfile[i].theta = inpfile[i].phi = -10001.; |
69 |
/* read header information */ |
70 |
while ((c = getc(fp)) == '#' || c == EOF) { |
71 |
char typ[64]; |
72 |
if (fgets(buf, sizeof(buf), fp) == NULL) { |
73 |
fputs(fname, stderr); |
74 |
fputs(": unexpected EOF\n", stderr); |
75 |
fclose(fp); |
76 |
return(0); |
77 |
} |
78 |
if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1) |
79 |
continue; |
80 |
if (sscanf(buf, "colorimetry: %s", typ) == 1) { |
81 |
if (!strcasecmp(typ, "CIE-XYZ")) |
82 |
inpfile[i].nspec = 3; |
83 |
else if (!strcasecmp(typ, "CIE-Y")) |
84 |
inpfile[i].nspec = 1; |
85 |
continue; |
86 |
} |
87 |
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
88 |
if (!strcasecmp(typ, "DSF")) |
89 |
inpfile[i].isDSF = 1; |
90 |
else if (!strcasecmp(typ, "BSDF") || |
91 |
!strcasecmp(typ, "BRDF") || |
92 |
!strcasecmp(typ, "BTDF")) |
93 |
inpfile[i].isDSF = 0; |
94 |
continue; |
95 |
} |
96 |
if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1) |
97 |
continue; |
98 |
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
99 |
continue; |
100 |
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
101 |
continue; |
102 |
if (sscanf(buf, "incident_angle %lf %lf", |
103 |
&inpfile[i].theta, &inpfile[i].phi) == 2) |
104 |
continue; |
105 |
} |
106 |
inpfile[i].dstart = ftell(fp) - 1; |
107 |
fclose(fp); |
108 |
if (inpfile[i].isDSF < 0) { |
109 |
fputs(fname, stderr); |
110 |
fputs(": unknown format\n", stderr); |
111 |
return(0); |
112 |
} |
113 |
if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) { |
114 |
fputs(fname, stderr); |
115 |
fputs(": unknown incident angle\n", stderr); |
116 |
return(0); |
117 |
} |
118 |
if (rev_orient) { /* reverse Z-axis to face outside */ |
119 |
inpfile[i].theta = 180. - inpfile[i].theta; |
120 |
inpfile[i].phi = 360. - inpfile[i].phi; |
121 |
} |
122 |
/* convert to Y-up orientation */ |
123 |
inpfile[i].phi += 90.-inpfile[i].up_phi; |
124 |
/* convert angle to grid position */ |
125 |
dv[2] = sin(M_PI/180.*inpfile[i].theta); |
126 |
dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2]; |
127 |
dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2]; |
128 |
dv[2] = sqrt(1. - dv[2]*dv[2]); |
129 |
if (inpfile[i].theta <= FTINY) |
130 |
inpfile[i].igp[0] = inpfile[i].igp[1] = grid_res/2 - 1; |
131 |
else |
132 |
pos_from_vec(inpfile[i].igp, dv); |
133 |
return(1); |
134 |
} |
135 |
|
136 |
/* Load a set of measurements corresponding to a particular incident angle */ |
137 |
static int |
138 |
add_pabopto_inp(const int i) |
139 |
{ |
140 |
FILE *fp = fopen(inpfile[i].fname, "r"); |
141 |
double theta_out, phi_out, val[3]; |
142 |
int n, c; |
143 |
|
144 |
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
145 |
fputs(inpfile[i].fname, stderr); |
146 |
fputs(": cannot open\n", stderr); |
147 |
return(0); |
148 |
} |
149 |
/* prepare input grid */ |
150 |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) { |
151 |
if (i) /* process previous incidence */ |
152 |
make_rbfrep(); |
153 |
#ifdef DEBUG |
154 |
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n", |
155 |
inpfile[i].theta, inpfile[i].phi); |
156 |
#endif |
157 |
if (inpfile[i].nspec) |
158 |
set_spectral_samples(inpfile[i].nspec); |
159 |
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
160 |
} |
161 |
#ifdef DEBUG |
162 |
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
163 |
#endif |
164 |
/* read scattering data */ |
165 |
while (fscanf(fp, "%lf %lf %lf", &theta_out, &phi_out, val) == 3) { |
166 |
for (n = 1; n < inpfile[i].nspec; n++) |
167 |
if (fscanf(fp, "%lf", val+n) != 1) { |
168 |
fprintf(stderr, "%s: warning: unexpected EOF\n", |
169 |
inpfile[i].fname); |
170 |
fclose(fp); |
171 |
return(1); |
172 |
} |
173 |
if (rev_orient) { /* reverse Z-axis to face outside */ |
174 |
theta_out = 180. - theta_out; |
175 |
phi_out = 360. - phi_out; |
176 |
} |
177 |
add_bsdf_data(theta_out, phi_out+90.-inpfile[i].up_phi, |
178 |
val, inpfile[i].isDSF); |
179 |
} |
180 |
n = 0; |
181 |
while ((c = getc(fp)) != EOF) |
182 |
n += !isspace(c); |
183 |
if (n) |
184 |
fprintf(stderr, |
185 |
"%s: warning: %d unexpected characters past EOD\n", |
186 |
inpfile[i].fname, n); |
187 |
fclose(fp); |
188 |
return(1); |
189 |
} |
190 |
|
191 |
#ifndef TEST_MAIN |
192 |
|
193 |
#define SYM_ILL '?' /* illegal symmetry value */ |
194 |
#define SYM_ISO 'I' /* isotropic */ |
195 |
#define SYM_QUAD 'Q' /* quadrilateral symmetry */ |
196 |
#define SYM_BILAT 'B' /* bilateral symmetry */ |
197 |
#define SYM_ANISO 'A' /* anisotropic */ |
198 |
|
199 |
static const char quadrant_rep[16][16] = { |
200 |
"in-plane","0-90","90-180","0-180", |
201 |
"180-270","0-90+180-270","90-270", |
202 |
"0-270","270-360","270-90", |
203 |
"90-180+270-360","270-180","180-360", |
204 |
"180-90","90-360","0-360" |
205 |
}; |
206 |
static const char quadrant_sym[16] = { |
207 |
SYM_ISO, SYM_QUAD, SYM_QUAD, SYM_BILAT, |
208 |
SYM_QUAD, SYM_ILL, SYM_BILAT, SYM_ILL, |
209 |
SYM_QUAD, SYM_BILAT, SYM_ILL, SYM_ILL, |
210 |
SYM_BILAT, SYM_ILL, SYM_ILL, SYM_ANISO |
211 |
}; |
212 |
|
213 |
/* Read in PAB-Opto BSDF files and output RBF interpolant */ |
214 |
int |
215 |
main(int argc, char *argv[]) |
216 |
{ |
217 |
extern int nprocs; |
218 |
const char *symmetry = "U"; |
219 |
int i; |
220 |
/* start header */ |
221 |
SET_FILE_BINARY(stdout); |
222 |
newheader("RADIANCE", stdout); |
223 |
printargs(argc, argv, stdout); |
224 |
fputnow(stdout); |
225 |
progname = argv[0]; /* get options */ |
226 |
while (argc > 2 && argv[1][0] == '-') { |
227 |
switch (argv[1][1]) { |
228 |
case 't': |
229 |
rev_orient = !rev_orient; |
230 |
break; |
231 |
case 'n': |
232 |
nprocs = atoi(argv[2]); |
233 |
argv++; argc--; |
234 |
break; |
235 |
case 's': |
236 |
symmetry = argv[2]; |
237 |
argv++; argc--; |
238 |
break; |
239 |
default: |
240 |
goto userr; |
241 |
} |
242 |
argv++; argc--; |
243 |
} |
244 |
/* initialize & sort inputs */ |
245 |
ninpfiles = argc - 1; |
246 |
if (ninpfiles < 2) |
247 |
goto userr; |
248 |
inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles); |
249 |
if (inpfile == NULL) |
250 |
return(1); |
251 |
for (i = 0; i < ninpfiles; i++) |
252 |
if (!init_pabopto_inp(i, argv[i+1])) |
253 |
return(1); |
254 |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir); |
255 |
/* compile measurements */ |
256 |
for (i = 0; i < ninpfiles; i++) |
257 |
if (!add_pabopto_inp(i)) |
258 |
return(1); |
259 |
make_rbfrep(); /* process last data set */ |
260 |
/* check input symmetry */ |
261 |
switch (toupper(symmetry[0])) { |
262 |
case 'U': /* unspecified symmetry */ |
263 |
if (quadrant_sym[inp_coverage] != SYM_ILL) |
264 |
break; /* anything legal goes */ |
265 |
fprintf(stderr, "%s: unsupported phi coverage (%s)\n", |
266 |
progname, quadrant_rep[inp_coverage]); |
267 |
return(1); |
268 |
case SYM_ISO: /* legal symmetry types */ |
269 |
case SYM_QUAD: |
270 |
case SYM_BILAT: |
271 |
case SYM_ANISO: |
272 |
if (quadrant_sym[inp_coverage] == toupper(symmetry[0])) |
273 |
break; /* matches spec */ |
274 |
fprintf(stderr, |
275 |
"%s: phi coverage (%s) does not match requested '%s' symmetry\n", |
276 |
progname, quadrant_rep[inp_coverage], symmetry); |
277 |
return(1); |
278 |
default: |
279 |
fprintf(stderr, |
280 |
"%s: -s option must be Isotropic, Quadrilateral, Bilateral, or Anisotropic\n", |
281 |
progname); |
282 |
return(1); |
283 |
} |
284 |
#ifdef DEBUG |
285 |
fprintf(stderr, "Input phi coverage (%s) has '%c' symmetry\n", |
286 |
quadrant_rep[inp_coverage], |
287 |
quadrant_sym[inp_coverage]); |
288 |
#endif |
289 |
build_mesh(); /* create interpolation */ |
290 |
save_bsdf_rep(stdout); /* write it out */ |
291 |
return(0); |
292 |
userr: |
293 |
fprintf(stderr, "Usage: %s [-t][-n nproc][-s symmetry] meas1.dat meas2.dat .. > bsdf.sir\n", |
294 |
progname); |
295 |
return(1); |
296 |
} |
297 |
|
298 |
#else /* TEST_MAIN */ |
299 |
|
300 |
/* Test main produces a Radiance model from the given input file */ |
301 |
int |
302 |
main(int argc, char *argv[]) |
303 |
{ |
304 |
PGINPUT pginp; |
305 |
char buf[128]; |
306 |
FILE *pfp; |
307 |
double bsdf, min_log; |
308 |
FVECT dir; |
309 |
int i, j, n; |
310 |
|
311 |
progname = argv[0]; |
312 |
if (argc != 2) { |
313 |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
314 |
return(1); |
315 |
} |
316 |
ninpfiles = 1; |
317 |
inpfile = &pginp; |
318 |
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
319 |
return(1); |
320 |
/* reduce data set */ |
321 |
if (make_rbfrep() == NULL) { |
322 |
fprintf(stderr, "%s: nothing to plot!\n", progname); |
323 |
exit(1); |
324 |
} |
325 |
#ifdef DEBUG |
326 |
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
327 |
#endif |
328 |
min_log = log(bsdf_min*.5 + 1e-5); |
329 |
#if 1 /* produce spheres at meas. */ |
330 |
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
331 |
n = 0; |
332 |
for (i = 0; i < grid_res; i++) |
333 |
for (j = 0; j < grid_res; j++) |
334 |
if (dsf_grid[i][j].sum.n > 0) { |
335 |
ovec_from_pos(dir, i, j); |
336 |
bsdf = dsf_grid[i][j].sum.v / |
337 |
((double)dsf_grid[i][j].sum.n*output_orient*dir[2]); |
338 |
if (bsdf <= bsdf_min*.6) |
339 |
continue; |
340 |
bsdf = log(bsdf + 1e-5) - min_log; |
341 |
ovec_from_pos(dir, i, j); |
342 |
printf("yellow sphere s%04d\n0\n0\n", ++n); |
343 |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
344 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
345 |
.007*bsdf); |
346 |
} |
347 |
#endif |
348 |
#if 1 /* spheres at RBF peaks */ |
349 |
puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n"); |
350 |
for (n = 0; n < dsf_list->nrbf; n++) { |
351 |
RBFVAL *rbf = &dsf_list->rbfa[n]; |
352 |
ovec_from_pos(dir, rbf->gx, rbf->gy); |
353 |
bsdf = eval_rbfrep(dsf_list, dir); |
354 |
bsdf = log(bsdf + 1e-5) - min_log; |
355 |
printf("red sphere p%04d\n0\n0\n", ++n); |
356 |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
357 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
358 |
.011*bsdf); |
359 |
} |
360 |
#endif |
361 |
#if 1 /* output continuous surface */ |
362 |
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
363 |
fflush(stdout); |
364 |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1); |
365 |
pfp = popen(buf, "w"); |
366 |
if (pfp == NULL) { |
367 |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
368 |
return(1); |
369 |
} |
370 |
for (i = 0; i < grid_res; i++) |
371 |
for (j = 0; j < grid_res; j++) { |
372 |
ovec_from_pos(dir, i, j); |
373 |
bsdf = eval_rbfrep(dsf_list, dir); |
374 |
bsdf = log(bsdf + 1e-5) - min_log; |
375 |
fprintf(pfp, "%.8e %.8e %.8e\n", |
376 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
377 |
} |
378 |
if (pclose(pfp) != 0) |
379 |
return(1); |
380 |
#endif |
381 |
return(0); |
382 |
} |
383 |
|
384 |
#endif /* TEST_MAIN */ |