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.27 by greg, Fri Oct 3 21:57:06 2014 UTC vs.
Revision 2.36 by greg, Fri Jul 19 17:37:56 2019 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  
# Line 25 | Line 24 | typedef struct {
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   int             ninpfiles;      /* number of input files */
33  
34 + int             rev_orient = 0; /* shall we reverse surface orientation? */
35 +
36   /* Compare incident angles */
37   static int
38   cmp_indir(const void *p1, const void *p2)
# Line 61 | Line 63 | init_pabopto_inp(const int i, const char *fname)
63          }
64          inpfile[i].fname = fname;
65          inpfile[i].isDSF = -1;
66 +        inpfile[i].nspec = 0;
67          inpfile[i].up_phi = 0;
68          inpfile[i].theta = inpfile[i].phi = -10001.;
69                                  /* read header information */
70          while ((c = getc(fp)) == '#' || c == EOF) {
71 <                char    typ[32];
71 >                char    typ[64];
72                  if (fgets(buf, sizeof(buf), fp) == NULL) {
73                          fputs(fname, stderr);
74                          fputs(": unexpected EOF\n", stderr);
# Line 74 | Line 77 | init_pabopto_inp(const int i, const char *fname)
77                  }
78                  if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1)
79                          continue;
80 +                if (sscanf(buf, "colorimetry: %s", typ) == 1) {
81 +                        if (!strcasecmp(typ, "CIE-XYZ"))
82 +                                inpfile[i].nspec = 3;
83 +                        else if (!strcasecmp(typ, "CIE-Y"))
84 +                                inpfile[i].nspec = 1;
85 +                        continue;
86 +                }
87                  if (sscanf(buf, "format: theta phi %s", typ) == 1) {
88 <                        if (!strcasecmp(typ, "DSF")) {
88 >                        if (!strcasecmp(typ, "DSF"))
89                                  inpfile[i].isDSF = 1;
90 <                                continue;
81 <                        }
82 <                        if (!strcasecmp(typ, "BSDF") ||
90 >                        else if (!strcasecmp(typ, "BSDF") ||
91                                          !strcasecmp(typ, "BRDF") ||
92 <                                        !strcasecmp(typ, "BTDF")) {
92 >                                        !strcasecmp(typ, "BTDF"))
93                                  inpfile[i].isDSF = 0;
94 <                                continue;
87 <                        }
94 >                        continue;
95                  }
96                  if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1)
97                          continue;
# Line 108 | Line 115 | init_pabopto_inp(const int i, const char *fname)
115                  fputs(": unknown incident angle\n", stderr);
116                  return(0);
117          }
118 +        if (rev_orient) {       /* reverse Z-axis to face outside */
119 +                inpfile[i].theta = 180. - inpfile[i].theta;
120 +                inpfile[i].phi = 360. - inpfile[i].phi;
121 +        }
122                                  /* convert to Y-up orientation */
123          inpfile[i].phi += 90.-inpfile[i].up_phi;
124                                  /* convert angle to grid position */
# Line 115 | Line 126 | init_pabopto_inp(const int i, const char *fname)
126          dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2];
127          dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2];
128          dv[2] = sqrt(1. - dv[2]*dv[2]);
129 <        pos_from_vec(inpfile[i].igp, dv);
129 >        if (inpfile[i].theta <= FTINY)
130 >                inpfile[i].igp[0] = inpfile[i].igp[1] = grid_res/2 - 1;
131 >        else
132 >                pos_from_vec(inpfile[i].igp, dv);
133          return(1);
134   }
135  
# Line 124 | Line 138 | static int
138   add_pabopto_inp(const int i)
139   {
140          FILE            *fp = fopen(inpfile[i].fname, "r");
141 <        double          theta_out, phi_out, val;
141 >        double          theta_out, phi_out, val[3];
142          int             n, c;
143          
144          if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
# Line 140 | Line 154 | add_pabopto_inp(const int i)
154                  fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n",
155                                          inpfile[i].theta, inpfile[i].phi);
156   #endif
157 +                if (inpfile[i].nspec)
158 +                        set_spectral_samples(inpfile[i].nspec);
159                  new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
160          }
161   #ifdef DEBUG
162          fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
163   #endif
164                                          /* read scattering data */
165 <        while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
165 >        while (fscanf(fp, "%lf %lf %lf", &theta_out, &phi_out, val) == 3) {
166 >                for (n = 1; n < inpfile[i].nspec; n++)
167 >                        if (fscanf(fp, "%lf", val+n) != 1) {
168 >                                fprintf(stderr, "%s: warning: unexpected EOF\n",
169 >                                                inpfile[i].fname);
170 >                                fclose(fp);
171 >                                return(1);
172 >                        }
173 >                if (rev_orient) {       /* reverse Z-axis to face outside */
174 >                        theta_out = 180. - theta_out;
175 >                        phi_out = 360. - phi_out;
176 >                }
177                  add_bsdf_data(theta_out, phi_out+90.-inpfile[i].up_phi,
178                                  val, inpfile[i].isDSF);
179 +        }
180          n = 0;
181          while ((c = getc(fp)) != EOF)
182                  n += !isspace(c);
# Line 161 | Line 189 | add_pabopto_inp(const int i)
189   }
190  
191   #ifndef TEST_MAIN
192 +
193 + #define SYM_ILL         '?'             /* illegal symmetry value */
194 + #define SYM_ISO         'I'             /* isotropic */
195 + #define SYM_QUAD        'Q'             /* quadrilateral symmetry */
196 + #define SYM_BILAT       'B'             /* bilateral symmetry */
197 + #define SYM_ANISO       'A'             /* anisotropic */
198 +
199 + static const char       quadrant_rep[16][16] = {
200 +                                "in-plane","0-90","90-180","0-180",
201 +                                "180-270","0-90+180-270","90-270",
202 +                                "0-270","270-360","270-90",
203 +                                "90-180+270-360","270-180","180-360",
204 +                                "180-90","90-360","0-360"
205 +                        };
206 + static const char       quadrant_sym[16] = {
207 +                                SYM_ISO, SYM_QUAD, SYM_QUAD, SYM_BILAT,
208 +                                SYM_QUAD, SYM_ILL, SYM_BILAT, SYM_ILL,
209 +                                SYM_QUAD, SYM_BILAT, SYM_ILL, SYM_ILL,
210 +                                SYM_BILAT, SYM_ILL, SYM_ILL, SYM_ANISO
211 +                        };
212 +
213   /* Read in PAB-Opto BSDF files and output RBF interpolant */
214   int
215   main(int argc, char *argv[])
216   {
217          extern int      nprocs;
218 +        const char      *symmetry = "U";
219          int             i;
220                                                  /* start header */
221          SET_FILE_BINARY(stdout);
# Line 175 | Line 225 | main(int argc, char *argv[])
225          progname = argv[0];                     /* get options */
226          while (argc > 2 && argv[1][0] == '-') {
227                  switch (argv[1][1]) {
228 +                case 't':
229 +                        rev_orient = !rev_orient;
230 +                        break;
231                  case 'n':
232                          nprocs = atoi(argv[2]);
233 +                        argv++; argc--;
234                          break;
235 +                case 's':
236 +                        symmetry = argv[2];
237 +                        argv++; argc--;
238 +                        break;
239                  default:
240                          goto userr;
241                  }
242 <                argv += 2; argc -= 2;
242 >                argv++; argc--;
243          }
244                                                  /* initialize & sort inputs */
245          ninpfiles = argc - 1;
# Line 199 | Line 257 | main(int argc, char *argv[])
257                  if (!add_pabopto_inp(i))
258                          return(1);
259          make_rbfrep();                          /* process last data set */
260 +                                                /* check input symmetry */
261 +        switch (toupper(symmetry[0])) {
262 +        case 'U':                               /* unspecified symmetry */
263 +                if (quadrant_sym[inp_coverage] != SYM_ILL)
264 +                        break;                  /* anything legal goes */
265 +                fprintf(stderr, "%s: unsupported phi coverage (%s)\n",
266 +                                progname, quadrant_rep[inp_coverage]);
267 +                return(1);
268 +        case SYM_ISO:                           /* legal symmetry types */
269 +        case SYM_QUAD:
270 +        case SYM_BILAT:
271 +        case SYM_ANISO:
272 +                if (quadrant_sym[inp_coverage] == toupper(symmetry[0]))
273 +                        break;                  /* matches spec */
274 +                fprintf(stderr,
275 +                "%s: phi coverage (%s) does not match requested '%s' symmetry\n",
276 +                                progname, quadrant_rep[inp_coverage], symmetry);
277 +                return(1);
278 +        default:
279 +                fprintf(stderr,
280 +  "%s: -s option must be Isotropic, Quadrilateral, Bilateral, or Anisotropic\n",
281 +                                progname);
282 +                return(1);
283 +        }
284 + #ifdef DEBUG
285 +        fprintf(stderr, "Input phi coverage (%s) has '%c' symmetry\n",
286 +                                quadrant_rep[inp_coverage],
287 +                                quadrant_sym[inp_coverage]);
288 + #endif
289          build_mesh();                           /* create interpolation */
290          save_bsdf_rep(stdout);                  /* write it out */
291          return(0);
292   userr:
293 <        fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
293 >        fprintf(stderr, "Usage: %s [-t][-n nproc][-s symmetry] meas1.dat meas2.dat .. > bsdf.sir\n",
294                                          progname);
295          return(1);
296   }
297 < #else
297 >
298 > #else           /* TEST_MAIN */
299 >
300   /* Test main produces a Radiance model from the given input file */
301   int
302   main(int argc, char *argv[])
# Line 240 | Line 329 | main(int argc, char *argv[])
329   #if 1                                           /* produce spheres at meas. */
330          puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
331          n = 0;
332 <        for (i = 0; i < GRIDRES; i++)
333 <            for (j = 0; j < GRIDRES; j++)
332 >        for (i = 0; i < grid_res; i++)
333 >            for (j = 0; j < grid_res; j++)
334                  if (dsf_grid[i][j].sum.n > 0) {
335                          ovec_from_pos(dir, i, j);
336                          bsdf = dsf_grid[i][j].sum.v /
# Line 272 | Line 361 | main(int argc, char *argv[])
361   #if 1                                           /* output continuous surface */
362          puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
363          fflush(stdout);
364 <        sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
364 >        sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1);
365          pfp = popen(buf, "w");
366          if (pfp == NULL) {
367                  fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
368                  return(1);
369          }
370 <        for (i = 0; i < GRIDRES; i++)
371 <            for (j = 0; j < GRIDRES; j++) {
370 >        for (i = 0; i < grid_res; i++)
371 >            for (j = 0; j < grid_res; j++) {
372                  ovec_from_pos(dir, i, j);
373                  bsdf = eval_rbfrep(dsf_list, dir);
374                  bsdf = log(bsdf + 1e-5) - min_log;
# Line 291 | Line 380 | main(int argc, char *argv[])
380   #endif
381          return(0);
382   }
383 < #endif
383 >
384 > #endif          /* TEST_MAIN */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines