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.9 by greg, Sun Aug 11 14:32:34 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 */
# Line 36 | Line 38 | get_basis(const char *bn)
38          return NULL;
39   }
40  
41 < /* Output XML prologue to stdout */
41 > /* Output XML header to stdout */
42   static void
43 < xml_prologue(int ac, char *av[])
43 > xml_header(int ac, char *av[])
44   {
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        }
45          puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
46          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\">");
47          fputs("<!-- File produced by:", stdout);
# Line 55 | Line 50 | xml_prologue(int ac, char *av[])
50                  fputs(*av++, stdout);
51          }
52          puts(" -->");
53 + }
54 +
55 + /* Output XML prologue to stdout */
56 + static void
57 + xml_prologue(const SDData *sd)
58 + {
59 +        const char      *matn = (sd && sd->matn[0]) ? sd->matn : "Name";
60 +        const char      *makr = (sd && sd->makr[0]) ? sd->makr : "Manufacturer";
61 +        ANGLE_BASIS     *abp = get_basis(kbasis);
62 +        int             i;
63 +
64 +        if (abp == NULL) {
65 +                fprintf(stderr, "%s: unknown angle basis '%s'\n", progname, kbasis);
66 +                exit(1);
67 +        }
68          puts("<WindowElementType>System</WindowElementType>");
69          puts("<FileType>BSDF</FileType>");
70          puts("<Optical>");
71          puts("<Layer>");
72          puts("\t<Material>");
73 <        puts("\t\t<Name>Name</Name>");
74 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
73 >        printf("\t\t<Name>%s</Name>\n", matn);
74 >        printf("\t\t<Manufacturer>%s</Manufacturer>\n", makr);
75 >        if (sd && sd->dim[2] > .001) {
76 >                printf("\t\t<Thickness unit=\"meter\">%.3f</Thickness>\n", sd->dim[2]);
77 >                printf("\t\t<Width unit=\"meter\">%.3f</Width>\n", sd->dim[0]);
78 >                printf("\t\t<Height unit=\"meter\">%.3f</Height>\n", sd->dim[1]);
79 >        }
80          puts("\t\t<DeviceType>Other</DeviceType>");
81          puts("\t</Material>");
82 +        if (sd && sd->mgf != NULL) {
83 +                puts("\t<Geometry format=\"MGF\">");
84 +                puts("\t\t<MGFblock unit=\"meter\">");
85 +                fputs(sd->mgf, stdout);
86 +                puts("</MGFblock>");
87 +                puts("\t</Geometry>");
88 +        }
89          puts("\t<DataDefinition>");
90          puts("\t\t<IncidentDataStructure>Columns</IncidentDataStructure>");
91          puts("\t\t<AngleBasis>");
# Line 73 | Line 95 | xml_prologue(int ac, char *av[])
95                  printf("\t\t\t<Theta>%g</Theta>\n", i ?
96                                  .5*(abp->lat[i].tmin + abp->lat[i+1].tmin) :
97                                  .0 );
98 <                printf("\t\t\t<nPhis>%d</nPhis>", abp->lat[i].nphis);
98 >                printf("\t\t\t<nPhis>%d</nPhis>\n", abp->lat[i].nphis);
99                  puts("\t\t\t<ThetaBounds>");
100                  printf("\t\t\t\t<LowerTheta>%g</LowerTheta>\n", abp->lat[i].tmin);
101                  printf("\t\t\t\t<UpperTheta>%g</UpperTheta>\n", abp->lat[i+1].tmin);
# Line 127 | Line 149 | xml_epilogue(void)
149          puts("</WindowElement>");
150   }
151  
152 < /* Load and resample XML BSDF description */
152 > /* Load and resample XML BSDF description using Klems basis */
153   static void
154   eval_bsdf(const char *fname)
155   {
# Line 142 | Line 164 | eval_bsdf(const char *fname)
164          SDclearBSDF(&bsd, fname);               /* load BSDF file */
165          if ((ec = SDloadFile(&bsd, fname)) != SDEnone)
166                  goto err;
167 +        xml_prologue(&bsd);                     /* pass geometry */
168                                                  /* front reflection */
169          if (bsd.rf != NULL || bsd.rLambFront.cieY > .002) {
170              input_orient = 1; output_orient = 1;
# Line 151 | Line 174 | eval_bsdf(const char *fname)
174                      sum = 0;                    /* average over patches */
175                      for (n = npsamps; n-- > 0; ) {
176                          fo_getvec(vout, j+(n+frandom())/npsamps, abp);
177 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
177 >                        fi_getvec(vin, i+urand(n), abp);
178                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
179                          if (ec != SDEnone)
180                                  goto err;
# Line 172 | Line 195 | eval_bsdf(const char *fname)
195                      sum = 0;                    /* average over patches */
196                      for (n = npsamps; n-- > 0; ) {
197                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
198 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
198 >                        bi_getvec(vin, i+urand(n), abp);
199                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
200                          if (ec != SDEnone)
201                                  goto err;
# Line 193 | Line 216 | eval_bsdf(const char *fname)
216                      sum = 0;                    /* average over patches */
217                      for (n = npsamps; n-- > 0; ) {
218                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
219 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
219 >                        fi_getvec(vin, i+urand(n), abp);
220                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
221                          if (ec != SDEnone)
222                                  goto err;
# Line 206 | Line 229 | eval_bsdf(const char *fname)
229              data_epilogue();
230          }
231                                                  /* back transmission */
232 <        if (bsd.tb != NULL) {
232 >        if ((bsd.tb != NULL) | (bsd.tf != NULL)) {
233              input_orient = -1; output_orient = 1;
234              data_prologue();
235              for (j = 0; j < abp->nangles; j++) {
236                  for (i = 0; i < abp->nangles; i++) {
237 <                    sum = 0;                    /* average over patches */
237 >                    sum = 0;            /* average over patches */
238                      for (n = npsamps; n-- > 0; ) {
239                          fo_getvec(vout, j+(n+frandom())/npsamps, abp);
240 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
240 >                        bi_getvec(vin, i+urand(n), abp);
241                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
242                          if (ec != SDEnone)
243                                  goto err;
# Line 233 | Line 256 | err:
256          exit(1);
257   }
258  
259 < /* Interpolate and output a BSDF function */
259 > /* Interpolate and output a BSDF function using Klems basis */
260   static void
261   eval_function(char *funame)
262   {
263          ANGLE_BASIS     *abp = get_basis(kbasis);
264 +        int             assignD = (fundefined(funame) < 6);
265          double          iovec[6];
266          double          sum;
267          int             i, j, n;
268  
269 +        initurand(npsamps);
270          data_prologue();                        /* begin output */
271          for (j = 0; j < abp->nangles; j++) {    /* run through directions */
272              for (i = 0; i < abp->nangles; i++) {
# Line 253 | Line 278 | eval_function(char *funame)
278                          bo_getvec(iovec+3, j+(n+frandom())/npsamps, abp);
279  
280                      if (input_orient > 0)
281 <                        fi_getvec(iovec, j+(n+frandom())/npsamps, abp);
281 >                        fi_getvec(iovec, i+urand(n), abp);
282                      else
283 <                        bi_getvec(iovec, j+(n+frandom())/npsamps, abp);
283 >                        bi_getvec(iovec, i+urand(n), abp);
284  
285 +                    if (assignD) {
286 +                        varset("Dx", '=', -iovec[3]);
287 +                        varset("Dy", '=', -iovec[4]);
288 +                        varset("Dz", '=', -iovec[5]);
289 +                        ++eclock;
290 +                    }
291                      sum += funvalue(funame, 6, iovec);
292                  }
293                  printf("\t%.3e\n", sum/npsamps);
# Line 271 | Line 302 | static void
302   eval_rbf(void)
303   {
304          ANGLE_BASIS     *abp = get_basis(kbasis);
305 <        double          iovec[6];
305 >        float           bsdfarr[MAXPATCHES*MAXPATCHES];
306 >        FVECT           vin, vout;
307 >        RBFNODE         *rbf;
308          double          sum;
309          int             i, j, n;
310 +                                                /* sanity check */
311 +        if (abp->nangles > MAXPATCHES) {
312 +                fprintf(stderr, "%s: too many patches!\n", progname);
313 +                exit(1);
314 +        }
315 +        data_prologue();                        /* begin output */
316 +        for (i = 0; i < abp->nangles; i++) {
317 +            if (input_orient > 0)               /* use incident patch center */
318 +                fi_getvec(vin, i+.5*(i>0), abp);
319 +            else
320 +                bi_getvec(vin, i+.5*(i>0), abp);
321  
322 < fprintf(stder, "%s: RBF evaluation currently unimplemented\n", progname);
323 < exit(1);
322 >            rbf = advect_rbf(vin);              /* compute radial basis func */
323 >
324 >            for (j = 0; j < abp->nangles; j++) {
325 >                sum = 0;                        /* sample over exiting patch */
326 >                for (n = npsamps; n--; ) {
327 >                    if (output_orient > 0)
328 >                        fo_getvec(vout, j+(n+frandom())/npsamps, abp);
329 >                    else
330 >                        bo_getvec(vout, j+(n+frandom())/npsamps, abp);
331 >
332 >                    sum += eval_rbfrep(rbf, vout) / vout[2];
333 >                }
334 >                bsdfarr[j*abp->nangles + i] = sum*output_orient/npsamps;
335 >            }
336 >            if (rbf != NULL)
337 >                free(rbf);
338 >        }
339 >        n = 0;                                  /* write out our matrix */
340 >        for (j = 0; j < abp->nangles; j++) {
341 >            for (i = 0; i < abp->nangles; i++)
342 >                printf("\t%.3e\n", bsdfarr[n++]);
343 >            putchar('\n');
344 >        }
345 >        data_epilogue();                        /* finish output */
346   }
347  
348   /* Read in BSDF and interpolate as Klems matrix representation */
# Line 292 | Line 358 | main(int argc, char *argv[])
358          esupport &= ~(E_INCHAN|E_OUTCHAN);
359          scompile("PI:3.14159265358979323846", NULL, 0);
360          biggerlib();
361 <        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
361 >        for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
362                  switch (argv[i][1]) {           /* get options */
363                  case 'n':
364                          npsamps = atoi(argv[++i]);
# Line 327 | Line 393 | main(int argc, char *argv[])
393                          fprintf(stderr,
394          "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
395                                          progname);
396 +                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n",
397 +                                        progname);
398                          goto userr;
399                  }
400 <                xml_prologue(argc, argv);       /* start XML output */
400 >                ++eclock;
401 >                xml_header(argc, argv);                 /* start XML output */
402 >                xml_prologue(NULL);
403                  if (dofwd) {
404                          input_orient = -1;
405                          output_orient = -1;
# Line 347 | Line 417 | main(int argc, char *argv[])
417                  xml_epilogue();                 /* finish XML output & exit */
418                  return(0);
419          }
420 <        if (i == argc-1 && (cp = strstr(argv[i], ".xml")) != NULL &&
421 <                        strlen(cp) == 4) {      /* XML input? */
422 <                xml_prologue(argc, argv);       /* start XML output */
420 >                                                /* XML input? */
421 >        if (i == argc-1 && (cp = argv[i]+strlen(argv[i])-4) > argv[i] &&
422 >                                !strcasecmp(cp, ".xml")) {
423 >                xml_header(argc, argv);         /* start XML output */
424                  eval_bsdf(argv[i]);             /* load & resample BSDF */
425                  xml_epilogue();                 /* finish XML output & exit */
426                  return(0);
# Line 366 | Line 437 | main(int argc, char *argv[])
437                          if (!load_bsdf_rep(fpin))
438                                  return(1);
439                          fclose(fpin);
440 <                        if (!nbsdf++)           /* start XML on first dist. */
441 <                                xml_prologue(argc, argv);
440 >                        if (!nbsdf++) {         /* start XML on first dist. */
441 >                                xml_header(argc, argv);
442 >                                xml_prologue(NULL);
443 >                        }
444                          eval_rbf();
445                  }
446                  xml_epilogue();                 /* finish XML output & exit */
# Line 376 | Line 449 | main(int argc, char *argv[])
449          SET_FILE_BINARY(stdin);                 /* load from stdin */
450          if (!load_bsdf_rep(stdin))
451                  return(1);
452 <        xml_prologue(argc, argv);               /* start XML output */
452 >        xml_header(argc, argv);                 /* start XML output */
453 >        xml_prologue(NULL);
454          eval_rbf();                             /* resample dist. */
455          xml_epilogue();                         /* finish XML output & exit */
456          return(0);
457   userr:
458          fprintf(stderr,
459 <        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n",
386 <                                progname);
459 >        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n", progname);
460          fprintf(stderr,
461 <        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n",
389 <                                progname);
461 >        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n", progname);
462          fprintf(stderr,
463          "   or: %s [-n spp][-h|-q][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
464                                  progname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines