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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines