ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2klems.c
(Generate patch)

Comparing ray/src/cv/bsdf2klems.c (file contents):
Revision 2.1 by greg, Sun Apr 21 23:01:14 2013 UTC vs.
Revision 2.10 by greg, Thu Sep 26 17:05:00 2013 UTC

# Line 17 | Line 17 | static const char RCSid[] = "$Id$";
17   #include "calcomp.h"
18   #include "bsdfrep.h"
19   #include "bsdf_m.h"
20 +                                /* assumed maximum # Klems patches */
21 + #define MAXPATCHES      145
22                                  /* global argv[0] */
23   char                    *progname;
24                                  /* selected basis function name */
25   static const char       *kbasis = "LBNL/Klems Full";
26                                  /* number of BSDF samples per patch */
27   static int              npsamps = 256;
28 +                                /* limit on number of RBF lobes */
29 + static int              lobe_lim = 15000;
30  
31   /* Return angle basis corresponding to the given name */
32   ANGLE_BASIS *
# Line 36 | Line 40 | get_basis(const char *bn)
40          return NULL;
41   }
42  
43 < /* Output XML prologue to stdout */
43 > /* Output XML header to stdout */
44   static void
45 < xml_prologue(int ac, char *av[])
45 > xml_header(int ac, char *av[])
46   {
43        ANGLE_BASIS     *abp = get_basis(kbasis);
44        int             i;
45
46        if (abp == NULL) {
47                fprintf(stderr, "%s: unknown angle basis '%s'\n", progname, kbasis);
48                exit(1);
49        }
47          puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
48          puts("<WindowElement xmlns=\"http://windows.lbl.gov\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://windows.lbl.gov/BSDF-v1.4.xsd\">");
49          fputs("<!-- File produced by:", stdout);
# Line 55 | Line 52 | xml_prologue(int ac, char *av[])
52                  fputs(*av++, stdout);
53          }
54          puts(" -->");
55 + }
56 +
57 + /* Output XML prologue to stdout */
58 + static void
59 + xml_prologue(const SDData *sd)
60 + {
61 +        const char      *matn = (sd && sd->matn[0]) ? sd->matn : "Name";
62 +        const char      *makr = (sd && sd->makr[0]) ? sd->makr : "Manufacturer";
63 +        ANGLE_BASIS     *abp = get_basis(kbasis);
64 +        int             i;
65 +
66 +        if (abp == NULL) {
67 +                fprintf(stderr, "%s: unknown angle basis '%s'\n", progname, kbasis);
68 +                exit(1);
69 +        }
70          puts("<WindowElementType>System</WindowElementType>");
71          puts("<FileType>BSDF</FileType>");
72          puts("<Optical>");
73          puts("<Layer>");
74          puts("\t<Material>");
75 <        puts("\t\t<Name>Name</Name>");
76 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
75 >        printf("\t\t<Name>%s</Name>\n", matn);
76 >        printf("\t\t<Manufacturer>%s</Manufacturer>\n", makr);
77 >        if (sd && sd->dim[2] > .001) {
78 >                printf("\t\t<Thickness unit=\"meter\">%.3f</Thickness>\n", sd->dim[2]);
79 >                printf("\t\t<Width unit=\"meter\">%.3f</Width>\n", sd->dim[0]);
80 >                printf("\t\t<Height unit=\"meter\">%.3f</Height>\n", sd->dim[1]);
81 >        }
82          puts("\t\t<DeviceType>Other</DeviceType>");
83          puts("\t</Material>");
84 +        if (sd && sd->mgf != NULL) {
85 +                puts("\t<Geometry format=\"MGF\">");
86 +                puts("\t\t<MGFblock unit=\"meter\">");
87 +                fputs(sd->mgf, stdout);
88 +                puts("</MGFblock>");
89 +                puts("\t</Geometry>");
90 +        }
91          puts("\t<DataDefinition>");
92          puts("\t\t<IncidentDataStructure>Columns</IncidentDataStructure>");
93          puts("\t\t<AngleBasis>");
# Line 73 | Line 97 | xml_prologue(int ac, char *av[])
97                  printf("\t\t\t<Theta>%g</Theta>\n", i ?
98                                  .5*(abp->lat[i].tmin + abp->lat[i+1].tmin) :
99                                  .0 );
100 <                printf("\t\t\t<nPhis>%d</nPhis>", abp->lat[i].nphis);
100 >                printf("\t\t\t<nPhis>%d</nPhis>\n", abp->lat[i].nphis);
101                  puts("\t\t\t<ThetaBounds>");
102                  printf("\t\t\t\t<LowerTheta>%g</LowerTheta>\n", abp->lat[i].tmin);
103                  printf("\t\t\t\t<UpperTheta>%g</UpperTheta>\n", abp->lat[i+1].tmin);
# Line 127 | Line 151 | xml_epilogue(void)
151          puts("</WindowElement>");
152   }
153  
154 < /* Load and resample XML BSDF description */
154 > /* Load and resample XML BSDF description using Klems basis */
155   static void
156   eval_bsdf(const char *fname)
157   {
# Line 142 | Line 166 | eval_bsdf(const char *fname)
166          SDclearBSDF(&bsd, fname);               /* load BSDF file */
167          if ((ec = SDloadFile(&bsd, fname)) != SDEnone)
168                  goto err;
169 +        xml_prologue(&bsd);                     /* pass geometry */
170                                                  /* front reflection */
171          if (bsd.rf != NULL || bsd.rLambFront.cieY > .002) {
172              input_orient = 1; output_orient = 1;
# Line 151 | Line 176 | eval_bsdf(const char *fname)
176                      sum = 0;                    /* average over patches */
177                      for (n = npsamps; n-- > 0; ) {
178                          fo_getvec(vout, j+(n+frandom())/npsamps, abp);
179 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
179 >                        fi_getvec(vin, i+urand(n), abp);
180                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
181                          if (ec != SDEnone)
182                                  goto err;
# Line 172 | Line 197 | eval_bsdf(const char *fname)
197                      sum = 0;                    /* average over patches */
198                      for (n = npsamps; n-- > 0; ) {
199                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
200 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
200 >                        bi_getvec(vin, i+urand(n), abp);
201                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
202                          if (ec != SDEnone)
203                                  goto err;
# Line 193 | Line 218 | eval_bsdf(const char *fname)
218                      sum = 0;                    /* average over patches */
219                      for (n = npsamps; n-- > 0; ) {
220                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
221 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
221 >                        fi_getvec(vin, i+urand(n), abp);
222                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
223                          if (ec != SDEnone)
224                                  goto err;
# Line 206 | Line 231 | eval_bsdf(const char *fname)
231              data_epilogue();
232          }
233                                                  /* back transmission */
234 <        if (bsd.tb != NULL) {
234 >        if ((bsd.tb != NULL) | (bsd.tf != NULL)) {
235              input_orient = -1; output_orient = 1;
236              data_prologue();
237              for (j = 0; j < abp->nangles; j++) {
238                  for (i = 0; i < abp->nangles; i++) {
239 <                    sum = 0;                    /* average over patches */
239 >                    sum = 0;            /* average over patches */
240                      for (n = npsamps; n-- > 0; ) {
241                          fo_getvec(vout, j+(n+frandom())/npsamps, abp);
242 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
242 >                        bi_getvec(vin, i+urand(n), abp);
243                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
244                          if (ec != SDEnone)
245                                  goto err;
# Line 233 | Line 258 | err:
258          exit(1);
259   }
260  
261 < /* Interpolate and output a BSDF function */
261 > /* Interpolate and output a BSDF function using Klems basis */
262   static void
263   eval_function(char *funame)
264   {
265          ANGLE_BASIS     *abp = get_basis(kbasis);
266 +        int             assignD = (fundefined(funame) < 6);
267          double          iovec[6];
268          double          sum;
269          int             i, j, n;
270  
271 +        initurand(npsamps);
272          data_prologue();                        /* begin output */
273          for (j = 0; j < abp->nangles; j++) {    /* run through directions */
274              for (i = 0; i < abp->nangles; i++) {
# Line 253 | Line 280 | eval_function(char *funame)
280                          bo_getvec(iovec+3, j+(n+frandom())/npsamps, abp);
281  
282                      if (input_orient > 0)
283 <                        fi_getvec(iovec, j+(n+frandom())/npsamps, abp);
283 >                        fi_getvec(iovec, i+urand(n), abp);
284                      else
285 <                        bi_getvec(iovec, j+(n+frandom())/npsamps, abp);
285 >                        bi_getvec(iovec, i+urand(n), abp);
286  
287 +                    if (assignD) {
288 +                        varset("Dx", '=', -iovec[3]);
289 +                        varset("Dy", '=', -iovec[4]);
290 +                        varset("Dz", '=', -iovec[5]);
291 +                        ++eclock;
292 +                    }
293                      sum += funvalue(funame, 6, iovec);
294                  }
295                  printf("\t%.3e\n", sum/npsamps);
# Line 271 | Line 304 | static void
304   eval_rbf(void)
305   {
306          ANGLE_BASIS     *abp = get_basis(kbasis);
307 <        double          iovec[6];
307 >        float           bsdfarr[MAXPATCHES*MAXPATCHES];
308 >        FVECT           vin, vout;
309 >        RBFNODE         *rbf;
310          double          sum;
311          int             i, j, n;
312 +                                                /* sanity check */
313 +        if (abp->nangles > MAXPATCHES) {
314 +                fprintf(stderr, "%s: too many patches!\n", progname);
315 +                exit(1);
316 +        }
317 +        data_prologue();                        /* begin output */
318 +        for (i = 0; i < abp->nangles; i++) {
319 +            if (input_orient > 0)               /* use incident patch center */
320 +                fi_getvec(vin, i+.5*(i>0), abp);
321 +            else
322 +                bi_getvec(vin, i+.5*(i>0), abp);
323  
324 < fprintf(stder, "%s: RBF evaluation currently unimplemented\n", progname);
325 < exit(1);
324 >            rbf = advect_rbf(vin, lobe_lim);    /* compute radial basis func */
325 >
326 >            for (j = 0; j < abp->nangles; j++) {
327 >                sum = 0;                        /* sample over exiting patch */
328 >                for (n = npsamps; n--; ) {
329 >                    if (output_orient > 0)
330 >                        fo_getvec(vout, j+(n+frandom())/npsamps, abp);
331 >                    else
332 >                        bo_getvec(vout, j+(n+frandom())/npsamps, abp);
333 >
334 >                    sum += eval_rbfrep(rbf, vout) / vout[2];
335 >                }
336 >                bsdfarr[j*abp->nangles + i] = sum*output_orient/npsamps;
337 >            }
338 >            if (rbf != NULL)
339 >                free(rbf);
340 >        }
341 >        n = 0;                                  /* write out our matrix */
342 >        for (j = 0; j < abp->nangles; j++) {
343 >            for (i = 0; i < abp->nangles; i++)
344 >                printf("\t%.3e\n", bsdfarr[n++]);
345 >            putchar('\n');
346 >        }
347 >        data_epilogue();                        /* finish output */
348   }
349  
350   /* Read in BSDF and interpolate as Klems matrix representation */
# Line 292 | Line 360 | main(int argc, char *argv[])
360          esupport &= ~(E_INCHAN|E_OUTCHAN);
361          scompile("PI:3.14159265358979323846", NULL, 0);
362          biggerlib();
363 <        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
363 >        for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
364                  switch (argv[i][1]) {           /* get options */
365                  case 'n':
366                          npsamps = atoi(argv[++i]);
# Line 319 | Line 387 | main(int argc, char *argv[])
387                  case 'q':
388                          kbasis = "LBNL/Klems Quarter";
389                          break;
390 +                case 'l':
391 +                        lobe_lim = atoi(argv[++i]);
392 +                        break;
393                  default:
394                          goto userr;
395                  }
# Line 327 | Line 398 | main(int argc, char *argv[])
398                          fprintf(stderr,
399          "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
400                                          progname);
401 +                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n",
402 +                                        progname);
403                          goto userr;
404                  }
405 <                xml_prologue(argc, argv);       /* start XML output */
405 >                ++eclock;
406 >                xml_header(argc, argv);                 /* start XML output */
407 >                xml_prologue(NULL);
408                  if (dofwd) {
409                          input_orient = -1;
410                          output_orient = -1;
# Line 347 | Line 422 | main(int argc, char *argv[])
422                  xml_epilogue();                 /* finish XML output & exit */
423                  return(0);
424          }
425 <        if (i == argc-1 && (cp = strstr(argv[i], ".xml")) != NULL &&
426 <                        strlen(cp) == 4) {      /* XML input? */
427 <                xml_prologue(argc, argv);       /* start XML output */
425 >                                                /* XML input? */
426 >        if (i == argc-1 && (cp = argv[i]+strlen(argv[i])-4) > argv[i] &&
427 >                                !strcasecmp(cp, ".xml")) {
428 >                xml_header(argc, argv);         /* start XML output */
429                  eval_bsdf(argv[i]);             /* load & resample BSDF */
430                  xml_epilogue();                 /* finish XML output & exit */
431                  return(0);
# Line 366 | Line 442 | main(int argc, char *argv[])
442                          if (!load_bsdf_rep(fpin))
443                                  return(1);
444                          fclose(fpin);
445 <                        if (!nbsdf++)           /* start XML on first dist. */
446 <                                xml_prologue(argc, argv);
445 >                        if (!nbsdf++) {         /* start XML on first dist. */
446 >                                xml_header(argc, argv);
447 >                                xml_prologue(NULL);
448 >                        }
449                          eval_rbf();
450                  }
451                  xml_epilogue();                 /* finish XML output & exit */
# Line 376 | Line 454 | main(int argc, char *argv[])
454          SET_FILE_BINARY(stdin);                 /* load from stdin */
455          if (!load_bsdf_rep(stdin))
456                  return(1);
457 <        xml_prologue(argc, argv);               /* start XML output */
457 >        xml_header(argc, argv);                 /* start XML output */
458 >        xml_prologue(NULL);
459          eval_rbf();                             /* resample dist. */
460          xml_epilogue();                         /* finish XML output & exit */
461          return(0);
462   userr:
463          fprintf(stderr,
464 <        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n",
386 <                                progname);
464 >        "Usage: %s [-n spp][-h|-q][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", progname);
465          fprintf(stderr,
466 <        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n",
389 <                                progname);
466 >        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n", progname);
467          fprintf(stderr,
468          "   or: %s [-n spp][-h|-q][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
469                                  progname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines