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.28 by greg, Fri Jan 29 16:21:56 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines