ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
(Generate patch)

Comparing ray/src/cv/pabopto2bsdf.c (file contents):
Revision 2.4 by greg, Sun Jun 30 14:46:29 2013 UTC vs.
Revision 2.42 by greg, Mon Nov 21 18:28:24 2022 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines