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.1 by greg, Fri Oct 19 04:14:29 2012 UTC vs.
Revision 2.38 by greg, Tue Feb 16 15:49:32 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>
12 < #include <string.h>
11 > #define _USE_MATH_DEFINES
12 > #include <stdlib.h>
13   #include <ctype.h>
14 + #include <math.h>
15 + #include "rtio.h"
16   #include "platform.h"
17   #include "bsdfrep.h"
18                                  /* global argv[0] */
19   char                    *progname;
20  
21 < /* Load a set of measurements corresponding to a particular incident angle */
21 > typedef struct {
22 >        const char      *fname;         /* input file path */
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 >
33 > int             rev_orient = 0; /* shall we reverse surface orientation? */
34 >
35 > /* Compare incident angles */
36   static int
37 < load_pabopto_meas(const char *fname)
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 (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)
69 + {
70          FILE    *fp = fopen(fname, "r");
23        int     inp_is_DSF = -1;
24        double  new_theta, new_phi, theta_out, phi_out, val;
71          char    buf[2048];
72 <        int     n, c;
72 >        int     c;
73          
74          if (fp == NULL) {
75                  fputs(fname, stderr);
76                  fputs(": cannot open\n", stderr);
77                  return(0);
78          }
79 < #ifdef DEBUG
80 <        fprintf(stderr, "Loading measurement file '%s'...\n", fname);
81 < #endif
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[64];
87                  if (fgets(buf, sizeof(buf), fp) == NULL) {
88                          fputs(fname, stderr);
89                          fputs(": unexpected EOF\n", stderr);
90                          fclose(fp);
91                          return(0);
92                  }
93 <                if (!strcmp(buf, "format: theta phi DSF\n")) {
45 <                        inp_is_DSF = 1;
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 (!strcmp(buf, "format: theta phi BSDF\n")) {
103 <                        inp_is_DSF = 0;
102 >                if (sscanf(buf, "format: theta phi %s", typ) == 1) {
103 >                        if (!strcasecmp(typ, "DSF"))
104 >                                inpfile[i].isDSF = 1;
105 >                        else if (!strcasecmp(typ, "BSDF") ||
106 >                                        !strcasecmp(typ, "BRDF") ||
107 >                                        !strcasecmp(typ, "BTDF"))
108 >                                inpfile[i].isDSF = 0;
109                          continue;
110                  }
111 <                if (sscanf(buf, "intheta %lf", &new_theta) == 1)
111 >                if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1)
112                          continue;
113 <                if (sscanf(buf, "inphi %lf", &new_phi) == 1)
113 >                if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
114                          continue;
115 +                if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
116 +                        continue;
117                  if (sscanf(buf, "incident_angle %lf %lf",
118 <                                &new_theta, &new_phi) == 2)
118 >                                &inpfile[i].theta, &inpfile[i].phi) == 2)
119                          continue;
120          }
121 <        ungetc(c, fp);
122 <        if (inp_is_DSF < 0) {
121 >        inpfile[i].dstart = ftell(fp) - 1;
122 >        fclose(fp);
123 >        if (inpfile[i].isDSF < 0) {
124                  fputs(fname, stderr);
125                  fputs(": unknown format\n", stderr);
64                fclose(fp);
126                  return(0);
127          }
128 +        if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
129 +                fputs(fname, stderr);
130 +                fputs(": unknown incident angle\n", stderr);
131 +                return(0);
132 +        }
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 +
144 + /* Load a set of measurements corresponding to a particular incident angle */
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[3];
150 +        int             n, c;
151 +        
152 +        if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
153 +                fputs(inpfile[i].fname, stderr);
154 +                fputs(": cannot open\n", stderr);
155 +                return(0);
156 +        }
157                                          /* prepare input grid */
158 <        new_bsdf_data(new_theta, new_phi);
159 <                                        /* read actual data */
160 <        while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
161 <                add_bsdf_data(theta_out, phi_out, val, inp_is_DSF);
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)=(%.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", &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);
191          if (n)
192                  fprintf(stderr,
193                          "%s: warning: %d unexpected characters past EOD\n",
194 <                                fname, n);
194 >                                inpfile[i].fname, n);
195          fclose(fp);
196          return(1);
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;
227 >        const char      *symmetry = "0";
228 >        int             ninpfiles, totinc;
229 >        int             a, i;
230                                                  /* start header */
231          SET_FILE_BINARY(stdout);
232          newheader("RADIANCE", stdout);
233          printargs(argc, argv, stdout);
234          fputnow(stdout);
235          progname = argv[0];                     /* get options */
236 <        while (argc > 2 && argv[1][0] == '-') {
237 <                switch (argv[1][1]) {
236 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
237 >                switch (argv[a][1]) {
238 >                case 't':
239 >                        rev_orient = !rev_orient;
240 >                        break;
241                  case 'n':
242 <                        nprocs = atoi(argv[2]);
242 >                        nprocs = atoi(argv[++a]);
243                          break;
244 +                case 's':
245 +                        symmetry = argv[++a];
246 +                        break;
247                  default:
248                          goto userr;
249                  }
250 <                argv += 2; argc -= 2;
251 <        }
105 <        if (argc < 3)
250 >        totinc = ninpfiles = argc - a;          /* initialize & sort inputs */
251 >        if (ninpfiles < 2)
252                  goto userr;
253 <        for (i = 1; i < argc; i++) {            /* compile measurements */
254 <                if (!load_pabopto_meas(argv[i]))
253 >        if (toupper(symmetry[0]) == SYM_UP)     /* special case for "up" symmetry */
254 >                totinc += ninpfiles;
255 >        inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*totinc);
256 >        if (inpfile == NULL)
257 >                return(1);
258 >        for (i = 0; i < ninpfiles; i++)
259 >                if (!init_pabopto_inp(i, argv[a+i]))
260                          return(1);
261 <                make_rbfrep();
261 >
262 >        for (i = ninpfiles; i < totinc; i++) {  /* copy for "up" symmetry */
263 >                inpfile[i] = inpfile[i-ninpfiles];
264 >                inpfile[i].phi += 180.;         /* invert duplicate data */
265 >                inpfile[i].up_phi -= 180.;
266 >                set_grid_pos(&inpfile[i]);      /* grid location for sorting */
267          }
268 +        qsort(inpfile, totinc, sizeof(PGINPUT), cmp_indir);
269 +                                                /* compile measurements */
270 +        for (i = 0; i < totinc; i++)
271 +                if (!add_pabopto_inp(i))
272 +                        return(1);
273 +        make_rbfrep();                          /* process last data set */
274 +                                                /* check input symmetry */
275 +        switch (toupper(symmetry[0])) {
276 +        case '0':                               /* unspecified symmetry */
277 +                if (quadrant_sym[inp_coverage] != SYM_ILL)
278 +                        break;                  /* anything legal goes */
279 +                fprintf(stderr, "%s: unsupported phi coverage (%s)\n",
280 +                                progname, quadrant_rep[inp_coverage]);
281 +                return(1);
282 +        case SYM_UP:                            /* faux "up" symmetry */
283 +                if (quadrant_sym[inp_coverage] == SYM_ANISO)
284 +                        break;
285 +                /* fall through */
286 +        case SYM_ISO:                           /* usual symmetry types */
287 +        case SYM_QUAD:
288 +        case SYM_BILAT:
289 +        case SYM_ANISO:
290 +                if (quadrant_sym[inp_coverage] == toupper(symmetry[0]))
291 +                        break;                  /* matches spec */
292 +                fprintf(stderr,
293 +                "%s: phi coverage (%s) does not match requested '%s' symmetry\n",
294 +                                progname, quadrant_rep[inp_coverage], symmetry);
295 +                return(1);
296 +        default:
297 +                fprintf(stderr,
298 +  "%s: -s option must be Isotropic, Quadrilateral, Bilateral, Up, or Anisotropic\n",
299 +                                progname);
300 +                return(1);
301 +        }
302 + #ifdef DEBUG
303 +        fprintf(stderr, "Input phi coverage (%s) has '%c' symmetry\n",
304 +                                quadrant_rep[inp_coverage],
305 +                                quadrant_sym[inp_coverage]);
306 + #endif
307          build_mesh();                           /* create interpolation */
308          save_bsdf_rep(stdout);                  /* write it out */
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 +
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[])
321 + {
322 +        PGINPUT pginp;
323 +        char    buf[128];
324 +        FILE    *pfp;
325 +        double  bsdf, min_log;
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", progname);
332 +                return(1);
333 +        }
334 +        inpfile = &pginp;
335 +        if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
336 +                return(1);
337 +                                                /* reduce data set */
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
345 +        min_log = log(bsdf_min*.5 + 1e-5);
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 < 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 +                           ((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;
358 +                        ovec_from_pos(dir, i, j);
359 +                        printf("yellow sphere s%04d\n0\n0\n", ++n);
360 +                        printf("4 %.6g %.6g %.6g %.6g\n\n",
361 +                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
362 +                                        .007*bsdf);
363 +                }
364 + #endif
365 + #if 1                                           /* spheres at RBF peaks */
366 +        puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
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);
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",
374 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
375 +                                .011*bsdf);
376 +        }
377 + #endif
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", grid_res-1, grid_res-1);
382 +        pfp = popen(buf, "w");
383 +        if (pfp == NULL) {
384 +                fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
385 +                return(1);
386 +        }
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);
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);
394 +            }
395 +        if (pclose(pfp) != 0)
396 +                return(1);
397 + #endif
398 +        return(0);
399 + }
400 +
401 + #endif          /* TEST_MAIN */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines