8 |
|
*/ |
9 |
|
|
10 |
|
#include <stdio.h> |
11 |
+ |
#include <stdlib.h> |
12 |
|
#include <string.h> |
13 |
|
#include <ctype.h> |
14 |
+ |
#include <math.h> |
15 |
|
#include "platform.h" |
16 |
|
#include "bsdfrep.h" |
17 |
|
/* global argv[0] */ |
18 |
|
char *progname; |
19 |
|
|
20 |
< |
/* Load a set of measurements corresponding to a particular incident angle */ |
20 |
> |
typedef struct { |
21 |
> |
const char *fname; /* input file path */ |
22 |
> |
double theta, phi; /* incident angles (in degrees) */ |
23 |
> |
int isDSF; /* data is DSF (rather than BSDF)? */ |
24 |
> |
long dstart; /* data start offset in file */ |
25 |
> |
} PGINPUT; |
26 |
> |
|
27 |
> |
PGINPUT *inpfile; /* input files sorted by incidence */ |
28 |
> |
int ninpfiles; /* number of input files */ |
29 |
> |
|
30 |
> |
/* Compare incident angles */ |
31 |
|
static int |
32 |
< |
load_pabopto_meas(const char *fname) |
32 |
> |
cmp_inang(const void *p1, const void *p2) |
33 |
|
{ |
34 |
+ |
const PGINPUT *inp1 = (const PGINPUT *)p1; |
35 |
+ |
const PGINPUT *inp2 = (const PGINPUT *)p2; |
36 |
+ |
|
37 |
+ |
if (inp1->theta > inp2->theta+FTINY) |
38 |
+ |
return(1); |
39 |
+ |
if (inp1->theta < inp2->theta-FTINY) |
40 |
+ |
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 |
+ |
} |
47 |
+ |
|
48 |
+ |
/* Prepare a PAB-Opto input file by reading its header */ |
49 |
+ |
static int |
50 |
+ |
init_pabopto_inp(const int i, const char *fname) |
51 |
+ |
{ |
52 |
|
FILE *fp = fopen(fname, "r"); |
23 |
– |
int inp_is_DSF = -1; |
24 |
– |
double new_theta, new_phi, theta_out, phi_out, val; |
53 |
|
char buf[2048]; |
54 |
< |
int n, c; |
54 |
> |
int c; |
55 |
|
|
56 |
|
if (fp == NULL) { |
57 |
|
fputs(fname, stderr); |
58 |
|
fputs(": cannot open\n", stderr); |
59 |
|
return(0); |
60 |
|
} |
61 |
< |
#ifdef DEBUG |
62 |
< |
fprintf(stderr, "Loading measurement file '%s'...\n", fname); |
63 |
< |
#endif |
61 |
> |
inpfile[i].fname = fname; |
62 |
> |
inpfile[i].isDSF = -1; |
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")) { |
74 |
< |
inp_is_DSF = 1; |
75 |
< |
continue; |
73 |
> |
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
74 |
> |
if (!strcasecmp(typ, "DSF")) { |
75 |
> |
inpfile[i].isDSF = 1; |
76 |
> |
continue; |
77 |
> |
} |
78 |
> |
if (!strcasecmp(typ, "BSDF")) { |
79 |
> |
inpfile[i].isDSF = 0; |
80 |
> |
continue; |
81 |
> |
} |
82 |
|
} |
83 |
< |
if (!strcmp(buf, "format: theta phi BSDF\n")) { |
49 |
< |
inp_is_DSF = 0; |
83 |
> |
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
84 |
|
continue; |
85 |
< |
} |
52 |
< |
if (sscanf(buf, "intheta %lf", &new_theta) == 1) |
85 |
> |
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
86 |
|
continue; |
54 |
– |
if (sscanf(buf, "inphi %lf", &new_phi) == 1) |
55 |
– |
continue; |
87 |
|
if (sscanf(buf, "incident_angle %lf %lf", |
88 |
< |
&new_theta, &new_phi) == 2) |
88 |
> |
&inpfile[i].theta, &inpfile[i].phi) == 2) |
89 |
|
continue; |
90 |
|
} |
91 |
< |
ungetc(c, fp); |
92 |
< |
if (inp_is_DSF < 0) { |
91 |
> |
inpfile[i].dstart = ftell(fp) - 1; |
92 |
> |
fclose(fp); |
93 |
> |
if (inpfile[i].isDSF < 0) { |
94 |
|
fputs(fname, stderr); |
95 |
|
fputs(": unknown format\n", stderr); |
64 |
– |
fclose(fp); |
96 |
|
return(0); |
97 |
|
} |
98 |
+ |
if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) { |
99 |
+ |
fputs(fname, stderr); |
100 |
+ |
fputs(": unknown incident angle\n", stderr); |
101 |
+ |
return(0); |
102 |
+ |
} |
103 |
+ |
while (inpfile[i].phi < 0) /* normalize phi direction */ |
104 |
+ |
inpfile[i].phi += 360.; |
105 |
+ |
return(1); |
106 |
+ |
} |
107 |
+ |
|
108 |
+ |
/* Load a set of measurements corresponding to a particular incident angle */ |
109 |
+ |
static int |
110 |
+ |
add_pabopto_inp(const int i) |
111 |
+ |
{ |
112 |
+ |
FILE *fp = fopen(inpfile[i].fname, "r"); |
113 |
+ |
double theta_out, phi_out, val; |
114 |
+ |
int n, c; |
115 |
+ |
|
116 |
+ |
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
117 |
+ |
fputs(inpfile[i].fname, stderr); |
118 |
+ |
fputs(": cannot open\n", stderr); |
119 |
+ |
return(0); |
120 |
+ |
} |
121 |
|
/* prepare input grid */ |
122 |
< |
new_bsdf_data(new_theta, new_phi); |
123 |
< |
/* read actual data */ |
122 |
> |
if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) { |
123 |
> |
if (i) /* need to process previous incidence */ |
124 |
> |
make_rbfrep(); |
125 |
> |
#ifdef DEBUG |
126 |
> |
fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n", |
127 |
> |
inpfile[i].theta, inpfile[i].phi); |
128 |
> |
#endif |
129 |
> |
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
130 |
> |
} |
131 |
> |
#ifdef DEBUG |
132 |
> |
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
133 |
> |
#endif |
134 |
> |
/* read scattering data */ |
135 |
|
while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) |
136 |
< |
add_bsdf_data(theta_out, phi_out, val, inp_is_DSF); |
136 |
> |
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF); |
137 |
|
n = 0; |
138 |
|
while ((c = getc(fp)) != EOF) |
139 |
|
n += !isspace(c); |
140 |
|
if (n) |
141 |
|
fprintf(stderr, |
142 |
|
"%s: warning: %d unexpected characters past EOD\n", |
143 |
< |
fname, n); |
143 |
> |
inpfile[i].fname, n); |
144 |
|
fclose(fp); |
145 |
|
return(1); |
146 |
|
} |
147 |
|
|
148 |
+ |
#ifndef TEST_MAIN |
149 |
|
/* Read in PAB-Opto BSDF files and output RBF interpolant */ |
150 |
|
int |
151 |
|
main(int argc, char *argv[]) |
168 |
|
} |
169 |
|
argv += 2; argc -= 2; |
170 |
|
} |
171 |
< |
if (argc < 3) |
171 |
> |
/* initialize & sort inputs */ |
172 |
> |
ninpfiles = argc - 1; |
173 |
> |
if (ninpfiles < 2) |
174 |
|
goto userr; |
175 |
< |
for (i = 1; i < argc; i++) { /* compile measurements */ |
176 |
< |
if (!load_pabopto_meas(argv[i])) |
175 |
> |
inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles); |
176 |
> |
if (inpfile == NULL) |
177 |
> |
return(1); |
178 |
> |
for (i = 0; i < ninpfiles; i++) |
179 |
> |
if (!init_pabopto_inp(i, argv[i+1])) |
180 |
|
return(1); |
181 |
< |
make_rbfrep(); |
182 |
< |
} |
181 |
> |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang); |
182 |
> |
/* compile measurements */ |
183 |
> |
for (i = 0; i < ninpfiles; i++) |
184 |
> |
if (!add_pabopto_inp(i)) |
185 |
> |
return(1); |
186 |
> |
make_rbfrep(); /* process last data set */ |
187 |
|
build_mesh(); /* create interpolation */ |
188 |
|
save_bsdf_rep(stdout); /* write it out */ |
189 |
|
return(0); |
192 |
|
progname); |
193 |
|
return(1); |
194 |
|
} |
195 |
+ |
#else |
196 |
+ |
/* Test main produces a Radiance model from the given input file */ |
197 |
+ |
int |
198 |
+ |
main(int argc, char *argv[]) |
199 |
+ |
{ |
200 |
+ |
PGINPUT pginp; |
201 |
+ |
char buf[128]; |
202 |
+ |
FILE *pfp; |
203 |
+ |
double bsdf, min_log; |
204 |
+ |
FVECT dir; |
205 |
+ |
int i, j, n; |
206 |
+ |
|
207 |
+ |
if (argc != 2) { |
208 |
+ |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]); |
209 |
+ |
return(1); |
210 |
+ |
} |
211 |
+ |
ninpfiles = 1; |
212 |
+ |
inpfile = &pginp; |
213 |
+ |
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
214 |
+ |
return(1); |
215 |
+ |
/* reduce data set */ |
216 |
+ |
make_rbfrep(); |
217 |
+ |
#ifdef DEBUG |
218 |
+ |
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
219 |
+ |
#endif |
220 |
+ |
/* produce spheres at meas. */ |
221 |
+ |
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
222 |
+ |
min_log = log(bsdf_min*.5); |
223 |
+ |
n = 0; |
224 |
+ |
for (i = 0; i < GRIDRES; i++) |
225 |
+ |
for (j = 0; j < GRIDRES; j++) |
226 |
+ |
if (dsf_grid[i][j].nval > 0) { |
227 |
+ |
ovec_from_pos(dir, i, j); |
228 |
+ |
bsdf = dsf_grid[i][j].vsum / |
229 |
+ |
(dsf_grid[i][j].nval*output_orient*dir[2]); |
230 |
+ |
if (bsdf <= bsdf_min*.6) |
231 |
+ |
continue; |
232 |
+ |
bsdf = log(bsdf) - min_log; |
233 |
+ |
ovec_from_pos(dir, i, j); |
234 |
+ |
printf("yellow sphere s%04d\n0\n0\n", ++n); |
235 |
+ |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
236 |
+ |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
237 |
+ |
.007*bsdf); |
238 |
+ |
} |
239 |
+ |
/* output continuous surface */ |
240 |
+ |
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
241 |
+ |
fflush(stdout); |
242 |
+ |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
243 |
+ |
pfp = popen(buf, "w"); |
244 |
+ |
if (pfp == NULL) { |
245 |
+ |
fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf); |
246 |
+ |
return(1); |
247 |
+ |
} |
248 |
+ |
for (i = 0; i < GRIDRES; i++) |
249 |
+ |
for (j = 0; j < GRIDRES; j++) { |
250 |
+ |
ovec_from_pos(dir, i, j); |
251 |
+ |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
252 |
+ |
bsdf = log(bsdf) - min_log; |
253 |
+ |
fprintf(pfp, "%.8e %.8e %.8e\n", |
254 |
+ |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
255 |
+ |
} |
256 |
+ |
return(pclose(pfp)==0 ? 0 : 1); |
257 |
+ |
} |
258 |
+ |
#endif |