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.29 by greg, Tue Feb 2 00:42:11 2016 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
12   #include <stdio.h>
13 + #include <stdlib.h>
14   #include <string.h>
15   #include <ctype.h>
16 + #include <math.h>
17   #include "platform.h"
18   #include "bsdfrep.h"
19 + #include "resolu.h"
20                                  /* global argv[0] */
21   char                    *progname;
22  
23 < /* Load a set of measurements corresponding to a particular incident angle */
23 > typedef struct {
24 >        const char      *fname;         /* input file path */
25 >        double          theta, phi;     /* input angles (degrees) */
26 >        double          up_phi;         /* azimuth for "up" direction */
27 >        int             igp[2];         /* input grid position */
28 >        int             isDSF;          /* data is DSF (rather than BSDF)? */
29 >        int             nspec;          /* number of spectral samples */
30 >        long            dstart;         /* data start offset in file */
31 > } PGINPUT;
32 >
33 > PGINPUT         *inpfile;       /* input files sorted by incidence */
34 > int             ninpfiles;      /* number of input files */
35 >
36 > int             rev_orient = 0; /* shall we reverse surface orientation? */
37 >
38 > /* Compare incident angles */
39   static int
40 < load_pabopto_meas(const char *fname)
40 > cmp_indir(const void *p1, const void *p2)
41   {
42 +        const PGINPUT   *inp1 = (const PGINPUT *)p1;
43 +        const PGINPUT   *inp2 = (const PGINPUT *)p2;
44 +        int             ydif = inp1->igp[1] - inp2->igp[1];
45 +        
46 +        if (ydif)
47 +                return(ydif);
48 +
49 +        return(inp1->igp[0] - inp2->igp[0]);
50 + }
51 +
52 + /* Prepare a PAB-Opto input file by reading its header */
53 + static int
54 + init_pabopto_inp(const int i, const char *fname)
55 + {
56          FILE    *fp = fopen(fname, "r");
57 <        int     inp_is_DSF = -1;
24 <        double  new_theta, new_phi, theta_out, phi_out, val;
57 >        FVECT   dv;
58          char    buf[2048];
59 <        int     n, c;
59 >        int     c;
60          
61          if (fp == NULL) {
62                  fputs(fname, stderr);
63                  fputs(": cannot open\n", stderr);
64                  return(0);
65          }
66 < #ifdef DEBUG
67 <        fprintf(stderr, "Loading measurement file '%s'...\n", fname);
68 < #endif
66 >        inpfile[i].fname = fname;
67 >        inpfile[i].isDSF = -1;
68 >        inpfile[i].nspec = 0;
69 >        inpfile[i].up_phi = 0;
70 >        inpfile[i].theta = inpfile[i].phi = -10001.;
71                                  /* read header information */
72          while ((c = getc(fp)) == '#' || c == EOF) {
73 +                char    typ[64];
74                  if (fgets(buf, sizeof(buf), fp) == NULL) {
75                          fputs(fname, stderr);
76                          fputs(": unexpected EOF\n", stderr);
77                          fclose(fp);
78                          return(0);
79                  }
80 <                if (!strcmp(buf, "format: theta phi DSF\n")) {
45 <                        inp_is_DSF = 1;
80 >                if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1)
81                          continue;
82 +                if (sscanf(buf, "colorimetry: %s", typ) == 1) {
83 +                        if (!strcasecmp(typ, "CIE-XYZ"))
84 +                                inpfile[i].nspec = 3;
85 +                        else if (!strcasecmp(typ, "CIE-Y"))
86 +                                inpfile[i].nspec = 1;
87 +                        continue;
88                  }
89 <                if (!strcmp(buf, "format: theta phi BSDF\n")) {
90 <                        inp_is_DSF = 0;
89 >                if (sscanf(buf, "format: theta phi %s", typ) == 1) {
90 >                        if (!strcasecmp(typ, "DSF"))
91 >                                inpfile[i].isDSF = 1;
92 >                        else if (!strcasecmp(typ, "BSDF") ||
93 >                                        !strcasecmp(typ, "BRDF") ||
94 >                                        !strcasecmp(typ, "BTDF"))
95 >                                inpfile[i].isDSF = 0;
96                          continue;
97                  }
98 <                if (sscanf(buf, "intheta %lf", &new_theta) == 1)
98 >                if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1)
99                          continue;
100 <                if (sscanf(buf, "inphi %lf", &new_phi) == 1)
100 >                if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
101                          continue;
102 +                if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
103 +                        continue;
104                  if (sscanf(buf, "incident_angle %lf %lf",
105 <                                &new_theta, &new_phi) == 2)
105 >                                &inpfile[i].theta, &inpfile[i].phi) == 2)
106                          continue;
107          }
108 <        ungetc(c, fp);
109 <        if (inp_is_DSF < 0) {
108 >        inpfile[i].dstart = ftell(fp) - 1;
109 >        fclose(fp);
110 >        if (inpfile[i].isDSF < 0) {
111                  fputs(fname, stderr);
112                  fputs(": unknown format\n", stderr);
64                fclose(fp);
113                  return(0);
114          }
115 +        if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
116 +                fputs(fname, stderr);
117 +                fputs(": unknown incident angle\n", stderr);
118 +                return(0);
119 +        }
120 +        if (rev_orient) {       /* reverse Z-axis to face outside */
121 +                inpfile[i].theta = 180. - inpfile[i].theta;
122 +                inpfile[i].phi = 360. - inpfile[i].phi;
123 +        }
124 +                                /* convert to Y-up orientation */
125 +        inpfile[i].phi += 90.-inpfile[i].up_phi;
126 +                                /* convert angle to grid position */
127 +        dv[2] = sin(M_PI/180.*inpfile[i].theta);
128 +        dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2];
129 +        dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2];
130 +        dv[2] = sqrt(1. - dv[2]*dv[2]);
131 +        pos_from_vec(inpfile[i].igp, dv);
132 +        return(1);
133 + }
134 +
135 + /* Load a set of measurements corresponding to a particular incident angle */
136 + static int
137 + add_pabopto_inp(const int i)
138 + {
139 +        FILE            *fp = fopen(inpfile[i].fname, "r");
140 +        double          theta_out, phi_out, val[3];
141 +        int             n, c;
142 +        
143 +        if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
144 +                fputs(inpfile[i].fname, stderr);
145 +                fputs(": cannot open\n", stderr);
146 +                return(0);
147 +        }
148                                          /* prepare input grid */
149 <        new_bsdf_data(new_theta, new_phi);
150 <                                        /* read actual data */
151 <        while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
152 <                add_bsdf_data(theta_out, phi_out, val, inp_is_DSF);
149 >        if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) {
150 >                if (i)                  /* process previous incidence */
151 >                        make_rbfrep();
152 > #ifdef DEBUG
153 >                fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n",
154 >                                        inpfile[i].theta, inpfile[i].phi);
155 > #endif
156 >                if (inpfile[i].nspec)
157 >                        set_spectral_samples(inpfile[i].nspec);
158 >                new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
159 >        }
160 > #ifdef DEBUG
161 >        fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
162 > #endif
163 >                                        /* read scattering data */
164 >        while (fscanf(fp, "%lf %lf %lf", &theta_out, &phi_out, val) == 3) {
165 >                for (n = 1; n < inpfile[i].nspec; n++)
166 >                        if (fscanf(fp, "%lf", val+n) != 1) {
167 >                                fprintf(stderr, "%s: warning: unexpected EOF\n",
168 >                                                inpfile[i].fname);
169 >                                fclose(fp);
170 >                                return(1);
171 >                        }
172 >                if (rev_orient) {       /* reverse Z-axis to face outside */
173 >                        theta_out = 180. - theta_out;
174 >                        phi_out = 360. - phi_out;
175 >                }
176 >                add_bsdf_data(theta_out, phi_out+90.-inpfile[i].up_phi,
177 >                                val, inpfile[i].isDSF);
178 >        }
179          n = 0;
180          while ((c = getc(fp)) != EOF)
181                  n += !isspace(c);
182          if (n)
183                  fprintf(stderr,
184                          "%s: warning: %d unexpected characters past EOD\n",
185 <                                fname, n);
185 >                                inpfile[i].fname, n);
186          fclose(fp);
187          return(1);
188   }
189  
190 + #ifndef TEST_MAIN
191   /* Read in PAB-Opto BSDF files and output RBF interpolant */
192   int
193   main(int argc, char *argv[])
# Line 94 | Line 202 | main(int argc, char *argv[])
202          progname = argv[0];                     /* get options */
203          while (argc > 2 && argv[1][0] == '-') {
204                  switch (argv[1][1]) {
205 +                case 't':
206 +                        rev_orient = !rev_orient;
207 +                        break;
208                  case 'n':
209                          nprocs = atoi(argv[2]);
210 +                        argv++; argc--;
211                          break;
212                  default:
213                          goto userr;
214                  }
215 <                argv += 2; argc -= 2;
215 >                argv++; argc--;
216          }
217 <        if (argc < 3)
217 >                                                /* initialize & sort inputs */
218 >        ninpfiles = argc - 1;
219 >        if (ninpfiles < 2)
220                  goto userr;
221 <        for (i = 1; i < argc; i++) {            /* compile measurements */
222 <                if (!load_pabopto_meas(argv[i]))
221 >        inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles);
222 >        if (inpfile == NULL)
223 >                return(1);
224 >        for (i = 0; i < ninpfiles; i++)
225 >                if (!init_pabopto_inp(i, argv[i+1]))
226                          return(1);
227 <                make_rbfrep();
228 <        }
227 >        qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir);
228 >                                                /* compile measurements */
229 >        for (i = 0; i < ninpfiles; i++)
230 >                if (!add_pabopto_inp(i))
231 >                        return(1);
232 >        make_rbfrep();                          /* process last data set */
233          build_mesh();                           /* create interpolation */
234          save_bsdf_rep(stdout);                  /* write it out */
235          return(0);
236   userr:
237 <        fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
237 >        fprintf(stderr, "Usage: %s [-t][-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
238                                          progname);
239          return(1);
240   }
241 + #else
242 + /* Test main produces a Radiance model from the given input file */
243 + int
244 + main(int argc, char *argv[])
245 + {
246 +        PGINPUT pginp;
247 +        char    buf[128];
248 +        FILE    *pfp;
249 +        double  bsdf, min_log;
250 +        FVECT   dir;
251 +        int     i, j, n;
252 +
253 +        progname = argv[0];
254 +        if (argc != 2) {
255 +                fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname);
256 +                return(1);
257 +        }
258 +        ninpfiles = 1;
259 +        inpfile = &pginp;
260 +        if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
261 +                return(1);
262 +                                                /* reduce data set */
263 +        if (make_rbfrep() == NULL) {
264 +                fprintf(stderr, "%s: nothing to plot!\n", progname);
265 +                exit(1);
266 +        }
267 + #ifdef DEBUG
268 +        fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
269 + #endif
270 +        min_log = log(bsdf_min*.5 + 1e-5);
271 + #if 1                                           /* produce spheres at meas. */
272 +        puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
273 +        n = 0;
274 +        for (i = 0; i < grid_res; i++)
275 +            for (j = 0; j < grid_res; j++)
276 +                if (dsf_grid[i][j].sum.n > 0) {
277 +                        ovec_from_pos(dir, i, j);
278 +                        bsdf = dsf_grid[i][j].sum.v /
279 +                           ((double)dsf_grid[i][j].sum.n*output_orient*dir[2]);
280 +                        if (bsdf <= bsdf_min*.6)
281 +                                continue;
282 +                        bsdf = log(bsdf + 1e-5) - min_log;
283 +                        ovec_from_pos(dir, i, j);
284 +                        printf("yellow sphere s%04d\n0\n0\n", ++n);
285 +                        printf("4 %.6g %.6g %.6g %.6g\n\n",
286 +                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
287 +                                        .007*bsdf);
288 +                }
289 + #endif
290 + #if 1                                           /* spheres at RBF peaks */
291 +        puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
292 +        for (n = 0; n < dsf_list->nrbf; n++) {
293 +                RBFVAL  *rbf = &dsf_list->rbfa[n];
294 +                ovec_from_pos(dir, rbf->gx, rbf->gy);
295 +                bsdf = eval_rbfrep(dsf_list, dir);
296 +                bsdf = log(bsdf + 1e-5) - min_log;
297 +                printf("red sphere p%04d\n0\n0\n", ++n);
298 +                printf("4 %.6g %.6g %.6g %.6g\n\n",
299 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
300 +                                .011*bsdf);
301 +        }
302 + #endif
303 + #if 1                                           /* output continuous surface */
304 +        puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
305 +        fflush(stdout);
306 +        sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1);
307 +        pfp = popen(buf, "w");
308 +        if (pfp == NULL) {
309 +                fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
310 +                return(1);
311 +        }
312 +        for (i = 0; i < grid_res; i++)
313 +            for (j = 0; j < grid_res; j++) {
314 +                ovec_from_pos(dir, i, j);
315 +                bsdf = eval_rbfrep(dsf_list, dir);
316 +                bsdf = log(bsdf + 1e-5) - min_log;
317 +                fprintf(pfp, "%.8e %.8e %.8e\n",
318 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
319 +            }
320 +        if (pclose(pfp) != 0)
321 +                return(1);
322 + #endif
323 +        return(0);
324 + }
325 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines