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.6 by greg, Sat Jun 29 21:03:25 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 214 | Line 237 | eval_bsdf(const char *fname)
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   {
# Line 242 | Line 265 | eval_function(char *funame)
265          double          sum;
266          int             i, j, n;
267  
268 +        initurand(npsamps);
269          data_prologue();                        /* begin output */
270          for (j = 0; j < abp->nangles; j++) {    /* run through directions */
271              for (i = 0; i < abp->nangles; i++) {
# Line 253 | Line 277 | eval_function(char *funame)
277                          bo_getvec(iovec+3, j+(n+frandom())/npsamps, abp);
278  
279                      if (input_orient > 0)
280 <                        fi_getvec(iovec, j+(n+frandom())/npsamps, abp);
280 >                        fi_getvec(iovec, i+urand(n), abp);
281                      else
282 <                        bi_getvec(iovec, j+(n+frandom())/npsamps, abp);
282 >                        bi_getvec(iovec, i+urand(n), abp);
283  
284                      sum += funvalue(funame, 6, iovec);
285                  }
# Line 271 | Line 295 | static void
295   eval_rbf(void)
296   {
297          ANGLE_BASIS     *abp = get_basis(kbasis);
298 <        double          iovec[6];
298 >        float           bsdfarr[MAXPATCHES*MAXPATCHES];
299 >        FVECT           vin, vout;
300 >        RBFNODE         *rbf;
301          double          sum;
302          int             i, j, n;
303 +                                                /* sanity check */
304 +        if (abp->nangles > MAXPATCHES) {
305 +                fprintf(stderr, "%s: too many patches!\n", progname);
306 +                exit(1);
307 +        }
308 +        data_prologue();                        /* begin output */
309 +        for (i = 0; i < abp->nangles; i++) {
310 +            if (input_orient > 0)               /* use incident patch center */
311 +                fi_getvec(vin, i+.5*(i>0), abp);
312 +            else
313 +                bi_getvec(vin, i+.5*(i>0), abp);
314  
315 < fprintf(stder, "%s: RBF evaluation currently unimplemented\n", progname);
316 < exit(1);
315 >            rbf = advect_rbf(vin);              /* compute radial basis func */
316 >
317 >            for (j = 0; j < abp->nangles; j++) {
318 >                sum = 0;                        /* sample over exiting patch */
319 >                for (n = npsamps; n--; ) {
320 >                    if (output_orient > 0)
321 >                        fo_getvec(vout, j+(n+frandom())/npsamps, abp);
322 >                    else
323 >                        bo_getvec(vout, j+(n+frandom())/npsamps, abp);
324 >
325 >                    sum += eval_rbfrep(rbf, vout) / vout[2];
326 >                }
327 >                bsdfarr[j*abp->nangles + i] = sum*output_orient/npsamps;
328 >            }
329 >            if (rbf != NULL)
330 >                free(rbf);
331 >        }
332 >        n = 0;                                  /* write out our matrix */
333 >        for (j = 0; j < abp->nangles; j++) {
334 >            for (i = 0; i < abp->nangles; i++)
335 >                printf("\t%.3e\n", bsdfarr[n++]);
336 >            putchar('\n');
337 >        }
338 >        data_epilogue();                        /* finish output */
339   }
340  
341   /* Read in BSDF and interpolate as Klems matrix representation */
# Line 292 | Line 351 | main(int argc, char *argv[])
351          esupport &= ~(E_INCHAN|E_OUTCHAN);
352          scompile("PI:3.14159265358979323846", NULL, 0);
353          biggerlib();
354 <        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
354 >        for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
355                  switch (argv[i][1]) {           /* get options */
356                  case 'n':
357                          npsamps = atoi(argv[++i]);
# Line 329 | Line 388 | main(int argc, char *argv[])
388                                          progname);
389                          goto userr;
390                  }
391 <                xml_prologue(argc, argv);       /* start XML output */
391 >                xml_header(argc, argv);                 /* start XML output */
392 >                xml_prologue(NULL);
393                  if (dofwd) {
394                          input_orient = -1;
395                          output_orient = -1;
# Line 347 | Line 407 | main(int argc, char *argv[])
407                  xml_epilogue();                 /* finish XML output & exit */
408                  return(0);
409          }
410 <        if (i == argc-1 && (cp = strstr(argv[i], ".xml")) != NULL &&
411 <                        strlen(cp) == 4) {      /* XML input? */
412 <                xml_prologue(argc, argv);       /* start XML output */
410 >                                                /* XML input? */
411 >        if (i == argc-1 && (cp = argv[i]+strlen(argv[i])-4) > argv[i] &&
412 >                                !strcasecmp(cp, ".xml")) {
413 >                xml_header(argc, argv);         /* start XML output */
414                  eval_bsdf(argv[i]);             /* load & resample BSDF */
415                  xml_epilogue();                 /* finish XML output & exit */
416                  return(0);
# Line 366 | Line 427 | main(int argc, char *argv[])
427                          if (!load_bsdf_rep(fpin))
428                                  return(1);
429                          fclose(fpin);
430 <                        if (!nbsdf++)           /* start XML on first dist. */
431 <                                xml_prologue(argc, argv);
430 >                        if (!nbsdf++) {         /* start XML on first dist. */
431 >                                xml_header(argc, argv);
432 >                                xml_prologue(NULL);
433 >                        }
434                          eval_rbf();
435                  }
436                  xml_epilogue();                 /* finish XML output & exit */
# Line 376 | Line 439 | main(int argc, char *argv[])
439          SET_FILE_BINARY(stdin);                 /* load from stdin */
440          if (!load_bsdf_rep(stdin))
441                  return(1);
442 <        xml_prologue(argc, argv);               /* start XML output */
442 >        xml_header(argc, argv);                 /* start XML output */
443 >        xml_prologue(NULL);
444          eval_rbf();                             /* resample dist. */
445          xml_epilogue();                         /* finish XML output & exit */
446          return(0);
447   userr:
448          fprintf(stderr,
449 <        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n",
386 <                                progname);
449 >        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n", progname);
450          fprintf(stderr,
451 <        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n",
389 <                                progname);
451 >        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n", progname);
452          fprintf(stderr,
453          "   or: %s [-n spp][-h|-q][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
454                                  progname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines