ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
Revision: 2.43
Committed: Tue Jun 3 21:31:51 2025 UTC (2 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.42: +4 -5 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

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