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.4 by greg, Sun Jun 30 14:46:29 2013 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   typedef struct {
23          const char      *fname;         /* input file path */
24 <        double          theta, phi;     /* incident angles (in degrees) */
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  
# Line 28 | Line 34 | int            ninpfiles;      /* number of input files */
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);
40 <        if (inp1->phi > inp2->phi+FTINY)
41 <                return(1);
42 <        if (inp1->phi < inp2->phi-FTINY)
43 <                return(-1);
44 <        return(0);
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 */
# Line 49 | Line 51 | static int
51   init_pabopto_inp(const int i, const char *fname)
52   {
53          FILE    *fp = fopen(fname, "r");
54 +        FVECT   dv;
55          char    buf[2048];
56          int     c;
57          
# Line 59 | Line 62 | init_pabopto_inp(const int i, const char *fname)
62          }
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")) {
72 <                        inpfile[i].isDSF = 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 <                        inpfile[i].isDSF = 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, "upphi %lf", &inpfile[i].up_phi) == 1)
96 +                        continue;
97                  if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
98                          continue;
99                  if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
# Line 96 | Line 114 | init_pabopto_inp(const int i, const char *fname)
114                  fputs(": unknown incident angle\n", stderr);
115                  return(0);
116          }
117 <        while (inpfile[i].phi < 0)      /* normalize phi direction */
118 <                inpfile[i].phi += 360.;
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  
# Line 105 | Line 129 | init_pabopto_inp(const int i, const char *fname)
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;
134 <        int     n, c;
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);
# Line 115 | Line 139 | add_pabopto_inp(const int i)
139                  return(0);
140          }
141                                          /* prepare input grid */
142 <        if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) {
143 <                if (i)                  /* need to process previous incidence */
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)=(%f,%f)\n",
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\n", &theta_out, &phi_out, &val) == 3)
158 <                add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
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);
# Line 141 | Line 176 | add_pabopto_inp(const int i)
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 173 | Line 209 | main(int argc, char *argv[])
209          for (i = 0; i < ninpfiles; i++)
210                  if (!init_pabopto_inp(i, argv[i+1]))
211                          return(1);
212 <        qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang);
212 >        qsort(inpfile, ninpfiles, sizeof(PGINPUT), cmp_indir);
213                                                  /* compile measurements */
214          for (i = 0; i < ninpfiles; i++)
215                  if (!add_pabopto_inp(i))
# Line 187 | 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