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.12 by greg, Wed Mar 4 17:42:54 2015 UTC vs.
Revision 2.27 by greg, Wed Sep 7 18:55:39 2022 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 25 | 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 };
# Line 48 | 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 59 | 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   ';'
71                                          /* data file(s) & spectra */
72 < enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
72 > enum { DTransFront, DTransBack, DTreflFront, DTreflBack };
73  
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  
# Line 79 | 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 115 | 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 160 | Line 192 | input2str(const char *inpspec)
192                          fprintf(stderr, "%s: cannot open\n", inpspec);
193                          return "";
194                  }
195 < #ifndef _WIN32                          /* XXX somehow broken on Windows */
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 384 | Line 417 | determine_angle_basis(const char *fn, ezxml_t wtl)
417          return -1;
418   }
419  
420 < /* Filter Klems angle basis, factoring out incident projected solid angle */
420 > /* Filter Klems angle data, factoring out incident projected solid angle */
421   static int
422   filter_klems_matrix(FILE *fp)
423   {
391 #define MAX_COLUMNS     145
424          const char      *bn = klems_basis_name[angle_basis];
425 <        float           col_corr[MAX_COLUMNS];
394 <        int             i, j, n = nabases;
425 >        int             i, j, c, n = nabases;
426                                          /* get angle basis */
427          while (n-- > 0)
428                  if (!strcasecmp(bn, abase_list[n].name))
429                          break;
430          if (n < 0)
431                  return 0;
401        if (abase_list[n].nangles > MAX_COLUMNS) {
402                fputs("Internal error - too many Klems columns!\n", stderr);
403                return 0;
404        }
405                                        /* get correction factors */
406        for (j = abase_list[n].nangles; j--; )
407                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)
437 >                if (fscanf(fp, "%lf ", &d) != 1)
438                          return 0;
439 +                if ((c = getc(fp)) != ',')
440 +                        ungetc(c, fp);
441                  if (d < -1e-3) {
442                          fputs("Negative BSDF data!\n", stderr);
443                          return 0;
444                  }
445 <                printf(" %.3e", d*col_corr[j]*(d > 0));
445 >                printf(" %.3e", d*corr*(d > 0));
446              }
447              fputc('\n', stdout);
448          }
449 <        while ((i = getc(fp)) != EOF)
450 <                if (!isspace(i)) {
449 >        while ((c = getc(fp)) != EOF)
450 >                if (!isspace(c)) {
451                          fputs("Unexpected data past EOF\n", stderr);
452                          return 0;
453                  }
454          return 1;                       /* all is good */
428 #undef MAX_COLUMNS
455   }
456  
457   /* Write out BSDF data block with surrounding tags */
# Line 445 | Line 471 | writeBSDFblock(const char *caller, struct s_dfile *df)
471                  break;
472          case DSxbar31:
473                  puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
474 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
474 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
475                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
476                  break;
477          case DSzbar31:
478                  puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
479 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
479 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
480                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
481                  break;
482          case DSuprime:
483 <                puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
484 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
459 <                puts("\t\t<DetectorSpectrum>ASTM E308 1931 u.dsp</DetectorSpectrum>");
483 >                puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>");
484 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
485                  break;
486          case DSvprime:
487 <                puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
488 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
464 <                puts("\t\t<DetectorSpectrum>ASTM E308 1931 v.dsp</DetectorSpectrum>");
487 >                puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>");
488 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
489                  break;
490          case DSsolar:
491                  puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
492 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
492 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
493                  puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
494                  break;
495          case DSnir:
496                  puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
497 <                puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
497 >                puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
498                  puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
499                  break;
500          default:
# Line 481 | Line 505 | writeBSDFblock(const char *caller, struct s_dfile *df)
505                                  spectr_file[df->spectrum]);
506                  if (cp != NULL)
507                          *cp = '.';
508 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
508 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
509                  printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
510                                  spectr_file[df->spectrum]);
511                  break;
512          }
513          puts("\t\t<WavelengthDataBlock>");
514          fputs("\t\t\t<WavelengthDataDirection>", stdout);
515 <        switch (df->type) {
492 <        case DTtransForward:
493 <                fputs("Transmission Front", stdout);
494 <                break;
495 <        case DTtransBackward:
496 <                fputs("Transmission Back", stdout);
497 <                break;
498 <        case DTreflForward:
499 <                fputs("Reflection Front", stdout);
500 <                break;
501 <        case DTreflBackward:
502 <                fputs("Reflection Back", stdout);
503 <                break;
504 <        default:
505 <                fprintf(stderr, "%s: internal - bad BSDF type (%d)\n", caller, df->type);
506 <                return 0;
507 <        }
515 >        fputs(component_name[df->type], stdout);
516          puts("</WavelengthDataDirection>");
517          switch (angle_basis) {
518          case ABklemsFull:
# Line 587 | Line 595 | writeBSDF(const char *caller, ezxml_t fl)
595                  free(xml);
596                  return 0;
597          }
598 +        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
599 +        for (i = 0; i < ncomm; i++)
600 +                printf("<!-- %s -->\n", commlist[i]);
601          fflush(stdout);                         /* write previous XML info. */
602          if (write(fileno(stdout), xml, ei) != ei) {
603                  free(xml);
# Line 600 | Line 611 | writeBSDF(const char *caller, ezxml_t fl)
611          fputs(xml+ei, stdout);                  /* write trailer */
612          free(xml);                              /* free string */
613          fputc('\n', stdout);
614 <        return (fflush(stdout) == 0);
614 >        if (fflush(stdout) != 0)
615 >                return 0;
616 >                                                /* unlink data files if req. */
617 >        for (i = ndataf*(unlink_datafiles != 0); i--; )
618 >                if (data_file[i].fname != stdin_name &&
619 >                                data_file[i].fname[0] != '!')
620 >                        unlink(data_file[i].fname);
621 >        if (unlink_datafiles > 1 && mgf_geometry != NULL &&
622 >                        mgf_geometry != stdin_name &&
623 >                        mgf_geometry[0] != '!')
624 >                unlink(mgf_geometry);
625 >        return 1;
626   }
627  
628   /* Insert BSDF data into XML wrapper */
# Line 719 | Line 741 | UsageExit(const char *pname)
741          fputs("Usage: ", stderr);
742          fputs(pname, stderr);
743          fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
744 <        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr);
744 >        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr);
745          fputs(" [input.xml]\n", stderr);
746          exit(1);
747   }
# Line 764 | Line 786 | main(int argc, char *argv[])
786                          }
787                          attr_unit = argv[i];
788                          continue;
789 +                case 'U':               /* unlink data files when done */
790 +                        unlink_datafiles = 1 + (argv[i][2] == 'U');
791 +                        continue;
792                  case 'a':               /* angle basis */
793                          if (++i >= argc)
794                                  UsageExit(argv[0]);
# Line 788 | Line 813 | main(int argc, char *argv[])
813                  case 'c':               /* correct solid angle */
814                          correct_solid_angle = 1;
815                          continue;
816 +                case 'C':               /* comment */
817 +                        if (ncomm >= MAXCOMM) {
818 +                                fprintf(stderr, "%s: too many comments\n",
819 +                                                argv[0]);
820 +                                return 1;
821 +                        }
822 +                        if (strstr(argv[++i], "-->") != NULL) {
823 +                                fprintf(stderr, "%s: illegal character in comment\n",
824 +                                                argv[0]);
825 +                                return 1;
826 +                        }
827 +                        commlist[ncomm++] = argv[i];
828 +                        continue;
829                  case 't':               /* transmission */
830                          if (i >= argc-1)
831                                  UsageExit(argv[0]);
# Line 797 | Line 835 | main(int argc, char *argv[])
835                                  return 1;
836                          }
837                          if (!strcmp(argv[i], "-tf"))
838 <                                data_file[ndataf].type = DTtransForward;
838 >                                data_file[ndataf].type = DTransFront;
839                          else if (!strcmp(argv[i], "-tb"))
840 <                                data_file[ndataf].type = DTtransBackward;
840 >                                data_file[ndataf].type = DTransBack;
841                          else
842                                  UsageExit(argv[0]);
843                          if (!strcmp(argv[++i], "-")) {
# Line 808 | Line 846 | main(int argc, char *argv[])
846                          }
847                          data_file[ndataf].fname = argv[i];
848                          data_file[ndataf].spectrum = cur_spectrum;
849 <                        ndataf++;
849 >                        ndataf += check_new_data_file();
850                          continue;
851                  case 'r':               /* reflection */
852                          if (i >= argc-1)
# Line 819 | Line 857 | main(int argc, char *argv[])
857                                  return 1;
858                          }
859                          if (!strcmp(argv[i], "-rf"))
860 <                                data_file[ndataf].type = DTreflForward;
860 >                                data_file[ndataf].type = DTreflFront;
861                          else if (!strcmp(argv[i], "-rb"))
862 <                                data_file[ndataf].type = DTreflBackward;
862 >                                data_file[ndataf].type = DTreflBack;
863                          else
864                                  UsageExit(argv[0]);
865                          if (!strcmp(argv[++i], "-")) {
# Line 830 | Line 868 | main(int argc, char *argv[])
868                          }
869                          data_file[ndataf].fname = argv[i];
870                          data_file[ndataf].spectrum = cur_spectrum;
871 <                        ndataf++;
871 >                        ndataf += check_new_data_file();
872                          continue;
873                  case 's':               /* spectrum name or input file */
874                          if (++i >= argc)
# Line 883 | Line 921 | main(int argc, char *argv[])
921                  }
922                  break;
923          }
924 < doneOptions:                                    /* get XML input */
924 >                                                /* get XML input */
925          if (i >= argc) {
926                  if (xml_input == NULL)
927                          xml_input = def_template;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines