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

Comparing ray/src/util/wrapBSDF.c (file contents):
Revision 2.4 by greg, Sat Feb 14 00:39:21 2015 UTC vs.
Revision 2.17 by greg, Tue Feb 2 18:02:32 2016 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9  
10   #include <ctype.h>
11   #include "rtio.h"
12 < #include "rtprocess.h"
12 > #include "paths.h"
13   #include "ezxml.h"
14   #include "bsdf.h"
15   #include "bsdf_m.h"
# Line 18 | Line 18 | const char     def_template[] = "minimalBSDFt.xml";
18   const char      win6_template[] = "WINDOW6BSDFt.xml";
19  
20   const char      stdin_name[] = "<stdin>";
21
21                                          /* input files (can be stdin_name) */
22   const char      *xml_input = NULL;
23                                          /* unit for materials & geometry */
# Line 26 | Line 25 | const char     *attr_unit = "meter";
25   const char      legal_units[] = "meter|foot|inch|centimeter|millimeter";
26                                          /* system materials & geometry */
27   const char      *mgf_geometry = NULL;
28 <
29 <                                        /* angle basis */
28 >                                        /* comment list */
29 > #define MAXCOMM         30
30 > const char      *commlist[MAXCOMM];
31 > int             ncomm = 0;
32 >                                        /* angle bases */
33   enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter,
34                  ABtensorTree3, ABtensorTree4, ABend };
35  
36   int             angle_basis = ABdefault;
37  
38 + int             correct_solid_angle = 0;
39 +
40 + const char      *klems_basis_name[] = {
41 +        "LBNL/Klems Full",
42 +        "LBNL/Klems Half",
43 +        "LBNL/Klems Quarter",
44 + };
45                                          /* field IDs and nicknames */
46   struct s_fieldID {
47          char            nickName[4];
# Line 57 | Line 66 | struct s_fieldID {
66   const char      *field_assignment[MAXASSIGN];
67   int             nfield_assign = 0;
68   #define FASEP   ';'
60
69                                          /* data file(s) & spectra */
70   enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
71  
72 < enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
72 > enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5,
73 >        DSuprime=-6, DSvprime=-7 };
74  
75   #define MAXFILES        20
76  
# Line 154 | Line 163 | input2str(const char *inpspec)
163                          fprintf(stderr, "%s: cannot open\n", inpspec);
164                          return "";
165                  }
166 + #ifndef _WIN32                          /* XXX somehow broken on Windows */
167                  len = lseek(fd, 0L, SEEK_END);
168                  if (len > 0) {
169                          lseek(fd, 0L, SEEK_SET);
# Line 172 | Line 182 | input2str(const char *inpspec)
182                          close(fd);
183                          return str;
184                  }
185 + #endif
186                  fp = fdopen(fd, "r");           /* not a regular file */
187          }
188                                                  /* reading from stream */
# Line 221 | Line 232 | mat_assignments(const char *caller, const char *fn, ez
232          for (i = 0; i < nfield_assign; i++) {
233                  const char      *fnext = field_assignment[i];
234                  for ( ; ; ) {
235 +                        int     added = 0;
236                          ezxml_t fld;
237                          char    sbuf[512];
238                          int     j;
# Line 253 | Line 265 | mat_assignments(const char *caller, const char *fn, ez
265                                          fprintf(stderr,
266                                                  "%s: warning - adding tag <%s>\n",
267                                                          fn, sbuf);
268 +                                ezxml_add_txt(wtl, "\t");
269                                  fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt));
270 +                                ++added;
271                          }
272                          if (XMLfieldID[j].has_unit)
273                                  ezxml_set_attr(fld, "unit", attr_unit);
# Line 277 | Line 291 | mat_assignments(const char *caller, const char *fn, ez
291                          }
292                          sbuf[j] = '\0';
293                          ezxml_set_txt_d(fld, sbuf);
294 +                        if (added)
295 +                                ezxml_add_txt(wtl, "\n\t");
296                          fnext += (*fnext == FASEP);
297                  }
298          }
# Line 284 | Line 300 | mat_assignments(const char *caller, const char *fn, ez
300          if (xml_input == win6_template)
301              for (i = 0; XMLfieldID[i].nickName[0]; i++)
302                  if (XMLfieldID[i].win_need &&
303 <                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) {
303 >                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0])
304                          fprintf(stderr,
305 <                        "%s: missing required '%s' assignment for WINDOW <%s>\n",
305 >                        "%s: warning - missing '%s' assignment for WINDOW <%s>\n",
306                                          caller, XMLfieldID[i].nickName,
307                                          XMLfieldID[i].fullName);
308 <                        return 0;
293 <                }
294 <        return 1;               /* no errors */
308 >        return 1;
309   }
310  
311   /* Complete angle basis specification */
# Line 373 | Line 387 | determine_angle_basis(const char *fn, ezxml_t wtl)
387          return -1;
388   }
389  
390 + /* Filter Klems angle basis, factoring out incident projected solid angle */
391 + static int
392 + filter_klems_matrix(FILE *fp)
393 + {
394 + #define MAX_COLUMNS     145
395 +        const char      *bn = klems_basis_name[angle_basis];
396 +        float           col_corr[MAX_COLUMNS];
397 +        int             i, j, n = nabases;
398 +                                        /* get angle basis */
399 +        while (n-- > 0)
400 +                if (!strcasecmp(bn, abase_list[n].name))
401 +                        break;
402 +        if (n < 0)
403 +                return 0;
404 +        if (abase_list[n].nangles > MAX_COLUMNS) {
405 +                fputs("Internal error - too many Klems columns!\n", stderr);
406 +                return 0;
407 +        }
408 +                                        /* get correction factors */
409 +        for (j = abase_list[n].nangles; j--; )
410 +                col_corr[j] = 1.f / io_getohm(j, &abase_list[n]);
411 +                                        /* read/correct/write matrix */
412 +        for (i = 0; i < abase_list[n].nangles; i++) {
413 +            for (j = 0; j < abase_list[n].nangles; j++) {
414 +                double  d;
415 +                if (fscanf(fp, "%lf", &d) != 1)
416 +                        return 0;
417 +                if (d < -1e-3) {
418 +                        fputs("Negative BSDF data!\n", stderr);
419 +                        return 0;
420 +                }
421 +                printf(" %.3e", d*col_corr[j]*(d > 0));
422 +            }
423 +            fputc('\n', stdout);
424 +        }
425 +        while ((i = getc(fp)) != EOF)
426 +                if (!isspace(i)) {
427 +                        fputs("Unexpected data past EOF\n", stderr);
428 +                        return 0;
429 +                }
430 +        return 1;                       /* all is good */
431 + #undef MAX_COLUMNS
432 + }
433 +
434   /* Write out BSDF data block with surrounding tags */
435   static int
436   writeBSDFblock(const char *caller, struct s_dfile *df)
437   {
438 +        int     correct_klems = correct_solid_angle;
439          char    *cp;
440  
441          puts("\t<WavelengthData>");
# Line 384 | Line 443 | writeBSDFblock(const char *caller, struct s_dfile *df)
443          switch (df->spectrum) {
444          case DSvisible:
445                  puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
446 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
446 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
447                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
448                  break;
449          case DSxbar31:
450                  puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
451 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
451 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
452                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
453                  break;
454          case DSzbar31:
455                  puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
456 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
456 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
457                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
458                  break;
459 +        case DSuprime:
460 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>");
461 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
462 +                break;
463 +        case DSvprime:
464 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>");
465 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
466 +                break;
467          case DSsolar:
468                  puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
469 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
469 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
470                  puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
471                  break;
472          case DSnir:
473                  puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
474 <                puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
474 >                puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
475                  puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
476                  break;
477          default:
# Line 415 | Line 482 | writeBSDFblock(const char *caller, struct s_dfile *df)
482                                  spectr_file[df->spectrum]);
483                  if (cp != NULL)
484                          *cp = '.';
485 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
485 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
486                  printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
487                                  spectr_file[df->spectrum]);
488                  break;
# Line 442 | Line 509 | writeBSDFblock(const char *caller, struct s_dfile *df)
509          puts("</WavelengthDataDirection>");
510          switch (angle_basis) {
511          case ABklemsFull:
445                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
446                break;
512          case ABklemsHalf:
448                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
449                break;
513          case ABklemsQuarter:
514 <                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
514 >                fputs("\t\t\t<ColumnAngleBasis>", stdout);
515 >                fputs(klems_basis_name[angle_basis], stdout);
516 >                puts("</ColumnAngleBasis>");
517 >                fputs("\t\t\t<RowAngleBasis>", stdout);
518 >                fputs(klems_basis_name[angle_basis], stdout);
519 >                puts("</RowAngleBasis>");
520                  break;
521          case ABtensorTree3:
522          case ABtensorTree4:
523                  puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
524 +                correct_klems = 0;
525                  break;
526          default:
527                  fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
# Line 461 | Line 530 | writeBSDFblock(const char *caller, struct s_dfile *df)
530          puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
531          puts("\t\t\t<ScatteringData>");
532          fflush(stdout);
533 <        if (df->fname == stdin_name) {
533 >        if (correct_klems) {                    /* correct Klems matrix data */
534 >                FILE    *fp = stdin;
535 >                if (df->fname[0] == '!')
536 >                        fp = popen(df->fname+1, "r");
537 >                else if (df->fname != stdin_name)
538 >                        fp = fopen(df->fname, "r");
539 >                if (fp == NULL) {
540 >                        fprintf(stderr, "%s: cannot open '%s'\n",
541 >                                        caller, df->fname);
542 >                        return 0;
543 >                }
544 >                if (!filter_klems_matrix(fp)) {
545 >                        fprintf(stderr, "%s: Klems data error from '%s'\n",
546 >                                        caller, df->fname);
547 >                        return 0;
548 >                }
549 >                if (df->fname[0] != '!') {
550 >                        fclose(fp);
551 >                } else if (pclose(fp)) {
552 >                        fprintf(stderr, "%s: error running '%s'\n",
553 >                                        caller, df->fname);
554 >                        return 0;
555 >                }
556 >        } else if (df->fname == stdin_name) {
557                  copy_and_close(fileno(stdin));
558          } else if (df->fname[0] != '!') {
559                  if (!copy_and_close(open(df->fname, O_RDONLY))) {
# Line 496 | Line 588 | writeBSDF(const char *caller, ezxml_t fl)
588                  free(xml);
589                  return 0;
590          }
591 +        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
592 +        for (i = 0; i < ncomm; i++)
593 +                printf("<!-- %s -->\n", commlist[i]);
594          fflush(stdout);                         /* write previous XML info. */
595          if (write(fileno(stdout), xml, ei) != ei) {
596                  free(xml);
# Line 593 | Line 688 | wrapBSDF(const char *caller)
688                  ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition");
689                  if (dd != NULL) {
690                          offset = dd->off;
691 <                        fprintf(stderr,
691 >                        if (dd->child != NULL)
692 >                                fprintf(stderr,
693                  "%s: warning - replacing existing <DataDefinition> in '%s'\n",
694                                                  caller, xml_path);
695                          ezxml_remove(dd);
# Line 626 | Line 722 | UsageExit(const char *pname)
722   {
723          fputs("Usage: ", stderr);
724          fputs(pname, stderr);
725 <        fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
726 <        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr);
725 >        fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
726 >        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr);
727          fputs(" [input.xml]\n", stderr);
728          exit(1);
729   }
# Line 693 | Line 789 | main(int argc, char *argv[])
789                          else
790                                  UsageExit(argv[0]);
791                          continue;
792 +                case 'c':               /* correct solid angle */
793 +                        correct_solid_angle = 1;
794 +                        continue;
795 +                case 'C':               /* comment */
796 +                        if (ncomm >= MAXCOMM) {
797 +                                fprintf(stderr, "%s: too many comments\n",
798 +                                                argv[0]);
799 +                                return 1;
800 +                        }
801 +                        if (strchr(argv[++i], '>') != NULL) {
802 +                                fprintf(stderr, "%s: illegal character in comment\n",
803 +                                                argv[0]);
804 +                                return 1;
805 +                        }
806 +                        commlist[ncomm++] = argv[i];
807 +                        continue;
808                  case 't':               /* transmission */
809                          if (i >= argc-1)
810                                  UsageExit(argv[0]);
# Line 734 | Line 846 | main(int argc, char *argv[])
846                                  argv[i] = (char *)stdin_name;
847                          }
848                          data_file[ndataf].fname = argv[i];
849 <                        data_file[ndataf++].spectrum = cur_spectrum;
849 >                        data_file[ndataf].spectrum = cur_spectrum;
850 >                        ndataf++;
851                          continue;
852                  case 's':               /* spectrum name or input file */
853                          if (++i >= argc)
# Line 748 | Line 861 | main(int argc, char *argv[])
861                                  cur_spectrum = DSxbar31;
862                          else if (!strcasecmp(argv[i], "CIE-Z"))
863                                  cur_spectrum = DSzbar31;
864 +                        else if (!strcasecmp(argv[i], "CIE-u"))
865 +                                cur_spectrum = DSuprime;
866 +                        else if (!strcasecmp(argv[i], "CIE-v"))
867 +                                cur_spectrum = DSvprime;
868                          else if (!strcasecmp(argv[i], "NIR"))
869                                  cur_spectrum = DSnir;
870                          else {
# Line 796 | Line 913 | doneOptions:                                   /* get XML input */
913          } else {
914                  xml_input = argv[i];
915          }
799        if ((xml_input == win6_template) & (angle_basis == ABdefault))
800                angle_basis = ABklemsFull;
916                                                  /* wrap it! */
917          return !wrapBSDF(argv[0]);
918   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines