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.20 by schorsch, Sun Mar 6 01:13:18 2016 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         30
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 57 | Line 67 | struct s_fieldID {
67   const char      *field_assignment[MAXASSIGN];
68   int             nfield_assign = 0;
69   #define FASEP   ';'
60
70                                          /* data file(s) & spectra */
71   enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
72  
73 < enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
73 > enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5,
74 >        DSuprime=-6, DSvprime=-7 };
75  
76   #define MAXFILES        20
77  
# Line 73 | Line 83 | struct s_dfile {
83  
84   int             ndataf = 0;             /* number of data files */
85  
86 + int             unlink_datafiles = 0;   /* unlink data files when done */
87 +
88   const char      *spectr_file[MAXFILES]; /* custom spectral curve input */
89  
90   const char      top_level_name[] = "WindowElement";
# Line 154 | Line 166 | input2str(const char *inpspec)
166                          fprintf(stderr, "%s: cannot open\n", inpspec);
167                          return "";
168                  }
169 + #if !defined(_WIN32) && !defined(_WIN64)
170 +                                /* XXX somehow broken on Windows */
171                  len = lseek(fd, 0L, SEEK_END);
172                  if (len > 0) {
173                          lseek(fd, 0L, SEEK_SET);
# Line 172 | Line 186 | input2str(const char *inpspec)
186                          close(fd);
187                          return str;
188                  }
189 + #endif
190                  fp = fdopen(fd, "r");           /* not a regular file */
191          }
192                                                  /* reading from stream */
# Line 289 | Line 304 | mat_assignments(const char *caller, const char *fn, ez
304          if (xml_input == win6_template)
305              for (i = 0; XMLfieldID[i].nickName[0]; i++)
306                  if (XMLfieldID[i].win_need &&
307 <                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) {
307 >                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0])
308                          fprintf(stderr,
309 <                        "%s: missing required '%s' assignment for WINDOW <%s>\n",
309 >                        "%s: warning - missing '%s' assignment for WINDOW <%s>\n",
310                                          caller, XMLfieldID[i].nickName,
311                                          XMLfieldID[i].fullName);
312 <                        return 0;
298 <                }
299 <        return 1;               /* no errors */
312 >        return 1;
313   }
314  
315   /* Complete angle basis specification */
# Line 378 | Line 391 | determine_angle_basis(const char *fn, ezxml_t wtl)
391          return -1;
392   }
393  
394 < /* Filter Klems angle basis, applying appropriate solid angle correction */
394 > /* Filter Klems angle basis, factoring out incident projected solid angle */
395   static int
396   filter_klems_matrix(FILE *fp)
397   {
398   #define MAX_COLUMNS     145
399 +        const char      *bn = klems_basis_name[angle_basis];
400          float           col_corr[MAX_COLUMNS];
387        const char      *bn;
401          int             i, j, n = nabases;
402                                          /* 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        }
403          while (n-- > 0)
404                  if (!strcasecmp(bn, abase_list[n].name))
405                          break;
406          if (n < 0)
407                  return 0;
408          if (abase_list[n].nangles > MAX_COLUMNS) {
409 <                fprintf(stderr, "Internal error - too many Klems columns!\n");
409 >                fputs("Internal error - too many Klems columns!\n", stderr);
410                  return 0;
411          }
412                                          /* get correction factors */
# Line 412 | Line 418 | filter_klems_matrix(FILE *fp)
418                  double  d;
419                  if (fscanf(fp, "%lf", &d) != 1)
420                          return 0;
421 <                printf(" %f", d*col_corr[j]);
421 >                if (d < -1e-3) {
422 >                        fputs("Negative BSDF data!\n", stderr);
423 >                        return 0;
424 >                }
425 >                printf(" %.3e", d*col_corr[j]*(d > 0));
426              }
427              fputc('\n', stdout);
428          }
429 <        return 1;
429 >        while ((i = getc(fp)) != EOF)
430 >                if (!isspace(i)) {
431 >                        fputs("Unexpected data past EOF\n", stderr);
432 >                        return 0;
433 >                }
434 >        return 1;                       /* all is good */
435   #undef MAX_COLUMNS
436   }
437  
# Line 424 | Line 439 | filter_klems_matrix(FILE *fp)
439   static int
440   writeBSDFblock(const char *caller, struct s_dfile *df)
441   {
442 <        int     klems_data = 0;
442 >        int     correct_klems = correct_solid_angle;
443          char    *cp;
444  
445          puts("\t<WavelengthData>");
# Line 432 | Line 447 | writeBSDFblock(const char *caller, struct s_dfile *df)
447          switch (df->spectrum) {
448          case DSvisible:
449                  puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
450 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
450 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
451                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
452                  break;
453          case DSxbar31:
454                  puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
455 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
455 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
456                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
457                  break;
458          case DSzbar31:
459                  puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
460 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
460 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
461                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
462                  break;
463 +        case DSuprime:
464 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>");
465 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
466 +                break;
467 +        case DSvprime:
468 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>");
469 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
470 +                break;
471          case DSsolar:
472                  puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
473 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
473 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
474                  puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
475                  break;
476          case DSnir:
477                  puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
478 <                puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
478 >                puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
479                  puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
480                  break;
481          default:
# Line 463 | Line 486 | writeBSDFblock(const char *caller, struct s_dfile *df)
486                                  spectr_file[df->spectrum]);
487                  if (cp != NULL)
488                          *cp = '.';
489 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
489 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
490                  printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
491                                  spectr_file[df->spectrum]);
492                  break;
# Line 488 | Line 511 | writeBSDFblock(const char *caller, struct s_dfile *df)
511                  return 0;
512          }
513          puts("</WavelengthDataDirection>");
491        klems_data = 1;
514          switch (angle_basis) {
515          case ABklemsFull:
494                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
495                break;
516          case ABklemsHalf:
497                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
498                break;
517          case ABklemsQuarter:
518 <                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
518 >                fputs("\t\t\t<ColumnAngleBasis>", stdout);
519 >                fputs(klems_basis_name[angle_basis], stdout);
520 >                puts("</ColumnAngleBasis>");
521 >                fputs("\t\t\t<RowAngleBasis>", stdout);
522 >                fputs(klems_basis_name[angle_basis], stdout);
523 >                puts("</RowAngleBasis>");
524                  break;
525          case ABtensorTree3:
526          case ABtensorTree4:
527                  puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
528 <                klems_data = 0;
528 >                correct_klems = 0;
529                  break;
530          default:
531                  fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
# Line 511 | Line 534 | writeBSDFblock(const char *caller, struct s_dfile *df)
534          puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
535          puts("\t\t\t<ScatteringData>");
536          fflush(stdout);
537 <        if (klems_data) {
537 >        if (correct_klems) {                    /* correct Klems matrix data */
538                  FILE    *fp = stdin;
539                  if (df->fname[0] == '!')
540                          fp = popen(df->fname+1, "r");
# Line 569 | Line 592 | writeBSDF(const char *caller, ezxml_t fl)
592                  free(xml);
593                  return 0;
594          }
595 +        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
596 +        for (i = 0; i < ncomm; i++)
597 +                printf("<!-- %s -->\n", commlist[i]);
598          fflush(stdout);                         /* write previous XML info. */
599          if (write(fileno(stdout), xml, ei) != ei) {
600                  free(xml);
# Line 582 | Line 608 | writeBSDF(const char *caller, ezxml_t fl)
608          fputs(xml+ei, stdout);                  /* write trailer */
609          free(xml);                              /* free string */
610          fputc('\n', stdout);
611 <        return (fflush(stdout) == 0);
611 >        if (fflush(stdout) != 0)
612 >                return 0;
613 >                                                /* unlink data files if req. */
614 >        for (i = ndataf*(unlink_datafiles != 0); i--; )
615 >                if (data_file[i].fname != stdin_name &&
616 >                                data_file[i].fname[0] != '!')
617 >                        unlink(data_file[i].fname);
618 >        if (unlink_datafiles > 1 && mgf_geometry != NULL &&
619 >                        mgf_geometry != stdin_name &&
620 >                        mgf_geometry[0] != '!')
621 >                unlink(mgf_geometry);
622 >        return 1;
623   }
624  
625   /* Insert BSDF data into XML wrapper */
# Line 700 | Line 737 | UsageExit(const char *pname)
737   {
738          fputs("Usage: ", stderr);
739          fputs(pname, stderr);
740 <        fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
741 <        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr);
740 >        fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
741 >        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr);
742          fputs(" [input.xml]\n", stderr);
743          exit(1);
744   }
# Line 746 | Line 783 | main(int argc, char *argv[])
783                          }
784                          attr_unit = argv[i];
785                          continue;
786 +                case 'U':               /* unlink data files when done */
787 +                        unlink_datafiles = 1 + (argv[i][2] == 'U');
788 +                        continue;
789                  case 'a':               /* angle basis */
790                          if (++i >= argc)
791                                  UsageExit(argv[0]);
# Line 767 | Line 807 | main(int argc, char *argv[])
807                          else
808                                  UsageExit(argv[0]);
809                          continue;
810 +                case 'c':               /* correct solid angle */
811 +                        correct_solid_angle = 1;
812 +                        continue;
813 +                case 'C':               /* comment */
814 +                        if (ncomm >= MAXCOMM) {
815 +                                fprintf(stderr, "%s: too many comments\n",
816 +                                                argv[0]);
817 +                                return 1;
818 +                        }
819 +                        if (strchr(argv[++i], '>') != NULL) {
820 +                                fprintf(stderr, "%s: illegal character in comment\n",
821 +                                                argv[0]);
822 +                                return 1;
823 +                        }
824 +                        commlist[ncomm++] = argv[i];
825 +                        continue;
826                  case 't':               /* transmission */
827                          if (i >= argc-1)
828                                  UsageExit(argv[0]);
# Line 808 | Line 864 | main(int argc, char *argv[])
864                                  argv[i] = (char *)stdin_name;
865                          }
866                          data_file[ndataf].fname = argv[i];
867 <                        data_file[ndataf++].spectrum = cur_spectrum;
867 >                        data_file[ndataf].spectrum = cur_spectrum;
868 >                        ndataf++;
869                          continue;
870                  case 's':               /* spectrum name or input file */
871                          if (++i >= argc)
# Line 822 | Line 879 | main(int argc, char *argv[])
879                                  cur_spectrum = DSxbar31;
880                          else if (!strcasecmp(argv[i], "CIE-Z"))
881                                  cur_spectrum = DSzbar31;
882 +                        else if (!strcasecmp(argv[i], "CIE-u"))
883 +                                cur_spectrum = DSuprime;
884 +                        else if (!strcasecmp(argv[i], "CIE-v"))
885 +                                cur_spectrum = DSvprime;
886                          else if (!strcasecmp(argv[i], "NIR"))
887                                  cur_spectrum = DSnir;
888                          else {
# Line 870 | Line 931 | doneOptions:                                   /* get XML input */
931          } else {
932                  xml_input = argv[i];
933          }
873        if ((xml_input == win6_template) & (angle_basis == ABdefault))
874                angle_basis = ABklemsFull;
934                                                  /* wrap it! */
935          return !wrapBSDF(argv[0]);
936   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines