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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines