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.16 by greg, Mon Mar 17 01:52:51 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 < #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 "bsdfrep.h"
17 #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;     /* incident angles (in degrees) */
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   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_inang(const void *p1, const void *p2)
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 (inp1->theta > inp2->theta+FTINY)
45 <                return(1);
46 <        if (inp1->theta < inp2->theta-FTINY)
47 <                return(-1);
42 <        if (inp1->phi > inp2->phi+FTINY)
43 <                return(1);
44 <        if (inp1->phi < inp2->phi-FTINY)
45 <                return(-1);
46 <        return(0);
44 >        if (ydif)
45 >                return(ydif);
46 >
47 >        return(inp1->igp[0] - inp2->igp[0]);
48   }
49  
50   /* Prepare a PAB-Opto input file by reading its header */
# Line 51 | Line 52 | static int
52   init_pabopto_inp(const int i, const char *fname)
53   {
54          FILE    *fp = fopen(fname, "r");
55 +        FVECT   dv;
56          char    buf[2048];
57          int     c;
58          
# 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 73 | 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;
91 <                        }
92 <                        if (!strcasecmp(typ, "BSDF")) {
90 >                        else if (!strcasecmp(typ, "BSDF") ||
91 >                                        !strcasecmp(typ, "BRDF") ||
92 >                                        !strcasecmp(typ, "BTDF"))
93                                  inpfile[i].isDSF = 0;
94 <                                continue;
84 <                        }
94 >                        continue;
95                  }
96 +                if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1)
97 +                        continue;
98                  if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
99                          continue;
100                  if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
# Line 103 | Line 115 | init_pabopto_inp(const int i, const char *fname)
115                  fputs(": unknown incident angle\n", stderr);
116                  return(0);
117          }
118 <        while (inpfile[i].phi < 0)      /* normalize phi direction */
119 <                inpfile[i].phi += 360.;
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 */
125 >        dv[2] = sin(M_PI/180.*inpfile[i].theta);
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 >        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 112 | Line 137 | init_pabopto_inp(const int i, const char *fname)
137   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;
142 <        int     n, c;
140 >        FILE            *fp = fopen(inpfile[i].fname, "r");
141 >        double          theta_out, phi_out, val[3];
142 >        int             n, c;
143          
144          if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
145                  fputs(inpfile[i].fname, stderr);
# Line 122 | Line 147 | add_pabopto_inp(const int i)
147                  return(0);
148          }
149                                          /* prepare input grid */
150 <        if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) {
151 <                if (i)                  /* need to process previous incidence */
150 >        if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) {
151 >                if (i)                  /* process previous incidence */
152                          make_rbfrep();
153   #ifdef DEBUG
154 <                fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n",
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)
166 <                add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
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 149 | 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 163 | 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 181 | Line 251 | main(int argc, char *argv[])
251          for (i = 0; i < ninpfiles; i++)
252                  if (!init_pabopto_inp(i, argv[i+1]))
253                          return(1);
254 <        qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang);
254 >        qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir);
255                                                  /* compile measurements */
256          for (i = 0; i < ninpfiles; i++)
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 207 | Line 308 | main(int argc, char *argv[])
308          FVECT   dir;
309          int     i, j, n;
310  
311 +        progname = argv[0];
312          if (argc != 2) {
313 <                fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]);
313 >                fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname);
314                  return(1);
315          }
316          ninpfiles = 1;
# Line 216 | Line 318 | main(int argc, char *argv[])
318          if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
319                  return(1);
320                                                  /* reduce data set */
321 <        make_rbfrep();
321 >        if (make_rbfrep() == NULL) {
322 >                fprintf(stderr, "%s: nothing to plot!\n", progname);
323 >                exit(1);
324 >        }
325   #ifdef DEBUG
326          fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
327   #endif
# Line 224 | 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 /
337 <                                (dsf_grid[i][j].sum.n*output_orient*dir[2]);
337 >                           ((double)dsf_grid[i][j].sum.n*output_orient*dir[2]);
338                          if (bsdf <= bsdf_min*.6)
339                                  continue;
340                          bsdf = log(bsdf + 1e-5) - min_log;
# Line 245 | Line 350 | main(int argc, char *argv[])
350          for (n = 0; n < dsf_list->nrbf; n++) {
351                  RBFVAL  *rbf = &dsf_list->rbfa[n];
352                  ovec_from_pos(dir, rbf->gx, rbf->gy);
353 <                bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
353 >                bsdf = eval_rbfrep(dsf_list, dir);
354                  bsdf = log(bsdf + 1e-5) - min_log;
355                  printf("red sphere p%04d\n0\n0\n", ++n);
356                  printf("4 %.6g %.6g %.6g %.6g\n\n",
# Line 256 | 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", argv[0], buf);
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) / (output_orient*dir[2]);
373 >                bsdf = eval_rbfrep(dsf_list, dir);
374                  bsdf = log(bsdf + 1e-5) - min_log;
375                  fprintf(pfp, "%.8e %.8e %.8e\n",
376                                  dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
# Line 275 | 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