7 |
|
* G. Ward |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#define _USE_MATH_DEFINES |
11 |
|
#include <stdio.h> |
12 |
|
#include <stdlib.h> |
13 |
|
#include <string.h> |
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 (degrees) */ |
25 |
> |
double up_phi; /* azimuth for "up" direction */ |
26 |
> |
int igp[2]; /* input grid position */ |
27 |
|
int isDSF; /* data is DSF (rather than BSDF)? */ |
28 |
+ |
int nspec; /* number of spectral samples */ |
29 |
|
long dstart; /* data start offset in file */ |
30 |
|
} PGINPUT; |
31 |
|
|
28 |
– |
double angle_eps = 0; /* epsilon for angle comparisons */ |
29 |
– |
|
32 |
|
PGINPUT *inpfile; /* input files sorted by incidence */ |
33 |
|
int ninpfiles; /* number of input files */ |
34 |
|
|
35 |
|
/* Compare incident angles */ |
36 |
|
static int |
37 |
< |
cmp_inang(const void *p1, const void *p2) |
37 |
> |
cmp_indir(const void *p1, const void *p2) |
38 |
|
{ |
39 |
|
const PGINPUT *inp1 = (const PGINPUT *)p1; |
40 |
|
const PGINPUT *inp2 = (const PGINPUT *)p2; |
41 |
+ |
int ydif = inp1->igp[1] - inp2->igp[1]; |
42 |
|
|
43 |
< |
if (inp1->theta > inp2->theta+angle_eps) |
44 |
< |
return(1); |
45 |
< |
if (inp1->theta < inp2->theta-angle_eps) |
46 |
< |
return(-1); |
44 |
< |
if (inp1->phi > inp2->phi+angle_eps) |
45 |
< |
return(1); |
46 |
< |
if (inp1->phi < inp2->phi-angle_eps) |
47 |
< |
return(-1); |
48 |
< |
return(0); |
43 |
> |
if (ydif) |
44 |
> |
return(ydif); |
45 |
> |
|
46 |
> |
return(inp1->igp[0] - inp2->igp[0]); |
47 |
|
} |
48 |
|
|
49 |
|
/* Prepare a PAB-Opto input file by reading its header */ |
51 |
|
init_pabopto_inp(const int i, const char *fname) |
52 |
|
{ |
53 |
|
FILE *fp = fopen(fname, "r"); |
54 |
+ |
FVECT dv; |
55 |
|
char buf[2048]; |
56 |
|
int c; |
57 |
|
|
62 |
|
} |
63 |
|
inpfile[i].fname = fname; |
64 |
|
inpfile[i].isDSF = -1; |
65 |
+ |
inpfile[i].nspec = 0; |
66 |
+ |
inpfile[i].up_phi = 0; |
67 |
|
inpfile[i].theta = inpfile[i].phi = -10001.; |
68 |
|
/* read header information */ |
69 |
|
while ((c = getc(fp)) == '#' || c == EOF) { |
70 |
< |
char typ[32]; |
70 |
> |
char typ[64]; |
71 |
|
if (fgets(buf, sizeof(buf), fp) == NULL) { |
72 |
|
fputs(fname, stderr); |
73 |
|
fputs(": unexpected EOF\n", stderr); |
76 |
|
} |
77 |
|
if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1) |
78 |
|
continue; |
79 |
+ |
if (sscanf(buf, "colorimetry: %s", typ) == 1) { |
80 |
+ |
if (!strcasecmp(typ, "CIE-XYZ")) |
81 |
+ |
inpfile[i].nspec = 3; |
82 |
+ |
else if (!strcasecmp(typ, "CIE-Y")) |
83 |
+ |
inpfile[i].nspec = 1; |
84 |
+ |
continue; |
85 |
+ |
} |
86 |
|
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
87 |
< |
if (!strcasecmp(typ, "DSF")) { |
87 |
> |
if (!strcasecmp(typ, "DSF")) |
88 |
|
inpfile[i].isDSF = 1; |
89 |
< |
continue; |
90 |
< |
} |
91 |
< |
if (!strcasecmp(typ, "BSDF")) { |
89 |
> |
else if (!strcasecmp(typ, "BSDF") || |
90 |
> |
!strcasecmp(typ, "BRDF") || |
91 |
> |
!strcasecmp(typ, "BTDF")) |
92 |
|
inpfile[i].isDSF = 0; |
93 |
< |
continue; |
86 |
< |
} |
93 |
> |
continue; |
94 |
|
} |
95 |
+ |
if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1) |
96 |
+ |
continue; |
97 |
|
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
98 |
|
continue; |
99 |
|
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
114 |
|
fputs(": unknown incident angle\n", stderr); |
115 |
|
return(0); |
116 |
|
} |
117 |
< |
while (inpfile[i].phi < 0) /* normalize phi direction */ |
118 |
< |
inpfile[i].phi += 360.; |
117 |
> |
/* convert to Y-up orientation */ |
118 |
> |
inpfile[i].phi += 90.-inpfile[i].up_phi; |
119 |
> |
/* convert angle to grid position */ |
120 |
> |
dv[2] = sin(M_PI/180.*inpfile[i].theta); |
121 |
> |
dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2]; |
122 |
> |
dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2]; |
123 |
> |
dv[2] = sqrt(1. - dv[2]*dv[2]); |
124 |
> |
pos_from_vec(inpfile[i].igp, dv); |
125 |
|
return(1); |
126 |
|
} |
127 |
|
|
129 |
|
static int |
130 |
|
add_pabopto_inp(const int i) |
131 |
|
{ |
132 |
< |
FILE *fp = fopen(inpfile[i].fname, "r"); |
133 |
< |
double theta_out, phi_out, val; |
134 |
< |
int n, c; |
132 |
> |
FILE *fp = fopen(inpfile[i].fname, "r"); |
133 |
> |
double theta_out, phi_out, val[3]; |
134 |
> |
int n, c; |
135 |
|
|
136 |
|
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
137 |
|
fputs(inpfile[i].fname, stderr); |
139 |
|
return(0); |
140 |
|
} |
141 |
|
/* prepare input grid */ |
142 |
< |
angle_eps = 180./2./GRIDRES; |
143 |
< |
if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) { |
129 |
< |
if (i) /* need to process previous incidence */ |
142 |
> |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) { |
143 |
> |
if (i) /* process previous incidence */ |
144 |
|
make_rbfrep(); |
145 |
|
#ifdef DEBUG |
146 |
< |
fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n", |
146 |
> |
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n", |
147 |
|
inpfile[i].theta, inpfile[i].phi); |
148 |
|
#endif |
149 |
+ |
if (inpfile[i].nspec) |
150 |
+ |
set_spectral_samples(inpfile[i].nspec); |
151 |
|
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
152 |
|
} |
153 |
|
#ifdef DEBUG |
154 |
|
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
155 |
|
#endif |
156 |
|
/* read scattering data */ |
157 |
< |
while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) |
158 |
< |
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF); |
157 |
> |
while (fscanf(fp, "%lf %lf %lf", &theta_out, &phi_out, val) == 3) { |
158 |
> |
for (n = 1; n < inpfile[i].nspec; n++) |
159 |
> |
if (fscanf(fp, "%lf", val+n) != 1) { |
160 |
> |
fprintf(stderr, "%s: warning: unexpected EOF\n", |
161 |
> |
inpfile[i].fname); |
162 |
> |
fclose(fp); |
163 |
> |
return(1); |
164 |
> |
} |
165 |
> |
add_bsdf_data(theta_out, phi_out+90.-inpfile[i].up_phi, |
166 |
> |
val, inpfile[i].isDSF); |
167 |
> |
} |
168 |
|
n = 0; |
169 |
|
while ((c = getc(fp)) != EOF) |
170 |
|
n += !isspace(c); |
209 |
|
for (i = 0; i < ninpfiles; i++) |
210 |
|
if (!init_pabopto_inp(i, argv[i+1])) |
211 |
|
return(1); |
212 |
< |
angle_eps = 0; |
188 |
< |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang); |
212 |
> |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir); |
213 |
|
/* compile measurements */ |
214 |
|
for (i = 0; i < ninpfiles; i++) |
215 |
|
if (!add_pabopto_inp(i)) |
235 |
|
FVECT dir; |
236 |
|
int i, j, n; |
237 |
|
|
238 |
+ |
progname = argv[0]; |
239 |
|
if (argc != 2) { |
240 |
< |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]); |
240 |
> |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
241 |
|
return(1); |
242 |
|
} |
243 |
|
ninpfiles = 1; |
245 |
|
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
246 |
|
return(1); |
247 |
|
/* reduce data set */ |
248 |
< |
make_rbfrep(); |
248 |
> |
if (make_rbfrep() == NULL) { |
249 |
> |
fprintf(stderr, "%s: nothing to plot!\n", progname); |
250 |
> |
exit(1); |
251 |
> |
} |
252 |
|
#ifdef DEBUG |
253 |
|
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
254 |
|
#endif |
256 |
|
#if 1 /* produce spheres at meas. */ |
257 |
|
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
258 |
|
n = 0; |
259 |
< |
for (i = 0; i < GRIDRES; i++) |
260 |
< |
for (j = 0; j < GRIDRES; j++) |
259 |
> |
for (i = 0; i < grid_res; i++) |
260 |
> |
for (j = 0; j < grid_res; j++) |
261 |
|
if (dsf_grid[i][j].sum.n > 0) { |
262 |
|
ovec_from_pos(dir, i, j); |
263 |
|
bsdf = dsf_grid[i][j].sum.v / |
264 |
< |
(dsf_grid[i][j].sum.n*output_orient*dir[2]); |
264 |
> |
((double)dsf_grid[i][j].sum.n*output_orient*dir[2]); |
265 |
|
if (bsdf <= bsdf_min*.6) |
266 |
|
continue; |
267 |
|
bsdf = log(bsdf + 1e-5) - min_log; |
277 |
|
for (n = 0; n < dsf_list->nrbf; n++) { |
278 |
|
RBFVAL *rbf = &dsf_list->rbfa[n]; |
279 |
|
ovec_from_pos(dir, rbf->gx, rbf->gy); |
280 |
< |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
280 |
> |
bsdf = eval_rbfrep(dsf_list, dir); |
281 |
|
bsdf = log(bsdf + 1e-5) - min_log; |
282 |
|
printf("red sphere p%04d\n0\n0\n", ++n); |
283 |
|
printf("4 %.6g %.6g %.6g %.6g\n\n", |
288 |
|
#if 1 /* output continuous surface */ |
289 |
|
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
290 |
|
fflush(stdout); |
291 |
< |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
291 |
> |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1); |
292 |
|
pfp = popen(buf, "w"); |
293 |
|
if (pfp == NULL) { |
294 |
< |
fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf); |
294 |
> |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
295 |
|
return(1); |
296 |
|
} |
297 |
< |
for (i = 0; i < GRIDRES; i++) |
298 |
< |
for (j = 0; j < GRIDRES; j++) { |
297 |
> |
for (i = 0; i < grid_res; i++) |
298 |
> |
for (j = 0; j < grid_res; j++) { |
299 |
|
ovec_from_pos(dir, i, j); |
300 |
< |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
300 |
> |
bsdf = eval_rbfrep(dsf_list, dir); |
301 |
|
bsdf = log(bsdf + 1e-5) - min_log; |
302 |
|
fprintf(pfp, "%.8e %.8e %.8e\n", |
303 |
|
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |