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.6 by greg, Sun Feb 15 17:23:06 2015 UTC vs.
Revision 2.26 by greg, Mon Oct 26 21:12:20 2020 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7   *      G. Ward         February 2015
8   */
9  
10 + #include "platform.h"
11   #include <ctype.h>
12   #include "rtio.h"
13 < #include "rtprocess.h"
13 > #include "paths.h"
14   #include "ezxml.h"
15   #include "bsdf.h"
16   #include "bsdf_m.h"
# Line 18 | Line 19 | const char     def_template[] = "minimalBSDFt.xml";
19   const char      win6_template[] = "WINDOW6BSDFt.xml";
20  
21   const char      stdin_name[] = "<stdin>";
21
22                                          /* input files (can be stdin_name) */
23   const char      *xml_input = NULL;
24                                          /* unit for materials & geometry */
# Line 26 | Line 26 | const char     *attr_unit = "meter";
26   const char      legal_units[] = "meter|foot|inch|centimeter|millimeter";
27                                          /* system materials & geometry */
28   const char      *mgf_geometry = NULL;
29 <
29 >                                        /* comment list */
30 > #define MAXCOMM         80
31 > const char      *commlist[MAXCOMM];
32 > int             ncomm = 0;
33                                          /* angle bases */
34   enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter,
35                  ABtensorTree3, ABtensorTree4, ABend };
36  
37   int             angle_basis = ABdefault;
38  
39 + int             correct_solid_angle = 0;
40 +
41 + const char      *klems_basis_name[] = {
42 +        "LBNL/Klems Full",
43 +        "LBNL/Klems Half",
44 +        "LBNL/Klems Quarter",
45 + };
46                                          /* field IDs and nicknames */
47   struct s_fieldID {
48          char            nickName[4];
# Line 42 | Line 52 | struct s_fieldID {
52   } XMLfieldID[] = {
53          {"m", 0, 1, "Manufacturer"},
54          {"n", 0, 1, "Name"},
55 +        {"d", 0, 0, "DeviceType"},
56          {"c", 0, 0, "ThermalConductivity"},
57          {"ef", 0, 0, "EmissivityFront"},
58          {"eb", 0, 0, "EmissivityBack"},
# Line 53 | Line 64 | struct s_fieldID {
64          {"\0", 0, 0, NULL}      /* terminator */
65   };
66                                          /* field assignments */
67 < #define MAXASSIGN       12
67 > #define MAXASSIGN       16
68   const char      *field_assignment[MAXASSIGN];
69   int             nfield_assign = 0;
70   #define FASEP   ';'
60
71                                          /* data file(s) & spectra */
72 < enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
72 > enum { DTransFront, DTransBack, DTreflFront, DTreflBack };
73  
74 < enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
74 > static const char       component_name[4][20] = {
75 >                "Transmission Front",
76 >                "Transmission Back",
77 >                "Reflection Front",
78 >                "Reflection Back"
79 > };
80  
81 + enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5,
82 +        DSuprime=-6, DSvprime=-7 };
83 +
84   #define MAXFILES        20
85  
86   struct s_dfile {
# Line 73 | Line 91 | struct s_dfile {
91  
92   int             ndataf = 0;             /* number of data files */
93  
94 + int             unlink_datafiles = 0;   /* unlink data files when done */
95 +
96   const char      *spectr_file[MAXFILES]; /* custom spectral curve input */
97  
98   const char      top_level_name[] = "WindowElement";
# Line 109 | Line 129 | static char    basis_definition[][256] = {
129          "\t</DataDefinition>\n",
130   };
131  
132 + /* Check that the last-added data file is unique */
133 + static int
134 + check_new_data_file()
135 + {
136 +        int     i = ndataf;
137 +
138 +        while (i-- > 0)
139 +                if ((data_file[i].spectrum == data_file[ndataf].spectrum) &
140 +                                (data_file[i].type == data_file[ndataf].type)) {
141 +                        fprintf(stderr,
142 +                                "%s: warning - ignoring duplicate component %s\n",
143 +                                        data_file[ndataf].fname,
144 +                                        component_name[data_file[i].type]);
145 +                        return 0;
146 +                }
147 +        return 1;
148 + }
149 +
150   /* Copy data from file descriptor to stdout and close */
151   static int
152   copy_and_close(int fd)
# Line 154 | Line 192 | input2str(const char *inpspec)
192                          fprintf(stderr, "%s: cannot open\n", inpspec);
193                          return "";
194                  }
195 + #if !defined(_WIN32) && !defined(_WIN64)
196 +                                /* XXX somehow broken on Windows */
197                  len = lseek(fd, 0L, SEEK_END);
198                  if (len > 0) {
199                          lseek(fd, 0L, SEEK_SET);
# Line 172 | Line 212 | input2str(const char *inpspec)
212                          close(fd);
213                          return str;
214                  }
215 + #endif
216                  fp = fdopen(fd, "r");           /* not a regular file */
217          }
218                                                  /* reading from stream */
# Line 289 | Line 330 | mat_assignments(const char *caller, const char *fn, ez
330          if (xml_input == win6_template)
331              for (i = 0; XMLfieldID[i].nickName[0]; i++)
332                  if (XMLfieldID[i].win_need &&
333 <                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) {
333 >                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0])
334                          fprintf(stderr,
335 <                        "%s: missing required '%s' assignment for WINDOW <%s>\n",
335 >                        "%s: warning - missing '%s' assignment for WINDOW <%s>\n",
336                                          caller, XMLfieldID[i].nickName,
337                                          XMLfieldID[i].fullName);
338 <                        return 0;
298 <                }
299 <        return 1;               /* no errors */
338 >        return 1;
339   }
340  
341   /* Complete angle basis specification */
# Line 378 | Line 417 | determine_angle_basis(const char *fn, ezxml_t wtl)
417          return -1;
418   }
419  
420 < /* Filter Klems angle basis, applying appropriate solid angle correction */
420 > /* Filter Klems angle basis, factoring out incident projected solid angle */
421   static int
422   filter_klems_matrix(FILE *fp)
423   {
424 < #define MAX_COLUMNS     145
386 <        float           col_corr[MAX_COLUMNS];
387 <        const char      *bn;
424 >        const char      *bn = klems_basis_name[angle_basis];
425          int             i, j, n = nabases;
426                                          /* get angle basis */
390        switch (angle_basis) {
391        case ABklemsFull:       bn = "LBNL/Klems Full"; break;
392        case ABklemsHalf:       bn = "LBNL/Klems Half"; break;
393        case ABklemsQuarter:    bn = "LBNL/Klems Quarter"; break;
394        default:
395                return 0;
396        }
427          while (n-- > 0)
428                  if (!strcasecmp(bn, abase_list[n].name))
429                          break;
430          if (n < 0)
431                  return 0;
402        if (abase_list[n].nangles > MAX_COLUMNS) {
403                fprintf(stderr, "Internal error - too many Klems columns!\n");
404                return 0;
405        }
406                                        /* get correction factors */
407        for (j = abase_list[n].nangles; j--; )
408                col_corr[j] = 1.f / io_getohm(j, &abase_list[n]);
432                                          /* read/correct/write matrix */
433          for (i = 0; i < abase_list[n].nangles; i++) {
434 +            const double        corr = 1./io_getohm(i, &abase_list[n]);
435              for (j = 0; j < abase_list[n].nangles; j++) {
436                  double  d;
437                  if (fscanf(fp, "%lf", &d) != 1)
438                          return 0;
439 <                printf(" %f", d*col_corr[j]);
439 >                if (d < -1e-3) {
440 >                        fputs("Negative BSDF data!\n", stderr);
441 >                        return 0;
442 >                }
443 >                printf(" %.3e", d*corr*(d > 0));
444              }
445              fputc('\n', stdout);
446          }
447 <        return 1;
448 < #undef MAX_COLUMNS
447 >        while ((i = getc(fp)) != EOF)
448 >                if (!isspace(i)) {
449 >                        fputs("Unexpected data past EOF\n", stderr);
450 >                        return 0;
451 >                }
452 >        return 1;                       /* all is good */
453   }
454  
455   /* Write out BSDF data block with surrounding tags */
456   static int
457   writeBSDFblock(const char *caller, struct s_dfile *df)
458   {
459 <        int     klems_data = 0;
459 >        int     correct_klems = correct_solid_angle;
460          char    *cp;
461  
462          puts("\t<WavelengthData>");
# Line 432 | Line 464 | writeBSDFblock(const char *caller, struct s_dfile *df)
464          switch (df->spectrum) {
465          case DSvisible:
466                  puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
467 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
467 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
468                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
469                  break;
470          case DSxbar31:
471                  puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
472 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
472 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
473                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
474                  break;
475          case DSzbar31:
476                  puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
477 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
477 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
478                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
479                  break;
480 +        case DSuprime:
481 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>");
482 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
483 +                break;
484 +        case DSvprime:
485 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>");
486 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
487 +                break;
488          case DSsolar:
489                  puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
490 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
490 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
491                  puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
492                  break;
493          case DSnir:
494                  puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
495 <                puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
495 >                puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
496                  puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
497                  break;
498          default:
# Line 463 | Line 503 | writeBSDFblock(const char *caller, struct s_dfile *df)
503                                  spectr_file[df->spectrum]);
504                  if (cp != NULL)
505                          *cp = '.';
506 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
506 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
507                  printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
508                                  spectr_file[df->spectrum]);
509                  break;
510          }
511          puts("\t\t<WavelengthDataBlock>");
512          fputs("\t\t\t<WavelengthDataDirection>", stdout);
513 <        switch (df->type) {
474 <        case DTtransForward:
475 <                fputs("Transmission Front", stdout);
476 <                break;
477 <        case DTtransBackward:
478 <                fputs("Transmission Back", stdout);
479 <                break;
480 <        case DTreflForward:
481 <                fputs("Reflection Front", stdout);
482 <                break;
483 <        case DTreflBackward:
484 <                fputs("Reflection Back", stdout);
485 <                break;
486 <        default:
487 <                fprintf(stderr, "%s: internal - bad BSDF type (%d)\n", caller, df->type);
488 <                return 0;
489 <        }
513 >        fputs(component_name[df->type], stdout);
514          puts("</WavelengthDataDirection>");
491        klems_data = 1;
515          switch (angle_basis) {
516          case ABklemsFull:
494                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
495                break;
517          case ABklemsHalf:
497                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
498                break;
518          case ABklemsQuarter:
519 <                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
519 >                fputs("\t\t\t<ColumnAngleBasis>", stdout);
520 >                fputs(klems_basis_name[angle_basis], stdout);
521 >                puts("</ColumnAngleBasis>");
522 >                fputs("\t\t\t<RowAngleBasis>", stdout);
523 >                fputs(klems_basis_name[angle_basis], stdout);
524 >                puts("</RowAngleBasis>");
525                  break;
526          case ABtensorTree3:
527          case ABtensorTree4:
528                  puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
529 <                klems_data = 0;
529 >                correct_klems = 0;
530                  break;
531          default:
532                  fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
# Line 511 | Line 535 | writeBSDFblock(const char *caller, struct s_dfile *df)
535          puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
536          puts("\t\t\t<ScatteringData>");
537          fflush(stdout);
538 <        if (klems_data) {
538 >        if (correct_klems) {                    /* correct Klems matrix data */
539                  FILE    *fp = stdin;
540                  if (df->fname[0] == '!')
541                          fp = popen(df->fname+1, "r");
# Line 569 | Line 593 | writeBSDF(const char *caller, ezxml_t fl)
593                  free(xml);
594                  return 0;
595          }
596 +        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
597 +        for (i = 0; i < ncomm; i++)
598 +                printf("<!-- %s -->\n", commlist[i]);
599          fflush(stdout);                         /* write previous XML info. */
600          if (write(fileno(stdout), xml, ei) != ei) {
601                  free(xml);
# Line 582 | Line 609 | writeBSDF(const char *caller, ezxml_t fl)
609          fputs(xml+ei, stdout);                  /* write trailer */
610          free(xml);                              /* free string */
611          fputc('\n', stdout);
612 <        return (fflush(stdout) == 0);
612 >        if (fflush(stdout) != 0)
613 >                return 0;
614 >                                                /* unlink data files if req. */
615 >        for (i = ndataf*(unlink_datafiles != 0); i--; )
616 >                if (data_file[i].fname != stdin_name &&
617 >                                data_file[i].fname[0] != '!')
618 >                        unlink(data_file[i].fname);
619 >        if (unlink_datafiles > 1 && mgf_geometry != NULL &&
620 >                        mgf_geometry != stdin_name &&
621 >                        mgf_geometry[0] != '!')
622 >                unlink(mgf_geometry);
623 >        return 1;
624   }
625  
626   /* Insert BSDF data into XML wrapper */
# Line 700 | Line 738 | UsageExit(const char *pname)
738   {
739          fputs("Usage: ", stderr);
740          fputs(pname, stderr);
741 <        fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
742 <        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr);
741 >        fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
742 >        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr);
743          fputs(" [input.xml]\n", stderr);
744          exit(1);
745   }
# Line 746 | Line 784 | main(int argc, char *argv[])
784                          }
785                          attr_unit = argv[i];
786                          continue;
787 +                case 'U':               /* unlink data files when done */
788 +                        unlink_datafiles = 1 + (argv[i][2] == 'U');
789 +                        continue;
790                  case 'a':               /* angle basis */
791                          if (++i >= argc)
792                                  UsageExit(argv[0]);
# Line 767 | Line 808 | main(int argc, char *argv[])
808                          else
809                                  UsageExit(argv[0]);
810                          continue;
811 +                case 'c':               /* correct solid angle */
812 +                        correct_solid_angle = 1;
813 +                        continue;
814 +                case 'C':               /* comment */
815 +                        if (ncomm >= MAXCOMM) {
816 +                                fprintf(stderr, "%s: too many comments\n",
817 +                                                argv[0]);
818 +                                return 1;
819 +                        }
820 +                        if (strstr(argv[++i], "-->") != NULL) {
821 +                                fprintf(stderr, "%s: illegal character in comment\n",
822 +                                                argv[0]);
823 +                                return 1;
824 +                        }
825 +                        commlist[ncomm++] = argv[i];
826 +                        continue;
827                  case 't':               /* transmission */
828                          if (i >= argc-1)
829                                  UsageExit(argv[0]);
# Line 776 | Line 833 | main(int argc, char *argv[])
833                                  return 1;
834                          }
835                          if (!strcmp(argv[i], "-tf"))
836 <                                data_file[ndataf].type = DTtransForward;
836 >                                data_file[ndataf].type = DTransFront;
837                          else if (!strcmp(argv[i], "-tb"))
838 <                                data_file[ndataf].type = DTtransBackward;
838 >                                data_file[ndataf].type = DTransBack;
839                          else
840                                  UsageExit(argv[0]);
841                          if (!strcmp(argv[++i], "-")) {
# Line 787 | Line 844 | main(int argc, char *argv[])
844                          }
845                          data_file[ndataf].fname = argv[i];
846                          data_file[ndataf].spectrum = cur_spectrum;
847 <                        ndataf++;
847 >                        ndataf += check_new_data_file();
848                          continue;
849                  case 'r':               /* reflection */
850                          if (i >= argc-1)
# Line 798 | Line 855 | main(int argc, char *argv[])
855                                  return 1;
856                          }
857                          if (!strcmp(argv[i], "-rf"))
858 <                                data_file[ndataf].type = DTreflForward;
858 >                                data_file[ndataf].type = DTreflFront;
859                          else if (!strcmp(argv[i], "-rb"))
860 <                                data_file[ndataf].type = DTreflBackward;
860 >                                data_file[ndataf].type = DTreflBack;
861                          else
862                                  UsageExit(argv[0]);
863                          if (!strcmp(argv[++i], "-")) {
# Line 808 | Line 865 | main(int argc, char *argv[])
865                                  argv[i] = (char *)stdin_name;
866                          }
867                          data_file[ndataf].fname = argv[i];
868 <                        data_file[ndataf++].spectrum = cur_spectrum;
868 >                        data_file[ndataf].spectrum = cur_spectrum;
869 >                        ndataf += check_new_data_file();
870                          continue;
871                  case 's':               /* spectrum name or input file */
872                          if (++i >= argc)
# Line 822 | Line 880 | main(int argc, char *argv[])
880                                  cur_spectrum = DSxbar31;
881                          else if (!strcasecmp(argv[i], "CIE-Z"))
882                                  cur_spectrum = DSzbar31;
883 +                        else if (!strcasecmp(argv[i], "CIE-u"))
884 +                                cur_spectrum = DSuprime;
885 +                        else if (!strcasecmp(argv[i], "CIE-v"))
886 +                                cur_spectrum = DSvprime;
887                          else if (!strcasecmp(argv[i], "NIR"))
888                                  cur_spectrum = DSnir;
889                          else {
# Line 857 | Line 919 | main(int argc, char *argv[])
919                  }
920                  break;
921          }
922 < doneOptions:                                    /* get XML input */
922 >                                                /* get XML input */
923          if (i >= argc) {
924                  if (xml_input == NULL)
925                          xml_input = def_template;
# Line 870 | Line 932 | doneOptions:                                   /* get XML input */
932          } else {
933                  xml_input = argv[i];
934          }
873        if ((xml_input == win6_template) & (angle_basis == ABdefault))
874                angle_basis = ABklemsFull;
935                                                  /* wrap it! */
936          return !wrapBSDF(argv[0]);
937   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines