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.24 by greg, Mon Mar 24 02:31:02 2014 UTC vs.
Revision 2.40 by greg, Mon Apr 5 19:56:18 2021 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   #define _USE_MATH_DEFINES
11 #include <stdio.h>
12   #include <stdlib.h>
13 #include <string.h>
13   #include <ctype.h>
14   #include <math.h>
15 + #include "rtio.h"
16   #include "platform.h"
17   #include "bsdfrep.h"
18 #include "resolu.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 */
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 */
31 int             ninpfiles;      /* number of input files */
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)
# Line 44 | Line 48 | cmp_indir(const void *p1, const void *p2)
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");
52        FVECT   dv;
73          char    buf[2048];
74          int     c;
75          
# Line 60 | Line 80 | init_pabopto_inp(const int i, const char *fname)
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[32];
88 >                char    typ[64];
89                  if (fgets(buf, sizeof(buf), fp) == NULL) {
90                          fputs(fname, stderr);
91                          fputs(": unexpected EOF\n", stderr);
# Line 72 | Line 94 | init_pabopto_inp(const int i, const char *fname)
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")) {
105 >                        if (!strcasecmp(typ, "DSF"))
106                                  inpfile[i].isDSF = 1;
107 <                                continue;
79 <                        }
80 <                        if (!strcasecmp(typ, "BSDF") ||
107 >                        else if (!strcasecmp(typ, "BSDF") ||
108                                          !strcasecmp(typ, "BRDF") ||
109 <                                        !strcasecmp(typ, "BTDF")) {
109 >                                        !strcasecmp(typ, "BTDF"))
110                                  inpfile[i].isDSF = 0;
111 <                                continue;
85 <                        }
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)
# Line 104 | Line 132 | init_pabopto_inp(const int i, const char *fname)
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 <        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);
142 >        set_grid_pos(&inpfile[i]);
143          return(1);
144   }
145  
# Line 118 | Line 148 | 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;
151 >        double          theta_out, phi_out, val[3];
152          int             n, c;
153          
154          if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
# Line 134 | Line 164 | add_pabopto_inp(const int i)
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\n", &theta_out, &phi_out, &val) == 3)
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);
# Line 154 | Line 202 | add_pabopto_inp(const int i)
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             i;
233 <                                                /* start header */
234 <        SET_FILE_BINARY(stdout);
235 <        newheader("RADIANCE", stdout);
236 <        printargs(argc, argv, stdout);
167 <        fputnow(stdout);
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 <        while (argc > 2 && argv[1][0] == '-') {
239 <                switch (argv[1][1]) {
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[2]);
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 <                argv += 2; argc -= 2;
178 <        }
179 <                                                /* initialize & sort inputs */
180 <        ninpfiles = argc - 1;
259 >        totinc = ninpfiles = argc - a;          /* initialize & sort inputs */
260          if (ninpfiles < 2)
261                  goto userr;
262 <        inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles);
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 <        for (i = 0; i < ninpfiles; i++)
268 <                if (!init_pabopto_inp(i, argv[i+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 <        qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_indir);
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 < ninpfiles; i++)
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 <        save_bsdf_rep(stdout);                  /* write it out */
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 [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
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 < #else
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[])
# Line 217 | Line 348 | main(int argc, char *argv[])
348                  fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname);
349                  return(1);
350          }
220        ninpfiles = 1;
351          inpfile = &pginp;
352          if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
353                  return(1);
# Line 233 | Line 363 | main(int argc, char *argv[])
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 < GRIDRES; i++)
367 <            for (j = 0; j < GRIDRES; j++)
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 /
# Line 254 | Line 384 | main(int argc, char *argv[])
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) / (output_orient*dir[2]);
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",
# Line 265 | Line 395 | main(int argc, char *argv[])
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", GRIDRES-1, GRIDRES-1);
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 < GRIDRES; i++)
405 <            for (j = 0; j < GRIDRES; j++) {
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) / (output_orient*dir[2]);
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);
# Line 284 | Line 414 | main(int argc, char *argv[])
414   #endif
415          return(0);
416   }
417 < #endif
417 >
418 > #endif          /* TEST_MAIN */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines