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 |
11 |
– |
#include <stdio.h> |
12 |
|
#include <stdlib.h> |
13 |
– |
#include <string.h> |
13 |
|
#include <ctype.h> |
14 |
|
#include <math.h> |
15 |
+ |
#include "rtio.h" |
16 |
|
#include "platform.h" |
17 |
|
#include "bsdfrep.h" |
18 |
– |
#include "resolu.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 */ |
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 */ |
31 |
– |
int ninpfiles; /* number of input files */ |
32 |
|
|
33 |
+ |
int rev_orient = 0; /* shall we reverse surface orientation? */ |
34 |
+ |
|
35 |
|
/* Compare incident angles */ |
36 |
|
static int |
37 |
|
cmp_indir(const void *p1, const void *p2) |
46 |
|
return(inp1->igp[0] - inp2->igp[0]); |
47 |
|
} |
48 |
|
|
49 |
+ |
/* Assign grid position from theta and phi */ |
50 |
+ |
static void |
51 |
+ |
set_grid_pos(PGINPUT *pip) |
52 |
+ |
{ |
53 |
+ |
FVECT dv; |
54 |
+ |
|
55 |
+ |
if (pip->theta <= FTINY) { |
56 |
+ |
pip->igp[0] = pip->igp[1] = grid_res/2 - 1; |
57 |
+ |
return; |
58 |
+ |
} |
59 |
+ |
dv[2] = sin(M_PI/180.*pip->theta); |
60 |
+ |
dv[0] = cos(M_PI/180.*pip->phi)*dv[2]; |
61 |
+ |
dv[1] = sin(M_PI/180.*pip->phi)*dv[2]; |
62 |
+ |
dv[2] = sqrt(1. - dv[2]*dv[2]); |
63 |
+ |
pos_from_vec(pip->igp, dv); |
64 |
+ |
} |
65 |
+ |
|
66 |
|
/* Prepare a PAB-Opto input file by reading its header */ |
67 |
|
static int |
68 |
|
init_pabopto_inp(const int i, const char *fname) |
69 |
|
{ |
70 |
|
FILE *fp = fopen(fname, "r"); |
52 |
– |
FVECT dv; |
71 |
|
char buf[2048]; |
72 |
|
int c; |
73 |
|
|
78 |
|
} |
79 |
|
inpfile[i].fname = fname; |
80 |
|
inpfile[i].isDSF = -1; |
81 |
+ |
inpfile[i].nspec = 0; |
82 |
+ |
inpfile[i].up_phi = 0; |
83 |
|
inpfile[i].theta = inpfile[i].phi = -10001.; |
84 |
|
/* read header information */ |
85 |
|
while ((c = getc(fp)) == '#' || c == EOF) { |
86 |
< |
char typ[32]; |
86 |
> |
char typ[64]; |
87 |
|
if (fgets(buf, sizeof(buf), fp) == NULL) { |
88 |
|
fputs(fname, stderr); |
89 |
|
fputs(": unexpected EOF\n", stderr); |
92 |
|
} |
93 |
|
if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1) |
94 |
|
continue; |
95 |
+ |
if (sscanf(buf, "colorimetry: %s", typ) == 1) { |
96 |
+ |
if (!strcasecmp(typ, "CIE-XYZ")) |
97 |
+ |
inpfile[i].nspec = 3; |
98 |
+ |
else if (!strcasecmp(typ, "CIE-Y")) |
99 |
+ |
inpfile[i].nspec = 1; |
100 |
+ |
continue; |
101 |
+ |
} |
102 |
|
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
103 |
< |
if (!strcasecmp(typ, "DSF")) { |
103 |
> |
if (!strcasecmp(typ, "DSF")) |
104 |
|
inpfile[i].isDSF = 1; |
105 |
< |
continue; |
79 |
< |
} |
80 |
< |
if (!strcasecmp(typ, "BSDF") || |
105 |
> |
else if (!strcasecmp(typ, "BSDF") || |
106 |
|
!strcasecmp(typ, "BRDF") || |
107 |
< |
!strcasecmp(typ, "BTDF")) { |
107 |
> |
!strcasecmp(typ, "BTDF")) |
108 |
|
inpfile[i].isDSF = 0; |
109 |
< |
continue; |
85 |
< |
} |
109 |
> |
continue; |
110 |
|
} |
111 |
+ |
if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1) |
112 |
+ |
continue; |
113 |
|
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
114 |
|
continue; |
115 |
|
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
130 |
|
fputs(": unknown incident angle\n", stderr); |
131 |
|
return(0); |
132 |
|
} |
133 |
+ |
if (rev_orient) { /* reverse Z-axis to face outside */ |
134 |
+ |
inpfile[i].theta = 180. - inpfile[i].theta; |
135 |
+ |
inpfile[i].phi = 360. - inpfile[i].phi; |
136 |
+ |
} |
137 |
+ |
/* convert to Y-up orientation */ |
138 |
+ |
inpfile[i].phi += 90.-inpfile[i].up_phi; |
139 |
|
/* convert angle to grid position */ |
140 |
< |
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); |
140 |
> |
set_grid_pos(&inpfile[i]); |
141 |
|
return(1); |
142 |
|
} |
143 |
|
|
146 |
|
add_pabopto_inp(const int i) |
147 |
|
{ |
148 |
|
FILE *fp = fopen(inpfile[i].fname, "r"); |
149 |
< |
double theta_out, phi_out, val; |
149 |
> |
double theta_out, phi_out, val[3]; |
150 |
|
int n, c; |
151 |
|
|
152 |
|
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
162 |
|
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n", |
163 |
|
inpfile[i].theta, inpfile[i].phi); |
164 |
|
#endif |
165 |
+ |
if (inpfile[i].nspec) |
166 |
+ |
set_spectral_samples(inpfile[i].nspec); |
167 |
|
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
168 |
|
} |
169 |
|
#ifdef DEBUG |
170 |
|
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
171 |
|
#endif |
172 |
|
/* read scattering data */ |
173 |
< |
while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) |
173 |
> |
while (fscanf(fp, "%lf %lf %lf", &theta_out, &phi_out, val) == 3) { |
174 |
> |
for (n = 1; n < inpfile[i].nspec; n++) |
175 |
> |
if (fscanf(fp, "%lf", val+n) != 1) { |
176 |
> |
fprintf(stderr, "%s: warning: unexpected EOF\n", |
177 |
> |
inpfile[i].fname); |
178 |
> |
fclose(fp); |
179 |
> |
return(1); |
180 |
> |
} |
181 |
> |
if (rev_orient) { /* reverse Z-axis to face outside */ |
182 |
> |
theta_out = 180. - theta_out; |
183 |
> |
phi_out = 360. - phi_out; |
184 |
> |
} |
185 |
> |
phi_out += 90.-inpfile[i].up_phi; |
186 |
|
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF); |
187 |
+ |
} |
188 |
|
n = 0; |
189 |
|
while ((c = getc(fp)) != EOF) |
190 |
|
n += !isspace(c); |
197 |
|
} |
198 |
|
|
199 |
|
#ifndef TEST_MAIN |
200 |
+ |
|
201 |
+ |
#define SYM_ILL '?' /* illegal symmetry value */ |
202 |
+ |
#define SYM_ISO 'I' /* isotropic */ |
203 |
+ |
#define SYM_QUAD 'Q' /* quadrilateral symmetry */ |
204 |
+ |
#define SYM_BILAT 'B' /* bilateral symmetry */ |
205 |
+ |
#define SYM_ANISO 'A' /* anisotropic */ |
206 |
+ |
#define SYM_UP 'U' /* "up-down" (180°) symmetry */ |
207 |
+ |
|
208 |
+ |
static const char quadrant_rep[16][16] = { |
209 |
+ |
"in-plane","0-90","90-180","0-180", |
210 |
+ |
"180-270","0-90+180-270","90-270", |
211 |
+ |
"0-270","270-360","270-90", |
212 |
+ |
"90-180+270-360","270-180","180-360", |
213 |
+ |
"180-90","90-360","0-360" |
214 |
+ |
}; |
215 |
+ |
static const char quadrant_sym[16] = { |
216 |
+ |
SYM_ISO, SYM_QUAD, SYM_QUAD, SYM_BILAT, |
217 |
+ |
SYM_QUAD, SYM_ILL, SYM_BILAT, SYM_ILL, |
218 |
+ |
SYM_QUAD, SYM_BILAT, SYM_ILL, SYM_ILL, |
219 |
+ |
SYM_BILAT, SYM_ILL, SYM_ILL, SYM_ANISO |
220 |
+ |
}; |
221 |
+ |
|
222 |
|
/* Read in PAB-Opto BSDF files and output RBF interpolant */ |
223 |
|
int |
224 |
|
main(int argc, char *argv[]) |
225 |
|
{ |
226 |
|
extern int nprocs; |
227 |
< |
int i; |
227 |
> |
const char *symmetry = "0"; |
228 |
> |
int ninpfiles, totinc; |
229 |
> |
int a, i; |
230 |
|
/* start header */ |
231 |
|
SET_FILE_BINARY(stdout); |
232 |
|
newheader("RADIANCE", stdout); |
233 |
|
printargs(argc, argv, stdout); |
234 |
|
fputnow(stdout); |
235 |
|
progname = argv[0]; /* get options */ |
236 |
< |
while (argc > 2 && argv[1][0] == '-') { |
237 |
< |
switch (argv[1][1]) { |
236 |
> |
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
237 |
> |
switch (argv[a][1]) { |
238 |
> |
case 't': |
239 |
> |
rev_orient = !rev_orient; |
240 |
> |
break; |
241 |
|
case 'n': |
242 |
< |
nprocs = atoi(argv[2]); |
242 |
> |
nprocs = atoi(argv[++a]); |
243 |
|
break; |
244 |
+ |
case 's': |
245 |
+ |
symmetry = argv[++a]; |
246 |
+ |
break; |
247 |
|
default: |
248 |
|
goto userr; |
249 |
|
} |
250 |
< |
argv += 2; argc -= 2; |
178 |
< |
} |
179 |
< |
/* initialize & sort inputs */ |
180 |
< |
ninpfiles = argc - 1; |
250 |
> |
totinc = ninpfiles = argc - a; /* initialize & sort inputs */ |
251 |
|
if (ninpfiles < 2) |
252 |
|
goto userr; |
253 |
< |
inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles); |
253 |
> |
if (toupper(symmetry[0]) == SYM_UP) /* special case for "up" symmetry */ |
254 |
> |
totinc += ninpfiles; |
255 |
> |
inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*totinc); |
256 |
|
if (inpfile == NULL) |
257 |
|
return(1); |
258 |
|
for (i = 0; i < ninpfiles; i++) |
259 |
< |
if (!init_pabopto_inp(i, argv[i+1])) |
259 |
> |
if (!init_pabopto_inp(i, argv[a+i])) |
260 |
|
return(1); |
261 |
+ |
|
262 |
+ |
for (i = ninpfiles; i < totinc; i++) { /* copy for "up" symmetry */ |
263 |
+ |
inpfile[i] = inpfile[i-ninpfiles]; |
264 |
+ |
inpfile[i].phi += 180.; /* invert duplicate data */ |
265 |
+ |
inpfile[i].up_phi -= 180.; |
266 |
+ |
set_grid_pos(&inpfile[i]); /* grid location for sorting */ |
267 |
+ |
} |
268 |
|
qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir); |
269 |
|
/* compile measurements */ |
270 |
< |
for (i = 0; i < ninpfiles; i++) |
270 |
> |
for (i = 0; i < totinc; i++) |
271 |
|
if (!add_pabopto_inp(i)) |
272 |
|
return(1); |
273 |
|
make_rbfrep(); /* process last data set */ |
274 |
+ |
/* check input symmetry */ |
275 |
+ |
switch (toupper(symmetry[0])) { |
276 |
+ |
case '0': /* unspecified symmetry */ |
277 |
+ |
if (quadrant_sym[inp_coverage] != SYM_ILL) |
278 |
+ |
break; /* anything legal goes */ |
279 |
+ |
fprintf(stderr, "%s: unsupported phi coverage (%s)\n", |
280 |
+ |
progname, quadrant_rep[inp_coverage]); |
281 |
+ |
return(1); |
282 |
+ |
case SYM_UP: /* faux "up" symmetry */ |
283 |
+ |
if (quadrant_sym[inp_coverage] == SYM_ANISO) |
284 |
+ |
break; |
285 |
+ |
/* fall through */ |
286 |
+ |
case SYM_ISO: /* usual symmetry types */ |
287 |
+ |
case SYM_QUAD: |
288 |
+ |
case SYM_BILAT: |
289 |
+ |
case SYM_ANISO: |
290 |
+ |
if (quadrant_sym[inp_coverage] == toupper(symmetry[0])) |
291 |
+ |
break; /* matches spec */ |
292 |
+ |
fprintf(stderr, |
293 |
+ |
"%s: phi coverage (%s) does not match requested '%s' symmetry\n", |
294 |
+ |
progname, quadrant_rep[inp_coverage], symmetry); |
295 |
+ |
return(1); |
296 |
+ |
default: |
297 |
+ |
fprintf(stderr, |
298 |
+ |
"%s: -s option must be Isotropic, Quadrilateral, Bilateral, Up, or Anisotropic\n", |
299 |
+ |
progname); |
300 |
+ |
return(1); |
301 |
+ |
} |
302 |
+ |
#ifdef DEBUG |
303 |
+ |
fprintf(stderr, "Input phi coverage (%s) has '%c' symmetry\n", |
304 |
+ |
quadrant_rep[inp_coverage], |
305 |
+ |
quadrant_sym[inp_coverage]); |
306 |
+ |
#endif |
307 |
|
build_mesh(); /* create interpolation */ |
308 |
|
save_bsdf_rep(stdout); /* write it out */ |
309 |
|
return(0); |
310 |
|
userr: |
311 |
< |
fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n", |
311 |
> |
fprintf(stderr, "Usage: %s [-t][-n nproc][-s symmetry] meas1.dat meas2.dat .. > bsdf.sir\n", |
312 |
|
progname); |
313 |
|
return(1); |
314 |
|
} |
315 |
< |
#else |
315 |
> |
|
316 |
> |
#else /* TEST_MAIN */ |
317 |
> |
|
318 |
|
/* Test main produces a Radiance model from the given input file */ |
319 |
|
int |
320 |
|
main(int argc, char *argv[]) |
331 |
|
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
332 |
|
return(1); |
333 |
|
} |
220 |
– |
ninpfiles = 1; |
334 |
|
inpfile = &pginp; |
335 |
|
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
336 |
|
return(1); |
346 |
|
#if 1 /* produce spheres at meas. */ |
347 |
|
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
348 |
|
n = 0; |
349 |
< |
for (i = 0; i < GRIDRES; i++) |
350 |
< |
for (j = 0; j < GRIDRES; j++) |
349 |
> |
for (i = 0; i < grid_res; i++) |
350 |
> |
for (j = 0; j < grid_res; j++) |
351 |
|
if (dsf_grid[i][j].sum.n > 0) { |
352 |
|
ovec_from_pos(dir, i, j); |
353 |
|
bsdf = dsf_grid[i][j].sum.v / |
367 |
|
for (n = 0; n < dsf_list->nrbf; n++) { |
368 |
|
RBFVAL *rbf = &dsf_list->rbfa[n]; |
369 |
|
ovec_from_pos(dir, rbf->gx, rbf->gy); |
370 |
< |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
370 |
> |
bsdf = eval_rbfrep(dsf_list, dir); |
371 |
|
bsdf = log(bsdf + 1e-5) - min_log; |
372 |
|
printf("red sphere p%04d\n0\n0\n", ++n); |
373 |
|
printf("4 %.6g %.6g %.6g %.6g\n\n", |
378 |
|
#if 1 /* output continuous surface */ |
379 |
|
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
380 |
|
fflush(stdout); |
381 |
< |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
381 |
> |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1); |
382 |
|
pfp = popen(buf, "w"); |
383 |
|
if (pfp == NULL) { |
384 |
|
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
385 |
|
return(1); |
386 |
|
} |
387 |
< |
for (i = 0; i < GRIDRES; i++) |
388 |
< |
for (j = 0; j < GRIDRES; j++) { |
387 |
> |
for (i = 0; i < grid_res; i++) |
388 |
> |
for (j = 0; j < grid_res; j++) { |
389 |
|
ovec_from_pos(dir, i, j); |
390 |
< |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
390 |
> |
bsdf = eval_rbfrep(dsf_list, dir); |
391 |
|
bsdf = log(bsdf + 1e-5) - min_log; |
392 |
|
fprintf(pfp, "%.8e %.8e %.8e\n", |
393 |
|
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
397 |
|
#endif |
398 |
|
return(0); |
399 |
|
} |
400 |
< |
#endif |
400 |
> |
|
401 |
> |
#endif /* TEST_MAIN */ |