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.2 by greg, Sat Jun 29 22:35:15 2013 UTC vs.
Revision 2.21 by greg, Fri Mar 21 01:04:42 2014 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 */
25 >        int             igp[2];         /* input grid position */
26          int             isDSF;          /* data is DSF (rather than BSDF)? */
27          long            dstart;         /* data start offset in file */
28   } PGINPUT;
# Line 28 | Line 32 | int            ninpfiles;      /* number of input files */
32  
33   /* Compare incident angles */
34   static int
35 < cmp_inang(const void *p1, const void *p2)
35 > cmp_indir(const void *p1, const void *p2)
36   {
37          const PGINPUT   *inp1 = (const PGINPUT *)p1;
38          const PGINPUT   *inp2 = (const PGINPUT *)p2;
39 +        int             ydif = inp1->igp[1] - inp2->igp[1];
40          
41 <        if (inp1->theta > inp2->theta+FTINY)
42 <                return(1);
43 <        if (inp1->theta < inp2->theta-FTINY)
44 <                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);
41 >        if (ydif)
42 >                return(ydif);
43 >
44 >        return(inp1->igp[0] - inp2->igp[0]);
45   }
46  
47   /* Prepare a PAB-Opto input file by reading its header */
# Line 49 | Line 49 | static int
49   init_pabopto_inp(const int i, const char *fname)
50   {
51          FILE    *fp = fopen(fname, "r");
52 +        FVECT   dv;
53          char    buf[2048];
54          int     c;
55          
# Line 62 | Line 63 | init_pabopto_inp(const int i, const char *fname)
63          inpfile[i].theta = inpfile[i].phi = -10001.;
64                                  /* read header information */
65          while ((c = getc(fp)) == '#' || c == EOF) {
66 +                char    typ[32];
67                  if (fgets(buf, sizeof(buf), fp) == NULL) {
68                          fputs(fname, stderr);
69                          fputs(": unexpected EOF\n", stderr);
70                          fclose(fp);
71                          return(0);
72                  }
73 <                if (!strcmp(buf, "format: theta phi DSF\n")) {
72 <                        inpfile[i].isDSF = 1;
73 >                if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1)
74                          continue;
75 +                if (sscanf(buf, "format: theta phi %s", typ) == 1) {
76 +                        if (!strcasecmp(typ, "DSF")) {
77 +                                inpfile[i].isDSF = 1;
78 +                                continue;
79 +                        }
80 +                        if (!strcasecmp(typ, "BSDF")) {
81 +                                inpfile[i].isDSF = 0;
82 +                                continue;
83 +                        }
84                  }
75                if (!strcmp(buf, "format: theta phi BSDF\n")) {
76                        inpfile[i].isDSF = 0;
77                        continue;
78                }
85                  if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
86                          continue;
87                  if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
# Line 96 | Line 102 | init_pabopto_inp(const int i, const char *fname)
102                  fputs(": unknown incident angle\n", stderr);
103                  return(0);
104          }
105 +                                /* convert angle to grid position */
106 +        dv[2] = sin(M_PI/180.*inpfile[i].theta);
107 +        dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2];
108 +        dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2];
109 +        dv[2] = sqrt(1. - dv[2]*dv[2]);
110 +        pos_from_vec(inpfile[i].igp, dv);
111          return(1);
112   }
113  
# Line 103 | Line 115 | init_pabopto_inp(const int i, const char *fname)
115   static int
116   add_pabopto_inp(const int i)
117   {
118 <        FILE    *fp = fopen(inpfile[i].fname, "r");
119 <        double  theta_out, phi_out, val;
120 <        int     n, c;
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          }
115 #ifdef DEBUG
116        fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
117 #endif
127                                          /* prepare input grid */
128 <        if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) {
129 <                if (i)                  /* need to process previous incidence */
128 >        if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) {
129 >                if (i)                  /* process previous incidence */
130                          make_rbfrep();
131   #ifdef DEBUG
132                  fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n",
# Line 125 | Line 134 | add_pabopto_inp(const int i)
134   #endif
135                  new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
136          }
137 + #ifdef DEBUG
138 +        fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
139 + #endif
140                                          /* read scattering data */
141          while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
142                  add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
# Line 139 | Line 151 | add_pabopto_inp(const int i)
151          return(1);
152   }
153  
154 + #ifndef TEST_MAIN
155   /* Read in PAB-Opto BSDF files and output RBF interpolant */
156   int
157   main(int argc, char *argv[])
# Line 171 | Line 184 | main(int argc, char *argv[])
184          for (i = 0; i < ninpfiles; i++)
185                  if (!init_pabopto_inp(i, argv[i+1]))
186                          return(1);
187 <        qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang);
187 >        qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_indir);
188                                                  /* compile measurements */
189          for (i = 0; i < ninpfiles; i++)
190                  if (!add_pabopto_inp(i))
# Line 185 | Line 198 | userr:
198                                          progname);
199          return(1);
200   }
201 + #else
202 + /* Test main produces a Radiance model from the given input file */
203 + int
204 + main(int argc, char *argv[])
205 + {
206 +        PGINPUT pginp;
207 +        char    buf[128];
208 +        FILE    *pfp;
209 +        double  bsdf, min_log;
210 +        FVECT   dir;
211 +        int     i, j, n;
212 +
213 +        progname = argv[0];
214 +        if (argc != 2) {
215 +                fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname);
216 +                return(1);
217 +        }
218 +        ninpfiles = 1;
219 +        inpfile = &pginp;
220 +        if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
221 +                return(1);
222 +                                                /* reduce data set */
223 +        if (make_rbfrep() == NULL) {
224 +                fprintf(stderr, "%s: nothing to plot!\n", progname);
225 +                exit(1);
226 +        }
227 + #ifdef DEBUG
228 +        fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
229 + #endif
230 +        min_log = log(bsdf_min*.5 + 1e-5);
231 + #if 1                                           /* produce spheres at meas. */
232 +        puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
233 +        n = 0;
234 +        for (i = 0; i < GRIDRES; i++)
235 +            for (j = 0; j < GRIDRES; j++)
236 +                if (dsf_grid[i][j].sum.n > 0) {
237 +                        ovec_from_pos(dir, i, j);
238 +                        bsdf = dsf_grid[i][j].sum.v /
239 +                                (dsf_grid[i][j].sum.n*output_orient*dir[2]);
240 +                        if (bsdf <= bsdf_min*.6)
241 +                                continue;
242 +                        bsdf = log(bsdf + 1e-5) - min_log;
243 +                        ovec_from_pos(dir, i, j);
244 +                        printf("yellow sphere s%04d\n0\n0\n", ++n);
245 +                        printf("4 %.6g %.6g %.6g %.6g\n\n",
246 +                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
247 +                                        .007*bsdf);
248 +                }
249 + #endif
250 + #if 1                                           /* spheres at RBF peaks */
251 +        puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
252 +        for (n = 0; n < dsf_list->nrbf; n++) {
253 +                RBFVAL  *rbf = &dsf_list->rbfa[n];
254 +                ovec_from_pos(dir, rbf->gx, rbf->gy);
255 +                bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
256 +                bsdf = log(bsdf + 1e-5) - min_log;
257 +                printf("red sphere p%04d\n0\n0\n", ++n);
258 +                printf("4 %.6g %.6g %.6g %.6g\n\n",
259 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
260 +                                .011*bsdf);
261 +        }
262 + #endif
263 + #if 1                                           /* output continuous surface */
264 +        puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
265 +        fflush(stdout);
266 +        sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
267 +        pfp = popen(buf, "w");
268 +        if (pfp == NULL) {
269 +                fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
270 +                return(1);
271 +        }
272 +        for (i = 0; i < GRIDRES; i++)
273 +            for (j = 0; j < GRIDRES; j++) {
274 +                ovec_from_pos(dir, i, j);
275 +                bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
276 +                bsdf = log(bsdf + 1e-5) - min_log;
277 +                fprintf(pfp, "%.8e %.8e %.8e\n",
278 +                                dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
279 +            }
280 +        if (pclose(pfp) != 0)
281 +                return(1);
282 + #endif
283 +        return(0);
284 + }
285 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines