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.17 by greg, Tue Mar 18 19:08:24 2014 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <stdio.h>
11 + #include <stdlib.h>
12   #include <string.h>
13   #include <ctype.h>
14 + #include <math.h>
15   #include "platform.h"
16   #include "bsdfrep.h"
17 + #include "resolu.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;     /* incident angles (in degrees) */
24 >        int             isDSF;          /* data is DSF (rather than BSDF)? */
25 >        long            dstart;         /* data start offset in file */
26 > } PGINPUT;
27 >
28 > const double    ANGLE_EPS = 180./2./GRIDRES;
29 >
30 > PGINPUT         *inpfile;       /* input files sorted by incidence */
31 > int             ninpfiles;      /* number of input files */
32 >
33 > /* Compare incident angles */
34   static int
35 < load_pabopto_meas(const char *fname)
35 > cmp_inang(const void *p1, const void *p2)
36   {
37 +        const PGINPUT   *inp1 = (const PGINPUT *)p1;
38 +        const PGINPUT   *inp2 = (const PGINPUT *)p2;
39 +        
40 +        if (inp1->theta > inp2->theta+ANGLE_EPS)
41 +                return(1);
42 +        if (inp1->theta < inp2->theta-ANGLE_EPS)
43 +                return(-1);
44 +        if (inp1->phi > inp2->phi+ANGLE_EPS)
45 +                return(1);
46 +        if (inp1->phi < inp2->phi-ANGLE_EPS)
47 +                return(-1);
48 +        return(0);
49 + }
50 +
51 + /* Prepare a PAB-Opto input file by reading its header */
52 + static int
53 + init_pabopto_inp(const int i, const char *fname)
54 + {
55          FILE    *fp = fopen(fname, "r");
23        int     inp_is_DSF = -1;
24        double  new_theta, new_phi, theta_out, phi_out, val;
56          char    buf[2048];
57 <        int     n, c;
57 >        int     c;
58          
59          if (fp == NULL) {
60                  fputs(fname, stderr);
61                  fputs(": cannot open\n", stderr);
62                  return(0);
63          }
64 < #ifdef DEBUG
65 <        fprintf(stderr, "Loading measurement file '%s'...\n", fname);
66 < #endif
64 >        inpfile[i].fname = fname;
65 >        inpfile[i].isDSF = -1;
66 >        inpfile[i].theta = inpfile[i].phi = -10001.;
67                                  /* read header information */
68          while ((c = getc(fp)) == '#' || c == EOF) {
69 +                char    typ[32];
70                  if (fgets(buf, sizeof(buf), fp) == NULL) {
71                          fputs(fname, stderr);
72                          fputs(": unexpected EOF\n", stderr);
73                          fclose(fp);
74                          return(0);
75                  }
76 <                if (!strcmp(buf, "format: theta phi DSF\n")) {
45 <                        inp_is_DSF = 1;
76 >                if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1)
77                          continue;
78 +                if (sscanf(buf, "format: theta phi %s", typ) == 1) {
79 +                        if (!strcasecmp(typ, "DSF")) {
80 +                                inpfile[i].isDSF = 1;
81 +                                continue;
82 +                        }
83 +                        if (!strcasecmp(typ, "BSDF")) {
84 +                                inpfile[i].isDSF = 0;
85 +                                continue;
86 +                        }
87                  }
88 <                if (!strcmp(buf, "format: theta phi BSDF\n")) {
49 <                        inp_is_DSF = 0;
88 >                if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
89                          continue;
90 <                }
52 <                if (sscanf(buf, "intheta %lf", &new_theta) == 1)
90 >                if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
91                          continue;
54                if (sscanf(buf, "inphi %lf", &new_phi) == 1)
55                        continue;
92                  if (sscanf(buf, "incident_angle %lf %lf",
93 <                                &new_theta, &new_phi) == 2)
93 >                                &inpfile[i].theta, &inpfile[i].phi) == 2)
94                          continue;
95          }
96 <        ungetc(c, fp);
97 <        if (inp_is_DSF < 0) {
96 >        inpfile[i].dstart = ftell(fp) - 1;
97 >        fclose(fp);
98 >        if (inpfile[i].isDSF < 0) {
99                  fputs(fname, stderr);
100                  fputs(": unknown format\n", stderr);
64                fclose(fp);
101                  return(0);
102          }
103 +        if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
104 +                fputs(fname, stderr);
105 +                fputs(": unknown incident angle\n", stderr);
106 +                return(0);
107 +        }
108 +        while (inpfile[i].phi < 0)      /* normalize phi direction */
109 +                inpfile[i].phi += 360.;
110 +        return(1);
111 + }
112 +
113 + /* Load a set of measurements corresponding to a particular incident angle */
114 + static int
115 + add_pabopto_inp(const int i)
116 + {
117 +        FILE    *fp = fopen(inpfile[i].fname, "r");
118 +        double  theta_out, phi_out, val;
119 +        int     n, c;
120 +        
121 +        if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
122 +                fputs(inpfile[i].fname, stderr);
123 +                fputs(": cannot open\n", stderr);
124 +                return(0);
125 +        }
126                                          /* prepare input grid */
127 <        new_bsdf_data(new_theta, new_phi);
128 <                                        /* read actual data */
127 >        if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) {
128 >                if (i)                  /* need to process previous incidence */
129 >                        make_rbfrep();
130 > #ifdef DEBUG
131 >                fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n",
132 >                                        inpfile[i].theta, inpfile[i].phi);
133 > #endif
134 >                new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
135 >        }
136 > #ifdef DEBUG
137 >        fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
138 > #endif
139 >                                        /* read scattering data */
140          while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
141 <                add_bsdf_data(theta_out, phi_out, val, inp_is_DSF);
141 >                add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
142          n = 0;
143          while ((c = getc(fp)) != EOF)
144                  n += !isspace(c);
145          if (n)
146                  fprintf(stderr,
147                          "%s: warning: %d unexpected characters past EOD\n",
148 <                                fname, n);
148 >                                inpfile[i].fname, n);
149          fclose(fp);
150          return(1);
151   }
152  
153 + #ifndef TEST_MAIN
154   /* Read in PAB-Opto BSDF files and output RBF interpolant */
155   int
156   main(int argc, char *argv[])
# Line 102 | Line 173 | main(int argc, char *argv[])
173                  }
174                  argv += 2; argc -= 2;
175          }
176 <        if (argc < 3)
176 >                                                /* initialize & sort inputs */
177 >        ninpfiles = argc - 1;
178 >        if (ninpfiles < 2)
179                  goto userr;
180 <        for (i = 1; i < argc; i++) {            /* compile measurements */
181 <                if (!load_pabopto_meas(argv[i]))
180 >        inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles);
181 >        if (inpfile == NULL)
182 >                return(1);
183 >        for (i = 0; i < ninpfiles; i++)
184 >                if (!init_pabopto_inp(i, argv[i+1]))
185                          return(1);
186 <                make_rbfrep();
187 <        }
186 >        qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang);
187 >                                                /* compile measurements */
188 >        for (i = 0; i < ninpfiles; i++)
189 >                if (!add_pabopto_inp(i))
190 >                        return(1);
191 >        make_rbfrep();                          /* process last data set */
192          build_mesh();                           /* create interpolation */
193          save_bsdf_rep(stdout);                  /* write it out */
194          return(0);
# Line 117 | Line 197 | userr:
197                                          progname);
198          return(1);
199   }
200 + #else
201 + /* Test main produces a Radiance model from the given input file */
202 + int
203 + main(int argc, char *argv[])
204 + {
205 +        PGINPUT pginp;
206 +        char    buf[128];
207 +        FILE    *pfp;
208 +        double  bsdf, min_log;
209 +        FVECT   dir;
210 +        int     i, j, n;
211 +
212 +        if (argc != 2) {
213 +                fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]);
214 +                return(1);
215 +        }
216 +        ninpfiles = 1;
217 +        inpfile = &pginp;
218 +        if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
219 +                return(1);
220 +                                                /* reduce data set */
221 +        make_rbfrep();
222 + #ifdef DEBUG
223 +        fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
224 + #endif
225 +        min_log = log(bsdf_min*.5 + 1e-5);
226 + #if 1                                           /* produce spheres at meas. */
227 +        puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
228 +        n = 0;
229 +        for (i = 0; i < GRIDRES; i++)
230 +            for (j = 0; j < GRIDRES; j++)
231 +                if (dsf_grid[i][j].sum.n > 0) {
232 +                        ovec_from_pos(dir, i, j);
233 +                        bsdf = dsf_grid[i][j].sum.v /
234 +                                (dsf_grid[i][j].sum.n*output_orient*dir[2]);
235 +                        if (bsdf <= bsdf_min*.6)
236 +                                continue;
237 +                        bsdf = log(bsdf + 1e-5) - min_log;
238 +                        ovec_from_pos(dir, i, j);
239 +                        printf("yellow sphere s%04d\n0\n0\n", ++n);
240 +                        printf("4 %.6g %.6g %.6g %.6g\n\n",
241 +                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
242 +                                        .007*bsdf);
243 +                }
244 + #endif
245 + #if 1                                           /* spheres at RBF peaks */
246 +        puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
247 +        for (n = 0; n < dsf_list->nrbf; n++) {
248 +                RBFVAL  *rbf = &dsf_list->rbfa[n];
249 +                ovec_from_pos(dir, rbf->gx, rbf->gy);
250 +                bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
251 +                bsdf = log(bsdf + 1e-5) - min_log;
252 +                printf("red sphere p%04d\n0\n0\n", ++n);
253 +                printf("4 %.6g %.6g %.6g %.6g\n\n",
254 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
255 +                                .011*bsdf);
256 +        }
257 + #endif
258 + #if 1                                           /* output continuous surface */
259 +        puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
260 +        fflush(stdout);
261 +        sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
262 +        pfp = popen(buf, "w");
263 +        if (pfp == NULL) {
264 +                fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf);
265 +                return(1);
266 +        }
267 +        for (i = 0; i < GRIDRES; i++)
268 +            for (j = 0; j < GRIDRES; j++) {
269 +                ovec_from_pos(dir, i, j);
270 +                bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
271 +                bsdf = log(bsdf + 1e-5) - min_log;
272 +                fprintf(pfp, "%.8e %.8e %.8e\n",
273 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
274 +            }
275 +        if (pclose(pfp) != 0)
276 +                return(1);
277 + #endif
278 +        return(0);
279 + }
280 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines