1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.25 2014/06/06 00:58:22 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Load measured BSDF data in PAB-Opto format. |
6 |
* |
7 |
* G. Ward |
8 |
*/ |
9 |
|
10 |
#define _USE_MATH_DEFINES |
11 |
#include <stdio.h> |
12 |
#include <stdlib.h> |
13 |
#include <string.h> |
14 |
#include <ctype.h> |
15 |
#include <math.h> |
16 |
#include "platform.h" |
17 |
#include "bsdfrep.h" |
18 |
#include "resolu.h" |
19 |
/* global argv[0] */ |
20 |
char *progname; |
21 |
|
22 |
typedef struct { |
23 |
const char *fname; /* input file path */ |
24 |
double theta, phi; /* input angles */ |
25 |
int igp[2]; /* input grid position */ |
26 |
int isDSF; /* data is DSF (rather than BSDF)? */ |
27 |
long dstart; /* data start offset in file */ |
28 |
} PGINPUT; |
29 |
|
30 |
PGINPUT *inpfile; /* input files sorted by incidence */ |
31 |
int ninpfiles; /* number of input files */ |
32 |
|
33 |
/* Compare incident angles */ |
34 |
static int |
35 |
cmp_indir(const void *p1, const void *p2) |
36 |
{ |
37 |
const PGINPUT *inp1 = (const PGINPUT *)p1; |
38 |
const PGINPUT *inp2 = (const PGINPUT *)p2; |
39 |
int ydif = inp1->igp[1] - inp2->igp[1]; |
40 |
|
41 |
if (ydif) |
42 |
return(ydif); |
43 |
|
44 |
return(inp1->igp[0] - inp2->igp[0]); |
45 |
} |
46 |
|
47 |
/* Prepare a PAB-Opto input file by reading its header */ |
48 |
static int |
49 |
init_pabopto_inp(const int i, const char *fname) |
50 |
{ |
51 |
FILE *fp = fopen(fname, "r"); |
52 |
FVECT dv; |
53 |
char buf[2048]; |
54 |
int c; |
55 |
|
56 |
if (fp == NULL) { |
57 |
fputs(fname, stderr); |
58 |
fputs(": cannot open\n", stderr); |
59 |
return(0); |
60 |
} |
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 (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1) |
74 |
continue; |
75 |
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
76 |
if (!strcasecmp(typ, "DSF")) { |
77 |
inpfile[i].isDSF = 1; |
78 |
continue; |
79 |
} |
80 |
if (!strcasecmp(typ, "BSDF") || |
81 |
!strcasecmp(typ, "BRDF") || |
82 |
!strcasecmp(typ, "BTDF")) { |
83 |
inpfile[i].isDSF = 0; |
84 |
continue; |
85 |
} |
86 |
} |
87 |
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
88 |
continue; |
89 |
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
90 |
continue; |
91 |
if (sscanf(buf, "incident_angle %lf %lf", |
92 |
&inpfile[i].theta, &inpfile[i].phi) == 2) |
93 |
continue; |
94 |
} |
95 |
inpfile[i].dstart = ftell(fp) - 1; |
96 |
fclose(fp); |
97 |
if (inpfile[i].isDSF < 0) { |
98 |
fputs(fname, stderr); |
99 |
fputs(": unknown format\n", stderr); |
100 |
return(0); |
101 |
} |
102 |
if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) { |
103 |
fputs(fname, stderr); |
104 |
fputs(": unknown incident angle\n", stderr); |
105 |
return(0); |
106 |
} |
107 |
/* convert angle to grid position */ |
108 |
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); |
113 |
return(1); |
114 |
} |
115 |
|
116 |
/* Load a set of measurements corresponding to a particular incident angle */ |
117 |
static int |
118 |
add_pabopto_inp(const int i) |
119 |
{ |
120 |
FILE *fp = fopen(inpfile[i].fname, "r"); |
121 |
double theta_out, phi_out, val; |
122 |
int n, c; |
123 |
|
124 |
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
125 |
fputs(inpfile[i].fname, stderr); |
126 |
fputs(": cannot open\n", stderr); |
127 |
return(0); |
128 |
} |
129 |
/* prepare input grid */ |
130 |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) { |
131 |
if (i) /* process previous incidence */ |
132 |
make_rbfrep(); |
133 |
#ifdef DEBUG |
134 |
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n", |
135 |
inpfile[i].theta, inpfile[i].phi); |
136 |
#endif |
137 |
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
138 |
} |
139 |
#ifdef DEBUG |
140 |
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
141 |
#endif |
142 |
/* read scattering data */ |
143 |
while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) |
144 |
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF); |
145 |
n = 0; |
146 |
while ((c = getc(fp)) != EOF) |
147 |
n += !isspace(c); |
148 |
if (n) |
149 |
fprintf(stderr, |
150 |
"%s: warning: %d unexpected characters past EOD\n", |
151 |
inpfile[i].fname, n); |
152 |
fclose(fp); |
153 |
return(1); |
154 |
} |
155 |
|
156 |
#ifndef TEST_MAIN |
157 |
/* Read in PAB-Opto BSDF files and output RBF interpolant */ |
158 |
int |
159 |
main(int argc, char *argv[]) |
160 |
{ |
161 |
extern int nprocs; |
162 |
int i; |
163 |
/* start header */ |
164 |
SET_FILE_BINARY(stdout); |
165 |
newheader("RADIANCE", stdout); |
166 |
printargs(argc, argv, stdout); |
167 |
fputnow(stdout); |
168 |
progname = argv[0]; /* get options */ |
169 |
while (argc > 2 && argv[1][0] == '-') { |
170 |
switch (argv[1][1]) { |
171 |
case 'n': |
172 |
nprocs = atoi(argv[2]); |
173 |
break; |
174 |
default: |
175 |
goto userr; |
176 |
} |
177 |
argv += 2; argc -= 2; |
178 |
} |
179 |
/* initialize & sort inputs */ |
180 |
ninpfiles = argc - 1; |
181 |
if (ninpfiles < 2) |
182 |
goto userr; |
183 |
inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles); |
184 |
if (inpfile == NULL) |
185 |
return(1); |
186 |
for (i = 0; i < ninpfiles; i++) |
187 |
if (!init_pabopto_inp(i, argv[i+1])) |
188 |
return(1); |
189 |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir); |
190 |
/* compile measurements */ |
191 |
for (i = 0; i < ninpfiles; i++) |
192 |
if (!add_pabopto_inp(i)) |
193 |
return(1); |
194 |
make_rbfrep(); /* process last data set */ |
195 |
build_mesh(); /* create interpolation */ |
196 |
save_bsdf_rep(stdout); /* write it out */ |
197 |
return(0); |
198 |
userr: |
199 |
fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n", |
200 |
progname); |
201 |
return(1); |
202 |
} |
203 |
#else |
204 |
/* Test main produces a Radiance model from the given input file */ |
205 |
int |
206 |
main(int argc, char *argv[]) |
207 |
{ |
208 |
PGINPUT pginp; |
209 |
char buf[128]; |
210 |
FILE *pfp; |
211 |
double bsdf, min_log; |
212 |
FVECT dir; |
213 |
int i, j, n; |
214 |
|
215 |
progname = argv[0]; |
216 |
if (argc != 2) { |
217 |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
218 |
return(1); |
219 |
} |
220 |
ninpfiles = 1; |
221 |
inpfile = &pginp; |
222 |
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
223 |
return(1); |
224 |
/* reduce data set */ |
225 |
if (make_rbfrep() == NULL) { |
226 |
fprintf(stderr, "%s: nothing to plot!\n", progname); |
227 |
exit(1); |
228 |
} |
229 |
#ifdef DEBUG |
230 |
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
231 |
#endif |
232 |
min_log = log(bsdf_min*.5 + 1e-5); |
233 |
#if 1 /* produce spheres at meas. */ |
234 |
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
235 |
n = 0; |
236 |
for (i = 0; i < GRIDRES; i++) |
237 |
for (j = 0; j < GRIDRES; j++) |
238 |
if (dsf_grid[i][j].sum.n > 0) { |
239 |
ovec_from_pos(dir, i, j); |
240 |
bsdf = dsf_grid[i][j].sum.v / |
241 |
((double)dsf_grid[i][j].sum.n*output_orient*dir[2]); |
242 |
if (bsdf <= bsdf_min*.6) |
243 |
continue; |
244 |
bsdf = log(bsdf + 1e-5) - min_log; |
245 |
ovec_from_pos(dir, i, j); |
246 |
printf("yellow sphere s%04d\n0\n0\n", ++n); |
247 |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
248 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
249 |
.007*bsdf); |
250 |
} |
251 |
#endif |
252 |
#if 1 /* spheres at RBF peaks */ |
253 |
puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n"); |
254 |
for (n = 0; n < dsf_list->nrbf; n++) { |
255 |
RBFVAL *rbf = &dsf_list->rbfa[n]; |
256 |
ovec_from_pos(dir, rbf->gx, rbf->gy); |
257 |
bsdf = eval_rbfrep(dsf_list, dir); |
258 |
bsdf = log(bsdf + 1e-5) - min_log; |
259 |
printf("red sphere p%04d\n0\n0\n", ++n); |
260 |
printf("4 %.6g %.6g %.6g %.6g\n\n", |
261 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
262 |
.011*bsdf); |
263 |
} |
264 |
#endif |
265 |
#if 1 /* output continuous surface */ |
266 |
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
267 |
fflush(stdout); |
268 |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
269 |
pfp = popen(buf, "w"); |
270 |
if (pfp == NULL) { |
271 |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
272 |
return(1); |
273 |
} |
274 |
for (i = 0; i < GRIDRES; i++) |
275 |
for (j = 0; j < GRIDRES; j++) { |
276 |
ovec_from_pos(dir, i, j); |
277 |
bsdf = eval_rbfrep(dsf_list, dir); |
278 |
bsdf = log(bsdf + 1e-5) - min_log; |
279 |
fprintf(pfp, "%.8e %.8e %.8e\n", |
280 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
281 |
} |
282 |
if (pclose(pfp) != 0) |
283 |
return(1); |
284 |
#endif |
285 |
return(0); |
286 |
} |
287 |
#endif |