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.41 by greg, Mon Apr 5 21:06: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 > double          lim_graze = 0;  /* limit scattering near grazing (deg above) */
36 >
37 > /* Compare incident angles */
38   static int
39 < load_pabopto_meas(const char *fname)
39 > cmp_indir(const void *p1, const void *p2)
40   {
41 +        const PGINPUT   *inp1 = (const PGINPUT *)p1;
42 +        const PGINPUT   *inp2 = (const PGINPUT *)p2;
43 +        int             ydif = inp1->igp[1] - inp2->igp[1];
44 +        
45 +        if (ydif)
46 +                return(ydif);
47 +
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");
23        int     inp_is_DSF = -1;
24        double  new_theta, new_phi, theta_out, phi_out, val;
73          char    buf[2048];
74 <        int     n, c;
74 >        int     c;
75          
76          if (fp == NULL) {
77                  fputs(fname, stderr);
78                  fputs(": cannot open\n", stderr);
79                  return(0);
80          }
81 < #ifdef DEBUG
82 <        fprintf(stderr, "Loading measurement file '%s'...\n", fname);
83 < #endif
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[64];
89                  if (fgets(buf, sizeof(buf), fp) == NULL) {
90                          fputs(fname, stderr);
91                          fputs(": unexpected EOF\n", stderr);
92                          fclose(fp);
93                          return(0);
94                  }
95 <                if (!strcmp(buf, "format: theta phi DSF\n")) {
45 <                        inp_is_DSF = 1;
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 (!strcmp(buf, "format: theta phi BSDF\n")) {
105 <                        inp_is_DSF = 0;
104 >                if (sscanf(buf, "format: theta phi %s", typ) == 1) {
105 >                        if (!strcasecmp(typ, "DSF"))
106 >                                inpfile[i].isDSF = 1;
107 >                        else if (!strcasecmp(typ, "BSDF") ||
108 >                                        !strcasecmp(typ, "BRDF") ||
109 >                                        !strcasecmp(typ, "BTDF"))
110 >                                inpfile[i].isDSF = 0;
111                          continue;
112                  }
113 <                if (sscanf(buf, "intheta %lf", &new_theta) == 1)
113 >                if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1)
114                          continue;
115 <                if (sscanf(buf, "inphi %lf", &new_phi) == 1)
115 >                if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
116                          continue;
117 +                if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
118 +                        continue;
119                  if (sscanf(buf, "incident_angle %lf %lf",
120 <                                &new_theta, &new_phi) == 2)
120 >                                &inpfile[i].theta, &inpfile[i].phi) == 2)
121                          continue;
122          }
123 <        ungetc(c, fp);
124 <        if (inp_is_DSF < 0) {
123 >        inpfile[i].dstart = ftell(fp) - 1;
124 >        fclose(fp);
125 >        if (inpfile[i].isDSF < 0) {
126                  fputs(fname, stderr);
127                  fputs(": unknown format\n", stderr);
64                fclose(fp);
128                  return(0);
129          }
130 +        if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
131 +                fputs(fname, stderr);
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 +        set_grid_pos(&inpfile[i]);
143 +        return(1);
144 + }
145 +
146 + /* Load a set of measurements corresponding to a particular incident angle */
147 + 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[3];
152 +        int             n, c;
153 +        
154 +        if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
155 +                fputs(inpfile[i].fname, stderr);
156 +                fputs(": cannot open\n", stderr);
157 +                return(0);
158 +        }
159                                          /* prepare input grid */
160 <        new_bsdf_data(new_theta, new_phi);
161 <                                        /* read actual data */
162 <        while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
163 <                add_bsdf_data(theta_out, phi_out, val, inp_is_DSF);
160 >        if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) {
161 >                if (i)                  /* process previous incidence */
162 >                        make_rbfrep();
163 > #ifdef DEBUG
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", &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);
196          if (n)
197                  fprintf(stderr,
198                          "%s: warning: %d unexpected characters past EOD\n",
199 <                                fname, n);
199 >                                inpfile[i].fname, n);
200          fclose(fp);
201          return(1);
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);
237 <        fputnow(stdout);
232 >        static char     gval_buf[16];
233 >        char *          auto_grazing = NULL;
234 >        const char      *symmetry = "0";
235 >        int             ninpfiles, totinc;
236 >        int             a, i;
237 >
238          progname = argv[0];                     /* get options */
239 <        while (argc > 2 && argv[1][0] == '-') {
240 <                switch (argv[1][1]) {
239 >        for (a = 1; a < argc && argv[a][0] == '-'; a++)
240 >                switch (argv[a][1]) {
241 >                case 't':
242 >                        rev_orient = !rev_orient;
243 >                        break;
244                  case 'n':
245 <                        nprocs = atoi(argv[2]);
245 >                        nprocs = atoi(argv[++a]);
246                          break;
247 +                case 's':
248 +                        symmetry = argv[++a];
249 +                        break;
250 +                case 'g':
251 +                        if (toupper(argv[a+1][0]) == 'A')
252 +                                auto_grazing = argv[a+1] = gval_buf;
253 +                        else
254 +                                lim_graze = atof(argv[a+1]);
255 +                        ++a;
256 +                        break;
257                  default:
258                          goto userr;
259                  }
260 <                argv += 2; argc -= 2;
261 <        }
105 <        if (argc < 3)
260 >        totinc = ninpfiles = argc - a;          /* initialize & sort inputs */
261 >        if (ninpfiles < 2)
262                  goto userr;
263 <        for (i = 1; i < argc; i++) {            /* compile measurements */
264 <                if (!load_pabopto_meas(argv[i]))
263 >        if (toupper(symmetry[0]) == SYM_UP)     /* special case for "up" symmetry */
264 >                totinc += ninpfiles;
265 >        inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*totinc);
266 >        if (inpfile == NULL)
267 >                return(1);
268 >        if (auto_grazing)
269 >                lim_graze = 90.;
270 >        for (i = 0; i < ninpfiles; i++) {
271 >                if (!init_pabopto_inp(i, argv[a+i]))
272                          return(1);
273 <                make_rbfrep();
273 >                if (auto_grazing && fabs(inpfile[i].theta - 90.) < lim_graze)
274 >                        lim_graze = fabs(inpfile[i].theta - 90.);
275          }
276 +        if (auto_grazing)
277 +                sprintf(auto_grazing, "%.2f", lim_graze);
278 +        for (i = ninpfiles; i < totinc; i++) {  /* copy for "up" symmetry */
279 +                inpfile[i] = inpfile[i-ninpfiles];
280 +                inpfile[i].phi += 180.;         /* invert duplicate data */
281 +                inpfile[i].up_phi -= 180.;
282 +                set_grid_pos(&inpfile[i]);      /* grid location for sorting */
283 +        }
284 +        qsort(inpfile, totinc, sizeof(PGINPUT), cmp_indir);
285 +                                                /* compile measurements */
286 +        for (i = 0; i < totinc; i++)
287 +                if (!add_pabopto_inp(i))
288 +                        return(1);
289 +        make_rbfrep();                          /* process last data set */
290 +                                                /* check input symmetry */
291 +        switch (toupper(symmetry[0])) {
292 +        case '0':                               /* unspecified symmetry */
293 +                if (quadrant_sym[inp_coverage] != SYM_ILL)
294 +                        break;                  /* anything legal goes */
295 +                fprintf(stderr, "%s: unsupported phi coverage (%s)\n",
296 +                                progname, quadrant_rep[inp_coverage]);
297 +                return(1);
298 +        case SYM_UP:                            /* faux "up" symmetry */
299 +                if (quadrant_sym[inp_coverage] == SYM_ANISO)
300 +                        break;
301 +                /* fall through */
302 +        case SYM_ISO:                           /* usual symmetry types */
303 +        case SYM_QUAD:
304 +        case SYM_BILAT:
305 +        case SYM_ANISO:
306 +                if (quadrant_sym[inp_coverage] == toupper(symmetry[0]))
307 +                        break;                  /* matches spec */
308 +                fprintf(stderr,
309 +                "%s: phi coverage (%s) does not match requested '%s' symmetry\n",
310 +                                progname, quadrant_rep[inp_coverage], symmetry);
311 +                return(1);
312 +        default:
313 +                fprintf(stderr,
314 +  "%s: -s option must be Isotropic, Quadrilateral, Bilateral, Up, or Anisotropic\n",
315 +                                progname);
316 +                return(1);
317 +        }
318 + #ifdef DEBUG
319 +        fprintf(stderr, "Input phi coverage (%s) has '%c' symmetry\n",
320 +                                quadrant_rep[inp_coverage],
321 +                                quadrant_sym[inp_coverage]);
322 + #endif
323          build_mesh();                           /* create interpolation */
324 <        save_bsdf_rep(stdout);                  /* write it out */
324 >        SET_FILE_BINARY(stdout);                /* start header */
325 >        newheader("RADIANCE", stdout);
326 >        printargs(argc, argv, stdout);
327 >        fputnow(stdout);
328 >        save_bsdf_rep(stdout);                  /* complete header + data */
329          return(0);
330   userr:
331 <        fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
331 >        fprintf(stderr, "Usage: %s [-t][-n nproc][-s symmetry][-g angle|'A'] meas1.dat meas2.dat .. > bsdf.sir\n",
332                                          progname);
333          return(1);
334   }
335 +
336 + #else           /* TEST_MAIN */
337 +
338 + /* Test main produces a Radiance model from the given input file */
339 + int
340 + main(int argc, char *argv[])
341 + {
342 +        PGINPUT pginp;
343 +        char    buf[128];
344 +        FILE    *pfp;
345 +        double  bsdf, min_log;
346 +        FVECT   dir;
347 +        int     i, j, n;
348 +
349 +        progname = argv[0];
350 +        if (argc != 2) {
351 +                fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname);
352 +                return(1);
353 +        }
354 +        inpfile = &pginp;
355 +        if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
356 +                return(1);
357 +                                                /* reduce data set */
358 +        if (make_rbfrep() == NULL) {
359 +                fprintf(stderr, "%s: nothing to plot!\n", progname);
360 +                exit(1);
361 +        }
362 + #ifdef DEBUG
363 +        fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
364 + #endif
365 +        min_log = log(bsdf_min*.5 + 1e-5);
366 + #if 1                                           /* produce spheres at meas. */
367 +        puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
368 +        n = 0;
369 +        for (i = 0; i < grid_res; i++)
370 +            for (j = 0; j < grid_res; j++)
371 +                if (dsf_grid[i][j].sum.n > 0) {
372 +                        ovec_from_pos(dir, i, j);
373 +                        bsdf = dsf_grid[i][j].sum.v /
374 +                           ((double)dsf_grid[i][j].sum.n*output_orient*dir[2]);
375 +                        if (bsdf <= bsdf_min*.6)
376 +                                continue;
377 +                        bsdf = log(bsdf + 1e-5) - min_log;
378 +                        ovec_from_pos(dir, i, j);
379 +                        printf("yellow sphere s%04d\n0\n0\n", ++n);
380 +                        printf("4 %.6g %.6g %.6g %.6g\n\n",
381 +                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
382 +                                        .007*bsdf);
383 +                }
384 + #endif
385 + #if 1                                           /* spheres at RBF peaks */
386 +        puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
387 +        for (n = 0; n < dsf_list->nrbf; n++) {
388 +                RBFVAL  *rbf = &dsf_list->rbfa[n];
389 +                ovec_from_pos(dir, rbf->gx, rbf->gy);
390 +                bsdf = eval_rbfrep(dsf_list, dir);
391 +                bsdf = log(bsdf + 1e-5) - min_log;
392 +                printf("red sphere p%04d\n0\n0\n", ++n);
393 +                printf("4 %.6g %.6g %.6g %.6g\n\n",
394 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
395 +                                .011*bsdf);
396 +        }
397 + #endif
398 + #if 1                                           /* output continuous surface */
399 +        puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
400 +        fflush(stdout);
401 +        sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1);
402 +        pfp = popen(buf, "w");
403 +        if (pfp == NULL) {
404 +                fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
405 +                return(1);
406 +        }
407 +        for (i = 0; i < grid_res; i++)
408 +            for (j = 0; j < grid_res; j++) {
409 +                ovec_from_pos(dir, i, j);
410 +                bsdf = eval_rbfrep(dsf_list, dir);
411 +                bsdf = log(bsdf + 1e-5) - min_log;
412 +                fprintf(pfp, "%.8e %.8e %.8e\n",
413 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
414 +            }
415 +        if (pclose(pfp) != 0)
416 +                return(1);
417 + #endif
418 +        return(0);
419 + }
420 +
421 + #endif          /* TEST_MAIN */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines