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.19 by greg, Wed Mar 19 19:48:57 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 > double          angle_eps = 0;  /* epsilon for angle comparisons */
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 +        static int      lastnew = -1;
118 +        FILE            *fp = fopen(inpfile[i].fname, "r");
119 +        double          theta_out, phi_out, val;
120 +        int             n, c;
121 +        
122 +        if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
123 +                fputs(inpfile[i].fname, stderr);
124 +                fputs(": cannot open\n", stderr);
125 +                return(0);
126 +        }
127                                          /* prepare input grid */
128 <        new_bsdf_data(new_theta, new_phi);
129 <                                        /* read actual data */
128 >        angle_eps = 180./2./GRIDRES;
129 >        if (lastnew < 0 || cmp_inang(&inpfile[lastnew], &inpfile[i])) {
130 >                if (lastnew >= 0)       /* need to process previous incidence */
131 >                        make_rbfrep();
132 > #ifdef DEBUG
133 >                fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n",
134 >                                        inpfile[i].theta, inpfile[i].phi);
135 > #endif
136 >                new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
137 >                lastnew = i;
138 >        }
139 > #ifdef DEBUG
140 >        fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
141 > #endif
142 >                                        /* read scattering data */
143          while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
144 <                add_bsdf_data(theta_out, phi_out, val, inp_is_DSF);
144 >                add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
145          n = 0;
146          while ((c = getc(fp)) != EOF)
147                  n += !isspace(c);
148          if (n)
149                  fprintf(stderr,
150                          "%s: warning: %d unexpected characters past EOD\n",
151 <                                fname, n);
151 >                                inpfile[i].fname, n);
152          fclose(fp);
153          return(1);
154   }
155  
156 + #ifndef TEST_MAIN
157   /* Read in PAB-Opto BSDF files and output RBF interpolant */
158   int
159   main(int argc, char *argv[])
# Line 102 | Line 176 | main(int argc, char *argv[])
176                  }
177                  argv += 2; argc -= 2;
178          }
179 <        if (argc < 3)
179 >                                                /* initialize & sort inputs */
180 >        ninpfiles = argc - 1;
181 >        if (ninpfiles < 2)
182                  goto userr;
183 <        for (i = 1; i < argc; i++) {            /* compile measurements */
184 <                if (!load_pabopto_meas(argv[i]))
183 >        inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles);
184 >        if (inpfile == NULL)
185 >                return(1);
186 >        for (i = 0; i < ninpfiles; i++)
187 >                if (!init_pabopto_inp(i, argv[i+1]))
188                          return(1);
189 <                make_rbfrep();
190 <        }
189 >        angle_eps = 0;
190 >        qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang);
191 >                                                /* compile measurements */
192 >        for (i = 0; i < ninpfiles; i++)
193 >                if (!add_pabopto_inp(i))
194 >                        return(1);
195 >        make_rbfrep();                          /* process last data set */
196          build_mesh();                           /* create interpolation */
197          save_bsdf_rep(stdout);                  /* write it out */
198          return(0);
# Line 117 | Line 201 | userr:
201                                          progname);
202          return(1);
203   }
204 + #else
205 + /* Test main produces a Radiance model from the given input file */
206 + int
207 + main(int argc, char *argv[])
208 + {
209 +        PGINPUT pginp;
210 +        char    buf[128];
211 +        FILE    *pfp;
212 +        double  bsdf, min_log;
213 +        FVECT   dir;
214 +        int     i, j, n;
215 +
216 +        if (argc != 2) {
217 +                fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]);
218 +                return(1);
219 +        }
220 +        ninpfiles = 1;
221 +        inpfile = &pginp;
222 +        if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
223 +                return(1);
224 +                                                /* reduce data set */
225 +        make_rbfrep();
226 + #ifdef DEBUG
227 +        fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
228 + #endif
229 +        min_log = log(bsdf_min*.5 + 1e-5);
230 + #if 1                                           /* produce spheres at meas. */
231 +        puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
232 +        n = 0;
233 +        for (i = 0; i < GRIDRES; i++)
234 +            for (j = 0; j < GRIDRES; j++)
235 +                if (dsf_grid[i][j].sum.n > 0) {
236 +                        ovec_from_pos(dir, i, j);
237 +                        bsdf = dsf_grid[i][j].sum.v /
238 +                                (dsf_grid[i][j].sum.n*output_orient*dir[2]);
239 +                        if (bsdf <= bsdf_min*.6)
240 +                                continue;
241 +                        bsdf = log(bsdf + 1e-5) - min_log;
242 +                        ovec_from_pos(dir, i, j);
243 +                        printf("yellow sphere s%04d\n0\n0\n", ++n);
244 +                        printf("4 %.6g %.6g %.6g %.6g\n\n",
245 +                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
246 +                                        .007*bsdf);
247 +                }
248 + #endif
249 + #if 1                                           /* spheres at RBF peaks */
250 +        puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
251 +        for (n = 0; n < dsf_list->nrbf; n++) {
252 +                RBFVAL  *rbf = &dsf_list->rbfa[n];
253 +                ovec_from_pos(dir, rbf->gx, rbf->gy);
254 +                bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
255 +                bsdf = log(bsdf + 1e-5) - min_log;
256 +                printf("red sphere p%04d\n0\n0\n", ++n);
257 +                printf("4 %.6g %.6g %.6g %.6g\n\n",
258 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
259 +                                .011*bsdf);
260 +        }
261 + #endif
262 + #if 1                                           /* output continuous surface */
263 +        puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
264 +        fflush(stdout);
265 +        sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
266 +        pfp = popen(buf, "w");
267 +        if (pfp == NULL) {
268 +                fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf);
269 +                return(1);
270 +        }
271 +        for (i = 0; i < GRIDRES; i++)
272 +            for (j = 0; j < GRIDRES; j++) {
273 +                ovec_from_pos(dir, i, j);
274 +                bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
275 +                bsdf = log(bsdf + 1e-5) - min_log;
276 +                fprintf(pfp, "%.8e %.8e %.8e\n",
277 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
278 +            }
279 +        if (pclose(pfp) != 0)
280 +                return(1);
281 + #endif
282 +        return(0);
283 + }
284 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines