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.2 by greg, Tue Apr 23 04:40:23 2013 UTC vs.
Revision 2.17 by greg, Tue May 5 22:16:49 2015 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 +                                /* progress bar length */
31 + static int              do_prog = 79;
32  
33 +
34 + /* Start new progress bar */
35 + #define prog_start(s)   if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else
36 +
37 + /* Draw progress bar of the appropriate length */
38 + static void
39 + prog_show(double frac)
40 + {
41 +        static unsigned call_cnt = 0;
42 +        static char     lastc[] = "-\\|/";
43 +        char            pbar[256];
44 +        int             nchars;
45 +
46 +        if (do_prog <= 1) return;
47 +        if (do_prog > sizeof(pbar)-2)
48 +                do_prog = sizeof(pbar)-2;
49 +        if (frac < 0) frac = 0;
50 +        else if (frac >= 1) frac = .9999;
51 +        nchars = do_prog*frac;
52 +        pbar[0] = '\r';
53 +        memset(pbar+1, '*', nchars);
54 +        pbar[nchars+1] = lastc[call_cnt++ & 3];
55 +        memset(pbar+2+nchars, '-', do_prog-nchars-1);
56 +        pbar[do_prog+1] = '\0';
57 +        fputs(pbar, stderr);
58 + }
59 +
60 + /* Finish progress bar */
61 + static void
62 + prog_done(void)
63 + {
64 +        int     n = do_prog;
65 +
66 +        if (n <= 1) return;
67 +        fputc('\r', stderr);
68 +        while (n--)
69 +                fputc(' ', stderr);
70 +        fputc('\r', stderr);
71 + }
72 +
73   /* Return angle basis corresponding to the given name */
74 < ANGLE_BASIS *
74 > static ANGLE_BASIS *
75   get_basis(const char *bn)
76   {
77          int     n = nabases;
# Line 36 | Line 82 | get_basis(const char *bn)
82          return NULL;
83   }
84  
85 < /* Output XML prologue to stdout */
85 > /* Output XML header to stdout */
86   static void
87 < xml_prologue(int ac, char *av[])
87 > xml_header(int ac, char *av[])
88   {
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        }
89          puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
90          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\">");
91          fputs("<!-- File produced by:", stdout);
# Line 55 | Line 94 | xml_prologue(int ac, char *av[])
94                  fputs(*av++, stdout);
95          }
96          puts(" -->");
97 + }
98 +
99 + /* Output XML prologue to stdout */
100 + static void
101 + xml_prologue(const SDData *sd)
102 + {
103 +        const char      *matn = (sd && sd->matn[0]) ? sd->matn :
104 +                                bsdf_name[0] ? bsdf_name : "Unknown";
105 +        const char      *makr = (sd && sd->makr[0]) ? sd->makr :
106 +                                bsdf_manuf[0] ? bsdf_manuf : "Unknown";
107 +        ANGLE_BASIS     *abp = get_basis(kbasis);
108 +        int             i;
109 +
110 +        if (abp == NULL) {
111 +                fprintf(stderr, "%s: unknown angle basis '%s'\n", progname, kbasis);
112 +                exit(1);
113 +        }
114          puts("<WindowElementType>System</WindowElementType>");
115          puts("<FileType>BSDF</FileType>");
116          puts("<Optical>");
117          puts("<Layer>");
118          puts("\t<Material>");
119 <        puts("\t\t<Name>Name</Name>");
120 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
119 >        printf("\t\t<Name>%s</Name>\n", matn);
120 >        printf("\t\t<Manufacturer>%s</Manufacturer>\n", makr);
121 >        if (sd && sd->dim[2] > .001) {
122 >                printf("\t\t<Thickness unit=\"meter\">%.3f</Thickness>\n", sd->dim[2]);
123 >                printf("\t\t<Width unit=\"meter\">%.3f</Width>\n", sd->dim[0]);
124 >                printf("\t\t<Height unit=\"meter\">%.3f</Height>\n", sd->dim[1]);
125 >        }
126          puts("\t\t<DeviceType>Other</DeviceType>");
127          puts("\t</Material>");
128 +        if (sd && sd->mgf != NULL) {
129 +                puts("\t<Geometry format=\"MGF\">");
130 +                puts("\t\t<MGFblock unit=\"meter\">");
131 +                fputs(sd->mgf, stdout);
132 +                puts("</MGFblock>");
133 +                puts("\t</Geometry>");
134 +        }
135          puts("\t<DataDefinition>");
136          puts("\t\t<IncidentDataStructure>Columns</IncidentDataStructure>");
137          puts("\t\t<AngleBasis>");
# Line 73 | Line 141 | xml_prologue(int ac, char *av[])
141                  printf("\t\t\t<Theta>%g</Theta>\n", i ?
142                                  .5*(abp->lat[i].tmin + abp->lat[i+1].tmin) :
143                                  .0 );
144 <                printf("\t\t\t<nPhis>%d</nPhis>", abp->lat[i].nphis);
144 >                printf("\t\t\t<nPhis>%d</nPhis>\n", abp->lat[i].nphis);
145                  puts("\t\t\t<ThetaBounds>");
146                  printf("\t\t\t\t<LowerTheta>%g</LowerTheta>\n", abp->lat[i].tmin);
147                  printf("\t\t\t\t<UpperTheta>%g</UpperTheta>\n", abp->lat[i+1].tmin);
# Line 142 | Line 210 | eval_bsdf(const char *fname)
210          SDclearBSDF(&bsd, fname);               /* load BSDF file */
211          if ((ec = SDloadFile(&bsd, fname)) != SDEnone)
212                  goto err;
213 +        xml_prologue(&bsd);                     /* pass geometry */
214                                                  /* front reflection */
215          if (bsd.rf != NULL || bsd.rLambFront.cieY > .002) {
216              input_orient = 1; output_orient = 1;
# Line 151 | Line 220 | eval_bsdf(const char *fname)
220                      sum = 0;                    /* average over patches */
221                      for (n = npsamps; n-- > 0; ) {
222                          fo_getvec(vout, j+(n+frandom())/npsamps, abp);
223 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
223 >                        fi_getvec(vin, i+urand(n), abp);
224                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
225                          if (ec != SDEnone)
226                                  goto err;
# Line 172 | Line 241 | eval_bsdf(const char *fname)
241                      sum = 0;                    /* average over patches */
242                      for (n = npsamps; n-- > 0; ) {
243                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
244 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
244 >                        bi_getvec(vin, i+urand(n), abp);
245                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
246                          if (ec != SDEnone)
247                                  goto err;
# Line 193 | Line 262 | eval_bsdf(const char *fname)
262                      sum = 0;                    /* average over patches */
263                      for (n = npsamps; n-- > 0; ) {
264                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
265 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
265 >                        fi_getvec(vin, i+urand(n), abp);
266                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
267                          if (ec != SDEnone)
268                                  goto err;
# Line 206 | Line 275 | eval_bsdf(const char *fname)
275              data_epilogue();
276          }
277                                                  /* back transmission */
278 <        if (bsd.tb != NULL) {
278 >        if ((bsd.tb != NULL) | (bsd.tf != NULL)) {
279              input_orient = -1; output_orient = 1;
280              data_prologue();
281              for (j = 0; j < abp->nangles; j++) {
282                  for (i = 0; i < abp->nangles; i++) {
283 <                    sum = 0;                    /* average over patches */
283 >                    sum = 0;            /* average over patches */
284                      for (n = npsamps; n-- > 0; ) {
285                          fo_getvec(vout, j+(n+frandom())/npsamps, abp);
286 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
286 >                        bi_getvec(vin, i+urand(n), abp);
287                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
288                          if (ec != SDEnone)
289                                  goto err;
# Line 238 | Line 307 | static void
307   eval_function(char *funame)
308   {
309          ANGLE_BASIS     *abp = get_basis(kbasis);
310 +        int             assignD = (fundefined(funame) < 6);
311          double          iovec[6];
312          double          sum;
313          int             i, j, n;
314  
315 +        initurand(npsamps);
316          data_prologue();                        /* begin output */
317          for (j = 0; j < abp->nangles; j++) {    /* run through directions */
318              for (i = 0; i < abp->nangles; i++) {
# Line 253 | Line 324 | eval_function(char *funame)
324                          bo_getvec(iovec+3, j+(n+frandom())/npsamps, abp);
325  
326                      if (input_orient > 0)
327 <                        fi_getvec(iovec, i+(n+frandom())/npsamps, abp);
327 >                        fi_getvec(iovec, i+urand(n), abp);
328                      else
329 <                        bi_getvec(iovec, i+(n+frandom())/npsamps, abp);
329 >                        bi_getvec(iovec, i+urand(n), abp);
330  
331 +                    if (assignD) {
332 +                        varset("Dx", '=', -iovec[3]);
333 +                        varset("Dy", '=', -iovec[4]);
334 +                        varset("Dz", '=', -iovec[5]);
335 +                        ++eclock;
336 +                    }
337                      sum += funvalue(funame, 6, iovec);
338                  }
339                  printf("\t%.3e\n", sum/npsamps);
340              }
341              putchar('\n');
342 +            prog_show((j+1.)/abp->nangles);
343          }
344          data_epilogue();                        /* finish output */
345 +        prog_done();
346   }
347  
348   /* Interpolate and output a radial basis function BSDF representation */
349   static void
350   eval_rbf(void)
351   {
273 #define MAXPATCHES      145
352          ANGLE_BASIS     *abp = get_basis(kbasis);
353          float           bsdfarr[MAXPATCHES*MAXPATCHES];
354          FVECT           vin, vout;
# Line 289 | Line 367 | eval_rbf(void)
367              else
368                  bi_getvec(vin, i+.5*(i>0), abp);
369  
370 <            rbf = advect_rbf(vin);              /* compute radial basis func */
370 >            rbf = advect_rbf(vin, lobe_lim);    /* compute radial basis func */
371  
372              for (j = 0; j < abp->nangles; j++) {
373                  sum = 0;                        /* sample over exiting patch */
# Line 299 | Line 377 | eval_rbf(void)
377                      else
378                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
379  
380 <                    sum += eval_rbfrep(rbf, vout) / vout[2];
380 >                    sum += eval_rbfrep(rbf, vout);
381                  }
382 <                bsdfarr[j*abp->nangles + i] = sum*output_orient/npsamps;
382 >                bsdfarr[j*abp->nangles + i] = sum / (double)npsamps;
383              }
384 +            if (rbf != NULL)
385 +                free(rbf);
386 +            prog_show((i+1.)/abp->nangles);
387          }
388          n = 0;                                  /* write out our matrix */
389          for (j = 0; j < abp->nangles; j++) {
# Line 311 | Line 392 | eval_rbf(void)
392              putchar('\n');
393          }
394          data_epilogue();                        /* finish output */
395 < #undef MAXPATCHES
395 >        prog_done();
396   }
397  
398   /* Read in BSDF and interpolate as Klems matrix representation */
# Line 354 | Line 435 | main(int argc, char *argv[])
435                  case 'q':
436                          kbasis = "LBNL/Klems Quarter";
437                          break;
438 +                case 'l':
439 +                        lobe_lim = atoi(argv[++i]);
440 +                        break;
441 +                case 'p':
442 +                        do_prog = atoi(argv[i]+2);
443 +                        break;
444                  default:
445                          goto userr;
446                  }
# Line 362 | Line 449 | main(int argc, char *argv[])
449                          fprintf(stderr,
450          "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
451                                          progname);
452 +                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
453                          goto userr;
454                  }
455 <                xml_prologue(argc, argv);       /* start XML output */
455 >                ++eclock;
456 >                xml_header(argc, argv);                 /* start XML output */
457 >                xml_prologue(NULL);
458                  if (dofwd) {
459                          input_orient = -1;
460                          output_orient = -1;
461 <                        eval_function(argv[i]);         /* outside reflectance */
461 >                        prog_start("Evaluating outside reflectance");
462 >                        eval_function(argv[i]);
463                          output_orient = 1;
464 <                        eval_function(argv[i]);         /* outside -> inside */
464 >                        prog_start("Evaluating outside->inside transmission");
465 >                        eval_function(argv[i]);
466                  }
467                  if (dobwd) {
468                          input_orient = 1;
469                          output_orient = 1;
470 <                        eval_function(argv[i]);         /* inside reflectance */
470 >                        prog_start("Evaluating inside reflectance");
471 >                        eval_function(argv[i]);
472                          output_orient = -1;
473 <                        eval_function(argv[i]);         /* inside -> outside */
473 >                        prog_start("Evaluating inside->outside transmission");
474 >                        eval_function(argv[i]);
475                  }
476                  xml_epilogue();                 /* finish XML output & exit */
477                  return(0);
# Line 385 | Line 479 | main(int argc, char *argv[])
479                                                  /* XML input? */
480          if (i == argc-1 && (cp = argv[i]+strlen(argv[i])-4) > argv[i] &&
481                                  !strcasecmp(cp, ".xml")) {
482 <                xml_prologue(argc, argv);       /* start XML output */
482 >                xml_header(argc, argv);         /* start XML output */
483                  eval_bsdf(argv[i]);             /* load & resample BSDF */
484                  xml_epilogue();                 /* finish XML output & exit */
485                  return(0);
# Line 393 | Line 487 | main(int argc, char *argv[])
487          if (i < argc) {                         /* open input files if given */
488                  int     nbsdf = 0;
489                  for ( ; i < argc; i++) {        /* interpolate each component */
490 +                        char    pbuf[256];
491                          FILE    *fpin = fopen(argv[i], "rb");
492                          if (fpin == NULL) {
493                                  fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
# Line 402 | Line 497 | main(int argc, char *argv[])
497                          if (!load_bsdf_rep(fpin))
498                                  return(1);
499                          fclose(fpin);
500 <                        if (!nbsdf++)           /* start XML on first dist. */
501 <                                xml_prologue(argc, argv);
500 >                        if (!nbsdf++) {         /* start XML on first dist. */
501 >                                xml_header(argc, argv);
502 >                                xml_prologue(NULL);
503 >                        }
504 >                        sprintf(pbuf, "Interpolating component '%s'", argv[i]);
505 >                        prog_start(pbuf);
506                          eval_rbf();
507                  }
508                  xml_epilogue();                 /* finish XML output & exit */
# Line 412 | Line 511 | main(int argc, char *argv[])
511          SET_FILE_BINARY(stdin);                 /* load from stdin */
512          if (!load_bsdf_rep(stdin))
513                  return(1);
514 <        xml_prologue(argc, argv);               /* start XML output */
514 >        xml_header(argc, argv);                 /* start XML output */
515 >        xml_prologue(NULL);
516 >        prog_start("Interpolating from standard input");
517          eval_rbf();                             /* resample dist. */
518          xml_epilogue();                         /* finish XML output & exit */
519          return(0);
520   userr:
521          fprintf(stderr,
522 <        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n",
422 <                                progname);
522 >        "Usage: %s [-n spp][-h|-q][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", progname);
523          fprintf(stderr,
524 <        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n",
425 <                                progname);
524 >        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n", progname);
525          fprintf(stderr,
526          "   or: %s [-n spp][-h|-q][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
527                                  progname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines