ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
Revision: 2.40
Committed: Mon Apr 5 19:56:18 2021 UTC (3 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.39: +22 -5 lines
Log Message:
feat(pabopto2bsdf): Added -g option to cull grazing measurements from input

File Contents

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